Skip to content

Commit

Permalink
working gap after insert of block
Browse files Browse the repository at this point in the history
  • Loading branch information
awcodes committed Nov 9, 2023
1 parent bf1da1c commit 399938b
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 58 deletions.
12 changes: 8 additions & 4 deletions resources/css/plugin.css
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@
width: 100%;
color: theme('colors.black');

&.ProseMirror-focused .ProseMirror-selectednode {
@apply outline-2 outline-offset-2 outline-dashed outline-gray-700 dark:outline-gray-300;
}

.tiptap-block-wrapper {
@apply rounded-md overflow-hidden bg-gray-100 dark:bg-gray-800;

Expand All @@ -62,14 +66,14 @@
}
}

.tiptap-block-actions {
@apply flex items-center gap-2;
}

.preview {
@apply p-4;
}
}

&.ProseMirror-selectednode {
@apply outline-2 outline-offset-2 outline-dashed outline-gray-700 dark:outline-gray-300;
}
}

.filament-tiptap-hurdle {
Expand Down
2 changes: 1 addition & 1 deletion resources/dist/filament-tiptap-editor.css

Large diffs are not rendered by default.

45 changes: 22 additions & 23 deletions resources/dist/filament-tiptap-editor.js

Large diffs are not rendered by default.

27 changes: 20 additions & 7 deletions resources/js/extensions/TiptapBlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import {mergeAttributes, Node} from "@tiptap/core"

export const TiptapBlock = Node.create({
name: 'tiptapBlock',
content: 'inline+',
content: 'inline',
group: 'block',
atom: true,
draggable: true,
selectable: true,
defining: true,
isolating: true,
allowGapCursor: true,
inline: false,
addAttributes() {
Expand Down Expand Up @@ -65,16 +65,16 @@ export const TiptapBlock = Node.create({
addNodeView() {
return ({node, extension, getPos, editor}) => {
const dom = document.createElement('div')
dom.contentEditable = 'false'
dom.classList.add('tiptap-block-wrapper')

dom.innerHTML = `
<div
x-data='{
openSettings() {
this.$dispatch("update-block", {
this.$dispatch("open-block-settings", {
type: "${node.attrs.type}",
data: JSON.parse(\`${JSON.stringify(node.attrs.data)}\`),
context: "update",
})
},
deleteBlock() {
Expand Down Expand Up @@ -115,11 +115,24 @@ export const TiptapBlock = Node.create({
},
addCommands() {
return {
insertBlock: (attributes) => ({ commands }) => {
insertBlock: (attributes) => ({ chain, state }) => {
const { $to: $originTo } = state.selection

const currentChain = chain()

if ($originTo.parentOffset === 0) {
currentChain.insertContentAt(Math.max($originTo.pos - 2, 0), { type: 'paragraph' })
} else {
currentChain.insertContent({ type: 'paragraph' })
}

return currentChain.setNode(this.name, attributes).insertContent({ type: 'paragraph' })
},
updateBlock: (attributes) => ({commands}) => {
return commands.setNode(this.name, attributes)
},
removeBlock: (attributes) => ({ commands }) => {
return commands.toggleNode(this.name, 'paragraph', attributes)
removeBlock: () => ({ commands }) => {
return commands.deleteSelection()
}
}
},
Expand Down
10 changes: 9 additions & 1 deletion resources/js/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -466,9 +466,17 @@ export default function tiptap({
label,
});
},
updateBlock(settings) {
openBlockSettings(settings) {
this.$wire.dispatchFormEvent("tiptap::updateBlock", statePath, settings);
},
updateBlock({type, data, preview, label}) {
this.editor().commands.updateBlock({
type,
data,
preview,
label,
});
},
deleteBlock() {
this.editor().commands.removeBlock();
}
Expand Down
2 changes: 1 addition & 1 deletion resources/views/components/dropdown-button.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class="text-[0.625rem] absolute top-0 right-0 font-mono text-gray-800 dark:text-
>
<div x-ref="arrow" class="absolute z-1 bg-inherit w-2 h-2 transform rotate-45"></div>
@if ($list)
<ul class="relative z-2 text-sm divide-y divide-gray-300 dark:divide-gray-700 min-w-[144px] text-gray-800 dark:text-white">
<ul class="relative z-2 text-sm divide-y rounded-md overflow-hidden divide-gray-300 dark:divide-gray-700 min-w-[144px] text-gray-800 dark:text-white">
{{ $slot }}
</ul>
@else
Expand Down
1 change: 1 addition & 0 deletions resources/views/tiptap-editor.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class="relative z-0 tiptap-wrapper rounded-md bg-white dark:bg-gray-900"
x-on:refresh-tiptap-editors.window="refreshEditorContent()"
x-on:insert-block.window="insertBlock($event.detail)"
x-on:update-block.window="updateBlock($event.detail)"
x-on:open-block-settings.window="openBlockSettings($event.detail)"
x-on:delete-block.window="deleteBlock($event.detail)"
x-trap.noscroll="fullScreenMode"
>
Expand Down
42 changes: 21 additions & 21 deletions src/TiptapEditor.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,26 +39,6 @@ class TiptapEditor extends Field

protected string $view = 'filament-tiptap-editor::tiptap-editor';

public function renderBlockPreviews(array $document): array
{
$content = $document['content'];

foreach ($content as $k => $block) {
if ($block['type'] === 'tiptapBlock') {
$instance = $this->getBlocks()[$block['attrs']['type']];
$preview = view($instance->preview, $block['attrs']['data'])->render();
$content[$k]['attrs']['preview'] = $preview;
$content[$k]['attrs']['label'] = $instance->getLabel();
} elseif (array_key_exists('content', $block)) {
$content[$k] = $this->renderBlockPreviews($block);
}
}

$document['content'] = $content;

return $document;
}

protected function setUp(): void
{
parent::setUp();
Expand Down Expand Up @@ -242,7 +222,7 @@ function(TiptapEditor $component) {
$preview = view(view: $block->preview, data: $data)->render();

$livewire->dispatch(
event: 'insert-block',
event: 'update-block',
type: $arguments['type'],
data: $data,
preview: $preview,
Expand All @@ -254,6 +234,26 @@ function(TiptapEditor $component) {
));
}

public function renderBlockPreviews(array $document): array
{
$content = $document['content'];

foreach ($content as $k => $block) {
if ($block['type'] === 'tiptapBlock') {
$instance = $this->getBlocks()[$block['attrs']['type']];
$preview = view($instance->preview, $block['attrs']['data'])->render();
$content[$k]['attrs']['preview'] = $preview;
$content[$k]['attrs']['label'] = $instance->getLabel();
} elseif (array_key_exists('content', $block)) {
$content[$k] = $this->renderBlockPreviews($block);
}
}

$document['content'] = $content;

return $document;
}

public function maxContentWidth(string | Closure $width): static
{
$this->maxContentWidth = $width;
Expand Down

0 comments on commit 399938b

Please sign in to comment.