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

@@ -61,17 +61,44 @@ export default {
new HeadingNode({ maxLevel: 3 }), new HeadingNode({ maxLevel: 3 }),
new ListItemNode(), new ListItemNode(),
new MentionNode({ new MentionNode({
items: [
{
name: 'Philipp Kühn',
id: 1,
},
{
name: 'Hans Pagel',
id: 2,
},
],
onEnter: args => { onEnter: args => {
console.log('start', args) console.log('start', args)
this.query = args.text this.query = args.query
this.filteredUsers = args.items
}, },
onChange: args => { onChange: args => {
console.log('change', args) console.log('change', args)
this.query = args.text this.query = args.query
this.filteredUsers = args.items
}, },
onExit: args => { onExit: args => {
console.log('stop', args) console.log('stop', args)
this.query = null this.query = null
this.filteredUsers = args.items
},
onFilter: (items, query) => {
if (!query) {
return items
}
const fuse = new Fuse(items, {
threshold: 0.2,
keys: [
'name',
],
})
return fuse.search(query)
}, },
}), }),
new OrderedListNode(), new OrderedListNode(),
@@ -84,34 +111,9 @@ export default {
new HistoryExtension(), new HistoryExtension(),
], ],
query: null, query: null,
users: [ filteredUsers: [],
{
name: 'Philipp Kühn',
id: 1,
},
{
name: 'Hans Pagel',
id: 2,
},
],
} }
}, },
computed: {
filteredUsers() {
if (!this.query) {
return []
}
const fuse = new Fuse(this.users, {
threshold: 0.2,
keys: [
'name',
],
})
return fuse.search(this.query)
},
},
methods: { methods: {
selectUser(user) { selectUser(user) {
this.$refs.editor.menuActions.nodes.mention.command({ this.$refs.editor.menuActions.nodes.mention.command({

View File

@@ -54,10 +54,12 @@ export default class MentionNode extends Node {
allowSpaces: true, allowSpaces: true,
startOfLine: false, startOfLine: false,
}), }),
items: this.options.items,
onEnter: this.options.onEnter, onEnter: this.options.onEnter,
onChange: this.options.onChange, onChange: this.options.onChange,
onExit: this.options.onExit, onExit: this.options.onExit,
onKeyDown: this.options.onKeyDown, 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({ export function suggestionsPlugin({
matcher = triggerCharacter('#'), matcher = triggerCharacter('#'),
suggestionClass = 'ProseMirror-suggestion', suggestionClass = 'ProseMirror-suggestion',
items = [],
onEnter = () => false, onEnter = () => false,
onChange = () => false, onChange = () => false,
onExit = () => false, onExit = () => false,
onKeyDown = () => false, onKeyDown = () => false,
onFilter = (searchItems, query) => {
if (!query) {
return searchItems
}
return searchItems
.filter(item => JSON.stringify(item).toLowerCase().includes(query.toLowerCase()))
},
debug = false, debug = false,
}) { }) {
return new Plugin({ return new Plugin({
@@ -94,9 +103,10 @@ export function suggestionsPlugin({
onExit({ onExit({
view, view,
range: prev.range, range: prev.range,
text: prev.text, query: prev.text,
fullText: prev.fullText, text: prev.fullText,
decorationNode, decorationNode,
items: onFilter(items, prev.text),
}) })
} }
@@ -104,9 +114,10 @@ export function suggestionsPlugin({
onChange({ onChange({
view, view,
range: next.range, range: next.range,
text: next.text, query: next.text,
fullText: next.fullText, text: next.fullText,
decorationNode, decorationNode,
items: onFilter(items, next.text),
}) })
} }
@@ -114,9 +125,10 @@ export function suggestionsPlugin({
onEnter({ onEnter({
view, view,
range: next.range, range: next.range,
text: next.text, query: next.text,
fullText: next.fullText, text: next.fullText,
decorationNode, decorationNode,
items: onFilter(items, next.text),
}) })
} }
}, },