From f24038cb2db1df40e2ba810d39c75a584f01d28d Mon Sep 17 00:00:00 2001 From: Seviche Date: Sat, 1 Apr 2023 19:11:42 +0800 Subject: [PATCH] feat: add draggable block --- components.d.ts | 2 + src/components/Tiptap/DragableHandle.vue | 56 ++++++++++++++++++++++++ src/components/Tiptap/DraggableItem.js | 34 ++++++++++++++ src/components/Tiptap/Editor.vue | 33 +++++++------- 4 files changed, 108 insertions(+), 17 deletions(-) create mode 100644 src/components/Tiptap/DragableHandle.vue create mode 100644 src/components/Tiptap/DraggableItem.js diff --git a/components.d.ts b/components.d.ts index daef8e5..b8722cd 100644 --- a/components.d.ts +++ b/components.d.ts @@ -11,6 +11,8 @@ declare module '@vue/runtime-core' { export interface GlobalComponents { BubbleMenu: typeof import('./src/components/Tiptap/BubbleMenu.vue')['default'] CommandsList: typeof import('./src/components/Tiptap/CommandsList.vue')['default'] + DragableHandle: typeof import('./src/components/Tiptap/DragableHandle.vue')['default'] + DragableNode: typeof import('./src/components/Tiptap/DragableNode.vue')['default'] Editor: typeof import('./src/components/Tiptap/Editor.vue')['default'] } } diff --git a/src/components/Tiptap/DragableHandle.vue b/src/components/Tiptap/DragableHandle.vue new file mode 100644 index 0000000..335dace --- /dev/null +++ b/src/components/Tiptap/DragableHandle.vue @@ -0,0 +1,56 @@ + + + + + diff --git a/src/components/Tiptap/DraggableItem.js b/src/components/Tiptap/DraggableItem.js new file mode 100644 index 0000000..6a99647 --- /dev/null +++ b/src/components/Tiptap/DraggableItem.js @@ -0,0 +1,34 @@ +import { Node, mergeAttributes } from '@tiptap/core' +import { VueNodeViewRenderer } from '@tiptap/vue-3' + +import DragableHandle from './DragableHandle.vue' + +export default Node.create({ + name: 'draggableItem', + + group: 'block', + + content: 'block+', + + draggable: true, + + parseHTML() { + return [ + { + tag: 'div[data-type="draggable-item"]', + }, + ] + }, + + renderHTML({ HTMLAttributes }) { + return [ + 'div', + mergeAttributes(HTMLAttributes, { 'data-type': 'draggable-item' }), + 0, + ] + }, + + addNodeView() { + return VueNodeViewRenderer(DragableHandle) + }, +}) diff --git a/src/components/Tiptap/Editor.vue b/src/components/Tiptap/Editor.vue index c8c4166..a91f956 100644 --- a/src/components/Tiptap/Editor.vue +++ b/src/components/Tiptap/Editor.vue @@ -22,6 +22,7 @@ import rust from 'highlight.js/lib/languages/rust' import suggestion from './suggestion' import Commands from './commands' +import DraggableItem from './DraggableItem' lowlight.registerLanguage('html', html) lowlight.registerLanguage('css', css) @@ -36,26 +37,24 @@ lowlight.registerLanguage('go', go) lowlight.registerLanguage('rs', rust) const editor = useEditor({ - content: `

- That’s a boring paragraph followed by a fenced code block: -

-
for (var i=1; i <= 20; i++)
-{
-  if (i % 15 == 0)
-    console.log("FizzBuzz");
-  else if (i % 3 == 0)
-    console.log("Fizz");
-  else if (i % 5 == 0)
-    console.log("Buzz");
-  else
-    console.log(i);
-}
-

- Press Command/Ctrl + Enter to leave the fenced code block and continue typing in boring paragraphs. -

`, + content: `

This is a boring paragraph.

+
+

Followed by a fancy draggable item.

+
+
+

And another draggable item.

+
+

And a nested one.

+
+

But can we go deeper?

+
+
+
+

Let’s finish with a boring paragraph.

`, extensions: [ StarterKit, Typography, + DraggableItem, Link.configure({ openOnClick: false, }),