@magic-marker/prosemirror-suggest-changes 0.2.1-wrap-unwrap.0 → 0.2.1-wrap-unwrap.2

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.
package/dist/commands.js CHANGED
@@ -5,7 +5,7 @@ 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 { revertAllStructureSuggestions } from "./features/wrapUnwrap/revertStructureSuggestion.js";
8
+ import { applyStructureSuggestion, isStructureSuggestion, revertAllStructureSuggestions, revertStructureSuggestion } from "./features/wrapUnwrap/revertStructureSuggestion.js";
9
9
  /**
10
10
  * Given a node and a transform, add a set of steps to the
11
11
  * transform that applies all marks of type markTypeToApply
@@ -194,6 +194,9 @@ export function applySuggestionsToRange(doc, from, to) {
194
194
  * contents left in the doc.
195
195
  */ export function applySuggestion(suggestionId, from, to) {
196
196
  return (state, dispatch)=>{
197
+ if (isStructureSuggestion(suggestionId, state.tr)) {
198
+ return applyStructureSuggestion(suggestionId)(state, dispatch);
199
+ }
197
200
  const { deletion, insertion } = getSuggestionMarks(state.schema);
198
201
  const tr = state.tr;
199
202
  applySuggestionsToTransform(state.doc, tr, insertion, deletion, suggestionId, from, to);
@@ -252,6 +255,9 @@ export function applySuggestionsToRange(doc, from, to) {
252
255
  * Modifications tracked in modification marks will be reverted.
253
256
  */ export function revertSuggestion(suggestionId, from, to) {
254
257
  return (state, dispatch)=>{
258
+ if (isStructureSuggestion(suggestionId, state.tr)) {
259
+ return revertStructureSuggestion(suggestionId)(state, dispatch);
260
+ }
255
261
  const { deletion, insertion } = getSuggestionMarks(state.schema);
256
262
  const tr = state.tr;
257
263
  applySuggestionsToTransform(state.doc, tr, deletion, insertion, suggestionId, from, to);
@@ -1,6 +1,44 @@
1
1
  import { type Transaction, type Command } from "prosemirror-state";
2
2
  import { type SuggestionId } from "../../generateId.js";
3
- import { type Node } from "prosemirror-model";
3
+ import { type Node, type Mark } from "prosemirror-model";
4
+ import { type Transform } from "prosemirror-transform";
5
+ export declare function isStructureSuggestion(suggestionId: SuggestionId, tr: Transaction): boolean;
4
6
  export declare function revertAllStructureSuggestions(doc: Node, tr: Transaction): void;
5
7
  export declare function revertStructureSuggestion(suggestionId: SuggestionId): Command;
8
+ export declare function applyStructureSuggestion(suggestionId: SuggestionId): Command;
6
9
  export declare function revertStructureSuggestions(suggestionIds: SuggestionId[]): Command;
10
+ export declare function applyStructureMarkGroup(group: {
11
+ type: "replaceAround";
12
+ markFrom: {
13
+ pos: number;
14
+ node: Node;
15
+ mark: Mark;
16
+ };
17
+ markTo: {
18
+ pos: number;
19
+ node: Node;
20
+ mark: Mark;
21
+ };
22
+ markGapFrom: {
23
+ pos: number;
24
+ node: Node;
25
+ mark: Mark;
26
+ };
27
+ markGapTo: {
28
+ pos: number;
29
+ node: Node;
30
+ mark: Mark;
31
+ };
32
+ } | {
33
+ type: "replace";
34
+ markFrom: {
35
+ pos: number;
36
+ node: Node;
37
+ mark: Mark;
38
+ };
39
+ markTo: {
40
+ pos: number;
41
+ node: Node;
42
+ mark: Mark;
43
+ };
44
+ }, tr: Transform): void;
@@ -2,6 +2,15 @@ import { suggestChangesKey } from "../../plugin.js";
2
2
  import { getSuggestionMarks } from "../../utils.js";
3
3
  import { Slice } from "prosemirror-model";
4
4
  import { ReplaceAroundStep, ReplaceStep } from "prosemirror-transform";
5
+ export function isStructureSuggestion(suggestionId, tr) {
6
+ try {
7
+ findStructureMarkGroupBySuggestionId(suggestionId, tr);
8
+ return true;
9
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
10
+ } catch (error) {
11
+ return false;
12
+ }
13
+ }
5
14
  export function revertAllStructureSuggestions(doc, tr) {
6
15
  const { structure } = getSuggestionMarks(doc.type.schema);
7
16
  // find structure mark in the doc
@@ -38,6 +47,18 @@ export function revertStructureSuggestion(suggestionId) {
38
47
  return true;
39
48
  };
40
49
  }
50
+ export function applyStructureSuggestion(suggestionId) {
51
+ return (state, dispatch)=>{
52
+ const tr = state.tr;
53
+ performStructureRevert(suggestionId, tr, "apply");
54
+ if (!tr.steps.length) return false;
55
+ tr.setMeta(suggestChangesKey, {
56
+ skip: true
57
+ });
58
+ dispatch?.(tr);
59
+ return true;
60
+ };
61
+ }
41
62
  export function revertStructureSuggestions(suggestionIds) {
42
63
  return (state, dispatch)=>{
43
64
  const tr = state.tr;
@@ -52,7 +73,7 @@ export function revertStructureSuggestions(suggestionIds) {
52
73
  return true;
53
74
  };
54
75
  }
55
- function performStructureRevert(suggestionId, tr) {
76
+ function performStructureRevert(suggestionId, tr, direction = "revert") {
56
77
  console.groupCollapsed(// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
57
78
  `performStructureRevert, suggestionId = ${suggestionId}`);
58
79
  const { structure } = getSuggestionMarks(tr.doc.type.schema);
@@ -167,7 +188,11 @@ function performStructureRevert(suggestionId, tr) {
167
188
  const markIds = Array.from(structureMarkGroups.values()).sort((a, b)=>Number(b) - Number(a));
168
189
  markIds.forEach((id)=>{
169
190
  const group = findStructureMarkGroupBySuggestionId(id, tr);
170
- revertStructureMarkGroup(group, tr);
191
+ if (direction === "apply") {
192
+ applyStructureMarkGroup(group, tr);
193
+ } else {
194
+ revertStructureMarkGroup(group, tr);
195
+ }
171
196
  });
172
197
  console.groupEnd();
173
198
  }
@@ -188,6 +213,25 @@ function getPosFromMark(mark, pos, node) {
188
213
  }
189
214
  return null;
190
215
  }
216
+ export function applyStructureMarkGroup(group, tr) {
217
+ console.groupCollapsed("apply structure group, id = ", group.markFrom.mark.attrs["id"]);
218
+ console.log({
219
+ group
220
+ });
221
+ if (group.type === "replace") {
222
+ const { markFrom, markTo } = group;
223
+ tr.removeNodeMark(markFrom.pos, markFrom.mark);
224
+ tr.removeNodeMark(markTo.pos, markTo.mark);
225
+ console.groupEnd();
226
+ return;
227
+ }
228
+ const { markFrom, markTo, markGapFrom, markGapTo } = group;
229
+ tr.removeNodeMark(markFrom.pos, markFrom.mark);
230
+ tr.removeNodeMark(markTo.pos, markTo.mark);
231
+ tr.removeNodeMark(markGapFrom.pos, markGapFrom.mark);
232
+ tr.removeNodeMark(markGapTo.pos, markGapTo.mark);
233
+ console.groupEnd();
234
+ }
191
235
  function revertStructureMarkGroup(group, tr) {
192
236
  console.groupCollapsed("revert structure group, id = ", group.markFrom.mark.attrs["id"]);
193
237
  console.log({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@magic-marker/prosemirror-suggest-changes",
3
- "version": "0.2.1-wrap-unwrap.0",
3
+ "version": "0.2.1-wrap-unwrap.2",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "module": "dist/index.js",