2022-11-25 13:21:02 +00:00
|
|
|
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 TiptapMentionList from '~/components/tiptap/TiptapMentionList.vue'
|
|
|
|
|
|
|
|
export const MentionSuggestion: Partial<SuggestionOptions> = {
|
|
|
|
pluginKey: new PluginKey('mention'),
|
|
|
|
char: '@',
|
2022-11-26 06:55:54 +00:00
|
|
|
async items({ query }) {
|
|
|
|
if (query.length === 0)
|
|
|
|
return []
|
|
|
|
|
2022-11-26 15:42:58 +00:00
|
|
|
const mentionPaginator = useMasto().search({ q: query, type: 'accounts', limit: 25, resolve: true })
|
2022-11-26 06:55:54 +00:00
|
|
|
const results = await mentionPaginator.next()
|
|
|
|
|
|
|
|
return results.value.accounts
|
2022-11-25 13:21:02 +00:00
|
|
|
},
|
|
|
|
render: createSuggestionRenderer(),
|
|
|
|
}
|
|
|
|
|
|
|
|
export const HashSuggestion: Partial<SuggestionOptions> = {
|
|
|
|
pluginKey: new PluginKey('hashtag'),
|
|
|
|
char: '#',
|
|
|
|
items({ query }) {
|
|
|
|
// TODO: query
|
|
|
|
return [
|
|
|
|
'TODO HASH QUERY',
|
|
|
|
].filter(item => item.toLowerCase().startsWith(query.toLowerCase())).slice(0, 5)
|
|
|
|
},
|
|
|
|
render: createSuggestionRenderer(),
|
|
|
|
}
|
|
|
|
|
|
|
|
function createSuggestionRenderer(): SuggestionOptions['render'] {
|
|
|
|
return () => {
|
|
|
|
let component: VueRenderer
|
|
|
|
let popup: Instance
|
|
|
|
|
|
|
|
return {
|
2022-11-27 06:54:56 +00:00
|
|
|
onStart(props) {
|
2022-11-25 13:21:02 +00:00
|
|
|
component = new VueRenderer(TiptapMentionList, {
|
|
|
|
props,
|
|
|
|
editor: props.editor,
|
|
|
|
})
|
|
|
|
|
|
|
|
if (!props.clientRect)
|
|
|
|
return
|
|
|
|
|
|
|
|
popup = tippy(document.body, {
|
|
|
|
getReferenceClientRect: props.clientRect as GetReferenceClientRect,
|
|
|
|
appendTo: () => document.body,
|
|
|
|
content: component.element,
|
|
|
|
showOnCreate: true,
|
|
|
|
interactive: true,
|
|
|
|
trigger: 'manual',
|
|
|
|
placement: 'bottom-start',
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
2022-11-27 07:05:17 +00:00
|
|
|
// Use arrow function here because Nuxt will transform it incorrectly as Vue hook causing the build to fail
|
|
|
|
onBeforeUpdate: (props) => {
|
2022-11-27 06:34:38 +00:00
|
|
|
component.updateProps({ ...props, isPending: true })
|
|
|
|
},
|
|
|
|
|
2022-11-25 13:21:02 +00:00
|
|
|
onUpdate(props) {
|
2022-11-27 06:34:38 +00:00
|
|
|
component.updateProps({ ...props, isPending: false })
|
2022-11-25 13:21:02 +00:00
|
|
|
|
|
|
|
if (!props.clientRect)
|
|
|
|
return
|
|
|
|
|
|
|
|
popup?.setProps({
|
|
|
|
getReferenceClientRect: props.clientRect as GetReferenceClientRect,
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
onKeyDown(props) {
|
|
|
|
if (props.event.key === 'Escape') {
|
|
|
|
popup?.hide()
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return component?.ref?.onKeyDown(props.event)
|
|
|
|
},
|
|
|
|
|
|
|
|
onExit() {
|
|
|
|
popup?.destroy()
|
|
|
|
component?.destroy()
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|