get link input to work

This commit is contained in:
Alina Beck 2019-08-26 16:08:46 +01:00
parent 62e34bf5b0
commit 1df3cb1ed1
5 changed files with 50 additions and 40 deletions

View File

@ -7,21 +7,28 @@ export default {
node: Object, node: Object,
}, },
methods: { methods: {
displayContextMenu(target, content, trigger) { displayContextMenu(target, content, type) {
const trigger = type === 'link' ? 'click' : 'mouseenter'
const showOnInit = type !== 'link'
if (this.menu) { if (this.menu) {
return return
} }
this.menu = tippy(target, { this.menu = tippy(target, {
content: content,
trigger: trigger || 'mouseenter',
interactive: true,
theme: 'dark',
placement: 'top-start',
inertia: true,
duration: [400, 200],
showOnInit: true,
arrow: true, arrow: true,
arrowType: 'round', arrowType: 'round',
content: content,
// duration: [400, 200],
inertia: true,
interactive: true,
placement: 'top-start',
showOnInit,
theme: 'dark',
trigger,
onMount(instance) {
instance.popper.querySelector('input').focus()
},
}) })
// we have to update tippy whenever the DOM is updated // we have to update tippy whenever the DOM is updated
if (MutationObserver) { if (MutationObserver) {

View File

@ -1,6 +1,6 @@
<template> <template>
<div class="editor"> <div class="editor">
<menu-bar :editor="editor" :showLinkMenu="showLinkMenu" /> <menu-bar :editor="editor" :toggleLinkInput="toggleLinkInput" />
<editor-content ref="editor" :editor="editor" /> <editor-content ref="editor" :editor="editor" />
<context-menu ref="contextMenu" /> <context-menu ref="contextMenu" />
<suggestion-list <suggestion-list
@ -11,7 +11,14 @@
:query="query" :query="query"
:select-item="selectItem" :select-item="selectItem"
/> />
<link-input v-show="linkMenuIsActive" ref="linkMenu" :editorCommand="editor.commands.link" /> <link-input
v-show="isLinkInputActive"
ref="linkInput"
:linkUrl="linkUrl"
:editor-command="editor.commands.link"
:toggle-link-input="toggleLinkInput"
:set-link-url="setLinkUrl"
/>
</div> </div>
</template> </template>
@ -54,7 +61,7 @@ export default {
lastValueHash: null, lastValueHash: null,
editor: null, editor: null,
linkUrl: null, linkUrl: null,
linkMenuIsActive: false, isLinkInputActive: false,
suggestionType: '', suggestionType: '',
query: null, query: null,
suggestionRange: null, suggestionRange: null,
@ -242,23 +249,17 @@ export default {
this.$emit('input', content) this.$emit('input', content)
} }
}, },
showLinkMenu(attrs, element) { toggleLinkInput(attrs, element) {
this.linkUrl = attrs.href if (!this.isLinkInputActive && attrs && element) {
this.linkMenuIsActive = true this.linkUrl = attrs.href
this.$refs.contextMenu.displayContextMenu(element, this.$refs.linkMenu.$el, 'click') this.isLinkInputActive = true
this.$nextTick(() => { this.$refs.contextMenu.displayContextMenu(element, this.$refs.linkInput.$el, 'link')
try { } else {
const input = this.$refs.linkMenu.$el.querySelector('input') this.$refs.contextMenu.hideContextMenu()
input.focus() this.linkUrl = null
input.select() this.isLinkInputActive = false
} catch (err) {} this.editor.focus()
}) }
},
hideLinkMenu() {
this.$refs.contextMenu.hideContextMenu()
this.linkUrl = null
this.linkMenuIsActive = false
this.editor.focus()
}, },
setLinkUrl(command, url) { setLinkUrl(command, url) {
const links = linkify().match(url) const links = linkify().match(url)
@ -267,7 +268,7 @@ export default {
command({ command({
href: links.pop().url, href: links.pop().url,
}) })
this.hideLinkMenu() this.toggleLinkInput()
this.editor.focus() this.editor.focus()
} else if (!url) { } else if (!url) {
// remove link // remove link

View File

@ -1,11 +1,13 @@
<template> <template>
<div> <div>
<ds-input <ds-input
id="linkInputId"
v-model="linkUrl" v-model="linkUrl"
autofocus
class="editor-menu-link-input" class="editor-menu-link-input"
placeholder="https://" placeholder="https://"
@blur.native.capture="hideLinkMenu()" @blur.native.capture="toggleLinkInput()"
@keydown.native.esc.prevent="hideLinkMenu()" @keydown.native.esc.prevent="toggleLinkInput()"
@keydown.native.enter.prevent="setLinkUrl(editorCommand, linkUrl)" @keydown.native.enter.prevent="setLinkUrl(editorCommand, linkUrl)"
/> />
</div> </div>
@ -13,11 +15,11 @@
<script> <script>
export default { export default {
props: ['hideLinkMenu', 'setLinkUrl', 'editorCommand'], props: {
data() { linkUrl: String,
return { editorCommand: Function,
linkUrl: null, toggleLinkInput: Function,
} setLinkUrl: Function,
}, },
} }
</script> </script>

View File

@ -8,7 +8,7 @@
<menu-bar-button <menu-bar-button
ref="linkButton" ref="linkButton"
:isActive="isActive.link()" :isActive="isActive.link()"
:onClick="event => showLinkMenu(getMarkAttrs('link'), event.currentTarget)" :onClick="event => toggleLinkInput(getMarkAttrs('link'), event.currentTarget)"
icon="link" icon="link"
/> />
@ -68,7 +68,7 @@ export default {
}, },
props: { props: {
editor: Object, editor: Object,
showLinkMenu: Function, toggleLinkInput: Function,
}, },
} }
</script> </script>

View File

@ -1,5 +1,5 @@
<template> <template>
<ds-button size="small" :ghost="!isActive" @click="onClick" :icon="icon"> <ds-button size="small" :ghost="!isActive" @click.prevent="onClick" :icon="icon">
<span v-if="label">{{ label }}</span> <span v-if="label">{{ label }}</span>
</ds-button> </ds-button>
</template> </template>