@lvce-editor/preview-sandbox-worker 3.4.0 → 4.0.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.
@@ -1129,6 +1129,60 @@ const terminate = () => {
1129
1129
  globalThis.close();
1130
1130
  };
1131
1131
 
1132
+ const ENTRY_SIZE = 9;
1133
+ const OFFSET_EXISTS = 0;
1134
+ const OFFSET_X = 1;
1135
+ const OFFSET_Y = 2;
1136
+ const OFFSET_TOP = 3;
1137
+ const OFFSET_LEFT = 4;
1138
+ const OFFSET_RIGHT = 5;
1139
+ const OFFSET_BOTTOM = 6;
1140
+ const OFFSET_WIDTH = 7;
1141
+ const OFFSET_HEIGHT = 8;
1142
+ const states$2 = new Map();
1143
+ const getIndex = hdId => {
1144
+ const value = Number(hdId);
1145
+ if (!Number.isInteger(value) || value < 0) {
1146
+ return -1;
1147
+ }
1148
+ return value;
1149
+ };
1150
+ const getOffset = hdId => {
1151
+ const index = getIndex(hdId);
1152
+ if (index === -1) {
1153
+ return -1;
1154
+ }
1155
+ return index * ENTRY_SIZE;
1156
+ };
1157
+
1158
+ // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
1159
+ const setGeometryBuffer = (uid, geometryBuffer) => {
1160
+ states$2.set(uid, new Float64Array(geometryBuffer));
1161
+ };
1162
+ const getRect = (uid, hdId) => {
1163
+ const view = states$2.get(uid);
1164
+ if (!view) {
1165
+ return undefined;
1166
+ }
1167
+ const offset = getOffset(hdId);
1168
+ if (offset === -1 || offset + ENTRY_SIZE > view.length) {
1169
+ return undefined;
1170
+ }
1171
+ if (view[offset + OFFSET_EXISTS] !== 1) {
1172
+ return undefined;
1173
+ }
1174
+ return {
1175
+ bottom: view[offset + OFFSET_BOTTOM],
1176
+ height: view[offset + OFFSET_HEIGHT],
1177
+ left: view[offset + OFFSET_LEFT],
1178
+ right: view[offset + OFFSET_RIGHT],
1179
+ top: view[offset + OFFSET_TOP],
1180
+ width: view[offset + OFFSET_WIDTH],
1181
+ x: view[offset + OFFSET_X],
1182
+ y: view[offset + OFFSET_Y]
1183
+ };
1184
+ };
1185
+
1132
1186
  /* eslint-disable @typescript-eslint/prefer-readonly-parameter-types */
1133
1187
 
1134
1188
  const callBacks = Object.create(null);
@@ -1343,6 +1397,20 @@ const TAGS_TO_SKIP_TAG_ONLY = new Set(['html', 'body', 'head']);
1343
1397
 
1344
1398
  // Tags where we extract content as CSS
1345
1399
  const CSS_TAGS = new Set(['style']);
1400
+ // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
1401
+ const getOrCreateHdId = (node, context) => {
1402
+ const existingHdId = node.getAttribute?.('data-id');
1403
+ if (existingHdId) {
1404
+ const numericValue = Number(existingHdId);
1405
+ if (Number.isInteger(numericValue) && numericValue >= context.nextId) {
1406
+ context.nextId = numericValue + 1;
1407
+ }
1408
+ return existingHdId;
1409
+ }
1410
+ const hdId = String(context.nextId++);
1411
+ node.setAttribute?.('data-id', hdId);
1412
+ return hdId;
1413
+ };
1346
1414
  const serializeAttributeName = attrName => {
1347
1415
  if (attrName === 'class') {
1348
1416
  return 'className';
@@ -1440,7 +1508,7 @@ const serializeNode = (node, dom, css, context) => {
1440
1508
  applyAllowedAttributes(newNode, node);
1441
1509
 
1442
1510
  // Assign element tracking ID for interactivity
1443
- const hdId = String(context.nextId++);
1511
+ const hdId = getOrCreateHdId(node, context);
1444
1512
  newNode['data-id'] = hdId;
1445
1513
  context.elementMap[hdId] = node
1446
1514
 
@@ -89829,12 +89897,16 @@ const createLocalStorage = () => {
89829
89897
  return localStorage;
89830
89898
  };
89831
89899
 
89832
- const getGlobals = (width, height, devicePixelRatio = 1) => {
89900
+ /* eslint-disable @typescript-eslint/prefer-readonly-parameter-types */
89901
+
89902
+ const getGlobals = (window, width, height, devicePixelRatio = 1) => {
89833
89903
  const localStorage = createLocalStorage();
89904
+ const getComputedStyle = window.getComputedStyle.bind(window);
89834
89905
  return {
89835
89906
  globalGlobals: {
89836
89907
  alert: alert,
89837
89908
  devicePixelRatio,
89909
+ getComputedStyle,
89838
89910
  innerHeight: height,
89839
89911
  innerWidth: width,
89840
89912
  localStorage
@@ -89842,6 +89914,7 @@ const getGlobals = (width, height, devicePixelRatio = 1) => {
89842
89914
  windowGlobals: {
89843
89915
  alert: alert,
89844
89916
  devicePixelRatio,
89917
+ getComputedStyle,
89845
89918
  innerHeight: height,
89846
89919
  innerWidth: width
89847
89920
  }
@@ -89938,6 +90011,10 @@ const getTopLevelFunctionNames = script => {
89938
90011
  /* eslint-disable @typescript-eslint/prefer-readonly-parameter-types */
89939
90012
 
89940
90013
  const setGlobals = (window, globalGlobals, windowGlobals) => {
90014
+ const globalScope = globalThis;
90015
+ globalScope.addEventListener = window.addEventListener.bind(window);
90016
+ globalScope.removeEventListener = window.removeEventListener.bind(window);
90017
+ globalScope.dispatchEvent = window.dispatchEvent.bind(window);
89941
90018
  for (const [key, value] of Object.entries(globalGlobals)) {
89942
90019
  // @ts-ignore
89943
90020
  globalThis[key] = value;
@@ -89956,7 +90033,7 @@ const executeScripts = (window, document, scripts, width = 0, height = 0, device
89956
90033
  const {
89957
90034
  globalGlobals,
89958
90035
  windowGlobals
89959
- } = getGlobals(width, height, devicePixelRatio);
90036
+ } = getGlobals(window, width, height, devicePixelRatio);
89960
90037
  setGlobals(window, globalGlobals, windowGlobals);
89961
90038
  let firstError = null;
89962
90039
  let firstCodeFrame = '';
@@ -90011,6 +90088,9 @@ const getParsedNodesChildNodeCount = parsedDom => {
90011
90088
  /* eslint-disable @typescript-eslint/no-floating-promises */
90012
90089
 
90013
90090
  const observers = new Map();
90091
+ const isInternalMutation = records => {
90092
+ return records.length > 0 && records.every(record => record.type === 'attributes' && record.attributeName === 'data-id');
90093
+ };
90014
90094
  const handleMutations = async uid => {
90015
90095
  const happyDomInstance = get(uid);
90016
90096
  if (!happyDomInstance) {
@@ -90036,7 +90116,10 @@ const observe = (uid, document, window) => {
90036
90116
  if (existingObserver) {
90037
90117
  existingObserver.disconnect();
90038
90118
  }
90039
- const observer = new window.MutationObserver(() => {
90119
+ const observer = new window.MutationObserver(records => {
90120
+ if (isInternalMutation(records)) {
90121
+ return;
90122
+ }
90040
90123
  handleMutations(uid);
90041
90124
  });
90042
90125
  observer.observe(document.documentElement, {
@@ -90068,6 +90151,12 @@ const toNumber = value => {
90068
90151
 
90069
90152
  /* eslint-disable @typescript-eslint/prefer-readonly-parameter-types */
90070
90153
 
90154
+ const toCanvasRect = rect => {
90155
+ return {
90156
+ ...rect,
90157
+ toJSON: () => rect
90158
+ };
90159
+ };
90071
90160
  const reflectCanvasDimensionAttribute = (element, name, value) => {
90072
90161
  element.setAttribute(name, String(value));
90073
90162
  };
@@ -90160,28 +90249,20 @@ const patchCanvasElements = async (document, uid) => {
90160
90249
 
90161
90250
  // @ts-ignore
90162
90251
  element.getBoundingClientRect = () => {
90163
- return {
90252
+ const rect = getRect(uid, dataId);
90253
+ if (rect) {
90254
+ return toCanvasRect(rect);
90255
+ }
90256
+ return toCanvasRect({
90164
90257
  bottom: heightValue,
90165
90258
  height: heightValue,
90166
90259
  left: 0,
90167
90260
  right: widthValue,
90168
- toJSON: () => {
90169
- return {
90170
- bottom: heightValue,
90171
- height: heightValue,
90172
- left: 0,
90173
- right: widthValue,
90174
- top: 0,
90175
- width: widthValue,
90176
- x: 0,
90177
- y: 0
90178
- };
90179
- },
90180
90261
  top: 0,
90181
90262
  width: widthValue,
90182
90263
  x: 0,
90183
90264
  y: 0
90184
- };
90265
+ });
90185
90266
  };
90186
90267
  instances.push({
90187
90268
  dataId,
@@ -90196,6 +90277,85 @@ const patchCanvasElements = async (document, uid) => {
90196
90277
  });
90197
90278
  };
90198
90279
 
90280
+ const toRect = rect => {
90281
+ return {
90282
+ ...rect,
90283
+ toJSON: () => rect
90284
+ };
90285
+ };
90286
+ const getCanvasDimension = (element, name) => {
90287
+ const value = Number(element[name]);
90288
+ if (Number.isFinite(value)) {
90289
+ return value;
90290
+ }
90291
+ const attributeValue = Number(element.getAttribute?.(name) || 0);
90292
+ if (Number.isFinite(attributeValue)) {
90293
+ return attributeValue;
90294
+ }
90295
+ return 0;
90296
+ };
90297
+ const getDirectCanvasChildRect = element => {
90298
+ const {
90299
+ children
90300
+ } = element;
90301
+ if (!children) {
90302
+ return undefined;
90303
+ }
90304
+ for (let i = 0; i < children.length; i++) {
90305
+ const child = children[i];
90306
+ const tagName = (child?.tagName || '').toLowerCase();
90307
+ if (tagName === 'canvas' && typeof child.getBoundingClientRect === 'function') {
90308
+ return toRect(child.getBoundingClientRect());
90309
+ }
90310
+ }
90311
+ return undefined;
90312
+ };
90313
+ const getFallbackRect = element => {
90314
+ const tagName = (element.tagName || '').toLowerCase();
90315
+ if (tagName === 'canvas') {
90316
+ const width = getCanvasDimension(element, 'width');
90317
+ const height = getCanvasDimension(element, 'height');
90318
+ return toRect({
90319
+ bottom: height,
90320
+ height,
90321
+ left: 0,
90322
+ right: width,
90323
+ top: 0,
90324
+ width,
90325
+ x: 0,
90326
+ y: 0
90327
+ });
90328
+ }
90329
+ const directCanvasChildRect = getDirectCanvasChildRect(element);
90330
+ if (directCanvasChildRect) {
90331
+ return directCanvasChildRect;
90332
+ }
90333
+ return toRect({
90334
+ bottom: 0,
90335
+ height: 0,
90336
+ left: 0,
90337
+ right: 0,
90338
+ top: 0,
90339
+ width: 0,
90340
+ x: 0,
90341
+ y: 0
90342
+ });
90343
+ };
90344
+ const patchElement = (uid, hdId, element) => {
90345
+ element.getBoundingClientRect = () => {
90346
+ const rect = getRect(uid, hdId);
90347
+ if (rect) {
90348
+ return toRect(rect);
90349
+ }
90350
+ return getFallbackRect(element);
90351
+ };
90352
+ };
90353
+ const patchElementGeometry = (uid, elementMap) => {
90354
+ for (const [hdId, element] of Object.entries(elementMap)) {
90355
+ patchElement(uid, hdId, element);
90356
+ }
90357
+ };
90358
+
90199
90359
  const updateContent = async (uid, width, height, content, scripts) => {
90200
90360
  try {
90201
90361
  const {
@@ -90203,12 +90363,16 @@ const updateContent = async (uid, width, height, content, scripts) => {
90203
90363
  window: happyDomWindow
90204
90364
  } = createWindow(content);
90205
90365
  await patchCanvasElements(happyDomDocument, uid);
90366
+ const initialElementMap = Object.create(null);
90367
+ serialize(happyDomDocument, initialElementMap);
90368
+ patchElementGeometry(uid, initialElementMap);
90206
90369
  const {
90207
90370
  codeFrame,
90208
90371
  error
90209
90372
  } = executeScripts(happyDomWindow, happyDomDocument, scripts, width, height);
90210
90373
  const elementMap = Object.create(null);
90211
90374
  serialize(happyDomDocument, elementMap);
90375
+ patchElementGeometry(uid, elementMap);
90212
90376
  set$1(uid, {
90213
90377
  document: happyDomDocument,
90214
90378
  elementMap,
@@ -90279,6 +90443,7 @@ const commandMap = {
90279
90443
  'SandBox.handlePointerup': handlePointerup,
90280
90444
  'SandBox.loadContent': loadContent,
90281
90445
  'SandBox.resize': resize,
90446
+ 'SandBox.setGeometryBuffer': setGeometryBuffer,
90282
90447
  'SandBox.terminate': terminate
90283
90448
  };
90284
90449
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/preview-sandbox-worker",
3
- "version": "3.4.0",
3
+ "version": "4.0.0",
4
4
  "description": "Preview SandBox Worker",
5
5
  "repository": {
6
6
  "type": "git",