Skip to content

Commit 3711ce4

Browse files
committed
work on adopted nodes
1 parent 7580282 commit 3711ce4

File tree

2 files changed

+53
-20
lines changed

2 files changed

+53
-20
lines changed

packages/web-component-designer/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"description": "A WYSIWYG designer webcomponent for html components",
33
"name": "@node-projects/web-component-designer",
4-
"version": "0.1.264",
4+
"version": "0.1.265",
55
"type": "module",
66
"main": "./dist/index.js",
77
"author": "jochen.kuehner@gmx.de",

packages/web-component-designer/src/elements/helper/getBoxQuads.js

Lines changed: 52 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,39 @@ export function addPolyfill(windowObj = window) {
3434
}
3535
}
3636

37+
/**
38+
* @param {globalThis} windowObj?
39+
*/
40+
export function patchAdoptNode(windowObj = window) {
41+
if (!windowObj.Node.prototype.getBoxQuads) {
42+
//@ts-ignore
43+
windowObj.Node.prototype.getBoxQuads = function (options) {
44+
return getBoxQuads(this, options)
45+
}
46+
}
47+
48+
if (!windowObj.Node.prototype.convertQuadFromNode) {
49+
//@ts-ignore
50+
windowObj.Node.prototype.convertQuadFromNode = function (quad, from, options) {
51+
return convertQuadFromNode(this, quad, from, options)
52+
}
53+
}
54+
55+
if (!windowObj.Node.prototype.convertRectFromNode) {
56+
//@ts-ignore
57+
windowObj.Node.prototype.convertRectFromNode = function (rect, from, options) {
58+
return convertRectFromNode(this, rect, from, options)
59+
}
60+
}
61+
62+
if (!windowObj.Node.prototype.convertPointFromNode) {
63+
//@ts-ignore
64+
windowObj.Node.prototype.convertPointFromNode = function (point, from, options) {
65+
return convertPointFromNode(this, point, from, options)
66+
}
67+
}
68+
}
69+
3770
/**
3871
* @param {Node} node
3972
* @param {DOMQuadInit} quad
@@ -48,7 +81,7 @@ export function convertQuadFromNode(node, quad, from, options) {
4881
quad = new DOMQuad(transformPointBox(quad.p1, options.fromBox, (node.ownerDocument.defaultView ?? window).getComputedStyle(from), -1), transformPointBox(quad.p2, options.fromBox, (node.ownerDocument.defaultView ?? window).getComputedStyle(from), -1), transformPointBox(quad.p3, options.fromBox, (node.ownerDocument.defaultView ?? window).getComputedStyle(from), -1), transformPointBox(quad.p4, options.fromBox, (node.ownerDocument.defaultView ?? window).getComputedStyle(from), -1))
4982
}
5083
let res = new DOMQuad(m2.transformPoint(m1.transformPoint(quad.p1)), m2.transformPoint(m1.transformPoint(quad.p2)), m2.transformPoint(m1.transformPoint(quad.p3)), m2.transformPoint(m1.transformPoint(quad.p4)));
51-
if (options?.toBox && options?.toBox !== 'border' && node instanceof (node.ownerDocument.defaultView ?? window).Element) {
84+
if (options?.toBox && options?.toBox !== 'border' && (node instanceof Element || node instanceof (node.ownerDocument.defaultView ?? window).Element)) {
5285
res = new DOMQuad(transformPointBox(res.p1, options.toBox, (node.ownerDocument.defaultView ?? window).getComputedStyle(node), -1), transformPointBox(res.p2, options.toBox, (node.ownerDocument.defaultView ?? window).getComputedStyle(node), -1), transformPointBox(res.p3, options.toBox, (node.ownerDocument.defaultView ?? window).getComputedStyle(node), -1), transformPointBox(res.p4, options.toBox, (node.ownerDocument.defaultView ?? window).getComputedStyle(node), -1))
5386
}
5487
return res;
@@ -70,7 +103,7 @@ export function convertRectFromNode(node, rect, from, options) {
70103
rect = new DOMRect(p.x, p.y, rect.width, rect.height);
71104
}
72105
let res = new DOMQuad(m2.transformPoint(m1.transformPoint(new DOMPoint(rect.x, rect.y))), m2.transformPoint(m1.transformPoint(new DOMPoint(rect.x + rect.width, rect.y))), m2.transformPoint(m1.transformPoint(new DOMPoint(rect.x + rect.width, rect.y + rect.height))), m2.transformPoint(m1.transformPoint(new DOMPoint(rect.x, rect.y + rect.height))));
73-
if (options?.toBox && options?.toBox !== 'border' && node instanceof (node.ownerDocument.defaultView ?? window).Element) {
106+
if (options?.toBox && options?.toBox !== 'border' && (node instanceof Element || node instanceof (node.ownerDocument.defaultView ?? window).Element)) {
74107
res = new DOMQuad(transformPointBox(res.p1, options.toBox, (node.ownerDocument.defaultView ?? window).getComputedStyle(node), -1), transformPointBox(res.p2, options.toBox, (node.ownerDocument.defaultView ?? window).getComputedStyle(node), -1), transformPointBox(res.p3, options.toBox, (node.ownerDocument.defaultView ?? window).getComputedStyle(node), -1), transformPointBox(res.p4, options.toBox, (node.ownerDocument.defaultView ?? window).getComputedStyle(node), -1))
75108
}
76109
return res;
@@ -90,7 +123,7 @@ export function convertPointFromNode(node, point, from, options) {
90123
point = transformPointBox(point, options.fromBox, (node.ownerDocument.defaultView ?? window).getComputedStyle(from), 1);
91124
}
92125
let res = m2.transformPoint(m1.transformPoint(point));
93-
if (options?.toBox && options?.toBox !== 'border' && node instanceof (node.ownerDocument.defaultView ?? window).Element) {
126+
if (options?.toBox && options?.toBox !== 'border' && (node instanceof Element || node instanceof (node.ownerDocument.defaultView ?? window).Element)) {
94127
res = transformPointBox(res, options.toBox, (node.ownerDocument.defaultView ?? window).getComputedStyle(node), -1);
95128
}
96129
return res;
@@ -165,7 +198,7 @@ export function getBoxQuads(node, options) {
165198

166199
/** @type {{x: number, y:number}[] } */
167200
let o = null;
168-
if (node instanceof (node.ownerDocument.defaultView ?? window).Element) {
201+
if ((node instanceof Element || node instanceof (node.ownerDocument.defaultView ?? window).Element)) {
169202
if (options?.box === 'margin') {
170203
const cs = (node.ownerDocument.defaultView ?? window).getComputedStyle(node);
171204
o = [{ x: parseFloat(cs.marginLeft), y: parseFloat(cs.marginTop) }, { x: -parseFloat(cs.marginRight), y: parseFloat(cs.marginTop) }, { x: -parseFloat(cs.marginRight), y: -parseFloat(cs.marginBottom) }, { x: parseFloat(cs.marginLeft), y: -parseFloat(cs.marginBottom) }];
@@ -228,21 +261,21 @@ function as2DPoint(point) {
228261
export function getElementSize(node, matrix) {
229262
let width = 0;
230263
let height = 0;
231-
if (node instanceof (node.ownerDocument.defaultView ?? window).HTMLElement) {
264+
if ((node instanceof HTMLElement || node instanceof (node.ownerDocument.defaultView ?? window).HTMLElement)) {
232265
width = node.offsetWidth;
233266
height = node.offsetHeight;
234-
} else if (node instanceof (node.ownerDocument.defaultView ?? window).SVGSVGElement) {
267+
} else if ((node instanceof SVGSVGElement || node instanceof (node.ownerDocument.defaultView ?? window).SVGSVGElement)) {
235268
width = node.width.baseVal.value
236269
height = node.height.baseVal.value
237-
} else if (node instanceof (node.ownerDocument.defaultView ?? window).SVGGraphicsElement) {
270+
} else if ((node instanceof SVGGraphicsElement || node instanceof (node.ownerDocument.defaultView ?? window).SVGGraphicsElement)) {
238271
const bbox = node.getBBox()
239272
width = bbox.width;
240273
height = bbox.height;
241-
} else if (node instanceof (node.ownerDocument.defaultView ?? window).MathMLElement) {
274+
} else if ((node instanceof MathMLElement || node instanceof (node.ownerDocument.defaultView ?? window).MathMLElement)) {
242275
const bbox = node.getBoundingClientRect()
243276
width = bbox.width / (matrix?.a ?? 1);
244277
height = bbox.height / (matrix?.d ?? 1);
245-
} else if (node instanceof (node.ownerDocument.defaultView ?? window).Text) {
278+
} else if ((node instanceof Text || node instanceof (node.ownerDocument.defaultView ?? window).Text)) {
246279
const range = document.createRange();
247280
range.selectNodeContents(node);
248281
const targetRect = range.getBoundingClientRect();
@@ -258,9 +291,9 @@ export function getElementSize(node, matrix) {
258291
* @param {HTMLIFrameElement[]} iframes
259292
*/
260293
function getElementOffsetsInContainer(node, includeScroll, iframes) {
261-
if (node instanceof (node.ownerDocument.defaultView ?? window).HTMLElement) {
294+
if ((node instanceof HTMLElement || node instanceof (node.ownerDocument.defaultView ?? window).HTMLElement)) {
262295
return new DOMPoint(node.offsetLeft - (includeScroll ? node.scrollLeft : 0), node.offsetTop - (includeScroll ? node.scrollTop : 0));
263-
} else if (node instanceof (node.ownerDocument.defaultView ?? window).Text) {
296+
} else if ((node instanceof Text || node instanceof (node.ownerDocument.defaultView ?? window).Text)) {
264297
const range = document.createRange();
265298
range.selectNodeContents(node);
266299
const r1 = range.getBoundingClientRect();
@@ -271,8 +304,8 @@ function getElementOffsetsInContainer(node, includeScroll, iframes) {
271304
const zX = parent.offsetWidth / r2.width;
272305
const zY = parent.offsetHeight / r2.height;
273306
return new DOMPoint((r1.x - r2.x) * zX, (r1.y - r2.y) * zY);
274-
} else if (node instanceof (node.ownerDocument.defaultView ?? window).Element) {
275-
if (node instanceof (node.ownerDocument.defaultView ?? window).SVGGraphicsElement && !(node instanceof (node.ownerDocument.defaultView ?? window).SVGSVGElement)) {
307+
} else if ((node instanceof Element || node instanceof (node.ownerDocument.defaultView ?? window).Element)) {
308+
if ((node instanceof SVGGraphicsElement || node instanceof (node.ownerDocument.defaultView ?? window).SVGGraphicsElement) && !((node instanceof SVGSVGElement || node instanceof (node.ownerDocument.defaultView ?? window).SVGSVGElement))) {
276309
const bb = node.getBBox();
277310
return new DOMPoint(bb.x, bb.y);
278311
}
@@ -339,8 +372,8 @@ export function getResultingTransformationBetweenElementAndAllAncestors(node, an
339372
}
340373
else {
341374

342-
if (actualElement instanceof (actualElement.ownerDocument.defaultView ?? window).HTMLElement) {
343-
if (lastOffsetParent !== actualElement.offsetParent && !(actualElement instanceof (actualElement.ownerDocument.defaultView ?? window).HTMLSlotElement)) {
375+
if ((actualElement instanceof HTMLElement || actualElement instanceof (actualElement.ownerDocument.defaultView ?? window).HTMLElement)) {
376+
if (lastOffsetParent !== actualElement.offsetParent && !((actualElement instanceof HTMLSlotElement || actualElement instanceof (actualElement.ownerDocument.defaultView ?? window).HTMLSlotElement))) {
344377
const offsets = getElementOffsetsInContainer(actualElement, actualElement !== node, iframes);
345378
lastOffsetParent = actualElement.offsetParent;
346379
const mvMat = new DOMMatrix().translate(offsets.x, offsets.y);
@@ -389,14 +422,14 @@ export function getResultingTransformationBetweenElementAndAllAncestors(node, an
389422
* @returns {Element}
390423
*/
391424
function getParentElementIncludingSlots(node, iframes) {
392-
if (node instanceof (node.ownerDocument.defaultView ?? window).Element && node.assignedSlot)
425+
if ((node instanceof Element || node instanceof (node.ownerDocument.defaultView ?? window).Element) && node.assignedSlot)
393426
return node.assignedSlot;
394427
if (node.parentElement == null) {
395-
if (node.parentNode instanceof (node.ownerDocument.defaultView ?? window).ShadowRoot) {
428+
if ((node.parentNode instanceof ShadowRoot || node.parentNode instanceof (node.ownerDocument.defaultView ?? window).ShadowRoot)) {
396429
return node.parentNode.host;
397430
}
398431
}
399-
if (node instanceof (node.ownerDocument.defaultView ?? window).HTMLHtmlElement) {
432+
if ((node instanceof HTMLHtmlElement || node instanceof (node.ownerDocument.defaultView ?? window).HTMLHtmlElement)) {
400433
if (iframes) {
401434
for (const f of iframes)
402435
if (f?.contentDocument == node.ownerDocument)
@@ -411,7 +444,7 @@ function getParentElementIncludingSlots(node, iframes) {
411444
* @param {HTMLIFrameElement[]=} iframes
412445
*/
413446
export function getElementCombinedTransform(element, iframes) {
414-
if (element instanceof (element.ownerDocument.defaultView ?? window).Text)
447+
if ((element instanceof Text || element instanceof (element.ownerDocument.defaultView ?? window).Text))
415448
return new DOMMatrix;
416449

417450
//https://www.w3.org/TR/css-transforms-2/#ctm

0 commit comments

Comments
 (0)