frontend/src/components/input/editor/suggestion.ts

214 lines
7.6 KiB
TypeScript
Raw Normal View History

2023-10-21 09:52:20 +00:00
import {VueRenderer} from '@tiptap/vue-3'
import tippy from 'tippy.js'
import CommandsList from './CommandsList.vue'
2023-10-21 11:00:12 +00:00
export default function suggestionSetup(t) {
return {
items: ({query}: { query: string }) => {
return [
{
title: t('input.editor.text'),
description: t('input.editor.textTooltip'),
icon: 'fa-font',
command: ({editor, range}) => {
editor
.chain()
.focus()
.deleteRange(range)
.setNode('paragraph', {level: 1})
.run()
},
2023-10-21 09:52:20 +00:00
},
2023-10-21 11:00:12 +00:00
{
title: t('input.editor.heading1'),
description: t('input.editor.heading1Tooltip'),
icon: 'fa-header',
command: ({editor, range}) => {
editor
.chain()
.focus()
.deleteRange(range)
.setNode('heading', {level: 1})
.run()
},
2023-10-21 09:52:20 +00:00
},
2023-10-21 11:00:12 +00:00
{
title: t('input.editor.heading2'),
description: t('input.editor.heading2Tooltip'),
icon: 'fa-header',
command: ({editor, range}) => {
editor
.chain()
.focus()
.deleteRange(range)
.setNode('heading', {level: 2})
.run()
},
2023-10-21 09:52:20 +00:00
},
2023-10-21 11:00:12 +00:00
{
title: t('input.editor.heading3'),
description: t('input.editor.heading3Tooltip'),
icon: 'fa-header',
command: ({editor, range}) => {
editor
.chain()
.focus()
.deleteRange(range)
.setNode('heading', {level: 2})
.run()
},
2023-10-21 09:52:20 +00:00
},
2023-10-21 11:00:12 +00:00
{
title: t('input.editor.bulletList'),
description: t('input.editor.bulletListTooltip'),
icon: 'fa-list-ul',
command: ({editor, range}) => {
editor
.chain()
.focus()
.deleteRange(range)
.toggleBulletList()
.run()
},
},
{
title: t('input.editor.orderedList'),
description: t('input.editor.orderedListTooltip'),
icon: 'fa-list-ol',
command: ({editor, range}) => {
editor
.chain()
.focus()
.deleteRange(range)
.toggleOrderedList()
.run()
},
},
{
title: t('input.editor.taskList'),
description: t('input.editor.taskListTooltip'),
icon: 'fa-list-check',
command: ({editor, range}) => {
editor
.chain()
.focus()
.deleteRange(range)
.toggleTaskList()
.run()
},
},
{
title: t('input.editor.quote'),
description: t('input.editor.quoteTooltip'),
icon: 'fa-quote-right',
command: ({editor, range}) => {
editor
.chain()
.focus()
.deleteRange(range)
.toggleBlockquote()
.run()
},
},
{
title: t('input.editor.code'),
description: t('input.editor.codeTooltip'),
icon: 'fa-code',
command: ({editor, range}) => {
editor
.chain()
.focus()
.deleteRange(range)
.toggleCodeBlock()
.run()
},
},
{
title: t('input.editor.image'),
description: t('input.editor.imageTooltip'),
icon: 'fa-image',
command: ({editor, range}) => {
editor
.chain()
.focus()
.deleteRange(range)
.run()
document.getElementById('tiptap__image-upload').click()
},
},
2023-10-21 11:00:12 +00:00
{
title: t('input.editor.horizontalRule'),
description: t('input.editor.horizontalRuleTooltip'),
icon: 'fa-ruler-horizontal',
command: ({editor, range}) => {
editor
.chain()
.focus()
.deleteRange(range)
.setHorizontalRule()
.run()
},
},
].filter(item => item.title.toLowerCase().startsWith(query.toLowerCase()))
},
2023-10-21 11:00:12 +00:00
render: () => {
let component: VueRenderer
let popup
2023-10-21 11:00:12 +00:00
return {
onStart: props => {
component = new VueRenderer(CommandsList, {
// using vue 2:
// parent: this,
// propsData: props,
props,
editor: props.editor,
})
2023-10-21 11:00:12 +00:00
if (!props.clientRect) {
return
}
2023-10-21 11:00:12 +00:00
popup = tippy('body', {
getReferenceClientRect: props.clientRect,
appendTo: () => document.body,
content: component.element,
showOnCreate: true,
interactive: true,
trigger: 'manual',
placement: 'bottom-start',
})
},
2023-10-21 11:00:12 +00:00
onUpdate(props) {
component.updateProps(props)
2023-10-21 11:00:12 +00:00
if (!props.clientRect) {
return
}
2023-10-21 11:00:12 +00:00
popup[0].setProps({
getReferenceClientRect: props.clientRect,
})
},
2023-10-21 11:00:12 +00:00
onKeyDown(props) {
if (props.event.key === 'Escape') {
popup[0].hide()
2023-10-21 11:00:12 +00:00
return true
}
2023-10-21 11:00:12 +00:00
return component.ref?.onKeyDown(props)
},
2023-10-21 11:00:12 +00:00
onExit() {
popup[0].destroy()
component.destroy()
},
}
},
}
}