@lvce-editor/editor-worker 19.61.3 → 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
  };
@@ -10286,14 +10323,16 @@ const getChanges = (lines, selections, languageConfiguration, indentUnit) => {
10286
10323
  if (isEmpty(selectionStartRow, selectionStartColumn, selectionEndRow, selectionEndColumn)) {
10287
10324
  const line = lines[selectionStartRow];
10288
10325
  const before = line.slice(0, selectionStartColumn);
10326
+ const after = line.slice(selectionStartColumn);
10289
10327
  const indent = getIndent(before);
10290
10328
  if (shouldIncreaseIndent(before, increaseIndentRegex)) {
10329
+ const inserted = after ? ['', indent + indentUnit, indent] : ['', indent + indentUnit];
10291
10330
  changes.push({
10292
10331
  deleted: getSelectionText({
10293
10332
  lines
10294
10333
  }, range),
10295
10334
  end: end,
10296
- inserted: ['', indent + indentUnit, indent],
10335
+ inserted,
10297
10336
  origin: InsertLineBreak,
10298
10337
  start: start
10299
10338
  });
@@ -11104,12 +11143,13 @@ const redo = state => {
11104
11143
  return state;
11105
11144
  }
11106
11145
  const last = redoStack.at(-1);
11146
+ const selectionChanges = last.selectionHistory?.after;
11107
11147
  const newState = {
11108
11148
  ...state,
11109
11149
  redoStack: redoStack.slice(0, -1),
11110
11150
  undoStack: [...state.undoStack, last]
11111
11151
  };
11112
- return scheduleDocumentAndCursorsSelectionIsUndo(newState, last);
11152
+ return scheduleDocumentAndCursorsSelectionIsUndo(newState, last, selectionChanges);
11113
11153
  };
11114
11154
 
11115
11155
  // @ts-ignore
@@ -12625,12 +12665,14 @@ const typeWithAutoClosing = async (editor, text) => {
12625
12665
  };
12626
12666
 
12627
12667
  const inverseChange = edit => {
12628
- 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;
12629
12671
  return {
12630
- deleted: edit.inserted,
12672
+ deleted: insertedLines,
12631
12673
  end: {
12632
12674
  columnIndex: endColumnIndex,
12633
- rowIndex: edit.end.rowIndex
12675
+ rowIndex: endRowIndex
12634
12676
  },
12635
12677
  inserted: edit.deleted,
12636
12678
  start: edit.start
@@ -12646,12 +12688,13 @@ const undo = state => {
12646
12688
  }
12647
12689
  const last = undoStack.at(-1);
12648
12690
  const inverseChanges = last.toReversed().map(inverseChange);
12691
+ const selectionChanges = last.selectionHistory?.before;
12649
12692
  const newState = {
12650
12693
  ...state,
12651
12694
  redoStack: [...(state.redoStack || []), last],
12652
12695
  undoStack: undoStack.slice(0, -1)
12653
12696
  };
12654
- return scheduleDocumentAndCursorsSelectionIsUndo(newState, inverseChanges);
12697
+ return scheduleDocumentAndCursorsSelectionIsUndo(newState, inverseChanges, selectionChanges);
12655
12698
  };
12656
12699
 
12657
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.3",
3
+ "version": "19.61.5",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git@github.com:lvce-editor/editor-worker.git"