add basic image support

This commit is contained in:
Philipp Kühn
2018-08-31 20:42:13 +02:00
parent 33575c0856
commit 8e2e037828
8 changed files with 135 additions and 4 deletions

View File

@@ -72,6 +72,12 @@
margin: 0; margin: 0;
} }
} }
img {
max-width: 100%;
border-radius: 3px;
}
} }
} }

View File

@@ -0,0 +1,67 @@
<template>
<div>
<editor class="editor" :extensions="extensions">
<div class="editor__content" slot="content" slot-scope="props">
<h2>
Images
</h2>
<p>
Nice
</p>
<img src="https://tiptap.scrumpy.io/assets/images/open-graph.png" />
</div>
</editor>
</div>
</template>
<script>
import Icon from 'Components/Icon'
import { Editor } from 'tiptap'
import {
BlockquoteNode,
BulletListNode,
CodeBlockNode,
HardBreakNode,
HeadingNode,
ImageNode,
ListItemNode,
OrderedListNode,
TodoItemNode,
TodoListNode,
BoldMark,
CodeMark,
ItalicMark,
LinkMark,
HistoryExtension,
} from 'tiptap-extensions'
export default {
components: {
Editor,
Icon,
},
data() {
return {
extensions: [
new BlockquoteNode(),
new BulletListNode(),
new CodeBlockNode(),
new HardBreakNode(),
new HeadingNode({ maxLevel: 3 }),
new ImageNode(),
new ListItemNode(),
new OrderedListNode(),
new TodoItemNode(),
new TodoListNode(),
new BoldMark(),
new CodeMark(),
new ItalicMark(),
new LinkMark(),
new HistoryExtension(),
],
}
},
}
</script>

View File

@@ -48,13 +48,13 @@
There is always something to do. Thankfully, there are checklists for that. Don't forget to call mom. There is always something to do. Thankfully, there are checklists for that. Don't forget to call mom.
</p> </p>
<ul data-type="todo_list"> <ul data-type="todo_list">
<li data-type="todo_item" data-done="true"> <li data-type="todo_item" data-done="true" :custom-prop="customProp">
Buy beer Buy beer
</li> </li>
<li data-type="todo_item" data-done="true"> <li data-type="todo_item" data-done="true" :custom-prop="customProp">
Buy meat Buy meat
</li> </li>
<li data-type="todo_item" data-done="true"> <li data-type="todo_item" data-done="true" :custom-prop="customProp">
Buy milk Buy milk
</li> </li>
<li data-type="todo_item" data-done="false"> <li data-type="todo_item" data-done="false">
@@ -94,6 +94,7 @@ export default {
}, },
data() { data() {
return { return {
customProp: 2,
extensions: [ extensions: [
new BlockquoteNode(), new BlockquoteNode(),
new BulletListNode(), new BulletListNode(),

View File

@@ -9,6 +9,9 @@
<router-link class="subnavigation__link" to="/links"> <router-link class="subnavigation__link" to="/links">
Links Links
</router-link> </router-link>
<router-link class="subnavigation__link" to="/images">
Images
</router-link>
<router-link class="subnavigation__link" to="/hiding-menu-bar"> <router-link class="subnavigation__link" to="/hiding-menu-bar">
Hiding Menu Bar Hiding Menu Bar
</router-link> </router-link>

View File

@@ -6,6 +6,7 @@ import App from 'Components/App'
import RouteBasic from 'Components/Routes/Basic' import RouteBasic from 'Components/Routes/Basic'
import RouteMenuBubble from 'Components/Routes/MenuBubble' import RouteMenuBubble from 'Components/Routes/MenuBubble'
import RouteLinks from 'Components/Routes/Links' import RouteLinks from 'Components/Routes/Links'
import RouteImages from 'Components/Routes/Images'
import RouteHidingMenuBar from 'Components/Routes/HidingMenuBar' import RouteHidingMenuBar from 'Components/Routes/HidingMenuBar'
import RouteTodoList from 'Components/Routes/TodoList' import RouteTodoList from 'Components/Routes/TodoList'
import RouteMarkdownShortcuts from 'Components/Routes/MarkdownShortcuts' import RouteMarkdownShortcuts from 'Components/Routes/MarkdownShortcuts'
@@ -42,6 +43,13 @@ const routes = [
githubUrl: 'https://github.com/heyscrumpy/tiptap/tree/master/examples/Components/Routes/Links', githubUrl: 'https://github.com/heyscrumpy/tiptap/tree/master/examples/Components/Routes/Links',
}, },
}, },
{
path: '/images',
component: RouteImages,
meta: {
githubUrl: 'https://github.com/heyscrumpy/tiptap/tree/master/examples/Components/Routes/Images',
},
},
{ {
path: '/hiding-menu-bar', path: '/hiding-menu-bar',
component: RouteHidingMenuBar, component: RouteHidingMenuBar,

View File

@@ -3,6 +3,7 @@ export { default as BulletListNode } from './nodes/BulletList'
export { default as CodeBlockNode } from './nodes/CodeBlock' export { default as CodeBlockNode } from './nodes/CodeBlock'
export { default as HardBreakNode } from './nodes/HardBreak' export { default as HardBreakNode } from './nodes/HardBreak'
export { default as HeadingNode } from './nodes/Heading' export { default as HeadingNode } from './nodes/Heading'
export { default as ImageNode } from './nodes/Image'
export { default as ListItemNode } from './nodes/ListItem' export { default as ListItemNode } from './nodes/ListItem'
export { default as OrderedListNode } from './nodes/OrderedList' export { default as OrderedListNode } from './nodes/OrderedList'
export { default as TodoItemNode } from './nodes/TodoItem' export { default as TodoItemNode } from './nodes/TodoItem'

View File

@@ -0,0 +1,37 @@
import { Node } from 'tiptap'
export default class ImageNode extends Node {
get name() {
return 'image'
}
get schema() {
return {
inline: true,
attrs: {
src: {},
alt: {
default: null,
},
title: {
default: null,
},
},
group: 'inline',
draggable: true,
parseDOM: [
{
tag: 'img[src]',
getAttrs: dom => ({
src: dom.getAttribute('src'),
title: dom.getAttribute('title'),
alt: dom.getAttribute('alt'),
}),
},
],
toDOM: node => ['img', node.attrs],
}
}
}

View File

@@ -21,6 +21,9 @@ export default class TodoItemNode extends Node {
<li data-type="todo_item" :data-done="node.attrs.done.toString()"> <li data-type="todo_item" :data-done="node.attrs.done.toString()">
<span class="todo-checkbox" contenteditable="false" @click="onChange"></span> <span class="todo-checkbox" contenteditable="false" @click="onChange"></span>
<div class="todo-content" ref="content" :contenteditable="editable.toString()"></div> <div class="todo-content" ref="content" :contenteditable="editable.toString()"></div>
<div>
{{ node.attrs.customProp }}
</DIV>
</li> </li>
`, `,
} }
@@ -32,15 +35,19 @@ export default class TodoItemNode extends Node {
done: { done: {
default: false, default: false,
}, },
customProp: {
default: null,
},
}, },
draggable: false, draggable: false,
content: 'paragraph', content: 'paragraph',
toDOM(node) { toDOM(node) {
const { done } = node.attrs const { done, customProp } = node.attrs
return ['li', { return ['li', {
'data-type': 'todo_item', 'data-type': 'todo_item',
'data-done': done.toString(), 'data-done': done.toString(),
'custom-prop': customProp.toString(),
}, },
['span', { class: 'todo-checkbox', contenteditable: 'false' }], ['span', { class: 'todo-checkbox', contenteditable: 'false' }],
['div', { class: 'todo-content' }, 0], ['div', { class: 'todo-content' }, 0],
@@ -51,6 +58,7 @@ export default class TodoItemNode extends Node {
tag: '[data-type="todo_item"]', tag: '[data-type="todo_item"]',
getAttrs: dom => ({ getAttrs: dom => ({
done: dom.getAttribute('data-done') === 'true', done: dom.getAttribute('data-done') === 'true',
customProp: dom.getAttribute('custom-prop'),
}), }),
}], }],
} }