@lvce-editor/editor-worker 19.28.1 → 19.28.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.
@@ -1589,7 +1589,7 @@ const sendMessagePortToErrorWorker = async (port, rpcId) => {
1589
1589
  const command = 'Errors.handleMessagePort';
1590
1590
  await invokeAndTransfer('SendMessagePortToExtensionHostWorker.sendMessagePortToErrorWorker', port, command, rpcId);
1591
1591
  };
1592
- const readFile = async uri => {
1592
+ const readFile$1 = async uri => {
1593
1593
  return invoke$b('FileSystem.readFile', uri);
1594
1594
  };
1595
1595
  const handleWorkspaceRefresh = async () => {
@@ -1639,7 +1639,7 @@ const RendererWorker = {
1639
1639
  invokeAndTransfer,
1640
1640
  openUri,
1641
1641
  readClipBoardText,
1642
- readFile,
1642
+ readFile: readFile$1,
1643
1643
  sendMessagePortToDialogWorker,
1644
1644
  sendMessagePortToErrorWorker,
1645
1645
  sendMessagePortToExtensionHostWorker,
@@ -3779,7 +3779,8 @@ const splitLines = lines => {
3779
3779
  };
3780
3780
 
3781
3781
  const {
3782
- invoke: invoke$7} = RendererWorker;
3782
+ invoke: invoke$7,
3783
+ readFile} = RendererWorker;
3783
3784
 
3784
3785
  const notifyTabModifiedStatusChange = async (uri, modified) => {
3785
3786
  try {
@@ -5025,44 +5026,80 @@ const applyEdit = async (editor, changes) => {
5025
5026
 
5026
5027
  // TODO maybe use a separate worker for bulk edits and bulk edit history
5027
5028
 
5028
- const getTextChanges = (editor, changes) => {
5029
+ const sortEditsDescending = edits => {
5030
+ return edits.toSorted((a, b) => b.offset - a.offset);
5031
+ };
5032
+ const applyEditsToText = (text, edits) => {
5033
+ let newText = text;
5034
+ for (const edit of sortEditsDescending(edits)) {
5035
+ newText = newText.slice(0, edit.offset) + edit.inserted + newText.slice(edit.offset + edit.deleted);
5036
+ }
5037
+ return newText;
5038
+ };
5039
+ const getTextChanges = (editor, edits) => {
5029
5040
  const textChanges = [];
5041
+ for (const edit of sortEditsDescending(edits)) {
5042
+ const startPosition = positionAt(editor, edit.offset);
5043
+ const endPosition = positionAt(editor, edit.offset + edit.deleted);
5044
+ const deleted = getSelectionText(editor, {
5045
+ end: endPosition,
5046
+ start: startPosition
5047
+ });
5048
+ const textChange = {
5049
+ deleted,
5050
+ end: endPosition,
5051
+ inserted: [edit.inserted],
5052
+ origin: Rename,
5053
+ start: startPosition
5054
+ };
5055
+ textChanges.push(textChange);
5056
+ }
5057
+ return textChanges;
5058
+ };
5059
+ const groupEditsByUri = changes => {
5060
+ const editsByUri = new Map();
5030
5061
  for (const change of changes) {
5031
- if (change.uri === editor.uri) {
5032
- for (const edit of change.edits) {
5033
- const startPosition = positionAt(editor, edit.offset);
5034
- const endPosition = positionAt(editor, edit.offset + edit.deleted);
5035
- const deleted = getSelectionText(editor, {
5036
- end: endPosition,
5037
- start: startPosition
5038
- });
5039
- const textChange = {
5040
- deleted,
5041
- end: endPosition,
5042
- inserted: [edit.inserted],
5043
- origin: Rename,
5044
- start: startPosition
5045
- };
5046
- textChanges.push(textChange);
5047
- }
5062
+ const edits = editsByUri.get(change.uri);
5063
+ if (edits) {
5064
+ edits.push(...change.edits);
5065
+ } else {
5066
+ editsByUri.set(change.uri, [...change.edits]);
5048
5067
  }
5049
5068
  }
5050
- return textChanges;
5069
+ return editsByUri;
5070
+ };
5071
+ const getOpenEditor = (currentEditor, uri) => {
5072
+ if (currentEditor.uri === uri) {
5073
+ return currentEditor;
5074
+ }
5075
+ for (const key of getKeys$2()) {
5076
+ const editor = get$7(Number(key))?.newState;
5077
+ if (editor && !editor.initial && editor.uri === uri) {
5078
+ return editor;
5079
+ }
5080
+ }
5081
+ return undefined;
5051
5082
  };
5052
5083
  const applyWorkspaceEdit = async (editor, changes) => {
5053
5084
  object(editor);
5054
5085
  array(changes);
5055
- const textChanges = getTextChanges(editor, changes);
5056
- if (textChanges.length === 0) {
5057
- return editor;
5086
+ let currentEditor = editor;
5087
+ const editsByUri = groupEditsByUri(changes);
5088
+ for (const [uri, edits] of editsByUri) {
5089
+ const openEditor = getOpenEditor(currentEditor, uri);
5090
+ if (openEditor) {
5091
+ const textChanges = getTextChanges(openEditor, edits);
5092
+ const updatedEditor = await scheduleDocumentAndCursorsSelections(openEditor, textChanges);
5093
+ if (openEditor.uid === currentEditor.uid) {
5094
+ currentEditor = updatedEditor;
5095
+ }
5096
+ continue;
5097
+ }
5098
+ const text = await readFile(uri);
5099
+ const newText = applyEditsToText(text, edits);
5100
+ await invoke$7('FileSystem.writeFile', uri, newText);
5058
5101
  }
5059
- // TODO
5060
- // for now only apply edits to single file, if it matches the URI
5061
- //
5062
- // in the future:
5063
- // 1. if a change targets the current editor, apply an edit to this editor
5064
- // 2. else, use file system worker to write file change
5065
- return scheduleDocumentAndCursorsSelections(editor, textChanges);
5102
+ return currentEditor;
5066
5103
  };
5067
5104
 
5068
5105
  const isPersistentWidget = widgetId => {
@@ -12354,7 +12391,7 @@ const loadContent = async (state, savedState) => {
12354
12391
  let content = existingEditor ? getText$1(existingEditor) : '';
12355
12392
  try {
12356
12393
  if (!existingEditor) {
12357
- content = await readFile(uri);
12394
+ content = await readFile$1(uri);
12358
12395
  }
12359
12396
  } catch (error) {
12360
12397
  const newEditor1 = setBounds(newEditor0, x, y, width, height, 9);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/editor-worker",
3
- "version": "19.28.1",
3
+ "version": "19.28.2",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git@github.com:lvce-editor/editor-worker.git"