@portabletext/editor 1.57.3 → 1.57.5

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.
Files changed (43) hide show
  1. package/lib/_chunks-cjs/selector.get-text-before.cjs +3 -3
  2. package/lib/_chunks-cjs/selector.get-text-before.cjs.map +1 -1
  3. package/lib/_chunks-cjs/selector.is-selecting-entire-blocks.cjs +114 -114
  4. package/lib/_chunks-cjs/selector.is-selecting-entire-blocks.cjs.map +1 -1
  5. package/lib/_chunks-cjs/{selector.get-focus-span.cjs → selector.is-selection-expanded.cjs} +25 -25
  6. package/lib/_chunks-cjs/selector.is-selection-expanded.cjs.map +1 -0
  7. package/lib/_chunks-es/selector.get-text-before.js +1 -1
  8. package/lib/_chunks-es/selector.is-selecting-entire-blocks.js +93 -93
  9. package/lib/_chunks-es/selector.is-selecting-entire-blocks.js.map +1 -1
  10. package/lib/_chunks-es/{selector.get-focus-span.js → selector.is-selection-expanded.js} +26 -26
  11. package/lib/_chunks-es/selector.is-selection-expanded.js.map +1 -0
  12. package/lib/index.cjs +143 -137
  13. package/lib/index.cjs.map +1 -1
  14. package/lib/index.js +108 -102
  15. package/lib/index.js.map +1 -1
  16. package/lib/plugins/index.cjs +16 -16
  17. package/lib/plugins/index.cjs.map +1 -1
  18. package/lib/plugins/index.js +1 -1
  19. package/lib/selectors/index.cjs +14 -14
  20. package/lib/selectors/index.cjs.map +1 -1
  21. package/lib/selectors/index.js +2 -2
  22. package/package.json +13 -13
  23. package/src/editor/Editable.tsx +1 -1
  24. package/src/editor/editor-dom.ts +1 -1
  25. package/src/editor/plugins/createWithEditableAPI.ts +1 -1
  26. package/src/editor/range-decorations-machine.ts +2 -1
  27. package/src/internal-utils/__tests__/ranges.test.ts +1 -1
  28. package/src/internal-utils/move-range-by-operation.ts +19 -0
  29. package/src/internal-utils/{ranges.test.ts → to-slate-range.test.ts} +101 -1
  30. package/src/internal-utils/to-slate-range.ts +171 -0
  31. package/src/operations/behavior.operation.block.set.ts +1 -1
  32. package/src/operations/behavior.operation.block.unset.ts +1 -1
  33. package/src/operations/behavior.operation.child.set.ts +1 -1
  34. package/src/operations/behavior.operation.child.unset.ts +1 -1
  35. package/src/operations/behavior.operation.decorator.add.ts +1 -1
  36. package/src/operations/behavior.operation.delete.ts +25 -1
  37. package/src/operations/behavior.operation.move.block.ts +34 -28
  38. package/src/operations/behavior.operation.select.ts +1 -1
  39. package/src/selectors/selector.get-mark-state.ts +3 -1
  40. package/lib/_chunks-cjs/selector.get-focus-span.cjs.map +0 -1
  41. package/lib/_chunks-es/selector.get-focus-span.js.map +0 -1
  42. package/src/internal-utils/paths.ts +0 -110
  43. package/src/internal-utils/ranges.ts +0 -70
@@ -1 +0,0 @@
1
- {"version":3,"file":"selector.get-focus-span.js","sources":["../../src/selectors/selector.get-focus-block.ts","../../src/selectors/selector.get-selection-start-point.ts","../../src/selectors/selector.get-focus-text-block.ts","../../src/selectors/selector.get-previous-inline-object.ts","../../src/selectors/selector.get-selected-value.ts","../../src/selectors/selector.get-selection-text.ts","../../src/selectors/selector.is-selection-collapsed.ts","../../src/selectors/selector.is-selection-expanded.ts","../../src/selectors/selector.get-focus-child.ts","../../src/selectors/selector.get-focus-span.ts"],"sourcesContent":["import type {PortableTextBlock} from '@sanity/types'\nimport type {EditorSelector} from '../editor/editor-selector'\nimport {getBlockKeyFromSelectionPoint} from '../selection/selection-point'\nimport type {BlockPath} from '../types/paths'\n\n/**\n * @public\n */\nexport const getFocusBlock: EditorSelector<\n {node: PortableTextBlock; path: BlockPath} | undefined\n> = (snapshot) => {\n if (!snapshot.context.selection) {\n return undefined\n }\n\n const key = getBlockKeyFromSelectionPoint(snapshot.context.selection.focus)\n const index = key ? snapshot.blockIndexMap.get(key) : undefined\n\n const node =\n index !== undefined ? snapshot.context.value.at(index) : undefined\n\n return node && key ? {node, path: [{_key: key}]} : undefined\n}\n","import type {EditorSelectionPoint} from '..'\nimport type {EditorSelector} from '../editor/editor-selector'\n\n/**\n * @public\n */\nexport const getSelectionStartPoint: EditorSelector<\n EditorSelectionPoint | undefined\n> = (snapshot) => {\n if (!snapshot.context.selection) {\n return undefined\n }\n\n return snapshot.context.selection.backward\n ? snapshot.context.selection.focus\n : snapshot.context.selection.anchor\n}\n","import type {PortableTextTextBlock} from '@sanity/types'\nimport type {EditorSelector} from '../editor/editor-selector'\nimport {isTextBlock} from '../internal-utils/parse-blocks'\nimport type {BlockPath} from '../types/paths'\nimport {getFocusBlock} from './selector.get-focus-block'\n\n/**\n * @public\n */\nexport const getFocusTextBlock: EditorSelector<\n {node: PortableTextTextBlock; path: BlockPath} | undefined\n> = (snapshot) => {\n const focusBlock = getFocusBlock(snapshot)\n\n return focusBlock && isTextBlock(snapshot.context, focusBlock.node)\n ? {node: focusBlock.node, path: focusBlock.path}\n : undefined\n}\n","import {isKeySegment, type PortableTextObject} from '@sanity/types'\nimport type {EditorSelector} from '../editor/editor-selector'\nimport type {ChildPath} from '../types/paths'\nimport {isSpan} from '../utils'\nimport {getFocusTextBlock} from './selector.get-focus-text-block'\nimport {getSelectionStartPoint} from './selector.get-selection-start-point'\n\n/**\n * @public\n */\nexport const getPreviousInlineObject: EditorSelector<\n | {\n node: PortableTextObject\n path: ChildPath\n }\n | undefined\n> = (snapshot) => {\n const focusTextBlock = getFocusTextBlock(snapshot)\n const selectionStartPoint = getSelectionStartPoint(snapshot)\n const selectionStartPointChildKey =\n selectionStartPoint && isKeySegment(selectionStartPoint.path[2])\n ? selectionStartPoint.path[2]._key\n : undefined\n\n if (!focusTextBlock || !selectionStartPointChildKey) {\n return undefined\n }\n\n let inlineObject:\n | {\n node: PortableTextObject\n path: ChildPath\n }\n | undefined\n\n for (const child of focusTextBlock.node.children) {\n if (child._key === selectionStartPointChildKey) {\n break\n }\n\n if (!isSpan(snapshot.context, child)) {\n inlineObject = {\n node: child,\n path: [...focusTextBlock.path, 'children', {_key: child._key}],\n }\n }\n }\n\n return inlineObject\n}\n","import type {PortableTextBlock} from '@sanity/types'\nimport type {EditorSelector} from '../editor/editor-selector'\nimport {getBlockKeyFromSelectionPoint} from '../selection/selection-point'\nimport {\n getSelectionEndPoint,\n getSelectionStartPoint,\n sliceBlocks,\n} from '../utils'\n\n/**\n * @public\n */\nexport const getSelectedValue: EditorSelector<Array<PortableTextBlock>> = (\n snapshot,\n) => {\n const selection = snapshot.context.selection\n\n if (!selection) {\n return []\n }\n\n const startPoint = getSelectionStartPoint(selection)\n const endPoint = getSelectionEndPoint(selection)\n const startBlockKey = getBlockKeyFromSelectionPoint(startPoint)\n const endBlockKey = getBlockKeyFromSelectionPoint(endPoint)\n\n if (!startBlockKey || !endBlockKey) {\n return []\n }\n\n const startBlockIndex = snapshot.blockIndexMap.get(startBlockKey)\n const endBlockIndex = snapshot.blockIndexMap.get(endBlockKey)\n\n if (startBlockIndex === undefined || endBlockIndex === undefined) {\n return []\n }\n\n const slicedValue = snapshot.context.value.slice(\n startBlockIndex,\n endBlockIndex + 1,\n )\n\n return sliceBlocks({\n context: snapshot.context,\n blocks: slicedValue,\n })\n}\n","import type {EditorSelector} from '../editor/editor-selector'\nimport {isSpan, isTextBlock} from '../internal-utils/parse-blocks'\nimport {getSelectedValue} from './selector.get-selected-value'\n\n/**\n * @public\n */\nexport const getSelectionText: EditorSelector<string> = (snapshot) => {\n const selectedValue = getSelectedValue(snapshot)\n\n return selectedValue.reduce((text, block) => {\n if (!isTextBlock(snapshot.context, block)) {\n return text\n }\n\n return (\n text +\n block.children.reduce((text, child) => {\n if (isSpan(snapshot.context, child)) {\n return text + child.text\n }\n\n return text\n }, '')\n )\n }, '')\n}\n","import type {EditorSelector} from '../editor/editor-selector'\n\n/**\n * @public\n */\nexport const isSelectionCollapsed: EditorSelector<boolean> = (snapshot) => {\n if (!snapshot.context.selection) {\n return false\n }\n\n return (\n JSON.stringify(snapshot.context.selection.anchor.path) ===\n JSON.stringify(snapshot.context.selection.focus.path) &&\n snapshot.context.selection?.anchor.offset ===\n snapshot.context.selection?.focus.offset\n )\n}\n","import type {EditorSelector} from '../editor/editor-selector'\nimport {isSelectionCollapsed} from './selector.is-selection-collapsed'\n\n/**\n * @public\n */\nexport const isSelectionExpanded: EditorSelector<boolean> = (snapshot) => {\n return !isSelectionCollapsed(snapshot)\n}\n","import type {PortableTextObject, PortableTextSpan} from '@sanity/types'\nimport type {EditorSelector} from '../editor/editor-selector'\nimport {getChildKeyFromSelectionPoint} from '../selection/selection-point'\nimport type {ChildPath} from '../types/paths'\nimport {getFocusTextBlock} from './selector.get-focus-text-block'\n\n/**\n * @public\n */\nexport const getFocusChild: EditorSelector<\n | {\n node: PortableTextObject | PortableTextSpan\n path: ChildPath\n }\n | undefined\n> = (snapshot) => {\n if (!snapshot.context.selection) {\n return undefined\n }\n\n const focusBlock = getFocusTextBlock(snapshot)\n\n if (!focusBlock) {\n return undefined\n }\n\n const key = getChildKeyFromSelectionPoint(snapshot.context.selection.focus)\n\n const node = key\n ? focusBlock.node.children.find((span) => span._key === key)\n : undefined\n\n return node && key\n ? {node, path: [...focusBlock.path, 'children', {_key: key}]}\n : undefined\n}\n","import type {PortableTextSpan} from '@sanity/types'\nimport type {EditorSelector} from '../editor/editor-selector'\nimport {isSpan} from '../internal-utils/parse-blocks'\nimport type {ChildPath} from '../types/paths'\nimport {getFocusChild} from './selector.get-focus-child'\n\n/**\n * @public\n */\nexport const getFocusSpan: EditorSelector<\n {node: PortableTextSpan; path: ChildPath} | undefined\n> = (snapshot) => {\n const focusChild = getFocusChild(snapshot)\n\n return focusChild && isSpan(snapshot.context, focusChild.node)\n ? {node: focusChild.node, path: focusChild.path}\n : undefined\n}\n"],"names":["getFocusBlock","snapshot","context","selection","key","getBlockKeyFromSelectionPoint","focus","index","blockIndexMap","get","undefined","node","value","at","path","_key","getSelectionStartPoint","backward","anchor","getFocusTextBlock","focusBlock","isTextBlock","getPreviousInlineObject","focusTextBlock","selectionStartPoint","selectionStartPointChildKey","isKeySegment","inlineObject","child","children","isSpan","getSelectedValue","startPoint","endPoint","getSelectionEndPoint","startBlockKey","endBlockKey","startBlockIndex","endBlockIndex","slicedValue","slice","sliceBlocks","blocks","getSelectionText","reduce","text","block","isSelectionCollapsed","JSON","stringify","offset","isSelectionExpanded","getFocusChild","getChildKeyFromSelectionPoint","find","span","getFocusSpan","focusChild"],"mappings":";;AAQO,MAAMA,gBAERC,CAAAA,aAAa;AAChB,MAAI,CAACA,SAASC,QAAQC;AACpB;AAGF,QAAMC,MAAMC,8BAA8BJ,SAASC,QAAQC,UAAUG,KAAK,GACpEC,QAAQH,MAAMH,SAASO,cAAcC,IAAIL,GAAG,IAAIM,QAEhDC,OACJJ,UAAUG,SAAYT,SAASC,QAAQU,MAAMC,GAAGN,KAAK,IAAIG;AAE3D,SAAOC,QAAQP,MAAM;AAAA,IAACO;AAAAA,IAAMG,MAAM,CAAC;AAAA,MAACC,MAAMX;AAAAA,IAAAA,CAAI;AAAA,EAAA,IAAKM;AACrD,GChBaM,yBAERf,CAAAA,aAAa;AAChB,MAAKA,SAASC,QAAQC;AAItB,WAAOF,SAASC,QAAQC,UAAUc,WAC9BhB,SAASC,QAAQC,UAAUG,QAC3BL,SAASC,QAAQC,UAAUe;AACjC,GCPaC,oBAERlB,CAAAA,aAAa;AAChB,QAAMmB,aAAapB,cAAcC,QAAQ;AAEzC,SAAOmB,cAAcC,YAAYpB,SAASC,SAASkB,WAAWT,IAAI,IAC9D;AAAA,IAACA,MAAMS,WAAWT;AAAAA,IAAMG,MAAMM,WAAWN;AAAAA,EAAAA,IACzCJ;AACN,GCPaY,0BAMRrB,CAAAA,aAAa;AAChB,QAAMsB,iBAAiBJ,kBAAkBlB,QAAQ,GAC3CuB,sBAAsBR,uBAAuBf,QAAQ,GACrDwB,8BACJD,uBAAuBE,aAAaF,oBAAoBV,KAAK,CAAC,CAAC,IAC3DU,oBAAoBV,KAAK,CAAC,EAAEC,OAC5BL;AAEN,MAAI,CAACa,kBAAkB,CAACE;AACtB;AAGF,MAAIE;AAOJ,aAAWC,SAASL,eAAeZ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e,OAAOJ,IAAI,MACnDkC,KAAKC,UAAUhD,SAASC,QAAQC,UAAUG,MAAMQ,IAAI,KACtDb,SAASC,QAAQC,WAAWe,OAAOgC,WACjCjD,SAASC,QAAQC,WAAWG,MAAM4C,SAP7B,ICDEC,sBAAgDlD,CAAAA,aACpD,CAAC8C,qBAAqB9C,QAAQ,GCE1BmD,gBAMRnD,CAAAA,aAAa;AAChB,MAAI,CAACA,SAASC,QAAQC;AACpB;AAGF,QAAMiB,aAAaD,kBAAkBlB,QAAQ;AAE7C,MAAI,CAACmB;AACH;AAGF,QAAMhB,MAAMiD,8BAA8BpD,SAASC,QAAQC,UAAUG,KAAK,GAEpEK,OAAOP,MACTgB,WAAWT,KAAKkB,SAASyB,KAAMC,UAASA,KAAKxC,SAASX,GAAG,IACzDM;AAEJ,SAAOC,QAAQP,MACX;AAAA,IAACO;AAAAA,IAAMG,MAAM,CAAC,GAAGM,WAAWN,MAAM,YAAY;AAAA,MAACC,MAAMX;AAAAA,IAAAA,CAAI;AAAA,EAAA,IACzDM;AACN,GC1Ba8C,eAERvD,CAAAA,aAAa;AAChB,QAAMwD,aAAaL,cAAcnD,QAAQ;AAEzC,SAAOwD,cAAc3B,SAAO7B,SAASC,SAASuD,WAAW9C,IAAI,IACzD;AAAA,IAACA,MAAM8C,WAAW9C;AAAAA,IAAMG,MAAM2C,WAAW3C;AAAAA,EAAAA,IACzCJ;AACN;"}
@@ -1,110 +0,0 @@
1
- import type {Path} from 'slate'
2
- import type {
3
- EditorContext,
4
- EditorSelectionPoint,
5
- EditorSnapshot,
6
- PortableTextBlock,
7
- PortableTextObject,
8
- PortableTextSpan,
9
- } from '..'
10
- import {
11
- getBlockKeyFromSelectionPoint,
12
- getChildKeyFromSelectionPoint,
13
- } from '../selection/selection-point'
14
- import {isSpan, isTextBlock} from './parse-blocks'
15
-
16
- export function toSlatePath(
17
- snapshot: {
18
- context: Pick<EditorContext, 'schema' | 'value'>
19
- } & Pick<EditorSnapshot, 'blockIndexMap'>,
20
- path: EditorSelectionPoint['path'],
21
- ): {
22
- block: PortableTextBlock | undefined
23
- child: PortableTextSpan | PortableTextObject | undefined
24
- path: Path
25
- } {
26
- const blockKey = getBlockKeyFromSelectionPoint({
27
- path,
28
- offset: 0,
29
- })
30
-
31
- if (!blockKey) {
32
- return {
33
- block: undefined,
34
- child: undefined,
35
- path: [],
36
- }
37
- }
38
-
39
- const blockIndex = snapshot.blockIndexMap.get(blockKey)
40
-
41
- if (blockIndex === undefined) {
42
- return {
43
- block: undefined,
44
- child: undefined,
45
- path: [],
46
- }
47
- }
48
-
49
- const block = snapshot.context.value.at(blockIndex)
50
-
51
- if (!block) {
52
- return {
53
- block: undefined,
54
- child: undefined,
55
- path: [],
56
- }
57
- }
58
-
59
- if (!isTextBlock(snapshot.context, block)) {
60
- return {
61
- block,
62
- child: undefined,
63
- path: [blockIndex, 0],
64
- }
65
- }
66
-
67
- const childKey = getChildKeyFromSelectionPoint({
68
- path,
69
- offset: 0,
70
- })
71
-
72
- if (!childKey) {
73
- return {
74
- block,
75
- child: undefined,
76
- path: [blockIndex, 0],
77
- }
78
- }
79
-
80
- let childPath: Array<number> = []
81
- let childIndex = -1
82
- let pathChild: PortableTextSpan | PortableTextObject | undefined = undefined
83
-
84
- for (const child of block.children) {
85
- childIndex++
86
- if (child._key === childKey) {
87
- pathChild = child
88
- if (isSpan(snapshot.context, child)) {
89
- childPath = [childIndex]
90
- } else {
91
- childPath = [childIndex, 0]
92
- }
93
- break
94
- }
95
- }
96
-
97
- if (childPath.length === 0) {
98
- return {
99
- block,
100
- child: undefined,
101
- path: [blockIndex, 0],
102
- }
103
- }
104
-
105
- return {
106
- block,
107
- child: pathChild,
108
- path: [blockIndex].concat(childPath),
109
- }
110
- }
@@ -1,70 +0,0 @@
1
- import {Point, type Operation, type Range} from 'slate'
2
- import type {EditorContext, EditorSnapshot} from '../editor/editor-snapshot'
3
- import {isSpan} from './parse-blocks'
4
- import {toSlatePath} from './paths'
5
-
6
- export function toSlateRange(
7
- snapshot: {
8
- context: Pick<EditorContext, 'schema' | 'value' | 'selection'>
9
- } & Pick<EditorSnapshot, 'blockIndexMap'>,
10
- ): Range | null {
11
- if (!snapshot.context.selection) {
12
- return null
13
- }
14
-
15
- const anchorPath = toSlatePath(
16
- snapshot,
17
- snapshot.context.selection.anchor.path,
18
- )
19
- const focusPath = toSlatePath(snapshot, snapshot.context.selection.focus.path)
20
-
21
- if (anchorPath.path.length === 0 || focusPath.path.length === 0) {
22
- return null
23
- }
24
-
25
- const anchorOffset = anchorPath.child
26
- ? isSpan(snapshot.context, anchorPath.child)
27
- ? Math.min(
28
- anchorPath.child.text.length,
29
- snapshot.context.selection.anchor.offset,
30
- )
31
- : 0
32
- : 0
33
- const focusOffset = focusPath.child
34
- ? isSpan(snapshot.context, focusPath.child)
35
- ? Math.min(
36
- focusPath.child.text.length,
37
- snapshot.context.selection.focus.offset,
38
- )
39
- : 0
40
- : 0
41
-
42
- return {
43
- anchor: {
44
- path: anchorPath.path,
45
- offset: anchorOffset,
46
- },
47
- focus: {
48
- path: focusPath.path,
49
- offset: focusOffset,
50
- },
51
- }
52
- }
53
-
54
- export function moveRangeByOperation(
55
- range: Range,
56
- operation: Operation,
57
- ): Range | null {
58
- const anchor = Point.transform(range.anchor, operation)
59
- const focus = Point.transform(range.focus, operation)
60
-
61
- if (anchor === null || focus === null) {
62
- return null
63
- }
64
-
65
- if (Point.equals(anchor, range.anchor) && Point.equals(focus, range.focus)) {
66
- return range
67
- }
68
-
69
- return {anchor, focus}
70
- }