@tiptap/core 2.0.0-beta.18 → 2.0.0-beta.180
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 +1 -1
- package/README.md +2 -2
- package/dist/packages/core/src/CommandManager.d.ts +13 -6
- package/dist/packages/core/src/Editor.d.ts +24 -19
- package/dist/packages/core/src/EventEmitter.d.ts +8 -4
- package/dist/packages/core/src/Extension.d.ts +99 -27
- package/dist/packages/core/src/ExtensionManager.d.ts +7 -13
- package/dist/packages/core/src/InputRule.d.ts +42 -0
- package/dist/packages/core/src/Mark.d.ts +131 -31
- package/dist/packages/core/src/Node.d.ts +157 -33
- package/dist/packages/core/src/NodeView.d.ts +8 -12
- package/dist/packages/core/src/PasteRule.d.ts +42 -0
- package/dist/packages/core/src/Tracker.d.ts +11 -0
- package/dist/packages/core/src/commands/blur.d.ts +3 -3
- package/dist/packages/core/src/commands/clearContent.d.ts +3 -3
- package/dist/packages/core/src/commands/clearNodes.d.ts +3 -3
- package/dist/packages/core/src/commands/command.d.ts +2 -2
- package/dist/packages/core/src/commands/createParagraphNear.d.ts +3 -3
- package/dist/packages/core/src/commands/deleteNode.d.ts +13 -0
- package/dist/packages/core/src/commands/deleteRange.d.ts +3 -3
- package/dist/packages/core/src/commands/deleteSelection.d.ts +3 -3
- package/dist/packages/core/src/commands/enter.d.ts +3 -3
- package/dist/packages/core/src/commands/exitCode.d.ts +3 -3
- package/dist/packages/core/src/commands/extendMarkRange.d.ts +3 -3
- package/dist/packages/core/src/commands/first.d.ts +3 -3
- package/dist/packages/core/src/commands/focus.d.ts +5 -3
- package/dist/packages/core/src/commands/forEach.d.ts +14 -0
- package/dist/packages/core/src/commands/index.d.ts +50 -0
- package/dist/packages/core/src/commands/insertContent.d.ts +7 -3
- package/dist/packages/core/src/commands/insertContentAt.d.ts +16 -0
- package/dist/packages/core/src/commands/joinBackward.d.ts +3 -3
- package/dist/packages/core/src/commands/joinForward.d.ts +3 -3
- package/dist/packages/core/src/commands/keyboardShortcut.d.ts +3 -3
- package/dist/packages/core/src/commands/lift.d.ts +3 -3
- package/dist/packages/core/src/commands/liftEmptyBlock.d.ts +3 -3
- package/dist/packages/core/src/commands/liftListItem.d.ts +3 -3
- package/dist/packages/core/src/commands/newlineInCode.d.ts +3 -3
- package/dist/packages/core/src/commands/resetAttributes.d.ts +13 -0
- package/dist/packages/core/src/commands/scrollIntoView.d.ts +3 -3
- package/dist/packages/core/src/commands/selectAll.d.ts +3 -3
- package/dist/packages/core/src/commands/selectNodeBackward.d.ts +3 -3
- package/dist/packages/core/src/commands/selectNodeForward.d.ts +3 -3
- package/dist/packages/core/src/commands/selectParentNode.d.ts +3 -3
- package/dist/packages/core/src/commands/selectTextblockEnd.d.ts +12 -0
- package/dist/packages/core/src/commands/selectTextblockStart.d.ts +12 -0
- package/dist/packages/core/src/commands/setContent.d.ts +4 -3
- package/dist/packages/core/src/commands/setMark.d.ts +3 -3
- package/dist/packages/core/src/commands/setMeta.d.ts +12 -0
- package/dist/packages/core/src/commands/setNode.d.ts +3 -3
- package/dist/packages/core/src/commands/setNodeSelection.d.ts +12 -0
- package/dist/packages/core/src/commands/setTextSelection.d.ts +12 -0
- package/dist/packages/core/src/commands/sinkListItem.d.ts +3 -3
- package/dist/packages/core/src/commands/splitBlock.d.ts +3 -3
- package/dist/packages/core/src/commands/splitListItem.d.ts +3 -3
- package/dist/packages/core/src/commands/toggleList.d.ts +3 -3
- package/dist/packages/core/src/commands/toggleMark.d.ts +8 -3
- package/dist/packages/core/src/commands/toggleNode.d.ts +3 -3
- package/dist/packages/core/src/commands/toggleWrap.d.ts +3 -3
- package/dist/packages/core/src/commands/undoInputRule.d.ts +3 -3
- package/dist/packages/core/src/commands/unsetAllMarks.d.ts +3 -3
- package/dist/packages/core/src/commands/unsetMark.d.ts +8 -3
- package/dist/packages/core/src/commands/updateAttributes.d.ts +13 -0
- package/dist/packages/core/src/commands/wrapIn.d.ts +3 -3
- package/dist/packages/core/src/commands/wrapInList.d.ts +3 -3
- package/dist/packages/core/src/extensions/clipboardTextSerializer.d.ts +1 -1
- package/dist/packages/core/src/extensions/commands.d.ts +2 -95
- package/dist/packages/core/src/extensions/editable.d.ts +1 -1
- package/dist/packages/core/src/extensions/focusEvents.d.ts +1 -1
- package/dist/packages/core/src/extensions/index.d.ts +1 -0
- package/dist/packages/core/src/extensions/keymap.d.ts +1 -1
- package/dist/packages/core/src/extensions/tabindex.d.ts +2 -0
- package/dist/packages/core/src/helpers/combineTransactionSteps.d.ts +7 -0
- package/dist/packages/core/src/helpers/createChainableState.d.ts +5 -0
- package/dist/packages/core/src/helpers/createDocument.d.ts +3 -4
- package/dist/packages/core/src/helpers/createNodeFromContent.d.ts +4 -5
- package/dist/packages/core/src/helpers/defaultBlockAt.d.ts +2 -0
- package/dist/packages/core/src/helpers/findChildren.d.ts +3 -0
- package/dist/packages/core/src/helpers/findChildrenInRange.d.ts +6 -0
- package/dist/packages/core/src/helpers/findParentNode.d.ts +2 -3
- package/dist/packages/core/src/helpers/findParentNodeClosestToPos.d.ts +2 -2
- package/dist/packages/core/src/helpers/generateHTML.d.ts +2 -2
- package/dist/packages/core/src/helpers/generateJSON.d.ts +2 -0
- package/dist/packages/core/src/helpers/generateText.d.ts +5 -0
- package/dist/packages/core/src/helpers/getAttributes.d.ts +3 -0
- package/dist/packages/core/src/helpers/getAttributesFromExtensions.d.ts +2 -2
- package/dist/packages/core/src/helpers/getChangedRanges.d.ts +11 -0
- package/dist/packages/core/src/helpers/getDebugJSON.d.ts +8 -0
- package/dist/packages/core/src/helpers/getExtensionField.d.ts +2 -0
- package/dist/packages/core/src/helpers/getHTMLFromFragment.d.ts +2 -2
- package/dist/packages/core/src/helpers/getMarkAttributes.d.ts +2 -3
- package/dist/packages/core/src/helpers/getMarkRange.d.ts +1 -1
- package/dist/packages/core/src/helpers/getMarkType.d.ts +1 -1
- package/dist/packages/core/src/helpers/getMarksBetween.d.ts +2 -2
- package/dist/packages/core/src/helpers/getNodeAttributes.d.ts +2 -3
- package/dist/packages/core/src/helpers/getNodeType.d.ts +1 -1
- package/dist/packages/core/src/helpers/getRenderedAttributes.d.ts +3 -3
- package/dist/packages/core/src/helpers/getSchema.d.ts +1 -1
- package/dist/packages/core/src/helpers/getSchemaByResolvedExtensions.d.ts +3 -0
- package/dist/packages/core/src/helpers/getSchemaTypeByName.d.ts +1 -1
- package/dist/packages/core/src/helpers/getSchemaTypeNameByName.d.ts +1 -1
- package/dist/packages/core/src/helpers/getSplittedAttributes.d.ts +2 -2
- package/dist/packages/core/src/helpers/getText.d.ts +6 -0
- package/dist/packages/core/src/helpers/getTextBetween.d.ts +6 -0
- package/dist/packages/core/src/helpers/getTextContentFromNodes.d.ts +2 -0
- package/dist/packages/core/src/helpers/getTextSerializersFromSchema.d.ts +3 -0
- package/dist/packages/core/src/helpers/index.d.ts +33 -0
- package/dist/packages/core/src/helpers/injectExtensionAttributesToParseRule.d.ts +1 -1
- package/dist/packages/core/src/helpers/isActive.d.ts +1 -2
- package/dist/packages/core/src/helpers/isExtensionRulesEnabled.d.ts +2 -0
- package/dist/packages/core/src/helpers/isList.d.ts +1 -1
- package/dist/packages/core/src/helpers/isMarkActive.d.ts +2 -3
- package/dist/packages/core/src/helpers/isNodeActive.d.ts +2 -3
- package/dist/packages/core/src/helpers/isNodeEmpty.d.ts +1 -1
- package/dist/packages/core/src/helpers/isNodeSelection.d.ts +1 -1
- package/dist/packages/core/src/helpers/isTextSelection.d.ts +1 -1
- package/dist/packages/core/src/helpers/posToDOMRect.d.ts +2 -0
- package/dist/packages/core/src/helpers/resolveFocusPosition.d.ts +4 -0
- package/dist/packages/core/src/helpers/selectionToInsertionEnd.d.ts +1 -1
- package/dist/packages/core/src/helpers/splitExtensions.d.ts +6 -6
- package/dist/packages/core/src/index.d.ts +14 -21
- package/dist/packages/core/src/inputRules/index.d.ts +5 -0
- package/dist/packages/core/src/inputRules/markInputRule.d.ts +11 -2
- package/dist/packages/core/src/inputRules/nodeInputRule.d.ts +11 -2
- package/dist/packages/core/src/inputRules/textInputRule.d.ts +9 -0
- package/dist/packages/core/src/inputRules/textblockTypeInputRule.d.ts +14 -0
- package/dist/packages/core/src/inputRules/wrappingInputRule.d.ts +23 -0
- package/dist/packages/core/src/pasteRules/index.d.ts +2 -0
- package/dist/packages/core/src/pasteRules/markPasteRule.d.ts +11 -2
- package/dist/packages/core/src/pasteRules/textPasteRule.d.ts +9 -0
- package/dist/packages/core/src/style.d.ts +1 -2
- package/dist/packages/core/src/types.d.ts +112 -56
- package/dist/packages/core/src/utilities/callOrReturn.d.ts +2 -1
- package/dist/packages/core/src/utilities/createStyleTag.d.ts +1 -1
- package/dist/packages/core/src/utilities/deleteProps.d.ts +1 -2
- package/dist/packages/core/src/utilities/elementFromString.d.ts +1 -1
- package/dist/packages/core/src/utilities/escapeForRegEx.d.ts +1 -0
- package/dist/packages/core/src/utilities/findDuplicates.d.ts +1 -0
- package/dist/packages/core/src/utilities/fromString.d.ts +1 -1
- package/dist/packages/core/src/utilities/index.d.ts +3 -0
- package/dist/packages/core/src/utilities/isClass.d.ts +1 -1
- package/dist/packages/core/src/utilities/isEmptyObject.d.ts +1 -1
- package/dist/packages/core/src/utilities/isFunction.d.ts +1 -0
- package/dist/packages/core/src/utilities/isMacOS.d.ts +1 -0
- package/dist/packages/core/src/utilities/isNumber.d.ts +1 -0
- package/dist/packages/core/src/utilities/isObject.d.ts +1 -1
- package/dist/packages/core/src/utilities/isPlainObject.d.ts +1 -1
- package/dist/packages/core/src/utilities/isRegExp.d.ts +1 -0
- package/dist/packages/core/src/utilities/isiOS.d.ts +1 -0
- package/dist/packages/core/src/utilities/mergeAttributes.d.ts +1 -2
- package/dist/packages/core/src/utilities/mergeDeep.d.ts +1 -2
- package/dist/packages/core/src/utilities/minMax.d.ts +1 -1
- package/dist/packages/core/src/utilities/objectIncludes.d.ts +3 -2
- package/dist/packages/core/src/utilities/removeDuplicates.d.ts +8 -0
- package/dist/tiptap-core.cjs.js +2675 -1378
- package/dist/tiptap-core.cjs.js.map +1 -1
- package/dist/tiptap-core.esm.js +2605 -1341
- package/dist/tiptap-core.esm.js.map +1 -1
- package/dist/tiptap-core.umd.js +2622 -1324
- package/dist/tiptap-core.umd.js.map +1 -1
- package/package.json +17 -20
- package/src/CommandManager.ts +63 -64
- package/src/Editor.ts +115 -78
- package/src/EventEmitter.ts +14 -4
- package/src/Extension.ts +193 -43
- package/src/ExtensionManager.ts +232 -83
- package/src/InputRule.ts +265 -0
- package/src/Mark.ts +239 -49
- package/src/Node.ts +279 -57
- package/src/NodeView.ts +115 -47
- package/src/PasteRule.ts +247 -0
- package/src/Tracker.ts +42 -0
- package/src/commands/blur.ts +12 -6
- package/src/commands/clearContent.ts +3 -3
- package/src/commands/clearNodes.ts +30 -18
- package/src/commands/command.ts +2 -2
- package/src/commands/createParagraphNear.ts +4 -3
- package/src/commands/deleteNode.ts +37 -0
- package/src/commands/deleteRange.ts +3 -3
- package/src/commands/deleteSelection.ts +4 -3
- package/src/commands/enter.ts +3 -3
- package/src/commands/exitCode.ts +4 -3
- package/src/commands/extendMarkRange.ts +12 -11
- package/src/commands/first.ts +3 -3
- package/src/commands/focus.ts +45 -44
- package/src/commands/forEach.ts +24 -0
- package/src/commands/index.ts +50 -0
- package/src/commands/insertContent.ts +13 -24
- package/src/commands/insertContentAt.ts +108 -0
- package/src/commands/joinBackward.ts +4 -3
- package/src/commands/joinForward.ts +4 -3
- package/src/commands/keyboardShortcut.ts +6 -6
- package/src/commands/lift.ts +6 -5
- package/src/commands/liftEmptyBlock.ts +4 -3
- package/src/commands/liftListItem.ts +6 -5
- package/src/commands/newlineInCode.ts +4 -3
- package/src/commands/resetAttributes.ts +62 -0
- package/src/commands/scrollIntoView.ts +3 -3
- package/src/commands/selectAll.ts +8 -6
- package/src/commands/selectNodeBackward.ts +4 -3
- package/src/commands/selectNodeForward.ts +4 -3
- package/src/commands/selectParentNode.ts +4 -3
- package/src/commands/selectTextblockEnd.ts +20 -0
- package/src/commands/selectTextblockStart.ts +20 -0
- package/src/commands/setContent.ts +8 -11
- package/src/commands/setMark.ts +36 -12
- package/src/commands/setMeta.ts +18 -0
- package/src/commands/setNode.ts +29 -7
- package/src/commands/setNodeSelection.ts +29 -0
- package/src/commands/setTextSelection.ts +33 -0
- package/src/commands/sinkListItem.ts +6 -5
- package/src/commands/splitBlock.ts +16 -10
- package/src/commands/splitListItem.ts +45 -18
- package/src/commands/toggleList.ts +83 -18
- package/src/commands/toggleMark.ts +18 -7
- package/src/commands/toggleNode.ts +6 -5
- package/src/commands/toggleWrap.ts +9 -9
- package/src/commands/undoInputRule.ts +34 -5
- package/src/commands/unsetAllMarks.ts +7 -11
- package/src/commands/unsetMark.ts +35 -22
- package/src/commands/updateAttributes.ts +73 -0
- package/src/commands/wrapIn.ts +5 -10
- package/src/commands/wrapInList.ts +6 -5
- package/src/extensions/clipboardTextSerializer.ts +14 -35
- package/src/extensions/commands.ts +3 -141
- package/src/extensions/editable.ts +1 -0
- package/src/extensions/focusEvents.ts +1 -3
- package/src/extensions/index.ts +1 -0
- package/src/extensions/keymap.ts +110 -13
- package/src/extensions/tabindex.ts +18 -0
- package/src/helpers/combineTransactionSteps.ts +18 -0
- package/src/helpers/createChainableState.ts +38 -0
- package/src/helpers/createDocument.ts +5 -6
- package/src/helpers/createNodeFromContent.ts +17 -19
- package/src/helpers/defaultBlockAt.ts +13 -0
- package/src/helpers/findChildren.ts +18 -0
- package/src/helpers/findChildrenInRange.ts +32 -0
- package/src/helpers/findParentNode.ts +3 -2
- package/src/helpers/findParentNodeClosestToPos.ts +3 -2
- package/src/helpers/generateHTML.ts +6 -5
- package/src/helpers/generateJSON.ts +14 -0
- package/src/helpers/generateText.ts +30 -0
- package/src/helpers/getAttributes.ts +28 -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 +6 -5
- package/src/helpers/getMarkAttributes.ts +14 -10
- package/src/helpers/getMarkRange.ts +41 -8
- package/src/helpers/getMarkType.ts +5 -1
- package/src/helpers/getMarksBetween.ts +32 -10
- package/src/helpers/getNodeAttributes.ts +9 -9
- package/src/helpers/getNodeType.ts +5 -1
- package/src/helpers/getRenderedAttributes.ts +6 -7
- package/src/helpers/getSchema.ts +7 -133
- package/src/helpers/getSchemaByResolvedExtensions.ts +148 -0
- package/src/helpers/getSchemaTypeByName.ts +2 -10
- package/src/helpers/getSchemaTypeNameByName.ts +1 -1
- package/src/helpers/getSplittedAttributes.ts +4 -4
- package/src/helpers/getText.ts +19 -0
- package/src/helpers/getTextBetween.ts +49 -0
- package/src/helpers/getTextContentFromNodes.ts +17 -0
- package/src/helpers/getTextSerializersFromSchema.ts +10 -0
- package/src/helpers/index.ts +33 -0
- package/src/helpers/injectExtensionAttributesToParseRule.ts +17 -19
- package/src/helpers/isActive.ts +5 -5
- package/src/helpers/isExtensionRulesEnabled.ts +15 -0
- package/src/helpers/isList.ts +14 -7
- package/src/helpers/isMarkActive.ts +45 -20
- package/src/helpers/isNodeActive.ts +28 -35
- package/src/helpers/isNodeEmpty.ts +1 -1
- package/src/helpers/isNodeSelection.ts +3 -2
- package/src/helpers/isTextSelection.ts +3 -2
- package/src/helpers/posToDOMRect.ts +35 -0
- package/src/helpers/resolveFocusPosition.ts +43 -0
- package/src/helpers/selectionToInsertionEnd.ts +2 -2
- 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 -39
- package/src/inputRules/nodeInputRule.ts +45 -11
- package/src/inputRules/textInputRule.ts +35 -0
- package/src/inputRules/textblockTypeInputRule.ts +38 -0
- package/src/inputRules/wrappingInputRule.ts +60 -0
- package/src/pasteRules/index.ts +2 -0
- package/src/pasteRules/markPasteRule.ts +62 -53
- package/src/pasteRules/textPasteRule.ts +35 -0
- package/src/style.ts +16 -3
- package/src/types.ts +129 -43
- 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 +3 -0
- package/src/utilities/isClass.ts +2 -2
- 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/isObject.ts +6 -6
- 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 -335
- 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/replace.d.ts +0 -13
- package/dist/packages/core/src/commands/replaceRange.d.ts +0 -13
- package/dist/packages/core/src/commands/resetNodeAttributes.d.ts +0 -13
- package/dist/packages/core/src/commands/updateNodeAttributes.d.ts +0 -13
- 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/src/commands/insertHTML.ts +0 -30
- package/src/commands/insertNode.ts +0 -33
- package/src/commands/insertText.ts +0 -22
- package/src/commands/replace.ts +0 -20
- package/src/commands/replaceRange.ts +0 -36
- package/src/commands/resetNodeAttributes.ts +0 -31
- package/src/commands/updateNodeAttributes.ts +0 -33
- package/src/utilities/removeElement.ts +0 -5
|
@@ -1,15 +1,16 @@
|
|
|
1
|
-
import { EditorState } from 'prosemirror-state'
|
|
2
1
|
import { MarkType } from 'prosemirror-model'
|
|
3
|
-
import
|
|
4
|
-
import getMarkType from './getMarkType'
|
|
5
|
-
import { AnyObject, MarkRange } from '../types'
|
|
2
|
+
import { EditorState } from 'prosemirror-state'
|
|
6
3
|
|
|
7
|
-
|
|
4
|
+
import { MarkRange } from '../types'
|
|
5
|
+
import { objectIncludes } from '../utilities/objectIncludes'
|
|
6
|
+
import { getMarkType } from './getMarkType'
|
|
7
|
+
|
|
8
|
+
export function isMarkActive(
|
|
8
9
|
state: EditorState,
|
|
9
10
|
typeOrName: MarkType | string | null,
|
|
10
|
-
attributes:
|
|
11
|
+
attributes: Record<string, any> = {},
|
|
11
12
|
): boolean {
|
|
12
|
-
const {
|
|
13
|
+
const { empty, ranges } = state.selection
|
|
13
14
|
const type = typeOrName
|
|
14
15
|
? getMarkType(typeOrName, state.schema)
|
|
15
16
|
: null
|
|
@@ -23,33 +24,41 @@ export default function isMarkActive(
|
|
|
23
24
|
|
|
24
25
|
return type.name === mark.type.name
|
|
25
26
|
})
|
|
26
|
-
.find(mark => objectIncludes(mark.attrs, attributes))
|
|
27
|
+
.find(mark => objectIncludes(mark.attrs, attributes, { strict: false }))
|
|
27
28
|
}
|
|
28
29
|
|
|
29
30
|
let selectionRange = 0
|
|
30
|
-
|
|
31
|
+
const markRanges: MarkRange[] = []
|
|
32
|
+
|
|
33
|
+
ranges.forEach(({ $from, $to }) => {
|
|
34
|
+
const from = $from.pos
|
|
35
|
+
const to = $to.pos
|
|
36
|
+
|
|
37
|
+
state.doc.nodesBetween(from, to, (node, pos) => {
|
|
38
|
+
if (!node.isText && !node.marks.length) {
|
|
39
|
+
return
|
|
40
|
+
}
|
|
31
41
|
|
|
32
|
-
state.doc.nodesBetween(from, to, (node, pos) => {
|
|
33
|
-
if (node.isText) {
|
|
34
42
|
const relativeFrom = Math.max(from, pos)
|
|
35
43
|
const relativeTo = Math.min(to, pos + node.nodeSize)
|
|
36
44
|
const range = relativeTo - relativeFrom
|
|
37
45
|
|
|
38
46
|
selectionRange += range
|
|
39
47
|
|
|
40
|
-
markRanges
|
|
48
|
+
markRanges.push(...node.marks.map(mark => ({
|
|
41
49
|
mark,
|
|
42
50
|
from: relativeFrom,
|
|
43
51
|
to: relativeTo,
|
|
44
|
-
}))
|
|
45
|
-
}
|
|
52
|
+
})))
|
|
53
|
+
})
|
|
46
54
|
})
|
|
47
55
|
|
|
48
56
|
if (selectionRange === 0) {
|
|
49
57
|
return false
|
|
50
58
|
}
|
|
51
59
|
|
|
52
|
-
|
|
60
|
+
// calculate range of matched mark
|
|
61
|
+
const matchedRange = markRanges
|
|
53
62
|
.filter(markRange => {
|
|
54
63
|
if (!type) {
|
|
55
64
|
return true
|
|
@@ -57,11 +66,27 @@ export default function isMarkActive(
|
|
|
57
66
|
|
|
58
67
|
return type.name === markRange.mark.type.name
|
|
59
68
|
})
|
|
60
|
-
.filter(markRange => objectIncludes(markRange.mark.attrs, attributes))
|
|
61
|
-
.reduce((sum, markRange) =>
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
69
|
+
.filter(markRange => objectIncludes(markRange.mark.attrs, attributes, { strict: false }))
|
|
70
|
+
.reduce((sum, markRange) => sum + markRange.to - markRange.from, 0)
|
|
71
|
+
|
|
72
|
+
// calculate range of marks that excludes the searched mark
|
|
73
|
+
// for example `code` doesn’t allow any other marks
|
|
74
|
+
const excludedRange = markRanges
|
|
75
|
+
.filter(markRange => {
|
|
76
|
+
if (!type) {
|
|
77
|
+
return true
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
return markRange.mark.type !== type
|
|
81
|
+
&& markRange.mark.type.excludes(type)
|
|
82
|
+
})
|
|
83
|
+
.reduce((sum, markRange) => sum + markRange.to - markRange.from, 0)
|
|
84
|
+
|
|
85
|
+
// we only include the result of `excludedRange`
|
|
86
|
+
// if there is a match at all
|
|
87
|
+
const range = matchedRange > 0
|
|
88
|
+
? matchedRange + excludedRange
|
|
89
|
+
: matchedRange
|
|
65
90
|
|
|
66
91
|
return range >= selectionRange
|
|
67
92
|
}
|
|
@@ -1,49 +1,39 @@
|
|
|
1
|
-
import { EditorState } from 'prosemirror-state'
|
|
2
1
|
import { NodeType } from 'prosemirror-model'
|
|
3
|
-
import
|
|
4
|
-
import getNodeType from './getNodeType'
|
|
5
|
-
import { AnyObject, NodeRange } from '../types'
|
|
2
|
+
import { EditorState } from 'prosemirror-state'
|
|
6
3
|
|
|
7
|
-
|
|
4
|
+
import { NodeRange } from '../types'
|
|
5
|
+
import { objectIncludes } from '../utilities/objectIncludes'
|
|
6
|
+
import { getNodeType } from './getNodeType'
|
|
7
|
+
|
|
8
|
+
export function isNodeActive(
|
|
8
9
|
state: EditorState,
|
|
9
10
|
typeOrName: NodeType | string | null,
|
|
10
|
-
attributes:
|
|
11
|
+
attributes: Record<string, any> = {},
|
|
11
12
|
): boolean {
|
|
12
13
|
const { from, to, empty } = state.selection
|
|
13
14
|
const type = typeOrName
|
|
14
15
|
? getNodeType(typeOrName, state.schema)
|
|
15
16
|
: null
|
|
16
17
|
|
|
17
|
-
|
|
18
|
+
const nodeRanges: NodeRange[] = []
|
|
18
19
|
|
|
19
20
|
state.doc.nodesBetween(from, to, (node, pos) => {
|
|
20
|
-
if (
|
|
21
|
-
|
|
22
|
-
const relativeTo = Math.min(to, pos + node.nodeSize)
|
|
23
|
-
|
|
24
|
-
nodeRanges = [...nodeRanges, {
|
|
25
|
-
node,
|
|
26
|
-
from: relativeFrom,
|
|
27
|
-
to: relativeTo,
|
|
28
|
-
}]
|
|
21
|
+
if (node.isText) {
|
|
22
|
+
return
|
|
29
23
|
}
|
|
30
|
-
})
|
|
31
24
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
.filter(nodeRange => {
|
|
35
|
-
if (!type) {
|
|
36
|
-
return true
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
return type.name === nodeRange.node.type.name
|
|
40
|
-
})
|
|
41
|
-
.find(nodeRange => objectIncludes(nodeRange.node.attrs, attributes))
|
|
42
|
-
}
|
|
25
|
+
const relativeFrom = Math.max(from, pos)
|
|
26
|
+
const relativeTo = Math.min(to, pos + node.nodeSize)
|
|
43
27
|
|
|
44
|
-
|
|
28
|
+
nodeRanges.push({
|
|
29
|
+
node,
|
|
30
|
+
from: relativeFrom,
|
|
31
|
+
to: relativeTo,
|
|
32
|
+
})
|
|
33
|
+
})
|
|
45
34
|
|
|
46
|
-
const
|
|
35
|
+
const selectionRange = to - from
|
|
36
|
+
const matchedNodeRanges = nodeRanges
|
|
47
37
|
.filter(nodeRange => {
|
|
48
38
|
if (!type) {
|
|
49
39
|
return true
|
|
@@ -51,11 +41,14 @@ export default function isNodeActive(
|
|
|
51
41
|
|
|
52
42
|
return type.name === nodeRange.node.type.name
|
|
53
43
|
})
|
|
54
|
-
.filter(nodeRange => objectIncludes(nodeRange.node.attrs, attributes))
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
44
|
+
.filter(nodeRange => objectIncludes(nodeRange.node.attrs, attributes, { strict: false }))
|
|
45
|
+
|
|
46
|
+
if (empty) {
|
|
47
|
+
return !!matchedNodeRanges.length
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const range = matchedNodeRanges
|
|
51
|
+
.reduce((sum, nodeRange) => sum + nodeRange.to - nodeRange.from, 0)
|
|
59
52
|
|
|
60
53
|
return range >= selectionRange
|
|
61
54
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Node as ProseMirrorNode } from 'prosemirror-model'
|
|
2
2
|
|
|
3
|
-
export
|
|
3
|
+
export function isNodeEmpty(node: ProseMirrorNode): boolean {
|
|
4
4
|
const defaultContent = node.type.createAndFill()?.toJSON()
|
|
5
5
|
const content = node.toJSON()
|
|
6
6
|
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { NodeSelection } from 'prosemirror-state'
|
|
2
|
-
import isObject from '../utilities/isObject'
|
|
3
2
|
|
|
4
|
-
|
|
3
|
+
import { isObject } from '../utilities/isObject'
|
|
4
|
+
|
|
5
|
+
export function isNodeSelection(value: unknown): value is NodeSelection {
|
|
5
6
|
return isObject(value) && value instanceof NodeSelection
|
|
6
7
|
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { TextSelection } from 'prosemirror-state'
|
|
2
|
-
import isObject from '../utilities/isObject'
|
|
3
2
|
|
|
4
|
-
|
|
3
|
+
import { isObject } from '../utilities/isObject'
|
|
4
|
+
|
|
5
|
+
export function isTextSelection(value: unknown): value is TextSelection {
|
|
5
6
|
return isObject(value) && value instanceof TextSelection
|
|
6
7
|
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { EditorView } from 'prosemirror-view'
|
|
2
|
+
|
|
3
|
+
import { minMax } from '../utilities/minMax'
|
|
4
|
+
|
|
5
|
+
export function posToDOMRect(view: EditorView, from: number, to: number): DOMRect {
|
|
6
|
+
const minPos = 0
|
|
7
|
+
const maxPos = view.state.doc.content.size
|
|
8
|
+
const resolvedFrom = minMax(from, minPos, maxPos)
|
|
9
|
+
const resolvedEnd = minMax(to, minPos, maxPos)
|
|
10
|
+
const start = view.coordsAtPos(resolvedFrom)
|
|
11
|
+
const end = view.coordsAtPos(resolvedEnd, -1)
|
|
12
|
+
const top = Math.min(start.top, end.top)
|
|
13
|
+
const bottom = Math.max(start.bottom, end.bottom)
|
|
14
|
+
const left = Math.min(start.left, end.left)
|
|
15
|
+
const right = Math.max(start.right, end.right)
|
|
16
|
+
const width = right - left
|
|
17
|
+
const height = bottom - top
|
|
18
|
+
const x = left
|
|
19
|
+
const y = top
|
|
20
|
+
const data = {
|
|
21
|
+
top,
|
|
22
|
+
bottom,
|
|
23
|
+
left,
|
|
24
|
+
right,
|
|
25
|
+
width,
|
|
26
|
+
height,
|
|
27
|
+
x,
|
|
28
|
+
y,
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return {
|
|
32
|
+
...data,
|
|
33
|
+
toJSON: () => data,
|
|
34
|
+
}
|
|
35
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { Node as ProseMirrorNode } from 'prosemirror-model'
|
|
2
|
+
import { Selection, TextSelection } from 'prosemirror-state'
|
|
3
|
+
|
|
4
|
+
import { FocusPosition } from '../types'
|
|
5
|
+
import { minMax } from '../utilities/minMax'
|
|
6
|
+
|
|
7
|
+
export function resolveFocusPosition(
|
|
8
|
+
doc: ProseMirrorNode,
|
|
9
|
+
position: FocusPosition = null,
|
|
10
|
+
): Selection | null {
|
|
11
|
+
|
|
12
|
+
if (!position) {
|
|
13
|
+
return null
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const selectionAtStart = Selection.atStart(doc)
|
|
17
|
+
const selectionAtEnd = Selection.atEnd(doc)
|
|
18
|
+
|
|
19
|
+
if (position === 'start' || position === true) {
|
|
20
|
+
return selectionAtStart
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
if (position === 'end') {
|
|
24
|
+
return selectionAtEnd
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const minPos = selectionAtStart.from
|
|
28
|
+
const maxPos = selectionAtEnd.to
|
|
29
|
+
|
|
30
|
+
if (position === 'all') {
|
|
31
|
+
return TextSelection.create(
|
|
32
|
+
doc,
|
|
33
|
+
minMax(0, minPos, maxPos),
|
|
34
|
+
minMax(doc.content.size, minPos, maxPos),
|
|
35
|
+
)
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return TextSelection.create(
|
|
39
|
+
doc,
|
|
40
|
+
minMax(position, minPos, maxPos),
|
|
41
|
+
minMax(position, minPos, maxPos),
|
|
42
|
+
)
|
|
43
|
+
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { Selection, Transaction } from 'prosemirror-state'
|
|
2
|
-
import {
|
|
2
|
+
import { ReplaceAroundStep, ReplaceStep } from 'prosemirror-transform'
|
|
3
3
|
|
|
4
4
|
// source: https://github.com/ProseMirror/prosemirror-state/blob/master/src/selection.js#L466
|
|
5
|
-
export
|
|
5
|
+
export function selectionToInsertionEnd(tr: Transaction, startLen: number, bias: number) {
|
|
6
6
|
const last = tr.steps.length - 1
|
|
7
7
|
|
|
8
8
|
if (last < startLen) {
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { Extensions } from '../types'
|
|
2
1
|
import { Extension } from '../Extension'
|
|
3
|
-
import { Node } from '../Node'
|
|
4
2
|
import { Mark } from '../Mark'
|
|
3
|
+
import { Node } from '../Node'
|
|
4
|
+
import { Extensions } from '../types'
|
|
5
5
|
|
|
6
|
-
export
|
|
6
|
+
export function splitExtensions(extensions: Extensions) {
|
|
7
7
|
const baseExtensions = extensions.filter(extension => extension.type === 'extension') as Extension[]
|
|
8
8
|
const nodeExtensions = extensions.filter(extension => extension.type === 'node') as Node[]
|
|
9
9
|
const markExtensions = extensions.filter(extension => extension.type === 'mark') as Mark[]
|
package/src/index.ts
CHANGED
|
@@ -1,36 +1,27 @@
|
|
|
1
|
+
export * from './CommandManager'
|
|
1
2
|
export * from './Editor'
|
|
2
3
|
export * from './Extension'
|
|
3
|
-
export * from './
|
|
4
|
+
export * as extensions from './extensions'
|
|
5
|
+
export * from './helpers'
|
|
6
|
+
export * from './InputRule'
|
|
7
|
+
export * from './inputRules'
|
|
4
8
|
export * from './Mark'
|
|
9
|
+
export * from './Node'
|
|
5
10
|
export * from './NodeView'
|
|
11
|
+
export * from './PasteRule'
|
|
12
|
+
export * from './pasteRules'
|
|
13
|
+
export * from './Tracker'
|
|
6
14
|
export * from './types'
|
|
15
|
+
export * from './utilities'
|
|
7
16
|
|
|
8
|
-
|
|
9
|
-
export
|
|
10
|
-
export { default as markPasteRule } from './pasteRules/markPasteRule'
|
|
11
|
-
|
|
12
|
-
export { default as callOrReturn } from './utilities/callOrReturn'
|
|
13
|
-
export { default as mergeAttributes } from './utilities/mergeAttributes'
|
|
14
|
-
|
|
15
|
-
export { default as generateHTML } from './helpers/generateHTML'
|
|
16
|
-
export { default as getSchema } from './helpers/getSchema'
|
|
17
|
-
export { default as getHTMLFromFragment } from './helpers/getHTMLFromFragment'
|
|
18
|
-
export { default as getMarkAttributes } from './helpers/getMarkAttributes'
|
|
19
|
-
export { default as isActive } from './helpers/isActive'
|
|
20
|
-
export { default as isMarkActive } from './helpers/isMarkActive'
|
|
21
|
-
export { default as isNodeActive } from './helpers/isNodeActive'
|
|
22
|
-
export { default as isNodeEmpty } from './helpers/isNodeEmpty'
|
|
23
|
-
export { default as isNodeSelection } from './helpers/isNodeSelection'
|
|
24
|
-
export { default as isTextSelection } from './helpers/isTextSelection'
|
|
25
|
-
export { default as findParentNodeClosestToPos } from './helpers/findParentNodeClosestToPos'
|
|
26
|
-
|
|
27
|
-
export interface Commands {}
|
|
17
|
+
// eslint-disable-next-line
|
|
18
|
+
export interface Commands<ReturnType = any> {}
|
|
28
19
|
|
|
29
20
|
// eslint-disable-next-line
|
|
30
|
-
export interface ExtensionConfig<Options = any> {}
|
|
21
|
+
export interface ExtensionConfig<Options = any, Storage = any> {}
|
|
31
22
|
|
|
32
23
|
// eslint-disable-next-line
|
|
33
|
-
export interface NodeConfig<Options = any> {}
|
|
24
|
+
export interface NodeConfig<Options = any, Storage = any> {}
|
|
34
25
|
|
|
35
26
|
// eslint-disable-next-line
|
|
36
|
-
export interface MarkConfig<Options = any> {}
|
|
27
|
+
export interface MarkConfig<Options = any, Storage = any> {}
|
|
@@ -1,50 +1,70 @@
|
|
|
1
|
-
import { InputRule } from 'prosemirror-inputrules'
|
|
2
1
|
import { MarkType } from 'prosemirror-model'
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
if (excludedMarks.length) {
|
|
2
|
+
|
|
3
|
+
import { getMarksBetween } from '../helpers/getMarksBetween'
|
|
4
|
+
import { InputRule, InputRuleFinder } from '../InputRule'
|
|
5
|
+
import { ExtendedRegExpMatchArray } from '../types'
|
|
6
|
+
import { callOrReturn } from '../utilities/callOrReturn'
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Build an input rule that adds a mark when the
|
|
10
|
+
* matched text is typed into it.
|
|
11
|
+
*/
|
|
12
|
+
export function markInputRule(config: {
|
|
13
|
+
find: InputRuleFinder,
|
|
14
|
+
type: MarkType,
|
|
15
|
+
getAttributes?:
|
|
16
|
+
| Record<string, any>
|
|
17
|
+
| ((match: ExtendedRegExpMatchArray) => Record<string, any>)
|
|
18
|
+
| false
|
|
19
|
+
| null
|
|
20
|
+
,
|
|
21
|
+
}) {
|
|
22
|
+
return new InputRule({
|
|
23
|
+
find: config.find,
|
|
24
|
+
handler: ({ state, range, match }) => {
|
|
25
|
+
const attributes = callOrReturn(config.getAttributes, undefined, match)
|
|
26
|
+
|
|
27
|
+
if (attributes === false || attributes === null) {
|
|
30
28
|
return null
|
|
31
29
|
}
|
|
32
30
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
31
|
+
const { tr } = state
|
|
32
|
+
const captureGroup = match[match.length - 1]
|
|
33
|
+
const fullMatch = match[0]
|
|
34
|
+
let markEnd = range.to
|
|
36
35
|
|
|
37
|
-
if (
|
|
38
|
-
|
|
39
|
-
|
|
36
|
+
if (captureGroup) {
|
|
37
|
+
const startSpaces = fullMatch.search(/\S/)
|
|
38
|
+
const textStart = range.from + fullMatch.indexOf(captureGroup)
|
|
39
|
+
const textEnd = textStart + captureGroup.length
|
|
40
40
|
|
|
41
|
-
|
|
41
|
+
const excludedMarks = getMarksBetween(range.from, range.to, state.doc)
|
|
42
|
+
.filter(item => {
|
|
43
|
+
// @ts-ignore
|
|
44
|
+
const excluded = item.mark.type.excluded as MarkType[]
|
|
42
45
|
|
|
43
|
-
|
|
46
|
+
return excluded.find(type => type === config.type && type !== item.mark.type)
|
|
47
|
+
})
|
|
48
|
+
.filter(item => item.to > textStart)
|
|
44
49
|
|
|
45
|
-
|
|
46
|
-
|
|
50
|
+
if (excludedMarks.length) {
|
|
51
|
+
return null
|
|
52
|
+
}
|
|
47
53
|
|
|
48
|
-
|
|
54
|
+
if (textEnd < range.to) {
|
|
55
|
+
tr.delete(textEnd, range.to)
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
if (textStart > range.from) {
|
|
59
|
+
tr.delete(range.from + startSpaces, textStart)
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
markEnd = range.from + startSpaces + captureGroup.length
|
|
63
|
+
|
|
64
|
+
tr.addMark(range.from + startSpaces, markEnd, config.type.create(attributes || {}))
|
|
65
|
+
|
|
66
|
+
tr.removeStoredMark(config.type)
|
|
67
|
+
}
|
|
68
|
+
},
|
|
49
69
|
})
|
|
50
70
|
}
|
|
@@ -1,17 +1,51 @@
|
|
|
1
|
-
import { InputRule } from 'prosemirror-inputrules'
|
|
2
1
|
import { NodeType } from 'prosemirror-model'
|
|
3
2
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
? getAttributes(match)
|
|
8
|
-
: getAttributes
|
|
9
|
-
const { tr } = state
|
|
3
|
+
import { InputRule, InputRuleFinder } from '../InputRule'
|
|
4
|
+
import { ExtendedRegExpMatchArray } from '../types'
|
|
5
|
+
import { callOrReturn } from '../utilities/callOrReturn'
|
|
10
6
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
7
|
+
/**
|
|
8
|
+
* Build an input rule that adds a node when the
|
|
9
|
+
* matched text is typed into it.
|
|
10
|
+
*/
|
|
11
|
+
export function nodeInputRule(config: {
|
|
12
|
+
find: InputRuleFinder,
|
|
13
|
+
type: NodeType,
|
|
14
|
+
getAttributes?:
|
|
15
|
+
| Record<string, any>
|
|
16
|
+
| ((match: ExtendedRegExpMatchArray) => Record<string, any>)
|
|
17
|
+
| false
|
|
18
|
+
| null
|
|
19
|
+
,
|
|
20
|
+
}) {
|
|
21
|
+
return new InputRule({
|
|
22
|
+
find: config.find,
|
|
23
|
+
handler: ({ state, range, match }) => {
|
|
24
|
+
const attributes = callOrReturn(config.getAttributes, undefined, match) || {}
|
|
25
|
+
const { tr } = state
|
|
26
|
+
const start = range.from
|
|
27
|
+
let end = range.to
|
|
14
28
|
|
|
15
|
-
|
|
29
|
+
if (match[1]) {
|
|
30
|
+
const offset = match[0].lastIndexOf(match[1])
|
|
31
|
+
let matchStart = start + offset
|
|
32
|
+
|
|
33
|
+
if (matchStart > end) {
|
|
34
|
+
matchStart = end
|
|
35
|
+
} else {
|
|
36
|
+
end = matchStart + match[1].length
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// insert last typed character
|
|
40
|
+
const lastChar = match[0][match[0].length - 1]
|
|
41
|
+
|
|
42
|
+
tr.insertText(lastChar, start + match[0].length - 1)
|
|
43
|
+
|
|
44
|
+
// insert node from input rule
|
|
45
|
+
tr.replaceWith(matchStart, end, config.type.create(attributes))
|
|
46
|
+
} else if (match[0]) {
|
|
47
|
+
tr.replaceWith(start, end, config.type.create(attributes))
|
|
48
|
+
}
|
|
49
|
+
},
|
|
16
50
|
})
|
|
17
51
|
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { InputRule, InputRuleFinder } from '../InputRule'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Build an input rule that replaces text when the
|
|
5
|
+
* matched text is typed into it.
|
|
6
|
+
*/
|
|
7
|
+
export function textInputRule(config: {
|
|
8
|
+
find: InputRuleFinder,
|
|
9
|
+
replace: string,
|
|
10
|
+
}) {
|
|
11
|
+
return new InputRule({
|
|
12
|
+
find: config.find,
|
|
13
|
+
handler: ({ state, range, match }) => {
|
|
14
|
+
let insert = config.replace
|
|
15
|
+
let start = range.from
|
|
16
|
+
const end = range.to
|
|
17
|
+
|
|
18
|
+
if (match[1]) {
|
|
19
|
+
const offset = match[0].lastIndexOf(match[1])
|
|
20
|
+
|
|
21
|
+
insert += match[0].slice(offset + match[1].length)
|
|
22
|
+
start += offset
|
|
23
|
+
|
|
24
|
+
const cutOff = start - end
|
|
25
|
+
|
|
26
|
+
if (cutOff > 0) {
|
|
27
|
+
insert = match[0].slice(offset - cutOff, offset) + insert
|
|
28
|
+
start = end
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
state.tr.insertText(insert, start, end)
|
|
33
|
+
},
|
|
34
|
+
})
|
|
35
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { NodeType } from 'prosemirror-model'
|
|
2
|
+
|
|
3
|
+
import { InputRule, InputRuleFinder } from '../InputRule'
|
|
4
|
+
import { ExtendedRegExpMatchArray } from '../types'
|
|
5
|
+
import { callOrReturn } from '../utilities/callOrReturn'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Build an input rule that changes the type of a textblock when the
|
|
9
|
+
* matched text is typed into it. When using a regular expresion you’ll
|
|
10
|
+
* probably want the regexp to start with `^`, so that the pattern can
|
|
11
|
+
* only occur at the start of a textblock.
|
|
12
|
+
*/
|
|
13
|
+
export function textblockTypeInputRule(config: {
|
|
14
|
+
find: InputRuleFinder,
|
|
15
|
+
type: NodeType,
|
|
16
|
+
getAttributes?:
|
|
17
|
+
| Record<string, any>
|
|
18
|
+
| ((match: ExtendedRegExpMatchArray) => Record<string, any>)
|
|
19
|
+
| false
|
|
20
|
+
| null
|
|
21
|
+
,
|
|
22
|
+
}) {
|
|
23
|
+
return new InputRule({
|
|
24
|
+
find: config.find,
|
|
25
|
+
handler: ({ state, range, match }) => {
|
|
26
|
+
const $start = state.doc.resolve(range.from)
|
|
27
|
+
const attributes = callOrReturn(config.getAttributes, undefined, match) || {}
|
|
28
|
+
|
|
29
|
+
if (!$start.node(-1).canReplaceWith($start.index(-1), $start.indexAfter(-1), config.type)) {
|
|
30
|
+
return null
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
state.tr
|
|
34
|
+
.delete(range.from, range.to)
|
|
35
|
+
.setBlockType(range.from, range.from, config.type, attributes)
|
|
36
|
+
},
|
|
37
|
+
})
|
|
38
|
+
}
|