@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
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { MarkType, NodeType } from '@tiptap/pm/model'
|
|
1
|
+
import type { MarkType, NodeType } from '@tiptap/pm/model'
|
|
2
2
|
|
|
3
3
|
import { getMarkType } from '../helpers/getMarkType.js'
|
|
4
4
|
import { getNodeType } from '../helpers/getNodeType.js'
|
|
5
5
|
import { getSchemaTypeNameByName } from '../helpers/getSchemaTypeNameByName.js'
|
|
6
|
-
import { RawCommands } from '../types.js'
|
|
6
|
+
import type { RawCommands } from '../types.js'
|
|
7
7
|
import { deleteProps } from '../utilities/deleteProps.js'
|
|
8
8
|
|
|
9
9
|
declare module '@tiptap/core' {
|
|
@@ -15,56 +15,51 @@ declare module '@tiptap/core' {
|
|
|
15
15
|
* @param attributes The attributes of the node to reset.
|
|
16
16
|
* @example editor.commands.resetAttributes('heading', 'level')
|
|
17
17
|
*/
|
|
18
|
-
resetAttributes: (
|
|
19
|
-
typeOrName: string | NodeType | MarkType,
|
|
20
|
-
attributes: string | string[],
|
|
21
|
-
) => ReturnType
|
|
18
|
+
resetAttributes: (typeOrName: string | NodeType | MarkType, attributes: string | string[]) => ReturnType
|
|
22
19
|
}
|
|
23
20
|
}
|
|
24
21
|
}
|
|
25
22
|
|
|
26
|
-
export const resetAttributes: RawCommands['resetAttributes'] =
|
|
27
|
-
|
|
28
|
-
|
|
23
|
+
export const resetAttributes: RawCommands['resetAttributes'] =
|
|
24
|
+
(typeOrName, attributes) =>
|
|
25
|
+
({ tr, state, dispatch }) => {
|
|
26
|
+
let nodeType: NodeType | null = null
|
|
27
|
+
let markType: MarkType | null = null
|
|
29
28
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
29
|
+
const schemaType = getSchemaTypeNameByName(
|
|
30
|
+
typeof typeOrName === 'string' ? typeOrName : typeOrName.name,
|
|
31
|
+
state.schema,
|
|
32
|
+
)
|
|
34
33
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
34
|
+
if (!schemaType) {
|
|
35
|
+
return false
|
|
36
|
+
}
|
|
38
37
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
38
|
+
if (schemaType === 'node') {
|
|
39
|
+
nodeType = getNodeType(typeOrName as NodeType, state.schema)
|
|
40
|
+
}
|
|
42
41
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
42
|
+
if (schemaType === 'mark') {
|
|
43
|
+
markType = getMarkType(typeOrName as MarkType, state.schema)
|
|
44
|
+
}
|
|
46
45
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
46
|
+
if (dispatch) {
|
|
47
|
+
tr.selection.ranges.forEach(range => {
|
|
48
|
+
state.doc.nodesBetween(range.$from.pos, range.$to.pos, (node, pos) => {
|
|
49
|
+
if (nodeType && nodeType === node.type) {
|
|
50
|
+
tr.setNodeMarkup(pos, undefined, deleteProps(node.attrs, attributes))
|
|
51
|
+
}
|
|
53
52
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
}
|
|
63
|
-
})
|
|
64
|
-
}
|
|
53
|
+
if (markType && node.marks.length) {
|
|
54
|
+
node.marks.forEach(mark => {
|
|
55
|
+
if (markType === mark.type) {
|
|
56
|
+
tr.addMark(pos, pos + node.nodeSize, markType.create(deleteProps(mark.attrs, attributes)))
|
|
57
|
+
}
|
|
58
|
+
})
|
|
59
|
+
}
|
|
60
|
+
})
|
|
65
61
|
})
|
|
66
|
-
}
|
|
67
|
-
}
|
|
62
|
+
}
|
|
68
63
|
|
|
69
|
-
|
|
70
|
-
}
|
|
64
|
+
return true
|
|
65
|
+
}
|
|
@@ -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,15 +7,17 @@ declare module '@tiptap/core' {
|
|
|
7
7
|
* Scroll the selection into view.
|
|
8
8
|
* @example editor.commands.scrollIntoView()
|
|
9
9
|
*/
|
|
10
|
-
scrollIntoView: () => ReturnType
|
|
10
|
+
scrollIntoView: () => ReturnType
|
|
11
11
|
}
|
|
12
12
|
}
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
-
export const scrollIntoView: RawCommands['scrollIntoView'] =
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
15
|
+
export const scrollIntoView: RawCommands['scrollIntoView'] =
|
|
16
|
+
() =>
|
|
17
|
+
({ tr, dispatch }) => {
|
|
18
|
+
if (dispatch) {
|
|
19
|
+
tr.scrollIntoView()
|
|
20
|
+
}
|
|
19
21
|
|
|
20
|
-
|
|
21
|
-
}
|
|
22
|
+
return true
|
|
23
|
+
}
|
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { AllSelection } from '@tiptap/pm/state'
|
|
2
|
+
|
|
3
|
+
import type { RawCommands } from '../types.js'
|
|
2
4
|
|
|
3
5
|
declare module '@tiptap/core' {
|
|
4
6
|
interface Commands<ReturnType> {
|
|
@@ -7,14 +9,19 @@ declare module '@tiptap/core' {
|
|
|
7
9
|
* Select the whole document.
|
|
8
10
|
* @example editor.commands.selectAll()
|
|
9
11
|
*/
|
|
10
|
-
selectAll: () => ReturnType
|
|
12
|
+
selectAll: () => ReturnType
|
|
11
13
|
}
|
|
12
14
|
}
|
|
13
15
|
}
|
|
14
16
|
|
|
15
|
-
export const selectAll: RawCommands['selectAll'] =
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
17
|
+
export const selectAll: RawCommands['selectAll'] =
|
|
18
|
+
() =>
|
|
19
|
+
({ tr, dispatch }) => {
|
|
20
|
+
if (dispatch) {
|
|
21
|
+
const selection = new AllSelection(tr.doc)
|
|
22
|
+
|
|
23
|
+
tr.setSelection(selection)
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
return true
|
|
27
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { selectNodeBackward as originalSelectNodeBackward } 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 selectNodeBackward: RawCommands['selectNodeBackward'] =
|
|
18
|
-
|
|
19
|
-
}
|
|
17
|
+
export const selectNodeBackward: RawCommands['selectNodeBackward'] =
|
|
18
|
+
() =>
|
|
19
|
+
({ state, dispatch }) => {
|
|
20
|
+
return originalSelectNodeBackward(state, dispatch)
|
|
21
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { selectNodeForward as originalSelectNodeForward } 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 selectNodeForward: RawCommands['selectNodeForward'] =
|
|
18
|
-
|
|
19
|
-
}
|
|
17
|
+
export const selectNodeForward: RawCommands['selectNodeForward'] =
|
|
18
|
+
() =>
|
|
19
|
+
({ state, dispatch }) => {
|
|
20
|
+
return originalSelectNodeForward(state, dispatch)
|
|
21
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { selectParentNode as originalSelectParentNode } 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 selectParentNode: RawCommands['selectParentNode'] =
|
|
18
|
-
|
|
19
|
-
}
|
|
17
|
+
export const selectParentNode: RawCommands['selectParentNode'] =
|
|
18
|
+
() =>
|
|
19
|
+
({ state, dispatch }) => {
|
|
20
|
+
return originalSelectParentNode(state, dispatch)
|
|
21
|
+
}
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// TODO: add types to @types/prosemirror-commands
|
|
3
3
|
import { selectTextblockEnd as originalSelectTextblockEnd } from '@tiptap/pm/commands'
|
|
4
4
|
|
|
5
|
-
import { RawCommands } from '../types.js'
|
|
5
|
+
import type { RawCommands } from '../types.js'
|
|
6
6
|
|
|
7
7
|
declare module '@tiptap/core' {
|
|
8
8
|
interface Commands<ReturnType> {
|
|
@@ -16,6 +16,8 @@ declare module '@tiptap/core' {
|
|
|
16
16
|
}
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
-
export const selectTextblockEnd: RawCommands['selectTextblockEnd'] =
|
|
20
|
-
|
|
21
|
-
}
|
|
19
|
+
export const selectTextblockEnd: RawCommands['selectTextblockEnd'] =
|
|
20
|
+
() =>
|
|
21
|
+
({ state, dispatch }) => {
|
|
22
|
+
return originalSelectTextblockEnd(state, dispatch)
|
|
23
|
+
}
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// TODO: add types to @types/prosemirror-commands
|
|
3
3
|
import { selectTextblockStart as originalSelectTextblockStart } from '@tiptap/pm/commands'
|
|
4
4
|
|
|
5
|
-
import { RawCommands } from '../types.js'
|
|
5
|
+
import type { RawCommands } from '../types.js'
|
|
6
6
|
|
|
7
7
|
declare module '@tiptap/core' {
|
|
8
8
|
interface Commands<ReturnType> {
|
|
@@ -16,6 +16,8 @@ declare module '@tiptap/core' {
|
|
|
16
16
|
}
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
-
export const selectTextblockStart: RawCommands['selectTextblockStart'] =
|
|
20
|
-
|
|
21
|
-
}
|
|
19
|
+
export const selectTextblockStart: RawCommands['selectTextblockStart'] =
|
|
20
|
+
() =>
|
|
21
|
+
({ state, dispatch }) => {
|
|
22
|
+
return originalSelectTextblockStart(state, dispatch)
|
|
23
|
+
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { ParseOptions } from '@tiptap/pm/model'
|
|
1
|
+
import type { Fragment, Node as ProseMirrorNode, ParseOptions } from '@tiptap/pm/model'
|
|
2
2
|
|
|
3
3
|
import { createDocument } from '../helpers/createDocument.js'
|
|
4
|
-
import { Content, RawCommands } from '../types.js'
|
|
4
|
+
import type { Content, RawCommands } from '../types.js'
|
|
5
5
|
|
|
6
6
|
declare module '@tiptap/core' {
|
|
7
7
|
interface Commands<ReturnType> {
|
|
@@ -17,61 +17,58 @@ declare module '@tiptap/core' {
|
|
|
17
17
|
/**
|
|
18
18
|
* The new content.
|
|
19
19
|
*/
|
|
20
|
-
content: Content,
|
|
20
|
+
content: Content | Fragment | ProseMirrorNode,
|
|
21
21
|
|
|
22
|
-
/**
|
|
23
|
-
* Whether to emit an update event.
|
|
24
|
-
* @default false
|
|
25
|
-
*/
|
|
26
|
-
emitUpdate?: boolean,
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* Options for parsing the content.
|
|
30
|
-
* @default {}
|
|
31
|
-
*/
|
|
32
|
-
parseOptions?: ParseOptions,
|
|
33
22
|
/**
|
|
34
23
|
* Options for `setContent`.
|
|
35
24
|
*/
|
|
36
25
|
options?: {
|
|
26
|
+
/**
|
|
27
|
+
* Options for parsing the content.
|
|
28
|
+
* @default {}
|
|
29
|
+
*/
|
|
30
|
+
parseOptions?: ParseOptions
|
|
31
|
+
|
|
37
32
|
/**
|
|
38
33
|
* Whether to throw an error if the content is invalid.
|
|
39
34
|
*/
|
|
40
|
-
|
|
35
|
+
errorOnInvalidContent?: boolean
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Whether to emit an update event.
|
|
39
|
+
* @default true
|
|
40
|
+
*/
|
|
41
|
+
emitUpdate?: boolean
|
|
41
42
|
},
|
|
42
43
|
) => ReturnType
|
|
43
44
|
}
|
|
44
45
|
}
|
|
45
46
|
}
|
|
46
47
|
|
|
47
|
-
export const setContent: RawCommands['setContent'] =
|
|
48
|
-
|
|
49
|
-
}) => {
|
|
50
|
-
|
|
48
|
+
export const setContent: RawCommands['setContent'] =
|
|
49
|
+
(content, { errorOnInvalidContent, emitUpdate = true, parseOptions = {} } = {}) =>
|
|
50
|
+
({ editor, tr, dispatch, commands }) => {
|
|
51
|
+
const { doc } = tr
|
|
51
52
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
53
|
+
// This is to keep backward compatibility with the previous behavior
|
|
54
|
+
// TODO remove this in the next major version
|
|
55
|
+
if (parseOptions.preserveWhitespace !== 'full') {
|
|
56
|
+
const document = createDocument(content, editor.schema, parseOptions, {
|
|
57
|
+
errorOnInvalidContent: errorOnInvalidContent ?? editor.options.enableContentCheck,
|
|
58
|
+
})
|
|
58
59
|
|
|
59
|
-
|
|
60
|
-
|
|
60
|
+
if (dispatch) {
|
|
61
|
+
tr.replaceWith(0, doc.content.size, document).setMeta('preventUpdate', !emitUpdate)
|
|
62
|
+
}
|
|
63
|
+
return true
|
|
61
64
|
}
|
|
62
|
-
return true
|
|
63
|
-
}
|
|
64
65
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
66
|
+
if (dispatch) {
|
|
67
|
+
tr.setMeta('preventUpdate', !emitUpdate)
|
|
68
|
+
}
|
|
68
69
|
|
|
69
|
-
|
|
70
|
-
{ from: 0, to: doc.content.size },
|
|
71
|
-
content,
|
|
72
|
-
{
|
|
70
|
+
return commands.insertContentAt({ from: 0, to: doc.content.size }, content, {
|
|
73
71
|
parseOptions,
|
|
74
|
-
errorOnInvalidContent:
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
}
|
|
72
|
+
errorOnInvalidContent: errorOnInvalidContent ?? editor.options.enableContentCheck,
|
|
73
|
+
})
|
|
74
|
+
}
|
package/src/commands/setMark.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { MarkType, ResolvedPos } from '@tiptap/pm/model'
|
|
2
|
-
import { EditorState, Transaction } from '@tiptap/pm/state'
|
|
1
|
+
import type { MarkType, ResolvedPos } from '@tiptap/pm/model'
|
|
2
|
+
import type { EditorState, Transaction } from '@tiptap/pm/state'
|
|
3
3
|
|
|
4
4
|
import { getMarkAttributes } from '../helpers/getMarkAttributes.js'
|
|
5
5
|
import { getMarkType } from '../helpers/getMarkType.js'
|
|
6
6
|
import { isTextSelection } from '../helpers/index.js'
|
|
7
|
-
import { RawCommands } from '../types.js'
|
|
7
|
+
import type { RawCommands } from '../types.js'
|
|
8
8
|
|
|
9
9
|
declare module '@tiptap/core' {
|
|
10
10
|
interface Commands<ReturnType> {
|
|
@@ -31,18 +31,14 @@ function canSetMark(state: EditorState, tr: Transaction, newMarkType: MarkType)
|
|
|
31
31
|
const currentMarks = state.storedMarks ?? cursor.marks()
|
|
32
32
|
|
|
33
33
|
// There can be no current marks that exclude the new mark
|
|
34
|
-
return (
|
|
35
|
-
!!newMarkType.isInSet(currentMarks)
|
|
36
|
-
|| !currentMarks.some(mark => mark.type.excludes(newMarkType))
|
|
37
|
-
)
|
|
34
|
+
return !!newMarkType.isInSet(currentMarks) || !currentMarks.some(mark => mark.type.excludes(newMarkType))
|
|
38
35
|
}
|
|
39
36
|
|
|
40
37
|
const { ranges } = selection
|
|
41
38
|
|
|
42
39
|
return ranges.some(({ $from, $to }) => {
|
|
43
|
-
let someNodeSupportsMark =
|
|
44
|
-
? state.doc.inlineContent && state.doc.type.allowsMarkType(newMarkType)
|
|
45
|
-
: false
|
|
40
|
+
let someNodeSupportsMark =
|
|
41
|
+
$from.depth === 0 ? state.doc.inlineContent && state.doc.type.allowsMarkType(newMarkType) : false
|
|
46
42
|
|
|
47
43
|
state.doc.nodesBetween($from.pos, $to.pos, (node, _pos, parent) => {
|
|
48
44
|
// If we already found a mark that we can enable, return false to bypass the remaining search
|
|
@@ -52,8 +48,8 @@ function canSetMark(state: EditorState, tr: Transaction, newMarkType: MarkType)
|
|
|
52
48
|
|
|
53
49
|
if (node.isInline) {
|
|
54
50
|
const parentAllowsMarkType = !parent || parent.type.allowsMarkType(newMarkType)
|
|
55
|
-
const currentMarksAllowMarkType =
|
|
56
|
-
|| !node.marks.some(otherMark => otherMark.type.excludes(newMarkType))
|
|
51
|
+
const currentMarksAllowMarkType =
|
|
52
|
+
!!newMarkType.isInSet(node.marks) || !node.marks.some(otherMark => otherMark.type.excludes(newMarkType))
|
|
57
53
|
|
|
58
54
|
someNodeSupportsMark = parentAllowsMarkType && currentMarksAllowMarkType
|
|
59
55
|
}
|
|
@@ -63,54 +59,56 @@ function canSetMark(state: EditorState, tr: Transaction, newMarkType: MarkType)
|
|
|
63
59
|
return someNodeSupportsMark
|
|
64
60
|
})
|
|
65
61
|
}
|
|
66
|
-
export const setMark: RawCommands['setMark'] =
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
62
|
+
export const setMark: RawCommands['setMark'] =
|
|
63
|
+
(typeOrName, attributes = {}) =>
|
|
64
|
+
({ tr, state, dispatch }) => {
|
|
65
|
+
const { selection } = tr
|
|
66
|
+
const { empty, ranges } = selection
|
|
67
|
+
const type = getMarkType(typeOrName, state.schema)
|
|
68
|
+
|
|
69
|
+
if (dispatch) {
|
|
70
|
+
if (empty) {
|
|
71
|
+
const oldAttributes = getMarkAttributes(state, type)
|
|
72
|
+
|
|
73
|
+
tr.addStoredMark(
|
|
74
|
+
type.create({
|
|
75
|
+
...oldAttributes,
|
|
76
|
+
...attributes,
|
|
77
|
+
}),
|
|
78
|
+
)
|
|
79
|
+
} else {
|
|
80
|
+
ranges.forEach(range => {
|
|
81
|
+
const from = range.$from.pos
|
|
82
|
+
const to = range.$to.pos
|
|
83
|
+
|
|
84
|
+
state.doc.nodesBetween(from, to, (node, pos) => {
|
|
85
|
+
const trimmedFrom = Math.max(pos, from)
|
|
86
|
+
const trimmedTo = Math.min(pos + node.nodeSize, to)
|
|
87
|
+
const someHasMark = node.marks.find(mark => mark.type === type)
|
|
88
|
+
|
|
89
|
+
// if there is already a mark of this type
|
|
90
|
+
// we know that we have to merge its attributes
|
|
91
|
+
// otherwise we add a fresh new mark
|
|
92
|
+
if (someHasMark) {
|
|
93
|
+
node.marks.forEach(mark => {
|
|
94
|
+
if (type === mark.type) {
|
|
95
|
+
tr.addMark(
|
|
96
|
+
trimmedFrom,
|
|
97
|
+
trimmedTo,
|
|
98
|
+
type.create({
|
|
99
|
+
...mark.attrs,
|
|
100
|
+
...attributes,
|
|
101
|
+
}),
|
|
102
|
+
)
|
|
103
|
+
}
|
|
104
|
+
})
|
|
105
|
+
} else {
|
|
106
|
+
tr.addMark(trimmedFrom, trimmedTo, type.create(attributes))
|
|
107
|
+
}
|
|
108
|
+
})
|
|
110
109
|
})
|
|
111
|
-
}
|
|
110
|
+
}
|
|
112
111
|
}
|
|
113
|
-
}
|
|
114
112
|
|
|
115
|
-
|
|
116
|
-
}
|
|
113
|
+
return canSetMark(state, tr, type)
|
|
114
|
+
}
|
package/src/commands/setMeta.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { Plugin, PluginKey } from '@tiptap/pm/state'
|
|
2
|
+
|
|
3
|
+
import type { RawCommands } from '../types.js'
|
|
2
4
|
|
|
3
5
|
declare module '@tiptap/core' {
|
|
4
6
|
interface Commands<ReturnType> {
|
|
@@ -9,13 +11,15 @@ declare module '@tiptap/core' {
|
|
|
9
11
|
* @param value The value to store.
|
|
10
12
|
* @example editor.commands.setMeta('foo', 'bar')
|
|
11
13
|
*/
|
|
12
|
-
setMeta: (key: string, value: any) => ReturnType
|
|
14
|
+
setMeta: (key: string | Plugin | PluginKey, value: any) => ReturnType
|
|
13
15
|
}
|
|
14
16
|
}
|
|
15
17
|
}
|
|
16
18
|
|
|
17
|
-
export const setMeta: RawCommands['setMeta'] =
|
|
18
|
-
|
|
19
|
+
export const setMeta: RawCommands['setMeta'] =
|
|
20
|
+
(key, value) =>
|
|
21
|
+
({ tr }) => {
|
|
22
|
+
tr.setMeta(key, value)
|
|
19
23
|
|
|
20
|
-
|
|
21
|
-
}
|
|
24
|
+
return true
|
|
25
|
+
}
|
package/src/commands/setNode.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { setBlockType } from '@tiptap/pm/commands'
|
|
2
|
-
import { NodeType } from '@tiptap/pm/model'
|
|
2
|
+
import type { NodeType } from '@tiptap/pm/model'
|
|
3
3
|
|
|
4
4
|
import { getNodeType } from '../helpers/getNodeType.js'
|
|
5
|
-
import { RawCommands } from '../types.js'
|
|
5
|
+
import type { RawCommands } from '../types.js'
|
|
6
6
|
|
|
7
7
|
declare module '@tiptap/core' {
|
|
8
8
|
interface Commands<ReturnType> {
|
|
@@ -18,31 +18,40 @@ declare module '@tiptap/core' {
|
|
|
18
18
|
}
|
|
19
19
|
}
|
|
20
20
|
|
|
21
|
-
export const setNode: RawCommands['setNode'] =
|
|
22
|
-
|
|
21
|
+
export const setNode: RawCommands['setNode'] =
|
|
22
|
+
(typeOrName, attributes = {}) =>
|
|
23
|
+
({ state, dispatch, chain }) => {
|
|
24
|
+
const type = getNodeType(typeOrName, state.schema)
|
|
23
25
|
|
|
24
|
-
|
|
25
|
-
if (!type.isTextblock) {
|
|
26
|
-
console.warn('[tiptap warn]: Currently "setNode()" only supports text block nodes.')
|
|
26
|
+
let attributesToCopy: Record<string, any> | undefined
|
|
27
27
|
|
|
28
|
-
|
|
29
|
-
|
|
28
|
+
if (state.selection.$anchor.sameParent(state.selection.$head)) {
|
|
29
|
+
// only copy attributes if the selection is pointing to a node of the same type
|
|
30
|
+
attributesToCopy = state.selection.$anchor.parent.attrs
|
|
31
|
+
}
|
|
30
32
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
33
|
+
// TODO: use a fallback like insertContent?
|
|
34
|
+
if (!type.isTextblock) {
|
|
35
|
+
console.warn('[tiptap warn]: Currently "setNode()" only supports text block nodes.')
|
|
36
|
+
|
|
37
|
+
return false
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return (
|
|
41
|
+
chain()
|
|
42
|
+
// try to convert node to default node if needed
|
|
43
|
+
.command(({ commands }) => {
|
|
44
|
+
const canSetBlock = setBlockType(type, { ...attributesToCopy, ...attributes })(state)
|
|
45
|
+
|
|
46
|
+
if (canSetBlock) {
|
|
47
|
+
return true
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return commands.clearNodes()
|
|
51
|
+
})
|
|
52
|
+
.command(({ state: updatedState }) => {
|
|
53
|
+
return setBlockType(type, { ...attributesToCopy, ...attributes })(updatedState, dispatch)
|
|
54
|
+
})
|
|
55
|
+
.run()
|
|
56
|
+
)
|
|
57
|
+
}
|