add placeholder extension

This commit is contained in:
Philipp Kühn 2018-09-06 22:58:54 +02:00
parent 80ec395c3a
commit db61d67156
7 changed files with 89 additions and 1 deletions

View File

@ -0,0 +1,36 @@
<template>
<div>
<editor class="editor" :extensions="extensions">
<div class="editor__content" slot="content" slot-scope="props"></div>
</editor>
</div>
</template>
<script>
import { Editor } from 'tiptap'
import { PlaceholderExtension } from 'tiptap-extensions'
export default {
components: {
Editor,
},
data() {
return {
extensions: [
new PlaceholderExtension(),
],
}
},
}
</script>
<style lang="scss">
.editor p.is-empty:first-child::before {
content: 'Start typing…';
float: left;
color: #aaa;
pointer-events: none;
height: 0;
font-style: italic;
}
</style>

View File

@ -30,6 +30,9 @@
<router-link class="subnavigation__link" to="/embeds">
Embeds
</router-link>
<router-link class="subnavigation__link" to="/placeholder">
Placeholder
</router-link>
<router-link class="subnavigation__link" to="/export">
Export HTML or JSON
</router-link>

View File

@ -13,6 +13,7 @@ import RouteMarkdownShortcuts from 'Components/Routes/MarkdownShortcuts'
import RouteCodeHighlighting from 'Components/Routes/CodeHighlighting'
import RouteReadOnly from 'Components/Routes/ReadOnly'
import RouteEmbeds from 'Components/Routes/Embeds'
import RoutePlaceholder from 'Components/Routes/Placeholder'
import RouteExport from 'Components/Routes/Export'
const __svg__ = { path: './assets/images/icons/*.svg', name: 'assets/images/[hash].sprite.svg' }
@ -93,6 +94,13 @@ const routes = [
githubUrl: 'https://github.com/heyscrumpy/tiptap/tree/master/examples/Components/Routes/Embeds',
},
},
{
path: '/placeholder',
component: RoutePlaceholder,
meta: {
githubUrl: 'https://github.com/heyscrumpy/tiptap/tree/master/examples/Components/Routes/Placeholder',
},
},
{
path: '/export',
component: RouteExport,

View File

@ -22,6 +22,7 @@
"dependencies": {
"lowlight": "^1.10.0",
"prosemirror-history": "^1.0.2",
"prosemirror-view": "^1.5.1",
"tiptap": "^0.10.0",
"tiptap-commands": "^0.3.0"
}

View File

@ -0,0 +1,39 @@
import { Extension, Plugin } from 'tiptap'
import { Decoration, DecorationSet } from 'prosemirror-view'
export default class PlaceholderExtension extends Extension {
get name() {
return 'placeholder'
}
get defaultOptions() {
return {
emptyNodeClass: 'is-empty',
}
}
get plugins() {
return [
new Plugin({
props: {
decorations: state => {
const decorations = []
state.doc.descendants((node, pos) => {
if (node.type.isBlock && node.childCount === 0) {
const decoration = Decoration.node(pos, pos + node.nodeSize, {
class: this.options.emptyNodeClass,
})
decorations.push(decoration)
}
})
return DecorationSet.create(state.doc, decorations)
},
},
}),
]
}
}

View File

@ -17,3 +17,4 @@ export { default as LinkMark } from './marks/Link'
export { default as StrikeMark } from './marks/Strike'
export { default as HistoryExtension } from './extensions/History'
export { default as PlaceholderExtension } from './extensions/Placeholder'

View File

@ -7791,7 +7791,7 @@ prosemirror-utils@^0.6.5:
version "0.6.5"
resolved "https://registry.yarnpkg.com/prosemirror-utils/-/prosemirror-utils-0.6.5.tgz#df18e39178d510917838a7337a8b64561324a70b"
prosemirror-view@^1.0.0, prosemirror-view@^1.4.3:
prosemirror-view@^1.0.0, prosemirror-view@^1.4.3, prosemirror-view@^1.5.1:
version "1.5.1"
resolved "https://registry.yarnpkg.com/prosemirror-view/-/prosemirror-view-1.5.1.tgz#545176a65124a89c9d16571797a9ef54853628c4"
dependencies: