use constants for keycodes and mention types

This commit is contained in:
Alina Beck 2019-08-26 11:58:15 +01:00
parent dc70da8826
commit 5c4ec0dd19
4 changed files with 53 additions and 54 deletions

View File

@ -4,14 +4,13 @@
<editor-content ref="editor" :editor="editor" /> <editor-content ref="editor" :editor="editor" />
<context-menu ref="contextMenu" /> <context-menu ref="contextMenu" />
<suggestion-list <suggestion-list
:showSuggestions="showSuggestions" v-show="showSuggestions"
ref="suggestions" ref="suggestions"
:suggestion-type="suggestionType"
:filtered-items="filteredItems" :filtered-items="filteredItems"
:navigated-item-index="navigatedItemIndex" :navigated-item-index="navigatedItemIndex"
:query="query" :query="query"
:select-item="selectItem" :select-item="selectItem"
:is-mention="isMention"
:is-hashtag="isHashtag"
:has-results="hasResults" :has-results="hasResults"
/> />
<link-input v-show="linkMenuIsActive" ref="linkMenu" :editorCommand="editor.commands.link" /> <link-input v-show="linkMenuIsActive" ref="linkMenu" :editorCommand="editor.commands.link" />
@ -24,6 +23,9 @@ import { Editor, EditorContent } from 'tiptap'
import { History } from 'tiptap-extensions' import { History } from 'tiptap-extensions'
import linkify from 'linkify-it' import linkify from 'linkify-it'
import stringHash from 'string-hash' import stringHash from 'string-hash'
import * as key from '../../constants/keycodes'
import { HASHTAG, MENTION } from '../../constants/editor'
import defaultExtensions from './defaultExtensions.js' import defaultExtensions from './defaultExtensions.js'
import EventHandler from './plugins/eventHandler.js' import EventHandler from './plugins/eventHandler.js'
import Hashtag from './nodes/Hashtag.js' import Hashtag from './nodes/Hashtag.js'
@ -34,8 +36,6 @@ import SuggestionList from './SuggestionList'
import LinkInput from './LinkInput' import LinkInput from './LinkInput'
let throttleInputEvent let throttleInputEvent
const HASHTAG = 'hashtag'
const MENTION = 'mention'
export default { export default {
components: { components: {
@ -74,12 +74,6 @@ export default {
showSuggestions() { showSuggestions() {
return this.query || this.hasResults return this.query || this.hasResults
}, },
isMention() {
return this.suggestionType === MENTION
},
isHashtag() {
return this.suggestionType === HASHTAG
},
optionalExtensions() { optionalExtensions() {
const extensions = [] const extensions = []
// Don't change the following line. The functionallity is in danger! // Don't change the following line. The functionallity is in danger!
@ -182,21 +176,28 @@ export default {
this.$refs.contextMenu.hideContextMenu() this.$refs.contextMenu.hideContextMenu()
}, },
navigateSuggestionList({ event }) { navigateSuggestionList({ event }) {
const item = this.filteredItems[this.navigatedItemIndex]
switch (event.keyCode) { switch (event.keyCode) {
case 38: case key.ARROW_UP:
this.handleUpArrow() this.navigatedItemIndex =
(this.navigatedItemIndex + this.filteredItems.length - 1) % this.filteredItems.length
return true return true
case 40: case key.ARROW_DOWN:
this.handleDownArrow() this.navigatedItemIndex = (this.navigatedItemIndex + 1) % this.filteredItems.length
return true return true
case 13: case key.RETURN:
this.handleEnter() if (item) {
this.selectItem(item)
}
return true return true
case 32: case key.SPACE:
this.handleSpace() if (this.suggestionType === HASHTAG && this.query !== '') {
this.selectItem({ id: this.query })
}
return true return true
default: default:
@ -222,24 +223,6 @@ export default {
} }
return query return query
}, },
handleUpArrow() {
this.navigatedItemIndex =
(this.navigatedItemIndex + this.filteredItems.length - 1) % this.filteredItems.length
},
handleDownArrow() {
this.navigatedItemIndex = (this.navigatedItemIndex + 1) % this.filteredItems.length
},
handleEnter() {
const item = this.filteredItems[this.navigatedItemIndex]
if (item) {
this.selectItem(item)
}
},
handleSpace() {
if (this.suggestionType === HASHTAG && this.query !== '') {
this.selectItem({ id: this.query })
}
},
// we have to replace our suggestion text with a mention // we have to replace our suggestion text with a mention
// so it's important to pass also the position of your suggestion text // so it's important to pass also the position of your suggestion text
selectItem(item) { selectItem(item) {

View File

@ -1,5 +1,5 @@
<template> <template>
<div class="suggestion-list" v-show="showSuggestions"> <div class="suggestion-list">
<!-- "filteredItems" array is not empty --> <!-- "filteredItems" array is not empty -->
<template v-if="hasResults"> <template v-if="hasResults">
<div <div
@ -26,13 +26,15 @@
</template> </template>
<!-- if "!hasResults" --> <!-- if "!hasResults" -->
<div v-else> <div v-else>
<div v-if="isMention" class="suggestion-list__item is-empty"> <div
{{ $t('editor.mention.noUsersFound') }} v-if="isMention"
</div> class="suggestion-list__item is-empty"
>{{ $t('editor.mention.noUsersFound') }}</div>
<div v-if="isHashtag"> <div v-if="isHashtag">
<div v-if="query === ''" class="suggestion-list__item is-empty"> <div
{{ $t('editor.hashtag.noHashtagsFound') }} v-if="query === ''"
</div> class="suggestion-list__item is-empty"
>{{ $t('editor.hashtag.noHashtagsFound') }}</div>
<!-- if "query" is not empty --> <!-- if "query" is not empty -->
<div v-else> <div v-else>
<div class="suggestion-list__item is-empty">{{ $t('editor.hashtag.addHashtag') }}</div> <div class="suggestion-list__item is-empty">{{ $t('editor.hashtag.addHashtag') }}</div>
@ -44,16 +46,24 @@
</template> </template>
<script> <script>
import { HASHTAG, MENTION } from '../../constants/editor'
export default { export default {
props: [ props: {
'hasResults', suggestionType: String,
'filteredItems', filteredItems: Array,
'navigatedItemIndex', query: String,
'isMention', navigatedItemIndex: Number,
'isHashtag', selectItem: Function,
'query', hasResults: Number,
'selectItem', },
'showSuggestions', computed: {
], isMention() {
return this.suggestionType === MENTION
},
isHashtag() {
return this.suggestionType === HASHTAG
},
},
} }
</script> </script>

View File

@ -0,0 +1,2 @@
export const HASHTAG = 'hashtag'
export const MENTION = 'mention'

View File

@ -0,0 +1,4 @@
export const ARROW_UP = 38
export const ARROW_DOWN = 40
export const RETURN = 13
export const SPACE = 32