@portabletext/editor 2.21.3 → 3.0.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 (95) hide show
  1. package/lib/_chunks-dts/index.d.ts +49 -209
  2. package/lib/_chunks-es/selector.is-at-the-start-of-block.js +103 -20
  3. package/lib/_chunks-es/selector.is-at-the-start-of-block.js.map +1 -1
  4. package/lib/_chunks-es/{util.get-text-block-text.js → util.slice-blocks.js} +73 -24
  5. package/lib/_chunks-es/util.slice-blocks.js.map +1 -0
  6. package/lib/_chunks-es/util.slice-text-block.js +13 -2
  7. package/lib/_chunks-es/util.slice-text-block.js.map +1 -1
  8. package/lib/behaviors/index.d.ts +1 -1
  9. package/lib/index.d.ts +2 -2
  10. package/lib/index.js +339 -341
  11. package/lib/index.js.map +1 -1
  12. package/lib/plugins/index.d.ts +2 -133
  13. package/lib/plugins/index.js +2 -796
  14. package/lib/plugins/index.js.map +1 -1
  15. package/lib/selectors/index.d.ts +2 -24
  16. package/lib/selectors/index.js +28 -130
  17. package/lib/selectors/index.js.map +1 -1
  18. package/lib/utils/index.d.ts +6 -4
  19. package/lib/utils/index.js +98 -9
  20. package/lib/utils/index.js.map +1 -1
  21. package/package.json +1 -3
  22. package/src/behaviors/behavior.abstract.split.ts +1 -0
  23. package/src/behaviors/behavior.perform-event.ts +7 -7
  24. package/src/converters/converter.portable-text.ts +1 -0
  25. package/src/converters/converter.text-html.ts +1 -0
  26. package/src/converters/converter.text-plain.ts +1 -0
  27. package/src/editor/Editable.tsx +1 -0
  28. package/src/editor/PortableTextEditor.tsx +0 -19
  29. package/src/editor/create-editor.ts +0 -3
  30. package/src/editor/editor-machine.ts +0 -10
  31. package/src/editor/event-to-change.tsx +5 -1
  32. package/src/editor/plugins/create-with-event-listeners.ts +30 -6
  33. package/src/editor/plugins/createWithObjectKeys.ts +2 -1
  34. package/src/editor/plugins/createWithPatches.ts +3 -3
  35. package/src/editor/plugins/createWithPlaceholderBlock.ts +2 -1
  36. package/src/editor/plugins/createWithPortableTextMarkModel.ts +2 -1
  37. package/src/editor/plugins/with-plugins.ts +10 -14
  38. package/src/editor/relay-machine.ts +0 -4
  39. package/src/editor/sync-machine.ts +2 -2
  40. package/src/editor.ts +0 -4
  41. package/src/history/behavior.operation.history.redo.ts +67 -0
  42. package/src/history/behavior.operation.history.undo.ts +71 -0
  43. package/src/history/event.history.undo.test.tsx +672 -0
  44. package/src/history/history.preserving-keys.test.tsx +112 -0
  45. package/src/history/remote-patches.ts +20 -0
  46. package/src/history/slate-plugin.history.ts +146 -0
  47. package/src/history/slate-plugin.redoing.ts +21 -0
  48. package/src/history/slate-plugin.undoing.ts +21 -0
  49. package/src/history/slate-plugin.without-history.ts +23 -0
  50. package/src/history/transform-operation.ts +245 -0
  51. package/src/history/undo-redo-collaboration.test.tsx +541 -0
  52. package/src/history/undo-redo.feature +125 -0
  53. package/src/history/undo-redo.test.tsx +195 -0
  54. package/src/history/undo-step.ts +148 -0
  55. package/src/index.ts +0 -1
  56. package/src/internal-utils/operation-to-patches.test.ts +23 -25
  57. package/src/internal-utils/operation-to-patches.ts +31 -22
  58. package/src/internal-utils/selection-text.test.ts +3 -0
  59. package/src/internal-utils/selection-text.ts +5 -2
  60. package/src/internal-utils/values.ts +23 -11
  61. package/src/operations/behavior.operation.block.set.ts +1 -0
  62. package/src/operations/behavior.operation.block.unset.ts +2 -0
  63. package/src/operations/behavior.operation.insert.block.ts +1 -0
  64. package/src/operations/behavior.operations.ts +2 -4
  65. package/src/plugins/index.ts +0 -3
  66. package/src/selectors/index.ts +0 -3
  67. package/src/test/vitest/step-definitions.tsx +57 -0
  68. package/src/test/vitest/test-editor.tsx +1 -1
  69. package/src/utils/parse-blocks.test.ts +296 -16
  70. package/src/utils/parse-blocks.ts +81 -22
  71. package/src/utils/util.merge-text-blocks.ts +5 -1
  72. package/src/utils/util.slice-blocks.ts +24 -10
  73. package/lib/_chunks-es/selector.get-selection-text.js +0 -92
  74. package/lib/_chunks-es/selector.get-selection-text.js.map +0 -1
  75. package/lib/_chunks-es/selector.get-text-before.js +0 -36
  76. package/lib/_chunks-es/selector.get-text-before.js.map +0 -1
  77. package/lib/_chunks-es/util.get-text-block-text.js.map +0 -1
  78. package/lib/_chunks-es/util.is-empty-text-block.js +0 -40
  79. package/lib/_chunks-es/util.is-empty-text-block.js.map +0 -1
  80. package/lib/_chunks-es/util.merge-text-blocks.js +0 -101
  81. package/lib/_chunks-es/util.merge-text-blocks.js.map +0 -1
  82. package/src/editor/plugins/createWithMaxBlocks.ts +0 -53
  83. package/src/editor/plugins/createWithUndoRedo.ts +0 -628
  84. package/src/editor/with-undo-step.ts +0 -37
  85. package/src/editor/withUndoRedo.ts +0 -34
  86. package/src/editor-event-listener.tsx +0 -28
  87. package/src/plugins/plugin.decorator-shortcut.ts +0 -238
  88. package/src/plugins/plugin.markdown.test.tsx +0 -42
  89. package/src/plugins/plugin.markdown.tsx +0 -131
  90. package/src/plugins/plugin.one-line.tsx +0 -123
  91. package/src/selectors/selector.get-list-state.test.ts +0 -189
  92. package/src/selectors/selector.get-list-state.ts +0 -96
  93. package/src/selectors/selector.get-selected-slice.ts +0 -13
  94. package/src/selectors/selector.get-trimmed-selection.test.ts +0 -657
  95. package/src/selectors/selector.get-trimmed-selection.ts +0 -189
@@ -0,0 +1,67 @@
1
+ import {flatten} from 'lodash'
2
+ import {Editor, Transforms} from 'slate'
3
+ import {debugWithName} from '../internal-utils/debug'
4
+ import type {BehaviorOperationImplementation} from '../operations/behavior.operations'
5
+ import {getRemotePatches} from './remote-patches'
6
+ import {pluginRedoing, setIsRedoing} from './slate-plugin.redoing'
7
+ import {
8
+ pluginWithoutHistory,
9
+ setWithHistory,
10
+ } from './slate-plugin.without-history'
11
+ import {transformOperation} from './transform-operation'
12
+
13
+ const debug = debugWithName('behavior.operation.history.redo')
14
+
15
+ export const historyRedoOperationImplementation: BehaviorOperationImplementation<
16
+ 'history.redo'
17
+ > = ({operation}) => {
18
+ const editor = operation.editor
19
+ const {redos} = editor.history
20
+ const remotePatches = getRemotePatches(editor)
21
+
22
+ if (redos.length > 0) {
23
+ const step = redos[redos.length - 1]
24
+ debug('Redoing', step)
25
+ if (step.operations.length > 0) {
26
+ const otherPatches = remotePatches.filter(
27
+ (item) => item.time >= step.timestamp,
28
+ )
29
+ let transformedOperations = step.operations
30
+ otherPatches.forEach((item) => {
31
+ transformedOperations = flatten(
32
+ transformedOperations.map((op) =>
33
+ transformOperation(
34
+ editor,
35
+ item.patch,
36
+ op,
37
+ item.snapshot,
38
+ item.previousSnapshot,
39
+ ),
40
+ ),
41
+ )
42
+ })
43
+ try {
44
+ Editor.withoutNormalizing(editor, () => {
45
+ pluginRedoing(editor, () => {
46
+ pluginWithoutHistory(editor, () => {
47
+ transformedOperations.forEach((op) => {
48
+ editor.apply(op)
49
+ })
50
+ })
51
+ })
52
+ })
53
+ } catch (err) {
54
+ debug('Could not perform redo step', err)
55
+ remotePatches.splice(0, remotePatches.length)
56
+ Transforms.deselect(editor)
57
+ editor.history = {undos: [], redos: []}
58
+ setWithHistory(editor, true)
59
+ setIsRedoing(editor, false)
60
+ editor.onChange()
61
+ return
62
+ }
63
+ editor.history.undos.push(step)
64
+ editor.history.redos.pop()
65
+ }
66
+ }
67
+ }
@@ -0,0 +1,71 @@
1
+ import {flatten} from 'lodash'
2
+ import {Editor, Operation, Transforms} from 'slate'
3
+ import {debugWithName} from '../internal-utils/debug'
4
+ import type {BehaviorOperationImplementation} from '../operations/behavior.operations'
5
+ import {getRemotePatches} from './remote-patches'
6
+ import {pluginUndoing, setIsUndoing} from './slate-plugin.undoing'
7
+ import {
8
+ pluginWithoutHistory,
9
+ setWithHistory,
10
+ } from './slate-plugin.without-history'
11
+ import {transformOperation} from './transform-operation'
12
+
13
+ const debug = debugWithName('behavior.operation.history.undo')
14
+
15
+ export const historyUndoOperationImplementation: BehaviorOperationImplementation<
16
+ 'history.undo'
17
+ > = ({operation}) => {
18
+ const editor = operation.editor
19
+ const {undos} = editor.history
20
+ const remotePatches = getRemotePatches(editor)
21
+
22
+ if (undos.length > 0) {
23
+ const step = undos[undos.length - 1]
24
+ debug('Undoing', step)
25
+ if (step.operations.length > 0) {
26
+ const otherPatches = remotePatches.filter(
27
+ (item) => item.time >= step.timestamp,
28
+ )
29
+ let transformedOperations = step.operations
30
+ otherPatches.forEach((item) => {
31
+ transformedOperations = flatten(
32
+ transformedOperations.map((op) =>
33
+ transformOperation(
34
+ editor,
35
+ item.patch,
36
+ op,
37
+ item.snapshot,
38
+ item.previousSnapshot,
39
+ ),
40
+ ),
41
+ )
42
+ })
43
+ const reversedOperations = transformedOperations
44
+ .map(Operation.inverse)
45
+ .reverse()
46
+
47
+ try {
48
+ Editor.withoutNormalizing(editor, () => {
49
+ pluginUndoing(editor, () => {
50
+ pluginWithoutHistory(editor, () => {
51
+ reversedOperations.forEach((op) => {
52
+ editor.apply(op)
53
+ })
54
+ })
55
+ })
56
+ })
57
+ } catch (err) {
58
+ debug('Could not perform undo step', err)
59
+ remotePatches.splice(0, remotePatches.length)
60
+ Transforms.deselect(editor)
61
+ editor.history = {undos: [], redos: []}
62
+ setWithHistory(editor, true)
63
+ setIsUndoing(editor, false)
64
+ editor.onChange()
65
+ return
66
+ }
67
+ editor.history.redos.push(step)
68
+ editor.history.undos.pop()
69
+ }
70
+ }
71
+ }