add getCharacterCount method to the editor

This commit is contained in:
Hans Pagel
2021-01-27 11:40:49 +01:00
parent 071322bfb7
commit 06b6fc25d4
2 changed files with 37 additions and 29 deletions

View File

@@ -145,7 +145,7 @@ new Editor({
<!-- <!--
| Setting | Type | Default | Description | | Setting | Type | Default | Description |
| ------------------- | --------------- | -------- | ---------------------------------------------------------------------------------------------------------------------------------------------------- | | ------------------ | --------------- | -------- | ---------------------------------------------------------------------------------------------------------------------------------------------------- |
| `autofocus` | `Boolean` | `false` | Focus the editor on init. | | `autofocus` | `Boolean` | `false` | Focus the editor on init. |
| `content` | `Object|String` | `null` | The editor state object used by Prosemirror. You can also pass HTML to the `content` slot. When used both, the `content` slot will be ignored. | | `content` | `Object|String` | `null` | The editor state object used by Prosemirror. You can also pass HTML to the `content` slot. When used both, the `content` slot will be ignored. |
| `editable` | `Boolean` | `true` | When set to `false` the editor is read-only. | | `editable` | `Boolean` | `true` | When set to `false` the editor is read-only. |
@@ -153,7 +153,7 @@ new Editor({
| `element` | `Element` | `false` | Focus the editor on init. | | `element` | `Element` | `false` | Focus the editor on init. |
| `extensions` | `Array` | `[]` | A list of extensions you would like to use. Can be [`Nodes`](/api/nodes), [`Marks`](/api/marks) or [`Extensions`](/api/extensions). | | `extensions` | `Array` | `[]` | A list of extensions you would like to use. Can be [`Nodes`](/api/nodes), [`Marks`](/api/marks) or [`Extensions`](/api/extensions). |
| `injectCSS` | `Boolean` | `true` | When set to `false` tiptap wont load [the default ProseMirror CSS](https://github.com/ueberdosis/tiptap-next/tree/main/packages/core/src/style.ts). | | `injectCSS` | `Boolean` | `true` | When set to `false` tiptap wont load [the default ProseMirror CSS](https://github.com/ueberdosis/tiptap-next/tree/main/packages/core/src/style.ts). |
| ~~`parseOptions`~~ | ~~`Object`~~ | ~~`{}`~~ | ~~A list of [Prosemirror parseOptions](https://prosemirror.net/docs/ref/#model.ParseOptions).~~ | --> | ~~`parseOptions`~~ | ~~`Object`~~ | ~~`{}`~~ | ~~A list of [Prosemirror parseOptions](https://prosemirror.net/docs/ref/#model.ParseOptions).~~ | --> |
## List of available methods ## List of available methods
An editor instance will provide the following public methods. Theyll help you to work with the editor. An editor instance will provide the following public methods. Theyll help you to work with the editor.
@@ -161,7 +161,7 @@ An editor instance will provide the following public methods. Theyll help you
Dont confuse methods with [commands](/api/commands), which are used to change the state of editor (content, selection, and so on) and only return `true` or `false`. Dont confuse methods with [commands](/api/commands), which are used to change the state of editor (content, selection, and so on) and only return `true` or `false`.
| Method | Parameters | Description | | Method | Parameters | Description |
| -------------------- | ----------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------- | | --------------------- | ----------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------- |
| `can()` | - | Check if a command or a command chain can be executed. Without executing it. | | `can()` | - | Check if a command or a command chain can be executed. Without executing it. |
| `chain()` | - | Create a command chain to call multiple commands at once. | | `chain()` | - | Create a command chain to call multiple commands at once. |
| `createDocument()` | `content` EditorContent<br>`parseOptions` | Creates a ProseMirror document. | | `createDocument()` | `content` EditorContent<br>`parseOptions` | Creates a ProseMirror document. |
@@ -173,6 +173,7 @@ Dont confuse methods with [commands](/api/commands), which are used to change
| `isActive()` | `name` Name of the node or mark<br>`attrs` Attributes of the node or mark | Returns if the currently selected node or mark is active. | | `isActive()` | `name` Name of the node or mark<br>`attrs` Attributes of the node or mark | Returns if the currently selected node or mark is active. |
| `isEditable()` | - | Returns whether the editor is editable. | | `isEditable()` | - | Returns whether the editor is editable. |
| `isEmpty()` | - | Check if there is no content. | | `isEmpty()` | - | Check if there is no content. |
| `getCharacterCount()` | - | Get the number of characters for the current document. |
| `registerCommand()` | `name` The name of your command<br>`callback` The method of your command | Register a command. | | `registerCommand()` | `name` The name of your command<br>`callback` The method of your command | Register a command. |
| `registerCommands()` | `commands` A list of commands | Register a list of commands. | | `registerCommands()` | `commands` A list of commands | Register a list of commands. |
| `registerPlugin()` | `plugin` A ProseMirror plugin<br>`handlePlugins` Control how to merge the plugin into the existing plugins. | Register a ProseMirror plugin. | | `registerPlugin()` | `plugin` A ProseMirror plugin<br>`handlePlugins` Control how to merge the plugin into the existing plugins. | Register a ProseMirror plugin. |

View File

@@ -395,6 +395,13 @@ export class Editor extends EventEmitter {
return JSON.stringify(defaultContent) === JSON.stringify(content) return JSON.stringify(defaultContent) === JSON.stringify(content)
} }
/**
* Get the number of characters for the current document.
*/
public getCharacterCount() {
return this.state.doc.content.size - 2
}
/** /**
* Destroy the editor. * Destroy the editor.
*/ */