refactoring

This commit is contained in:
Philipp Kühn
2018-09-29 12:33:18 +02:00
parent 20466b2819
commit 7728ca1de0
3 changed files with 48 additions and 15 deletions

View File

@@ -1,7 +1,7 @@
<template> <template>
<div> <div>
<editor class="editor" :extensions="extensions" ref="editor">
<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>
Suggestions Suggestions
@@ -13,7 +13,6 @@
This is an example how to mention some users like <span data-mention-id="1">Philipp Kühn</span> or <span data-mention-id="2">Hans Pagel</span>. Try to type <code>@</code> and a popup (rendered with tippy.js) will appear. You can navigate with arrow keys through a list of suggestions. This is an example how to mention some users like <span data-mention-id="1">Philipp Kühn</span> or <span data-mention-id="2">Hans Pagel</span>. Try to type <code>@</code> and a popup (rendered with tippy.js) will appear. You can navigate with arrow keys through a list of suggestions.
</p> </p>
</div> </div>
</editor> </editor>
<div class="suggestion-list" v-show="showSuggestions" ref="suggestions"> <div class="suggestion-list" v-show="showSuggestions" ref="suggestions">
@@ -32,6 +31,7 @@
No users found. No users found.
</div> </div>
</div> </div>
</div> </div>
</template> </template>
@@ -50,43 +50,54 @@ import {
} from 'tiptap-extensions' } from 'tiptap-extensions'
export default { export default {
components: { components: {
Editor, Editor,
Icon, Icon,
}, },
data() { data() {
return { return {
extensions: [ extensions: [
new HardBreakNode(), new HardBreakNode(),
new HeadingNode({ maxLevel: 3 }), new HeadingNode({ maxLevel: 3 }),
new MentionNode({ new MentionNode({
// a list of all suggested items
items: [ items: [
{ id: 1, name: 'Philipp Kühn' }, { id: 1, name: 'Philipp Kühn' },
{ id: 2, name: 'Hans Pagel' }, { id: 2, name: 'Hans Pagel' },
{ id: 3, name: 'Kris Siepert' }, { id: 3, name: 'Kris Siepert' },
{ id: 4, name: 'Justin Schueler' }, { id: 4, name: 'Justin Schueler' },
], ],
// is called when a suggestion starts
onEnter: ({ items, query, range, command, virtualNode }) => { onEnter: ({ items, query, range, command, virtualNode }) => {
this.query = query this.query = query
this.filteredUsers = items this.filteredUsers = items
this.mentionPosition = range this.suggestionRange = range
this.insertMention = command
this.renderPopup(virtualNode) this.renderPopup(virtualNode)
// we save the command for inserting a selected mention
// this allows us to call it inside of our custom popup
// via keyboard navigation and on click
this.insertMention = command
}, },
// is called when a suggestion has changed
onChange: ({ items, query, range, virtualNode }) => { onChange: ({ items, query, range, virtualNode }) => {
this.query = query this.query = query
this.filteredUsers = items this.filteredUsers = items
this.mentionPosition = range this.suggestionRange = range
this.navigatedUserIndex = 0 this.navigatedUserIndex = 0
this.renderPopup(virtualNode) this.renderPopup(virtualNode)
}, },
// is called when a suggestion is cancelled
onExit: () => { onExit: () => {
// reset all saved values
this.query = null this.query = null
this.filteredUsers = [] this.filteredUsers = []
this.mentionPosition = null this.suggestionRange = null
this.navigatedUserIndex = 0 this.navigatedUserIndex = 0
this.destroyPopup() this.destroyPopup()
}, },
// is called on every keyDown event while a suggestion is active
onKeyDown: ({ event }) => { onKeyDown: ({ event }) => {
// pressing up arrow // pressing up arrow
if (event.keyCode === 38) { if (event.keyCode === 38) {
@@ -106,6 +117,10 @@ export default {
return false return false
}, },
// is called when a suggestion has changed
// this function is optional because there is basic filtering built-in
// you can overwrite it if you prefer your own filtering
// in this example we use fuse.js with support for fuzzy search
onFilter: (items, query) => { onFilter: (items, query) => {
if (!query) { if (!query) {
return items return items
@@ -113,9 +128,7 @@ export default {
const fuse = new Fuse(items, { const fuse = new Fuse(items, {
threshold: 0.2, threshold: 0.2,
keys: [ keys: ['name'],
'name',
],
}) })
return fuse.search(query) return fuse.search(query)
@@ -126,27 +139,39 @@ export default {
new ItalicMark(), new ItalicMark(),
], ],
query: null, query: null,
mentionPosition: null, suggestionRange: null,
filteredUsers: [], filteredUsers: [],
navigatedUserIndex: 0, navigatedUserIndex: 0,
insertMention: () => {}, insertMention: () => {},
} }
}, },
computed: { computed: {
hasResults() { hasResults() {
return this.filteredUsers.length return this.filteredUsers.length
}, },
showSuggestions() { showSuggestions() {
return this.query || this.hasResults return this.query || this.hasResults
}, },
}, },
methods: { methods: {
// navigate to the previous item
// if it's the first item, navigate to the last one
upHandler() { upHandler() {
this.navigatedUserIndex = ((this.navigatedUserIndex + this.filteredUsers.length) - 1) % this.filteredUsers.length this.navigatedUserIndex = ((this.navigatedUserIndex + this.filteredUsers.length) - 1) % this.filteredUsers.length
}, },
// navigate to the next item
// if it's the last item, navigate to the first one
downHandler() { downHandler() {
this.navigatedUserIndex = (this.navigatedUserIndex + 1) % this.filteredUsers.length this.navigatedUserIndex = (this.navigatedUserIndex + 1) % this.filteredUsers.length
}, },
enterHandler() { enterHandler() {
const user = this.filteredUsers[this.navigatedUserIndex] const user = this.filteredUsers[this.navigatedUserIndex]
@@ -154,15 +179,21 @@ export default {
this.selectUser(user) this.selectUser(user)
} }
}, },
// we have to replace our suggestion text with a mention
// so it's important to pass also the position of your suggestion text
selectUser(user) { selectUser(user) {
this.insertMention({ this.insertMention({
position: this.mentionPosition, replaceRange: this.suggestionRange,
attrs: { attrs: {
id: user.id, id: user.id,
label: user.name, label: user.name,
}, },
}) })
}, },
// renders a popup with suggestions
// tiptap provides a virtualNode object for using popper.js (or tippy.js) for popups
renderPopup(node) { renderPopup(node) {
if (this.popup) { if (this.popup) {
return return
@@ -182,12 +213,14 @@ export default {
arrowType: 'round', arrowType: 'round',
}) })
}, },
destroyPopup() { destroyPopup() {
if (this.popup) { if (this.popup) {
this.popup.destroyAll() this.popup.destroyAll()
this.popup = null this.popup = null
} }
}, },
}, },
} }
</script> </script>

View File

@@ -48,8 +48,8 @@ export default class MentionNode extends Node {
allowSpaces: false, allowSpaces: false,
startOfLine: false, startOfLine: false,
}, },
command: ({ position, attrs, schema }) => { command: ({ range, attrs, schema }) => {
return replaceText(position, schema.nodes.mention, attrs) return replaceText(range, schema.nodes.mention, attrs)
}, },
items: this.options.items, items: this.options.items,
onEnter: this.options.onEnter, onEnter: this.options.onEnter,

View File

@@ -146,9 +146,9 @@ export default function SuggestionsPlugin({
decorationNode, decorationNode,
virtualNode, virtualNode,
items: onFilter(items, next.text), items: onFilter(items, next.text),
command: ({ position, attrs }) => { command: ({ replaceRange, attrs }) => {
command({ command({
position, range: replaceRange,
attrs, attrs,
schema: view.state.schema, schema: view.state.schema,
})(view.state, view.dispatch, view) })(view.state, view.dispatch, view)