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

113 lines
2.4 KiB
Vue
Raw Normal View History

2018-08-22 20:10:44 +08:00
<template>
2018-10-24 05:21:45 +08:00
<div class="editor">
<menu-bubble class="menububble" :editor="editor">
2018-11-05 01:03:39 +08:00
<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-10-30 06:58:07 +08:00
<form class="menububble__form" v-if="linkMenuIsActive" @submit.prevent="setLinkUrl(commands.link, linkUrl)">
2018-10-24 05:21:45 +08:00
<input class="menububble__input" type="text" v-model="linkUrl" placeholder="https://" ref="linkInput" @keydown.esc="hideLinkMenu"/>
2018-10-30 06:58:07 +08:00
<button class="menububble__button" @click="setLinkUrl(commands.link, null)" type="button">
2018-10-24 05:21:45 +08:00
<icon name="remove" />
</button>
</form>
2018-08-22 20:10:44 +08:00
2018-10-24 05:21:45 +08:00
<template v-else>
<button
class="menububble__button"
2018-10-30 06:58:07 +08:00
@click="showLinkMenu(markAttrs('link'))"
:class="{ 'is-active': isActive('link') }"
2018-10-24 05:21:45 +08:00
>
<span>Add Link</span>
<icon name="link" />
</button>
2018-08-22 20:10:44 +08:00
</template>
2018-10-24 05:21:45 +08:00
2018-11-05 01:03:39 +08:00
</div>
2018-10-24 05:21:45 +08:00
</menu-bubble>
<editor-content class="editor__content" :editor="editor" />
2018-08-22 20:10:44 +08:00
</div>
</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-10-24 13:46:47 +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-08-23 01:28:57 +08:00
components: {
2018-10-24 05:21:45 +08:00
EditorContent,
MenuBubble,
2018-08-23 01:28:57 +08:00
Icon,
2018-08-22 20:10:44 +08:00
},
data() {
2018-08-23 01:28:57 +08:00
return {
2018-10-24 05:21:45 +08:00
editor: new Editor({
extensions: [
2018-10-24 13:46:47 +08:00
new Blockquote(),
new BulletList(),
new CodeBlock(),
new HardBreak(),
new Heading({ levels: [1, 2, 3] }),
2018-10-24 13:46:47 +08:00
new ListItem(),
new OrderedList(),
new TodoItem(),
new TodoList(),
new Bold(),
new Code(),
new Italic(),
new Link(),
new History(),
2018-10-24 05:21:45 +08:00
],
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>
`,
}),
2018-08-23 01:28:57 +08:00
linkUrl: null,
2018-08-22 20:10:44 +08:00
linkMenuIsActive: false,
}
},
2018-08-23 01:28:57 +08:00
methods: {
2018-10-30 06:58:07 +08:00
showLinkMenu(attrs) {
this.linkUrl = attrs.href
2018-08-23 01:28:57 +08:00
this.linkMenuIsActive = true
this.$nextTick(() => {
this.$refs.linkInput.focus()
})
},
hideLinkMenu() {
this.linkUrl = null
this.linkMenuIsActive = false
},
2018-10-30 06:58:07 +08:00
setLinkUrl(command, url) {
command({ href: url })
2018-08-23 01:28:57 +08:00
this.hideLinkMenu()
2018-10-24 05:21:45 +08:00
this.editor.focus()
2018-08-23 01:28:57 +08:00
},
},
2018-08-22 20:10:44 +08:00
}
</script>