1
0
Fork 1
mirror of https://github.com/elk-zone/elk.git synced 2024-11-12 20:09:59 +00:00
elk/composables/tiptap.ts

118 lines
2.8 KiB
TypeScript
Raw Normal View History

2022-12-13 13:02:43 +00:00
import { Extension, useEditor } from '@tiptap/vue-3'
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 02:01:50 +00: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'
import { Plugin } from 'prosemirror-state'
import type { Ref } from 'vue'
import { HashSuggestion, MentionSuggestion } from './tiptap/suggestion'
2022-12-13 13:02:43 +00:00
import { CodeBlockShiki } from './tiptap/shiki'
2022-12-27 19:13:50 +00:00
import { CustomEmoji } from './tiptap/custom-emoji'
import { Emoji } from './tiptap/emoji'
export interface UseTiptapOptions {
content: Ref<string | undefined>
2022-11-29 22:47:24 +00:00
placeholder: Ref<string | undefined>
2022-11-25 14:07:31 +00:00
onSubmit: () => void
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 02:01:50 +00:00
HardBreak,
Bold,
Italic,
Code,
Text,
Emoji,
2022-12-27 19:13:50 +00:00
CustomEmoji.configure({
inline: true,
HTMLAttributes: {
class: 'custom-emoji',
},
}),
Mention.configure({
suggestion: MentionSuggestion,
}),
Mention
.extend({ name: 'hastag' })
.configure({
suggestion: HashSuggestion,
}),
Placeholder.configure({
2022-11-29 22:47:24 +00:00
placeholder: placeholder.value,
}),
CharacterCount.configure({
limit: characterLimit.value,
}),
2022-12-13 13:02:43 +00:00
CodeBlockShiki,
Extension.create({
name: 'api',
addKeyboardShortcuts() {
return {
'Mod-Enter': () => {
2022-11-25 14:07:31 +00:00
options.onSubmit()
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-25 18:10:17 +00:00
watch(content, (value) => {
if (editor.value?.getHTML() === value)
return
editor.value?.commands.setContent(value || '', false)
})
return {
editor,
}
}