generated from spatie/package-skeleton-laravel
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathContent.php
199 lines (160 loc) · 6.01 KB
/
Content.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?php
namespace Tonysm\RichTextLaravel;
use DOMElement;
use DOMNode;
use Illuminate\Pipeline\Pipeline;
use Illuminate\Support\Collection;
use Tonysm\RichTextLaravel\Actions\FragmentByCanonicalizingAttachmentGalleries;
class Content
{
public Fragment $fragment;
private $cachedAttachments;
private $cachedAttachmentNodes;
private $cachedAttachmentGalleries;
private $cachedAttachmentGalleryNodes;
private $cachedGalleryAttachments;
private $cachedAttachables;
public static function fromStorage(?string $value = null): self
{
return new Content($value ?: '', ['canonicalize' => false]);
}
public static function toStorage(?string $value = null)
{
return static::fragmentByCanonicalizingContent($value ?: '')->toHtml();
}
public static function fragmentByCanonicalizingContent(string $content)
{
return (new Pipeline(app()))
->send($content)
->through([
Actions\FragmentByCanonicalizingAttachments::class,
Actions\FragmentByCanonicalizingAttachmentGalleries::class,
])
->thenReturn();
}
public function __construct($content, array $options = [])
{
if ($options['canonicalize'] ?? true) {
$this->fragment = static::fragmentByCanonicalizingContent($content);
} else {
$this->fragment = Fragment::wrap($content);
}
}
public function links(): Collection
{
return $this->fragment->findAll('//a[@href]')
->map(fn (DOMElement $node) => $node->getAttribute('href'))
->unique();
}
public function attachments(): Collection
{
return $this->cachedAttachments ??= $this->attachmentNodes()->map(fn (DOMElement $node) => (
$this->attachmentForNode($node)
));
}
public function attachmentGalleries(): Collection
{
return $this->cachedAttachmentGalleries ??= $this->attachmentGalleryNodes()->map(fn (DOMElement $node) => (
$this->attachmentGalleryForNode($node)
));
}
public function attachables(): Collection
{
return $this->cachedAttachables ??= $this->attachmentNodes()->map(fn (DOMElement $node) => (
AttachableFactory::fromNode($node)
));
}
public function galleryAttachments(): Collection
{
return $this->cachedGalleryAttachments ??= $this->attachmentGalleries()->flatMap(fn (AttachmentGallery $attachmentGallery) => $attachmentGallery->attachments());
}
public function renderAttachments(array $options, callable $callback): static
{
$content = $this->fragment->replace(Attachment::$SELECTOR, function (DOMNode $node) use ($options, $callback) {
return $callback($this->attachmentForNode($node, $options));
});
return new static($content, ['canonicalize' => false]);
}
public function toPlainText(): string
{
return $this->renderAttachments(
['withFullAttributes' => false],
fn (Attachment $item) => $item->toPlainText()
)->fragment->toPlainText();
}
public function toTrixHtml()
{
return $this->renderAttachments(
[],
fn (Attachment $attachment) => (HtmlConversion::fragmentForHtml($attachment->toTrixAttachment()->toHtml()))
)->toHtml();
}
public function toHtml(): string
{
return $this->renderAttachments([], fn (Attachment $attachment) => $attachment->toTrixAttachment())
->fragment->toHtml();
}
public function renderWithAttachments(): string
{
return $this->renderAttachments([], function (Attachment $attachment) {
// If this is a gallery attachment, we'll render it separately.
if ($this->galleryAttachments()->first(fn (Attachment $galleryAttachment) => $galleryAttachment->is($attachment))) {
return null;
}
return HtmlConversion::fragmentForHtml($this->renderAttachment($attachment, [
'in_gallery' => false,
]));
})->renderAttachmentGalleries(fn (AttachmentGallery $attachmentGallery) => (
$attachmentGallery->richTextRender()
))->fragment->toHtml();
}
public function renderAttachmentGalleries(callable $renderer): static
{
$content = (new FragmentByCanonicalizingAttachmentGalleries)->fragmentByReplacingAttachmentGalleryNodes($this->fragment, function (DOMElement $node) use ($renderer) {
return HtmlConversion::document($renderer($this->attachmentGalleryForNode($node)));
});
return new static($content, ['canonicalize' => false]);
}
public function renderAttachment(Attachment $attachment, array $locals = []): string
{
return $attachment->attachable->richTextRender(options: $locals);
}
public function render()
{
return view('rich-text-laravel::content', [
'content' => $this,
])->render();
}
public function raw(): string
{
return $this->fragment->toHtml();
}
public function isEmpty(): bool
{
return empty(trim($this->toHtml()));
}
public function __toString(): string
{
return $this->render();
}
private function attachmentNodes(): Collection
{
return $this->cachedAttachmentNodes ??= $this->fragment->findAll(Attachment::$SELECTOR);
}
private function attachmentGalleryNodes(): Collection
{
return $this->cachedAttachmentGalleryNodes ??= (new FragmentByCanonicalizingAttachmentGalleries)->findAttachmentGalleryNodes($this->fragment);
}
private function attachmentGalleryForNode(DOMElement $node)
{
return AttachmentGallery::fromNode($node);
}
private function attachmentForNode(DOMNode $node, array $options = []): Attachment
{
$attachment = Attachment::fromNode($node);
if ($options['withFullAttributes'] ?? false) {
return $attachment->withFullAttributes();
}
return $attachment;
}
}