|
| 1 | +<script setup lang="ts"> |
| 2 | +import { Editor, EditorContent } from '@tiptap/vue-3' |
| 3 | +import StarterKit from '@tiptap/starter-kit' |
| 4 | +import { Markdown } from '@tiptap/markdown' |
| 5 | +
|
| 6 | +const modelValue = defineModel<string>({ default: '' }) |
| 7 | +
|
| 8 | +const editor = ref<Editor>() |
| 9 | +
|
| 10 | +onMounted(() => { |
| 11 | + editor.value = new Editor({ |
| 12 | + extensions: [StarterKit, Markdown], |
| 13 | + content: modelValue.value, |
| 14 | + onUpdate: ({ editor }) => { |
| 15 | + modelValue.value = editor.getMarkdown() |
| 16 | + }, |
| 17 | + }) |
| 18 | +}) |
| 19 | +
|
| 20 | +watch(modelValue, (value) => { |
| 21 | + const currentMarkdown = editor.value?.getMarkdown() |
| 22 | + if (value === currentMarkdown) return |
| 23 | + editor.value?.commands.setContent(value, { contentType: 'markdown' }) |
| 24 | +}) |
| 25 | +
|
| 26 | +onBeforeUnmount(() => { |
| 27 | + editor.value?.destroy() |
| 28 | +}) |
| 29 | +
|
| 30 | +const toolbarItems = [ |
| 31 | + { |
| 32 | + icon: 'i-lucide-bold', |
| 33 | + action: () => editor.value?.chain().focus().toggleBold().run(), |
| 34 | + isActive: () => editor.value?.isActive('bold'), |
| 35 | + title: 'Bold', |
| 36 | + }, |
| 37 | + { |
| 38 | + icon: 'i-lucide-italic', |
| 39 | + action: () => editor.value?.chain().focus().toggleItalic().run(), |
| 40 | + isActive: () => editor.value?.isActive('italic'), |
| 41 | + title: 'Italic', |
| 42 | + }, |
| 43 | + { |
| 44 | + icon: 'i-lucide-strikethrough', |
| 45 | + action: () => editor.value?.chain().focus().toggleStrike().run(), |
| 46 | + isActive: () => editor.value?.isActive('strike'), |
| 47 | + title: 'Strikethrough', |
| 48 | + }, |
| 49 | + { type: 'divider' }, |
| 50 | + { |
| 51 | + icon: 'i-lucide-heading-2', |
| 52 | + action: () => |
| 53 | + editor.value?.chain().focus().toggleHeading({ level: 2 }).run(), |
| 54 | + isActive: () => editor.value?.isActive('heading', { level: 2 }), |
| 55 | + title: 'Heading 2', |
| 56 | + }, |
| 57 | + { |
| 58 | + icon: 'i-lucide-heading-3', |
| 59 | + action: () => |
| 60 | + editor.value?.chain().focus().toggleHeading({ level: 3 }).run(), |
| 61 | + isActive: () => editor.value?.isActive('heading', { level: 3 }), |
| 62 | + title: 'Heading 3', |
| 63 | + }, |
| 64 | + { type: 'divider' }, |
| 65 | + { |
| 66 | + icon: 'i-lucide-list', |
| 67 | + action: () => editor.value?.chain().focus().toggleBulletList().run(), |
| 68 | + isActive: () => editor.value?.isActive('bulletList'), |
| 69 | + title: 'Bullet List', |
| 70 | + }, |
| 71 | + { |
| 72 | + icon: 'i-lucide-list-ordered', |
| 73 | + action: () => editor.value?.chain().focus().toggleOrderedList().run(), |
| 74 | + isActive: () => editor.value?.isActive('orderedList'), |
| 75 | + title: 'Ordered List', |
| 76 | + }, |
| 77 | + { type: 'divider' }, |
| 78 | + { |
| 79 | + icon: 'i-lucide-undo', |
| 80 | + action: () => editor.value?.chain().focus().undo().run(), |
| 81 | + isActive: () => false, |
| 82 | + disabled: () => !editor.value?.can().undo(), |
| 83 | + title: 'Undo', |
| 84 | + }, |
| 85 | + { |
| 86 | + icon: 'i-lucide-redo', |
| 87 | + action: () => editor.value?.chain().focus().redo().run(), |
| 88 | + isActive: () => false, |
| 89 | + disabled: () => !editor.value?.can().redo(), |
| 90 | + title: 'Redo', |
| 91 | + }, |
| 92 | +] |
| 93 | +</script> |
| 94 | + |
| 95 | +<template> |
| 96 | + <div class="border-accented overflow-hidden rounded-md border"> |
| 97 | + <div |
| 98 | + v-if="editor" |
| 99 | + class="bg-elevated border-accented flex flex-wrap gap-1 border-b p-1" |
| 100 | + > |
| 101 | + <template v-for="(item, index) in toolbarItems" :key="index"> |
| 102 | + <div |
| 103 | + v-if="item.type === 'divider'" |
| 104 | + class="bg-accented mx-1 w-px self-stretch" |
| 105 | + /> |
| 106 | + <UButton |
| 107 | + v-else |
| 108 | + :icon="item.icon" |
| 109 | + variant="ghost" |
| 110 | + size="xs" |
| 111 | + :color="item.isActive?.() ? 'primary' : 'neutral'" |
| 112 | + :disabled="item.disabled?.()" |
| 113 | + :title="item.title" |
| 114 | + @click=" |
| 115 | + () => { |
| 116 | + item.action?.() |
| 117 | + } |
| 118 | + " |
| 119 | + /> |
| 120 | + </template> |
| 121 | + </div> |
| 122 | + <EditorContent |
| 123 | + :editor |
| 124 | + class="prose prose-sm dark:prose-invert max-w-none p-3 focus:outline-none [&_.ProseMirror]:min-h-[150px] [&_.ProseMirror]:outline-none" |
| 125 | + /> |
| 126 | + </div> |
| 127 | +</template> |
0 commit comments