add command to suggestions

This commit is contained in:
Philipp Kühn
2018-09-28 13:31:01 +02:00
parent a8dce68290
commit bfb5783db0
3 changed files with 29 additions and 13 deletions

View File

@@ -62,17 +62,21 @@ export default {
id: 2, id: 2,
}, },
], ],
onEnter: ({ items, query }) => { onEnter: ({ items, query, range, command }) => {
this.query = query this.query = query
this.filteredUsers = items this.filteredUsers = items
this.pos = range
this.insertMention = command
}, },
onChange: ({ items, query }) => { onChange: ({ items, query, range }) => {
this.query = query this.query = query
this.filteredUsers = items this.filteredUsers = items
this.pos = range
}, },
onExit: () => { onExit: () => {
this.query = null this.query = null
this.filteredUsers = [] this.filteredUsers = []
this.pos = null
}, },
onKeyDown: ({ event }) => { onKeyDown: ({ event }) => {
// pressing up arrow // pressing up arrow
@@ -110,8 +114,10 @@ export default {
}), }),
], ],
query: null, query: null,
pos: null,
filteredUsers: [], filteredUsers: [],
navigatedUserIndex: 0, navigatedUserIndex: 0,
insertMention: () => {},
} }
}, },
methods: { methods: {
@@ -126,10 +132,13 @@ export default {
this.selectUser(user) this.selectUser(user)
}, },
selectUser(user) { selectUser(user) {
this.$refs.editor.menuActions.nodes.mention.command({ this.insertMention({
type: 'user', pos: this.pos,
id: user.id, attrs: {
label: user.name, type: 'user',
id: user.id,
label: user.name,
},
}) })
}, },
}, },

View File

@@ -1,5 +1,5 @@
import { Node } from 'tiptap' import { Node } from 'tiptap'
import { setInlineBlockType } from 'tiptap-commands' import { replaceText } from 'tiptap-commands'
import { triggerCharacter, suggestionsPlugin } from '../plugins/suggestions' import { triggerCharacter, suggestionsPlugin } from '../plugins/suggestions'
export default class MentionNode extends Node { export default class MentionNode extends Node {
@@ -42,10 +42,6 @@ export default class MentionNode extends Node {
} }
} }
command({ type, attrs }) {
return setInlineBlockType(type, attrs)
}
get plugins() { get plugins() {
return [ return [
suggestionsPlugin({ suggestionsPlugin({
@@ -54,6 +50,9 @@ export default class MentionNode extends Node {
allowSpaces: true, allowSpaces: true,
startOfLine: false, startOfLine: false,
}), }),
command: ({ pos, attrs, schema }) => {
return replaceText(pos, schema.nodes.mention, attrs)
},
items: this.options.items, items: this.options.items,
onEnter: this.options.onEnter, onEnter: this.options.onEnter,
onChange: this.options.onChange, onChange: this.options.onChange,

View File

@@ -67,6 +67,7 @@ export function triggerCharacter(char, { allowSpaces = false, startOfLine = fals
export function suggestionsPlugin({ export function suggestionsPlugin({
matcher = triggerCharacter('#'), matcher = triggerCharacter('#'),
suggestionClass = 'ProseMirror-suggestion', suggestionClass = 'ProseMirror-suggestion',
command = () => false,
items = [], items = [],
onEnter = () => false, onEnter = () => false,
onChange = () => false, onChange = () => false,
@@ -129,6 +130,13 @@ export function suggestionsPlugin({
text: next.fullText, text: next.fullText,
decorationNode, decorationNode,
items: onFilter(items, next.text), items: onFilter(items, next.text),
command: ({ pos, attrs }) => {
command({
pos,
attrs,
schema: view.state.schema,
})(view.state, view.dispatch, view)
},
}) })
} }
}, },
@@ -209,11 +217,11 @@ export function suggestionsPlugin({
* @returns {boolean} * @returns {boolean}
*/ */
handleKeyDown(view, event) { handleKeyDown(view, event) {
const { active } = this.getState(view.state) const { active, range } = this.getState(view.state)
if (!active) return false if (!active) return false
return onKeyDown({ view, event }) return onKeyDown({ view, event, range })
}, },
/** /**