make optional extensions a computed property

This commit is contained in:
Alina Beck 2019-08-24 12:42:34 +01:00
parent 6b452a2217
commit dc70da8826

View File

@ -19,22 +19,23 @@
</template> </template>
<script> <script>
import defaultExtensions from './defaultExtensions.js' import { mapGetters } from 'vuex'
import { Editor, EditorContent } from 'tiptap'
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 Fuse from 'fuse.js' import defaultExtensions from './defaultExtensions.js'
import { Editor, EditorContent } from 'tiptap'
import EventHandler from './plugins/eventHandler.js' import EventHandler from './plugins/eventHandler.js'
import { History } from 'tiptap-extensions'
import Hashtag from './nodes/Hashtag.js' import Hashtag from './nodes/Hashtag.js'
import Mention from './nodes/Mention.js' import Mention from './nodes/Mention.js'
import { mapGetters } from 'vuex'
import MenuBar from './MenuBar' import MenuBar from './MenuBar'
import ContextMenu from './ContextMenu' import ContextMenu from './ContextMenu'
import SuggestionList from './SuggestionList' 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: {
@ -51,60 +52,12 @@ export default {
doc: { type: Object, default: () => {} }, doc: { type: Object, default: () => {} },
}, },
data() { data() {
let optionalExtensions = []
// Don't change the following line. The functionallity is in danger!
if (this.users) {
optionalExtensions.push(
new Mention({
items: () => {
return this.users
},
onEnter: props => this.openSuggestionList(props, this.mentionSuggestionType),
onChange: this.updateSuggestionList,
onExit: this.closeSuggestionList,
onKeyDown: this.navigateSuggestionList,
onFilter: this.filterSuggestionList,
}),
)
}
// Don't change the following line. The functionallity is in danger!
if (this.hashtags) {
optionalExtensions.push(
new Hashtag({
items: () => {
return this.hashtags
},
onEnter: props => this.openSuggestionList(props, this.hashtagSuggestionType),
onChange: this.updateSuggestionList,
onExit: this.closeSuggestionList,
onKeyDown: this.navigateSuggestionList,
onFilter: this.filterSuggestionList,
}),
)
}
return { return {
lastValueHash: null, lastValueHash: null,
editor: new Editor({ editor: null,
content: this.value || '',
doc: this.doc,
extensions: [
...defaultExtensions(this),
new EventHandler(),
new History(),
...optionalExtensions,
],
onUpdate: e => {
clearTimeout(throttleInputEvent)
throttleInputEvent = setTimeout(() => this.onUpdate(e), 300)
},
}),
linkUrl: null, linkUrl: null,
linkMenuIsActive: false, linkMenuIsActive: false,
nullSuggestionType: '', suggestionType: '',
mentionSuggestionType: 'mention',
hashtagSuggestionType: 'hashtag',
suggestionType: this.nullSuggestionType,
query: null, query: null,
suggestionRange: null, suggestionRange: null,
filteredItems: [], filteredItems: [],
@ -122,10 +75,44 @@ export default {
return this.query || this.hasResults return this.query || this.hasResults
}, },
isMention() { isMention() {
return this.suggestionType === this.mentionSuggestionType return this.suggestionType === MENTION
}, },
isHashtag() { isHashtag() {
return this.suggestionType === this.hashtagSuggestionType return this.suggestionType === HASHTAG
},
optionalExtensions() {
const extensions = []
// Don't change the following line. The functionallity is in danger!
if (this.users) {
extensions.push(
new Mention({
items: () => {
return this.users
},
onEnter: props => this.openSuggestionList(props, MENTION),
onChange: this.updateSuggestionList,
onExit: this.closeSuggestionList,
onKeyDown: this.navigateSuggestionList,
onFilter: this.filterSuggestionList,
}),
)
}
// Don't change the following line. The functionallity is in danger!
if (this.hashtags) {
extensions.push(
new Hashtag({
items: () => {
return this.hashtags
},
onEnter: props => this.openSuggestionList(props, HASHTAG),
onChange: this.updateSuggestionList,
onExit: this.closeSuggestionList,
onKeyDown: this.navigateSuggestionList,
onFilter: this.filterSuggestionList,
}),
)
}
return extensions
}, },
}, },
watch: { watch: {
@ -150,6 +137,22 @@ export default {
}, },
}, },
}, },
created() {
this.editor = new Editor({
content: this.value || '',
doc: this.doc,
extensions: [
...defaultExtensions(this),
new EventHandler(),
new History(),
...this.optionalExtensions,
],
onUpdate: e => {
clearTimeout(throttleInputEvent)
throttleInputEvent = setTimeout(() => this.onUpdate(e), 300)
},
})
},
beforeDestroy() { beforeDestroy() {
this.editor.destroy() this.editor.destroy()
}, },
@ -171,7 +174,7 @@ export default {
this.$refs.contextMenu.displayContextMenu(virtualNode, this.$refs.suggestions.$el) this.$refs.contextMenu.displayContextMenu(virtualNode, this.$refs.suggestions.$el)
}, },
closeSuggestionList() { closeSuggestionList() {
this.suggestionType = this.nullSuggestionType this.suggestionType = ''
this.query = null this.query = null
this.filteredItems = [] this.filteredItems = []
this.suggestionRange = null this.suggestionRange = null
@ -211,7 +214,7 @@ export default {
}) })
}, },
sanitizeQuery(query) { sanitizeQuery(query) {
if (this.suggestionType === this.hashtagSuggestionType) { if (this.suggestionType === HASHTAG) {
// remove all not allowed chars // remove all not allowed chars
query = query.replace(/[^a-zA-Z0-9]/gm, '') query = query.replace(/[^a-zA-Z0-9]/gm, '')
// if the query is only made of digits, make it empty // if the query is only made of digits, make it empty
@ -233,7 +236,7 @@ export default {
} }
}, },
handleSpace() { handleSpace() {
if (this.suggestionType === this.hashtagSuggestionType && this.query !== '') { if (this.suggestionType === HASHTAG && this.query !== '') {
this.selectItem({ id: this.query }) this.selectItem({ id: this.query })
} }
}, },