@lvce-editor/preview-sandbox-worker 3.1.0 → 3.3.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/previewSandBoxWorkerMain.js +188 -33
- package/package.json +1 -1
|
@@ -1343,6 +1343,32 @@ const TAGS_TO_SKIP_TAG_ONLY = new Set(['html', 'body', 'head']);
|
|
|
1343
1343
|
|
|
1344
1344
|
// Tags where we extract content as CSS
|
|
1345
1345
|
const CSS_TAGS = new Set(['style']);
|
|
1346
|
+
const serializeAttributeName = attrName => {
|
|
1347
|
+
if (attrName === 'class') {
|
|
1348
|
+
return 'className';
|
|
1349
|
+
}
|
|
1350
|
+
if (attrName === 'type') {
|
|
1351
|
+
return 'inputType';
|
|
1352
|
+
}
|
|
1353
|
+
return attrName;
|
|
1354
|
+
};
|
|
1355
|
+
const applyAllowedAttributes = (targetNode, sourceNode) => {
|
|
1356
|
+
const {
|
|
1357
|
+
attributes
|
|
1358
|
+
} = sourceNode;
|
|
1359
|
+
if (!attributes) {
|
|
1360
|
+
return;
|
|
1361
|
+
}
|
|
1362
|
+
for (let i = 0; i < attributes.length; i++) {
|
|
1363
|
+
const attr = attributes[i];
|
|
1364
|
+
const attrName = attr.name;
|
|
1365
|
+
if (isDefaultAllowedAttribute(attrName, [])) {
|
|
1366
|
+
const finalName = serializeAttributeName(attrName);
|
|
1367
|
+
targetNode[finalName] = attr.value;
|
|
1368
|
+
}
|
|
1369
|
+
}
|
|
1370
|
+
};
|
|
1371
|
+
|
|
1346
1372
|
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
|
|
1347
1373
|
const serializeNode = (node, dom, css, context) => {
|
|
1348
1374
|
const {
|
|
@@ -1398,6 +1424,7 @@ const serializeNode = (node, dom, css, context) => {
|
|
|
1398
1424
|
type: Reference,
|
|
1399
1425
|
uid: node.__canvasId
|
|
1400
1426
|
};
|
|
1427
|
+
applyAllowedAttributes(refNode, node);
|
|
1401
1428
|
if (context.elementMap) {
|
|
1402
1429
|
context.elementMap[node.__canvasId + ''] = node;
|
|
1403
1430
|
}
|
|
@@ -1410,26 +1437,7 @@ const serializeNode = (node, dom, css, context) => {
|
|
|
1410
1437
|
childCount: 0,
|
|
1411
1438
|
type: getVirtualDomTag(tagName)
|
|
1412
1439
|
};
|
|
1413
|
-
|
|
1414
|
-
// Copy allowed attributes
|
|
1415
|
-
const {
|
|
1416
|
-
attributes
|
|
1417
|
-
} = node;
|
|
1418
|
-
if (attributes) {
|
|
1419
|
-
for (let i = 0; i < attributes.length; i++) {
|
|
1420
|
-
const attr = attributes[i];
|
|
1421
|
-
const attrName = attr.name;
|
|
1422
|
-
if (isDefaultAllowedAttribute(attrName, [])) {
|
|
1423
|
-
let finalName = attrName;
|
|
1424
|
-
if (attrName === 'class') {
|
|
1425
|
-
finalName = 'className';
|
|
1426
|
-
} else if (attrName === 'type') {
|
|
1427
|
-
finalName = 'inputType';
|
|
1428
|
-
}
|
|
1429
|
-
newNode[finalName] = attr.value;
|
|
1430
|
-
}
|
|
1431
|
-
}
|
|
1432
|
-
}
|
|
1440
|
+
applyAllowedAttributes(newNode, node);
|
|
1433
1441
|
|
|
1434
1442
|
// Assign element tracking ID for interactivity
|
|
1435
1443
|
const hdId = String(context.nextId++);
|
|
@@ -1599,12 +1607,137 @@ const handleInput = (uid, hdId, value) => {
|
|
|
1599
1607
|
return handleInputLocal(uid, hdId, value);
|
|
1600
1608
|
};
|
|
1601
1609
|
|
|
1610
|
+
const numericCodeMap = {
|
|
1611
|
+
13: 'Enter',
|
|
1612
|
+
27: 'Escape',
|
|
1613
|
+
32: 'Space',
|
|
1614
|
+
37: 'ArrowLeft',
|
|
1615
|
+
38: 'ArrowUp',
|
|
1616
|
+
39: 'ArrowRight',
|
|
1617
|
+
40: 'ArrowDown',
|
|
1618
|
+
8: 'Backspace',
|
|
1619
|
+
9: 'Tab'
|
|
1620
|
+
};
|
|
1621
|
+
|
|
1622
|
+
// cspell:ignore Spacebar
|
|
1623
|
+
|
|
1624
|
+
const invalidCodeValues = new Set(['code', 'event.code', 'e.code', 'key', 'event.key', 'e.key']);
|
|
1625
|
+
const getNumericCode = code => {
|
|
1626
|
+
if (typeof code === 'number' && Number.isFinite(code)) {
|
|
1627
|
+
return code;
|
|
1628
|
+
}
|
|
1629
|
+
if (typeof code === 'string' && /^\d+$/.test(code)) {
|
|
1630
|
+
return Number(code);
|
|
1631
|
+
}
|
|
1632
|
+
return undefined;
|
|
1633
|
+
};
|
|
1634
|
+
const getCodeFromNumericCode = numericCode => {
|
|
1635
|
+
if (numericCodeMap[numericCode]) {
|
|
1636
|
+
return numericCodeMap[numericCode];
|
|
1637
|
+
}
|
|
1638
|
+
if (numericCode >= 48 && numericCode <= 57) {
|
|
1639
|
+
return `Digit${String.fromCodePoint(numericCode)}`;
|
|
1640
|
+
}
|
|
1641
|
+
if (numericCode >= 65 && numericCode <= 90) {
|
|
1642
|
+
return `Key${String.fromCodePoint(numericCode)}`;
|
|
1643
|
+
}
|
|
1644
|
+
return String(numericCode);
|
|
1645
|
+
};
|
|
1646
|
+
const getCodeFromKey = key => {
|
|
1647
|
+
if (key === ' ' || key === 'Space' || key === 'Spacebar') {
|
|
1648
|
+
return 'Space';
|
|
1649
|
+
}
|
|
1650
|
+
if (key === 'Enter' || key.startsWith('Arrow')) {
|
|
1651
|
+
return key;
|
|
1652
|
+
}
|
|
1653
|
+
if (/^[a-z]$/i.test(key)) {
|
|
1654
|
+
return `Key${key.toUpperCase()}`;
|
|
1655
|
+
}
|
|
1656
|
+
if (/^\d$/.test(key)) {
|
|
1657
|
+
return `Digit${key}`;
|
|
1658
|
+
}
|
|
1659
|
+
return key;
|
|
1660
|
+
};
|
|
1661
|
+
const isInvalidCodeString = code => {
|
|
1662
|
+
const trimmedCode = code.trim();
|
|
1663
|
+
if (!trimmedCode) {
|
|
1664
|
+
return true;
|
|
1665
|
+
}
|
|
1666
|
+
return invalidCodeValues.has(trimmedCode);
|
|
1667
|
+
};
|
|
1668
|
+
const getNormalizedCode = (key, code) => {
|
|
1669
|
+
const numericCode = getNumericCode(code);
|
|
1670
|
+
if (numericCode !== undefined) {
|
|
1671
|
+
return getCodeFromNumericCode(numericCode);
|
|
1672
|
+
}
|
|
1673
|
+
if (typeof code === 'string' && !isInvalidCodeString(code)) {
|
|
1674
|
+
return code.trim();
|
|
1675
|
+
}
|
|
1676
|
+
return getCodeFromKey(key);
|
|
1677
|
+
};
|
|
1678
|
+
const getNormalizedKey = (key, normalizedCode) => {
|
|
1679
|
+
if (key === 'Space' || key === 'Spacebar') {
|
|
1680
|
+
return ' ';
|
|
1681
|
+
}
|
|
1682
|
+
if (key) {
|
|
1683
|
+
return key;
|
|
1684
|
+
}
|
|
1685
|
+
if (normalizedCode === 'Space') {
|
|
1686
|
+
return ' ';
|
|
1687
|
+
}
|
|
1688
|
+
if (/^Key[A-Z]$/.test(normalizedCode)) {
|
|
1689
|
+
return normalizedCode.slice(3).toLowerCase();
|
|
1690
|
+
}
|
|
1691
|
+
if (/^Digit\d$/.test(normalizedCode)) {
|
|
1692
|
+
return normalizedCode.slice(5);
|
|
1693
|
+
}
|
|
1694
|
+
return normalizedCode;
|
|
1695
|
+
};
|
|
1696
|
+
const getLegacyKeyCode = (normalizedCode, code) => {
|
|
1697
|
+
const numericCode = getNumericCode(code);
|
|
1698
|
+
if (numericCode !== undefined) {
|
|
1699
|
+
return numericCode;
|
|
1700
|
+
}
|
|
1701
|
+
for (const [key, value] of Object.entries(numericCodeMap)) {
|
|
1702
|
+
if (value === normalizedCode) {
|
|
1703
|
+
return Number(key);
|
|
1704
|
+
}
|
|
1705
|
+
}
|
|
1706
|
+
if (/^Key[A-Z]$/.test(normalizedCode)) {
|
|
1707
|
+
return normalizedCode.codePointAt(3) ?? 0;
|
|
1708
|
+
}
|
|
1709
|
+
if (/^Digit\d$/.test(normalizedCode)) {
|
|
1710
|
+
return normalizedCode.codePointAt(5) ?? 0;
|
|
1711
|
+
}
|
|
1712
|
+
return 0;
|
|
1713
|
+
};
|
|
1714
|
+
const getKeyboardEventInit = (key, code) => {
|
|
1715
|
+
const normalizedCode = getNormalizedCode(key, code);
|
|
1716
|
+
const normalizedKey = getNormalizedKey(key, normalizedCode);
|
|
1717
|
+
const keyCode = getLegacyKeyCode(normalizedCode, code);
|
|
1718
|
+
return {
|
|
1719
|
+
code: normalizedCode,
|
|
1720
|
+
key: normalizedKey,
|
|
1721
|
+
keyCode
|
|
1722
|
+
};
|
|
1723
|
+
};
|
|
1724
|
+
|
|
1725
|
+
const setLegacyKeyCode$1 = (event, keyCode) => {
|
|
1726
|
+
Object.defineProperty(event, 'keyCode', {
|
|
1727
|
+
value: keyCode
|
|
1728
|
+
});
|
|
1729
|
+
Object.defineProperty(event, 'which', {
|
|
1730
|
+
value: keyCode
|
|
1731
|
+
});
|
|
1732
|
+
};
|
|
1602
1733
|
const dispatchKeydownEvent = (element, window, key, code) => {
|
|
1734
|
+
const keyboardEventInit = getKeyboardEventInit(key, code);
|
|
1603
1735
|
const keydownEvent = new window.KeyboardEvent('keydown', {
|
|
1604
1736
|
bubbles: true,
|
|
1605
|
-
code,
|
|
1606
|
-
key
|
|
1737
|
+
code: keyboardEventInit.code,
|
|
1738
|
+
key: keyboardEventInit.key
|
|
1607
1739
|
});
|
|
1740
|
+
setLegacyKeyCode$1(keydownEvent, keyboardEventInit.keyCode);
|
|
1608
1741
|
dispatchEvent(element, keydownEvent);
|
|
1609
1742
|
};
|
|
1610
1743
|
|
|
@@ -1613,28 +1746,43 @@ const handleKeydownLocal = (uid, hdId, key, code) => {
|
|
|
1613
1746
|
if (!happyDomInstance) {
|
|
1614
1747
|
return;
|
|
1615
1748
|
}
|
|
1616
|
-
const
|
|
1749
|
+
const {
|
|
1750
|
+
document,
|
|
1751
|
+
elementMap,
|
|
1752
|
+
window
|
|
1753
|
+
} = happyDomInstance;
|
|
1754
|
+
const element = hdId ? elementMap[hdId] : document;
|
|
1617
1755
|
if (!element) {
|
|
1618
1756
|
return;
|
|
1619
1757
|
}
|
|
1620
|
-
dispatchKeydownEvent(element,
|
|
1621
|
-
const
|
|
1758
|
+
dispatchKeydownEvent(element, window, key, code);
|
|
1759
|
+
const newElementMap = Object.create(null);
|
|
1622
1760
|
set$1(uid, {
|
|
1623
|
-
document
|
|
1624
|
-
elementMap,
|
|
1625
|
-
window
|
|
1761
|
+
document,
|
|
1762
|
+
elementMap: newElementMap,
|
|
1763
|
+
window
|
|
1626
1764
|
});
|
|
1627
1765
|
};
|
|
1628
1766
|
const handleKeydown = (uid, hdId, key, code) => {
|
|
1629
1767
|
return handleKeydownLocal(uid, hdId, key, code);
|
|
1630
1768
|
};
|
|
1631
1769
|
|
|
1770
|
+
const setLegacyKeyCode = (event, keyCode) => {
|
|
1771
|
+
Object.defineProperty(event, 'keyCode', {
|
|
1772
|
+
value: keyCode
|
|
1773
|
+
});
|
|
1774
|
+
Object.defineProperty(event, 'which', {
|
|
1775
|
+
value: keyCode
|
|
1776
|
+
});
|
|
1777
|
+
};
|
|
1632
1778
|
const dispatchKeyupEvent = (element, window, key, code) => {
|
|
1779
|
+
const keyboardEventInit = getKeyboardEventInit(key, code);
|
|
1633
1780
|
const keyupEvent = new window.KeyboardEvent('keyup', {
|
|
1634
1781
|
bubbles: true,
|
|
1635
|
-
code,
|
|
1636
|
-
key
|
|
1782
|
+
code: keyboardEventInit.code,
|
|
1783
|
+
key: keyboardEventInit.key
|
|
1637
1784
|
});
|
|
1785
|
+
setLegacyKeyCode(keyupEvent, keyboardEventInit.keyCode);
|
|
1638
1786
|
dispatchEvent(element, keyupEvent);
|
|
1639
1787
|
};
|
|
1640
1788
|
|
|
@@ -89681,17 +89829,19 @@ const createLocalStorage = () => {
|
|
|
89681
89829
|
return localStorage;
|
|
89682
89830
|
};
|
|
89683
89831
|
|
|
89684
|
-
const getGlobals = (width, height) => {
|
|
89832
|
+
const getGlobals = (width, height, devicePixelRatio = 1) => {
|
|
89685
89833
|
const localStorage = createLocalStorage();
|
|
89686
89834
|
return {
|
|
89687
89835
|
globalGlobals: {
|
|
89688
89836
|
alert: alert,
|
|
89837
|
+
devicePixelRatio,
|
|
89689
89838
|
innerHeight: height,
|
|
89690
89839
|
innerWidth: width,
|
|
89691
89840
|
localStorage
|
|
89692
89841
|
},
|
|
89693
89842
|
windowGlobals: {
|
|
89694
89843
|
alert: alert,
|
|
89844
|
+
devicePixelRatio,
|
|
89695
89845
|
innerHeight: height,
|
|
89696
89846
|
innerWidth: width
|
|
89697
89847
|
}
|
|
@@ -89801,12 +89951,12 @@ const setGlobals = (window, globalGlobals, windowGlobals) => {
|
|
|
89801
89951
|
/* eslint-disable @typescript-eslint/prefer-readonly-parameter-types */
|
|
89802
89952
|
/* eslint-disable @typescript-eslint/no-implied-eval */
|
|
89803
89953
|
|
|
89804
|
-
const executeScripts = (window, document, scripts, width = 0, height = 0) => {
|
|
89954
|
+
const executeScripts = (window, document, scripts, width = 0, height = 0, devicePixelRatio = 1) => {
|
|
89805
89955
|
exposeCanvasGlobals(window, document);
|
|
89806
89956
|
const {
|
|
89807
89957
|
globalGlobals,
|
|
89808
89958
|
windowGlobals
|
|
89809
|
-
} = getGlobals(width, height);
|
|
89959
|
+
} = getGlobals(width, height, devicePixelRatio);
|
|
89810
89960
|
setGlobals(window, globalGlobals, windowGlobals);
|
|
89811
89961
|
let firstError = null;
|
|
89812
89962
|
let firstCodeFrame = '';
|
|
@@ -89918,6 +90068,9 @@ const toNumber = value => {
|
|
|
89918
90068
|
|
|
89919
90069
|
/* eslint-disable @typescript-eslint/prefer-readonly-parameter-types */
|
|
89920
90070
|
|
|
90071
|
+
const reflectCanvasDimensionAttribute = (element, name, value) => {
|
|
90072
|
+
element.setAttribute(name, String(value));
|
|
90073
|
+
};
|
|
89921
90074
|
const patchCanvasElements = async (document, uid) => {
|
|
89922
90075
|
const canvasElements = document.querySelectorAll('canvas');
|
|
89923
90076
|
if (canvasElements.length === 0) {
|
|
@@ -89965,6 +90118,7 @@ const patchCanvasElements = async (document, uid) => {
|
|
|
89965
90118
|
get: () => widthValue,
|
|
89966
90119
|
set: newWidth => {
|
|
89967
90120
|
widthValue = toNumber(newWidth);
|
|
90121
|
+
reflectCanvasDimensionAttribute(element, 'width', widthValue);
|
|
89968
90122
|
// @ts-ignore
|
|
89969
90123
|
element.__offscreenCanvas.width = widthValue;
|
|
89970
90124
|
}
|
|
@@ -89978,6 +90132,7 @@ const patchCanvasElements = async (document, uid) => {
|
|
|
89978
90132
|
get: () => heightValue,
|
|
89979
90133
|
set: newHeight => {
|
|
89980
90134
|
heightValue = toNumber(newHeight);
|
|
90135
|
+
reflectCanvasDimensionAttribute(element, 'height', heightValue);
|
|
89981
90136
|
// @ts-ignore
|
|
89982
90137
|
element.__offscreenCanvas.height = heightValue;
|
|
89983
90138
|
}
|