Skip to content

Commit

Permalink
First revision of pluggable blocks
Browse files Browse the repository at this point in the history
See #19.
  • Loading branch information
Roelof Roos committed Nov 17, 2019
1 parent 8fa273e commit a179f0a
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
35 changes: 35 additions & 0 deletions resources/js/components/FormField.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions src/NovaEditorJs.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use EditorJS\EditorJS;
use EditorJS\EditorJSException;
use Illuminate\Support\Str;
use Laravel\Nova\Fields\Field;

class NovaEditorJs extends Field
Expand Down Expand Up @@ -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']);
}
}
}

Expand Down

0 comments on commit a179f0a

Please sign in to comment.