docs: add a word break experiment

This commit is contained in:
Hans Pagel 2021-04-15 19:34:05 +02:00
parent f05e237346
commit ef6bf3616e
4 changed files with 146 additions and 0 deletions

View File

@ -0,0 +1,69 @@
<template>
<div v-if="editor">
<button @click="editor.chain().focus().setWordBreak().run()" :class="{ 'is-active': editor.isActive('wordBreak') }">
wbr
</button>
<editor-content :editor="editor" />
<h2>HTML</h2>
{{ editor.getHTML() }}
</div>
</template>
<script>
import { Editor, EditorContent } from '@tiptap/vue-2'
import { defaultExtensions } from '@tiptap/starter-kit'
import { WordBreak } from './word-break'
export default {
components: {
EditorContent,
},
data() {
return {
editor: null,
}
},
mounted() {
this.editor = new Editor({
extensions: [
...defaultExtensions(),
WordBreak,
],
content: `
<p>super<wbr>longword</p>
`,
})
},
beforeDestroy() {
this.editor.destroy()
},
}
</script>
<style lang="scss" scoped>
::v-deep {
.ProseMirror {
> * + * {
margin-top: 0.75em;
}
}
.word-break {
display: inline-block;
height: 1em;
margin: 0 0.1em;
line-height: 1em;
border: 1px dashed #868e96;
color: #868e96;
&::before {
content: '-';
}
}
}
</style>

View File

@ -0,0 +1,71 @@
import { Command, Node, mergeAttributes } from '@tiptap/core'
import { exitCode } from 'prosemirror-commands'
export interface WordBreakOptions {
HTMLAttributes: {
[key: string]: any
},
}
declare module '@tiptap/core' {
interface Commands {
wordBreak: {
/**
* Add a hard break
*/
setWordBreak: () => Command,
}
}
}
export const WordBreak = Node.create<WordBreakOptions>({
name: 'wordBreak',
defaultOptions: {
HTMLAttributes: {},
},
inline: true,
group: 'inline',
selectable: false,
parseHTML() {
return [
{ tag: 'wbr' },
]
},
renderHTML({ HTMLAttributes }) {
return ['wbr', mergeAttributes(this.options.HTMLAttributes, HTMLAttributes)]
},
addCommands() {
return {
setWordBreak: () => ({ commands, state, dispatch }) => {
return commands.first([
() => exitCode(state, dispatch),
() => {
if (dispatch) {
state.tr.replaceSelectionWith(this.type.create()).scrollIntoView()
}
return true
},
])
},
}
},
addNodeView() {
return () => {
const dom = document.createElement('span')
dom.classList.add('word-break')
return {
dom,
}
}
},
})

View File

@ -9,3 +9,4 @@ Congratulations! Youve found our playground with a list of experiments. Be aw
* [@tiptap/extension-iframe?](/experiments/embeds)
* [@tiptap/extension-toggle-list?](/experiments/details)
* [@tiptap/extension-collaboration-annotation](/experiments/collaboration-annotation)
* [@tiptap/extension-word-break](/experiments/word-break)

View File

@ -0,0 +1,5 @@
# Word break
⚠️ Experiment
<demo name="Experiments/WordBreak" />