-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathNovaEditorJsField.php
86 lines (70 loc) · 2.64 KB
/
NovaEditorJsField.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
<?php
declare(strict_types=1);
namespace Advoor\NovaEditorJs;
use JsonException;
use Laravel\Nova\Fields\Field;
class NovaEditorJsField extends Field
{
/**
* The field's component.
*
* @var string
*/
public $component = 'nova-editor-js';
public function __construct($name, $attribute = null, ?callable $resolveCallback = null)
{
parent::__construct($name, $attribute, $resolveCallback);
// DEPRECATED: Simplify to `nova-editor-js.editorSettings.defaultBlock` in v4.0.0
$defaultBlock = config('nova-editor-js.editorSettings.initialBlock', config('nova-editor-js.editorSettings.defaultBlock', 'paragraph'));
$this->withMeta([
'editorSettings' => [
'placeholder' => config('nova-editor-js.editorSettings.placeholder', ''),
'defaultBlock' => $defaultBlock,
'autofocus' => config('nova-editor-js.editorSettings.autofocus', false),
'rtl' => config('nova-editor-js.editorSettings.rtl', false),
],
'toolSettings' => config('nova-editor-js.toolSettings'),
'uploadImageByFileEndpoint' => route('editor-js-upload-image-by-file'),
'uploadImageByUrlEndpoint' => route('editor-js-upload-image-by-url'),
'fetchUrlEndpoint' => route('editor-js-fetch-url'),
]);
}
/**
* Resolve the field's value for display.
*
* @param string|null $attribute
*
* @throws \Throwable
*/
public function resolveForDisplay($resource, $attribute = null): void
{
$attribute = $attribute ?? $this->attribute;
if ($attribute === 'ComputedField') {
return;
}
$value = data_get($resource, str_replace('->', '.', $attribute), $placeholder = new \stdClass());
if (is_callable($this->resolveCallback)) {
$value = call_user_func($this->resolveCallback, $value, $resource, $attribute);
}
if (! $this->displayCallback) {
$this->withMeta(['asHtml' => true]);
$this->value = (string) NovaEditorJs::generateHtmlOutput($value);
return;
}
if (! is_callable($this->displayCallback) || $value === $placeholder) {
return;
}
// Convert from JSON
if (is_string($value)) {
try {
$value = json_decode($value, true, 512, JSON_THROW_ON_ERROR);
} catch (JsonException) {
//
}
}
if ($value !== null) {
$value = new NovaEditorJsData($value);
}
$this->value = call_user_func($this->displayCallback, $value);
}
}