@lvce-editor/editor-worker 19.61.4 → 19.61.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.
@@ -4211,6 +4211,14 @@ const scheduleDocumentAndCursorsSelections = async (editor, changes, selectionCh
4211
4211
  const newLines = applyEdits(editor, changes);
4212
4212
  const partialNewEditor = updateLines(editor, newLines);
4213
4213
  const newSelections = selectionChanges || applyEdit$1(partialNewEditor, changes);
4214
+ const selectionHistory = {
4215
+ after: newSelections,
4216
+ before: editor.selections
4217
+ };
4218
+ Object.defineProperty(changes, 'selectionHistory', {
4219
+ configurable: true,
4220
+ value: selectionHistory
4221
+ });
4214
4222
  // TODO should separate rendering from business logic somehow
4215
4223
  // currently hard to test because need to mock editor height, top, left,
4216
4224
  // invalidStartIndex, lineCache, etc. just for testing editorType
@@ -4281,7 +4289,7 @@ const scheduleDocumentAndCursorsSelections = async (editor, changes, selectionCh
4281
4289
  };
4282
4290
  };
4283
4291
  // @ts-ignore
4284
- const scheduleDocumentAndCursorsSelectionIsUndo = async (editor, changes) => {
4292
+ const scheduleDocumentAndCursorsSelectionIsUndo = async (editor, changes, selectionChanges = undefined) => {
4285
4293
  object(editor);
4286
4294
  array(changes);
4287
4295
  if (changes.length === 0) {
@@ -4289,7 +4297,7 @@ const scheduleDocumentAndCursorsSelectionIsUndo = async (editor, changes) => {
4289
4297
  }
4290
4298
  const newLines = applyEdits(editor, changes);
4291
4299
  const partialNewEditor = updateLines(editor, newLines);
4292
- const newSelections = applyEdit$1(partialNewEditor, changes);
4300
+ const newSelections = selectionChanges || applyEdit$1(partialNewEditor, changes);
4293
4301
  const invalidStartIndex = Math.min(editor.invalidStartIndex, changes[0].start.rowIndex);
4294
4302
  const newEditor = {
4295
4303
  ...partialNewEditor,
@@ -7445,40 +7453,69 @@ const copy$1 = async editor => {
7445
7453
  return editor;
7446
7454
  };
7447
7455
 
7448
- // TODO handle multiline selection
7449
-
7456
+ const getCopyLineOperations = selections => {
7457
+ const operations = [];
7458
+ for (let i = 0; i < selections.length; i += 4) {
7459
+ const [startRowIndex,, selectionEndRowIndex, endColumnIndex] = getSelectionPairs(selections, i);
7460
+ const endRowIndex = startRowIndex < selectionEndRowIndex && endColumnIndex === 0 ? selectionEndRowIndex - 1 : selectionEndRowIndex;
7461
+ operations.push({
7462
+ endRowIndex,
7463
+ startRowIndex
7464
+ });
7465
+ }
7466
+ operations.sort((a, b) => a.startRowIndex - b.startRowIndex || a.endRowIndex - b.endRowIndex);
7467
+ const mergedOperations = [];
7468
+ for (const operation of operations) {
7469
+ const previous = mergedOperations.at(-1);
7470
+ if (previous && previous.endRowIndex >= operation.startRowIndex) {
7471
+ mergedOperations[mergedOperations.length - 1] = {
7472
+ ...previous,
7473
+ endRowIndex: Math.max(previous.endRowIndex, operation.endRowIndex)
7474
+ };
7475
+ } else {
7476
+ mergedOperations.push(operation);
7477
+ }
7478
+ }
7479
+ return mergedOperations;
7480
+ };
7450
7481
  const copyLineDown = editor => {
7451
7482
  const {
7452
7483
  selections
7453
7484
  } = editor;
7454
- const rows = [];
7455
- for (let i = 0; i < selections.length; i += 4) {
7456
- const rowIndex = selections[i];
7457
- number(rowIndex);
7458
- rows.push(rowIndex);
7459
- }
7460
- const uniqueRows = [...new Set(rows)].toSorted((a, b) => a - b);
7461
- const changes = uniqueRows.map(rowIndex => {
7462
- const position = {
7463
- columnIndex: 0,
7464
- rowIndex
7465
- };
7485
+ const operations = getCopyLineOperations(selections);
7486
+ const changes = operations.map(({
7487
+ endRowIndex,
7488
+ startRowIndex
7489
+ }) => {
7490
+ const selectedLines = editor.lines.slice(startRowIndex, endRowIndex + 1);
7466
7491
  return {
7467
- deleted: [''],
7468
- end: position,
7469
- inserted: [getLine$1(editor, rowIndex), ''],
7470
- start: position
7492
+ deleted: selectedLines,
7493
+ end: {
7494
+ columnIndex: editor.lines[endRowIndex].length,
7495
+ rowIndex: endRowIndex
7496
+ },
7497
+ inserted: [...selectedLines, ...selectedLines],
7498
+ start: {
7499
+ columnIndex: 0,
7500
+ rowIndex: startRowIndex
7501
+ }
7471
7502
  };
7472
7503
  });
7473
- const rowOffsets = new Map(uniqueRows.map((row, index) => [row, index + 1]));
7504
+ const getRowOffset = rowIndex => {
7505
+ let offset = 0;
7506
+ for (const operation of operations) {
7507
+ if (operation.startRowIndex <= rowIndex) {
7508
+ offset += operation.endRowIndex - operation.startRowIndex + 1;
7509
+ }
7510
+ }
7511
+ return offset;
7512
+ };
7474
7513
  const selectionChanges = new Uint32Array(selections.length);
7475
7514
  for (let i = 0; i < selections.length; i += 4) {
7476
- const rowIndex = selections[i] + rowOffsets.get(selections[i]);
7477
- const columnIndex = selections[i + 1];
7478
- selectionChanges[i] = rowIndex;
7479
- selectionChanges[i + 1] = columnIndex;
7480
- selectionChanges[i + 2] = rowIndex;
7481
- selectionChanges[i + 3] = columnIndex;
7515
+ selectionChanges[i] = selections[i] + getRowOffset(selections[i]);
7516
+ selectionChanges[i + 1] = selections[i + 1];
7517
+ selectionChanges[i + 2] = selections[i + 2] + getRowOffset(selections[i + 2]);
7518
+ selectionChanges[i + 3] = selections[i + 3];
7482
7519
  }
7483
7520
  return scheduleDocumentAndCursorsSelections(editor, changes, selectionChanges);
7484
7521
  };
@@ -11106,12 +11143,13 @@ const redo = state => {
11106
11143
  return state;
11107
11144
  }
11108
11145
  const last = redoStack.at(-1);
11146
+ const selectionChanges = last.selectionHistory?.after;
11109
11147
  const newState = {
11110
11148
  ...state,
11111
11149
  redoStack: redoStack.slice(0, -1),
11112
11150
  undoStack: [...state.undoStack, last]
11113
11151
  };
11114
- return scheduleDocumentAndCursorsSelectionIsUndo(newState, last);
11152
+ return scheduleDocumentAndCursorsSelectionIsUndo(newState, last, selectionChanges);
11115
11153
  };
11116
11154
 
11117
11155
  // @ts-ignore
@@ -12627,12 +12665,14 @@ const typeWithAutoClosing = async (editor, text) => {
12627
12665
  };
12628
12666
 
12629
12667
  const inverseChange = edit => {
12630
- const endColumnIndex = edit.end.columnIndex - edit.deleted[0].length + edit.inserted[0].length;
12668
+ const insertedLines = edit.inserted;
12669
+ const endRowIndex = edit.start.rowIndex + insertedLines.length - 1;
12670
+ const endColumnIndex = insertedLines.length === 1 ? edit.start.columnIndex + insertedLines[0].length : insertedLines.at(-1).length;
12631
12671
  return {
12632
- deleted: edit.inserted,
12672
+ deleted: insertedLines,
12633
12673
  end: {
12634
12674
  columnIndex: endColumnIndex,
12635
- rowIndex: edit.end.rowIndex
12675
+ rowIndex: endRowIndex
12636
12676
  },
12637
12677
  inserted: edit.deleted,
12638
12678
  start: edit.start
@@ -12648,12 +12688,13 @@ const undo = state => {
12648
12688
  }
12649
12689
  const last = undoStack.at(-1);
12650
12690
  const inverseChanges = last.toReversed().map(inverseChange);
12691
+ const selectionChanges = last.selectionHistory?.before;
12651
12692
  const newState = {
12652
12693
  ...state,
12653
12694
  redoStack: [...(state.redoStack || []), last],
12654
12695
  undoStack: undoStack.slice(0, -1)
12655
12696
  };
12656
- return scheduleDocumentAndCursorsSelectionIsUndo(newState, inverseChanges);
12697
+ return scheduleDocumentAndCursorsSelectionIsUndo(newState, inverseChanges, selectionChanges);
12657
12698
  };
12658
12699
 
12659
12700
  const unfold = editor => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/editor-worker",
3
- "version": "19.61.4",
3
+ "version": "19.61.5",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git@github.com:lvce-editor/editor-worker.git"