@portabletext/editor 1.13.0 → 1.14.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (74) hide show
  1. package/README.md +1 -1
  2. package/lib/_chunks-cjs/selector.get-text-before.cjs +320 -0
  3. package/lib/_chunks-cjs/selector.get-text-before.cjs.map +1 -0
  4. package/lib/_chunks-es/selector.get-text-before.js +321 -0
  5. package/lib/_chunks-es/selector.get-text-before.js.map +1 -0
  6. package/lib/{index.esm.js → index.cjs} +1703 -1431
  7. package/lib/index.cjs.map +1 -0
  8. package/lib/{index.d.mts → index.d.cts} +4038 -313
  9. package/lib/index.d.ts +4038 -313
  10. package/lib/index.js +1724 -1407
  11. package/lib/index.js.map +1 -1
  12. package/lib/selectors/index.cjs +35 -0
  13. package/lib/selectors/index.cjs.map +1 -0
  14. package/lib/selectors/index.d.cts +243 -0
  15. package/lib/selectors/index.d.ts +243 -0
  16. package/lib/selectors/index.js +36 -0
  17. package/lib/selectors/index.js.map +1 -0
  18. package/package.json +21 -13
  19. package/src/editor/Editable.tsx +1 -1
  20. package/src/editor/PortableTextEditor.tsx +19 -4
  21. package/src/editor/__tests__/handleClick.test.tsx +4 -4
  22. package/src/editor/behavior/behavior.action.insert-block-object.ts +1 -1
  23. package/src/editor/behavior/behavior.action.insert-break.ts +3 -3
  24. package/src/editor/behavior/behavior.action.insert-inline-object.ts +58 -0
  25. package/src/editor/behavior/behavior.action.insert-span.ts +1 -1
  26. package/src/editor/behavior/behavior.action.list-item.ts +100 -0
  27. package/src/editor/behavior/behavior.action.style.ts +108 -0
  28. package/src/editor/behavior/behavior.action.text-block.set.ts +25 -0
  29. package/src/editor/behavior/behavior.action.text-block.unset.ts +17 -0
  30. package/src/editor/behavior/behavior.actions.ts +178 -109
  31. package/src/editor/behavior/behavior.code-editor.ts +30 -40
  32. package/src/editor/behavior/behavior.core.block-objects.ts +26 -26
  33. package/src/editor/behavior/behavior.core.decorators.ts +9 -6
  34. package/src/editor/behavior/behavior.core.lists.ts +139 -17
  35. package/src/editor/behavior/behavior.core.ts +5 -2
  36. package/src/editor/behavior/behavior.guards.ts +28 -0
  37. package/src/editor/behavior/behavior.links.ts +7 -7
  38. package/src/editor/behavior/behavior.markdown.ts +68 -79
  39. package/src/editor/behavior/behavior.types.ts +86 -60
  40. package/src/editor/{use-editor.ts → create-editor.ts} +13 -8
  41. package/src/editor/editor-event-listener.tsx +2 -2
  42. package/src/editor/editor-machine.ts +54 -15
  43. package/src/editor/editor-provider.tsx +5 -5
  44. package/src/editor/editor-selector.ts +49 -0
  45. package/src/editor/editor-snapshot.ts +22 -0
  46. package/src/editor/get-value.ts +11 -0
  47. package/src/editor/plugins/create-with-event-listeners.ts +93 -5
  48. package/src/editor/plugins/createWithEditableAPI.ts +69 -20
  49. package/src/editor/plugins/createWithHotKeys.ts +0 -54
  50. package/src/editor/plugins/createWithPortableTextBlockStyle.ts +1 -55
  51. package/src/editor/plugins/with-plugins.ts +4 -8
  52. package/src/editor/{behavior/behavior.utils.block-offset.test.ts → utils/utils.block-offset.test.ts} +1 -1
  53. package/src/editor/{behavior/behavior.utils.block-offset.ts → utils/utils.block-offset.ts} +1 -8
  54. package/src/editor/{behavior/behavior.utils.reverse-selection.ts → utils/utils.reverse-selection.ts} +3 -5
  55. package/src/editor/utils/utils.ts +21 -0
  56. package/src/index.ts +13 -13
  57. package/src/selectors/index.ts +15 -0
  58. package/src/selectors/selector.get-active-list-item.ts +37 -0
  59. package/src/{editor/behavior/behavior.utils.get-selection-text.ts → selectors/selector.get-selection-text.ts} +10 -15
  60. package/src/selectors/selector.get-text-before.ts +41 -0
  61. package/src/selectors/selectors.ts +329 -0
  62. package/src/types/editor.ts +0 -60
  63. package/src/utils/is-hotkey.test.ts +2 -0
  64. package/src/utils/operationToPatches.ts +5 -0
  65. package/src/utils/paths.ts +4 -11
  66. package/src/utils/ranges.ts +3 -3
  67. package/lib/index.esm.js.map +0 -1
  68. package/lib/index.mjs +0 -7541
  69. package/lib/index.mjs.map +0 -1
  70. package/src/editor/behavior/behavior.utils.ts +0 -218
  71. package/src/editor/behavior/behavior.utilts.get-text-before.ts +0 -31
  72. package/src/editor/plugins/createWithPortableTextLists.ts +0 -172
  73. /package/src/editor/{behavior/behavior.utils.get-start-point.ts → utils/utils.get-start-point.ts} +0 -0
  74. /package/src/editor/{behavior/behavior.utils.is-keyed-segment.ts → utils/utils.is-keyed-segment.ts} +0 -0
@@ -133,66 +133,6 @@ export interface PortableTextSlateEditor extends ReactEditor {
133
133
  isTextSpan: (value: unknown) => value is PortableTextSpan
134
134
  isListBlock: (value: unknown) => value is PortableTextListBlock
135
135
 
136
- /**
137
- * Increments selected list items levels, or decrements them if `reverse` is true.
138
- *
139
- * @param reverse - if true, decrement instead of incrementing
140
- * @returns True if anything was incremented in the selection
141
- */
142
- pteIncrementBlockLevels: (reverse?: boolean) => boolean
143
-
144
- /**
145
- * Toggle selected blocks as listItem
146
- *
147
- * @param listStyle - Style of list item to toggle on/off
148
- */
149
- pteToggleListItem: (listStyle: string) => void
150
-
151
- /**
152
- * Set selected block as listItem
153
- *
154
- * @param listStyle - Style of list item to set
155
- */
156
- pteSetListItem: (listStyle: string) => void
157
-
158
- /**
159
- * Unset selected block as listItem
160
- *
161
- * @param listStyle - Style of list item to unset
162
- */
163
- pteUnsetListItem: (listStyle: string) => void
164
-
165
- /**
166
- * Ends a list
167
- *
168
- * @returns True if a list was ended in the selection
169
- */
170
- pteEndList: () => boolean
171
-
172
- /**
173
- * Toggle the selected block style
174
- *
175
- * @param style - The style name
176
- *
177
- */
178
- pteToggleBlockStyle: (style: string) => void
179
-
180
- /**
181
- * Test if the current selection has a certain block style
182
- *
183
- * @param style - The style name
184
- *
185
- */
186
- pteHasBlockStyle: (style: string) => boolean
187
-
188
- /**
189
- * Test if the current selection has a certain list style
190
- *
191
- * @param listStyle - Style name to check whether or not the selection has
192
- *
193
- */
194
- pteHasListStyle: (style: string) => boolean
195
-
196
136
  /**
197
137
  * Try to expand the current selection to a word
198
138
  */
@@ -91,6 +91,8 @@ const testCases: TestCase[] = [
91
91
  ['Enter', e('Enter'), true],
92
92
  ['enter', e(13), true],
93
93
  ['Enter', e(13), true],
94
+ ['enter', e('Enter', ['shiftKey']), false],
95
+ ['Enter', e('Enter', ['shiftKey']), false],
94
96
 
95
97
  ['cmd+=', e(187, ['metaKey']), true],
96
98
  ['Cmd+=', e(187, ['metaKey']), true],
@@ -438,6 +438,11 @@ export function createOperationToPatches(
438
438
  const patches: Patch[] = []
439
439
  const block = beforeValue[operation.path[0]]
440
440
  const targetBlock = beforeValue[operation.newPath[0]]
441
+
442
+ if (!targetBlock) {
443
+ return patches
444
+ }
445
+
441
446
  if (operation.path.length === 1) {
442
447
  const position: InsertPosition =
443
448
  operation.path[0] > operation.newPath[0] ? 'before' : 'after'
@@ -7,10 +7,7 @@ import {
7
7
  type Point,
8
8
  type Path as SlatePath,
9
9
  } from 'slate'
10
- import type {
11
- EditorSelectionPoint,
12
- PortableTextMemberSchemaTypes,
13
- } from '../types/editor'
10
+ import type {PortableTextMemberSchemaTypes} from '../types/editor'
14
11
  import type {ObjectWithKeyAndType} from './ranges'
15
12
 
16
13
  export function createKeyedPath(
@@ -41,10 +38,7 @@ export function createKeyedPath(
41
38
  ) as Path
42
39
  }
43
40
 
44
- export function createArrayedPath(
45
- point: EditorSelectionPoint,
46
- editor: Editor,
47
- ): SlatePath {
41
+ export function toSlatePath(path: Path, editor: Editor): SlatePath {
48
42
  if (!editor) {
49
43
  return []
50
44
  }
@@ -52,8 +46,7 @@ export function createArrayedPath(
52
46
  Editor.nodes(editor, {
53
47
  at: [],
54
48
  match: (n) =>
55
- isKeySegment(point.path[0]) &&
56
- (n as Descendant)._key === point.path[0]._key,
49
+ isKeySegment(path[0]) && (n as Descendant)._key === path[0]._key,
57
50
  }),
58
51
  )[0] || [undefined, undefined]
59
52
  if (!block || !Element.isElement(block)) {
@@ -62,7 +55,7 @@ export function createArrayedPath(
62
55
  if (editor.isVoid(block)) {
63
56
  return [blockPath[0], 0]
64
57
  }
65
- const childPath = [point.path[2]]
58
+ const childPath = [path[2]]
66
59
  const childIndex = block.children.findIndex((child) =>
67
60
  isEqual([{_key: child._key}], childPath),
68
61
  )
@@ -4,7 +4,7 @@ import type {
4
4
  EditorSelectionPoint,
5
5
  PortableTextMemberSchemaTypes,
6
6
  } from '../types/editor'
7
- import {createArrayedPath, createKeyedPath} from './paths'
7
+ import {createKeyedPath, toSlatePath} from './paths'
8
8
 
9
9
  export interface ObjectWithKeyAndType {
10
10
  _key: string
@@ -50,11 +50,11 @@ export function toSlateRange(
50
50
  return null
51
51
  }
52
52
  const anchor = {
53
- path: createArrayedPath(selection.anchor, editor),
53
+ path: toSlatePath(selection.anchor.path, editor),
54
54
  offset: selection.anchor.offset,
55
55
  }
56
56
  const focus = {
57
- path: createArrayedPath(selection.focus, editor),
57
+ path: toSlatePath(selection.focus.path, editor),
58
58
  offset: selection.focus.offset,
59
59
  }
60
60
  if (focus.path.length === 0 || anchor.path.length === 0) {