feat: add getText() and generateText() methods (fix #1428) (#1875)

* move getTextBetween method

* add getText method

* refactoring

* refactoring

* refactoring

* move renderText to schema, add generateText method

* add GenerateText demo

* docs: update

* remove demo from html page
This commit is contained in:
Philipp Kühn
2021-09-09 23:51:05 +02:00
committed by GitHub
parent 42e8755d87
commit fe6a3e7491
15 changed files with 241 additions and 64 deletions

View File

@@ -1,37 +1,6 @@
import { Editor } from '@tiptap/core'
import { Plugin, PluginKey } from 'prosemirror-state'
import { Extension } from '../Extension'
const textBetween = (
editor: Editor,
from: number,
to: number,
blockSeparator?: string,
leafText?: string,
): string => {
let text = ''
let separated = true
editor.state.doc.nodesBetween(from, to, (node, pos) => {
const textSerializer = editor.extensionManager.textSerializers[node.type.name]
if (textSerializer) {
text += textSerializer({ node })
separated = !blockSeparator
} else if (node.isText) {
text += node?.text?.slice(Math.max(from, pos) - pos, to - pos)
separated = !blockSeparator
} else if (node.isLeaf && leafText) {
text += leafText
separated = !blockSeparator
} else if (!separated && node.isBlock) {
text += blockSeparator
separated = true
}
}, 0)
return text
}
import getTextBetween from '../helpers/getTextBetween'
export const ClipboardTextSerializer = Extension.create({
name: 'editable',
@@ -43,9 +12,15 @@ export const ClipboardTextSerializer = Extension.create({
props: {
clipboardTextSerializer: () => {
const { editor } = this
const { from, to } = editor.state.selection
const { state, extensionManager } = editor
const { doc, selection } = state
const { from, to } = selection
const { textSerializers } = extensionManager
const range = { from, to }
return textBetween(editor, from, to, '\n')
return getTextBetween(doc, range, {
textSerializers,
})
},
},
}),