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

106 lines
2.2 KiB
Vue
Raw Normal View History

2018-08-22 20:10:44 +08:00
<template>
<div>
2018-08-24 04:08:19 +08:00
<editor class="editor" :extensions="extensions" @update="onUpdate">
2018-08-22 20:10:44 +08:00
<div class="menububble" slot="menububble" slot-scope="{ marks, focus }">
<template v-if="marks">
<form class="menububble__form" v-if="linkMenuIsActive" @submit.prevent="setLinkUrl(linkUrl, marks.link, focus)">
<input class="menububble__input" type="text" v-model="linkUrl" placeholder="https://" ref="linkInput" @keydown.esc="hideLinkMenu"/>
<button class="menububble__button" @click="setLinkUrl(null, marks.link, focus)" type="button">
<icon name="remove" />
</button>
</form>
<template v-else>
2018-08-22 21:58:32 +08:00
<button
class="menububble__button"
@click="showLinkMenu(marks.link)"
:class="{ 'is-active': marks.link.active() }"
>
2018-08-22 20:10:44 +08:00
<span>Add Link</span>
<icon name="link" />
</button>
</template>
</template>
</div>
<div class="editor__content" slot="content" slot-scope="props">
2018-08-23 14:36:10 +08:00
<h2>
2018-08-22 22:48:52 +08:00
Links
2018-08-23 14:36:10 +08:00
</h2>
2018-08-22 20:10:44 +08:00
<p>
Try to add some links to the <a href="https://en.wikipedia.org/wiki/World_Wide_Web">world wide web</a>.
</p>
</div>
</editor>
</div>
</template>
<script>
import Icon from 'Components/Icon'
import { Editor } from 'tiptap'
2018-08-24 04:08:19 +08:00
import {
Blockquote,
BulletList,
CodeBlock,
HardBreak,
Heading,
ListItem,
OrderedList,
TodoItem,
TodoList,
Bold,
Code,
Italic,
Link,
} from 'tiptap-extensions'
2018-08-22 20:10:44 +08:00
export default {
2018-08-23 01:28:57 +08:00
components: {
Editor,
Icon,
2018-08-22 20:10:44 +08:00
},
data() {
2018-08-23 01:28:57 +08:00
return {
2018-08-24 04:08:19 +08:00
extensions: [
new Blockquote(),
new BulletList(),
new CodeBlock(),
new HardBreak(),
2018-08-24 14:41:17 +08:00
new Heading({ maxLevel: 3 }),
2018-08-24 04:08:19 +08:00
new ListItem(),
new OrderedList(),
new TodoItem(),
new TodoList(),
new Bold(),
new Code(),
new Italic(),
new Link(),
],
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: {
showLinkMenu(type) {
this.linkUrl = type.attrs.href
this.linkMenuIsActive = true
this.$nextTick(() => {
this.$refs.linkInput.focus()
})
},
hideLinkMenu() {
this.linkUrl = null
this.linkMenuIsActive = false
},
setLinkUrl(url, type, focus) {
type.command({ href: url })
this.hideLinkMenu()
focus()
},
onUpdate(state) {
// console.log(state.doc.toJSON())
},
},
2018-08-22 20:10:44 +08:00
}
</script>