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

@@ -11,7 +11,9 @@ import isActive from './helpers/isActive'
import removeElement from './utilities/removeElement'
import createDocument from './helpers/createDocument'
import getHTMLFromFragment from './helpers/getHTMLFromFragment'
import getText from './helpers/getText'
import isNodeEmpty from './helpers/isNodeEmpty'
import getTextSeralizersFromSchema from './helpers/getTextSeralizersFromSchema'
import createStyleTag from './utilities/createStyleTag'
import CommandManager from './CommandManager'
import ExtensionManager from './ExtensionManager'
@@ -21,6 +23,7 @@ import {
CanCommands,
ChainedCommands,
SingleCommands,
TextSerializer,
} from './types'
import * as extensions from './extensions'
import style from './style'
@@ -394,6 +397,27 @@ export class Editor extends EventEmitter {
return getHTMLFromFragment(this.state.doc, this.schema)
}
/**
* Get the document as text.
*/
public getText(options?: {
blockSeparator?: string,
textSerializers?: Record<string, TextSerializer>,
}): string {
const {
blockSeparator = '\n\n',
textSerializers = {},
} = options || {}
return getText(this.state.doc, {
blockSeparator,
textSerializers: {
...textSerializers,
...getTextSeralizersFromSchema(this.schema),
},
})
}
/**
* Check if there is no content.
*/