refactoring

This commit is contained in:
Philipp Kühn
2018-09-29 09:28:46 +02:00
parent cb2ac95a73
commit 4447e9845e
3 changed files with 36 additions and 37 deletions

View File

@@ -16,20 +16,21 @@
</editor> </editor>
<div class="suggestion-list" v-show="query || filteredUsers.length" ref="suggestions"> <div class="suggestion-list" v-show="showSuggestions" ref="suggestions">
<div class="suggestion-list__item" v-if="query && !filteredUsers.length"> <template v-if="hasResults">
No users found.
</div>
<div <div
v-else
v-for="(user, index) in filteredUsers" v-for="(user, index) in filteredUsers"
:key="user.id" :key="user.id"
@click="selectUser(user)"
class="suggestion-list__item" class="suggestion-list__item"
:class="{ 'is-selected': navigatedUserIndex === index }" :class="{ 'is-selected': navigatedUserIndex === index }"
@click="selectUser(user)"
> >
{{ user.name }} {{ user.name }}
</div> </div>
</template>
<div v-else class="suggestion-list__item">
No users found.
</div>
</div> </div>
</div> </div>
</template> </template>
@@ -60,40 +61,30 @@ export default {
new HeadingNode({ maxLevel: 3 }), new HeadingNode({ maxLevel: 3 }),
new MentionNode({ new MentionNode({
items: [ items: [
{ { id: 1, name: 'Philipp Kühn' },
name: 'Philipp Kühn', { id: 2, name: 'Hans Pagel' },
id: 1, { id: 3, name: 'Kris Siepert' },
}, { id: 4, name: 'Justin Schueler' },
{
name: 'Hans Pagel',
id: 2,
},
{
name: 'Kris Siepert',
id: 3,
},
{
name: 'Justin Schüler',
id: 4,
},
], ],
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.pos = range this.mentionPosition = range
this.insertMention = command this.insertMention = command
this.renderPopup(virtualNode) this.renderPopup(virtualNode)
}, },
onChange: ({ items, query, range, virtualNode }) => { onChange: ({ items, query, range, virtualNode }) => {
this.query = query this.query = query
this.filteredUsers = items this.filteredUsers = items
this.pos = range this.mentionPosition = range
this.navigatedUserIndex = 0
this.renderPopup(virtualNode) this.renderPopup(virtualNode)
}, },
onExit: () => { onExit: () => {
this.query = null this.query = null
this.filteredUsers = [] this.filteredUsers = []
this.pos = null this.mentionPosition = null
this.navigatedUserIndex = 0
this.destroyPopup() this.destroyPopup()
}, },
onKeyDown: ({ event }) => { onKeyDown: ({ event }) => {
@@ -135,12 +126,20 @@ export default {
new ItalicMark(), new ItalicMark(),
], ],
query: null, query: null,
pos: null, mentionPosition: null,
filteredUsers: [], filteredUsers: [],
navigatedUserIndex: 0, navigatedUserIndex: 0,
insertMention: () => {}, insertMention: () => {},
} }
}, },
computed: {
hasResults() {
return this.filteredUsers.length
},
showSuggestions() {
return this.query || this.hasResults
},
},
methods: { methods: {
upHandler() { upHandler() {
this.navigatedUserIndex = ((this.navigatedUserIndex + this.filteredUsers.length) - 1) % this.filteredUsers.length this.navigatedUserIndex = ((this.navigatedUserIndex + this.filteredUsers.length) - 1) % this.filteredUsers.length
@@ -157,7 +156,7 @@ export default {
}, },
selectUser(user) { selectUser(user) {
this.insertMention({ this.insertMention({
pos: this.pos, position: this.mentionPosition,
attrs: { attrs: {
id: user.id, id: user.id,
label: user.name, label: user.name,

View File

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

View File

@@ -139,9 +139,9 @@ export function suggestionsPlugin({
decorationNode, decorationNode,
virtualNode, virtualNode,
items: onFilter(items, next.text), items: onFilter(items, next.text),
command: ({ pos, attrs }) => { command: ({ position, attrs }) => {
command({ command({
pos, position,
attrs, attrs,
schema: view.state.schema, schema: view.state.schema,
})(view.state, view.dispatch, view) })(view.state, view.dispatch, view)