diff --git a/composer.json b/composer.json index 6ef9080..f734d35 100644 --- a/composer.json +++ b/composer.json @@ -14,6 +14,7 @@ "ext-exif": "*", "ext-json": "*", "codex-team/editor.js": "*", + "illuminate/support": "^5.7.15 | ^6.0", "laravel/nova": "^2.0", "spatie/image": "^1.7" }, diff --git a/resources/js/components/FormField.vue b/resources/js/components/FormField.vue index 5596c0d..ea26fd8 100644 --- a/resources/js/components/FormField.vue +++ b/resources/js/components/FormField.vue @@ -171,6 +171,38 @@ } } + /** + * Applies filters listening to the given name, returns changed data. + * + * @param {String} name + * @param {Object} data + */ + function applyGlobalFilter(name, data) { + // Check for filters + if (!window.hasOwnProperty('novaEditorjsFields') || !window.novaEditorjsFields || typeof window.novaEditorjsFields !== 'object') { + return data + } + + const editorJsFields = window.novaEditorjsFields + + // Check for this filter + if (!editorJsFields.hasOwnProperty(name) || !Array.isArray(editorJsFields[name])) { + return data + } + + // Apply filter + editorJsFields[name].forEach(entry => { + try { + data = entry(data) + } catch (error) { + console.error('Recieved hook error on %s: %o. Tried to issue %o with %o.', name, error, entry, data) + } + }) + + // Return updated data + return data + } + export default { mixins: [FormField, HandlesValidationErrors], @@ -201,6 +233,9 @@ setRawToolSettings(self, tools); setEmbedToolSettings(self, tools); + // Allow plugins to add or change filters + tools = applyGlobalFilter('tools', tools) + var editor = new EditorJS({ /** * Wrapper of Editor diff --git a/src/NovaEditorJs.php b/src/NovaEditorJs.php index 56bec7e..fdef6ed 100644 --- a/src/NovaEditorJs.php +++ b/src/NovaEditorJs.php @@ -4,6 +4,7 @@ use EditorJS\EditorJS; use EditorJS\EditorJSException; +use Illuminate\Support\Str; use Laravel\Nova\Fields\Field; class NovaEditorJs extends Field @@ -127,6 +128,11 @@ public static function generateHtmlOutput($jsonData): string case 'embed': $htmlOutput .= view('nova-editor-js::embed', $block['data'])->render(); break; + default: + $methodName = 'render' . ucfirst($block['type']) . 'Field'; + if ($this->hasMacro($methodName)) { + $htmlOutput .= $this->$methodName($block['data']); + } } }