@lvce-editor/preview-worker 2.3.0 → 2.5.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.
@@ -514,7 +514,7 @@ const IpcParentWithMessagePort$1 = {
514
514
 
515
515
  const Two$1 = '2.0';
516
516
  const callbacks$1 = Object.create(null);
517
- const get$4 = id => {
517
+ const get$3 = id => {
518
518
  return callbacks$1[id];
519
519
  };
520
520
  const remove$1 = id => {
@@ -663,7 +663,7 @@ const warn = (...args) => {
663
663
  console.warn(...args);
664
664
  };
665
665
  const resolve = (id, response) => {
666
- const fn = get$4(id);
666
+ const fn = get$3(id);
667
667
  if (!fn) {
668
668
  console.log(response);
669
669
  warn(`callback ${id} may already be disposed`);
@@ -865,9 +865,9 @@ const create$s = (id, method, params) => {
865
865
  };
866
866
  return message;
867
867
  };
868
- let id = 0;
868
+ let id$1 = 0;
869
869
  const create$r = () => {
870
- return ++id;
870
+ return ++id$1;
871
871
  };
872
872
 
873
873
  /* eslint-disable n/no-unsupported-features/es-syntax */
@@ -1142,7 +1142,7 @@ const rpcs = Object.create(null);
1142
1142
  const set$5 = (id, rpc) => {
1143
1143
  rpcs[id] = rpc;
1144
1144
  };
1145
- const get$3 = id => {
1145
+ const get$2 = id => {
1146
1146
  return rpcs[id];
1147
1147
  };
1148
1148
  const remove = id => {
@@ -1153,18 +1153,18 @@ const remove = id => {
1153
1153
  const create$2 = rpcId => {
1154
1154
  return {
1155
1155
  async dispose() {
1156
- const rpc = get$3(rpcId);
1156
+ const rpc = get$2(rpcId);
1157
1157
  await rpc.dispose();
1158
1158
  },
1159
1159
  // @ts-ignore
1160
1160
  invoke(method, ...params) {
1161
- const rpc = get$3(rpcId);
1161
+ const rpc = get$2(rpcId);
1162
1162
  // @ts-ignore
1163
1163
  return rpc.invoke(method, ...params);
1164
1164
  },
1165
1165
  // @ts-ignore
1166
1166
  invokeAndTransfer(method, ...params) {
1167
- const rpc = get$3(rpcId);
1167
+ const rpc = get$2(rpcId);
1168
1168
  // @ts-ignore
1169
1169
  return rpc.invokeAndTransfer(method, ...params);
1170
1170
  },
@@ -1294,7 +1294,7 @@ const terminate = () => {
1294
1294
  };
1295
1295
 
1296
1296
  const {
1297
- get: get$2,
1297
+ get: get$1,
1298
1298
  getCommandIds,
1299
1299
  getKeys: getKeys$1,
1300
1300
  registerCommands,
@@ -1360,11 +1360,48 @@ const diff2 = uid => {
1360
1360
  const {
1361
1361
  newState,
1362
1362
  oldState
1363
- } = get$2(uid);
1363
+ } = get$1(uid);
1364
1364
  const result = diff(oldState, newState);
1365
1365
  return result;
1366
1366
  };
1367
1367
 
1368
+ /* eslint-disable @typescript-eslint/prefer-readonly-parameter-types */
1369
+ const callBacks = Object.create(null);
1370
+ let id = 0;
1371
+ const registerCallback = () => {
1372
+ const nextId = id++;
1373
+ const {
1374
+ promise,
1375
+ resolve
1376
+ } = Promise.withResolvers();
1377
+ callBacks[nextId] = resolve;
1378
+ return {
1379
+ id: nextId,
1380
+ promise
1381
+ };
1382
+ };
1383
+ const executeCallback = (id, ...args) => {
1384
+ const callback = callBacks[id];
1385
+ if (callback) {
1386
+ callback(args);
1387
+ delete callBacks[id];
1388
+ } else {
1389
+ console.warn(`[preview-worker] No callback found for id ${id}`);
1390
+ }
1391
+ };
1392
+ const getOffscreenCanvas = async (width, height) => {
1393
+ const {
1394
+ id,
1395
+ promise
1396
+ } = registerCallback();
1397
+ await invoke('OffscreenCanvas.createForPreview', id, width, height);
1398
+ const [offscreenCanvas, canvasId] = await promise;
1399
+ return {
1400
+ canvasId,
1401
+ offscreenCanvas
1402
+ };
1403
+ };
1404
+
1368
1405
  const dispatchEvent = (element, event) => {
1369
1406
  element.dispatchEvent(event);
1370
1407
 
@@ -1420,7 +1457,7 @@ const getParsedNodesChildNodeCount = parsedDom => {
1420
1457
  /* eslint-disable @typescript-eslint/prefer-readonly-parameter-types */
1421
1458
 
1422
1459
  const states$1 = new Map();
1423
- const get$1 = uid => {
1460
+ const get = uid => {
1424
1461
  return states$1.get(uid);
1425
1462
  };
1426
1463
  const set$1 = (uid, instance) => {
@@ -1982,6 +2019,20 @@ const serializeNode = (node, dom, css, context) => {
1982
2019
  return childCount;
1983
2020
  }
1984
2021
 
2022
+ // Canvas element with transferred OffscreenCanvas — emit a Reference node
2023
+ if (tagName === 'canvas' && node.__canvasId !== undefined) {
2024
+ const refNode = {
2025
+ childCount: 0,
2026
+ type: Reference,
2027
+ uid: node.__canvasId
2028
+ };
2029
+ if (context.elementMap) {
2030
+ context.elementMap.set(node.__canvasId + '', node);
2031
+ }
2032
+ dom.push(refNode);
2033
+ return 1;
2034
+ }
2035
+
1985
2036
  // Normal element - create a VirtualDomNode
1986
2037
  const newNode = {
1987
2038
  childCount: 0,
@@ -2072,7 +2123,7 @@ const handleClick = (state, hdId) => {
2072
2123
  if (!hdId) {
2073
2124
  return state;
2074
2125
  }
2075
- const happyDomInstance = get$1(state.uid);
2126
+ const happyDomInstance = get(state.uid);
2076
2127
  if (!happyDomInstance) {
2077
2128
  return state;
2078
2129
  }
@@ -2674,7 +2725,7 @@ const handleEditorChanged = async () => {
2674
2725
  for (const previewUid of previewKeys) {
2675
2726
  const {
2676
2727
  newState: state
2677
- } = get$2(previewUid);
2728
+ } = get$1(previewUid);
2678
2729
 
2679
2730
  // Skip if no URI is set
2680
2731
  if (!state.uri) {
@@ -90349,65 +90400,11 @@ const executeScripts = (window, document, scripts) => {
90349
90400
  /* eslint-disable @typescript-eslint/prefer-readonly-parameter-types */
90350
90401
 
90351
90402
  const states = new Map();
90352
- const get = uid => {
90353
- return states.get(uid);
90354
- };
90355
90403
  const set = (uid, entry) => {
90356
90404
  states.set(uid, entry);
90357
90405
  };
90358
- const addAnimationFrameHandle = (uid, handle) => {
90359
- const entry = states.get(uid);
90360
- if (entry) {
90361
- entry.animationFrameHandles.push(handle);
90362
- }
90363
- };
90364
-
90365
- const flushCanvasFrame = uid => {
90366
- const canvasStateEntry = get(uid);
90367
- if (!canvasStateEntry) {
90368
- return;
90369
- }
90370
- for (const instance of canvasStateEntry.instances) {
90371
- try {
90372
- const bitmap = instance.offscreenCanvas.transferToImageBitmap();
90373
- // @ts-ignore
90374
- void invoke('Preview.drawCanvas', uid, instance.dataId, bitmap);
90375
- } catch {
90376
- // transferToImageBitmap can fail if canvas is zero-sized
90377
- }
90378
- }
90379
- };
90380
-
90381
- const FRAME_INTERVAL = 16;
90382
- const overrideRequestAnimationFrame = (window, uid) => {
90383
- let nextId = 1;
90384
- const callbacks = new Map();
90385
- const tick = () => {
90386
- const currentCallbacks = [...callbacks.entries()];
90387
- callbacks.clear();
90388
- const timestamp = performance.now();
90389
- for (const [, callback] of currentCallbacks) {
90390
- try {
90391
- callback(timestamp);
90392
- } catch (error) {
90393
- console.warn('[preview-worker] requestAnimationFrame callback error:', error);
90394
- }
90395
- }
90396
- flushCanvasFrame(uid);
90397
- };
90398
- window.requestAnimationFrame = callback => {
90399
- const id = nextId++;
90400
- callbacks.set(id, callback);
90401
- const handle = setTimeout(tick, FRAME_INTERVAL);
90402
- addAnimationFrameHandle(uid, handle);
90403
- return id;
90404
- };
90405
- window.cancelAnimationFrame = id => {
90406
- callbacks.delete(id);
90407
- };
90408
- };
90409
90406
 
90410
- const patchCanvasElements = (document, uid) => {
90407
+ const patchCanvasElements = async (document, uid) => {
90411
90408
  const canvasElements = document.querySelectorAll('canvas');
90412
90409
  if (canvasElements.length === 0) {
90413
90410
  return;
@@ -90415,10 +90412,16 @@ const patchCanvasElements = (document, uid) => {
90415
90412
  const instances = [];
90416
90413
  for (let i = 0; i < canvasElements.length; i++) {
90417
90414
  const element = canvasElements[i];
90418
- const width = Number.parseInt(element.getAttribute('width') || '300', 10);
90419
- const height = Number.parseInt(element.getAttribute('height') || '150', 10);
90420
- const offscreenCanvas = new OffscreenCanvas(width, height);
90421
- const dataId = String(i);
90415
+ const width = element.getAttribute('width') || 300;
90416
+ const height = element.getAttribute('height') || 300;
90417
+ element.width = width;
90418
+ element.height = height;
90419
+ const {
90420
+ canvasId,
90421
+ offscreenCanvas
90422
+ } = await getOffscreenCanvas(width, height);
90423
+ const dataId = String(canvasId);
90424
+ element.__canvasId = canvasId;
90422
90425
  const context = offscreenCanvas.getContext('2d');
90423
90426
  element.getContext = contextType => {
90424
90427
  if (contextType === '2d') {
@@ -90443,7 +90446,6 @@ const patchCanvasElements = (document, uid) => {
90443
90446
  const updateContent = async (state, uri) => {
90444
90447
  try {
90445
90448
  // Read the file content using RendererWorker RPC
90446
- // @ts-ignore
90447
90449
  const content = await readFile(uri);
90448
90450
 
90449
90451
  // Parse the content into virtual DOM and CSS
@@ -90463,8 +90465,7 @@ const updateContent = async (state, uri) => {
90463
90465
  document: happyDomDocument,
90464
90466
  window: happyDomWindow
90465
90467
  } = createWindow(content);
90466
- patchCanvasElements(happyDomDocument, state.uid);
90467
- overrideRequestAnimationFrame(happyDomWindow, state.uid);
90468
+ await patchCanvasElements(happyDomDocument, state.uid);
90468
90469
  executeScripts(happyDomWindow, happyDomDocument, scripts);
90469
90470
  const elementMap = new Map();
90470
90471
  const serialized = serialize(happyDomDocument, elementMap);
@@ -90475,7 +90476,8 @@ const updateContent = async (state, uri) => {
90475
90476
  elementMap,
90476
90477
  window: happyDomWindow
90477
90478
  });
90478
- } catch {
90479
+ } catch (error) {
90480
+ console.error(error);
90479
90481
  // If script execution fails, fall back to static HTML parsing
90480
90482
  }
90481
90483
  }
@@ -90534,7 +90536,7 @@ const handleInput = (state, hdId, value) => {
90534
90536
  if (!hdId) {
90535
90537
  return state;
90536
90538
  }
90537
- const happyDomInstance = get$1(state.uid);
90539
+ const happyDomInstance = get(state.uid);
90538
90540
  if (!happyDomInstance) {
90539
90541
  return state;
90540
90542
  }
@@ -90759,7 +90761,7 @@ const render2 = (uid, diffResult) => {
90759
90761
  const {
90760
90762
  newState,
90761
90763
  oldState
90762
- } = get$2(uid);
90764
+ } = get$1(uid);
90763
90765
  set$2(uid, newState, newState);
90764
90766
  const commands = applyRender(oldState, newState, diffResult);
90765
90767
  return commands;
@@ -90827,6 +90829,7 @@ const commandMap = {
90827
90829
  handleEditorChanged: handleEditorChanged,
90828
90830
  'Preview.create': create,
90829
90831
  'Preview.diff2': diff2,
90832
+ 'Preview.executeCallback': executeCallback,
90830
90833
  'Preview.getCommandIds': getCommandIds,
90831
90834
  'Preview.handleClick': wrapCommand(handleClick),
90832
90835
  'Preview.handleFileEdited': wrapCommand(handleFileEdited),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/preview-worker",
3
- "version": "2.3.0",
3
+ "version": "2.5.0",
4
4
  "description": "Preview Worker",
5
5
  "repository": {
6
6
  "type": "git",