@lvce-editor/editor-worker 5.2.0 → 5.4.0

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.
@@ -483,6 +483,10 @@ const getTabCount = string => {
483
483
  return count;
484
484
  };
485
485
 
486
+ const measureTextWidthFast = (text, charWidth) => {
487
+ return text.length * charWidth;
488
+ };
489
+
486
490
  const getFontString = (fontWeight, fontSize, fontFamily) => {
487
491
  return `${fontWeight} ${fontSize}px ${fontFamily}`;
488
492
  };
@@ -516,16 +520,15 @@ const getContext = () => {
516
520
  return ctx;
517
521
  };
518
522
 
519
- const measureTextWidth = (text, fontWeight, fontSize, fontFamily, letterSpacing, isMonoSpaceFont, charWidth) => {
523
+ // TODO for text editor, could dispose measuring canvas after editor has been initialized to free up offscreencanvas space
524
+
525
+ const measureTextWidthSlow = (text, fontWeight, fontSize, fontFamily, letterSpacing, isMonoSpaceFont, charWidth) => {
520
526
  string(text);
521
527
  number$1(fontWeight);
522
528
  number$1(fontSize);
523
529
  string(fontFamily);
524
530
  boolean(isMonoSpaceFont);
525
531
  number$1(charWidth);
526
- if (isMonoSpaceFont) {
527
- return text.length * charWidth;
528
- }
529
532
  if (typeof letterSpacing !== 'number') {
530
533
  throw new TypeError('letterSpacing must be of type number');
531
534
  }
@@ -539,6 +542,13 @@ const measureTextWidth = (text, fontWeight, fontSize, fontFamily, letterSpacing,
539
542
  return width;
540
543
  };
541
544
 
545
+ const measureTextWidth = (text, fontWeight, fontSize, fontFamily, letterSpacing, isMonoSpaceFont, charWidth) => {
546
+ if (isMonoSpaceFont) {
547
+ return measureTextWidthFast(text, charWidth);
548
+ }
549
+ return measureTextWidthSlow(text, fontWeight, fontSize, fontFamily, letterSpacing, isMonoSpaceFont, charWidth);
550
+ };
551
+
542
552
  const normalizeText = (text, normalize, tabSize) => {
543
553
  if (normalize) {
544
554
  return text.replaceAll(Tab, Space$1.repeat(tabSize));
@@ -1797,28 +1807,54 @@ const getDiagnosticType = diagnostic => {
1797
1807
 
1798
1808
  const getVisibleDiagnostics = (editor, diagnostics) => {
1799
1809
  const visibleDiagnostics = [];
1810
+ const {
1811
+ width,
1812
+ rowHeight,
1813
+ minLineY,
1814
+ charWidth,
1815
+ letterSpacing,
1816
+ lines,
1817
+ fontWeight,
1818
+ fontSize,
1819
+ fontFamily,
1820
+ isMonospaceFont,
1821
+ tabSize
1822
+ } = editor;
1800
1823
  for (const diagnostic of diagnostics) {
1824
+ const {
1825
+ rowIndex,
1826
+ columnIndex,
1827
+ endColumnIndex
1828
+ } = diagnostic;
1829
+ const columnDelta = endColumnIndex - columnIndex;
1830
+ const diagnosticWidth = columnDelta * charWidth;
1831
+ const endLineDifference = 0;
1832
+ const halfCursorWidth = 0;
1833
+ const x = getX(lines[rowIndex], columnIndex, fontWeight, fontSize, fontFamily, isMonospaceFont, letterSpacing, tabSize, halfCursorWidth, width, charWidth, endLineDifference);
1834
+ const y = getY(rowIndex, minLineY, rowHeight);
1801
1835
  visibleDiagnostics.push({
1802
- x: diagnostic.columnIndex * editor.columnWidth,
1803
- y: (diagnostic.rowIndex - editor.minLineY) * editor.rowHeight,
1804
- width: 20,
1805
- height: editor.rowHeight,
1836
+ x,
1837
+ y,
1838
+ width: diagnosticWidth,
1839
+ height: rowHeight,
1806
1840
  type: getDiagnosticType(diagnostic)
1807
1841
  });
1808
1842
  }
1809
1843
  return visibleDiagnostics;
1810
1844
  };
1811
1845
 
1812
- const updateDiagnostics = async uid => {
1846
+ const updateDiagnostics = async newState => {
1813
1847
  try {
1814
1848
  // TODO handle error
1815
1849
  // TODO handle race condition
1816
- const {
1817
- newState
1818
- } = get$6(uid);
1819
- // TODO request diagnostics from extension host
1850
+
1851
+ // TODO sync textdocument incrementally
1852
+ // TODO sync and ask for diagnostics at the same time?
1853
+ // TODO throttle diagnostics
1854
+ const content = getText$1(newState);
1855
+ await invoke$2(TextDocumentSyncFull, newState.uri, newState.id, newState.languageId, content);
1820
1856
  const diagnostics = await executeDiagnosticProvider(newState);
1821
- const latest = get$6(uid);
1857
+ const latest = get$6(newState.id);
1822
1858
  if (!latest) {
1823
1859
  return;
1824
1860
  }
@@ -1828,9 +1864,13 @@ const updateDiagnostics = async uid => {
1828
1864
  diagnostics,
1829
1865
  decorations
1830
1866
  };
1831
- set$6(uid, latest.oldState, newEditor);
1832
- await invoke$3('Editor.rerender', uid);
1867
+ set$6(newState.id, latest.oldState, newEditor);
1868
+ await invoke$3('Editor.rerender', newState.id);
1833
1869
  } catch (error) {
1870
+ // @ts-ignore
1871
+ if (error && error.message.includes('No diagnostic provider found')) {
1872
+ return;
1873
+ }
1834
1874
  console.error(`Failed to update diagnostics: ${error}`);
1835
1875
  }
1836
1876
  };
@@ -1960,7 +2000,7 @@ const createEditor = async ({
1960
2000
  set$6(id, emptyEditor, newEditor4);
1961
2001
  await invoke$2(TextDocumentSyncFull, uri, id, languageId, content);
1962
2002
  if (diagnosticsEnabled) {
1963
- updateDiagnostics(editor.uid);
2003
+ updateDiagnostics(newEditor4);
1964
2004
  }
1965
2005
  };
1966
2006
 
@@ -8377,9 +8417,6 @@ const loadFont = async (fontName, fontUrl) => {
8377
8417
  }
8378
8418
  };
8379
8419
 
8380
- const load = async (fontName, fontUrl) => {
8381
- return loadFont(fontName, fontUrl);
8382
- };
8383
8420
  const ensure = async (fontName, fontUrl) => {
8384
8421
  if (isLoaded(fontName)) {
8385
8422
  return;
@@ -8387,7 +8424,7 @@ const ensure = async (fontName, fontUrl) => {
8387
8424
  if (hasPending(fontName)) {
8388
8425
  return getPending(fontName);
8389
8426
  }
8390
- const promise = load(fontName, fontUrl);
8427
+ const promise = loadFont(fontName, fontUrl);
8391
8428
  setPending(fontName, promise);
8392
8429
  await promise;
8393
8430
  removePending(fontName);
@@ -9326,6 +9363,17 @@ const renderEditor = async id => {
9326
9363
  return commands;
9327
9364
  };
9328
9365
 
9366
+ const editorDiagnosticEffect = {
9367
+ isActive(oldEditor, newEditor) {
9368
+ // TODO avoid slow comparison
9369
+ return newEditor.diagnosticsEnabled && JSON.stringify(oldEditor.lines) !== JSON.stringify(newEditor.lines);
9370
+ },
9371
+ // TODO set effects delay / diagnostic delay
9372
+ async apply(editor) {
9373
+ await updateDiagnostics(editor);
9374
+ }
9375
+ };
9376
+
9329
9377
  const keep = [
9330
9378
  // 'ColorPicker.handleSliderPointerDown',
9331
9379
  // 'ColorPicker.handleSliderPointerMove',
@@ -9383,9 +9431,16 @@ const widgetCommands = {
9383
9431
 
9384
9432
  // TODO wrap commands globally, not per editor
9385
9433
  // TODO only store editor state in editor worker, not in renderer worker also
9434
+
9435
+ const effects = [editorDiagnosticEffect];
9386
9436
  const wrapCommand = fn => async (editorUid, ...args) => {
9387
9437
  const oldInstance = get$6(editorUid);
9388
9438
  const newEditor = await fn(oldInstance.newState, ...args);
9439
+ for (const effect of effects) {
9440
+ if (effect.isActive(oldInstance.newState, newEditor)) {
9441
+ effect.apply(newEditor);
9442
+ }
9443
+ }
9389
9444
  set$6(editorUid, oldInstance.newState, newEditor);
9390
9445
  // TODO if possible, rendering should be sync
9391
9446
  const commands = await renderEditor(editorUid);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/editor-worker",
3
- "version": "5.2.0",
3
+ "version": "5.4.0",
4
4
  "description": "",
5
5
  "main": "dist/editorWorkerMain.js",
6
6
  "type": "module",