@lvce-editor/preview-worker 2.5.0 → 2.7.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 +192 -4
- package/package.json +2 -2
|
@@ -1411,6 +1411,9 @@ const dispatchEvent = (element, event) => {
|
|
|
1411
1411
|
if (typeof handler === 'function') {
|
|
1412
1412
|
handler.call(element, event);
|
|
1413
1413
|
} else if (handler === null || handler === undefined) {
|
|
1414
|
+
if (!element.getAttribute) {
|
|
1415
|
+
return;
|
|
1416
|
+
}
|
|
1414
1417
|
// Check if there's an inline HTML attribute that wasn't converted to a property
|
|
1415
1418
|
const attrValue = element.getAttribute(handlerName);
|
|
1416
1419
|
if (attrValue && typeof attrValue === 'string' && element.ownerDocument && element.ownerDocument.defaultView) {
|
|
@@ -90366,12 +90369,89 @@ const alert = message => {
|
|
|
90366
90369
|
void invoke('ConfirmPrompt.prompt', message);
|
|
90367
90370
|
};
|
|
90368
90371
|
|
|
90372
|
+
// const FUNCTION_REGEX = /(?:^|[\n;])\s*function\s+([a-zA-Z_$][\w$]*)/g
|
|
90373
|
+
|
|
90369
90374
|
const getTopLevelFunctionNames = script => {
|
|
90370
90375
|
const names = [];
|
|
90371
|
-
|
|
90372
|
-
let
|
|
90373
|
-
while (
|
|
90374
|
-
|
|
90376
|
+
let braceDepth = 0;
|
|
90377
|
+
let i = 0;
|
|
90378
|
+
while (i < script.length) {
|
|
90379
|
+
const char = script[i];
|
|
90380
|
+
|
|
90381
|
+
// Skip strings
|
|
90382
|
+
if (char === '"' || char === "'" || char === '`') {
|
|
90383
|
+
const quote = char;
|
|
90384
|
+
i++;
|
|
90385
|
+
while (i < script.length) {
|
|
90386
|
+
if (script[i] === '\\') {
|
|
90387
|
+
i += 2;
|
|
90388
|
+
continue;
|
|
90389
|
+
}
|
|
90390
|
+
if (script[i] === quote) {
|
|
90391
|
+
i++;
|
|
90392
|
+
break;
|
|
90393
|
+
}
|
|
90394
|
+
i++;
|
|
90395
|
+
}
|
|
90396
|
+
continue;
|
|
90397
|
+
}
|
|
90398
|
+
|
|
90399
|
+
// Skip comments
|
|
90400
|
+
if (char === '/' && script[i + 1] === '/') {
|
|
90401
|
+
// Line comment
|
|
90402
|
+
i += 2;
|
|
90403
|
+
while (i < script.length && script[i] !== '\n') {
|
|
90404
|
+
i++;
|
|
90405
|
+
}
|
|
90406
|
+
continue;
|
|
90407
|
+
}
|
|
90408
|
+
if (char === '/' && script[i + 1] === '*') {
|
|
90409
|
+
// Block comment
|
|
90410
|
+
i += 2;
|
|
90411
|
+
while (i < script.length - 1) {
|
|
90412
|
+
if (script[i] === '*' && script[i + 1] === '/') {
|
|
90413
|
+
i += 2;
|
|
90414
|
+
break;
|
|
90415
|
+
}
|
|
90416
|
+
i++;
|
|
90417
|
+
}
|
|
90418
|
+
continue;
|
|
90419
|
+
}
|
|
90420
|
+
|
|
90421
|
+
// Track braces
|
|
90422
|
+
if (char === '{') {
|
|
90423
|
+
braceDepth++;
|
|
90424
|
+
i++;
|
|
90425
|
+
continue;
|
|
90426
|
+
}
|
|
90427
|
+
if (char === '}') {
|
|
90428
|
+
braceDepth--;
|
|
90429
|
+
i++;
|
|
90430
|
+
continue;
|
|
90431
|
+
}
|
|
90432
|
+
|
|
90433
|
+
// Only look for functions at depth 0
|
|
90434
|
+
if (braceDepth === 0 && char === 'f' && script.slice(i, i + 8) === 'function') {
|
|
90435
|
+
// Check if 'function' is a complete word
|
|
90436
|
+
const charBefore = i > 0 ? script[i - 1] : ' ';
|
|
90437
|
+
const charAfter = script[i + 8] ?? ' ';
|
|
90438
|
+
const isValidBefore = /\s|^|;|{|}/.test(charBefore);
|
|
90439
|
+
const isValidAfter = /\s/.test(charAfter);
|
|
90440
|
+
if (isValidBefore && isValidAfter) {
|
|
90441
|
+
i += 8;
|
|
90442
|
+
// Skip whitespace
|
|
90443
|
+
while (i < script.length && /\s/.test(script[i])) {
|
|
90444
|
+
i++;
|
|
90445
|
+
}
|
|
90446
|
+
// Extract function name
|
|
90447
|
+
const nameMatch = script.slice(i).match(/^([a-zA-Z_$][\w$]*)/);
|
|
90448
|
+
if (nameMatch) {
|
|
90449
|
+
names.push(nameMatch[1]);
|
|
90450
|
+
}
|
|
90451
|
+
continue;
|
|
90452
|
+
}
|
|
90453
|
+
}
|
|
90454
|
+
i++;
|
|
90375
90455
|
}
|
|
90376
90456
|
return names;
|
|
90377
90457
|
};
|
|
@@ -90574,6 +90654,96 @@ const handleInput = (state, hdId, value) => {
|
|
|
90574
90654
|
};
|
|
90575
90655
|
};
|
|
90576
90656
|
|
|
90657
|
+
const dispatchKeydownEvent = (element, window, key, code) => {
|
|
90658
|
+
const keydownEvent = new window.KeyboardEvent('keydown', {
|
|
90659
|
+
bubbles: true,
|
|
90660
|
+
code,
|
|
90661
|
+
key
|
|
90662
|
+
});
|
|
90663
|
+
dispatchEvent(element, keydownEvent);
|
|
90664
|
+
};
|
|
90665
|
+
|
|
90666
|
+
const handleKeydown = (state, hdId, key, code) => {
|
|
90667
|
+
const happyDomInstance = get(state.uid);
|
|
90668
|
+
if (!happyDomInstance) {
|
|
90669
|
+
return state;
|
|
90670
|
+
}
|
|
90671
|
+
const element = hdId ? happyDomInstance.elementMap.get(hdId) : happyDomInstance.document;
|
|
90672
|
+
if (!element) {
|
|
90673
|
+
return state;
|
|
90674
|
+
}
|
|
90675
|
+
|
|
90676
|
+
// Dispatch keydown event in happy-dom so event listeners fire
|
|
90677
|
+
dispatchKeydownEvent(element, happyDomInstance.window, key, code);
|
|
90678
|
+
|
|
90679
|
+
// Re-serialize the (potentially mutated) DOM
|
|
90680
|
+
const elementMap = new Map();
|
|
90681
|
+
const serialized = serialize(happyDomInstance.document, elementMap);
|
|
90682
|
+
|
|
90683
|
+
// Update happy-dom state with new element map
|
|
90684
|
+
set$1(state.uid, {
|
|
90685
|
+
document: happyDomInstance.document,
|
|
90686
|
+
elementMap,
|
|
90687
|
+
window: happyDomInstance.window
|
|
90688
|
+
});
|
|
90689
|
+
const parsedDom = serialized.dom;
|
|
90690
|
+
const {
|
|
90691
|
+
css
|
|
90692
|
+
} = serialized;
|
|
90693
|
+
const parsedNodesChildNodeCount = getParsedNodesChildNodeCount(parsedDom);
|
|
90694
|
+
return {
|
|
90695
|
+
...state,
|
|
90696
|
+
css,
|
|
90697
|
+
parsedDom,
|
|
90698
|
+
parsedNodesChildNodeCount
|
|
90699
|
+
};
|
|
90700
|
+
};
|
|
90701
|
+
|
|
90702
|
+
const dispatchKeyupEvent = (element, window, key, code) => {
|
|
90703
|
+
const keyupEvent = new window.KeyboardEvent('keyup', {
|
|
90704
|
+
bubbles: true,
|
|
90705
|
+
code,
|
|
90706
|
+
key
|
|
90707
|
+
});
|
|
90708
|
+
dispatchEvent(element, keyupEvent);
|
|
90709
|
+
};
|
|
90710
|
+
|
|
90711
|
+
const handleKeyup = (state, hdId, key, code) => {
|
|
90712
|
+
const happyDomInstance = get(state.uid);
|
|
90713
|
+
if (!happyDomInstance) {
|
|
90714
|
+
return state;
|
|
90715
|
+
}
|
|
90716
|
+
const element = hdId ? happyDomInstance.elementMap.get(hdId) : happyDomInstance.document;
|
|
90717
|
+
if (!element) {
|
|
90718
|
+
return state;
|
|
90719
|
+
}
|
|
90720
|
+
|
|
90721
|
+
// Dispatch keyup event in happy-dom so event listeners fire
|
|
90722
|
+
dispatchKeyupEvent(element, happyDomInstance.window, key, code);
|
|
90723
|
+
|
|
90724
|
+
// Re-serialize the (potentially mutated) DOM
|
|
90725
|
+
const elementMap = new Map();
|
|
90726
|
+
const serialized = serialize(happyDomInstance.document, elementMap);
|
|
90727
|
+
|
|
90728
|
+
// Update happy-dom state with new element map
|
|
90729
|
+
set$1(state.uid, {
|
|
90730
|
+
document: happyDomInstance.document,
|
|
90731
|
+
elementMap,
|
|
90732
|
+
window: happyDomInstance.window
|
|
90733
|
+
});
|
|
90734
|
+
const parsedDom = serialized.dom;
|
|
90735
|
+
const {
|
|
90736
|
+
css
|
|
90737
|
+
} = serialized;
|
|
90738
|
+
const parsedNodesChildNodeCount = getParsedNodesChildNodeCount(parsedDom);
|
|
90739
|
+
return {
|
|
90740
|
+
...state,
|
|
90741
|
+
css,
|
|
90742
|
+
parsedDom,
|
|
90743
|
+
parsedNodesChildNodeCount
|
|
90744
|
+
};
|
|
90745
|
+
};
|
|
90746
|
+
|
|
90577
90747
|
const loadContent = async state => {
|
|
90578
90748
|
// Try to register to receive editor change notifications from the editor worker.
|
|
90579
90749
|
// Use dynamic access and ignore errors so this is safe in environments where
|
|
@@ -90663,6 +90833,8 @@ const renderCss = (oldState, newState) => {
|
|
|
90663
90833
|
|
|
90664
90834
|
const HandleInput = 4;
|
|
90665
90835
|
const HandleClick = 11;
|
|
90836
|
+
const HandleKeydown = 12;
|
|
90837
|
+
const HandleKeyup = 13;
|
|
90666
90838
|
|
|
90667
90839
|
const getEmptyPreviewDom = () => {
|
|
90668
90840
|
return [{
|
|
@@ -90695,6 +90867,9 @@ const getPreviewDom = state => {
|
|
|
90695
90867
|
className: 'Viewlet Preview',
|
|
90696
90868
|
onClick: HandleClick,
|
|
90697
90869
|
onInput: HandleInput,
|
|
90870
|
+
onKeyDown: HandleKeydown,
|
|
90871
|
+
onKeyUp: HandleKeyup,
|
|
90872
|
+
tabIndex: 0,
|
|
90698
90873
|
type: Div$1
|
|
90699
90874
|
}, ...parsedDom];
|
|
90700
90875
|
}
|
|
@@ -90703,6 +90878,9 @@ const getPreviewDom = state => {
|
|
|
90703
90878
|
className: 'Viewlet Preview',
|
|
90704
90879
|
onClick: HandleClick,
|
|
90705
90880
|
onInput: HandleInput,
|
|
90881
|
+
onKeyDown: HandleKeydown,
|
|
90882
|
+
onKeyUp: HandleKeyup,
|
|
90883
|
+
tabIndex: 0,
|
|
90706
90884
|
type: Div$1
|
|
90707
90885
|
}, {
|
|
90708
90886
|
childCount: 1,
|
|
@@ -90776,6 +90954,14 @@ const renderEventListeners = () => {
|
|
|
90776
90954
|
capture: true,
|
|
90777
90955
|
name: HandleInput,
|
|
90778
90956
|
params: ['handleInput', 'event.target.dataset.id', 'event.target.value']
|
|
90957
|
+
}, {
|
|
90958
|
+
capture: true,
|
|
90959
|
+
name: HandleKeydown,
|
|
90960
|
+
params: ['handleKeyDown', 'event.target.dataset.id', 'event.key', 'event.code']
|
|
90961
|
+
}, {
|
|
90962
|
+
capture: true,
|
|
90963
|
+
name: HandleKeyup,
|
|
90964
|
+
params: ['handleKeyUp', 'event.target.dataset.id', 'event.key', 'event.code']
|
|
90779
90965
|
}];
|
|
90780
90966
|
};
|
|
90781
90967
|
|
|
@@ -90834,6 +91020,8 @@ const commandMap = {
|
|
|
90834
91020
|
'Preview.handleClick': wrapCommand(handleClick),
|
|
90835
91021
|
'Preview.handleFileEdited': wrapCommand(handleFileEdited),
|
|
90836
91022
|
'Preview.handleInput': wrapCommand(handleInput),
|
|
91023
|
+
'Preview.handleKeyDown': wrapCommand(handleKeydown),
|
|
91024
|
+
'Preview.handleKeyUp': wrapCommand(handleKeyup),
|
|
90837
91025
|
'Preview.loadContent': wrapCommand(loadContent),
|
|
90838
91026
|
'Preview.render2': render2,
|
|
90839
91027
|
'Preview.renderEventListeners': renderEventListeners,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lvce-editor/preview-worker",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.7.0",
|
|
4
4
|
"description": "Preview Worker",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
"type": "module",
|
|
12
12
|
"main": "dist/previewWorkerMain.js",
|
|
13
13
|
"dependencies": {
|
|
14
|
-
"@lvce-editor/virtual-dom-worker": "^
|
|
14
|
+
"@lvce-editor/virtual-dom-worker": "^8.2.0",
|
|
15
15
|
"happy-dom-without-node": "^14.12.3"
|
|
16
16
|
}
|
|
17
17
|
}
|