@portabletext/editor 2.21.3 → 3.0.0

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 (78) 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} +29 -5
  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 +297 -320
  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 +3 -3
  19. package/lib/utils/index.js +97 -9
  20. package/lib/utils/index.js.map +1 -1
  21. package/package.json +3 -5
  22. package/src/behaviors/behavior.perform-event.ts +7 -7
  23. package/src/editor/PortableTextEditor.tsx +0 -19
  24. package/src/editor/create-editor.ts +0 -3
  25. package/src/editor/editor-machine.ts +0 -10
  26. package/src/editor/event-to-change.tsx +5 -1
  27. package/src/editor/plugins/create-with-event-listeners.ts +0 -4
  28. package/src/editor/plugins/createWithObjectKeys.ts +2 -1
  29. package/src/editor/plugins/createWithPatches.ts +3 -3
  30. package/src/editor/plugins/createWithPlaceholderBlock.ts +2 -1
  31. package/src/editor/plugins/createWithPortableTextMarkModel.ts +2 -1
  32. package/src/editor/plugins/with-plugins.ts +10 -14
  33. package/src/editor/relay-machine.ts +0 -4
  34. package/src/editor/sync-machine.ts +2 -2
  35. package/src/editor.ts +0 -4
  36. package/src/history/behavior.operation.history.redo.ts +67 -0
  37. package/src/history/behavior.operation.history.undo.ts +71 -0
  38. package/src/history/event.history.undo.test.tsx +672 -0
  39. package/src/history/history.preserving-keys.test.tsx +112 -0
  40. package/src/history/remote-patches.ts +20 -0
  41. package/src/history/slate-plugin.history.ts +146 -0
  42. package/src/history/slate-plugin.redoing.ts +21 -0
  43. package/src/history/slate-plugin.undoing.ts +21 -0
  44. package/src/history/slate-plugin.without-history.ts +23 -0
  45. package/src/history/transform-operation.ts +245 -0
  46. package/src/history/undo-redo-collaboration.test.tsx +541 -0
  47. package/src/history/undo-redo.feature +125 -0
  48. package/src/history/undo-redo.test.tsx +195 -0
  49. package/src/history/undo-step.ts +148 -0
  50. package/src/index.ts +0 -1
  51. package/src/operations/behavior.operations.ts +2 -4
  52. package/src/plugins/index.ts +0 -3
  53. package/src/selectors/index.ts +0 -3
  54. package/src/test/vitest/step-definitions.tsx +55 -0
  55. package/src/test/vitest/test-editor.tsx +1 -1
  56. package/lib/_chunks-es/selector.get-selection-text.js +0 -92
  57. package/lib/_chunks-es/selector.get-selection-text.js.map +0 -1
  58. package/lib/_chunks-es/selector.get-text-before.js +0 -36
  59. package/lib/_chunks-es/selector.get-text-before.js.map +0 -1
  60. package/lib/_chunks-es/util.get-text-block-text.js.map +0 -1
  61. package/lib/_chunks-es/util.is-empty-text-block.js +0 -40
  62. package/lib/_chunks-es/util.is-empty-text-block.js.map +0 -1
  63. package/lib/_chunks-es/util.merge-text-blocks.js +0 -101
  64. package/lib/_chunks-es/util.merge-text-blocks.js.map +0 -1
  65. package/src/editor/plugins/createWithMaxBlocks.ts +0 -53
  66. package/src/editor/plugins/createWithUndoRedo.ts +0 -628
  67. package/src/editor/with-undo-step.ts +0 -37
  68. package/src/editor/withUndoRedo.ts +0 -34
  69. package/src/editor-event-listener.tsx +0 -28
  70. package/src/plugins/plugin.decorator-shortcut.ts +0 -238
  71. package/src/plugins/plugin.markdown.test.tsx +0 -42
  72. package/src/plugins/plugin.markdown.tsx +0 -131
  73. package/src/plugins/plugin.one-line.tsx +0 -123
  74. package/src/selectors/selector.get-list-state.test.ts +0 -189
  75. package/src/selectors/selector.get-list-state.ts +0 -96
  76. package/src/selectors/selector.get-selected-slice.ts +0 -13
  77. package/src/selectors/selector.get-trimmed-selection.test.ts +0 -657
  78. package/src/selectors/selector.get-trimmed-selection.ts +0 -189
@@ -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
+ }