@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.
- package/LICENSE.md +21 -0
- package/dist/index.cjs +315 -297
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +47 -0
- package/dist/index.d.ts +47 -8
- package/dist/index.js +286 -290
- package/dist/index.js.map +1 -1
- package/package.json +12 -10
- package/src/helpers/NodeRangeBookmark.ts +2 -4
- package/src/helpers/NodeRangeSelection.ts +9 -25
- package/src/helpers/getNodeRangeDecorations.ts +1 -1
- package/src/helpers/getSelectionRanges.ts +11 -15
- package/src/node-range.ts +28 -25
- package/dist/helpers/NodeRangeBookmark.d.ts +0 -11
- package/dist/helpers/NodeRangeBookmark.d.ts.map +0 -1
- package/dist/helpers/NodeRangeSelection.d.ts +0 -24
- package/dist/helpers/NodeRangeSelection.d.ts.map +0 -1
- package/dist/helpers/getNodeRangeDecorations.d.ts +0 -4
- package/dist/helpers/getNodeRangeDecorations.d.ts.map +0 -1
- package/dist/helpers/getSelectionRanges.d.ts +0 -4
- package/dist/helpers/getSelectionRanges.d.ts.map +0 -1
- package/dist/helpers/isNodeRangeSelection.d.ts +0 -3
- package/dist/helpers/isNodeRangeSelection.d.ts.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/index.umd.js +0 -320
- package/dist/index.umd.js.map +0 -1
- package/dist/node-range.d.ts +0 -7
- package/dist/node-range.d.ts.map +0 -1
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","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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E,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/node-range.ts","../src/helpers/getNodeRangeDecorations.ts","../src/helpers/getSelectionRanges.ts","../src/helpers/NodeRangeSelection.ts","../src/helpers/NodeRangeBookmark.ts","../src/helpers/isNodeRangeSelection.ts","../src/index.ts"],"sourcesContent":["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","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"],"mappings":";AAAA,SAAS,iBAAiB;AAE1B,SAAS,QAAQ,iBAAiB;;;ACDlC,SAAS,YAAY,qBAAqB;AAEnC,SAAS,wBAAwB,QAAyC;AAC/E,MAAI,CAAC,OAAO,QAAQ;AAClB,WAAO,cAAc;AAAA,EACvB;AAEA,QAAM,cAA4B,CAAC;AACnC,QAAM,MAAM,OAAO,CAAC,EAAE,MAAM,KAAK,CAAC;AAElC,SAAO,QAAQ,WAAS;AACtB,UAAM,MAAM,MAAM,MAAM;AACxB,UAAM,OAAO,MAAM,MAAM;AAEzB,QAAI,CAAC,MAAM;AACT;AAAA,IACF;AAEA,gBAAY;AAAA,MACV,WAAW,KAAK,KAAK,MAAM,KAAK,UAAU;AAAA,QACxC,OAAO;AAAA,MACT,CAAC;AAAA,IACH;AAAA,EACF,CAAC;AAED,SAAO,cAAc,OAAO,KAAK,WAAW;AAC9C;;;AC3BA,SAA2B,i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AEA,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;;;AMzMD,IAAO,gBAAQC;","names":["$from","$to","NodeRange","nodeRangeSelection","NodeRange"]}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tiptap/extension-node-range",
|
|
3
3
|
"description": "node range extension for tiptap",
|
|
4
|
-
"version": "
|
|
4
|
+
"version": "3.0.0-beta.11",
|
|
5
5
|
"homepage": "https://tiptap.dev",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"tiptap",
|
|
@@ -20,29 +20,31 @@
|
|
|
20
20
|
"type": "module",
|
|
21
21
|
"exports": {
|
|
22
22
|
".": {
|
|
23
|
-
"types":
|
|
23
|
+
"types": {
|
|
24
|
+
"import": "./dist/index.d.ts",
|
|
25
|
+
"require": "./dist/index.d.cts"
|
|
26
|
+
},
|
|
24
27
|
"import": "./dist/index.js",
|
|
25
28
|
"require": "./dist/index.cjs"
|
|
26
29
|
}
|
|
27
30
|
},
|
|
28
31
|
"main": "dist/index.cjs",
|
|
29
32
|
"module": "dist/index.js",
|
|
30
|
-
"umd": "dist/index.umd.js",
|
|
31
33
|
"types": "dist/index.d.ts",
|
|
32
34
|
"files": [
|
|
33
35
|
"src",
|
|
34
36
|
"dist"
|
|
35
37
|
],
|
|
36
38
|
"peerDependencies": {
|
|
37
|
-
"@tiptap/core": "
|
|
38
|
-
"@tiptap/pm": "
|
|
39
|
+
"@tiptap/core": "3.0.0-beta.11",
|
|
40
|
+
"@tiptap/pm": "3.0.0-beta.11"
|
|
39
41
|
},
|
|
40
42
|
"devDependencies": {
|
|
41
|
-
"@tiptap/
|
|
42
|
-
"@tiptap/
|
|
43
|
+
"@tiptap/pm": "3.0.0-beta.11",
|
|
44
|
+
"@tiptap/core": "3.0.0-beta.11"
|
|
43
45
|
},
|
|
44
46
|
"scripts": {
|
|
45
|
-
"
|
|
46
|
-
"
|
|
47
|
+
"build": "tsup",
|
|
48
|
+
"lint": "prettier ./src/ --check && eslint --cache --quiet --no-error-on-unmatched-pattern ./src/"
|
|
47
49
|
}
|
|
48
|
-
}
|
|
50
|
+
}
|
|
@@ -1,10 +1,9 @@
|
|
|
1
|
-
import { Node as ProseMirrorNode } from '@tiptap/pm/model'
|
|
2
|
-
import { Mappable } from '@tiptap/pm/transform'
|
|
1
|
+
import type { Node as ProseMirrorNode } from '@tiptap/pm/model'
|
|
2
|
+
import type { Mappable } from '@tiptap/pm/transform'
|
|
3
3
|
|
|
4
4
|
import { NodeRangeSelection } from './NodeRangeSelection.js'
|
|
5
5
|
|
|
6
6
|
export class NodeRangeBookmark {
|
|
7
|
-
|
|
8
7
|
anchor!: number
|
|
9
8
|
|
|
10
9
|
head!: number
|
|
@@ -24,5 +23,4 @@ export class NodeRangeBookmark {
|
|
|
24
23
|
|
|
25
24
|
return new NodeRangeSelection($anchor, $head)
|
|
26
25
|
}
|
|
27
|
-
|
|
28
26
|
}
|
|
@@ -1,12 +1,11 @@
|
|
|
1
|
-
import { Node as ProseMirrorNode, ResolvedPos } from '@tiptap/pm/model'
|
|
1
|
+
import type { Node as ProseMirrorNode, ResolvedPos } from '@tiptap/pm/model'
|
|
2
2
|
import { Selection } from '@tiptap/pm/state'
|
|
3
|
-
import { Mapping } from '@tiptap/pm/transform'
|
|
3
|
+
import type { Mapping } from '@tiptap/pm/transform'
|
|
4
4
|
|
|
5
5
|
import { getSelectionRanges } from './getSelectionRanges.js'
|
|
6
6
|
import { NodeRangeBookmark } from './NodeRangeBookmark.js'
|
|
7
7
|
|
|
8
8
|
export class NodeRangeSelection extends Selection {
|
|
9
|
-
|
|
10
9
|
depth: number | undefined
|
|
11
10
|
|
|
12
11
|
constructor($anchor: ResolvedPos, $head: ResolvedPos, depth?: number, bias = 1) {
|
|
@@ -15,30 +14,18 @@ export class NodeRangeSelection extends Selection {
|
|
|
15
14
|
const { doc } = $anchor
|
|
16
15
|
const isCursor = $anchor === $head
|
|
17
16
|
const isCursorAtEnd = $anchor.pos === doc.content.size && $head.pos === doc.content.size
|
|
18
|
-
const $correctedHead = isCursor && !isCursorAtEnd
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
const
|
|
22
|
-
? doc.resolve($anchor.pos - (bias > 0 ? 1 : -1))
|
|
23
|
-
: $anchor
|
|
24
|
-
|
|
25
|
-
const ranges = getSelectionRanges(
|
|
26
|
-
$correctedAnchor.min($correctedHead),
|
|
27
|
-
$correctedAnchor.max($correctedHead),
|
|
28
|
-
depth,
|
|
29
|
-
)
|
|
17
|
+
const $correctedHead = isCursor && !isCursorAtEnd ? doc.resolve($head.pos + (bias > 0 ? 1 : -1)) : $head
|
|
18
|
+
const $correctedAnchor = isCursor && isCursorAtEnd ? doc.resolve($anchor.pos - (bias > 0 ? 1 : -1)) : $anchor
|
|
19
|
+
|
|
20
|
+
const ranges = getSelectionRanges($correctedAnchor.min($correctedHead), $correctedAnchor.max($correctedHead), depth)
|
|
30
21
|
|
|
31
22
|
// get the smallest range start position
|
|
32
23
|
// this will become the $anchor
|
|
33
|
-
const $rangeFrom =
|
|
34
|
-
? ranges[0].$from
|
|
35
|
-
: ranges[ranges.length - 1].$to
|
|
24
|
+
const $rangeFrom = $correctedHead.pos >= $anchor.pos ? ranges[0].$from : ranges[ranges.length - 1].$to
|
|
36
25
|
|
|
37
26
|
// get the biggest range end position
|
|
38
27
|
// this will become the $head
|
|
39
|
-
const $rangeTo =
|
|
40
|
-
? ranges[ranges.length - 1].$to
|
|
41
|
-
: ranges[0].$from
|
|
28
|
+
const $rangeTo = $correctedHead.pos >= $anchor.pos ? ranges[ranges.length - 1].$to : ranges[0].$from
|
|
42
29
|
|
|
43
30
|
super($rangeFrom, $rangeTo, ranges)
|
|
44
31
|
|
|
@@ -52,9 +39,7 @@ export class NodeRangeSelection extends Selection {
|
|
|
52
39
|
}
|
|
53
40
|
|
|
54
41
|
eq(other: Selection): boolean {
|
|
55
|
-
return other instanceof NodeRangeSelection
|
|
56
|
-
&& other.$from.pos === this.$from.pos
|
|
57
|
-
&& other.$to.pos === this.$to.pos
|
|
42
|
+
return other instanceof NodeRangeSelection && other.$from.pos === this.$from.pos && other.$to.pos === this.$to.pos
|
|
58
43
|
}
|
|
59
44
|
|
|
60
45
|
map(doc: ProseMirrorNode, mapping: Mapping): NodeRangeSelection {
|
|
@@ -125,7 +110,6 @@ export class NodeRangeSelection extends Selection {
|
|
|
125
110
|
getBookmark(): NodeRangeBookmark {
|
|
126
111
|
return new NodeRangeBookmark(this.anchor, this.head)
|
|
127
112
|
}
|
|
128
|
-
|
|
129
113
|
}
|
|
130
114
|
|
|
131
115
|
NodeRangeSelection.prototype.visible = false
|
|
@@ -1,25 +1,21 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { type ResolvedPos, NodeRange } from '@tiptap/pm/model'
|
|
2
2
|
import { SelectionRange } from '@tiptap/pm/state'
|
|
3
3
|
|
|
4
|
-
export function getSelectionRanges(
|
|
5
|
-
$from: ResolvedPos,
|
|
6
|
-
$to: ResolvedPos,
|
|
7
|
-
depth?: number,
|
|
8
|
-
): SelectionRange[] {
|
|
4
|
+
export function getSelectionRanges($from: ResolvedPos, $to: ResolvedPos, depth?: number): SelectionRange[] {
|
|
9
5
|
const ranges: SelectionRange[] = []
|
|
10
6
|
const doc = $from.node(0)
|
|
11
7
|
|
|
12
|
-
//
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
8
|
+
// Determine the appropriate depth
|
|
9
|
+
if (typeof depth === 'number' && depth >= 0) {
|
|
10
|
+
// Use the provided depth
|
|
11
|
+
} else if ($from.sameParent($to)) {
|
|
12
|
+
depth = Math.max(0, $from.sharedDepth($to.pos) - 1)
|
|
13
|
+
} else {
|
|
14
|
+
depth = $from.sharedDepth($to.pos)
|
|
15
|
+
}
|
|
18
16
|
|
|
19
17
|
const nodeRange = new NodeRange($from, $to, depth)
|
|
20
|
-
const offset = nodeRange.depth === 0
|
|
21
|
-
? 0
|
|
22
|
-
: doc.resolve(nodeRange.start).posAtIndex(0)
|
|
18
|
+
const offset = nodeRange.depth === 0 ? 0 : doc.resolve(nodeRange.start).posAtIndex(0)
|
|
23
19
|
|
|
24
20
|
nodeRange.parent.forEach((node, pos) => {
|
|
25
21
|
const from = offset + pos
|
package/src/node-range.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Extension } from '@tiptap/core'
|
|
2
|
-
import {
|
|
2
|
+
import type { SelectionRange } from '@tiptap/pm/state'
|
|
3
|
+
import { Plugin, PluginKey } from '@tiptap/pm/state'
|
|
3
4
|
|
|
4
5
|
import { getNodeRangeDecorations } from './helpers/getNodeRangeDecorations.js'
|
|
5
6
|
import { getSelectionRanges } from './helpers/getSelectionRanges.js'
|
|
@@ -7,8 +8,8 @@ import { isNodeRangeSelection } from './helpers/isNodeRangeSelection.js'
|
|
|
7
8
|
import { NodeRangeSelection } from './helpers/NodeRangeSelection.js'
|
|
8
9
|
|
|
9
10
|
export interface NodeRangeOptions {
|
|
10
|
-
depth: number | undefined
|
|
11
|
-
key: 'Shift' | 'Control' | 'Alt' | 'Meta' | 'Mod' | null | undefined
|
|
11
|
+
depth: number | undefined
|
|
12
|
+
key: 'Shift' | 'Control' | 'Alt' | 'Meta' | 'Mod' | null | undefined
|
|
12
13
|
}
|
|
13
14
|
|
|
14
15
|
export const NodeRange = Extension.create<NodeRangeOptions>({
|
|
@@ -121,18 +122,16 @@ export const NodeRange = Extension.create<NodeRangeOptions>({
|
|
|
121
122
|
const isControl = !!event.ctrlKey
|
|
122
123
|
const isAlt = !!event.altKey
|
|
123
124
|
const isMeta = !!event.metaKey
|
|
124
|
-
const isMod = isMac
|
|
125
|
-
? isMeta
|
|
126
|
-
: isControl
|
|
125
|
+
const isMod = isMac ? isMeta : isControl
|
|
127
126
|
|
|
128
127
|
if (
|
|
129
|
-
key === null
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
128
|
+
key === null ||
|
|
129
|
+
key === undefined ||
|
|
130
|
+
(key === 'Shift' && isShift) ||
|
|
131
|
+
(key === 'Control' && isControl) ||
|
|
132
|
+
(key === 'Alt' && isAlt) ||
|
|
133
|
+
(key === 'Meta' && isMeta) ||
|
|
134
|
+
(key === 'Mod' && isMod)
|
|
136
135
|
) {
|
|
137
136
|
activeMouseSelection = true
|
|
138
137
|
}
|
|
@@ -141,22 +140,26 @@ export const NodeRange = Extension.create<NodeRangeOptions>({
|
|
|
141
140
|
return false
|
|
142
141
|
}
|
|
143
142
|
|
|
144
|
-
document.addEventListener(
|
|
145
|
-
|
|
143
|
+
document.addEventListener(
|
|
144
|
+
'mouseup',
|
|
145
|
+
() => {
|
|
146
|
+
activeMouseSelection = false
|
|
146
147
|
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
148
|
+
const { state } = view
|
|
149
|
+
const { doc, selection, tr } = state
|
|
150
|
+
const { $anchor, $head } = selection
|
|
150
151
|
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
152
|
+
if ($anchor.sameParent($head)) {
|
|
153
|
+
return
|
|
154
|
+
}
|
|
154
155
|
|
|
155
|
-
|
|
156
|
+
const nodeRangeSelection = NodeRangeSelection.create(doc, $anchor.pos, $head.pos, this.options.depth)
|
|
156
157
|
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
158
|
+
tr.setSelection(nodeRangeSelection)
|
|
159
|
+
view.dispatch(tr)
|
|
160
|
+
},
|
|
161
|
+
{ once: true },
|
|
162
|
+
)
|
|
160
163
|
|
|
161
164
|
return false
|
|
162
165
|
},
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { Node as ProseMirrorNode } from '@tiptap/pm/model';
|
|
2
|
-
import { Mappable } from '@tiptap/pm/transform';
|
|
3
|
-
import { NodeRangeSelection } from './NodeRangeSelection.js';
|
|
4
|
-
export declare class NodeRangeBookmark {
|
|
5
|
-
anchor: number;
|
|
6
|
-
head: number;
|
|
7
|
-
constructor(anchor: number, head: number);
|
|
8
|
-
map(mapping: Mappable): NodeRangeBookmark;
|
|
9
|
-
resolve(doc: ProseMirrorNode): NodeRangeSelection;
|
|
10
|
-
}
|
|
11
|
-
//# sourceMappingURL=NodeRangeBookmark.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"NodeRangeBookmark.d.ts","sourceRoot":"","sources":["../../src/helpers/NodeRangeBookmark.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,IAAI,eAAe,EAAE,MAAM,kBAAkB,CAAA;AAC1D,OAAO,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAA;AAE/C,OAAO,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAA;AAE5D,qBAAa,iBAAiB;IAE5B,MAAM,EAAG,MAAM,CAAA;IAEf,IAAI,EAAG,MAAM,CAAA;gBAED,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM;IAKxC,GAAG,CAAC,OAAO,EAAE,QAAQ;IAIrB,OAAO,CAAC,GAAG,EAAE,eAAe;CAO7B"}
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import { Node as ProseMirrorNode, ResolvedPos } from '@tiptap/pm/model';
|
|
2
|
-
import { Selection } from '@tiptap/pm/state';
|
|
3
|
-
import { Mapping } from '@tiptap/pm/transform';
|
|
4
|
-
import { NodeRangeBookmark } from './NodeRangeBookmark.js';
|
|
5
|
-
export declare class NodeRangeSelection extends Selection {
|
|
6
|
-
depth: number | undefined;
|
|
7
|
-
constructor($anchor: ResolvedPos, $head: ResolvedPos, depth?: number, bias?: number);
|
|
8
|
-
get $to(): ResolvedPos;
|
|
9
|
-
eq(other: Selection): boolean;
|
|
10
|
-
map(doc: ProseMirrorNode, mapping: Mapping): NodeRangeSelection;
|
|
11
|
-
toJSON(): {
|
|
12
|
-
type: string;
|
|
13
|
-
anchor: number;
|
|
14
|
-
head: number;
|
|
15
|
-
};
|
|
16
|
-
get isForwards(): boolean;
|
|
17
|
-
get isBackwards(): boolean;
|
|
18
|
-
extendBackwards(): NodeRangeSelection;
|
|
19
|
-
extendForwards(): NodeRangeSelection;
|
|
20
|
-
static fromJSON(doc: ProseMirrorNode, json: any): NodeRangeSelection;
|
|
21
|
-
static create(doc: ProseMirrorNode, anchor: number, head: number, depth?: number, bias?: number): NodeRangeSelection;
|
|
22
|
-
getBookmark(): NodeRangeBookmark;
|
|
23
|
-
}
|
|
24
|
-
//# sourceMappingURL=NodeRangeSelection.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"NodeRangeSelection.d.ts","sourceRoot":"","sources":["../../src/helpers/NodeRangeSelection.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,IAAI,eAAe,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAA;AACvE,OAAO,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAA;AAC5C,OAAO,EAAE,OAAO,EAAE,MAAM,sBAAsB,CAAA;AAG9C,OAAO,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAA;AAE1D,qBAAa,kBAAmB,SAAQ,SAAS;IAE/C,KAAK,EAAE,MAAM,GAAG,SAAS,CAAA;gBAEb,OAAO,EAAE,WAAW,EAAE,KAAK,EAAE,WAAW,EAAE,KAAK,CAAC,EAAE,MAAM,EAAE,IAAI,SAAI;IAsC9E,IAAI,GAAG,gBAEN;IAED,EAAE,CAAC,KAAK,EAAE,SAAS,GAAG,OAAO;IAM7B,GAAG,CAAC,GAAG,EAAE,eAAe,EAAE,OAAO,EAAE,OAAO,GAAG,kBAAkB;IAO/D,MAAM;;;;;IAQN,IAAI,UAAU,IAAI,OAAO,CAExB;IAED,IAAI,WAAW,IAAI,OAAO,CAEzB;IAED,eAAe,IAAI,kBAAkB;IAiBrC,cAAc,IAAI,kBAAkB;IAiBpC,MAAM,CAAC,QAAQ,CAAC,GAAG,EAAE,eAAe,EAAE,IAAI,EAAE,GAAG,GAAG,kBAAkB;IAIpE,MAAM,CAAC,MAAM,CAAC,GAAG,EAAE,eAAe,EAAE,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,MAAM,EAAE,IAAI,SAAI,GAAG,kBAAkB;IAI/G,WAAW,IAAI,iBAAiB;CAIjC"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"getNodeRangeDecorations.d.ts","sourceRoot":"","sources":["../../src/helpers/getNodeRangeDecorations.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAA;AACjD,OAAO,EAAc,aAAa,EAAE,MAAM,iBAAiB,CAAA;AAE3D,wBAAgB,uBAAuB,CAAC,MAAM,EAAE,cAAc,EAAE,GAAG,aAAa,CAwB/E"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"getSelectionRanges.d.ts","sourceRoot":"","sources":["../../src/helpers/getSelectionRanges.ts"],"names":[],"mappings":"AAAA,OAAO,EAAa,WAAW,EAAE,MAAM,kBAAkB,CAAA;AACzD,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAA;AAEjD,wBAAgB,kBAAkB,CAChC,KAAK,EAAE,WAAW,EAClB,GAAG,EAAE,WAAW,EAChB,KAAK,CAAC,EAAE,MAAM,GACb,cAAc,EAAE,CA8BlB"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"isNodeRangeSelection.d.ts","sourceRoot":"","sources":["../../src/helpers/isNodeRangeSelection.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAA;AAE5D,wBAAgB,oBAAoB,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,kBAAkB,CAEhF"}
|
package/dist/index.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAA;AAE3C,cAAc,sCAAsC,CAAA;AACpD,cAAc,iCAAiC,CAAA;AAC/C,cAAc,mCAAmC,CAAA;AACjD,cAAc,iCAAiC,CAAA;AAC/C,cAAc,iBAAiB,CAAA;AAE/B,eAAe,SAAS,CAAA"}
|