@tiptap/core 3.0.0 → 3.0.1
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/LICENSE.md +21 -0
- package/README.md +5 -1
- package/dist/index.cjs +5169 -4654
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +3356 -0
- package/dist/index.d.ts +3356 -0
- package/dist/index.js +5051 -4561
- package/dist/index.js.map +1 -1
- package/dist/jsx-runtime/jsx-runtime.cjs +56 -0
- package/dist/jsx-runtime/jsx-runtime.cjs.map +1 -0
- package/dist/jsx-runtime/jsx-runtime.d.cts +22 -0
- package/dist/jsx-runtime/jsx-runtime.d.ts +22 -0
- package/dist/jsx-runtime/jsx-runtime.js +26 -0
- package/dist/jsx-runtime/jsx-runtime.js.map +1 -0
- package/jsx-dev-runtime/index.cjs +1 -0
- package/jsx-dev-runtime/index.d.cts +1 -0
- package/jsx-dev-runtime/index.d.ts +1 -0
- package/jsx-dev-runtime/index.js +1 -0
- package/jsx-runtime/index.cjs +1 -0
- package/jsx-runtime/index.d.cts +1 -0
- package/jsx-runtime/index.d.ts +1 -0
- package/jsx-runtime/index.js +1 -0
- package/package.json +30 -10
- package/src/CommandManager.ts +4 -11
- package/src/Editor.ts +273 -100
- package/src/EventEmitter.ts +16 -10
- package/src/Extendable.ts +484 -0
- package/src/Extension.ts +29 -493
- package/src/ExtensionManager.ts +100 -143
- package/src/InputRule.ts +28 -24
- package/src/Mark.ts +157 -624
- package/src/MarkView.ts +122 -0
- package/src/Node.ts +343 -807
- package/src/NodePos.ts +18 -9
- package/src/NodeView.ts +58 -35
- package/src/PasteRule.ts +68 -24
- package/src/Tracker.ts +8 -10
- package/src/commands/blur.ts +15 -13
- package/src/commands/clearContent.ts +13 -6
- package/src/commands/clearNodes.ts +33 -31
- package/src/commands/command.ts +2 -2
- package/src/commands/createParagraphNear.ts +6 -4
- package/src/commands/cut.ts +13 -11
- package/src/commands/deleteCurrentNode.ts +24 -22
- package/src/commands/deleteNode.ts +20 -18
- package/src/commands/deleteRange.ts +11 -9
- package/src/commands/deleteSelection.ts +6 -4
- package/src/commands/enter.ts +7 -5
- package/src/commands/exitCode.ts +6 -4
- package/src/commands/extendMarkRange.ts +16 -14
- package/src/commands/first.ts +3 -5
- package/src/commands/focus.ts +51 -53
- package/src/commands/forEach.ts +3 -3
- package/src/commands/insertContent.ts +8 -10
- package/src/commands/insertContentAt.ts +131 -76
- package/src/commands/join.ts +21 -13
- package/src/commands/joinItemBackward.ts +17 -19
- package/src/commands/joinItemForward.ts +17 -19
- package/src/commands/joinTextblockBackward.ts +6 -4
- package/src/commands/joinTextblockForward.ts +6 -4
- package/src/commands/keyboardShortcut.ts +30 -35
- package/src/commands/lift.ts +12 -10
- package/src/commands/liftEmptyBlock.ts +7 -5
- package/src/commands/liftListItem.ts +8 -6
- package/src/commands/newlineInCode.ts +6 -4
- package/src/commands/resetAttributes.ts +38 -43
- package/src/commands/scrollIntoView.ts +10 -8
- package/src/commands/selectAll.ts +15 -8
- package/src/commands/selectNodeBackward.ts +6 -4
- package/src/commands/selectNodeForward.ts +6 -4
- package/src/commands/selectParentNode.ts +6 -4
- package/src/commands/selectTextblockEnd.ts +6 -4
- package/src/commands/selectTextblockStart.ts +6 -4
- package/src/commands/setContent.ts +37 -40
- package/src/commands/setMark.ts +58 -60
- package/src/commands/setMeta.ts +10 -6
- package/src/commands/setNode.ts +36 -27
- package/src/commands/setNodeSelection.ts +12 -10
- package/src/commands/setTextSelection.ts +16 -14
- package/src/commands/sinkListItem.ts +8 -6
- package/src/commands/splitBlock.ts +60 -68
- package/src/commands/splitListItem.ts +99 -101
- package/src/commands/toggleList.ts +76 -74
- package/src/commands/toggleMark.ts +13 -11
- package/src/commands/toggleNode.ts +22 -11
- package/src/commands/toggleWrap.ts +12 -10
- package/src/commands/undoInputRule.ts +32 -30
- package/src/commands/unsetAllMarks.ts +17 -15
- package/src/commands/unsetMark.ts +29 -27
- package/src/commands/updateAttributes.ts +97 -45
- package/src/commands/wrapIn.ts +8 -6
- package/src/commands/wrapInList.ts +8 -6
- package/src/extensions/clipboardTextSerializer.ts +2 -4
- package/src/extensions/delete.ts +89 -0
- package/src/extensions/drop.ts +26 -0
- package/src/extensions/focusEvents.ts +5 -7
- package/src/extensions/index.ts +4 -1
- package/src/extensions/keymap.ts +63 -52
- package/src/extensions/paste.ts +25 -0
- package/src/extensions/tabindex.ts +1 -1
- package/src/helpers/combineTransactionSteps.ts +3 -6
- package/src/helpers/createChainableState.ts +2 -5
- package/src/helpers/createDocument.ts +3 -3
- package/src/helpers/createNodeFromContent.ts +37 -25
- package/src/helpers/defaultBlockAt.ts +1 -1
- package/src/helpers/findChildren.ts +2 -2
- package/src/helpers/findChildrenInRange.ts +3 -7
- package/src/helpers/findParentNode.ts +5 -3
- package/src/helpers/findParentNodeClosestToPos.ts +2 -2
- package/src/helpers/flattenExtensions.ts +30 -0
- package/src/helpers/generateHTML.ts +1 -1
- package/src/helpers/generateJSON.ts +1 -1
- package/src/helpers/generateText.ts +1 -1
- package/src/helpers/getAttributes.ts +3 -6
- package/src/helpers/getAttributesFromExtensions.ts +29 -38
- package/src/helpers/getChangedRanges.ts +15 -13
- package/src/helpers/getDebugJSON.ts +2 -2
- package/src/helpers/getExtensionField.ts +12 -12
- package/src/helpers/getHTMLFromFragment.ts +2 -1
- package/src/helpers/getMarkAttributes.ts +3 -6
- package/src/helpers/getMarkRange.ts +36 -19
- package/src/helpers/getMarkType.ts +2 -4
- package/src/helpers/getMarksBetween.ts +3 -3
- package/src/helpers/getNodeAtPosition.ts +2 -2
- package/src/helpers/getNodeAttributes.ts +3 -6
- package/src/helpers/getNodeType.ts +2 -4
- package/src/helpers/getRenderedAttributes.ts +3 -2
- package/src/helpers/getSchema.ts +5 -5
- package/src/helpers/getSchemaByResolvedExtensions.ts +49 -79
- package/src/helpers/getSchemaTypeByName.ts +1 -1
- package/src/helpers/getSchemaTypeNameByName.ts +1 -1
- package/src/helpers/getSplittedAttributes.ts +5 -5
- package/src/helpers/getText.ts +2 -2
- package/src/helpers/getTextBetween.ts +2 -2
- package/src/helpers/getTextContentFromNodes.ts +9 -12
- package/src/helpers/getTextSerializersFromSchema.ts +2 -2
- package/src/helpers/index.ts +4 -0
- package/src/helpers/injectExtensionAttributesToParseRule.ts +3 -3
- package/src/helpers/isActive.ts +2 -6
- package/src/helpers/isAtEndOfNode.ts +1 -1
- package/src/helpers/isAtStartOfNode.ts +1 -1
- package/src/helpers/isExtensionRulesEnabled.ts +2 -4
- package/src/helpers/isList.ts +2 -2
- package/src/helpers/isMarkActive.ts +3 -3
- package/src/helpers/isNodeActive.ts +3 -3
- package/src/helpers/isNodeEmpty.ts +56 -5
- package/src/helpers/posToDOMRect.ts +1 -1
- package/src/helpers/resolveExtensions.ts +25 -0
- package/src/helpers/resolveFocusPosition.ts +5 -16
- package/src/helpers/rewriteUnknownContent.ts +149 -0
- package/src/helpers/selectionToInsertionEnd.ts +2 -1
- package/src/helpers/sortExtensions.ts +26 -0
- package/src/helpers/splitExtensions.ts +4 -4
- package/src/index.ts +3 -7
- package/src/inputRules/markInputRule.ts +6 -9
- package/src/inputRules/nodeInputRule.ts +7 -13
- package/src/inputRules/textInputRule.ts +4 -6
- package/src/inputRules/textblockTypeInputRule.ts +7 -12
- package/src/inputRules/wrappingInputRule.ts +19 -24
- package/src/jsx-runtime.ts +64 -0
- package/src/pasteRules/markPasteRule.ts +6 -7
- package/src/pasteRules/nodePasteRule.ts +15 -11
- package/src/pasteRules/textPasteRule.ts +4 -6
- package/src/style.ts +2 -6
- package/src/types.ts +513 -58
- package/src/utilities/callOrReturn.ts +1 -1
- package/src/utilities/canInsertNode.ts +30 -0
- package/src/utilities/createStyleTag.ts +3 -1
- package/src/utilities/deleteProps.ts +7 -11
- package/src/utilities/elementFromString.ts +3 -0
- package/src/utilities/findDuplicates.ts +5 -2
- package/src/utilities/index.ts +2 -0
- package/src/utilities/isFunction.ts +1 -0
- package/src/utilities/isMacOS.ts +1 -3
- package/src/utilities/isiOS.ts +5 -10
- package/src/utilities/mergeAttributes.ts +32 -5
- package/src/utilities/removeDuplicates.ts +1 -3
- package/dist/index.umd.js +0 -5098
- package/dist/index.umd.js.map +0 -1
- package/dist/packages/core/src/CommandManager.d.ts +0 -20
- package/dist/packages/core/src/Editor.d.ts +0 -161
- package/dist/packages/core/src/EventEmitter.d.ts +0 -11
- package/dist/packages/core/src/Extension.d.ts +0 -343
- package/dist/packages/core/src/ExtensionManager.d.ts +0 -55
- package/dist/packages/core/src/InputRule.d.ts +0 -42
- package/dist/packages/core/src/Mark.d.ts +0 -451
- package/dist/packages/core/src/Node.d.ts +0 -611
- package/dist/packages/core/src/NodePos.d.ts +0 -44
- package/dist/packages/core/src/NodeView.d.ts +0 -31
- package/dist/packages/core/src/PasteRule.d.ts +0 -50
- package/dist/packages/core/src/Tracker.d.ts +0 -11
- package/dist/packages/core/src/commands/blur.d.ts +0 -13
- package/dist/packages/core/src/commands/clearContent.d.ts +0 -14
- package/dist/packages/core/src/commands/clearNodes.d.ts +0 -13
- package/dist/packages/core/src/commands/command.d.ts +0 -18
- package/dist/packages/core/src/commands/createParagraphNear.d.ts +0 -13
- package/dist/packages/core/src/commands/cut.d.ts +0 -20
- package/dist/packages/core/src/commands/deleteCurrentNode.d.ts +0 -13
- package/dist/packages/core/src/commands/deleteNode.d.ts +0 -15
- package/dist/packages/core/src/commands/deleteRange.d.ts +0 -14
- package/dist/packages/core/src/commands/deleteSelection.d.ts +0 -13
- package/dist/packages/core/src/commands/enter.d.ts +0 -13
- package/dist/packages/core/src/commands/exitCode.d.ts +0 -13
- package/dist/packages/core/src/commands/extendMarkRange.d.ts +0 -25
- package/dist/packages/core/src/commands/first.d.ts +0 -14
- package/dist/packages/core/src/commands/focus.d.ts +0 -27
- package/dist/packages/core/src/commands/forEach.d.ts +0 -14
- package/dist/packages/core/src/commands/index.d.ts +0 -55
- package/dist/packages/core/src/commands/insertContent.d.ts +0 -34
- package/dist/packages/core/src/commands/insertContentAt.d.ts +0 -47
- package/dist/packages/core/src/commands/join.d.ts +0 -41
- package/dist/packages/core/src/commands/joinItemBackward.d.ts +0 -13
- package/dist/packages/core/src/commands/joinItemForward.d.ts +0 -13
- package/dist/packages/core/src/commands/joinTextblockBackward.d.ts +0 -12
- package/dist/packages/core/src/commands/joinTextblockForward.d.ts +0 -12
- package/dist/packages/core/src/commands/keyboardShortcut.d.ts +0 -14
- package/dist/packages/core/src/commands/lift.d.ts +0 -17
- package/dist/packages/core/src/commands/liftEmptyBlock.d.ts +0 -13
- package/dist/packages/core/src/commands/liftListItem.d.ts +0 -15
- package/dist/packages/core/src/commands/newlineInCode.d.ts +0 -13
- package/dist/packages/core/src/commands/resetAttributes.d.ts +0 -16
- package/dist/packages/core/src/commands/scrollIntoView.d.ts +0 -13
- package/dist/packages/core/src/commands/selectAll.d.ts +0 -13
- package/dist/packages/core/src/commands/selectNodeBackward.d.ts +0 -13
- package/dist/packages/core/src/commands/selectNodeForward.d.ts +0 -13
- package/dist/packages/core/src/commands/selectParentNode.d.ts +0 -13
- package/dist/packages/core/src/commands/selectTextblockEnd.d.ts +0 -13
- package/dist/packages/core/src/commands/selectTextblockStart.d.ts +0 -13
- package/dist/packages/core/src/commands/setContent.d.ts +0 -40
- package/dist/packages/core/src/commands/setMark.d.ts +0 -15
- package/dist/packages/core/src/commands/setMeta.d.ts +0 -15
- package/dist/packages/core/src/commands/setNode.d.ts +0 -16
- package/dist/packages/core/src/commands/setNodeSelection.d.ts +0 -14
- package/dist/packages/core/src/commands/setTextSelection.d.ts +0 -14
- package/dist/packages/core/src/commands/sinkListItem.d.ts +0 -15
- package/dist/packages/core/src/commands/splitBlock.d.ts +0 -17
- package/dist/packages/core/src/commands/splitListItem.d.ts +0 -15
- package/dist/packages/core/src/commands/toggleList.d.ts +0 -18
- package/dist/packages/core/src/commands/toggleMark.d.ts +0 -30
- package/dist/packages/core/src/commands/toggleNode.d.ts +0 -17
- package/dist/packages/core/src/commands/toggleWrap.d.ts +0 -16
- package/dist/packages/core/src/commands/undoInputRule.d.ts +0 -13
- package/dist/packages/core/src/commands/unsetAllMarks.d.ts +0 -13
- package/dist/packages/core/src/commands/unsetMark.d.ts +0 -25
- package/dist/packages/core/src/commands/updateAttributes.d.ts +0 -24
- package/dist/packages/core/src/commands/wrapIn.d.ts +0 -16
- package/dist/packages/core/src/commands/wrapInList.d.ts +0 -16
- package/dist/packages/core/src/extensions/clipboardTextSerializer.d.ts +0 -5
- package/dist/packages/core/src/extensions/commands.d.ts +0 -3
- 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 -6
- package/dist/packages/core/src/extensions/keymap.d.ts +0 -2
- package/dist/packages/core/src/extensions/tabindex.d.ts +0 -2
- package/dist/packages/core/src/helpers/combineTransactionSteps.d.ts +0 -10
- package/dist/packages/core/src/helpers/createChainableState.d.ts +0 -10
- package/dist/packages/core/src/helpers/createDocument.d.ts +0 -12
- package/dist/packages/core/src/helpers/createNodeFromContent.d.ts +0 -15
- package/dist/packages/core/src/helpers/defaultBlockAt.d.ts +0 -7
- package/dist/packages/core/src/helpers/findChildren.d.ts +0 -9
- package/dist/packages/core/src/helpers/findChildrenInRange.d.ts +0 -10
- package/dist/packages/core/src/helpers/findParentNode.d.ts +0 -16
- package/dist/packages/core/src/helpers/findParentNodeClosestToPos.d.ts +0 -17
- package/dist/packages/core/src/helpers/generateHTML.d.ts +0 -8
- package/dist/packages/core/src/helpers/generateJSON.d.ts +0 -8
- package/dist/packages/core/src/helpers/generateText.d.ts +0 -12
- package/dist/packages/core/src/helpers/getAttributes.d.ts +0 -9
- package/dist/packages/core/src/helpers/getAttributesFromExtensions.d.ts +0 -6
- package/dist/packages/core/src/helpers/getChangedRanges.d.ts +0 -11
- package/dist/packages/core/src/helpers/getDebugJSON.d.ts +0 -8
- package/dist/packages/core/src/helpers/getExtensionField.d.ts +0 -9
- package/dist/packages/core/src/helpers/getHTMLFromFragment.d.ts +0 -2
- package/dist/packages/core/src/helpers/getMarkAttributes.d.ts +0 -3
- 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/getNodeAtPosition.d.ts +0 -11
- package/dist/packages/core/src/helpers/getNodeAttributes.d.ts +0 -3
- 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 -4
- package/dist/packages/core/src/helpers/getSchemaByResolvedExtensions.d.ts +0 -10
- package/dist/packages/core/src/helpers/getSchemaTypeByName.d.ts +0 -8
- package/dist/packages/core/src/helpers/getSchemaTypeNameByName.d.ts +0 -8
- package/dist/packages/core/src/helpers/getSplittedAttributes.d.ts +0 -9
- package/dist/packages/core/src/helpers/getText.d.ts +0 -15
- package/dist/packages/core/src/helpers/getTextBetween.d.ts +0 -14
- package/dist/packages/core/src/helpers/getTextContentFromNodes.d.ts +0 -8
- package/dist/packages/core/src/helpers/getTextSerializersFromSchema.d.ts +0 -8
- package/dist/packages/core/src/helpers/index.d.ts +0 -50
- package/dist/packages/core/src/helpers/injectExtensionAttributesToParseRule.d.ts +0 -9
- package/dist/packages/core/src/helpers/isActive.d.ts +0 -2
- package/dist/packages/core/src/helpers/isAtEndOfNode.d.ts +0 -2
- package/dist/packages/core/src/helpers/isAtStartOfNode.d.ts +0 -2
- package/dist/packages/core/src/helpers/isExtensionRulesEnabled.d.ts +0 -2
- package/dist/packages/core/src/helpers/isList.d.ts +0 -2
- package/dist/packages/core/src/helpers/isMarkActive.d.ts +0 -3
- package/dist/packages/core/src/helpers/isNodeActive.d.ts +0 -3
- 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/posToDOMRect.d.ts +0 -2
- package/dist/packages/core/src/helpers/resolveFocusPosition.d.ts +0 -4
- 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 -24
- package/dist/packages/core/src/inputRules/index.d.ts +0 -5
- package/dist/packages/core/src/inputRules/markInputRule.d.ts +0 -13
- package/dist/packages/core/src/inputRules/nodeInputRule.d.ts +0 -23
- package/dist/packages/core/src/inputRules/textInputRule.d.ts +0 -10
- package/dist/packages/core/src/inputRules/textblockTypeInputRule.d.ts +0 -15
- package/dist/packages/core/src/inputRules/wrappingInputRule.d.ts +0 -28
- package/dist/packages/core/src/pasteRules/index.d.ts +0 -3
- package/dist/packages/core/src/pasteRules/markPasteRule.d.ts +0 -13
- package/dist/packages/core/src/pasteRules/nodePasteRule.d.ts +0 -13
- package/dist/packages/core/src/pasteRules/textPasteRule.d.ts +0 -10
- package/dist/packages/core/src/style.d.ts +0 -1
- package/dist/packages/core/src/types.d.ts +0 -255
- package/dist/packages/core/src/utilities/callOrReturn.d.ts +0 -9
- package/dist/packages/core/src/utilities/createStyleTag.d.ts +0 -1
- package/dist/packages/core/src/utilities/deleteProps.d.ts +0 -6
- package/dist/packages/core/src/utilities/elementFromString.d.ts +0 -1
- package/dist/packages/core/src/utilities/escapeForRegEx.d.ts +0 -1
- package/dist/packages/core/src/utilities/findDuplicates.d.ts +0 -1
- package/dist/packages/core/src/utilities/fromString.d.ts +0 -1
- package/dist/packages/core/src/utilities/index.d.ts +0 -20
- package/dist/packages/core/src/utilities/isAndroid.d.ts +0 -1
- package/dist/packages/core/src/utilities/isEmptyObject.d.ts +0 -1
- package/dist/packages/core/src/utilities/isFunction.d.ts +0 -1
- package/dist/packages/core/src/utilities/isMacOS.d.ts +0 -1
- package/dist/packages/core/src/utilities/isNumber.d.ts +0 -1
- package/dist/packages/core/src/utilities/isPlainObject.d.ts +0 -1
- package/dist/packages/core/src/utilities/isRegExp.d.ts +0 -1
- package/dist/packages/core/src/utilities/isString.d.ts +0 -1
- package/dist/packages/core/src/utilities/isiOS.d.ts +0 -1
- package/dist/packages/core/src/utilities/mergeAttributes.d.ts +0 -1
- package/dist/packages/core/src/utilities/mergeDeep.d.ts +0 -1
- package/dist/packages/core/src/utilities/minMax.d.ts +0 -1
- package/dist/packages/core/src/utilities/objectIncludes.d.ts +0 -8
- package/dist/packages/core/src/utilities/removeDuplicates.d.ts +0 -8
package/src/NodePos.ts
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
Fragment, Node, ResolvedPos,
|
|
3
|
-
} from '@tiptap/pm/model'
|
|
1
|
+
import type { Fragment, Node, ResolvedPos } from '@tiptap/pm/model'
|
|
4
2
|
|
|
5
|
-
import { Editor } from './Editor.js'
|
|
6
|
-
import { Content, Range } from './types.js'
|
|
3
|
+
import type { Editor } from './Editor.js'
|
|
4
|
+
import type { Content, Range } from './types.js'
|
|
7
5
|
|
|
8
6
|
export class NodePos {
|
|
9
7
|
private resolvedPos: ResolvedPos
|
|
@@ -135,8 +133,15 @@ export class NodePos {
|
|
|
135
133
|
|
|
136
134
|
this.node.content.forEach((node, offset) => {
|
|
137
135
|
const isBlock = node.isBlock && !node.isTextblock
|
|
136
|
+
const isNonTextAtom = node.isAtom && !node.isText
|
|
137
|
+
|
|
138
|
+
const targetPos = this.pos + offset + (isNonTextAtom ? 0 : 1)
|
|
139
|
+
|
|
140
|
+
// Check if targetPos is within valid document range
|
|
141
|
+
if (targetPos < 0 || targetPos > this.resolvedPos.doc.nodeSize - 2) {
|
|
142
|
+
return
|
|
143
|
+
}
|
|
138
144
|
|
|
139
|
-
const targetPos = this.pos + offset + 1
|
|
140
145
|
const $pos = this.resolvedPos.doc.resolve(targetPos)
|
|
141
146
|
|
|
142
147
|
if (!isBlock && $pos.depth <= this.depth) {
|
|
@@ -235,9 +240,13 @@ export class NodePos {
|
|
|
235
240
|
}
|
|
236
241
|
|
|
237
242
|
setAttribute(attributes: { [key: string]: any }) {
|
|
238
|
-
const
|
|
243
|
+
const { tr } = this.editor.state
|
|
244
|
+
|
|
245
|
+
tr.setNodeMarkup(this.from, undefined, {
|
|
246
|
+
...this.node.attrs,
|
|
247
|
+
...attributes,
|
|
248
|
+
})
|
|
239
249
|
|
|
240
|
-
this.editor.
|
|
241
|
-
.run()
|
|
250
|
+
this.editor.view.dispatch(tr)
|
|
242
251
|
}
|
|
243
252
|
}
|
package/src/NodeView.ts
CHANGED
|
@@ -1,10 +1,8 @@
|
|
|
1
|
-
import { Node as ProseMirrorNode } from '@tiptap/pm/model'
|
|
2
1
|
import { NodeSelection } from '@tiptap/pm/state'
|
|
3
|
-
import { NodeView as ProseMirrorNodeView } from '@tiptap/pm/view'
|
|
2
|
+
import type { NodeView as ProseMirrorNodeView, ViewMutationRecord } from '@tiptap/pm/view'
|
|
4
3
|
|
|
5
|
-
import { Editor as CoreEditor } from './Editor.js'
|
|
6
|
-
import {
|
|
7
|
-
import { DecorationWithType, NodeViewRendererOptions, NodeViewRendererProps } from './types.js'
|
|
4
|
+
import type { Editor as CoreEditor } from './Editor.js'
|
|
5
|
+
import type { DecorationWithType, NodeViewRendererOptions, NodeViewRendererProps } from './types.js'
|
|
8
6
|
import { isAndroid } from './utilities/isAndroid.js'
|
|
9
7
|
import { isiOS } from './utilities/isiOS.js'
|
|
10
8
|
|
|
@@ -16,20 +14,27 @@ export class NodeView<
|
|
|
16
14
|
Component,
|
|
17
15
|
NodeEditor extends CoreEditor = CoreEditor,
|
|
18
16
|
Options extends NodeViewRendererOptions = NodeViewRendererOptions,
|
|
19
|
-
> implements ProseMirrorNodeView
|
|
17
|
+
> implements ProseMirrorNodeView
|
|
18
|
+
{
|
|
20
19
|
component: Component
|
|
21
20
|
|
|
22
21
|
editor: NodeEditor
|
|
23
22
|
|
|
24
23
|
options: Options
|
|
25
24
|
|
|
26
|
-
extension:
|
|
25
|
+
extension: NodeViewRendererProps['extension']
|
|
27
26
|
|
|
28
|
-
node:
|
|
27
|
+
node: NodeViewRendererProps['node']
|
|
29
28
|
|
|
30
|
-
decorations:
|
|
29
|
+
decorations: NodeViewRendererProps['decorations']
|
|
31
30
|
|
|
32
|
-
|
|
31
|
+
innerDecorations: NodeViewRendererProps['innerDecorations']
|
|
32
|
+
|
|
33
|
+
view: NodeViewRendererProps['view']
|
|
34
|
+
|
|
35
|
+
getPos: NodeViewRendererProps['getPos']
|
|
36
|
+
|
|
37
|
+
HTMLAttributes: NodeViewRendererProps['HTMLAttributes']
|
|
33
38
|
|
|
34
39
|
isDragging = false
|
|
35
40
|
|
|
@@ -44,6 +49,9 @@ export class NodeView<
|
|
|
44
49
|
this.extension = props.extension
|
|
45
50
|
this.node = props.node
|
|
46
51
|
this.decorations = props.decorations as DecorationWithType[]
|
|
52
|
+
this.innerDecorations = props.innerDecorations
|
|
53
|
+
this.view = props.view
|
|
54
|
+
this.HTMLAttributes = props.HTMLAttributes
|
|
47
55
|
this.getPos = props.getPos
|
|
48
56
|
this.mount()
|
|
49
57
|
}
|
|
@@ -67,9 +75,8 @@ export class NodeView<
|
|
|
67
75
|
|
|
68
76
|
// get the drag handle element
|
|
69
77
|
// `closest` is not available for text nodes so we may have to use its parent
|
|
70
|
-
const dragHandle =
|
|
71
|
-
? target.parentElement?.closest('[data-drag-handle]')
|
|
72
|
-
: target.closest('[data-drag-handle]')
|
|
78
|
+
const dragHandle =
|
|
79
|
+
target.nodeType === 3 ? target.parentElement?.closest('[data-drag-handle]') : target.closest('[data-drag-handle]')
|
|
73
80
|
|
|
74
81
|
if (!this.dom || this.contentDOM?.contains(target) || !dragHandle) {
|
|
75
82
|
return
|
|
@@ -91,11 +98,18 @@ export class NodeView<
|
|
|
91
98
|
y = handleBox.y - domBox.y + offsetY
|
|
92
99
|
}
|
|
93
100
|
|
|
94
|
-
|
|
101
|
+
const clonedNode = this.dom.cloneNode(true) as HTMLElement
|
|
102
|
+
|
|
103
|
+
event.dataTransfer?.setDragImage(clonedNode, x, y)
|
|
95
104
|
|
|
105
|
+
const pos = this.getPos()
|
|
106
|
+
|
|
107
|
+
if (typeof pos !== 'number') {
|
|
108
|
+
return
|
|
109
|
+
}
|
|
96
110
|
// we need to tell ProseMirror that we want to move the whole node
|
|
97
111
|
// so we create a NodeSelection
|
|
98
|
-
const selection = NodeSelection.create(view.state.doc,
|
|
112
|
+
const selection = NodeSelection.create(view.state.doc, pos)
|
|
99
113
|
const transaction = view.state.tr.setSelection(selection)
|
|
100
114
|
|
|
101
115
|
view.dispatch(transaction)
|
|
@@ -139,11 +153,11 @@ export class NodeView<
|
|
|
139
153
|
// ProseMirror tries to drag selectable nodes
|
|
140
154
|
// even if `draggable` is set to `false`
|
|
141
155
|
// this fix prevents that
|
|
142
|
-
if (!isDraggable && isSelectable && isDragEvent) {
|
|
156
|
+
if (!isDraggable && isSelectable && isDragEvent && event.target === this.dom) {
|
|
143
157
|
event.preventDefault()
|
|
144
158
|
}
|
|
145
159
|
|
|
146
|
-
if (isDraggable && isDragEvent && !isDragging) {
|
|
160
|
+
if (isDraggable && isDragEvent && !isDragging && event.target === this.dom) {
|
|
147
161
|
event.preventDefault()
|
|
148
162
|
return false
|
|
149
163
|
}
|
|
@@ -183,21 +197,19 @@ export class NodeView<
|
|
|
183
197
|
}
|
|
184
198
|
|
|
185
199
|
// these events are handled by prosemirror
|
|
186
|
-
if (
|
|
187
|
-
isDragging
|
|
188
|
-
|| isDropEvent
|
|
189
|
-
|| isCopyEvent
|
|
190
|
-
|| isPasteEvent
|
|
191
|
-
|| isCutEvent
|
|
192
|
-
|| (isClickEvent && isSelectable)
|
|
193
|
-
) {
|
|
200
|
+
if (isDragging || isDropEvent || isCopyEvent || isPasteEvent || isCutEvent || (isClickEvent && isSelectable)) {
|
|
194
201
|
return false
|
|
195
202
|
}
|
|
196
203
|
|
|
197
204
|
return true
|
|
198
205
|
}
|
|
199
206
|
|
|
200
|
-
|
|
207
|
+
/**
|
|
208
|
+
* Called when a DOM [mutation](https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver) or a selection change happens within the view.
|
|
209
|
+
* @return `false` if the editor should re-read the selection or re-parse the range around the mutation
|
|
210
|
+
* @return `true` if it can safely be ignored.
|
|
211
|
+
*/
|
|
212
|
+
ignoreMutation(mutation: ViewMutationRecord) {
|
|
201
213
|
if (!this.dom || !this.contentDOM) {
|
|
202
214
|
return true
|
|
203
215
|
}
|
|
@@ -223,15 +235,12 @@ export class NodeView<
|
|
|
223
235
|
// see: https://github.com/ueberdosis/tiptap/issues/1214
|
|
224
236
|
// see: https://github.com/ueberdosis/tiptap/issues/2534
|
|
225
237
|
if (
|
|
226
|
-
this.dom.contains(mutation.target)
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
238
|
+
this.dom.contains(mutation.target) &&
|
|
239
|
+
mutation.type === 'childList' &&
|
|
240
|
+
(isiOS() || isAndroid()) &&
|
|
241
|
+
this.editor.isFocused
|
|
230
242
|
) {
|
|
231
|
-
const changedNodes = [
|
|
232
|
-
...Array.from(mutation.addedNodes),
|
|
233
|
-
...Array.from(mutation.removedNodes),
|
|
234
|
-
] as HTMLElement[]
|
|
243
|
+
const changedNodes = [...Array.from(mutation.addedNodes), ...Array.from(mutation.removedNodes)] as HTMLElement[]
|
|
235
244
|
|
|
236
245
|
// we’ll check if every changed node is contentEditable
|
|
237
246
|
// to make sure it’s probably mutated by ProseMirror
|
|
@@ -254,10 +263,17 @@ export class NodeView<
|
|
|
254
263
|
return true
|
|
255
264
|
}
|
|
256
265
|
|
|
257
|
-
|
|
266
|
+
/**
|
|
267
|
+
* Update the attributes of the prosemirror node.
|
|
268
|
+
*/
|
|
269
|
+
updateAttributes(attributes: Record<string, any>): void {
|
|
258
270
|
this.editor.commands.command(({ tr }) => {
|
|
259
271
|
const pos = this.getPos()
|
|
260
272
|
|
|
273
|
+
if (typeof pos !== 'number') {
|
|
274
|
+
return false
|
|
275
|
+
}
|
|
276
|
+
|
|
261
277
|
tr.setNodeMarkup(pos, undefined, {
|
|
262
278
|
...this.node.attrs,
|
|
263
279
|
...attributes,
|
|
@@ -267,8 +283,15 @@ export class NodeView<
|
|
|
267
283
|
})
|
|
268
284
|
}
|
|
269
285
|
|
|
286
|
+
/**
|
|
287
|
+
* Delete the node.
|
|
288
|
+
*/
|
|
270
289
|
deleteNode(): void {
|
|
271
290
|
const from = this.getPos()
|
|
291
|
+
|
|
292
|
+
if (typeof from !== 'number') {
|
|
293
|
+
return
|
|
294
|
+
}
|
|
272
295
|
const to = from + this.node.nodeSize
|
|
273
296
|
|
|
274
297
|
this.editor.commands.deleteRange({ from, to })
|
package/src/PasteRule.ts
CHANGED
|
@@ -1,15 +1,13 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { Node as ProseMirrorNode } from '@tiptap/pm/model'
|
|
2
|
+
import { Fragment } from '@tiptap/pm/model'
|
|
3
|
+
import type { EditorState } from '@tiptap/pm/state'
|
|
4
|
+
import { Plugin } from '@tiptap/pm/state'
|
|
2
5
|
|
|
3
6
|
import { CommandManager } from './CommandManager.js'
|
|
4
|
-
import { Editor } from './Editor.js'
|
|
7
|
+
import type { Editor } from './Editor.js'
|
|
5
8
|
import { createChainableState } from './helpers/createChainableState.js'
|
|
6
|
-
import {
|
|
7
|
-
|
|
8
|
-
ChainedCommands,
|
|
9
|
-
ExtendedRegExpMatchArray,
|
|
10
|
-
Range,
|
|
11
|
-
SingleCommands,
|
|
12
|
-
} from './types.js'
|
|
9
|
+
import { getHTMLFromFragment } from './helpers/getHTMLFromFragment.js'
|
|
10
|
+
import type { CanCommands, ChainedCommands, ExtendedRegExpMatchArray, Range, SingleCommands } from './types.js'
|
|
13
11
|
import { isNumber } from './utilities/isNumber.js'
|
|
14
12
|
import { isRegExp } from './utilities/isRegExp.js'
|
|
15
13
|
|
|
@@ -21,11 +19,13 @@ export type PasteRuleMatch = {
|
|
|
21
19
|
data?: Record<string, any>
|
|
22
20
|
}
|
|
23
21
|
|
|
24
|
-
export type PasteRuleFinder =
|
|
22
|
+
export type PasteRuleFinder =
|
|
23
|
+
| RegExp
|
|
24
|
+
| ((text: string, event?: ClipboardEvent | null) => PasteRuleMatch[] | null | undefined)
|
|
25
25
|
|
|
26
26
|
/**
|
|
27
27
|
* Paste rules are used to react to pasted content.
|
|
28
|
-
* @see https://tiptap.dev/
|
|
28
|
+
* @see https://tiptap.dev/docs/editor/extensions/custom-extensions/extend-existing#paste-rules
|
|
29
29
|
*/
|
|
30
30
|
export class PasteRule {
|
|
31
31
|
find: PasteRuleFinder
|
|
@@ -83,9 +83,7 @@ const pasteRuleMatcherHandler = (
|
|
|
83
83
|
|
|
84
84
|
if (pasteRuleMatch.replaceWith) {
|
|
85
85
|
if (!pasteRuleMatch.text.includes(pasteRuleMatch.replaceWith)) {
|
|
86
|
-
console.warn(
|
|
87
|
-
'[tiptap warn]: "pasteRuleMatch.replaceWith" must be part of "pasteRuleMatch.text".',
|
|
88
|
-
)
|
|
86
|
+
console.warn('[tiptap warn]: "pasteRuleMatch.replaceWith" must be part of "pasteRuleMatch.text".')
|
|
89
87
|
}
|
|
90
88
|
|
|
91
89
|
result.push(pasteRuleMatch.replaceWith)
|
|
@@ -104,9 +102,7 @@ function run(config: {
|
|
|
104
102
|
pasteEvent: ClipboardEvent | null
|
|
105
103
|
dropEvent: DragEvent | null
|
|
106
104
|
}): boolean {
|
|
107
|
-
const {
|
|
108
|
-
editor, state, from, to, rule, pasteEvent, dropEvent,
|
|
109
|
-
} = config
|
|
105
|
+
const { editor, state, from, to, rule, pasteEvent, dropEvent } = config
|
|
110
106
|
|
|
111
107
|
const { commands, chain, can } = new CommandManager({
|
|
112
108
|
editor,
|
|
@@ -158,6 +154,9 @@ function run(config: {
|
|
|
158
154
|
return success
|
|
159
155
|
}
|
|
160
156
|
|
|
157
|
+
// When dragging across editors, must get another editor instance to delete selection content.
|
|
158
|
+
let tiptapDragFromOtherEditor: Editor | null = null
|
|
159
|
+
|
|
161
160
|
const createClipboardPasteEvent = (text: string) => {
|
|
162
161
|
const event = new ClipboardEvent('paste', {
|
|
163
162
|
clipboardData: new DataTransfer(),
|
|
@@ -179,7 +178,13 @@ export function pasteRulesPlugin(props: { editor: Editor; rules: PasteRule[] }):
|
|
|
179
178
|
let isPastedFromProseMirror = false
|
|
180
179
|
let isDroppedFromProseMirror = false
|
|
181
180
|
let pasteEvent = typeof ClipboardEvent !== 'undefined' ? new ClipboardEvent('paste') : null
|
|
182
|
-
let dropEvent
|
|
181
|
+
let dropEvent: DragEvent | null
|
|
182
|
+
|
|
183
|
+
try {
|
|
184
|
+
dropEvent = typeof DragEvent !== 'undefined' ? new DragEvent('drop') : null
|
|
185
|
+
} catch {
|
|
186
|
+
dropEvent = null
|
|
187
|
+
}
|
|
183
188
|
|
|
184
189
|
const processEvent = ({
|
|
185
190
|
state,
|
|
@@ -214,7 +219,11 @@ export function pasteRulesPlugin(props: { editor: Editor; rules: PasteRule[] }):
|
|
|
214
219
|
return
|
|
215
220
|
}
|
|
216
221
|
|
|
217
|
-
|
|
222
|
+
try {
|
|
223
|
+
dropEvent = typeof DragEvent !== 'undefined' ? new DragEvent('drop') : null
|
|
224
|
+
} catch {
|
|
225
|
+
dropEvent = null
|
|
226
|
+
}
|
|
218
227
|
pasteEvent = typeof ClipboardEvent !== 'undefined' ? new ClipboardEvent('paste') : null
|
|
219
228
|
|
|
220
229
|
return tr
|
|
@@ -225,16 +234,26 @@ export function pasteRulesPlugin(props: { editor: Editor; rules: PasteRule[] }):
|
|
|
225
234
|
// we register a global drag handler to track the current drag source element
|
|
226
235
|
view(view) {
|
|
227
236
|
const handleDragstart = (event: DragEvent) => {
|
|
228
|
-
dragSourceElement = view.dom.parentElement?.contains(event.target as Element)
|
|
229
|
-
|
|
230
|
-
|
|
237
|
+
dragSourceElement = view.dom.parentElement?.contains(event.target as Element) ? view.dom.parentElement : null
|
|
238
|
+
|
|
239
|
+
if (dragSourceElement) {
|
|
240
|
+
tiptapDragFromOtherEditor = editor
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
const handleDragend = () => {
|
|
245
|
+
if (tiptapDragFromOtherEditor) {
|
|
246
|
+
tiptapDragFromOtherEditor = null
|
|
247
|
+
}
|
|
231
248
|
}
|
|
232
249
|
|
|
233
250
|
window.addEventListener('dragstart', handleDragstart)
|
|
251
|
+
window.addEventListener('dragend', handleDragend)
|
|
234
252
|
|
|
235
253
|
return {
|
|
236
254
|
destroy() {
|
|
237
255
|
window.removeEventListener('dragstart', handleDragstart)
|
|
256
|
+
window.removeEventListener('dragend', handleDragend)
|
|
238
257
|
},
|
|
239
258
|
}
|
|
240
259
|
},
|
|
@@ -245,6 +264,20 @@ export function pasteRulesPlugin(props: { editor: Editor; rules: PasteRule[] }):
|
|
|
245
264
|
isDroppedFromProseMirror = dragSourceElement === view.dom.parentElement
|
|
246
265
|
dropEvent = event as DragEvent
|
|
247
266
|
|
|
267
|
+
if (!isDroppedFromProseMirror) {
|
|
268
|
+
const dragFromOtherEditor = tiptapDragFromOtherEditor
|
|
269
|
+
|
|
270
|
+
if (dragFromOtherEditor?.isEditable) {
|
|
271
|
+
// setTimeout to avoid the wrong content after drop, timeout arg can't be empty or 0
|
|
272
|
+
setTimeout(() => {
|
|
273
|
+
const selection = dragFromOtherEditor.state.selection
|
|
274
|
+
|
|
275
|
+
if (selection) {
|
|
276
|
+
dragFromOtherEditor.commands.deleteRange({ from: selection.from, to: selection.to })
|
|
277
|
+
}
|
|
278
|
+
}, 10)
|
|
279
|
+
}
|
|
280
|
+
}
|
|
248
281
|
return false
|
|
249
282
|
},
|
|
250
283
|
|
|
@@ -266,7 +299,9 @@ export function pasteRulesPlugin(props: { editor: Editor; rules: PasteRule[] }):
|
|
|
266
299
|
const isDrop = transaction.getMeta('uiEvent') === 'drop' && !isDroppedFromProseMirror
|
|
267
300
|
|
|
268
301
|
// if PasteRule is triggered by insertContent()
|
|
269
|
-
const simulatedPasteMeta = transaction.getMeta('applyPasteRules')
|
|
302
|
+
const simulatedPasteMeta = transaction.getMeta('applyPasteRules') as
|
|
303
|
+
| undefined
|
|
304
|
+
| { from: number; text: string | ProseMirrorNode | Fragment }
|
|
270
305
|
const isSimulatedPaste = !!simulatedPasteMeta
|
|
271
306
|
|
|
272
307
|
if (!isPaste && !isDrop && !isSimulatedPaste) {
|
|
@@ -275,8 +310,17 @@ export function pasteRulesPlugin(props: { editor: Editor; rules: PasteRule[] }):
|
|
|
275
310
|
|
|
276
311
|
// Handle simulated paste
|
|
277
312
|
if (isSimulatedPaste) {
|
|
278
|
-
|
|
313
|
+
let { text } = simulatedPasteMeta
|
|
314
|
+
|
|
315
|
+
if (typeof text === 'string') {
|
|
316
|
+
text = text as string
|
|
317
|
+
} else {
|
|
318
|
+
text = getHTMLFromFragment(Fragment.from(text), state.schema)
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
const { from } = simulatedPasteMeta
|
|
279
322
|
const to = from + text.length
|
|
323
|
+
|
|
280
324
|
const pasteEvt = createClipboardPasteEvent(text)
|
|
281
325
|
|
|
282
326
|
return processEvent({
|
package/src/Tracker.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Transaction } from '@tiptap/pm/state'
|
|
1
|
+
import type { Transaction } from '@tiptap/pm/state'
|
|
2
2
|
|
|
3
3
|
export interface TrackerResult {
|
|
4
4
|
position: number
|
|
@@ -18,17 +18,15 @@ export class Tracker {
|
|
|
18
18
|
map(position: number): TrackerResult {
|
|
19
19
|
let deleted = false
|
|
20
20
|
|
|
21
|
-
const mappedPosition = this.transaction.steps
|
|
22
|
-
.
|
|
23
|
-
.reduce((newPosition, step) => {
|
|
24
|
-
const mapResult = step.getMap().mapResult(newPosition)
|
|
21
|
+
const mappedPosition = this.transaction.steps.slice(this.currentStep).reduce((newPosition, step) => {
|
|
22
|
+
const mapResult = step.getMap().mapResult(newPosition)
|
|
25
23
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
24
|
+
if (mapResult.deleted) {
|
|
25
|
+
deleted = true
|
|
26
|
+
}
|
|
29
27
|
|
|
30
|
-
|
|
31
|
-
|
|
28
|
+
return mapResult.pos
|
|
29
|
+
}, position)
|
|
32
30
|
|
|
33
31
|
return {
|
|
34
32
|
position: mappedPosition,
|
package/src/commands/blur.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { RawCommands } from '../types.js'
|
|
1
|
+
import type { RawCommands } from '../types.js'
|
|
2
2
|
|
|
3
3
|
declare module '@tiptap/core' {
|
|
4
4
|
interface Commands<ReturnType> {
|
|
@@ -7,21 +7,23 @@ declare module '@tiptap/core' {
|
|
|
7
7
|
* Removes focus from the editor.
|
|
8
8
|
* @example editor.commands.blur()
|
|
9
9
|
*/
|
|
10
|
-
blur: () => ReturnType
|
|
10
|
+
blur: () => ReturnType
|
|
11
11
|
}
|
|
12
12
|
}
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
-
export const blur: RawCommands['blur'] =
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
15
|
+
export const blur: RawCommands['blur'] =
|
|
16
|
+
() =>
|
|
17
|
+
({ editor, view }) => {
|
|
18
|
+
requestAnimationFrame(() => {
|
|
19
|
+
if (!editor.isDestroyed) {
|
|
20
|
+
;(view.dom as HTMLElement).blur()
|
|
19
21
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
22
|
+
// Browsers should remove the caret on blur but safari does not.
|
|
23
|
+
// See: https://github.com/ueberdosis/tiptap/issues/2405
|
|
24
|
+
window?.getSelection()?.removeAllRanges()
|
|
25
|
+
}
|
|
26
|
+
})
|
|
25
27
|
|
|
26
|
-
|
|
27
|
-
}
|
|
28
|
+
return true
|
|
29
|
+
}
|
|
@@ -1,18 +1,25 @@
|
|
|
1
|
-
import { RawCommands } from '../types.js'
|
|
1
|
+
import type { RawCommands } from '../types.js'
|
|
2
2
|
|
|
3
3
|
declare module '@tiptap/core' {
|
|
4
4
|
interface Commands<ReturnType> {
|
|
5
5
|
clearContent: {
|
|
6
6
|
/**
|
|
7
7
|
* Clear the whole document.
|
|
8
|
-
* @param emitUpdate Whether to emit an update event.
|
|
9
8
|
* @example editor.commands.clearContent()
|
|
10
9
|
*/
|
|
11
|
-
clearContent: (
|
|
10
|
+
clearContent: (
|
|
11
|
+
/**
|
|
12
|
+
* Whether to emit an update event.
|
|
13
|
+
* @default true
|
|
14
|
+
*/
|
|
15
|
+
emitUpdate?: boolean,
|
|
16
|
+
) => ReturnType
|
|
12
17
|
}
|
|
13
18
|
}
|
|
14
19
|
}
|
|
15
20
|
|
|
16
|
-
export const clearContent: RawCommands['clearContent'] =
|
|
17
|
-
|
|
18
|
-
}
|
|
21
|
+
export const clearContent: RawCommands['clearContent'] =
|
|
22
|
+
(emitUpdate = true) =>
|
|
23
|
+
({ commands }) => {
|
|
24
|
+
return commands.setContent('', { emitUpdate })
|
|
25
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { liftTarget } from '@tiptap/pm/transform'
|
|
2
2
|
|
|
3
|
-
import { RawCommands } from '../types.js'
|
|
3
|
+
import type { RawCommands } from '../types.js'
|
|
4
4
|
|
|
5
5
|
declare module '@tiptap/core' {
|
|
6
6
|
interface Commands<ReturnType> {
|
|
@@ -9,47 +9,49 @@ declare module '@tiptap/core' {
|
|
|
9
9
|
* Normalize nodes to a simple paragraph.
|
|
10
10
|
* @example editor.commands.clearNodes()
|
|
11
11
|
*/
|
|
12
|
-
clearNodes: () => ReturnType
|
|
12
|
+
clearNodes: () => ReturnType
|
|
13
13
|
}
|
|
14
14
|
}
|
|
15
15
|
}
|
|
16
16
|
|
|
17
|
-
export const clearNodes: RawCommands['clearNodes'] =
|
|
18
|
-
|
|
19
|
-
|
|
17
|
+
export const clearNodes: RawCommands['clearNodes'] =
|
|
18
|
+
() =>
|
|
19
|
+
({ state, tr, dispatch }) => {
|
|
20
|
+
const { selection } = tr
|
|
21
|
+
const { ranges } = selection
|
|
20
22
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
23
|
+
if (!dispatch) {
|
|
24
|
+
return true
|
|
25
|
+
}
|
|
24
26
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
27
|
+
ranges.forEach(({ $from, $to }) => {
|
|
28
|
+
state.doc.nodesBetween($from.pos, $to.pos, (node, pos) => {
|
|
29
|
+
if (node.type.isText) {
|
|
30
|
+
return
|
|
31
|
+
}
|
|
30
32
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
33
|
+
const { doc, mapping } = tr
|
|
34
|
+
const $mappedFrom = doc.resolve(mapping.map(pos))
|
|
35
|
+
const $mappedTo = doc.resolve(mapping.map(pos + node.nodeSize))
|
|
36
|
+
const nodeRange = $mappedFrom.blockRange($mappedTo)
|
|
35
37
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
38
|
+
if (!nodeRange) {
|
|
39
|
+
return
|
|
40
|
+
}
|
|
39
41
|
|
|
40
|
-
|
|
42
|
+
const targetLiftDepth = liftTarget(nodeRange)
|
|
41
43
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
+
if (node.type.isTextblock) {
|
|
45
|
+
const { defaultType } = $mappedFrom.parent.contentMatchAt($mappedFrom.index())
|
|
44
46
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
+
tr.setNodeMarkup(nodeRange.start, defaultType)
|
|
48
|
+
}
|
|
47
49
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
50
|
+
if (targetLiftDepth || targetLiftDepth === 0) {
|
|
51
|
+
tr.lift(nodeRange, targetLiftDepth)
|
|
52
|
+
}
|
|
53
|
+
})
|
|
51
54
|
})
|
|
52
|
-
})
|
|
53
55
|
|
|
54
|
-
|
|
55
|
-
}
|
|
56
|
+
return true
|
|
57
|
+
}
|
package/src/commands/command.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Command, RawCommands } from '../types.js'
|
|
1
|
+
import type { Command, RawCommands } from '../types.js'
|
|
2
2
|
|
|
3
3
|
declare module '@tiptap/core' {
|
|
4
4
|
interface Commands<ReturnType> {
|
|
@@ -12,7 +12,7 @@ declare module '@tiptap/core' {
|
|
|
12
12
|
* return true
|
|
13
13
|
* })
|
|
14
14
|
*/
|
|
15
|
-
command: (fn: (props: Parameters<Command>[0]) => boolean) => ReturnType
|
|
15
|
+
command: (fn: (props: Parameters<Command>[0]) => boolean) => ReturnType
|
|
16
16
|
}
|
|
17
17
|
}
|
|
18
18
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { createParagraphNear as originalCreateParagraphNear } from '@tiptap/pm/commands'
|
|
2
2
|
|
|
3
|
-
import { RawCommands } from '../types.js'
|
|
3
|
+
import type { RawCommands } from '../types.js'
|
|
4
4
|
|
|
5
5
|
declare module '@tiptap/core' {
|
|
6
6
|
interface Commands<ReturnType> {
|
|
@@ -14,6 +14,8 @@ declare module '@tiptap/core' {
|
|
|
14
14
|
}
|
|
15
15
|
}
|
|
16
16
|
|
|
17
|
-
export const createParagraphNear: RawCommands['createParagraphNear'] =
|
|
18
|
-
|
|
19
|
-
}
|
|
17
|
+
export const createParagraphNear: RawCommands['createParagraphNear'] =
|
|
18
|
+
() =>
|
|
19
|
+
({ state, dispatch }) => {
|
|
20
|
+
return originalCreateParagraphNear(state, dispatch)
|
|
21
|
+
}
|