Created
March 29, 2023 14:24
-
-
Save awcodes/fb0425c4a2afd0fc8c3318784d9a53fb to your computer and use it in GitHub Desktop.
Custom Tiptap Editor Extensions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Config file | |
... | |
'extensions' => [ | |
[ | |
'id' => 'hurdle', | |
'name' => 'Hurdle', | |
'view' => 'tools.hurdle', | |
'source' => 'resources/js/tools/hurdle.js', | |
'builder' => 'vite', | |
] | |
], | |
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@php | |
$colors = [ | |
'gray_light' => 'Gray - Light', | |
'gray' => 'Gray', | |
'gray_dark' => 'Gray - Dark', | |
'primary' => 'Primary', | |
'secondary' => 'Secondary', | |
'tertiary' => 'Tertiary', | |
'accent' => 'Accent', | |
]; | |
@endphp | |
<x-filament-tiptap-editor::dropdown-button | |
label="Hurdle" | |
active="'hurdle'" | |
icon="hurdle" | |
> | |
@foreach($colors as $key => $label) | |
<x-filament-tiptap-editor::dropdown-button-item | |
action="editor().chain().focus().setHurdle({ color: '{{ $key }}' }).run()" | |
> | |
{{ $label }} | |
</x-filament-tiptap-editor::dropdown-button-item> | |
@endforeach | |
</x-filament-tiptap-editor::dropdown-button> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Node, mergeAttributes } from "@tiptap/core"; | |
const Hurdle = Node.create({ | |
name: "hurdle", | |
group: "block", | |
content: "block+", | |
addOptions() { | |
return { | |
colors: [ | |
"gray_light", | |
"gray", | |
"gray_dark", | |
"primary", | |
"secondary", | |
"tertiary", | |
"accent", | |
], | |
HTMLAttributes: { | |
class: "filament-tiptap-hurdle", | |
}, | |
}; | |
}, | |
addAttributes() { | |
return { | |
color: { | |
default: "gray", | |
parseHTML: (element) => element.getAttribute("data-color"), | |
renderHTML: (attributes) => { | |
return { | |
"data-color": attributes.color, | |
}; | |
}, | |
}, | |
}; | |
}, | |
parseHTML() { | |
return [ | |
{ | |
tag: "div", | |
getAttrs: (element) => | |
element.classList.contains("filament-tiptap-hurdle"), | |
}, | |
]; | |
}, | |
renderHTML({ node, HTMLAttributes }) { | |
return [ | |
"div", | |
mergeAttributes(this.options.HTMLAttributes, HTMLAttributes), | |
0, | |
]; | |
}, | |
addCommands() { | |
return { | |
setHurdle: | |
(attributes) => | |
({ commands }) => { | |
if (!this.options.colors.includes(attributes.color)) { | |
return false; | |
} | |
return commands.toggleWrap(this.name, attributes); | |
}, | |
}; | |
}, | |
}); | |
document.addEventListener("tiptapeditor:init", () => { | |
window.registerTiptapEditorExtension('hurdle', Hurdle) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Solved it.
the hurdle.blade.php needs to be inside
resources/views/Components/tools/