@magic-marker/prosemirror-suggest-changes 0.2.1-block-join.4 → 0.2.1-block-join.5
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 =
|
|
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,18 @@ 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
|
-
|
|
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 (isPosValid(newState.selection.$anchor) && isPosValid(newState.selection.$head)) {
|
|
54
|
+
return null;
|
|
55
|
+
}
|
|
52
56
|
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
53
57
|
if (TRACE_ENABLED) console.groupCollapsed("[ensureSelectionPlugin]", "appendTransaction");
|
|
54
|
-
const pluginState = ensureSelectionKey.getState(newState);
|
|
55
58
|
trace("appendTransaction", "search for new valid $anchor...");
|
|
56
59
|
let $newAnchor = getNewValidPos(newState.selection.$anchor, getDirection(oldState.selection.$anchor, newState.selection.$anchor, pluginState));
|
|
57
60
|
trace("appendTransaction", "new valid $anchor", $newAnchor?.pos, {
|
|
@@ -145,27 +148,22 @@ function isPosValid($pos) {
|
|
|
145
148
|
const isZWSPBefore = $pos.nodeBefore && $pos.nodeBefore.textContent.replace(ZWSP_REGEXP, "") === "";
|
|
146
149
|
const isZWSPAfter = $pos.nodeAfter && $pos.nodeAfter.textContent.replace(ZWSP_REGEXP, "") === "";
|
|
147
150
|
if (insertionBefore && insertionAfter && isZWSPBefore && isZWSPAfter) {
|
|
148
|
-
|
|
151
|
+
trace("isPosValid", $pos.pos, "pos invalid", "reason: between two ZWSP insertions", {
|
|
149
152
|
$pos
|
|
150
153
|
});
|
|
151
154
|
return false;
|
|
152
155
|
}
|
|
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
156
|
if (insertionBefore && isZWSPBefore && $pos.nodeAfter == null && // a position like this:
|
|
159
157
|
// <p><insertion>ZWSP</insertion>|</p>
|
|
160
158
|
// because it means this paragraph was just created and it's empty
|
|
161
159
|
$pos.parent.textContent.replace(ZWSP_REGEXP, "") !== "") {
|
|
162
|
-
|
|
160
|
+
trace("isPosValid", $pos.pos, "pos invalid", "reason: between ZWSP insertion and right node boundary", {
|
|
163
161
|
$pos
|
|
164
162
|
});
|
|
165
163
|
return false;
|
|
166
164
|
}
|
|
167
165
|
if (insertionAfter && isZWSPAfter && $pos.nodeBefore == null) {
|
|
168
|
-
|
|
166
|
+
trace("isPosValid", $pos.pos, "pos invalid", "reason: between ZWSP insertion and left node boundary", {
|
|
169
167
|
$pos
|
|
170
168
|
});
|
|
171
169
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
29
|
+
trace(`added ${String(transform.steps.length)} remove zwsp steps to tr`);
|
|
23
30
|
if (opts?.experimental_deletions !== "hidden") {
|
|
24
|
-
|
|
25
|
-
|
|
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
|
-
|
|
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
|
-
|
|
52
|
-
|
|
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
|
}
|