@adecore/editor-core 0.54.4 → 0.54.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.
@@ -20,6 +20,7 @@ export interface DocumentEditOptions extends EditOptions {
20
20
  readonly head: Position;
21
21
  }[];
22
22
  expectedRevision?: number;
23
+ clearHistory?: boolean;
23
24
  }
24
25
  export interface TypeTextOptions extends CommandOptions {
25
26
  historyGroup?: string;
@@ -77,7 +78,7 @@ export declare class DocumentModel {
77
78
  setSelections(selections: readonly Selection[]): void;
78
79
  subscribe(listener: (snapshot: EditorSnapshot) => void): Disposable;
79
80
  applyEdits(edits: readonly TextEdit[], options?: DocumentEditOptions): boolean;
80
- setText(text: string): void;
81
+ setText(text: string, options?: Pick<DocumentEditOptions, 'clearHistory'>): void;
81
82
  undo(): boolean;
82
83
  redo(): boolean;
83
84
  positionAt(offset: number): Position;
@@ -188,6 +189,7 @@ export declare class DocumentModel {
188
189
  private addCaretPerSelectedLine;
189
190
  private changeSelections;
190
191
  private structure;
192
+ private clearHistory;
191
193
  private state;
192
194
  private invalidate;
193
195
  private restore;
package/dist/document.js CHANGED
@@ -411,6 +411,9 @@ export class DocumentModel {
411
411
  // Several edits can cancel each other out, like moving a character and moving it back.
412
412
  const unchanged = next === this.rope || (changed.length > 1 && next.length === this.rope.length && next.equalsRange(this.rope, changed[0].from, changed.at(-1).to));
413
413
  if (unchanged) {
414
+ if (options.clearHistory) {
415
+ this.clearHistory();
416
+ }
414
417
  if (options.selections) {
415
418
  this.setSelections(options.selections);
416
419
  }
@@ -439,7 +442,10 @@ export class DocumentModel {
439
442
  last.after.rope === before.rope &&
440
443
  sameSelections(last.after.selections, before.selections);
441
444
  const joinsPrevious = options.joinPrevious === true && last !== undefined && last.after.rope === before.rope;
442
- if (last !== undefined && (joinsGroup || joinsPrevious)) {
445
+ if (options.clearHistory) {
446
+ this.undoStack = [];
447
+ }
448
+ else if (last !== undefined && (joinsGroup || joinsPrevious)) {
443
449
  last.after = after;
444
450
  last.changes.push(changes);
445
451
  }
@@ -449,14 +455,14 @@ export class DocumentModel {
449
455
  if (this.undoStack.length > historyLimit) {
450
456
  this.undoStack.shift();
451
457
  }
452
- this.groupOpen = Boolean(options.historyGroup) && !joinsPrevious;
458
+ this.groupOpen = Boolean(options.historyGroup) && !joinsPrevious && !options.clearHistory;
453
459
  this.redoStack = [];
454
460
  this.emit([changes], options.source ?? 'external', () => (rebuild ? stretchEdits(previous, next, [changes]) : editsOf(previous, changed)), options.typed);
455
461
  return true;
456
462
  }
457
- /* Replaces everything as one undoable external edit. */
458
- setText(text) {
459
- this.applyEdits([{ from: 0, to: this.rope.length, text }], { source: 'external' });
463
+ /* Replaces everything as one undoable external edit, or with `clearHistory` as a text with no history before it, such as a reload from disk. */
464
+ setText(text, options = {}) {
465
+ this.applyEdits([{ from: 0, to: this.rope.length, text }], { source: 'external', clearHistory: options.clearHistory });
460
466
  }
461
467
  undo() {
462
468
  const entry = this.undoStack.pop();
@@ -2108,6 +2114,15 @@ export class DocumentModel {
2108
2114
  structure() {
2109
2115
  return (this.structureCache ??= scanStructure(this.getText()));
2110
2116
  }
2117
+ clearHistory() {
2118
+ if (this.undoStack.length === 0 && this.redoStack.length === 0) {
2119
+ return;
2120
+ }
2121
+ this.undoStack = [];
2122
+ this.redoStack = [];
2123
+ this.groupOpen = false;
2124
+ this.emit();
2125
+ }
2111
2126
  state() {
2112
2127
  return { rope: this.rope, selections: this.selections.map((selection) => ({ ...selection })) };
2113
2128
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adecore/editor-core",
3
- "version": "0.54.4",
3
+ "version": "0.54.6",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {
@@ -16,7 +16,7 @@
16
16
  "test": "bun test --conditions=source"
17
17
  },
18
18
  "dependencies": {
19
- "@adecore/utils": "0.54.4"
19
+ "@adecore/utils": "0.54.6"
20
20
  },
21
21
  "devDependencies": {
22
22
  "@types/bun": "^1.4.4",
package/src/document.ts CHANGED
@@ -66,6 +66,8 @@ export interface DocumentEditOptions extends EditOptions {
66
66
  selectionPositions?: readonly { readonly anchor: Position; readonly head: Position }[];
67
67
  /* The edit is refused, with nothing changed, unless the document is still at this revision. */
68
68
  expectedRevision?: number;
69
+ /* Takes no undo step and drops the history, even when the text stays the same, so undo never goes back past this text. */
70
+ clearHistory?: boolean;
69
71
  }
70
72
 
71
73
  export interface TypeTextOptions extends CommandOptions {
@@ -636,6 +638,10 @@ export class DocumentModel {
636
638
  next === this.rope || (changed.length > 1 && next.length === this.rope.length && next.equalsRange(this.rope, changed[0]!.from, changed.at(-1)!.to));
637
639
 
638
640
  if (unchanged) {
641
+ if (options.clearHistory) {
642
+ this.clearHistory();
643
+ }
644
+
639
645
  if (options.selections) {
640
646
  this.setSelections(options.selections);
641
647
  }
@@ -672,7 +678,9 @@ export class DocumentModel {
672
678
 
673
679
  const joinsPrevious = options.joinPrevious === true && last !== undefined && last.after.rope === before.rope;
674
680
 
675
- if (last !== undefined && (joinsGroup || joinsPrevious)) {
681
+ if (options.clearHistory) {
682
+ this.undoStack = [];
683
+ } else if (last !== undefined && (joinsGroup || joinsPrevious)) {
676
684
  last.after = after;
677
685
  last.changes.push(changes);
678
686
  } else {
@@ -683,7 +691,7 @@ export class DocumentModel {
683
691
  this.undoStack.shift();
684
692
  }
685
693
 
686
- this.groupOpen = Boolean(options.historyGroup) && !joinsPrevious;
694
+ this.groupOpen = Boolean(options.historyGroup) && !joinsPrevious && !options.clearHistory;
687
695
  this.redoStack = [];
688
696
 
689
697
  this.emit(
@@ -696,9 +704,9 @@ export class DocumentModel {
696
704
  return true;
697
705
  }
698
706
 
699
- /* Replaces everything as one undoable external edit. */
700
- setText(text: string): void {
701
- this.applyEdits([{ from: 0, to: this.rope.length, text }], { source: 'external' });
707
+ /* Replaces everything as one undoable external edit, or with `clearHistory` as a text with no history before it, such as a reload from disk. */
708
+ setText(text: string, options: Pick<DocumentEditOptions, 'clearHistory'> = {}): void {
709
+ this.applyEdits([{ from: 0, to: this.rope.length, text }], { source: 'external', clearHistory: options.clearHistory });
702
710
  }
703
711
 
704
712
  undo(): boolean {
@@ -2950,6 +2958,17 @@ export class DocumentModel {
2950
2958
  return (this.structureCache ??= scanStructure(this.getText()));
2951
2959
  }
2952
2960
 
2961
+ private clearHistory(): void {
2962
+ if (this.undoStack.length === 0 && this.redoStack.length === 0) {
2963
+ return;
2964
+ }
2965
+
2966
+ this.undoStack = [];
2967
+ this.redoStack = [];
2968
+ this.groupOpen = false;
2969
+ this.emit();
2970
+ }
2971
+
2953
2972
  private state(): State {
2954
2973
  return { rope: this.rope, selections: this.selections.map((selection) => ({ ...selection })) };
2955
2974
  }