@lvce-editor/preview-worker 2.3.0 → 2.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.
- package/dist/previewWorkerMain.js +79 -41
- package/package.json +1 -1
|
@@ -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$
|
|
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$
|
|
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$
|
|
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$
|
|
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$
|
|
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$
|
|
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$
|
|
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$
|
|
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
|
|
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
|
|
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$
|
|
2728
|
+
} = get$1(previewUid);
|
|
2678
2729
|
|
|
2679
2730
|
// Skip if no URI is set
|
|
2680
2731
|
if (!state.uri) {
|
|
@@ -90349,9 +90400,6 @@ 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
|
};
|
|
@@ -90362,22 +90410,6 @@ const addAnimationFrameHandle = (uid, handle) => {
|
|
|
90362
90410
|
}
|
|
90363
90411
|
};
|
|
90364
90412
|
|
|
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
90413
|
const FRAME_INTERVAL = 16;
|
|
90382
90414
|
const overrideRequestAnimationFrame = (window, uid) => {
|
|
90383
90415
|
let nextId = 1;
|
|
@@ -90393,7 +90425,6 @@ const overrideRequestAnimationFrame = (window, uid) => {
|
|
|
90393
90425
|
console.warn('[preview-worker] requestAnimationFrame callback error:', error);
|
|
90394
90426
|
}
|
|
90395
90427
|
}
|
|
90396
|
-
flushCanvasFrame(uid);
|
|
90397
90428
|
};
|
|
90398
90429
|
window.requestAnimationFrame = callback => {
|
|
90399
90430
|
const id = nextId++;
|
|
@@ -90407,7 +90438,7 @@ const overrideRequestAnimationFrame = (window, uid) => {
|
|
|
90407
90438
|
};
|
|
90408
90439
|
};
|
|
90409
90440
|
|
|
90410
|
-
const patchCanvasElements = (document, uid) => {
|
|
90441
|
+
const patchCanvasElements = async (document, uid) => {
|
|
90411
90442
|
const canvasElements = document.querySelectorAll('canvas');
|
|
90412
90443
|
if (canvasElements.length === 0) {
|
|
90413
90444
|
return;
|
|
@@ -90415,10 +90446,16 @@ const patchCanvasElements = (document, uid) => {
|
|
|
90415
90446
|
const instances = [];
|
|
90416
90447
|
for (let i = 0; i < canvasElements.length; i++) {
|
|
90417
90448
|
const element = canvasElements[i];
|
|
90418
|
-
const width =
|
|
90419
|
-
const height =
|
|
90420
|
-
|
|
90421
|
-
|
|
90449
|
+
const width = element.getAttribute('width') || 300;
|
|
90450
|
+
const height = element.getAttribute('height') || 300;
|
|
90451
|
+
element.width = width;
|
|
90452
|
+
element.height = height;
|
|
90453
|
+
const {
|
|
90454
|
+
canvasId,
|
|
90455
|
+
offscreenCanvas
|
|
90456
|
+
} = await getOffscreenCanvas(width, height);
|
|
90457
|
+
const dataId = String(canvasId);
|
|
90458
|
+
element.__canvasId = canvasId;
|
|
90422
90459
|
const context = offscreenCanvas.getContext('2d');
|
|
90423
90460
|
element.getContext = contextType => {
|
|
90424
90461
|
if (contextType === '2d') {
|
|
@@ -90463,7 +90500,7 @@ const updateContent = async (state, uri) => {
|
|
|
90463
90500
|
document: happyDomDocument,
|
|
90464
90501
|
window: happyDomWindow
|
|
90465
90502
|
} = createWindow(content);
|
|
90466
|
-
patchCanvasElements(happyDomDocument, state.uid);
|
|
90503
|
+
await patchCanvasElements(happyDomDocument, state.uid);
|
|
90467
90504
|
overrideRequestAnimationFrame(happyDomWindow, state.uid);
|
|
90468
90505
|
executeScripts(happyDomWindow, happyDomDocument, scripts);
|
|
90469
90506
|
const elementMap = new Map();
|
|
@@ -90534,7 +90571,7 @@ const handleInput = (state, hdId, value) => {
|
|
|
90534
90571
|
if (!hdId) {
|
|
90535
90572
|
return state;
|
|
90536
90573
|
}
|
|
90537
|
-
const happyDomInstance = get
|
|
90574
|
+
const happyDomInstance = get(state.uid);
|
|
90538
90575
|
if (!happyDomInstance) {
|
|
90539
90576
|
return state;
|
|
90540
90577
|
}
|
|
@@ -90759,7 +90796,7 @@ const render2 = (uid, diffResult) => {
|
|
|
90759
90796
|
const {
|
|
90760
90797
|
newState,
|
|
90761
90798
|
oldState
|
|
90762
|
-
} = get$
|
|
90799
|
+
} = get$1(uid);
|
|
90763
90800
|
set$2(uid, newState, newState);
|
|
90764
90801
|
const commands = applyRender(oldState, newState, diffResult);
|
|
90765
90802
|
return commands;
|
|
@@ -90827,6 +90864,7 @@ const commandMap = {
|
|
|
90827
90864
|
handleEditorChanged: handleEditorChanged,
|
|
90828
90865
|
'Preview.create': create,
|
|
90829
90866
|
'Preview.diff2': diff2,
|
|
90867
|
+
'Preview.executeCallback': executeCallback,
|
|
90830
90868
|
'Preview.getCommandIds': getCommandIds,
|
|
90831
90869
|
'Preview.handleClick': wrapCommand(handleClick),
|
|
90832
90870
|
'Preview.handleFileEdited': wrapCommand(handleFileEdited),
|