@tiptap/extension-node-range 2.24.2 → 3.0.0-beta.11

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.
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","sources":["../src/helpers/getNodeRangeDecorations.ts","../src/helpers/getSelectionRanges.ts","../src/helpers/NodeRangeBookmark.ts","../src/helpers/NodeRangeSelection.ts","../src/helpers/isNodeRangeSelection.ts","../src/node-range.ts"],"sourcesContent":["import { SelectionRange } from '@tiptap/pm/state'\nimport { Decoration, DecorationSet } from '@tiptap/pm/view'\n\nexport function getNodeRangeDecorations(ranges: SelectionRange[]): DecorationSet {\n if (!ranges.length) {\n return DecorationSet.empty\n }\n\n const decorations: Decoration[] = []\n const doc = ranges[0].$from.node(0)\n\n ranges.forEach(range => {\n const pos = range.$from.pos\n const node = range.$from.nodeAfter\n\n if (!node) {\n return\n }\n\n decorations.push(\n Decoration.node(pos, pos + node.nodeSize, {\n class: 'ProseMirror-selectednoderange',\n }),\n )\n })\n\n return DecorationSet.create(doc, decorations)\n}\n","import { NodeRange, ResolvedPos } from '@tiptap/pm/model'\nimport { SelectionRange } from '@tiptap/pm/state'\n\nexport function getSelectionRanges(\n $from: ResolvedPos,\n $to: ResolvedPos,\n depth?: number,\n): SelectionRange[] {\n const ranges: SelectionRange[] = []\n const doc = $from.node(0)\n\n // eslint-disable-next-line\n depth = (typeof depth === 'number' && depth >= 0)\n ? depth\n : $from.sameParent($to)\n ? Math.max(0, $from.sharedDepth($to.pos) - 1)\n : $from.sharedDepth($to.pos)\n\n const nodeRange = new NodeRange($from, $to, depth)\n const offset = nodeRange.depth === 0\n ? 0\n : doc.resolve(nodeRange.start).posAtIndex(0)\n\n nodeRange.parent.forEach((node, pos) => {\n const from = offset + pos\n const to = from + node.nodeSize\n\n if (from < nodeRange.start || from >= nodeRange.end) {\n return\n }\n\n const selectionRange = new SelectionRange(doc.resolve(from), doc.resolve(to))\n\n ranges.push(selectionRange)\n })\n\n return ranges\n}\n","import { Node as ProseMirrorNode } from '@tiptap/pm/model'\nimport { Mappable } from '@tiptap/pm/transform'\n\nimport { NodeRangeSelection } from './NodeRangeSelection.js'\n\nexport class NodeRangeBookmark {\n\n anchor!: number\n\n head!: number\n\n constructor(anchor: number, head: number) {\n this.anchor = anchor\n this.head = head\n }\n\n map(mapping: Mappable) {\n return new NodeRangeBookmark(mapping.map(this.anchor), mapping.map(this.head))\n }\n\n resolve(doc: ProseMirrorNode) {\n const $anchor = doc.resolve(this.anchor)\n const $head = doc.resolve(this.head)\n\n return new NodeRangeSelection($anchor, $head)\n }\n\n}\n","import { Node as ProseMirrorNode, ResolvedPos } from '@tiptap/pm/model'\nimport { Selection } from '@tiptap/pm/state'\nimport { Mapping } from '@tiptap/pm/transform'\n\nimport { getSelectionRanges } from './getSelectionRanges.js'\nimport { NodeRangeBookmark } from './NodeRangeBookmark.js'\n\nexport class NodeRangeSelection extends Selection {\n\n depth: number | undefined\n\n constructor($anchor: ResolvedPos, $head: ResolvedPos, depth?: number, bias = 1) {\n // if there is only a cursor we can’t calculate a direction of the selection\n // that’s why we adjust the head position by 1 in the desired direction\n const { doc } = $anchor\n const isCursor = $anchor === $head\n const isCursorAtEnd = $anchor.pos === doc.content.size && $head.pos === doc.content.size\n const $correctedHead = isCursor && !isCursorAtEnd\n ? doc.resolve($head.pos + (bias > 0 ? 1 : -1))\n : $head\n const $correctedAnchor = isCursor && isCursorAtEnd\n ? doc.resolve($anchor.pos - (bias > 0 ? 1 : -1))\n : $anchor\n\n const ranges = getSelectionRanges(\n $correctedAnchor.min($correctedHead),\n $correctedAnchor.max($correctedHead),\n depth,\n )\n\n // get the smallest range start position\n // this will become the $anchor\n const $rangeFrom = ($correctedHead.pos >= $anchor.pos)\n ? ranges[0].$from\n : ranges[ranges.length - 1].$to\n\n // get the biggest range end position\n // this will become the $head\n const $rangeTo = ($correctedHead.pos >= $anchor.pos)\n ? ranges[ranges.length - 1].$to\n : ranges[0].$from\n\n super($rangeFrom, $rangeTo, ranges)\n\n this.depth = depth\n }\n\n // we can safely ignore this TypeScript error: https://github.com/Microsoft/TypeScript/issues/338\n // @ts-ignore\n get $to() {\n return this.ranges[this.ranges.length - 1].$to\n }\n\n eq(other: Selection): boolean {\n return other instanceof NodeRangeSelection\n && other.$from.pos === this.$from.pos\n && other.$to.pos === this.$to.pos\n }\n\n map(doc: ProseMirrorNode, mapping: Mapping): NodeRangeSelection {\n const $anchor = doc.resolve(mapping.map(this.anchor))\n const $head = doc.resolve(mapping.map(this.head))\n\n return new NodeRangeSelection($anchor, $head)\n }\n\n toJSON() {\n return {\n type: 'nodeRange',\n anchor: this.anchor,\n head: this.head,\n }\n }\n\n get isForwards(): boolean {\n return this.head >= this.anchor\n }\n\n get isBackwards(): boolean {\n return !this.isForwards\n }\n\n extendBackwards(): NodeRangeSelection {\n const { doc } = this.$from\n\n if (this.isForwards && this.ranges.length > 1) {\n const ranges = this.ranges.slice(0, -1)\n const $from = ranges[0].$from\n const $to = ranges[ranges.length - 1].$to\n\n return new NodeRangeSelection($from, $to, this.depth)\n }\n\n const firstRange = this.ranges[0]\n const $from = doc.resolve(Math.max(0, firstRange.$from.pos - 1))\n\n return new NodeRangeSelection(this.$anchor, $from, this.depth)\n }\n\n extendForwards(): NodeRangeSelection {\n const { doc } = this.$from\n\n if (this.isBackwards && this.ranges.length > 1) {\n const ranges = this.ranges.slice(1)\n const $from = ranges[0].$from\n const $to = ranges[ranges.length - 1].$to\n\n return new NodeRangeSelection($to, $from, this.depth)\n }\n\n const lastRange = this.ranges[this.ranges.length - 1]\n const $to = doc.resolve(Math.min(doc.content.size, lastRange.$to.pos + 1))\n\n return new NodeRangeSelection(this.$anchor, $to, this.depth)\n }\n\n static fromJSON(doc: ProseMirrorNode, json: any): NodeRangeSelection {\n return new NodeRangeSelection(doc.resolve(json.anchor), doc.resolve(json.head))\n }\n\n static create(doc: ProseMirrorNode, anchor: number, head: number, depth?: number, bias = 1): NodeRangeSelection {\n return new this(doc.resolve(anchor), doc.resolve(head), depth, bias)\n }\n\n getBookmark(): NodeRangeBookmark {\n return new NodeRangeBookmark(this.anchor, this.head)\n }\n\n}\n\nNodeRangeSelection.prototype.visible = false\n","import { NodeRangeSelection } from './NodeRangeSelection.js'\n\nexport function isNodeRangeSelection(value: unknown): value is NodeRangeSelection {\n return value instanceof NodeRangeSelection\n}\n","import { Extension } from '@tiptap/core'\nimport { Plugin, PluginKey, SelectionRange } from '@tiptap/pm/state'\n\nimport { getNodeRangeDecorations } from './helpers/getNodeRangeDecorations.js'\nimport { getSelectionRanges } from './helpers/getSelectionRanges.js'\nimport { isNodeRangeSelection } from './helpers/isNodeRangeSelection.js'\nimport { NodeRangeSelection } from './helpers/NodeRangeSelection.js'\n\nexport interface NodeRangeOptions {\n depth: number | undefined,\n key: 'Shift' | 'Control' | 'Alt' | 'Meta' | 'Mod' | null | undefined,\n}\n\nexport const NodeRange = Extension.create<NodeRangeOptions>({\n name: 'nodeRange',\n\n addOptions() {\n return {\n depth: undefined,\n key: 'Mod',\n }\n },\n\n addKeyboardShortcuts() {\n return {\n // extend NodeRangeSelection upwards\n 'Shift-ArrowUp': ({ editor }) => {\n const { depth } = this.options\n const { view, state } = editor\n const { doc, selection, tr } = state\n const { anchor, head } = selection\n\n if (!isNodeRangeSelection(selection)) {\n const nodeRangeSelection = NodeRangeSelection.create(doc, anchor, head, depth, -1)\n\n tr.setSelection(nodeRangeSelection)\n view.dispatch(tr)\n\n return true\n }\n\n const nodeRangeSelection = selection.extendBackwards()\n\n tr.setSelection(nodeRangeSelection)\n view.dispatch(tr)\n\n return true\n },\n\n // extend NodeRangeSelection downwards\n 'Shift-ArrowDown': ({ editor }) => {\n const { depth } = this.options\n const { view, state } = editor\n const { doc, selection, tr } = state\n const { anchor, head } = selection\n\n if (!isNodeRangeSelection(selection)) {\n const nodeRangeSelection = NodeRangeSelection.create(doc, anchor, head, depth)\n\n tr.setSelection(nodeRangeSelection)\n view.dispatch(tr)\n\n return true\n }\n\n const nodeRangeSelection = selection.extendForwards()\n\n tr.setSelection(nodeRangeSelection)\n view.dispatch(tr)\n\n return true\n },\n\n // add `NodeRangeSelection` to all nodes\n 'Mod-a': ({ editor }) => {\n const { depth } = this.options\n const { view, state } = editor\n const { doc, tr } = state\n const nodeRangeSelection = NodeRangeSelection.create(doc, 0, doc.content.size, depth)\n\n tr.setSelection(nodeRangeSelection)\n view.dispatch(tr)\n\n return true\n },\n }\n },\n\n onSelectionUpdate() {\n const { selection } = this.editor.state\n\n if (isNodeRangeSelection(selection)) {\n this.editor.view.dom.classList.add('ProseMirror-noderangeselection')\n }\n },\n\n addProseMirrorPlugins() {\n let hideTextSelection = false\n let activeMouseSelection = false\n\n return [\n new Plugin({\n key: new PluginKey('nodeRange'),\n\n props: {\n attributes: () => {\n if (hideTextSelection) {\n return {\n class: 'ProseMirror-noderangeselection',\n }\n }\n\n return { class: '' }\n },\n\n handleDOMEvents: {\n mousedown: (view, event) => {\n const { key } = this.options\n const isMac = /Mac/.test(navigator.platform)\n const isShift = !!event.shiftKey\n const isControl = !!event.ctrlKey\n const isAlt = !!event.altKey\n const isMeta = !!event.metaKey\n const isMod = isMac\n ? isMeta\n : isControl\n\n if (\n key === null\n || key === undefined\n || (key === 'Shift' && isShift)\n || (key === 'Control' && isControl)\n || (key === 'Alt' && isAlt)\n || (key === 'Meta' && isMeta)\n || (key === 'Mod' && isMod)\n ) {\n activeMouseSelection = true\n }\n\n if (!activeMouseSelection) {\n return false\n }\n\n document.addEventListener('mouseup', () => {\n activeMouseSelection = false\n\n const { state } = view\n const { doc, selection, tr } = state\n const { $anchor, $head } = selection\n\n if ($anchor.sameParent($head)) {\n return\n }\n\n const nodeRangeSelection = NodeRangeSelection.create(doc, $anchor.pos, $head.pos, this.options.depth)\n\n tr.setSelection(nodeRangeSelection)\n view.dispatch(tr)\n }, { once: true })\n\n return false\n },\n },\n\n // when selecting some text we want to render some decorations\n // to preview a `NodeRangeSelection`\n decorations: state => {\n const { selection } = state\n const isNodeRange = isNodeRangeSelection(selection)\n\n hideTextSelection = false\n\n if (!activeMouseSelection) {\n if (!isNodeRange) {\n return null\n }\n\n hideTextSelection = true\n\n return getNodeRangeDecorations(selection.ranges as SelectionRange[])\n }\n\n const { $from, $to } = selection\n\n // selection is probably in the same node like a paragraph\n // so we don’t render decorations and show\n // a simple text selection instead\n if (!isNodeRange && $from.sameParent($to)) {\n return null\n }\n\n // try to calculate some node ranges\n const nodeRanges = getSelectionRanges($from, $to, this.options.depth)\n\n if (!nodeRanges.length) {\n return null\n }\n\n hideTextSelection = true\n\n return getNodeRangeDecorations(nodeRanges)\n },\n },\n }),\n ]\n },\n})\n"],"names":["DecorationSet","Decoration","NodeRange","SelectionRange","Selection","Extension","Plugin","PluginKey"],"mappings":";;;;;;;;;AAGM,SAAU,uBAAuB,CAAC,MAAwB,EAAA;AAC9D,IAAA,IAAI,CAAC,MAAM,CAAC,MAAM,EAAE;QAClB,OAAOA,kBAAa,CAAC,KAAK;;IAG5B,MAAM,WAAW,GAAiB,EAAE;AACpC,IAAA,MAAM,GAAG,GAAG,MAAM,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC;AAEnC,IAAA,MAAM,CAAC,OAAO,CAAC,KAAK,IAAG;AACrB,QAAA,MAAM,GAAG,GAAG,KAAK,CAAC,KAAK,CAAC,GAAG;AAC3B,QAAA,MAAM,IAAI,GAAG,KAAK,CAAC,KAAK,CAAC,SAAS;QAElC,IAAI,CAAC,IAAI,EAAE;YACT;;AAGF,QAAA,WAAW,CAAC,IAAI,CACdC,eAAU,CAAC,IAAI,CAAC,GAAG,EAAE,GAAG,GAAG,IAAI,CAAC,QAAQ,EAAE;AACxC,YAAA,KAAK,EAAE,+BAA+B;AACvC,SAAA,CAAC,CACH;AACH,KAAC,CAAC;IAEF,OAAOD,kBAAa,CAAC,MAAM,CAAC,GAAG,EAAE,WAAW,CAAC;AAC/C;;SCxBgB,kBAAkB,CAChC,KAA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,IAAI,CAAC,oBAAoB,EAAE;4BACzB,IAAI,CAAC,WAAW,EAAE;AAChB,gCAAA,OAAO,IAAI;;4BAGb,iBAAiB,GAAG,IAAI;AAExB,4BAAA,OAAO,uBAAuB,CAAC,SAAS,CAAC,MAA0B,CAAC;;AAGtE,wBAAA,MAAM,EAAE,KAAK,EAAE,GAAG,EAAE,GAAG,SAAS;;;;wBAKhC,IAAI,CAAC,WAAW,IAAI,KAAK,CAAC,UAAU,CAAC,GAAG,CAAC,EAAE;AACzC,4BAAA,OAAO,IAAI;;;AAIb,wBAAA,MAAM,UAAU,GAAG,kBAAkB,CAAC,KAAK,EAAE,GAAG,EAAE,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC;AAErE,wBAAA,IAAI,CAAC,UAAU,CAAC,MAAM,EAAE;AACtB,4BAAA,OAAO,IAAI;;wBAGb,iBAAiB,GAAG,IAAI;AAExB,wBAAA,OAAO,uBAAuB,CAAC,UAAU,CAAC;qBAC3C;AACF,iBAAA;aACF,CAAC;SACH;KACF;AACF,CAAA;;;;;;;;;"}
1
+ {"version":3,"sources":["../src/index.ts","../src/node-range.ts","../src/helpers/getNodeRangeDecorations.ts","../src/helpers/getSelectionRanges.ts","../src/helpers/NodeRangeSelection.ts","../src/helpers/NodeRangeBookmark.ts","../src/helpers/isNodeRangeSelection.ts"],"sourcesContent":["import { NodeRange } from './node-range.js'\n\nexport * from './helpers/getNodeRangeDecorations.js'\nexport * from './helpers/getSelectionRanges.js'\nexport * from './helpers/isNodeRangeSelection.js'\nexport * from './helpers/NodeRangeSelection.js'\nexport * from './node-range.js'\n\nexport default NodeRange\n","import { Extension } from '@tiptap/core'\nimport type { SelectionRange } from '@tiptap/pm/state'\nimport { Plugin, PluginKey } from '@tiptap/pm/state'\n\nimport { getNodeRangeDecorations } from './helpers/getNodeRangeDecorations.js'\nimport { getSelectionRanges } from './helpers/getSelectionRanges.js'\nimport { isNodeRangeSelection } from './helpers/isNodeRangeSelection.js'\nimport { NodeRangeSelection } from './helpers/NodeRangeSelection.js'\n\nexport interface NodeRangeOptions {\n depth: number | undefined\n key: 'Shift' | 'Control' | 'Alt' | 'Meta' | 'Mod' | null | undefined\n}\n\nexport const NodeRange = Extension.create<NodeRangeOptions>({\n name: 'nodeRange',\n\n addOptions() {\n return {\n depth: undefined,\n key: 'Mod',\n }\n },\n\n addKeyboardShortcuts() {\n return {\n // extend NodeRangeSelection upwards\n 'Shift-ArrowUp': ({ editor }) => {\n const { depth } = this.options\n const { view, state } = editor\n const { doc, selection, tr } = state\n const { anchor, head } = selection\n\n if (!isNodeRangeSelection(selection)) {\n const nodeRangeSelection = NodeRangeSelection.create(doc, anchor, head, depth, -1)\n\n tr.setSelection(nodeRangeSelection)\n view.dispatch(tr)\n\n return true\n }\n\n const nodeRangeSelection = selection.extendBackwards()\n\n tr.setSelection(nodeRangeSelection)\n view.dispatch(tr)\n\n return true\n },\n\n // extend NodeRangeSelection downwards\n 'Shift-ArrowDown': ({ editor }) => {\n const { depth } = this.options\n const { view, state } = editor\n const { doc, selection, tr } = state\n const { anchor, head } = selection\n\n if (!isNodeRangeSelection(selection)) {\n const nodeRangeSelection = NodeRangeSelection.create(doc, anchor, head, depth)\n\n tr.setSelection(nodeRangeSelection)\n view.dispatch(tr)\n\n return true\n }\n\n const nodeRangeSelection = selection.extendForwards()\n\n tr.setSelection(nodeRangeSelection)\n view.dispatch(tr)\n\n return true\n },\n\n // add `NodeRangeSelection` to all nodes\n 'Mod-a': ({ editor }) => {\n const { depth } = this.options\n const { view, state } = editor\n const { doc, tr } = state\n const nodeRangeSelection = NodeRangeSelection.create(doc, 0, doc.content.size, depth)\n\n tr.setSelection(nodeRangeSelection)\n view.dispatch(tr)\n\n return true\n },\n }\n },\n\n onSelectionUpdate() {\n const { selection } = this.editor.state\n\n if (isNodeRangeSelection(selection)) {\n this.editor.view.dom.classList.add('ProseMirror-noderangeselection')\n }\n },\n\n addProseMirrorPlugins() {\n let hideTextSelection = false\n let activeMouseSelection = false\n\n return [\n new Plugin({\n key: new PluginKey('nodeRange'),\n\n props: {\n attributes: () => {\n if (hideTextSelection) {\n return {\n class: 'ProseMirror-noderangeselection',\n }\n }\n\n return { class: '' }\n },\n\n handleDOMEvents: {\n mousedown: (view, event) => {\n const { key } = this.options\n const isMac = /Mac/.test(navigator.platform)\n const isShift = !!event.shiftKey\n const isControl = !!event.ctrlKey\n const isAlt = !!event.altKey\n const isMeta = !!event.metaKey\n const isMod = isMac ? isMeta : isControl\n\n if (\n key === null ||\n key === undefined ||\n (key === 'Shift' && isShift) ||\n (key === 'Control' && isControl) ||\n (key === 'Alt' && isAlt) ||\n (key === 'Meta' && isMeta) ||\n (key === 'Mod' && isMod)\n ) {\n activeMouseSelection = true\n }\n\n if (!activeMouseSelection) {\n return false\n }\n\n document.addEventListener(\n 'mouseup',\n () => {\n activeMouseSelection = false\n\n const { state } = view\n const { doc, selection, tr } = state\n const { $anchor, $head } = selection\n\n if ($anchor.sameParent($head)) {\n return\n }\n\n const nodeRangeSelection = NodeRangeSelection.create(doc, $anchor.pos, $head.pos, this.options.depth)\n\n tr.setSelection(nodeRangeSelection)\n view.dispatch(tr)\n },\n { once: true },\n )\n\n return false\n },\n },\n\n // when selecting some text we want to render some decorations\n // to preview a `NodeRangeSelection`\n decorations: state => {\n const { selection } = state\n const isNodeRange = isNodeRangeSelection(selection)\n\n hideTextSelection = false\n\n if (!activeMouseSelection) {\n if (!isNodeRange) {\n return null\n }\n\n hideTextSelection = true\n\n return getNodeRangeDecorations(selection.ranges as SelectionRange[])\n }\n\n const { $from, $to } = selection\n\n // selection is probably in the same node like a paragraph\n // so we don’t render decorations and show\n // a simple text selection instead\n if (!isNodeRange && $from.sameParent($to)) {\n return null\n }\n\n // try to calculate some node ranges\n const nodeRanges = getSelectionRanges($from, $to, this.options.depth)\n\n if (!nodeRanges.length) {\n return null\n }\n\n hideTextSelection = true\n\n return getNodeRangeDecorations(nodeRanges)\n },\n },\n }),\n ]\n },\n})\n","import type { SelectionRange } from '@tiptap/pm/state'\nimport { Decoration, DecorationSet } from '@tiptap/pm/view'\n\nexport function getNodeRangeDecorations(ranges: SelectionRange[]): DecorationSet {\n if (!ranges.length) {\n return DecorationSet.empty\n }\n\n const decorations: Decoration[] = []\n const doc = ranges[0].$from.node(0)\n\n ranges.forEach(range => {\n const pos = range.$from.pos\n const node = range.$from.nodeAfter\n\n if (!node) {\n return\n }\n\n decorations.push(\n Decoration.node(pos, pos + node.nodeSize, {\n class: 'ProseMirror-selectednoderange',\n }),\n )\n })\n\n return DecorationSet.create(doc, decorations)\n}\n","import { type ResolvedPos, NodeRange } from '@tiptap/pm/model'\nimport { SelectionRange } from '@tiptap/pm/state'\n\nexport function getSelectionRanges($from: ResolvedPos, $to: ResolvedPos, depth?: number): SelectionRange[] {\n const ranges: SelectionRange[] = []\n const doc = $from.node(0)\n\n // Determine the appropriate depth\n if (typeof depth === 'number' && depth >= 0) {\n // Use the provided depth\n } else if ($from.sameParent($to)) {\n depth = Math.max(0, $from.sharedDepth($to.pos) - 1)\n } else {\n depth = $from.sharedDepth($to.pos)\n }\n\n const nodeRange = new NodeRange($from, $to, depth)\n const offset = nodeRange.depth === 0 ? 0 : doc.resolve(nodeRange.start).posAtIndex(0)\n\n nodeRange.parent.forEach((node, pos) => {\n const from = offset + pos\n const to = from + node.nodeSize\n\n if (from < nodeRange.start || from >= nodeRange.end) {\n return\n }\n\n const selectionRange = new SelectionRange(doc.resolve(from), doc.resolve(to))\n\n ranges.push(selectionRange)\n })\n\n return ranges\n}\n","import type { Node as ProseMirrorNode, ResolvedPos } from '@tiptap/pm/model'\nimport { Selection } from '@tiptap/pm/state'\nimport type { Mapping } from '@tiptap/pm/transform'\n\nimport { getSelectionRanges } from './getSelectionRanges.js'\nimport { NodeRangeBookmark } from './NodeRangeBookmark.js'\n\nexport class NodeRangeSelection extends Selection {\n depth: number | undefined\n\n constructor($anchor: ResolvedPos, $head: ResolvedPos, depth?: number, bias = 1) {\n // if there is only a cursor we can’t calculate a direction of the selection\n // that’s why we adjust the head position by 1 in the desired direction\n const { doc } = $anchor\n const isCursor = $anchor === $head\n const isCursorAtEnd = $anchor.pos === doc.content.size && $head.pos === doc.content.size\n const $correctedHead = isCursor && !isCursorAtEnd ? doc.resolve($head.pos + (bias > 0 ? 1 : -1)) : $head\n const $correctedAnchor = isCursor && isCursorAtEnd ? doc.resolve($anchor.pos - (bias > 0 ? 1 : -1)) : $anchor\n\n const ranges = getSelectionRanges($correctedAnchor.min($correctedHead), $correctedAnchor.max($correctedHead), depth)\n\n // get the smallest range start position\n // this will become the $anchor\n const $rangeFrom = $correctedHead.pos >= $anchor.pos ? ranges[0].$from : ranges[ranges.length - 1].$to\n\n // get the biggest range end position\n // this will become the $head\n const $rangeTo = $correctedHead.pos >= $anchor.pos ? ranges[ranges.length - 1].$to : ranges[0].$from\n\n super($rangeFrom, $rangeTo, ranges)\n\n this.depth = depth\n }\n\n // we can safely ignore this TypeScript error: https://github.com/Microsoft/TypeScript/issues/338\n // @ts-ignore\n get $to() {\n return this.ranges[this.ranges.length - 1].$to\n }\n\n eq(other: Selection): boolean {\n return other instanceof NodeRangeSelection && other.$from.pos === this.$from.pos && other.$to.pos === this.$to.pos\n }\n\n map(doc: ProseMirrorNode, mapping: Mapping): NodeRangeSelection {\n const $anchor = doc.resolve(mapping.map(this.anchor))\n const $head = doc.resolve(mapping.map(this.head))\n\n return new NodeRangeSelection($anchor, $head)\n }\n\n toJSON() {\n return {\n type: 'nodeRange',\n anchor: this.anchor,\n head: this.head,\n }\n }\n\n get isForwards(): boolean {\n return this.head >= this.anchor\n }\n\n get isBackwards(): boolean {\n return !this.isForwards\n }\n\n extendBackwards(): NodeRangeSelection {\n const { doc } = this.$from\n\n if (this.isForwards && this.ranges.length > 1) {\n const ranges = this.ranges.slice(0, -1)\n const $from = ranges[0].$from\n const $to = ranges[ranges.length - 1].$to\n\n return new NodeRangeSelection($from, $to, this.depth)\n }\n\n const firstRange = this.ranges[0]\n const $from = doc.resolve(Math.max(0, firstRange.$from.pos - 1))\n\n return new NodeRangeSelection(this.$anchor, $from, this.depth)\n }\n\n extendForwards(): NodeRangeSelection {\n const { doc } = this.$from\n\n if (this.isBackwards && this.ranges.length > 1) {\n const ranges = this.ranges.slice(1)\n const $from = ranges[0].$from\n const $to = ranges[ranges.length - 1].$to\n\n return new NodeRangeSelection($to, $from, this.depth)\n }\n\n const lastRange = this.ranges[this.ranges.length - 1]\n const $to = doc.resolve(Math.min(doc.content.size, lastRange.$to.pos + 1))\n\n return new NodeRangeSelection(this.$anchor, $to, this.depth)\n }\n\n static fromJSON(doc: ProseMirrorNode, json: any): NodeRangeSelection {\n return new NodeRangeSelection(doc.resolve(json.anchor), doc.resolve(json.head))\n }\n\n static create(doc: ProseMirrorNode, anchor: number, head: number, depth?: number, bias = 1): NodeRangeSelection {\n return new this(doc.resolve(anchor), doc.resolve(head), depth, bias)\n }\n\n getBookmark(): NodeRangeBookmark {\n return new NodeRangeBookmark(this.anchor, this.head)\n }\n}\n\nNodeRangeSelection.prototype.visible = false\n","import type { Node as ProseMirrorNode } from '@tiptap/pm/model'\nimport type { Mappable } from '@tiptap/pm/transform'\n\nimport { NodeRangeSelection } from './NodeRangeSelection.js'\n\nexport class NodeRangeBookmark {\n anchor!: number\n\n head!: number\n\n constructor(anchor: number, head: number) {\n this.anchor = anchor\n this.head = head\n }\n\n map(mapping: Mappable) {\n return new NodeRangeBookmark(mapping.map(this.anchor), mapping.map(this.head))\n }\n\n resolve(doc: ProseMirrorNode) {\n const $anchor = doc.resolve(this.anchor)\n const $head = doc.resolve(this.head)\n\n return new NodeRangeSelection($anchor, $head)\n }\n}\n","import { NodeRangeSelection } from './NodeRangeSelection.js'\n\nexport function isNodeRangeSelection(value: unknown): value is NodeRangeSelection {\n return value instanceof NodeRangeSelection\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA,mBAAAA;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,kBA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WAAW,KAAK,GAAG;AAC7B;AAAA,kBACF;AAEA,wBAAM,qBAAqB,mBAAmB,OAAO,KAAK,QAAQ,KAAK,MAAM,KAAK,KAAK,QAAQ,KAAK;AAEpG,qBAAG,aAAa,kBAAkB;AAClC,uBAAK,SAAS,EAAE;AAAA,gBAClB;AAAA,gBACA,EAAE,MAAM,KAAK;AAAA,cACf;AAEA,qBAAO;AAAA,YACT;AAAA,UACF;AAAA;AAAA;AAAA,UAIA,aAAa,WAAS;AACpB,kBAAM,EAAE,UAAU,IAAI;AACtB,kBAAM,cAAc,qBAAqB,SAAS;AAElD,gCAAoB;AAEpB,gBAAI,CAAC,sBAAsB;AACzB,kBAAI,CAAC,aAAa;AAChB,uBAAO;AAAA,cACT;AAEA,kCAAoB;AAEpB,qBAAO,wBAAwB,UAAU,MAA0B;AAAA,YACrE;AAEA,kBAAM,EAAE,OAAO,IAAI,IAAI;AAKvB,gBAAI,CAAC,eAAe,MAAM,WAAW,GAAG,GAAG;AACzC,qBAAO;AAAA,YACT;AAGA,kBAAM,aAAa,mBAAmB,OAAO,KAAK,KAAK,QAAQ,KAAK;AAEpE,gBAAI,CAAC,WAAW,QAAQ;AACtB,qBAAO;AAAA,YACT;AAEA,gCAAoB;AAEpB,mBAAO,wBAAwB,UAAU;AAAA,UAC3C;AAAA,QACF;AAAA,MACF,CAAC;AAAA,IACH;AAAA,EACF;AACF,CAAC;;;ADzMD,IAAO,gBAAQC;","names":["NodeRange","import_state","import_state","$from","$to","NodeRange","nodeRangeSelection","NodeRange"]}
@@ -0,0 +1,47 @@
1
+ import { Extension } from '@tiptap/core';
2
+ import { SelectionRange, Selection } from '@tiptap/pm/state';
3
+ import { DecorationSet } from '@tiptap/pm/view';
4
+ import { ResolvedPos, Node } from '@tiptap/pm/model';
5
+ import { Mappable, Mapping } from '@tiptap/pm/transform';
6
+
7
+ interface NodeRangeOptions {
8
+ depth: number | undefined;
9
+ key: 'Shift' | 'Control' | 'Alt' | 'Meta' | 'Mod' | null | undefined;
10
+ }
11
+ declare const NodeRange: Extension<NodeRangeOptions, any>;
12
+
13
+ declare function getNodeRangeDecorations(ranges: SelectionRange[]): DecorationSet;
14
+
15
+ declare function getSelectionRanges($from: ResolvedPos, $to: ResolvedPos, depth?: number): SelectionRange[];
16
+
17
+ declare class NodeRangeBookmark {
18
+ anchor: number;
19
+ head: number;
20
+ constructor(anchor: number, head: number);
21
+ map(mapping: Mappable): NodeRangeBookmark;
22
+ resolve(doc: Node): NodeRangeSelection;
23
+ }
24
+
25
+ declare class NodeRangeSelection extends Selection {
26
+ depth: number | undefined;
27
+ constructor($anchor: ResolvedPos, $head: ResolvedPos, depth?: number, bias?: number);
28
+ get $to(): ResolvedPos;
29
+ eq(other: Selection): boolean;
30
+ map(doc: Node, mapping: Mapping): NodeRangeSelection;
31
+ toJSON(): {
32
+ type: string;
33
+ anchor: number;
34
+ head: number;
35
+ };
36
+ get isForwards(): boolean;
37
+ get isBackwards(): boolean;
38
+ extendBackwards(): NodeRangeSelection;
39
+ extendForwards(): NodeRangeSelection;
40
+ static fromJSON(doc: Node, json: any): NodeRangeSelection;
41
+ static create(doc: Node, anchor: number, head: number, depth?: number, bias?: number): NodeRangeSelection;
42
+ getBookmark(): NodeRangeBookmark;
43
+ }
44
+
45
+ declare function isNodeRangeSelection(value: unknown): value is NodeRangeSelection;
46
+
47
+ export { NodeRange, type NodeRangeOptions, NodeRangeSelection, NodeRange as default, getNodeRangeDecorations, getSelectionRanges, isNodeRangeSelection };
package/dist/index.d.ts CHANGED
@@ -1,8 +1,47 @@
1
- import { NodeRange } from './node-range.js';
2
- export * from './helpers/getNodeRangeDecorations.js';
3
- export * from './helpers/getSelectionRanges.js';
4
- export * from './helpers/isNodeRangeSelection.js';
5
- export * from './helpers/NodeRangeSelection.js';
6
- export * from './node-range.js';
7
- export default NodeRange;
8
- //# sourceMappingURL=index.d.ts.map
1
+ import { Extension } from '@tiptap/core';
2
+ import { SelectionRange, Selection } from '@tiptap/pm/state';
3
+ import { DecorationSet } from '@tiptap/pm/view';
4
+ import { ResolvedPos, Node } from '@tiptap/pm/model';
5
+ import { Mappable, Mapping } from '@tiptap/pm/transform';
6
+
7
+ interface NodeRangeOptions {
8
+ depth: number | undefined;
9
+ key: 'Shift' | 'Control' | 'Alt' | 'Meta' | 'Mod' | null | undefined;
10
+ }
11
+ declare const NodeRange: Extension<NodeRangeOptions, any>;
12
+
13
+ declare function getNodeRangeDecorations(ranges: SelectionRange[]): DecorationSet;
14
+
15
+ declare function getSelectionRanges($from: ResolvedPos, $to: ResolvedPos, depth?: number): SelectionRange[];
16
+
17
+ declare class NodeRangeBookmark {
18
+ anchor: number;
19
+ head: number;
20
+ constructor(anchor: number, head: number);
21
+ map(mapping: Mappable): NodeRangeBookmark;
22
+ resolve(doc: Node): NodeRangeSelection;
23
+ }
24
+
25
+ declare class NodeRangeSelection extends Selection {
26
+ depth: number | undefined;
27
+ constructor($anchor: ResolvedPos, $head: ResolvedPos, depth?: number, bias?: number);
28
+ get $to(): ResolvedPos;
29
+ eq(other: Selection): boolean;
30
+ map(doc: Node, mapping: Mapping): NodeRangeSelection;
31
+ toJSON(): {
32
+ type: string;
33
+ anchor: number;
34
+ head: number;
35
+ };
36
+ get isForwards(): boolean;
37
+ get isBackwards(): boolean;
38
+ extendBackwards(): NodeRangeSelection;
39
+ extendForwards(): NodeRangeSelection;
40
+ static fromJSON(doc: Node, json: any): NodeRangeSelection;
41
+ static create(doc: Node, anchor: number, head: number, depth?: number, bias?: number): NodeRangeSelection;
42
+ getBookmark(): NodeRangeBookmark;
43
+ }
44
+
45
+ declare function isNodeRangeSelection(value: unknown): value is NodeRangeSelection;
46
+
47
+ export { NodeRange, type NodeRangeOptions, NodeRangeSelection, NodeRange as default, getNodeRangeDecorations, getSelectionRanges, isNodeRangeSelection };