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'
|
2023-01-04 20:47:29 +00:00
|
|
|
import type { Component } from 'vue'
|
2023-01-16 10:22:26 +00:00
|
|
|
import type { Emoji, EmojiMartData } from '@emoji-mart/data'
|
|
|
|
import type { mastodon } from 'masto'
|
|
|
|
import { currentCustomEmojis, updateCustomEmojis } from '~/composables/emojis'
|
2022-11-25 13:21:02 +00:00
|
|
|
import TiptapMentionList from '~/components/tiptap/TiptapMentionList.vue'
|
2023-01-04 20:47:29 +00:00
|
|
|
import TiptapHashtagList from '~/components/tiptap/TiptapHashtagList.vue'
|
2023-01-16 10:22:26 +00:00
|
|
|
import TiptapEmojiList from '~/components/tiptap/TiptapEmojiList.vue'
|
|
|
|
|
2023-10-02 23:09:18 +00:00
|
|
|
export type { Emoji }
|
2023-01-16 10:22:26 +00:00
|
|
|
|
|
|
|
export type CustomEmoji = (mastodon.v1.CustomEmoji & { custom: true })
|
2023-03-30 19:01:24 +00:00
|
|
|
export function isCustomEmoji(emoji: CustomEmoji | Emoji): emoji is CustomEmoji {
|
|
|
|
return !!(emoji as CustomEmoji).custom
|
|
|
|
}
|
2022-11-25 13:21:02 +00:00
|
|
|
|
2024-02-24 16:46:14 +00:00
|
|
|
export const TiptapMentionSuggestion: Partial<SuggestionOptions> = import.meta.server
|
2023-02-06 09:34:50 +00:00
|
|
|
? {}
|
|
|
|
: {
|
|
|
|
pluginKey: new PluginKey('mention'),
|
|
|
|
char: '@',
|
|
|
|
async items({ query }) {
|
|
|
|
if (query.length === 0)
|
|
|
|
return []
|
2022-11-26 06:55:54 +00:00
|
|
|
|
2024-01-09 08:56:15 +00:00
|
|
|
const paginator = useMastoClient().v2.search.list({ q: query, type: 'accounts', limit: 25, resolve: true })
|
|
|
|
return (await paginator.next()).value?.accounts ?? []
|
2023-02-06 09:34:50 +00:00
|
|
|
},
|
|
|
|
render: createSuggestionRenderer(TiptapMentionList),
|
|
|
|
}
|
2022-11-25 13:21:02 +00:00
|
|
|
|
2023-01-16 11:40:47 +00:00
|
|
|
export const TiptapHashtagSuggestion: Partial<SuggestionOptions> = {
|
2022-11-25 13:21:02 +00:00
|
|
|
pluginKey: new PluginKey('hashtag'),
|
|
|
|
char: '#',
|
2023-01-04 20:47:29 +00:00
|
|
|
async items({ query }) {
|
|
|
|
if (query.length === 0)
|
|
|
|
return []
|
|
|
|
|
2024-01-09 08:56:15 +00:00
|
|
|
const paginator = useMastoClient().v2.search.list({
|
2023-01-08 13:34:53 +00:00
|
|
|
q: query,
|
|
|
|
type: 'hashtags',
|
|
|
|
limit: 25,
|
|
|
|
resolve: false,
|
|
|
|
excludeUnreviewed: true,
|
|
|
|
})
|
2024-01-09 08:56:15 +00:00
|
|
|
return (await paginator.next()).value?.hashtags ?? []
|
2022-11-25 13:21:02 +00:00
|
|
|
},
|
2023-01-04 20:47:29 +00:00
|
|
|
render: createSuggestionRenderer(TiptapHashtagList),
|
2022-11-25 13:21:02 +00:00
|
|
|
}
|
|
|
|
|
2023-01-16 11:40:47 +00:00
|
|
|
export const TiptapEmojiSuggestion: Partial<SuggestionOptions> = {
|
2023-01-16 10:22:26 +00:00
|
|
|
pluginKey: new PluginKey('emoji'),
|
|
|
|
char: ':',
|
|
|
|
async items({ query }): Promise<(CustomEmoji | Emoji)[]> {
|
2024-02-24 16:46:14 +00:00
|
|
|
if (import.meta.server || query.length === 0)
|
2023-01-16 10:22:26 +00:00
|
|
|
return []
|
|
|
|
|
|
|
|
if (currentCustomEmojis.value.emojis.length === 0)
|
|
|
|
await updateCustomEmojis()
|
|
|
|
|
2024-02-19 09:23:58 +00:00
|
|
|
const lowerCaseQuery = query.toLowerCase()
|
|
|
|
|
|
|
|
const { data } = await useAsyncData<EmojiMartData>('emoji-data', () => import('@emoji-mart/data').then(r => r.default as EmojiMartData))
|
|
|
|
const emojis: Emoji[] = Object.values(data.value?.emojis || []).filter(({ id }) => id.toLowerCase().startsWith(lowerCaseQuery))
|
2023-01-16 10:22:26 +00:00
|
|
|
|
|
|
|
const customEmojis: CustomEmoji[] = currentCustomEmojis.value.emojis
|
2024-02-19 09:23:58 +00:00
|
|
|
.filter(emoji => emoji.shortcode.toLowerCase().startsWith(lowerCaseQuery))
|
2023-01-16 10:22:26 +00:00
|
|
|
.map(emoji => ({ ...emoji, custom: true }))
|
2024-02-19 09:23:58 +00:00
|
|
|
|
2023-01-16 10:22:26 +00:00
|
|
|
return [...emojis, ...customEmojis]
|
|
|
|
},
|
|
|
|
command: ({ editor, props, range }) => {
|
|
|
|
const emoji: CustomEmoji | Emoji = props.emoji
|
|
|
|
editor.commands.deleteRange(range)
|
|
|
|
if (isCustomEmoji(emoji)) {
|
|
|
|
editor.commands.insertCustomEmoji({
|
|
|
|
title: emoji.shortcode,
|
|
|
|
src: emoji.url,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
const skin = emoji.skins.find(skin => skin.native !== undefined)
|
|
|
|
if (skin)
|
|
|
|
editor.commands.insertEmoji(skin.native)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
render: createSuggestionRenderer(TiptapEmojiList),
|
|
|
|
}
|
|
|
|
|
2023-01-04 20:47:29 +00:00
|
|
|
function createSuggestionRenderer(component: Component): SuggestionOptions['render'] {
|
2022-11-25 13:21:02 +00:00
|
|
|
return () => {
|
2023-01-04 20:47:29 +00:00
|
|
|
let renderer: VueRenderer
|
2022-11-25 13:21:02 +00:00
|
|
|
let popup: Instance
|
|
|
|
|
|
|
|
return {
|
2022-11-27 06:54:56 +00:00
|
|
|
onStart(props) {
|
2023-01-04 20:47:29 +00:00
|
|
|
renderer = new VueRenderer(component, {
|
2022-11-25 13:21:02 +00:00
|
|
|
props,
|
|
|
|
editor: props.editor,
|
|
|
|
})
|
|
|
|
|
|
|
|
if (!props.clientRect)
|
|
|
|
return
|
|
|
|
|
|
|
|
popup = tippy(document.body, {
|
|
|
|
getReferenceClientRect: props.clientRect as GetReferenceClientRect,
|
|
|
|
appendTo: () => document.body,
|
2023-01-04 20:47:29 +00:00
|
|
|
content: renderer.element,
|
2022-11-25 13:21:02 +00:00
|
|
|
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) => {
|
2023-01-14 10:38:31 +00:00
|
|
|
props.editor.isFocused && renderer.updateProps({ ...props, isPending: true })
|
2022-11-27 06:34:38 +00:00
|
|
|
},
|
|
|
|
|
2022-11-25 13:21:02 +00:00
|
|
|
onUpdate(props) {
|
2023-01-14 10:38:31 +00:00
|
|
|
if (!props.editor.isFocused)
|
|
|
|
return
|
|
|
|
|
2023-01-04 20:47:29 +00:00
|
|
|
renderer.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
|
|
|
|
}
|
2023-01-04 20:47:29 +00:00
|
|
|
return renderer?.ref?.onKeyDown(props.event)
|
2022-11-25 13:21:02 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
onExit() {
|
|
|
|
popup?.destroy()
|
2023-01-04 20:47:29 +00:00
|
|
|
renderer?.destroy()
|
2022-11-25 13:21:02 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|