add command for mentions

This commit is contained in:
Philipp Kühn
2018-09-25 07:43:21 +02:00
parent b2e73fc0a4
commit a511704f49
4 changed files with 60 additions and 7 deletions

View File

@@ -1,6 +1,6 @@
<template> <template>
<div> <div>
<editor class="editor" :extensions="extensions"> <editor class="editor" :extensions="extensions" ref="editor">
<div class="editor__content" slot="content" slot-scope="props"> <div class="editor__content" slot="content" slot-scope="props">
<h2> <h2>
@@ -12,6 +12,12 @@
</div> </div>
</editor> </editor>
<div class="suggestions">
<div v-for="user in users" :key="user.id" @click="selectUser(user)">
{{ user.name }}
</div>
</div>
</div> </div>
</template> </template>
@@ -51,14 +57,14 @@ export default {
new HeadingNode({ maxLevel: 3 }), new HeadingNode({ maxLevel: 3 }),
new ListItemNode(), new ListItemNode(),
new MentionNode({ new MentionNode({
onEnter(args) { onEnter: args => {
console.log('start', args); console.log('start', args)
}, },
onChange(args) { onChange: args => {
console.log('change', args); console.log('change', args)
}, },
onExit(args) { onExit: args => {
console.log('stop', args); console.log('stop', args)
}, },
}), }),
new OrderedListNode(), new OrderedListNode(),
@@ -70,8 +76,27 @@ export default {
new LinkMark(), new LinkMark(),
new HistoryExtension(), new HistoryExtension(),
], ],
users: [
{
name: 'Philipp Kühn',
id: 1,
},
{
name: 'Hans Pagel',
id: 2,
},
],
} }
}, },
methods: {
selectUser(user) {
this.$refs.editor.menuActions.nodes.mention.command({
type: 'user',
id: user.id,
label: user.name,
})
},
},
} }
</script> </script>
@@ -86,4 +111,9 @@ export default {
border-radius: 5px; border-radius: 5px;
padding: 0.2rem 0.5rem; padding: 0.2rem 0.5rem;
} }
.suggestions {
max-width: 30rem;
margin: 0 auto 2rem auto;
}
</style> </style>

View File

@@ -0,0 +1,16 @@
export default function (type, attrs = {}) {
return (state, dispatch) => {
const { $from } = state.selection
const index = $from.index()
if (!$from.parent.canReplaceWith(index, index, type)) {
return false
}
if (dispatch) {
dispatch(state.tr.replaceSelectionWith(type.create(attrs)))
}
return true
}
}

View File

@@ -40,6 +40,7 @@ import {
import markInputRule from './commands/markInputRule' import markInputRule from './commands/markInputRule'
import removeMark from './commands/removeMark' import removeMark from './commands/removeMark'
import setInlineBlockType from './commands/setInlineBlockType'
import splitToDefaultListItem from './commands/splitToDefaultListItem' import splitToDefaultListItem from './commands/splitToDefaultListItem'
import toggleBlockType from './commands/toggleBlockType' import toggleBlockType from './commands/toggleBlockType'
import toggleList from './commands/toggleList' import toggleList from './commands/toggleList'
@@ -86,6 +87,7 @@ export {
// custom // custom
markInputRule, markInputRule,
removeMark, removeMark,
setInlineBlockType,
splitToDefaultListItem, splitToDefaultListItem,
toggleBlockType, toggleBlockType,
toggleList, toggleList,

View File

@@ -1,4 +1,5 @@
import { Node } from 'tiptap' import { Node } from 'tiptap'
import { setInlineBlockType } 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 {
@@ -41,6 +42,10 @@ export default class MentionNode extends Node {
} }
} }
command({ type, attrs }) {
return setInlineBlockType(type, attrs)
}
get plugins() { get plugins() {
return [ return [
suggestionsPlugin({ suggestionsPlugin({