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

232 lines
4.5 KiB
Vue
Raw Normal View History

2018-08-28 15:31:08 +08:00
<template>
<div>
2018-10-24 05:05:15 +08:00
<div class="editor">
2018-11-05 05:43:26 +08:00
<menu-bar :editor="editor">
<div class="menubar" slot-scope="{ commands, isActive }">
2018-08-28 15:31:08 +08:00
<button
class="menubar__button"
2018-10-30 06:31:13 +08:00
:class="{ 'is-active': isActive('bold') }"
2018-10-29 05:57:05 +08:00
@click="commands.bold"
2018-08-28 15:31:08 +08:00
>
<icon name="bold" />
</button>
<button
class="menubar__button"
2018-10-30 06:31:13 +08:00
:class="{ 'is-active': isActive('italic') }"
2018-10-29 05:57:05 +08:00
@click="commands.italic"
2018-08-28 15:31:08 +08:00
>
<icon name="italic" />
</button>
<button
class="menubar__button"
2018-10-30 06:31:13 +08:00
:class="{ 'is-active': isActive('code') }"
2018-10-29 05:57:05 +08:00
@click="commands.code"
>
2018-08-28 15:31:08 +08:00
<icon name="code" />
</button>
<button
class="menubar__button"
2018-10-30 06:31:13 +08:00
:class="{ 'is-active': isActive('paragraph') }"
2018-10-29 05:57:05 +08:00
@click="commands.paragraph"
2018-08-28 15:31:08 +08:00
>
<icon name="paragraph" />
</button>
<button
class="menubar__button"
2018-10-30 06:31:13 +08:00
:class="{ 'is-active': isActive('heading', { level: 1 }) }"
2018-10-29 05:57:05 +08:00
@click="commands.heading({ level: 1 })"
2018-08-28 15:31:08 +08:00
>
H1
</button>
<button
class="menubar__button"
2018-10-30 06:31:13 +08:00
:class="{ 'is-active': isActive('heading', { level: 2 }) }"
2018-10-29 05:57:05 +08:00
@click="commands.heading({ level: 2 })"
2018-08-28 15:31:08 +08:00
>
H2
</button>
<button
class="menubar__button"
2018-10-30 06:31:13 +08:00
:class="{ 'is-active': isActive('heading', { level: 3 }) }"
2018-10-29 05:57:05 +08:00
@click="commands.heading({ level: 3 })"
2018-08-28 15:31:08 +08:00
>
H3
</button>
<button
class="menubar__button"
2018-10-30 06:31:13 +08:00
:class="{ 'is-active': isActive('bullet_list') }"
2018-10-29 05:57:05 +08:00
@click="commands.bullet_list"
2018-08-28 15:31:08 +08:00
>
<icon name="ul" />
</button>
<button
class="menubar__button"
2018-10-30 06:31:13 +08:00
:class="{ 'is-active': isActive('ordered_list') }"
2018-10-29 05:57:05 +08:00
@click="commands.ordered_list"
2018-08-28 15:31:08 +08:00
>
<icon name="ol" />
</button>
<button
class="menubar__button"
2018-10-30 06:31:13 +08:00
:class="{ 'is-active': isActive('code_block') }"
2018-10-29 05:57:05 +08:00
@click="commands.code_block"
2018-08-28 15:31:08 +08:00
>
<icon name="code" />
</button>
2018-11-05 05:43:26 +08:00
</div>
2018-10-24 05:05:15 +08:00
</menu-bar>
2018-08-28 15:31:08 +08:00
2018-10-24 05:05:15 +08:00
<editor-content class="editor__content" :editor="editor" />
2018-08-28 15:31:08 +08:00
2018-10-24 05:05:15 +08:00
</div>
2018-08-28 15:31:08 +08:00
<div class="actions">
<button class="button" @click="clearContent">
Clear Content
</button>
<button class="button" @click="setContent">
Set Content
</button>
</div>
2018-08-28 15:31:08 +08:00
<div class="export">
<h3>JSON</h3>
<pre><code v-html="json"></code></pre>
<h3>HTML</h3>
<pre><code>{{ html }}</code></pre>
</div>
</div>
</template>
<script>
import Icon from 'Components/Icon'
2018-10-24 05:05:15 +08:00
import { Editor, EditorContent, MenuBar } from 'tiptap'
2018-08-28 15:31:08 +08:00
import {
2018-10-24 13:46:47 +08:00
Blockquote,
CodeBlock,
HardBreak,
Heading,
OrderedList,
BulletList,
ListItem,
2018-10-24 13:46:47 +08:00
TodoItem,
TodoList,
Bold,
Code,
Italic,
Link,
History,
2018-08-28 15:31:08 +08:00
} from 'tiptap-extensions'
export default {
components: {
2018-10-24 05:05:15 +08:00
EditorContent,
MenuBar,
2018-08-28 15:31:08 +08:00
Icon,
},
data() {
return {
2018-10-24 05:05:15 +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:05:15 +08:00
],
content: `
<h2>
Export HTML or JSON
</h2>
<p>
You are able to export your data as <code>HTML</code> or <code>JSON</code>. To pass <code>HTML</code> to the editor use the <code>content</code> slot. To pass <code>JSON</code> to the editor use the <code>doc</code> prop.
</p>
`,
onUpdate: ({ getJSON, getHTML }) => {
this.json = getJSON()
this.html = getHTML()
},
}),
2018-08-28 15:31:08 +08:00
json: 'Update content to see changes',
html: 'Update content to see changes',
}
},
methods: {
clearContent() {
2018-10-24 05:05:15 +08:00
this.editor.clearContent(true)
this.editor.focus()
},
setContent() {
// you can pass a json document
2018-10-24 05:05:15 +08:00
this.editor.setContent({
type: 'doc',
content: [{
type: 'paragraph',
content: [
{
type: 'text',
text: 'This is some inserted text. 👋',
},
],
}],
}, true)
// HTML string is also supported
2018-10-24 05:05:15 +08:00
// this.editor.setContent('<p>This is some inserted text. 👋</p>')
2018-10-24 05:05:15 +08:00
this.editor.focus()
},
2018-08-28 15:31:08 +08:00
},
}
</script>
<style lang="scss" scoped>
@import "~variables";
.actions {
max-width: 30rem;
margin: 0 auto 2rem auto;
}
2018-08-28 15:31:08 +08:00
.export {
max-width: 30rem;
margin: 0 auto 2rem auto;
2018-08-28 15:31:08 +08:00
pre {
padding: 1rem;
border-radius: 5px;
font-size: 0.8rem;
font-weight: bold;
background: rgba($color-black, 0.05);
color: rgba($color-black, 0.8);
}
code {
display: block;
white-space: pre-wrap;
}
}
</style>