move mention filtering to plugin

This commit is contained in:
Philipp Kühn
2018-09-27 11:35:32 +02:00
parent 55ee1b20d6
commit 56316476a9
3 changed files with 50 additions and 34 deletions

View File

@@ -54,10 +54,12 @@ export default class MentionNode extends Node {
allowSpaces: true,
startOfLine: false,
}),
items: this.options.items,
onEnter: this.options.onEnter,
onChange: this.options.onChange,
onExit: this.options.onExit,
onKeyDown: this.options.onKeyDown,
onFilter: this.options.onFilter,
}),
]
}

View File

@@ -67,10 +67,19 @@ export function triggerCharacter(char, { allowSpaces = false, startOfLine = fals
export function suggestionsPlugin({
matcher = triggerCharacter('#'),
suggestionClass = 'ProseMirror-suggestion',
items = [],
onEnter = () => false,
onChange = () => false,
onExit = () => false,
onKeyDown = () => false,
onFilter = (searchItems, query) => {
if (!query) {
return searchItems
}
return searchItems
.filter(item => JSON.stringify(item).toLowerCase().includes(query.toLowerCase()))
},
debug = false,
}) {
return new Plugin({
@@ -94,9 +103,10 @@ export function suggestionsPlugin({
onExit({
view,
range: prev.range,
text: prev.text,
fullText: prev.fullText,
query: prev.text,
text: prev.fullText,
decorationNode,
items: onFilter(items, prev.text),
})
}
@@ -104,9 +114,10 @@ export function suggestionsPlugin({
onChange({
view,
range: next.range,
text: next.text,
fullText: next.fullText,
query: next.text,
text: next.fullText,
decorationNode,
items: onFilter(items, next.text),
})
}
@@ -114,9 +125,10 @@ export function suggestionsPlugin({
onEnter({
view,
range: next.range,
text: next.text,
fullText: next.fullText,
query: next.text,
text: next.fullText,
decorationNode,
items: onFilter(items, next.text),
})
}
},