2022-11-25 22:21:02 +09: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'
|
2023-01-05 05:47:29 +09:00
|
|
|
import type { Component } from 'vue'
|
2022-11-25 22:21:02 +09:00
|
|
|
import TiptapMentionList from '~/components/tiptap/TiptapMentionList.vue'
|
2023-01-05 05:47:29 +09:00
|
|
|
import TiptapHashtagList from '~/components/tiptap/TiptapHashtagList.vue'
|
2022-11-25 22:21:02 +09:00
|
|
|
|
|
|
|
export const MentionSuggestion: Partial<SuggestionOptions> = {
|
|
|
|
pluginKey: new PluginKey('mention'),
|
|
|
|
char: '@',
|
2022-11-26 15:55:54 +09:00
|
|
|
async items({ query }) {
|
|
|
|
if (query.length === 0)
|
|
|
|
return []
|
|
|
|
|
2023-01-13 01:21:27 +09:00
|
|
|
const results = await useMasto().v2.search({ q: query, type: 'accounts', limit: 25, resolve: true })
|
|
|
|
return results.accounts
|
2022-11-25 22:21:02 +09:00
|
|
|
},
|
2023-01-05 05:47:29 +09:00
|
|
|
render: createSuggestionRenderer(TiptapMentionList),
|
2022-11-25 22:21:02 +09:00
|
|
|
}
|
|
|
|
|
2023-01-05 05:47:29 +09:00
|
|
|
export const HashtagSuggestion: Partial<SuggestionOptions> = {
|
2022-11-25 22:21:02 +09:00
|
|
|
pluginKey: new PluginKey('hashtag'),
|
|
|
|
char: '#',
|
2023-01-05 05:47:29 +09:00
|
|
|
async items({ query }) {
|
|
|
|
if (query.length === 0)
|
|
|
|
return []
|
|
|
|
|
2023-01-13 01:21:27 +09:00
|
|
|
const results = await useMasto().v2.search({
|
2023-01-08 22:34:53 +09:00
|
|
|
q: query,
|
|
|
|
type: 'hashtags',
|
|
|
|
limit: 25,
|
|
|
|
resolve: false,
|
|
|
|
excludeUnreviewed: true,
|
|
|
|
})
|
2023-01-13 01:21:27 +09:00
|
|
|
return results.hashtags
|
2022-11-25 22:21:02 +09:00
|
|
|
},
|
2023-01-05 05:47:29 +09:00
|
|
|
render: createSuggestionRenderer(TiptapHashtagList),
|
2022-11-25 22:21:02 +09:00
|
|
|
}
|
|
|
|
|
2023-01-05 05:47:29 +09:00
|
|
|
function createSuggestionRenderer(component: Component): SuggestionOptions['render'] {
|
2022-11-25 22:21:02 +09:00
|
|
|
return () => {
|
2023-01-05 05:47:29 +09:00
|
|
|
let renderer: VueRenderer
|
2022-11-25 22:21:02 +09:00
|
|
|
let popup: Instance
|
|
|
|
|
|
|
|
return {
|
2022-11-27 15:54:56 +09:00
|
|
|
onStart(props) {
|
2023-01-05 05:47:29 +09:00
|
|
|
renderer = new VueRenderer(component, {
|
2022-11-25 22:21:02 +09:00
|
|
|
props,
|
|
|
|
editor: props.editor,
|
|
|
|
})
|
|
|
|
|
|
|
|
if (!props.clientRect)
|
|
|
|
return
|
|
|
|
|
|
|
|
popup = tippy(document.body, {
|
|
|
|
getReferenceClientRect: props.clientRect as GetReferenceClientRect,
|
|
|
|
appendTo: () => document.body,
|
2023-01-05 05:47:29 +09:00
|
|
|
content: renderer.element,
|
2022-11-25 22:21:02 +09:00
|
|
|
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) => {
|
2023-01-14 19:38:31 +09:00
|
|
|
props.editor.isFocused && renderer.updateProps({ ...props, isPending: true })
|
2022-11-27 15:34:38 +09:00
|
|
|
},
|
|
|
|
|
2022-11-25 22:21:02 +09:00
|
|
|
onUpdate(props) {
|
2023-01-14 19:38:31 +09:00
|
|
|
if (!props.editor.isFocused)
|
|
|
|
return
|
|
|
|
|
2023-01-05 05:47:29 +09:00
|
|
|
renderer.updateProps({ ...props, isPending: false })
|
2022-11-25 22:21:02 +09:00
|
|
|
|
|
|
|
if (!props.clientRect)
|
|
|
|
return
|
|
|
|
|
|
|
|
popup?.setProps({
|
|
|
|
getReferenceClientRect: props.clientRect as GetReferenceClientRect,
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
onKeyDown(props) {
|
|
|
|
if (props.event.key === 'Escape') {
|
|
|
|
popup?.hide()
|
|
|
|
return true
|
|
|
|
}
|
2023-01-05 05:47:29 +09:00
|
|
|
return renderer?.ref?.onKeyDown(props.event)
|
2022-11-25 22:21:02 +09:00
|
|
|
},
|
|
|
|
|
|
|
|
onExit() {
|
|
|
|
popup?.destroy()
|
2023-01-05 05:47:29 +09:00
|
|
|
renderer?.destroy()
|
2022-11-25 22:21:02 +09:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|