refactoring

This commit is contained in:
Philipp Kühn 2021-01-15 15:58:39 +01:00 committed by Hans Pagel
parent be9167589e
commit 58b7aa7baa
2 changed files with 41 additions and 19 deletions

View File

@ -1,9 +1,17 @@
import { Node } from '@tiptap/core'
import Suggestion from '@tiptap/suggestion'
export interface MentionOptions {
renderer: any,
}
export const Mention = Node.create({
name: 'mention',
defaultOptions: <MentionOptions>{
renderer: null,
},
group: 'inline',
inline: true,
@ -37,7 +45,11 @@ export const Mention = Node.create({
addProseMirrorPlugins() {
return [
Suggestion({}),
Suggestion({
editor: this.editor,
char: '@',
renderer: this.options.renderer,
}),
]
},
})

View File

@ -1,29 +1,40 @@
import { Editor } from '@tiptap/core'
import { Plugin, PluginKey } from 'prosemirror-state'
import { Decoration, DecorationSet } from 'prosemirror-view'
import { findSuggestionMatch } from './findSuggestionMatch'
import { getVirtualNode } from './getVirtualNode'
export interface SuggestionOptions {
editor: Editor,
char?: string,
allowSpaces?: boolean,
startOfLine?: boolean,
suggestionClass?: string,
command?: () => any,
items?: (query: string) => any[],
onStart?: (props: any) => any,
onUpdate?: (props: any) => any,
onExit?: (props: any) => any,
onKeyDown?: (props: any) => any,
renderer?: any,
}
export function Suggestion({
editor,
char = '@',
allowSpaces = false,
startOfLine = false,
appendText = null,
suggestionClass = 'suggestion',
command = () => false,
items = [],
onEnter = (props: any) => false,
onUpdate = (props: any) => false,
onExit = (props: any) => false,
onKeyDown = (props: any) => false,
onFilter = (searchItems: any[], query: string) => {
if (!query) {
return searchItems
}
command = () => null,
items = () => [],
onStart = () => null,
onUpdate = () => null,
onExit = () => null,
onKeyDown = () => null,
renderer = () => ({}),
}: SuggestionOptions) {
// const testRenderer = renderer()
return searchItems
.filter(item => JSON.stringify(item).toLowerCase().includes(query.toLowerCase()))
},
}) {
return new Plugin({
key: new PluginKey('suggestions'),
@ -61,8 +72,7 @@ export function Suggestion({
? getVirtualNode(decorationNode)
: null,
items: (handleChange || handleStart)
// @ts-ignore
? await onFilter(Array.isArray(items) ? items : await items(), state.query)
? await items(state.query)
: [],
command: () => {
console.log('command')
@ -90,7 +100,7 @@ export function Suggestion({
}
if (handleStart) {
onEnter(props)
onStart(props)
}
},
}