@magic-marker/prosemirror-suggest-changes 0.2.1-block-join.4 → 0.2.1-block-join.6

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.
@@ -2,7 +2,7 @@ import { Plugin, PluginKey, TextSelection } from "prosemirror-state";
2
2
  import { getSuggestionMarks } from "./utils.js";
3
3
  import { ZWSP } from "./constants.js";
4
4
  // import { ZWSP } from "./constants.js";
5
- const TRACE_ENABLED = false;
5
+ const TRACE_ENABLED = true;
6
6
  function trace(...args) {
7
7
  // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
8
8
  if (!TRACE_ENABLED) return;
@@ -43,15 +43,21 @@ export function ensureSelection() {
43
43
  newState.handleKeyDown.arrowLeft = event.key === "ArrowLeft";
44
44
  newState.handleKeyDown.arrowRight = event.key === "ArrowRight";
45
45
  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) {
46
- console.log("handleKeyDown newState =", newState);
46
+ trace("handleKeyDown newState =", newState);
47
47
  view.dispatch(view.state.tr.setMeta(ensureSelectionKey, newState));
48
48
  }
49
49
  }
50
50
  },
51
51
  appendTransaction (_transactions, oldState, newState) {
52
+ const pluginState = ensureSelectionKey.getState(newState);
53
+ if (!(newState.selection instanceof TextSelection)) {
54
+ return null;
55
+ }
56
+ if (isPosValid(newState.selection.$anchor) && isPosValid(newState.selection.$head)) {
57
+ return null;
58
+ }
52
59
  // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
53
60
  if (TRACE_ENABLED) console.groupCollapsed("[ensureSelectionPlugin]", "appendTransaction");
54
- const pluginState = ensureSelectionKey.getState(newState);
55
61
  trace("appendTransaction", "search for new valid $anchor...");
56
62
  let $newAnchor = getNewValidPos(newState.selection.$anchor, getDirection(oldState.selection.$anchor, newState.selection.$anchor, pluginState));
57
63
  trace("appendTransaction", "new valid $anchor", $newAnchor?.pos, {
@@ -92,12 +98,18 @@ export function isEnsureSelectionEnabled() {
92
98
  function isPosValid($pos) {
93
99
  // text selection is only valid in nodes that allow inline content
94
100
  // https://github.com/ProseMirror/prosemirror-state/blob/1.4.4/src/selection.ts#L219
95
- if (!$pos.parent.inlineContent) {
96
- trace("isPosValid", $pos.pos, "pos invalid", "reason: not in inlineContent node", {
97
- $pos
98
- });
99
- return false;
100
- }
101
+ // if (!$pos.parent.inlineContent) {
102
+ // trace(
103
+ // "isPosValid",
104
+ // $pos.pos,
105
+ // "pos invalid",
106
+ // "reason: not in inlineContent node",
107
+ // {
108
+ // $pos,
109
+ // },
110
+ // );
111
+ // return false;
112
+ // }
101
113
  const { deletion, insertion } = getSuggestionMarks($pos.doc.type.schema);
102
114
  const deletionBefore = deletion.isInSet($pos.nodeBefore?.marks ?? []);
103
115
  const deletionAfter = deletion.isInSet($pos.nodeAfter?.marks ?? []);
@@ -145,27 +157,22 @@ function isPosValid($pos) {
145
157
  const isZWSPBefore = $pos.nodeBefore && $pos.nodeBefore.textContent.replace(ZWSP_REGEXP, "") === "";
146
158
  const isZWSPAfter = $pos.nodeAfter && $pos.nodeAfter.textContent.replace(ZWSP_REGEXP, "") === "";
147
159
  if (insertionBefore && insertionAfter && isZWSPBefore && isZWSPAfter) {
148
- console.log("isPosValid", $pos.pos, "pos invalid", "reason: between two ZWSP insertions", {
160
+ trace("isPosValid", $pos.pos, "pos invalid", "reason: between two ZWSP insertions", {
149
161
  $pos
150
162
  });
151
163
  return false;
152
164
  }
153
- console.log("ZWSP checks", {
154
- nodeAfterCheck: $pos.nodeAfter?.textContent.replace(ZWSP_REGEXP, ""),
155
- nodeBeforeCheck: $pos.nodeBefore?.textContent.replace(ZWSP_REGEXP, ""),
156
- parentCheck: $pos.parent.textContent.replace(ZWSP_REGEXP, "")
157
- });
158
165
  if (insertionBefore && isZWSPBefore && $pos.nodeAfter == null && // a position like this:
159
166
  // <p><insertion>ZWSP</insertion>|</p>
160
167
  // because it means this paragraph was just created and it's empty
161
168
  $pos.parent.textContent.replace(ZWSP_REGEXP, "") !== "") {
162
- console.log("isPosValid", $pos.pos, "pos invalid", "reason: between ZWSP insertion and right node boundary", {
169
+ trace("isPosValid", $pos.pos, "pos invalid", "reason: between ZWSP insertion and right node boundary", {
163
170
  $pos
164
171
  });
165
172
  return false;
166
173
  }
167
174
  if (insertionAfter && isZWSPAfter && $pos.nodeBefore == null) {
168
- console.log("isPosValid", $pos.pos, "pos invalid", "reason: between ZWSP insertion and left node boundary", {
175
+ trace("isPosValid", $pos.pos, "pos invalid", "reason: between ZWSP insertion and left node boundary", {
169
176
  $pos
170
177
  });
171
178
  return false;
@@ -1,8 +1,15 @@
1
1
  import { getSuggestionMarks } from "./utils.js";
2
2
  import { Transform } from "prosemirror-transform";
3
3
  import { ZWSP } from "./constants.js";
4
+ const TRACE_ENABLED = true;
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
+ }
4
10
  export function prependDeletionsWithZWSP(transaction, opts) {
5
- console.groupCollapsed("prepend deletions with zwsp");
11
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
12
+ if (TRACE_ENABLED) console.groupCollapsed("prepend deletions with zwsp");
6
13
  const { deletion } = getSuggestionMarks(transaction.doc.type.schema);
7
14
  let transform = new Transform(transaction.doc);
8
15
  transform.doc.descendants((node, pos)=>{
@@ -10,7 +17,7 @@ export function prependDeletionsWithZWSP(transaction, opts) {
10
17
  if (!node.isText || mark == null) return true;
11
18
  const mappedPos = transform.mapping.map(pos);
12
19
  transform.delete(mappedPos, mappedPos + node.nodeSize);
13
- console.log("found zwsp, deleted", {
20
+ trace("found zwsp, deleted", {
14
21
  from: mappedPos,
15
22
  to: mappedPos + node.nodeSize
16
23
  });
@@ -19,10 +26,11 @@ export function prependDeletionsWithZWSP(transaction, opts) {
19
26
  transform.steps.forEach((step)=>{
20
27
  transaction.step(step);
21
28
  });
22
- console.log(`added ${String(transform.steps.length)} remove zwsp steps to tr`);
29
+ trace(`added ${String(transform.steps.length)} remove zwsp steps to tr`);
23
30
  if (opts?.experimental_deletions !== "hidden") {
24
- console.log("deletions are visible, skipping prepend zwsp");
25
- console.groupEnd();
31
+ trace("deletions are visible, skipping prepend zwsp");
32
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
33
+ if (TRACE_ENABLED) console.groupEnd();
26
34
  return transaction;
27
35
  }
28
36
  transform = new Transform(transaction.doc);
@@ -39,7 +47,7 @@ export function prependDeletionsWithZWSP(transaction, opts) {
39
47
  ]);
40
48
  const mappedPos = transform.mapping.map(pos);
41
49
  transform.insert(mappedPos + 1, zwspNode);
42
- console.log("found first-child-deletion inline content node, inserted zwsp at", {
50
+ trace("found first-child-deletion inline content node, inserted zwsp at", {
43
51
  pos,
44
52
  mappedPos
45
53
  });
@@ -48,7 +56,8 @@ export function prependDeletionsWithZWSP(transaction, opts) {
48
56
  transform.steps.forEach((step)=>{
49
57
  transaction.step(step);
50
58
  });
51
- console.log(`added ${String(transform.steps.length)} add zwsp steps to tr`);
52
- console.groupEnd();
59
+ trace(`added ${String(transform.steps.length)} add zwsp steps to tr`);
60
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
61
+ if (TRACE_ENABLED) console.groupEnd();
53
62
  return transaction;
54
63
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@magic-marker/prosemirror-suggest-changes",
3
- "version": "0.2.1-block-join.4",
3
+ "version": "0.2.1-block-join.6",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "module": "dist/index.js",