1
0
elk/composables/tiptap/suggestion.ts

102 lines
2.7 KiB
TypeScript
Raw Normal View History

import type { GetReferenceClientRect, Instance } from 'tippy.js'
import tippy from 'tippy.js'
import { VueRenderer } from '@tiptap/vue-3'
import type { SuggestionOptions } from '@tiptap/suggestion'
import { PluginKey } from 'prosemirror-state'
import type { Component } from 'vue'
import TiptapMentionList from '~/components/tiptap/TiptapMentionList.vue'
import TiptapHashtagList from '~/components/tiptap/TiptapHashtagList.vue'
export const MentionSuggestion: Partial<SuggestionOptions> = {
pluginKey: new PluginKey('mention'),
char: '@',
async items({ query }) {
if (query.length === 0)
return []
2023-01-15 17:38:02 +09:00
const results = await useMastoClient().v2.search({ q: query, type: 'accounts', limit: 25, resolve: true })
2023-01-13 01:21:27 +09:00
return results.accounts
},
render: createSuggestionRenderer(TiptapMentionList),
}
export const HashtagSuggestion: Partial<SuggestionOptions> = {
pluginKey: new PluginKey('hashtag'),
char: '#',
async items({ query }) {
if (query.length === 0)
return []
2023-01-15 17:38:02 +09:00
const results = await useMastoClient().v2.search({
q: query,
type: 'hashtags',
limit: 25,
resolve: false,
excludeUnreviewed: true,
})
2023-01-13 01:21:27 +09:00
return results.hashtags
},
render: createSuggestionRenderer(TiptapHashtagList),
}
function createSuggestionRenderer(component: Component): SuggestionOptions['render'] {
return () => {
let renderer: VueRenderer
let popup: Instance
return {
2022-11-27 15:54:56 +09:00
onStart(props) {
renderer = new VueRenderer(component, {
props,
editor: props.editor,
})
if (!props.clientRect)
return
popup = tippy(document.body, {
getReferenceClientRect: props.clientRect as GetReferenceClientRect,
appendTo: () => document.body,
content: renderer.element,
showOnCreate: true,
interactive: true,
trigger: 'manual',
placement: 'bottom-start',
})
},
2022-11-27 16:05:17 +09:00
// Use arrow function here because Nuxt will transform it incorrectly as Vue hook causing the build to fail
onBeforeUpdate: (props) => {
props.editor.isFocused && renderer.updateProps({ ...props, isPending: true })
},
onUpdate(props) {
if (!props.editor.isFocused)
return
renderer.updateProps({ ...props, isPending: false })
if (!props.clientRect)
return
popup?.setProps({
getReferenceClientRect: props.clientRect as GetReferenceClientRect,
})
},
onKeyDown(props) {
if (props.event.key === 'Escape') {
popup?.hide()
return true
}
return renderer?.ref?.onKeyDown(props.event)
},
onExit() {
popup?.destroy()
renderer?.destroy()
},
}
}
}