2022-12-13 22:02:43 +09:00
|
|
|
import { Extension, useEditor } from '@tiptap/vue-3'
|
2022-11-25 22:21:02 +09:00
|
|
|
import Placeholder from '@tiptap/extension-placeholder'
|
|
|
|
import Document from '@tiptap/extension-document'
|
|
|
|
import Paragraph from '@tiptap/extension-paragraph'
|
|
|
|
import Text from '@tiptap/extension-text'
|
|
|
|
import Mention from '@tiptap/extension-mention'
|
|
|
|
import CharacterCount from '@tiptap/extension-character-count'
|
2022-11-26 11:01:50 +09:00
|
|
|
import HardBreak from '@tiptap/extension-hard-break'
|
|
|
|
import Bold from '@tiptap/extension-bold'
|
|
|
|
import Italic from '@tiptap/extension-italic'
|
|
|
|
import Code from '@tiptap/extension-code'
|
2022-11-25 22:21:02 +09:00
|
|
|
import { Plugin } from 'prosemirror-state'
|
|
|
|
|
|
|
|
import type { Ref } from 'vue'
|
|
|
|
import { HashSuggestion, MentionSuggestion } from './tiptap/suggestion'
|
2022-12-13 22:02:43 +09:00
|
|
|
import { CodeBlockShiki } from './tiptap/shiki'
|
2022-12-28 04:13:50 +09:00
|
|
|
import { CustomEmoji } from './tiptap/custom-emoji'
|
2022-12-28 03:38:57 +09:00
|
|
|
import { Emoji } from './tiptap/emoji'
|
2022-11-25 22:21:02 +09:00
|
|
|
|
|
|
|
export interface UseTiptapOptions {
|
|
|
|
content: Ref<string | undefined>
|
2022-11-30 07:47:24 +09:00
|
|
|
placeholder: Ref<string | undefined>
|
2022-11-25 23:07:31 +09:00
|
|
|
onSubmit: () => void
|
2022-11-25 22:21:02 +09:00
|
|
|
onFocus: () => void
|
|
|
|
onPaste: (event: ClipboardEvent) => void
|
|
|
|
autofocus: boolean
|
|
|
|
}
|
|
|
|
|
|
|
|
export function useTiptap(options: UseTiptapOptions) {
|
|
|
|
const {
|
|
|
|
autofocus,
|
|
|
|
content,
|
|
|
|
placeholder,
|
|
|
|
} = options
|
|
|
|
|
|
|
|
const editor = useEditor({
|
|
|
|
content: content.value,
|
|
|
|
extensions: [
|
|
|
|
Document,
|
|
|
|
Paragraph,
|
2022-11-26 11:01:50 +09:00
|
|
|
HardBreak,
|
|
|
|
Bold,
|
|
|
|
Italic,
|
|
|
|
Code,
|
2022-11-25 22:21:02 +09:00
|
|
|
Text,
|
2022-12-28 03:38:57 +09:00
|
|
|
Emoji,
|
2022-12-28 04:13:50 +09:00
|
|
|
CustomEmoji.configure({
|
2022-12-28 03:38:57 +09:00
|
|
|
inline: true,
|
|
|
|
HTMLAttributes: {
|
|
|
|
class: 'custom-emoji',
|
|
|
|
},
|
|
|
|
}),
|
2022-11-25 22:21:02 +09:00
|
|
|
Mention.configure({
|
|
|
|
suggestion: MentionSuggestion,
|
|
|
|
}),
|
2022-11-26 15:55:54 +09:00
|
|
|
Mention
|
|
|
|
.extend({ name: 'hastag' })
|
|
|
|
.configure({
|
|
|
|
suggestion: HashSuggestion,
|
|
|
|
}),
|
2022-11-25 22:21:02 +09:00
|
|
|
Placeholder.configure({
|
2022-11-30 07:47:24 +09:00
|
|
|
placeholder: placeholder.value,
|
2022-11-25 22:21:02 +09:00
|
|
|
}),
|
|
|
|
CharacterCount.configure({
|
2022-11-25 23:01:28 +09:00
|
|
|
limit: characterLimit.value,
|
2022-11-25 22:21:02 +09:00
|
|
|
}),
|
2022-12-13 22:02:43 +09:00
|
|
|
CodeBlockShiki,
|
2022-11-25 22:21:02 +09:00
|
|
|
Extension.create({
|
|
|
|
name: 'api',
|
|
|
|
addKeyboardShortcuts() {
|
|
|
|
return {
|
|
|
|
'Mod-Enter': () => {
|
2022-11-25 23:07:31 +09:00
|
|
|
options.onSubmit()
|
2022-11-25 22:21:02 +09:00
|
|
|
return true
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
onFocus() {
|
|
|
|
options.onFocus()
|
|
|
|
},
|
|
|
|
addProseMirrorPlugins() {
|
|
|
|
return [
|
|
|
|
new Plugin({
|
|
|
|
props: {
|
|
|
|
handleDOMEvents: {
|
|
|
|
paste(view, event) {
|
|
|
|
options.onPaste(event)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
]
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
onUpdate({ editor }) {
|
|
|
|
content.value = editor.getHTML()
|
|
|
|
},
|
|
|
|
editorProps: {
|
|
|
|
attributes: {
|
|
|
|
class: 'content-editor content-rich',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
autofocus,
|
|
|
|
editable: true,
|
|
|
|
})
|
|
|
|
|
2022-11-26 03:10:17 +09:00
|
|
|
watch(content, (value) => {
|
|
|
|
if (editor.value?.getHTML() === value)
|
|
|
|
return
|
|
|
|
editor.value?.commands.setContent(value || '', false)
|
|
|
|
})
|
|
|
|
|
2022-11-25 22:21:02 +09:00
|
|
|
return {
|
|
|
|
editor,
|
|
|
|
}
|
|
|
|
}
|