@tiptap/core 2.0.0-beta.21 → 2.0.0-beta.210
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/dist/index.cjs +4311 -0
- package/dist/index.d.ts +2330 -0
- package/dist/index.js +4311 -0
- package/package.json +39 -25
- package/src/CommandManager.ts +76 -86
- package/src/Editor.ts +120 -81
- package/src/EventEmitter.ts +14 -4
- package/src/Extension.ts +280 -108
- package/src/ExtensionManager.ts +254 -108
- package/src/InputRule.ts +260 -0
- package/src/Mark.ts +398 -147
- package/src/Node.ts +437 -171
- package/src/NodeView.ts +132 -63
- package/src/PasteRule.ts +240 -0
- package/src/Tracker.ts +38 -0
- package/src/commands/blur.ts +12 -6
- package/src/commands/clearContent.ts +3 -3
- package/src/commands/clearNodes.ts +31 -19
- package/src/commands/command.ts +2 -2
- package/src/commands/createParagraphNear.ts +5 -4
- package/src/commands/deleteCurrentNode.ts +41 -0
- package/src/commands/deleteNode.ts +37 -0
- package/src/commands/deleteRange.ts +3 -3
- package/src/commands/deleteSelection.ts +5 -4
- package/src/commands/enter.ts +3 -3
- package/src/commands/exitCode.ts +5 -4
- package/src/commands/extendMarkRange.ts +16 -12
- package/src/commands/first.ts +3 -3
- package/src/commands/focus.ts +47 -44
- package/src/commands/forEach.ts +24 -0
- package/src/commands/index.ts +50 -0
- package/src/commands/insertContent.ts +17 -24
- package/src/commands/insertContentAt.ts +94 -0
- package/src/commands/join.ts +53 -0
- package/src/commands/keyboardShortcut.ts +6 -6
- package/src/commands/lift.ts +8 -7
- package/src/commands/liftEmptyBlock.ts +5 -4
- package/src/commands/liftListItem.ts +7 -6
- package/src/commands/newlineInCode.ts +5 -4
- package/src/commands/resetAttributes.ts +18 -12
- package/src/commands/scrollIntoView.ts +3 -3
- package/src/commands/selectAll.ts +8 -6
- package/src/commands/selectNodeBackward.ts +5 -4
- package/src/commands/selectNodeForward.ts +5 -4
- package/src/commands/selectParentNode.ts +5 -4
- package/src/commands/selectTextblockEnd.ts +20 -0
- package/src/commands/selectTextblockStart.ts +20 -0
- package/src/commands/setContent.ts +9 -16
- package/src/commands/setMark.ts +90 -14
- package/src/commands/setMeta.ts +18 -0
- package/src/commands/setNode.ts +32 -8
- package/src/commands/setNodeSelection.ts +27 -0
- package/src/commands/setTextSelection.ts +31 -0
- package/src/commands/sinkListItem.ts +7 -6
- package/src/commands/splitBlock.ts +31 -41
- package/src/commands/splitListItem.ts +46 -29
- package/src/commands/toggleList.ts +88 -20
- package/src/commands/toggleMark.ts +19 -8
- package/src/commands/toggleNode.ts +11 -6
- package/src/commands/toggleWrap.ts +10 -10
- package/src/commands/undoInputRule.ts +34 -5
- package/src/commands/unsetAllMarks.ts +7 -11
- package/src/commands/unsetMark.ts +36 -23
- package/src/commands/updateAttributes.ts +27 -15
- package/src/commands/wrapIn.ts +7 -12
- package/src/commands/wrapInList.ts +7 -6
- package/src/extensions/clipboardTextSerializer.ts +15 -36
- package/src/extensions/commands.ts +3 -147
- package/src/extensions/editable.ts +2 -1
- package/src/extensions/focusEvents.ts +4 -6
- package/src/extensions/index.ts +1 -0
- package/src/extensions/keymap.ts +106 -13
- package/src/extensions/tabindex.ts +18 -0
- package/src/helpers/combineTransactionSteps.ts +21 -0
- package/src/helpers/createChainableState.ts +38 -0
- package/src/helpers/createDocument.ts +5 -6
- package/src/helpers/createNodeFromContent.ts +20 -28
- package/src/helpers/defaultBlockAt.ts +13 -0
- package/src/helpers/findChildren.ts +18 -0
- package/src/helpers/findChildrenInRange.ts +36 -0
- package/src/helpers/findParentNode.ts +4 -3
- package/src/helpers/findParentNodeClosestToPos.ts +13 -7
- package/src/helpers/generateHTML.ts +7 -6
- package/src/helpers/generateJSON.ts +12 -0
- package/src/helpers/generateText.ts +27 -0
- package/src/helpers/getAttributes.ts +26 -0
- package/src/helpers/getAttributesFromExtensions.ts +42 -14
- package/src/helpers/getChangedRanges.ts +83 -0
- package/src/helpers/getDebugJSON.ts +54 -0
- package/src/helpers/getExtensionField.ts +25 -0
- package/src/helpers/getHTMLFromFragment.ts +5 -6
- package/src/helpers/getMarkAttributes.ts +18 -11
- package/src/helpers/getMarkRange.ts +41 -8
- package/src/helpers/getMarkType.ts +8 -2
- package/src/helpers/getMarksBetween.ts +34 -10
- package/src/helpers/getNodeAttributes.ts +14 -13
- package/src/helpers/getNodeType.ts +8 -2
- package/src/helpers/getRenderedAttributes.ts +9 -7
- package/src/helpers/getSchema.ts +7 -133
- package/src/helpers/getSchemaByResolvedExtensions.ts +192 -0
- package/src/helpers/getSchemaTypeByName.ts +3 -11
- package/src/helpers/getSchemaTypeNameByName.ts +2 -2
- package/src/helpers/getSplittedAttributes.ts +4 -4
- package/src/helpers/getText.ts +19 -0
- package/src/helpers/getTextBetween.ts +46 -0
- package/src/helpers/getTextContentFromNodes.ts +26 -0
- package/src/helpers/getTextSerializersFromSchema.ts +11 -0
- package/src/helpers/index.ts +33 -0
- package/src/helpers/injectExtensionAttributesToParseRule.ts +22 -23
- package/src/helpers/isActive.ts +10 -6
- package/src/helpers/isExtensionRulesEnabled.ts +15 -0
- package/src/helpers/isList.ts +14 -7
- package/src/helpers/isMarkActive.ts +49 -27
- package/src/helpers/isNodeActive.ts +29 -39
- package/src/helpers/isNodeEmpty.ts +2 -2
- package/src/helpers/isNodeSelection.ts +3 -4
- package/src/helpers/isTextSelection.ts +3 -4
- package/src/helpers/posToDOMRect.ts +35 -0
- package/src/helpers/resolveFocusPosition.ts +42 -0
- package/src/helpers/selectionToInsertionEnd.ts +3 -3
- package/src/helpers/splitExtensions.ts +3 -3
- package/src/index.ts +15 -24
- package/src/inputRules/index.ts +5 -0
- package/src/inputRules/markInputRule.ts +59 -40
- package/src/inputRules/nodeInputRule.ts +45 -12
- package/src/inputRules/textInputRule.ts +35 -0
- package/src/inputRules/textblockTypeInputRule.ts +37 -0
- package/src/inputRules/wrappingInputRule.ts +59 -0
- package/src/pasteRules/index.ts +3 -0
- package/src/pasteRules/markPasteRule.ts +61 -53
- package/src/pasteRules/nodePasteRule.ts +37 -0
- package/src/pasteRules/textPasteRule.ts +35 -0
- package/src/style.ts +16 -3
- package/src/types.ts +170 -97
- package/src/utilities/callOrReturn.ts +6 -3
- package/src/utilities/createStyleTag.ts +12 -1
- package/src/utilities/deleteProps.ts +2 -4
- package/src/utilities/elementFromString.ts +4 -5
- package/src/utilities/escapeForRegEx.ts +4 -0
- package/src/utilities/findDuplicates.ts +5 -0
- package/src/utilities/fromString.ts +2 -2
- package/src/utilities/index.ts +20 -0
- package/src/utilities/isEmptyObject.ts +2 -2
- package/src/utilities/isFunction.ts +3 -0
- package/src/utilities/isMacOS.ts +5 -0
- package/src/utilities/isNumber.ts +3 -0
- package/src/utilities/isPlainObject.ts +8 -5
- package/src/utilities/isRegExp.ts +3 -0
- package/src/utilities/isString.ts +3 -0
- package/src/utilities/isiOS.ts +12 -0
- package/src/utilities/mergeAttributes.ts +2 -3
- package/src/utilities/mergeDeep.ts +2 -3
- package/src/utilities/minMax.ts +1 -1
- package/src/utilities/objectIncludes.ts +17 -5
- package/src/utilities/removeDuplicates.ts +15 -0
- package/CHANGELOG.md +0 -365
- package/LICENSE.md +0 -21
- package/dist/packages/core/src/CommandManager.d.ts +0 -13
- package/dist/packages/core/src/Editor.d.ts +0 -142
- package/dist/packages/core/src/EventEmitter.d.ts +0 -7
- package/dist/packages/core/src/Extension.d.ts +0 -148
- package/dist/packages/core/src/ExtensionManager.d.ts +0 -24
- package/dist/packages/core/src/Mark.d.ts +0 -211
- package/dist/packages/core/src/Node.d.ts +0 -265
- package/dist/packages/core/src/NodeView.d.ts +0 -31
- package/dist/packages/core/src/commands/blur.d.ts +0 -12
- package/dist/packages/core/src/commands/clearContent.d.ts +0 -12
- package/dist/packages/core/src/commands/clearNodes.d.ts +0 -12
- package/dist/packages/core/src/commands/command.d.ts +0 -12
- package/dist/packages/core/src/commands/createParagraphNear.d.ts +0 -12
- package/dist/packages/core/src/commands/deleteRange.d.ts +0 -12
- package/dist/packages/core/src/commands/deleteSelection.d.ts +0 -12
- package/dist/packages/core/src/commands/enter.d.ts +0 -12
- package/dist/packages/core/src/commands/exitCode.d.ts +0 -12
- package/dist/packages/core/src/commands/extendMarkRange.d.ts +0 -13
- package/dist/packages/core/src/commands/first.d.ts +0 -12
- package/dist/packages/core/src/commands/focus.d.ts +0 -12
- package/dist/packages/core/src/commands/insertContent.d.ts +0 -12
- package/dist/packages/core/src/commands/insertHTML.d.ts +0 -12
- package/dist/packages/core/src/commands/insertNode.d.ts +0 -13
- package/dist/packages/core/src/commands/insertText.d.ts +0 -12
- package/dist/packages/core/src/commands/joinBackward.d.ts +0 -12
- package/dist/packages/core/src/commands/joinForward.d.ts +0 -12
- package/dist/packages/core/src/commands/keyboardShortcut.d.ts +0 -12
- package/dist/packages/core/src/commands/lift.d.ts +0 -13
- package/dist/packages/core/src/commands/liftEmptyBlock.d.ts +0 -12
- package/dist/packages/core/src/commands/liftListItem.d.ts +0 -13
- package/dist/packages/core/src/commands/newlineInCode.d.ts +0 -12
- package/dist/packages/core/src/commands/replace.d.ts +0 -13
- package/dist/packages/core/src/commands/replaceRange.d.ts +0 -13
- package/dist/packages/core/src/commands/resetAttributes.d.ts +0 -13
- package/dist/packages/core/src/commands/resetNodeAttributes.d.ts +0 -13
- package/dist/packages/core/src/commands/scrollIntoView.d.ts +0 -12
- package/dist/packages/core/src/commands/selectAll.d.ts +0 -12
- package/dist/packages/core/src/commands/selectNodeBackward.d.ts +0 -12
- package/dist/packages/core/src/commands/selectNodeForward.d.ts +0 -12
- package/dist/packages/core/src/commands/selectParentNode.d.ts +0 -12
- package/dist/packages/core/src/commands/setContent.d.ts +0 -12
- package/dist/packages/core/src/commands/setMark.d.ts +0 -13
- package/dist/packages/core/src/commands/setNode.d.ts +0 -13
- package/dist/packages/core/src/commands/sinkListItem.d.ts +0 -13
- package/dist/packages/core/src/commands/splitBlock.d.ts +0 -14
- package/dist/packages/core/src/commands/splitListItem.d.ts +0 -13
- package/dist/packages/core/src/commands/toggleList.d.ts +0 -13
- package/dist/packages/core/src/commands/toggleMark.d.ts +0 -13
- package/dist/packages/core/src/commands/toggleNode.d.ts +0 -13
- package/dist/packages/core/src/commands/toggleWrap.d.ts +0 -13
- package/dist/packages/core/src/commands/undoInputRule.d.ts +0 -12
- package/dist/packages/core/src/commands/unsetAllMarks.d.ts +0 -12
- package/dist/packages/core/src/commands/unsetMark.d.ts +0 -13
- package/dist/packages/core/src/commands/updateAttributes.d.ts +0 -13
- package/dist/packages/core/src/commands/updateNodeAttributes.d.ts +0 -13
- package/dist/packages/core/src/commands/wrapIn.d.ts +0 -13
- package/dist/packages/core/src/commands/wrapInList.d.ts +0 -13
- package/dist/packages/core/src/extensions/clipboardTextSerializer.d.ts +0 -2
- package/dist/packages/core/src/extensions/commands.d.ts +0 -100
- package/dist/packages/core/src/extensions/editable.d.ts +0 -2
- package/dist/packages/core/src/extensions/focusEvents.d.ts +0 -2
- package/dist/packages/core/src/extensions/index.d.ts +0 -5
- package/dist/packages/core/src/extensions/keymap.d.ts +0 -2
- package/dist/packages/core/src/helpers/createDocument.d.ts +0 -4
- package/dist/packages/core/src/helpers/createNodeFromContent.d.ts +0 -8
- package/dist/packages/core/src/helpers/findParentNode.d.ts +0 -9
- package/dist/packages/core/src/helpers/findParentNodeClosestToPos.d.ts +0 -8
- package/dist/packages/core/src/helpers/generateHTML.d.ts +0 -2
- package/dist/packages/core/src/helpers/getAttributesFromExtensions.d.ts +0 -6
- package/dist/packages/core/src/helpers/getHTMLFromFragment.d.ts +0 -2
- package/dist/packages/core/src/helpers/getMarkAttributes.d.ts +0 -4
- package/dist/packages/core/src/helpers/getMarkRange.d.ts +0 -3
- package/dist/packages/core/src/helpers/getMarkType.d.ts +0 -2
- package/dist/packages/core/src/helpers/getMarksBetween.d.ts +0 -3
- package/dist/packages/core/src/helpers/getNodeAttributes.d.ts +0 -4
- package/dist/packages/core/src/helpers/getNodeType.d.ts +0 -2
- package/dist/packages/core/src/helpers/getRenderedAttributes.d.ts +0 -3
- package/dist/packages/core/src/helpers/getSchema.d.ts +0 -3
- package/dist/packages/core/src/helpers/getSchemaTypeByName.d.ts +0 -2
- package/dist/packages/core/src/helpers/getSchemaTypeNameByName.d.ts +0 -2
- package/dist/packages/core/src/helpers/getSplittedAttributes.d.ts +0 -2
- package/dist/packages/core/src/helpers/injectExtensionAttributesToParseRule.d.ts +0 -9
- package/dist/packages/core/src/helpers/isActive.d.ts +0 -3
- package/dist/packages/core/src/helpers/isList.d.ts +0 -2
- package/dist/packages/core/src/helpers/isMarkActive.d.ts +0 -4
- package/dist/packages/core/src/helpers/isNodeActive.d.ts +0 -4
- package/dist/packages/core/src/helpers/isNodeEmpty.d.ts +0 -2
- package/dist/packages/core/src/helpers/isNodeSelection.d.ts +0 -2
- package/dist/packages/core/src/helpers/isTextSelection.d.ts +0 -2
- package/dist/packages/core/src/helpers/selectionToInsertionEnd.d.ts +0 -2
- package/dist/packages/core/src/helpers/splitExtensions.d.ts +0 -9
- package/dist/packages/core/src/index.d.ts +0 -30
- package/dist/packages/core/src/inputRules/markInputRule.d.ts +0 -3
- package/dist/packages/core/src/inputRules/nodeInputRule.d.ts +0 -3
- package/dist/packages/core/src/pasteRules/markPasteRule.d.ts +0 -3
- package/dist/packages/core/src/style.d.ts +0 -2
- package/dist/packages/core/src/types.d.ts +0 -154
- package/dist/packages/core/src/utilities/callOrReturn.d.ts +0 -8
- package/dist/packages/core/src/utilities/createStyleTag.d.ts +0 -1
- package/dist/packages/core/src/utilities/deleteProps.d.ts +0 -7
- package/dist/packages/core/src/utilities/elementFromString.d.ts +0 -1
- package/dist/packages/core/src/utilities/fromString.d.ts +0 -1
- package/dist/packages/core/src/utilities/isClass.d.ts +0 -1
- package/dist/packages/core/src/utilities/isEmptyObject.d.ts +0 -1
- package/dist/packages/core/src/utilities/isObject.d.ts +0 -1
- package/dist/packages/core/src/utilities/isPlainObject.d.ts +0 -1
- package/dist/packages/core/src/utilities/mergeAttributes.d.ts +0 -2
- package/dist/packages/core/src/utilities/mergeDeep.d.ts +0 -2
- package/dist/packages/core/src/utilities/minMax.d.ts +0 -1
- package/dist/packages/core/src/utilities/objectIncludes.d.ts +0 -7
- package/dist/packages/core/src/utilities/removeElement.d.ts +0 -1
- package/dist/tiptap-core.bundle.umd.min.js +0 -17
- package/dist/tiptap-core.bundle.umd.min.js.map +0 -1
- package/dist/tiptap-core.cjs.js +0 -3027
- package/dist/tiptap-core.cjs.js.map +0 -1
- package/dist/tiptap-core.esm.js +0 -3002
- package/dist/tiptap-core.esm.js.map +0 -1
- package/dist/tiptap-core.umd.js +0 -3024
- package/dist/tiptap-core.umd.js.map +0 -1
- package/src/commands/insertHTML.ts +0 -30
- package/src/commands/insertNode.ts +0 -33
- package/src/commands/insertText.ts +0 -22
- package/src/commands/joinBackward.ts +0 -17
- package/src/commands/joinForward.ts +0 -17
- package/src/commands/replace.ts +0 -20
- package/src/commands/replaceRange.ts +0 -36
- package/src/commands/resetNodeAttributes.ts +0 -33
- package/src/commands/updateNodeAttributes.ts +0 -35
- package/src/utilities/isClass.ts +0 -7
- package/src/utilities/isObject.ts +0 -10
- package/src/utilities/removeElement.ts +0 -5
package/src/Editor.ts
CHANGED
|
@@ -1,28 +1,33 @@
|
|
|
1
|
+
import { MarkType, NodeType, Schema } from '@tiptap/pm/model'
|
|
1
2
|
import {
|
|
2
3
|
EditorState, Plugin, PluginKey, Transaction,
|
|
3
|
-
} from '
|
|
4
|
-
import { EditorView } from '
|
|
5
|
-
|
|
6
|
-
import
|
|
7
|
-
import
|
|
8
|
-
import
|
|
9
|
-
import
|
|
10
|
-
import createDocument from './helpers/createDocument'
|
|
11
|
-
import
|
|
12
|
-
import
|
|
13
|
-
import
|
|
14
|
-
import
|
|
15
|
-
import
|
|
16
|
-
import
|
|
4
|
+
} from '@tiptap/pm/state'
|
|
5
|
+
import { EditorView } from '@tiptap/pm/view'
|
|
6
|
+
|
|
7
|
+
import { CommandManager } from './CommandManager'
|
|
8
|
+
import { EventEmitter } from './EventEmitter'
|
|
9
|
+
import { ExtensionManager } from './ExtensionManager'
|
|
10
|
+
import * as extensions from './extensions'
|
|
11
|
+
import { createDocument } from './helpers/createDocument'
|
|
12
|
+
import { getAttributes } from './helpers/getAttributes'
|
|
13
|
+
import { getHTMLFromFragment } from './helpers/getHTMLFromFragment'
|
|
14
|
+
import { getText } from './helpers/getText'
|
|
15
|
+
import { getTextSerializersFromSchema } from './helpers/getTextSerializersFromSchema'
|
|
16
|
+
import { isActive } from './helpers/isActive'
|
|
17
|
+
import { isNodeEmpty } from './helpers/isNodeEmpty'
|
|
18
|
+
import { resolveFocusPosition } from './helpers/resolveFocusPosition'
|
|
19
|
+
import { style } from './style'
|
|
17
20
|
import {
|
|
18
|
-
EditorOptions,
|
|
19
21
|
CanCommands,
|
|
20
22
|
ChainedCommands,
|
|
23
|
+
EditorEvents,
|
|
24
|
+
EditorOptions,
|
|
25
|
+
JSONContent,
|
|
21
26
|
SingleCommands,
|
|
22
|
-
|
|
27
|
+
TextSerializer,
|
|
23
28
|
} from './types'
|
|
24
|
-
import
|
|
25
|
-
import
|
|
29
|
+
import { createStyleTag } from './utilities/createStyleTag'
|
|
30
|
+
import { isFunction } from './utilities/isFunction'
|
|
26
31
|
|
|
27
32
|
export { extensions }
|
|
28
33
|
|
|
@@ -30,8 +35,7 @@ export interface HTMLElement {
|
|
|
30
35
|
editor?: Editor
|
|
31
36
|
}
|
|
32
37
|
|
|
33
|
-
export class Editor extends EventEmitter {
|
|
34
|
-
|
|
38
|
+
export class Editor extends EventEmitter<EditorEvents> {
|
|
35
39
|
private commandManager!: CommandManager
|
|
36
40
|
|
|
37
41
|
public extensionManager!: ExtensionManager
|
|
@@ -44,12 +48,13 @@ export class Editor extends EventEmitter {
|
|
|
44
48
|
|
|
45
49
|
public isFocused = false
|
|
46
50
|
|
|
47
|
-
|
|
51
|
+
public extensionStorage: Record<string, any> = {}
|
|
48
52
|
|
|
49
53
|
public options: EditorOptions = {
|
|
50
54
|
element: document.createElement('div'),
|
|
51
55
|
content: '',
|
|
52
56
|
injectCSS: true,
|
|
57
|
+
injectNonce: undefined,
|
|
53
58
|
extensions: [],
|
|
54
59
|
autofocus: false,
|
|
55
60
|
editable: true,
|
|
@@ -57,13 +62,14 @@ export class Editor extends EventEmitter {
|
|
|
57
62
|
parseOptions: {},
|
|
58
63
|
enableInputRules: true,
|
|
59
64
|
enablePasteRules: true,
|
|
65
|
+
enableCoreExtensions: true,
|
|
66
|
+
onBeforeCreate: () => null,
|
|
60
67
|
onCreate: () => null,
|
|
61
68
|
onUpdate: () => null,
|
|
62
69
|
onSelectionUpdate: () => null,
|
|
63
70
|
onTransaction: () => null,
|
|
64
71
|
onFocus: () => null,
|
|
65
72
|
onBlur: () => null,
|
|
66
|
-
onResize: () => null,
|
|
67
73
|
onDestroy: () => null,
|
|
68
74
|
}
|
|
69
75
|
|
|
@@ -73,6 +79,8 @@ export class Editor extends EventEmitter {
|
|
|
73
79
|
this.createExtensionManager()
|
|
74
80
|
this.createCommandManager()
|
|
75
81
|
this.createSchema()
|
|
82
|
+
this.on('beforeCreate', this.options.onBeforeCreate)
|
|
83
|
+
this.emit('beforeCreate', { editor: this })
|
|
76
84
|
this.createView()
|
|
77
85
|
this.injectCSS()
|
|
78
86
|
this.on('create', this.options.onCreate)
|
|
@@ -84,38 +92,41 @@ export class Editor extends EventEmitter {
|
|
|
84
92
|
this.on('destroy', this.options.onDestroy)
|
|
85
93
|
|
|
86
94
|
window.setTimeout(() => {
|
|
95
|
+
if (this.isDestroyed) {
|
|
96
|
+
return
|
|
97
|
+
}
|
|
98
|
+
|
|
87
99
|
this.commands.focus(this.options.autofocus)
|
|
88
100
|
this.emit('create', { editor: this })
|
|
89
|
-
|
|
90
|
-
if (window.ResizeObserver) {
|
|
91
|
-
this.resizeObserver = new ResizeObserver(() => {
|
|
92
|
-
this.emit('resize', { editor: this })
|
|
93
|
-
})
|
|
94
|
-
this.resizeObserver.observe(this.view.dom)
|
|
95
|
-
}
|
|
96
101
|
}, 0)
|
|
102
|
+
}
|
|
97
103
|
|
|
104
|
+
/**
|
|
105
|
+
* Returns the editor storage.
|
|
106
|
+
*/
|
|
107
|
+
public get storage(): Record<string, any> {
|
|
108
|
+
return this.extensionStorage
|
|
98
109
|
}
|
|
99
110
|
|
|
100
111
|
/**
|
|
101
112
|
* An object of all registered commands.
|
|
102
113
|
*/
|
|
103
114
|
public get commands(): SingleCommands {
|
|
104
|
-
return this.commandManager.
|
|
115
|
+
return this.commandManager.commands
|
|
105
116
|
}
|
|
106
117
|
|
|
107
118
|
/**
|
|
108
119
|
* Create a command chain to call multiple commands at once.
|
|
109
120
|
*/
|
|
110
121
|
public chain(): ChainedCommands {
|
|
111
|
-
return this.commandManager.
|
|
122
|
+
return this.commandManager.chain()
|
|
112
123
|
}
|
|
113
124
|
|
|
114
125
|
/**
|
|
115
126
|
* Check if a command or a command chain can be executed. Without executing it.
|
|
116
127
|
*/
|
|
117
128
|
public can(): CanCommands {
|
|
118
|
-
return this.commandManager.
|
|
129
|
+
return this.commandManager.can()
|
|
119
130
|
}
|
|
120
131
|
|
|
121
132
|
/**
|
|
@@ -123,7 +134,7 @@ export class Editor extends EventEmitter {
|
|
|
123
134
|
*/
|
|
124
135
|
private injectCSS(): void {
|
|
125
136
|
if (this.options.injectCSS && document) {
|
|
126
|
-
this.css = createStyleTag(style)
|
|
137
|
+
this.css = createStyleTag(style, this.options.injectNonce)
|
|
127
138
|
}
|
|
128
139
|
}
|
|
129
140
|
|
|
@@ -133,17 +144,30 @@ export class Editor extends EventEmitter {
|
|
|
133
144
|
* @param options A list of options
|
|
134
145
|
*/
|
|
135
146
|
public setOptions(options: Partial<EditorOptions> = {}): void {
|
|
136
|
-
this.options = {
|
|
147
|
+
this.options = {
|
|
148
|
+
...this.options,
|
|
149
|
+
...options,
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
if (!this.view || !this.state || this.isDestroyed) {
|
|
153
|
+
return
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
if (this.options.editorProps) {
|
|
157
|
+
this.view.setProps(this.options.editorProps)
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
this.view.updateState(this.state)
|
|
137
161
|
}
|
|
138
162
|
|
|
139
163
|
/**
|
|
140
164
|
* Update editable state of the editor.
|
|
141
165
|
*/
|
|
142
|
-
public setEditable(editable: boolean): void {
|
|
166
|
+
public setEditable(editable: boolean, emitUpdate = true): void {
|
|
143
167
|
this.setOptions({ editable })
|
|
144
168
|
|
|
145
|
-
if (
|
|
146
|
-
this.
|
|
169
|
+
if (emitUpdate) {
|
|
170
|
+
this.emit('update', { editor: this, transaction: this.state.tr })
|
|
147
171
|
}
|
|
148
172
|
}
|
|
149
173
|
|
|
@@ -151,7 +175,10 @@ export class Editor extends EventEmitter {
|
|
|
151
175
|
* Returns whether the editor is editable.
|
|
152
176
|
*/
|
|
153
177
|
public get isEditable(): boolean {
|
|
154
|
-
|
|
178
|
+
// since plugins are applied after creating the view
|
|
179
|
+
// `editable` is always `true` for one tick.
|
|
180
|
+
// that’s why we also have to check for `options.editable`
|
|
181
|
+
return this.options.editable && this.view && this.view.editable
|
|
155
182
|
}
|
|
156
183
|
|
|
157
184
|
/**
|
|
@@ -167,9 +194,12 @@ export class Editor extends EventEmitter {
|
|
|
167
194
|
* @param plugin A ProseMirror plugin
|
|
168
195
|
* @param handlePlugins Control how to merge the plugin into the existing plugins.
|
|
169
196
|
*/
|
|
170
|
-
public registerPlugin(
|
|
171
|
-
|
|
172
|
-
|
|
197
|
+
public registerPlugin(
|
|
198
|
+
plugin: Plugin,
|
|
199
|
+
handlePlugins?: (newPlugin: Plugin, plugins: Plugin[]) => Plugin[],
|
|
200
|
+
): void {
|
|
201
|
+
const plugins = isFunction(handlePlugins)
|
|
202
|
+
? handlePlugins(plugin, [...this.state.plugins])
|
|
173
203
|
: [...this.state.plugins, plugin]
|
|
174
204
|
|
|
175
205
|
const state = this.state.reconfigure({ plugins })
|
|
@@ -180,17 +210,15 @@ export class Editor extends EventEmitter {
|
|
|
180
210
|
/**
|
|
181
211
|
* Unregister a ProseMirror plugin.
|
|
182
212
|
*
|
|
183
|
-
* @param
|
|
213
|
+
* @param nameOrPluginKey The plugins name
|
|
184
214
|
*/
|
|
185
215
|
public unregisterPlugin(nameOrPluginKey: string | PluginKey): void {
|
|
186
216
|
if (this.isDestroyed) {
|
|
187
217
|
return
|
|
188
218
|
}
|
|
189
219
|
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
// @ts-ignore
|
|
193
|
-
: nameOrPluginKey.key
|
|
220
|
+
// @ts-ignore
|
|
221
|
+
const name = typeof nameOrPluginKey === 'string' ? `${nameOrPluginKey}$` : nameOrPluginKey.key
|
|
194
222
|
|
|
195
223
|
const state = this.state.reconfigure({
|
|
196
224
|
// @ts-ignore
|
|
@@ -204,7 +232,7 @@ export class Editor extends EventEmitter {
|
|
|
204
232
|
* Creates an extension manager.
|
|
205
233
|
*/
|
|
206
234
|
private createExtensionManager(): void {
|
|
207
|
-
const coreExtensions = Object.
|
|
235
|
+
const coreExtensions = this.options.enableCoreExtensions ? Object.values(extensions) : []
|
|
208
236
|
const allExtensions = [...coreExtensions, ...this.options.extensions].filter(extension => {
|
|
209
237
|
return ['extension', 'node', 'mark'].includes(extension?.type)
|
|
210
238
|
})
|
|
@@ -216,7 +244,9 @@ export class Editor extends EventEmitter {
|
|
|
216
244
|
* Creates an command manager.
|
|
217
245
|
*/
|
|
218
246
|
private createCommandManager(): void {
|
|
219
|
-
this.commandManager = new CommandManager(
|
|
247
|
+
this.commandManager = new CommandManager({
|
|
248
|
+
editor: this,
|
|
249
|
+
})
|
|
220
250
|
}
|
|
221
251
|
|
|
222
252
|
/**
|
|
@@ -230,11 +260,15 @@ export class Editor extends EventEmitter {
|
|
|
230
260
|
* Creates a ProseMirror view.
|
|
231
261
|
*/
|
|
232
262
|
private createView(): void {
|
|
263
|
+
const doc = createDocument(this.options.content, this.schema, this.options.parseOptions)
|
|
264
|
+
const selection = resolveFocusPosition(doc, this.options.autofocus)
|
|
265
|
+
|
|
233
266
|
this.view = new EditorView(this.options.element, {
|
|
234
267
|
...this.options.editorProps,
|
|
235
268
|
dispatchTransaction: this.dispatchTransaction.bind(this),
|
|
236
269
|
state: EditorState.create({
|
|
237
|
-
doc
|
|
270
|
+
doc,
|
|
271
|
+
selection: selection || undefined,
|
|
238
272
|
}),
|
|
239
273
|
})
|
|
240
274
|
|
|
@@ -251,6 +285,7 @@ export class Editor extends EventEmitter {
|
|
|
251
285
|
// Let’s store the editor instance in the DOM element.
|
|
252
286
|
// So we’ll have access to it for tests.
|
|
253
287
|
const dom = this.view.dom as HTMLElement
|
|
288
|
+
|
|
254
289
|
dom.editor = this
|
|
255
290
|
}
|
|
256
291
|
|
|
@@ -285,10 +320,6 @@ export class Editor extends EventEmitter {
|
|
|
285
320
|
* @param transaction An editor state transaction
|
|
286
321
|
*/
|
|
287
322
|
private dispatchTransaction(transaction: Transaction): void {
|
|
288
|
-
if (transaction.docChanged && !this.isEditable) {
|
|
289
|
-
return
|
|
290
|
-
}
|
|
291
|
-
|
|
292
323
|
if (this.isCapturingTransaction) {
|
|
293
324
|
if (!this.capturedTransaction) {
|
|
294
325
|
this.capturedTransaction = transaction
|
|
@@ -313,6 +344,7 @@ export class Editor extends EventEmitter {
|
|
|
313
344
|
if (selectionHasChanged) {
|
|
314
345
|
this.emit('selectionUpdate', {
|
|
315
346
|
editor: this,
|
|
347
|
+
transaction,
|
|
316
348
|
})
|
|
317
349
|
}
|
|
318
350
|
|
|
@@ -323,6 +355,7 @@ export class Editor extends EventEmitter {
|
|
|
323
355
|
this.emit('focus', {
|
|
324
356
|
editor: this,
|
|
325
357
|
event: focus.event,
|
|
358
|
+
transaction,
|
|
326
359
|
})
|
|
327
360
|
}
|
|
328
361
|
|
|
@@ -330,6 +363,7 @@ export class Editor extends EventEmitter {
|
|
|
330
363
|
this.emit('blur', {
|
|
331
364
|
editor: this,
|
|
332
365
|
event: blur.event,
|
|
366
|
+
transaction,
|
|
333
367
|
})
|
|
334
368
|
}
|
|
335
369
|
|
|
@@ -344,21 +378,10 @@ export class Editor extends EventEmitter {
|
|
|
344
378
|
}
|
|
345
379
|
|
|
346
380
|
/**
|
|
347
|
-
* Get attributes of the currently selected node.
|
|
348
|
-
*
|
|
349
|
-
* @param name Name of the node
|
|
350
|
-
*/
|
|
351
|
-
public getNodeAttributes(name: string): AnyObject {
|
|
352
|
-
return getNodeAttributes(this.state, name)
|
|
353
|
-
}
|
|
354
|
-
|
|
355
|
-
/**
|
|
356
|
-
* Get attributes of the currently selected mark.
|
|
357
|
-
*
|
|
358
|
-
* @param name Name of the mark
|
|
381
|
+
* Get attributes of the currently selected node or mark.
|
|
359
382
|
*/
|
|
360
|
-
public
|
|
361
|
-
return
|
|
383
|
+
public getAttributes(nameOrType: string | NodeType | MarkType): Record<string, any> {
|
|
384
|
+
return getAttributes(this.state, nameOrType)
|
|
362
385
|
}
|
|
363
386
|
|
|
364
387
|
/**
|
|
@@ -367,16 +390,12 @@ export class Editor extends EventEmitter {
|
|
|
367
390
|
* @param name Name of the node or mark
|
|
368
391
|
* @param attributes Attributes of the node or mark
|
|
369
392
|
*/
|
|
370
|
-
public isActive(name: string, attributes?: {}): boolean
|
|
371
|
-
public isActive(attributes: {}): boolean
|
|
393
|
+
public isActive(name: string, attributes?: {}): boolean
|
|
394
|
+
public isActive(attributes: {}): boolean
|
|
372
395
|
public isActive(nameOrAttributes: string, attributesOrUndefined?: {}): boolean {
|
|
373
|
-
const name = typeof nameOrAttributes === 'string'
|
|
374
|
-
? nameOrAttributes
|
|
375
|
-
: null
|
|
396
|
+
const name = typeof nameOrAttributes === 'string' ? nameOrAttributes : null
|
|
376
397
|
|
|
377
|
-
const attributes = typeof nameOrAttributes === 'string'
|
|
378
|
-
? attributesOrUndefined
|
|
379
|
-
: nameOrAttributes
|
|
398
|
+
const attributes = typeof nameOrAttributes === 'string' ? attributesOrUndefined : nameOrAttributes
|
|
380
399
|
|
|
381
400
|
return isActive(this.state, name, attributes)
|
|
382
401
|
}
|
|
@@ -384,7 +403,7 @@ export class Editor extends EventEmitter {
|
|
|
384
403
|
/**
|
|
385
404
|
* Get the document as JSON.
|
|
386
405
|
*/
|
|
387
|
-
public getJSON():
|
|
406
|
+
public getJSON(): JSONContent {
|
|
388
407
|
return this.state.doc.toJSON()
|
|
389
408
|
}
|
|
390
409
|
|
|
@@ -392,7 +411,25 @@ export class Editor extends EventEmitter {
|
|
|
392
411
|
* Get the document as HTML.
|
|
393
412
|
*/
|
|
394
413
|
public getHTML(): string {
|
|
395
|
-
return getHTMLFromFragment(this.state.doc, this.schema)
|
|
414
|
+
return getHTMLFromFragment(this.state.doc.content, this.schema)
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
/**
|
|
418
|
+
* Get the document as text.
|
|
419
|
+
*/
|
|
420
|
+
public getText(options?: {
|
|
421
|
+
blockSeparator?: string
|
|
422
|
+
textSerializers?: Record<string, TextSerializer>
|
|
423
|
+
}): string {
|
|
424
|
+
const { blockSeparator = '\n\n', textSerializers = {} } = options || {}
|
|
425
|
+
|
|
426
|
+
return getText(this.state.doc, {
|
|
427
|
+
blockSeparator,
|
|
428
|
+
textSerializers: {
|
|
429
|
+
...textSerializers,
|
|
430
|
+
...getTextSerializersFromSchema(this.schema),
|
|
431
|
+
},
|
|
432
|
+
})
|
|
396
433
|
}
|
|
397
434
|
|
|
398
435
|
/**
|
|
@@ -404,8 +441,14 @@ export class Editor extends EventEmitter {
|
|
|
404
441
|
|
|
405
442
|
/**
|
|
406
443
|
* Get the number of characters for the current document.
|
|
444
|
+
*
|
|
445
|
+
* @deprecated
|
|
407
446
|
*/
|
|
408
447
|
public getCharacterCount(): number {
|
|
448
|
+
console.warn(
|
|
449
|
+
'[tiptap warn]: "editor.getCharacterCount()" is deprecated. Please use "editor.storage.characterCount.characters()" instead.',
|
|
450
|
+
)
|
|
451
|
+
|
|
409
452
|
return this.state.doc.content.size - 2
|
|
410
453
|
}
|
|
411
454
|
|
|
@@ -413,8 +456,6 @@ export class Editor extends EventEmitter {
|
|
|
413
456
|
* Destroy the editor.
|
|
414
457
|
*/
|
|
415
458
|
public destroy(): void {
|
|
416
|
-
this.resizeObserver?.unobserve(this.view.dom)
|
|
417
|
-
|
|
418
459
|
this.emit('destroy')
|
|
419
460
|
|
|
420
461
|
if (this.view) {
|
|
@@ -422,7 +463,6 @@ export class Editor extends EventEmitter {
|
|
|
422
463
|
}
|
|
423
464
|
|
|
424
465
|
this.removeAllListeners()
|
|
425
|
-
removeElement(this.css)
|
|
426
466
|
}
|
|
427
467
|
|
|
428
468
|
/**
|
|
@@ -432,5 +472,4 @@ export class Editor extends EventEmitter {
|
|
|
432
472
|
// @ts-ignore
|
|
433
473
|
return !this.view?.docView
|
|
434
474
|
}
|
|
435
|
-
|
|
436
475
|
}
|
package/src/EventEmitter.ts
CHANGED
|
@@ -1,8 +1,18 @@
|
|
|
1
|
-
|
|
1
|
+
type StringKeyOf<T> = Extract<keyof T, string>
|
|
2
|
+
type CallbackType<
|
|
3
|
+
T extends Record<string, any>,
|
|
4
|
+
EventName extends StringKeyOf<T>,
|
|
5
|
+
> = T[EventName] extends any[] ? T[EventName] : [T[EventName]]
|
|
6
|
+
type CallbackFunction<
|
|
7
|
+
T extends Record<string, any>,
|
|
8
|
+
EventName extends StringKeyOf<T>,
|
|
9
|
+
> = (...props: CallbackType<T, EventName>) => any
|
|
10
|
+
|
|
11
|
+
export class EventEmitter<T extends Record<string, any>> {
|
|
2
12
|
|
|
3
13
|
private callbacks: { [key: string]: Function[] } = {}
|
|
4
14
|
|
|
5
|
-
public on(event:
|
|
15
|
+
public on<EventName extends StringKeyOf<T>>(event: EventName, fn: CallbackFunction<T, EventName>): this {
|
|
6
16
|
if (!this.callbacks[event]) {
|
|
7
17
|
this.callbacks[event] = []
|
|
8
18
|
}
|
|
@@ -12,7 +22,7 @@ export default class EventEmitter {
|
|
|
12
22
|
return this
|
|
13
23
|
}
|
|
14
24
|
|
|
15
|
-
protected emit(event:
|
|
25
|
+
protected emit<EventName extends StringKeyOf<T>>(event: EventName, ...args: CallbackType<T, EventName>): this {
|
|
16
26
|
const callbacks = this.callbacks[event]
|
|
17
27
|
|
|
18
28
|
if (callbacks) {
|
|
@@ -22,7 +32,7 @@ export default class EventEmitter {
|
|
|
22
32
|
return this
|
|
23
33
|
}
|
|
24
34
|
|
|
25
|
-
public off(event:
|
|
35
|
+
public off<EventName extends StringKeyOf<T>>(event: EventName, fn?: CallbackFunction<T, EventName>): this {
|
|
26
36
|
const callbacks = this.callbacks[event]
|
|
27
37
|
|
|
28
38
|
if (callbacks) {
|