refactoring

This commit is contained in:
Philipp Kühn
2021-01-15 15:58:39 +01:00
parent decabc09c6
commit 9f0ae94201
2 changed files with 41 additions and 19 deletions

View File

@@ -1,9 +1,17 @@
import { Node } from '@tiptap/core' import { Node } from '@tiptap/core'
import Suggestion from '@tiptap/suggestion' import Suggestion from '@tiptap/suggestion'
export interface MentionOptions {
renderer: any,
}
export const Mention = Node.create({ export const Mention = Node.create({
name: 'mention', name: 'mention',
defaultOptions: <MentionOptions>{
renderer: null,
},
group: 'inline', group: 'inline',
inline: true, inline: true,
@@ -37,7 +45,11 @@ export const Mention = Node.create({
addProseMirrorPlugins() { addProseMirrorPlugins() {
return [ 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 { Plugin, PluginKey } from 'prosemirror-state'
import { Decoration, DecorationSet } from 'prosemirror-view' import { Decoration, DecorationSet } from 'prosemirror-view'
import { findSuggestionMatch } from './findSuggestionMatch' import { findSuggestionMatch } from './findSuggestionMatch'
import { getVirtualNode } from './getVirtualNode' 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({ export function Suggestion({
editor,
char = '@', char = '@',
allowSpaces = false, allowSpaces = false,
startOfLine = false, startOfLine = false,
appendText = null,
suggestionClass = 'suggestion', suggestionClass = 'suggestion',
command = () => false, command = () => null,
items = [], items = () => [],
onEnter = (props: any) => false, onStart = () => null,
onUpdate = (props: any) => false, onUpdate = () => null,
onExit = (props: any) => false, onExit = () => null,
onKeyDown = (props: any) => false, onKeyDown = () => null,
onFilter = (searchItems: any[], query: string) => { renderer = () => ({}),
if (!query) { }: SuggestionOptions) {
return searchItems // const testRenderer = renderer()
}
return searchItems
.filter(item => JSON.stringify(item).toLowerCase().includes(query.toLowerCase()))
},
}) {
return new Plugin({ return new Plugin({
key: new PluginKey('suggestions'), key: new PluginKey('suggestions'),
@@ -61,8 +72,7 @@ export function Suggestion({
? getVirtualNode(decorationNode) ? getVirtualNode(decorationNode)
: null, : null,
items: (handleChange || handleStart) items: (handleChange || handleStart)
// @ts-ignore ? await items(state.query)
? await onFilter(Array.isArray(items) ? items : await items(), state.query)
: [], : [],
command: () => { command: () => {
console.log('command') console.log('command')
@@ -90,7 +100,7 @@ export function Suggestion({
} }
if (handleStart) { if (handleStart) {
onEnter(props) onStart(props)
} }
}, },
} }