@magic-marker/prosemirror-suggest-changes 0.2.1-wrap-unwrap.2 → 0.3.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 (100) hide show
  1. package/dist/__tests__/playwrightBaseTest.d.ts +6 -0
  2. package/dist/__tests__/playwrightPage.d.ts +15 -0
  3. package/dist/commands.js +1 -10
  4. package/dist/constants.d.ts +1 -0
  5. package/dist/constants.js +1 -0
  6. package/dist/decorations.js +4 -3
  7. package/dist/ensureSelectionPlugin.d.ts +13 -0
  8. package/dist/ensureSelectionPlugin.js +306 -0
  9. package/dist/generateId.js +2 -2
  10. package/dist/index.d.ts +2 -1
  11. package/dist/index.js +2 -1
  12. package/dist/plugin.js +2 -14
  13. package/dist/prependDeletionsWithZWSP.d.ts +2 -0
  14. package/dist/prependDeletionsWithZWSP.js +45 -0
  15. package/dist/replaceAroundStep.js +1 -6
  16. package/dist/replaceStep.js +3 -7
  17. package/dist/schema.d.ts +4 -2
  18. package/dist/schema.js +32 -39
  19. package/dist/testing/testBuilders.d.ts +2 -1
  20. package/dist/utils.d.ts +0 -1
  21. package/dist/utils.js +2 -6
  22. package/dist/withSuggestChanges.js +4 -0
  23. package/package.json +1 -1
  24. package/dist/contentBetween.d.ts +0 -2
  25. package/dist/contentBetween.js +0 -33
  26. package/dist/features/wrapUnwrap/__tests__/blockquoteUnwrapAllNodes.data.d.ts +0 -38
  27. package/dist/features/wrapUnwrap/__tests__/blockquoteUnwrapOneNode.data.d.ts +0 -45
  28. package/dist/features/wrapUnwrap/__tests__/blockquoteUnwrapOneNode.test.d.ts +0 -1
  29. package/dist/features/wrapUnwrap/__tests__/blockquoteUnwrapSingleNode.data.d.ts +0 -38
  30. package/dist/features/wrapUnwrap/__tests__/blockquoteUnwrapSingleNode.playwright.test.d.ts +0 -1
  31. package/dist/features/wrapUnwrap/__tests__/blockquoteUnwrapSingleNode.test.d.ts +0 -1
  32. package/dist/features/wrapUnwrap/__tests__/blockquoteWrapAllNodes.data.d.ts +0 -38
  33. package/dist/features/wrapUnwrap/__tests__/blockquoteWrapAllNodes.playwright.test.d.ts +0 -1
  34. package/dist/features/wrapUnwrap/__tests__/blockquoteWrapAllNodes.test.d.ts +0 -1
  35. package/dist/features/wrapUnwrap/__tests__/blockquoteWrapSingleNode.data.d.ts +0 -38
  36. package/dist/features/wrapUnwrap/__tests__/blockquoteWrapSingleNode.playwright.test.d.ts +0 -1
  37. package/dist/features/wrapUnwrap/__tests__/blockquoteWrapSingleNode.test.d.ts +0 -1
  38. package/dist/features/wrapUnwrap/__tests__/listItemLiftLast.data.d.ts +0 -54
  39. package/dist/features/wrapUnwrap/__tests__/listItemLiftLast.playwright.test.d.ts +0 -1
  40. package/dist/features/wrapUnwrap/__tests__/listItemLiftLast.test.d.ts +0 -1
  41. package/dist/features/wrapUnwrap/__tests__/listItemLiftMiddle.data.d.ts +0 -48
  42. package/dist/features/wrapUnwrap/__tests__/listItemLiftMiddle.playwright.test.d.ts +0 -1
  43. package/dist/features/wrapUnwrap/__tests__/listItemLiftMiddle.test.d.ts +0 -1
  44. package/dist/features/wrapUnwrap/__tests__/listItemLiftMultipleToTheTop.data.d.ts +0 -74
  45. package/dist/features/wrapUnwrap/__tests__/listItemLiftMultipleToTheTop.playwright.test.d.ts +0 -1
  46. package/dist/features/wrapUnwrap/__tests__/listItemLiftMultipleToTheTop.test.d.ts +0 -1
  47. package/dist/features/wrapUnwrap/__tests__/listItemLiftNestedOnce.data.d.ts +0 -71
  48. package/dist/features/wrapUnwrap/__tests__/listItemLiftNestedOnce.playwright.test.d.ts +0 -1
  49. package/dist/features/wrapUnwrap/__tests__/listItemLiftNestedOnce.test.d.ts +0 -1
  50. package/dist/features/wrapUnwrap/__tests__/listItemLiftTop.data.d.ts +0 -54
  51. package/dist/features/wrapUnwrap/__tests__/listItemLiftTop.playwright.test.d.ts +0 -1
  52. package/dist/features/wrapUnwrap/__tests__/listItemLiftTop.test.d.ts +0 -1
  53. package/dist/features/wrapUnwrap/__tests__/listItemSinkMultiple.data.d.ts +0 -221
  54. package/dist/features/wrapUnwrap/__tests__/listItemSinkMultiple.page.d.ts +0 -16
  55. package/dist/features/wrapUnwrap/__tests__/listItemSinkMultiple.playwright.test.d.ts +0 -1
  56. package/dist/features/wrapUnwrap/__tests__/listItemSinkMultiple.test.d.ts +0 -1
  57. package/dist/features/wrapUnwrap/__tests__/listItemSinkOneOnce.data.d.ts +0 -51
  58. package/dist/features/wrapUnwrap/__tests__/listItemSinkOneOnce.playwright.test.d.ts +0 -1
  59. package/dist/features/wrapUnwrap/__tests__/listItemSinkOneOnce.test.d.ts +0 -1
  60. package/dist/features/wrapUnwrap/__tests__/listItemsLiftMixedLevels.data.d.ts +0 -74
  61. package/dist/features/wrapUnwrap/__tests__/listItemsLiftMixedLevels.playwright.test.d.ts +0 -1
  62. package/dist/features/wrapUnwrap/__tests__/listItemsLiftMixedLevels.test.d.ts +0 -1
  63. package/dist/features/wrapUnwrap/__tests__/nestedListItemLiftToOuter.data.d.ts +0 -71
  64. package/dist/features/wrapUnwrap/__tests__/nestedListItemLiftToOuter.playwright.test.d.ts +0 -1
  65. package/dist/features/wrapUnwrap/__tests__/nestedListItemLiftToOuter.test.d.ts +0 -1
  66. package/dist/features/wrapUnwrap/__tests__/nestedListItemsLiftMultipleLevels.data.d.ts +0 -114
  67. package/dist/features/wrapUnwrap/__tests__/nestedListItemsLiftMultipleLevels.playwright.test.d.ts +0 -1
  68. package/dist/features/wrapUnwrap/__tests__/nestedListItemsLiftMultipleLevels.test.d.ts +0 -1
  69. package/dist/features/wrapUnwrap/__tests__/testUtils.d.ts +0 -5
  70. package/dist/features/wrapUnwrap/__tests__/tripleBlockquoteLiftMultipleNodesToTheTop.data.d.ts +0 -44
  71. package/dist/features/wrapUnwrap/__tests__/tripleBlockquoteLiftMultipleNodesToTheTop.playwright.test.d.ts +0 -1
  72. package/dist/features/wrapUnwrap/__tests__/tripleBlockquoteLiftMultipleNodesToTheTop.test.d.ts +0 -1
  73. package/dist/features/wrapUnwrap/__tests__/tripleBlockquoteLiftMultipleNodesUp.data.d.ts +0 -38
  74. package/dist/features/wrapUnwrap/__tests__/tripleBlockquoteLiftMultipleNodesUp.playwright.test.d.ts +0 -1
  75. package/dist/features/wrapUnwrap/__tests__/tripleBlockquoteLiftMultipleNodesUp.test.d.ts +0 -1
  76. package/dist/features/wrapUnwrap/__tests__/tripleBlockquoteLiftOneNodeFromMiddle.data.d.ts +0 -46
  77. package/dist/features/wrapUnwrap/__tests__/tripleBlockquoteLiftOneNodeFromMiddle.playwright.test.d.ts +0 -1
  78. package/dist/features/wrapUnwrap/__tests__/tripleBlockquoteLiftOneNodeFromMiddle.test.d.ts +0 -1
  79. package/dist/features/wrapUnwrap/__tests__/tripleBlockquoteLiftOneNodeToTheTop.data.d.ts +0 -57
  80. package/dist/features/wrapUnwrap/__tests__/tripleBlockquoteLiftOneNodeToTheTop.playwright.test.d.ts +0 -1
  81. package/dist/features/wrapUnwrap/__tests__/tripleBlockquoteLiftOneNodeToTheTop.test.d.ts +0 -1
  82. package/dist/features/wrapUnwrap/__tests__/tripleBlockquoteLiftOneNodeUp.data.d.ts +0 -45
  83. package/dist/features/wrapUnwrap/__tests__/tripleBlockquoteLiftOneNodeUp.playwright.test.d.ts +0 -1
  84. package/dist/features/wrapUnwrap/__tests__/tripleBlockquoteLiftOneNodeUp.test.d.ts +0 -1
  85. package/dist/features/wrapUnwrap/__tests__/tripleBlockquoteWrap.data.d.ts +0 -44
  86. package/dist/features/wrapUnwrap/__tests__/tripleBlockquoteWrap.playwright.test.d.ts +0 -1
  87. package/dist/features/wrapUnwrap/__tests__/tripleBlockquoteWrap.test.d.ts +0 -1
  88. package/dist/features/wrapUnwrap/__tests__/tripleBlockquoteWrapMultiple.data.d.ts +0 -44
  89. package/dist/features/wrapUnwrap/__tests__/tripleBlockquoteWrapMultiple.playwright.test.d.ts +0 -1
  90. package/dist/features/wrapUnwrap/__tests__/tripleBlockquoteWrapMultiple.test.d.ts +0 -1
  91. package/dist/features/wrapUnwrap/handleStructureStep.d.ts +0 -4
  92. package/dist/features/wrapUnwrap/handleStructureStep.js +0 -630
  93. package/dist/features/wrapUnwrap/revertStructureSuggestion.d.ts +0 -44
  94. package/dist/features/wrapUnwrap/revertStructureSuggestion.js +0 -368
  95. package/dist/rebaseStep.d.ts +0 -9
  96. package/dist/rebaseStep.js +0 -11
  97. package/src/features/wrapUnwrap/README.md +0 -198
  98. /package/dist/features/{wrapUnwrap/__tests__/blockquoteUnwrapAllNodes.playwright.test.d.ts → hiddenDeletions/__tests__/hiddenDeletionsBehavior.playwright.test.d.ts} +0 -0
  99. /package/dist/features/{wrapUnwrap/__tests__/blockquoteUnwrapAllNodes.test.d.ts → joinBlocks/__tests__/joinBlockBackspace.playwright.test.d.ts} +0 -0
  100. /package/dist/features/{wrapUnwrap/__tests__/blockquoteUnwrapOneNode.playwright.test.d.ts → joinBlocks/__tests__/joinBlockDelete.playwright.test.d.ts} +0 -0
@@ -0,0 +1,6 @@
1
+ import { expect } from "@playwright/test";
2
+ export interface TestOptions {
3
+ deletionMarksVisibility: "hidden" | "visible";
4
+ }
5
+ export declare const test: import("@playwright/test").TestType<import("@playwright/test").PlaywrightTestArgs & import("@playwright/test").PlaywrightTestOptions & TestOptions, import("@playwright/test").PlaywrightWorkerArgs & import("@playwright/test").PlaywrightWorkerOptions>;
6
+ export { expect };
@@ -0,0 +1,15 @@
1
+ import { type Locator, type Page } from "@playwright/test";
2
+ export declare class EditorPage {
3
+ readonly page: Page;
4
+ private readonly selectors;
5
+ constructor(page: Page);
6
+ get editor(): Locator;
7
+ getParagraphText(index: number): Promise<string>;
8
+ getParagraphCount(): Promise<number>;
9
+ getProseMirrorMarkCount(name: string): Promise<number>;
10
+ getProseMirrorSelection(): Promise<{
11
+ anchor: number;
12
+ head: number;
13
+ }>;
14
+ getDOMTextContentOfChildAtIndex(index: number): Promise<string>;
15
+ }
package/dist/commands.js CHANGED
@@ -5,7 +5,6 @@ import { suggestChangesKey } from "./plugin.js";
5
5
  import { getSuggestionMarks } from "./utils.js";
6
6
  import { ZWSP } from "./constants.js";
7
7
  import { maybeRevertJoinMark } from "./features/joinOnDelete/index.js";
8
- import { applyStructureSuggestion, isStructureSuggestion, revertAllStructureSuggestions, revertStructureSuggestion } from "./features/wrapUnwrap/revertStructureSuggestion.js";
9
8
  /**
10
9
  * Given a node and a transform, add a set of steps to the
11
10
  * transform that applies all marks of type markTypeToApply
@@ -194,9 +193,6 @@ export function applySuggestionsToRange(doc, from, to) {
194
193
  * contents left in the doc.
195
194
  */ export function applySuggestion(suggestionId, from, to) {
196
195
  return (state, dispatch)=>{
197
- if (isStructureSuggestion(suggestionId, state.tr)) {
198
- return applyStructureSuggestion(suggestionId)(state, dispatch);
199
- }
200
196
  const { deletion, insertion } = getSuggestionMarks(state.schema);
201
197
  const tr = state.tr;
202
198
  applySuggestionsToTransform(state.doc, tr, insertion, deletion, suggestionId, from, to);
@@ -218,9 +214,7 @@ export function applySuggestionsToRange(doc, from, to) {
218
214
  */ export function revertSuggestions(state, dispatch) {
219
215
  const { deletion, insertion } = getSuggestionMarks(state.schema);
220
216
  const tr = state.tr;
221
- const doc = state.doc;
222
- revertAllStructureSuggestions(doc, tr);
223
- applySuggestionsToTransform(doc, tr, deletion, insertion);
217
+ applySuggestionsToTransform(state.doc, tr, deletion, insertion);
224
218
  applyModificationsToTransform(tr.doc, tr, -1);
225
219
  tr.setMeta(suggestChangesKey, {
226
220
  skip: true
@@ -255,9 +249,6 @@ export function applySuggestionsToRange(doc, from, to) {
255
249
  * Modifications tracked in modification marks will be reverted.
256
250
  */ export function revertSuggestion(suggestionId, from, to) {
257
251
  return (state, dispatch)=>{
258
- if (isStructureSuggestion(suggestionId, state.tr)) {
259
- return revertStructureSuggestion(suggestionId)(state, dispatch);
260
- }
261
252
  const { deletion, insertion } = getSuggestionMarks(state.schema);
262
253
  const tr = state.tr;
263
254
  applySuggestionsToTransform(state.doc, tr, deletion, insertion, suggestionId, from, to);
@@ -1 +1,2 @@
1
1
  export declare const ZWSP = "\u200B";
2
+ export declare const PILCROW = "\u00B6";
package/dist/constants.js CHANGED
@@ -1 +1,2 @@
1
1
  export const ZWSP = "\u200B";
2
+ export const PILCROW = "¶";
@@ -1,8 +1,9 @@
1
1
  import { Decoration, DecorationSet } from "prosemirror-view";
2
2
  import { getSuggestionMarks } from "./utils.js";
3
+ import { PILCROW } from "./constants.js";
3
4
  function pilcrow() {
4
5
  const span = document.createElement("span");
5
- span.appendChild(document.createTextNode("¶"));
6
+ span.appendChild(document.createTextNode(PILCROW));
6
7
  return span;
7
8
  }
8
9
  export function getSuggestionDecorations(state) {
@@ -43,7 +44,7 @@ export function getSuggestionDecorations(state) {
43
44
  if (currentDeletionMark?.attrs["id"] !== lastDeletionMark?.attrs["id"] && currentInsertionMark?.attrs["id"] !== lastInsertionMark?.attrs["id"]) {
44
45
  return true;
45
46
  }
46
- if (currentDeletionMark) {
47
+ if (currentDeletionMark && currentDeletionMark.attrs["id"] === lastDeletionMark?.attrs["id"]) {
47
48
  changeDecorations.push(Decoration.widget(widgetPos, pilcrow, {
48
49
  // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
49
50
  key: currentDeletionMark.attrs["id"],
@@ -55,7 +56,7 @@ export function getSuggestionDecorations(state) {
55
56
  ]
56
57
  }));
57
58
  }
58
- if (currentInsertionMark) {
59
+ if (currentInsertionMark && currentInsertionMark.attrs["id"] === lastInsertionMark?.attrs["id"]) {
59
60
  changeDecorations.push(Decoration.widget(widgetPos, pilcrow, {
60
61
  // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
61
62
  key: currentInsertionMark.attrs["id"],
@@ -0,0 +1,13 @@
1
+ import { Plugin, PluginKey } from "prosemirror-state";
2
+ interface PluginState {
3
+ handleKeyDown: {
4
+ backspace: boolean;
5
+ delete: boolean;
6
+ arrowLeft: boolean;
7
+ arrowRight: boolean;
8
+ };
9
+ }
10
+ export declare const ensureSelectionKey: PluginKey<PluginState>;
11
+ export declare function ensureSelection(): Plugin<PluginState>;
12
+ export declare function isEnsureSelectionEnabled(): boolean;
13
+ export {};
@@ -0,0 +1,306 @@
1
+ import { Plugin, PluginKey, TextSelection } from "prosemirror-state";
2
+ import { getSuggestionMarks } from "./utils.js";
3
+ import { ZWSP } from "./constants.js";
4
+ const TRACE_ENABLED = false;
5
+ function trace(...args) {
6
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
7
+ if (!TRACE_ENABLED) return;
8
+ console.log("[ensureSelectionPlugin]", ...args);
9
+ }
10
+ export const ensureSelectionKey = new PluginKey("@handlewithcare/prosemirror-suggest-changes-ensure-selection");
11
+ export function ensureSelection() {
12
+ return new Plugin({
13
+ key: ensureSelectionKey,
14
+ state: {
15
+ apply (tr, state) {
16
+ const newPluginState = tr.getMeta(ensureSelectionKey);
17
+ if (newPluginState) {
18
+ return newPluginState;
19
+ }
20
+ return state;
21
+ },
22
+ init () {
23
+ return {
24
+ handleKeyDown: {
25
+ backspace: false,
26
+ delete: false,
27
+ arrowLeft: false,
28
+ arrowRight: false
29
+ }
30
+ };
31
+ }
32
+ },
33
+ props: {
34
+ handleKeyDown (view, event) {
35
+ const state = this.getState(view.state);
36
+ if (!state) return;
37
+ const newState = {
38
+ ...state
39
+ };
40
+ newState.handleKeyDown.backspace = event.key === "Backspace";
41
+ newState.handleKeyDown.delete = event.key === "Delete";
42
+ newState.handleKeyDown.arrowLeft = event.key === "ArrowLeft";
43
+ newState.handleKeyDown.arrowRight = event.key === "ArrowRight";
44
+ if (newState.handleKeyDown.backspace !== state.handleKeyDown.backspace || newState.handleKeyDown.delete !== state.handleKeyDown.delete || newState.handleKeyDown.arrowLeft !== state.handleKeyDown.arrowLeft || newState.handleKeyDown.arrowRight !== state.handleKeyDown.arrowRight) {
45
+ trace("handleKeyDown newState =", newState);
46
+ view.dispatch(view.state.tr.setMeta(ensureSelectionKey, newState));
47
+ }
48
+ }
49
+ },
50
+ appendTransaction (_transactions, oldState, newState) {
51
+ const pluginState = ensureSelectionKey.getState(newState);
52
+ if (!(newState.selection instanceof TextSelection)) {
53
+ return null;
54
+ }
55
+ if (isPosValid(newState.selection.$anchor) && isPosValid(newState.selection.$head)) {
56
+ return null;
57
+ }
58
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
59
+ if (TRACE_ENABLED) console.groupCollapsed("[ensureSelectionPlugin]", "appendTransaction");
60
+ trace("appendTransaction", "search for new valid $anchor...");
61
+ let $newAnchor = getNewValidPos(newState.selection.$anchor, getDirection(oldState.selection.$anchor, newState.selection.$anchor, pluginState));
62
+ trace("appendTransaction", "new valid $anchor", $newAnchor?.pos, {
63
+ $newAnchor
64
+ });
65
+ trace("appendTransaction", "search for new valid $head...");
66
+ let $newHead = getNewValidPos(newState.selection.$head, getDirection(oldState.selection.$head, newState.selection.$head, pluginState));
67
+ trace("appendTransaction", "new valid $head", $newHead?.pos, {
68
+ $newHead
69
+ });
70
+ $newAnchor = $newAnchor ?? newState.selection.$anchor;
71
+ $newHead = $newHead ?? newState.selection.$head;
72
+ const newSelection = new TextSelection($newAnchor, $newHead);
73
+ if (newSelection.anchor === newState.selection.anchor && newSelection.head === newState.selection.head) {
74
+ trace("appendTransaction", "new selection is the same as old selection, skipping", {
75
+ $newAnchor,
76
+ $newHead,
77
+ selection: newSelection
78
+ });
79
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
80
+ if (TRACE_ENABLED) console.groupEnd();
81
+ return null;
82
+ }
83
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
84
+ if (TRACE_ENABLED) console.groupEnd();
85
+ trace("appendTransaction", "setting new selection", $newAnchor.pos, $newHead.pos, {
86
+ $newAnchor,
87
+ $newHead,
88
+ selection: newSelection
89
+ });
90
+ return newState.tr.setSelection(newSelection);
91
+ }
92
+ });
93
+ }
94
+ export function isEnsureSelectionEnabled() {
95
+ return true;
96
+ }
97
+ function isPosValid($pos) {
98
+ // text selection is only valid in nodes that allow inline content
99
+ // https://github.com/ProseMirror/prosemirror-state/blob/1.4.4/src/selection.ts#L219
100
+ if (!$pos.parent.inlineContent) {
101
+ trace("isPosValid", $pos.pos, "pos invalid", "reason: not in inlineContent node", {
102
+ $pos
103
+ });
104
+ return false;
105
+ }
106
+ const { deletion, insertion } = getSuggestionMarks($pos.doc.type.schema);
107
+ const deletionBefore = deletion.isInSet($pos.nodeBefore?.marks ?? []);
108
+ const deletionAfter = deletion.isInSet($pos.nodeAfter?.marks ?? []);
109
+ const isAnchorBefore = deletionBefore && deletionBefore.attrs["type"] === "anchor";
110
+ const isAnchorAfter = deletionAfter && deletionAfter.attrs["type"] === "anchor";
111
+ if (isAnchorBefore && deletionAfter && !isAnchorAfter) {
112
+ trace("isPosValid", $pos.pos, "pos invalid", "reason: between deletion anchor and non-anchor deletion", {
113
+ $pos
114
+ });
115
+ return false;
116
+ }
117
+ if (deletionBefore && deletionAfter && !isAnchorBefore && !isAnchorAfter) {
118
+ trace("isPosValid", $pos.pos, "pos invalid", "reason: between two non-anchor deletions", {
119
+ $pos
120
+ });
121
+ return false;
122
+ }
123
+ if ($pos.nodeBefore == null && deletionAfter && !isAnchorAfter) {
124
+ trace("isPosValid", $pos.pos, "pos invalid", "reason: between node boundary and non-anchor deletion", {
125
+ $pos
126
+ });
127
+ return false;
128
+ }
129
+ if (deletionBefore && $pos.nodeAfter == null && !isAnchorBefore) {
130
+ trace("isPosValid", $pos.pos, "pos invalid", "reason: between non-anchor deletion and node boundary", {
131
+ $pos
132
+ });
133
+ return false;
134
+ }
135
+ if (deletionBefore && !isAnchorBefore && $pos.nodeAfter == null) {
136
+ trace("isPosValid", $pos.pos, "pos invalid", "reason: between non-anchor deletion and node boundary", {
137
+ $pos
138
+ });
139
+ return false;
140
+ }
141
+ if (deletionBefore && !isAnchorBefore) {
142
+ trace("isPosValid", $pos.pos, "pos invalid", "reason: between non-anchor deletion and anything", {
143
+ $pos
144
+ });
145
+ return false;
146
+ }
147
+ const insertionBefore = insertion.isInSet($pos.nodeBefore?.marks ?? []);
148
+ const insertionAfter = insertion.isInSet($pos.nodeAfter?.marks ?? []);
149
+ const ZWSP_REGEXP = new RegExp(ZWSP, "g");
150
+ const isZWSPBefore = $pos.nodeBefore && $pos.nodeBefore.textContent.replace(ZWSP_REGEXP, "") === "";
151
+ const isZWSPAfter = $pos.nodeAfter && $pos.nodeAfter.textContent.replace(ZWSP_REGEXP, "") === "";
152
+ if (insertionBefore && insertionAfter && isZWSPBefore && isZWSPAfter) {
153
+ trace("isPosValid", $pos.pos, "pos invalid", "reason: between two ZWSP insertions", {
154
+ $pos
155
+ });
156
+ return false;
157
+ }
158
+ if (insertionBefore && isZWSPBefore && $pos.nodeAfter == null && // a position like this:
159
+ // <p><insertion>ZWSP</insertion>|</p>
160
+ // because it means this paragraph was just created and it's empty
161
+ $pos.parent.textContent.replace(ZWSP_REGEXP, "") !== "") {
162
+ trace("isPosValid", $pos.pos, "pos invalid", "reason: between ZWSP insertion and right node boundary", {
163
+ $pos
164
+ });
165
+ return false;
166
+ }
167
+ if (insertionAfter && isZWSPAfter && $pos.nodeBefore == null) {
168
+ trace("isPosValid", $pos.pos, "pos invalid", "reason: between ZWSP insertion and left node boundary", {
169
+ $pos
170
+ });
171
+ return false;
172
+ }
173
+ return true;
174
+ }
175
+ function findNextValidPos($initialPos) {
176
+ let $pos = $initialPos;
177
+ // to keep searching for the next valid pos we need non-null nodeAfter so we can go right or non-root depth so we can go up
178
+ while(!isPosValid($pos) && ($pos.nodeAfter != null || $pos.depth > 0)){
179
+ // first check if we can go into nodeAfter
180
+ if ($pos.nodeAfter != null) {
181
+ // if nodeAfter is inline, we can step into it and search for the valid pos in it
182
+ if ($pos.nodeAfter.isInline) {
183
+ // nodeAfter is inline - move in by one
184
+ $pos = $pos.doc.resolve($pos.pos + 1);
185
+ } else {
186
+ // nodeAfter is not inline - find starting position of the first inline descendant in nodeAfter
187
+ let localStartPos = null;
188
+ $pos.nodeAfter.descendants((child, pos)=>{
189
+ if (!child.isInline) return true;
190
+ if (localStartPos !== null) return false;
191
+ localStartPos = pos;
192
+ return false;
193
+ });
194
+ if (localStartPos !== null) {
195
+ // we have a local starting position of the first inline descendant - convert it to global position
196
+ // +1 to "enter" the node, and add local pos
197
+ $pos = $pos.doc.resolve($pos.pos + 1 + localStartPos);
198
+ } else {
199
+ // unable to find first inline descendant of nodeAfter - just skip nodeAfter altogether
200
+ $pos = $pos.doc.resolve($pos.pos + $pos.nodeAfter.nodeSize);
201
+ }
202
+ }
203
+ } else if ($pos.depth > 0) {
204
+ // nodeAfter is null - go up
205
+ $pos = $pos.doc.resolve($pos.after());
206
+ }
207
+ }
208
+ return isPosValid($pos) ? $pos : null;
209
+ }
210
+ function findPreviousValidPos($initialPos) {
211
+ let $pos = $initialPos;
212
+ // in order to be able to keep searching, we need either nodeBefore so we can go left, or non-root depth so we can go up
213
+ while(!isPosValid($pos) && ($pos.nodeBefore != null || $pos.depth > 0)){
214
+ // first check if we can go into nodeBefore
215
+ if ($pos.nodeBefore != null) {
216
+ // if nodeBefore is inline, we can step into it and search for the valid pos in it
217
+ if ($pos.nodeBefore.isInline) {
218
+ // nodeBefore is inline - move in by one
219
+ $pos = $pos.doc.resolve($pos.pos - 1);
220
+ } else {
221
+ // nodeBefore is not inline - find ending position of the last inline descendant in nodeBefore
222
+ let localEndPos = null;
223
+ $pos.nodeBefore.descendants((child, pos)=>{
224
+ if (!child.isInline) return true;
225
+ localEndPos = pos + child.nodeSize;
226
+ return false;
227
+ });
228
+ if (localEndPos !== null) {
229
+ // we have a local ending position of the last inline descendant - convert it to global position
230
+ // move pos to start of node before, add 1 to "enter" nodeBefore, then add local pos
231
+ $pos = $pos.doc.resolve($pos.pos - $pos.nodeBefore.nodeSize + 1 + localEndPos);
232
+ } else {
233
+ // unable to find last inline descendant of nodeBefore - just skip nodeBefore altogether
234
+ $pos = $pos.doc.resolve($pos.pos - $pos.nodeBefore.nodeSize);
235
+ }
236
+ }
237
+ } else if ($pos.depth > 0) {
238
+ // nodeBefore is null - go up
239
+ $pos = $pos.doc.resolve($pos.before());
240
+ }
241
+ }
242
+ return isPosValid($pos) ? $pos : null;
243
+ }
244
+ function getNewValidPos($pos, dir) {
245
+ if (isPosValid($pos)) return $pos;
246
+ trace("getNewValidPos for", $pos.pos, {
247
+ $pos,
248
+ dir
249
+ });
250
+ if (dir === "right") {
251
+ const $nextValidPos = findNextValidPos($pos);
252
+ trace("getNewValidPos", "$nextValidPos", $nextValidPos?.pos, {
253
+ dir,
254
+ $pos,
255
+ $nextValidPos
256
+ });
257
+ if ($nextValidPos != null) return $nextValidPos;
258
+ const $prevValidPos = findPreviousValidPos($pos);
259
+ trace("getNewValidPos", "$prevValidPos", $prevValidPos?.pos, {
260
+ dir,
261
+ $pos,
262
+ $prevValidPos
263
+ });
264
+ if ($prevValidPos != null) return $prevValidPos;
265
+ return null;
266
+ }
267
+ if (dir === "left") {
268
+ const $prevValidPos = findPreviousValidPos($pos);
269
+ trace("getNewValidPos", "$prevValidPos", $prevValidPos?.pos, {
270
+ dir,
271
+ $pos,
272
+ $prevValidPos
273
+ });
274
+ if ($prevValidPos != null) return $prevValidPos;
275
+ const $nextValidPos = findNextValidPos($pos);
276
+ trace("getNewValidPos", "$nextValidPos", $nextValidPos?.pos, {
277
+ dir,
278
+ $pos,
279
+ $nextValidPos
280
+ });
281
+ if ($nextValidPos != null) return $nextValidPos;
282
+ return null;
283
+ }
284
+ const $nextValidPos = findNextValidPos($pos);
285
+ const $prevValidPos = findPreviousValidPos($pos);
286
+ trace("getNewValidPos", "$nextValidPos", $nextValidPos?.pos, "$prevValidPos", $prevValidPos?.pos, {
287
+ dir,
288
+ $pos,
289
+ $nextValidPos,
290
+ $prevValidPos
291
+ });
292
+ if ($nextValidPos == null && $prevValidPos == null) {
293
+ return null;
294
+ }
295
+ if ($nextValidPos == null) return $prevValidPos;
296
+ if ($prevValidPos == null) return $nextValidPos;
297
+ const nextDist = Math.abs($pos.pos - $nextValidPos.pos);
298
+ const prevDist = Math.abs($pos.pos - $prevValidPos.pos);
299
+ return nextDist <= prevDist ? $nextValidPos : $prevValidPos;
300
+ }
301
+ function getDirection($oldPos, $newPos, pluginState) {
302
+ if (pluginState?.handleKeyDown.backspace) return "left";
303
+ if ($newPos.pos > $oldPos.pos) return "right";
304
+ if ($newPos.pos < $oldPos.pos) return "left";
305
+ return null;
306
+ }
@@ -8,12 +8,12 @@ export function parseSuggestionId(id) {
8
8
  return parsed;
9
9
  }
10
10
  export function generateNextNumberId(schema, doc) {
11
- const { deletion, insertion, modification, structure } = getSuggestionMarks(schema);
11
+ const { deletion, insertion, modification } = getSuggestionMarks(schema);
12
12
  // Find the highest change id in the document so far,
13
13
  // and use that as the starting point for new changes
14
14
  let suggestionId = 0;
15
15
  doc?.descendants((node)=>{
16
- const mark = node.marks.find((mark)=>mark.type === insertion || mark.type === deletion || mark.type === modification || mark.type === structure);
16
+ const mark = node.marks.find((mark)=>mark.type === insertion || mark.type === deletion || mark.type === modification);
17
17
  if (mark) {
18
18
  suggestionId = Math.max(suggestionId, mark.attrs["id"]);
19
19
  return false;
package/dist/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
- export { addSuggestionMarks, insertion, deletion, modification, structure, } from "./schema.js";
1
+ export { addSuggestionMarks, insertion, deletion, modification, hiddenDeletion, } from "./schema.js";
2
2
  export { selectSuggestion, revertSuggestion, revertSuggestions, applySuggestion, applySuggestions, enableSuggestChanges, disableSuggestChanges, toggleSuggestChanges, } from "./commands.js";
3
3
  export { suggestChanges, suggestChangesKey, isSuggestChangesEnabled, } from "./plugin.js";
4
4
  export { withSuggestChanges, transformToSuggestionTransaction, } from "./withSuggestChanges.js";
5
+ export { ensureSelection as experimental_ensureSelection, ensureSelectionKey as experimental_ensureSelectionKey, isEnsureSelectionEnabled as experimental_isEnsureSelectionEnabled, } from "./ensureSelectionPlugin.js";
package/dist/index.js CHANGED
@@ -1,4 +1,5 @@
1
- export { addSuggestionMarks, insertion, deletion, modification, structure } from "./schema.js";
1
+ export { addSuggestionMarks, insertion, deletion, modification, hiddenDeletion } from "./schema.js";
2
2
  export { selectSuggestion, revertSuggestion, revertSuggestions, applySuggestion, applySuggestions, enableSuggestChanges, disableSuggestChanges, toggleSuggestChanges } from "./commands.js";
3
3
  export { suggestChanges, suggestChangesKey, isSuggestChangesEnabled } from "./plugin.js";
4
4
  export { withSuggestChanges, transformToSuggestionTransaction } from "./withSuggestChanges.js";
5
+ export { ensureSelection as experimental_ensureSelection, ensureSelectionKey as experimental_ensureSelectionKey, isEnsureSelectionEnabled as experimental_isEnsureSelectionEnabled } from "./ensureSelectionPlugin.js";
package/dist/plugin.js CHANGED
@@ -1,4 +1,4 @@
1
- import { Plugin, PluginKey, TextSelection } from "prosemirror-state";
1
+ import { Plugin, PluginKey } from "prosemirror-state";
2
2
  import { getSuggestionDecorations } from "./decorations.js";
3
3
  export const suggestChangesKey = new PluginKey("@handlewithcare/prosemirror-suggest-changes");
4
4
  export function suggestChanges() {
@@ -17,19 +17,7 @@ export function suggestChanges() {
17
17
  }
18
18
  },
19
19
  props: {
20
- decorations: getSuggestionDecorations,
21
- // Add a custom keydown handler that skips over any zero-width
22
- // spaces that we've inserted so that users aren't aware of them
23
- handleKeyDown (view, event) {
24
- if (event.key === "ArrowRight" && view.state.selection instanceof TextSelection && view.state.selection.empty && view.state.selection.$cursor?.nodeAfter?.text?.startsWith("\u200B")) {
25
- view.dispatch(view.state.tr.setSelection(TextSelection.create(view.state.doc, view.state.selection.$cursor.pos + 1)));
26
- }
27
- if (event.key === "ArrowLeft" && view.state.selection instanceof TextSelection && view.state.selection.empty && view.state.selection.$cursor?.nodeBefore?.text?.endsWith("\u200B")) {
28
- view.dispatch(view.state.tr.setSelection(TextSelection.create(view.state.doc, view.state.selection.$cursor.pos - 1)));
29
- }
30
- // Never block any other handlers from running after
31
- return false;
32
- }
20
+ decorations: getSuggestionDecorations
33
21
  }
34
22
  });
35
23
  }
@@ -0,0 +1,2 @@
1
+ import { type Transaction } from "prosemirror-state";
2
+ export declare function prependDeletionsWithZWSP(transaction: Transaction): Transaction;
@@ -0,0 +1,45 @@
1
+ import { getSuggestionMarks } from "./utils.js";
2
+ import { Transform } from "prosemirror-transform";
3
+ import { ZWSP } from "./constants.js";
4
+ const TRACE_ENABLED = false;
5
+ function trace(...args) {
6
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
7
+ if (!TRACE_ENABLED) return;
8
+ console.log("[prependDeletionsWithZWSP]", ...args);
9
+ }
10
+ export function prependDeletionsWithZWSP(transaction) {
11
+ const { deletion } = getSuggestionMarks(transaction.doc.type.schema);
12
+ let transform = new Transform(transaction.doc);
13
+ transform.doc.descendants((node, pos)=>{
14
+ const mark = node.marks.find((mark)=>mark.type === deletion && mark.attrs["type"] === "anchor");
15
+ if (!node.isText || mark == null) return true;
16
+ const mappedPos = transform.mapping.map(pos);
17
+ transform.delete(mappedPos, mappedPos + node.nodeSize);
18
+ return true;
19
+ });
20
+ transform.steps.forEach((step)=>{
21
+ transaction.step(step);
22
+ });
23
+ if (transform.steps.length > 0) trace(`added ${String(transform.steps.length)} remove zwsp steps to tr`, transform);
24
+ transform = new Transform(transaction.doc);
25
+ transform.doc.descendants((node, pos)=>{
26
+ if (!node.inlineContent) return true;
27
+ const mark = node.firstChild?.marks.find((mark)=>mark.type === deletion);
28
+ if (mark == null) return true;
29
+ if (mark.attrs["type"] === "anchor") return true;
30
+ const zwspNode = transform.doc.type.schema.text(ZWSP, [
31
+ deletion.create({
32
+ type: "anchor",
33
+ id: mark.attrs["id"]
34
+ })
35
+ ]);
36
+ const mappedPos = transform.mapping.map(pos);
37
+ transform.insert(mappedPos + 1, zwspNode);
38
+ return true;
39
+ });
40
+ transform.steps.forEach((step)=>{
41
+ transaction.step(step);
42
+ });
43
+ if (transform.steps.length > 0) trace(`added ${String(transform.steps.length)} add zwsp steps to tr`, transform);
44
+ return transaction;
45
+ }
@@ -6,7 +6,6 @@ import { suggestRemoveNodeMarkStep } from "./removeNodeMarkStep.js";
6
6
  import { suggestReplaceStep } from "./replaceStep.js";
7
7
  import { getSuggestionMarks } from "./utils.js";
8
8
  import { applySuggestionsToRange } from "./commands.js";
9
- import { handleStructureStep } from "./features/wrapUnwrap/handleStructureStep.js";
10
9
  /**
11
10
  * This detects and handles changes from `setNodeMarkup` so that these are tracked as a modification
12
11
  * instead of a deletion + insertion
@@ -81,11 +80,7 @@ import { handleStructureStep } from "./features/wrapUnwrap/handleStructureStep.j
81
80
  * equivalent replace step will be generated, and then processed via
82
81
  * trackReplaceStep().
83
82
  */ export function suggestReplaceAroundStep(trackedTransaction, state, doc, step, prevSteps, suggestionId) {
84
- let handled = handleStructureStep(trackedTransaction, state, step, prevSteps, suggestionId);
85
- if (handled) {
86
- return true;
87
- }
88
- handled = suggestSetNodeMarkup(trackedTransaction, state, doc, step, prevSteps, suggestionId);
83
+ const handled = suggestSetNodeMarkup(trackedTransaction, state, doc, step, prevSteps, suggestionId);
89
84
  if (handled) {
90
85
  return true;
91
86
  }
@@ -4,7 +4,6 @@ import { rebasePos } from "./rebasePos.js";
4
4
  import { getSuggestionMarks } from "./utils.js";
5
5
  import { joinBlocks } from "./features/joinBlocks/index.js";
6
6
  import { collapseZWSPNodes, findJoinMark, joinNodesAndMarkJoinPoints, removeZWSPDeletions } from "./features/joinOnDelete/index.js";
7
- import { handleStructureStep } from "./features/wrapUnwrap/handleStructureStep.js";
8
7
  /**
9
8
  * Transform a replace step into its equivalent tracked steps.
10
9
  *
@@ -35,10 +34,6 @@ import { handleStructureStep } from "./features/wrapUnwrap/handleStructureStep.j
35
34
  * will be joined and given the same ids. Any no-longer-necessary
36
35
  * zero-width spaces will be removed.
37
36
  */ export function suggestReplaceStep(trackedTransaction, state, doc, step, prevSteps, suggestionId) {
38
- const handled = handleStructureStep(trackedTransaction, state, step, prevSteps, suggestionId);
39
- if (handled) {
40
- return true;
41
- }
42
37
  const { deletion, insertion } = getSuggestionMarks(state.schema);
43
38
  // Check for insertion and deletion marks directly
44
39
  // adjacent to this step's boundaries. If they exist,
@@ -205,15 +200,16 @@ import { handleStructureStep } from "./features/wrapUnwrap/handleStructureStep.j
205
200
  trackedTransaction.setSelection(TextSelection.near(trackedTransaction.doc.resolve($stepFrom.pos - 1)));
206
201
  }
207
202
  // Handle insertions
208
- // When didBlockJoin is true, only process insertions if the slice contains
203
+ // When didBlockJoin is true, or nodes were joined, only process insertions if the slice contains
209
204
  // actual new content (closed slice) rather than just structural info for the join (open slice).
210
205
  // Open slices have openStart > 0 or openEnd > 0 and represent block structure.
211
206
  // Closed slices have openStart = 0 and openEnd = 0 and contain new user content.
212
207
  // TODO: Done with AI, not 100% sure about the argument but it works. Kind of.
213
208
  // The replaced content is not equivalent to what would happen without suggestions, just with insertions
214
209
  // but it's workable. Only issues are with deleting between different depths ( for ex. between list and root level paragraph )
210
+ const didJoinNodes = didBlockJoin || joinNodesTransform.steps.length > 0;
215
211
  const sliceHasNewContent = step.slice.openStart === 0 && step.slice.openEnd === 0;
216
- const shouldProcessInsertion = step.slice.content.size && (!didBlockJoin || sliceHasNewContent);
212
+ const shouldProcessInsertion = step.slice.content.size && (!didJoinNodes || sliceHasNewContent);
217
213
  if (shouldProcessInsertion) {
218
214
  const $to = trackedTransaction.doc.resolve(stepTo);
219
215
  // Don't allow inserting content within an existing deletion
package/dist/schema.d.ts CHANGED
@@ -1,10 +1,12 @@
1
1
  import { type MarkSpec } from "prosemirror-model";
2
2
  export declare const deletion: MarkSpec;
3
+ export declare const hiddenDeletion: MarkSpec;
3
4
  export declare const insertion: MarkSpec;
4
5
  export declare const modification: MarkSpec;
5
- export declare const structure: MarkSpec;
6
6
  /**
7
7
  * Add the deletion, insertion, and modification marks to
8
8
  * the provided MarkSpec map.
9
9
  */
10
- export declare function addSuggestionMarks<Marks extends string>(marks: Record<Marks, MarkSpec>): Record<Marks | "deletion" | "insertion" | "modification" | "structure", MarkSpec>;
10
+ export declare function addSuggestionMarks<Marks extends string>(marks: Record<Marks, MarkSpec>, opts?: {
11
+ experimental_deletions?: "hidden" | "visible";
12
+ }): Record<Marks | "deletion" | "insertion" | "modification", MarkSpec>;