mirror of
https://github.com/ueberdosis/tiptap.git
synced 2024-11-25 12:39:03 +08:00
docs: add a word break experiment
This commit is contained in:
parent
f05e237346
commit
ef6bf3616e
69
docs/src/demos/Experiments/WordBreak/index.vue
Normal file
69
docs/src/demos/Experiments/WordBreak/index.vue
Normal 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>
|
71
docs/src/demos/Experiments/WordBreak/word-break.ts
Normal file
71
docs/src/demos/Experiments/WordBreak/word-break.ts
Normal 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,
|
||||
}
|
||||
}
|
||||
},
|
||||
})
|
@ -9,3 +9,4 @@ Congratulations! You’ve 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)
|
||||
|
5
docs/src/docPages/experiments/word-break.md
Normal file
5
docs/src/docPages/experiments/word-break.md
Normal file
@ -0,0 +1,5 @@
|
||||
# Word break
|
||||
|
||||
⚠️ Experiment
|
||||
|
||||
<demo name="Experiments/WordBreak" />
|
Loading…
Reference in New Issue
Block a user