tiptap/examples/Components/Routes/Links/index.vue

114 lines
2.8 KiB
Vue
Raw Normal View History

2018-08-22 20:10:44 +08:00
<template>
2018-11-09 05:03:10 +08:00
<div class="editor">
<menu-bubble class="menububble" :editor="editor">
<div
slot-scope="{ commands, isActive, markAttrs, menu }"
class="menububble"
:class="{ 'is-active': menu.isActive }"
:style="`left: ${menu.left}px; bottom: ${menu.bottom}px;`"
>
2018-08-22 20:10:44 +08:00
2018-11-09 05:03:10 +08:00
<form class="menububble__form" v-if="linkMenuIsActive" @submit.prevent="setLinkUrl(commands.link, linkUrl)">
<input class="menububble__input" type="text" v-model="linkUrl" placeholder="https://" ref="linkInput" @keydown.esc="hideLinkMenu"/>
<button class="menububble__button" @click="setLinkUrl(commands.link, null)" type="button">
<icon name="remove" />
</button>
</form>
2018-08-22 20:10:44 +08:00
2018-11-09 05:03:10 +08:00
<template v-else>
<button
class="menububble__button"
@click="showLinkMenu(markAttrs('link'))"
:class="{ 'is-active': isActive('link') }"
>
<span>Add Link</span>
<icon name="link" />
</button>
</template>
2018-10-24 05:21:45 +08:00
2018-11-09 05:03:10 +08:00
</div>
</menu-bubble>
2018-10-24 05:21:45 +08:00
2018-11-09 05:03:10 +08:00
<editor-content class="editor__content" :editor="editor" />
</div>
2018-08-22 20:10:44 +08:00
</template>
<script>
import Icon from 'Components/Icon'
2018-10-24 05:21:45 +08:00
import { Editor, EditorContent, MenuBubble } from 'tiptap'
2018-08-24 04:08:19 +08:00
import {
2018-11-09 05:03:10 +08:00
Blockquote,
BulletList,
CodeBlock,
HardBreak,
Heading,
ListItem,
OrderedList,
TodoItem,
TodoList,
Bold,
Code,
Italic,
Link,
History,
2018-08-24 04:08:19 +08:00
} from 'tiptap-extensions'
2018-08-22 20:10:44 +08:00
export default {
2018-11-09 05:03:10 +08:00
components: {
EditorContent,
MenuBubble,
Icon,
},
data() {
return {
editor: new Editor({
extensions: [
new Blockquote(),
new BulletList(),
new CodeBlock(),
new HardBreak(),
new Heading({ levels: [1, 2, 3] }),
new ListItem(),
new OrderedList(),
new TodoItem(),
new TodoList(),
new Bold(),
new Code(),
new Italic(),
new Link(),
new History(),
],
content: `
<h2>
Links
</h2>
<p>
Try to add some links to the <a href="https://en.wikipedia.org/wiki/World_Wide_Web">world wide web</a>. By default every link will get a <code>rel="noopener noreferrer nofollow"</code> attribute.
</p>
`,
}),
linkUrl: null,
linkMenuIsActive: false,
}
},
methods: {
showLinkMenu(attrs) {
this.linkUrl = attrs.href
this.linkMenuIsActive = true
this.$nextTick(() => {
this.$refs.linkInput.focus()
})
},
hideLinkMenu() {
this.linkUrl = null
this.linkMenuIsActive = false
},
setLinkUrl(command, url) {
command({ href: url })
this.hideLinkMenu()
this.editor.focus()
},
},
2018-08-22 20:10:44 +08:00
}
2018-11-09 05:03:10 +08:00
</script>