@pairdraw/scene-graph 0.19.2 → 0.20.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/components/index.d.ts +2 -2
- package/dist/components/index.js +2 -2
- package/dist/components/variants.d.ts +12 -1
- package/dist/components/variants.d.ts.map +1 -1
- package/dist/components/variants.js +30 -1
- package/dist/components/variants.js.map +1 -1
- package/dist/constants.d.ts +25 -1
- package/dist/constants.d.ts.map +1 -1
- package/dist/constants.js +94 -1
- package/dist/constants.js.map +1 -1
- package/dist/copy.d.ts.map +1 -1
- package/dist/copy.js +17 -1
- package/dist/copy.js.map +1 -1
- package/dist/css/index.d.ts +2 -2
- package/dist/css/index.js +2 -2
- package/dist/css/text.d.ts +17 -1
- package/dist/css/text.d.ts.map +1 -1
- package/dist/css/text.js +62 -1
- package/dist/css/text.js.map +1 -1
- package/dist/design-memory/index.d.ts +3 -0
- package/dist/design-memory/index.js +3 -0
- package/dist/design-memory/model.d.ts +33 -0
- package/dist/design-memory/model.d.ts.map +1 -0
- package/dist/design-memory/model.js +142 -0
- package/dist/design-memory/model.js.map +1 -0
- package/dist/design-memory/schema.d.ts +76 -0
- package/dist/design-memory/schema.d.ts.map +1 -0
- package/dist/design-memory/schema.js +72 -0
- package/dist/design-memory/schema.js.map +1 -0
- package/dist/effects.d.ts +7 -4
- package/dist/effects.d.ts.map +1 -1
- package/dist/effects.js +9 -4
- package/dist/effects.js.map +1 -1
- package/dist/empty-values.js +93 -0
- package/dist/empty-values.js.map +1 -0
- package/dist/fields/components.d.ts +9 -0
- package/dist/fields/components.d.ts.map +1 -0
- package/dist/fields/components.js +24 -0
- package/dist/fields/components.js.map +1 -0
- package/dist/fields/index.d.ts +3 -2
- package/dist/fields/index.js +3 -2
- package/dist/fields/layout.d.ts +3 -1
- package/dist/fields/layout.d.ts.map +1 -1
- package/dist/fields/layout.js +10 -1
- package/dist/fields/layout.js.map +1 -1
- package/dist/graph.d.ts +8 -1
- package/dist/graph.d.ts.map +1 -1
- package/dist/graph.js +28 -5
- package/dist/graph.js.map +1 -1
- package/dist/grid-values.d.ts +10 -0
- package/dist/grid-values.d.ts.map +1 -0
- package/dist/grid-values.js +53 -0
- package/dist/grid-values.js.map +1 -0
- package/dist/hit-test/drop.d.ts +20 -0
- package/dist/hit-test/drop.d.ts.map +1 -0
- package/dist/hit-test/drop.js +54 -0
- package/dist/hit-test/drop.js.map +1 -0
- package/dist/hit-test/geometry.js +86 -0
- package/dist/hit-test/geometry.js.map +1 -0
- package/dist/hit-test/masks.d.ts +14 -0
- package/dist/hit-test/masks.d.ts.map +1 -0
- package/dist/hit-test/masks.js +41 -0
- package/dist/hit-test/masks.js.map +1 -0
- package/dist/hit-test/paint.d.ts +6 -0
- package/dist/hit-test/paint.d.ts.map +1 -0
- package/dist/hit-test/paint.js +19 -0
- package/dist/hit-test/paint.js.map +1 -0
- package/dist/hit-test.d.ts +4 -15
- package/dist/hit-test.d.ts.map +1 -1
- package/dist/hit-test.js +5 -173
- package/dist/hit-test.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/instances/fields.d.ts.map +1 -1
- package/dist/instances/fields.js +2 -0
- package/dist/instances/fields.js.map +1 -1
- package/dist/instances/index.d.ts +2 -1
- package/dist/instances/index.d.ts.map +1 -1
- package/dist/instances/index.js +1 -1
- package/dist/instances/index.js.map +1 -1
- package/dist/instances/source-walk.d.ts +12 -0
- package/dist/instances/source-walk.d.ts.map +1 -0
- package/dist/node-defaults.d.ts.map +1 -1
- package/dist/node-defaults.js +41 -36
- package/dist/node-defaults.js.map +1 -1
- package/dist/plugin-data/fields.d.ts +75 -0
- package/dist/plugin-data/fields.d.ts.map +1 -1
- package/dist/plugin-data/fields.js +18 -1
- package/dist/plugin-data/fields.js.map +1 -1
- package/dist/preview.d.ts.map +1 -1
- package/dist/preview.js +2 -0
- package/dist/preview.js.map +1 -1
- package/dist/scaling/node.d.ts.map +1 -1
- package/dist/scaling/node.js +5 -1
- package/dist/scaling/node.js.map +1 -1
- package/dist/token-sources/document.d.ts +26 -0
- package/dist/token-sources/document.d.ts.map +1 -0
- package/dist/token-sources/document.js +80 -0
- package/dist/token-sources/document.js.map +1 -0
- package/dist/token-sources/index.d.ts +3 -0
- package/dist/token-sources/index.js +3 -0
- package/dist/token-sources/schema.d.ts +43 -0
- package/dist/token-sources/schema.d.ts.map +1 -0
- package/dist/token-sources/schema.js +50 -0
- package/dist/token-sources/schema.js.map +1 -0
- package/dist/types.d.ts +102 -14
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/dist/variations/copy.d.ts +16 -0
- package/dist/variations/copy.d.ts.map +1 -0
- package/dist/variations/copy.js +78 -0
- package/dist/variations/copy.js.map +1 -0
- package/dist/variations/index.d.ts +6 -0
- package/dist/variations/index.js +6 -0
- package/dist/variations/match.d.ts +23 -0
- package/dist/variations/match.d.ts.map +1 -0
- package/dist/variations/match.js +80 -0
- package/dist/variations/match.js.map +1 -0
- package/dist/variations/model.d.ts +39 -0
- package/dist/variations/model.d.ts.map +1 -0
- package/dist/variations/model.js +90 -0
- package/dist/variations/model.js.map +1 -0
- package/dist/variations/pick.d.ts +43 -0
- package/dist/variations/pick.d.ts.map +1 -0
- package/dist/variations/pick.js +211 -0
- package/dist/variations/pick.js.map +1 -0
- package/dist/variations/schema.d.ts +27 -0
- package/dist/variations/schema.d.ts.map +1 -0
- package/dist/variations/schema.js +27 -0
- package/dist/variations/schema.js.map +1 -0
- package/package.json +21 -1
- package/src/components/variants.ts +39 -0
- package/src/constants.ts +75 -0
- package/src/copy.ts +20 -1
- package/src/css/index.ts +7 -1
- package/src/css/text.ts +62 -0
- package/src/design-memory/index.ts +2 -0
- package/src/design-memory/model.ts +143 -0
- package/src/design-memory/schema.ts +84 -0
- package/src/effects.ts +16 -7
- package/src/empty-values.ts +101 -0
- package/src/fields/components.ts +21 -0
- package/src/fields/index.ts +1 -0
- package/src/fields/layout.ts +10 -0
- package/src/graph.ts +37 -5
- package/src/grid-values.ts +47 -0
- package/src/hit-test/drop.ts +90 -0
- package/src/hit-test/geometry.ts +131 -0
- package/src/hit-test/masks.ts +57 -0
- package/src/hit-test/paint.ts +24 -0
- package/src/hit-test.ts +8 -277
- package/src/instances/fields.ts +2 -0
- package/src/instances/index.ts +1 -0
- package/src/node-defaults.ts +49 -39
- package/src/plugin-data/fields.ts +18 -1
- package/src/preview.ts +2 -0
- package/src/scaling/node.ts +7 -2
- package/src/token-sources/document.ts +104 -0
- package/src/token-sources/index.ts +2 -0
- package/src/token-sources/schema.ts +59 -0
- package/src/types.ts +115 -13
- package/src/variations/copy.ts +89 -0
- package/src/variations/index.ts +5 -0
- package/src/variations/match.ts +100 -0
- package/src/variations/model.ts +132 -0
- package/src/variations/pick.ts +293 -0
- package/src/variations/schema.ts +29 -0
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { containsPoint } from "./geometry.js";
|
|
2
|
+
//#region src/hit-test/drop.ts
|
|
3
|
+
const LAYER_TARGETS = /* @__PURE__ */ new Set([
|
|
4
|
+
"FRAME",
|
|
5
|
+
"SECTION",
|
|
6
|
+
"COMPONENT",
|
|
7
|
+
"INSTANCE"
|
|
8
|
+
]);
|
|
9
|
+
const COMPONENT_TARGETS = /* @__PURE__ */ new Set([
|
|
10
|
+
"FRAME",
|
|
11
|
+
"SECTION",
|
|
12
|
+
"INSTANCE"
|
|
13
|
+
]);
|
|
14
|
+
function dropRules(options) {
|
|
15
|
+
const { componentSetIds } = options;
|
|
16
|
+
if (!componentSetIds) return {
|
|
17
|
+
takes: (node) => LAYER_TARGETS.has(node.type),
|
|
18
|
+
passThrough: /* @__PURE__ */ new Set(["GROUP", "COMPONENT_SET"])
|
|
19
|
+
};
|
|
20
|
+
return {
|
|
21
|
+
takes: (node) => COMPONENT_TARGETS.has(node.type) || node.type === "COMPONENT_SET" && componentSetIds.has(node.id),
|
|
22
|
+
passThrough: /* @__PURE__ */ new Set(["GROUP"])
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
function dropTargetIn(graph, px, py, parent, excludeIds, rules, transformCache) {
|
|
26
|
+
if (parent.type !== "CANVAS" && parent.clipsContent && !containsPoint(px, py, parent, graph, transformCache)) return null;
|
|
27
|
+
for (let i = parent.childIds.length - 1; i >= 0; i--) {
|
|
28
|
+
const childId = parent.childIds[i];
|
|
29
|
+
if (excludeIds.has(childId)) continue;
|
|
30
|
+
const child = graph.nodes.get(childId);
|
|
31
|
+
if (!child || child.internalOnly || !child.visible || child.locked) continue;
|
|
32
|
+
const target = rules.takes(child);
|
|
33
|
+
if (!target && !rules.passThrough.has(child.type)) continue;
|
|
34
|
+
const deeper = dropTargetIn(graph, px, py, child, excludeIds, rules, transformCache);
|
|
35
|
+
if (deeper) return deeper;
|
|
36
|
+
if (target && containsPoint(px, py, child, graph, transformCache)) return child;
|
|
37
|
+
}
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* The topmost unlocked frame, section, component, or instance under the point, in its rotated
|
|
42
|
+
* shape and inside its clipping ancestors. Groups and component sets are looked through but never
|
|
43
|
+
* returned, except that a set takes dropped components; locked layers and their contents are
|
|
44
|
+
* skipped.
|
|
45
|
+
*/
|
|
46
|
+
function hitTestDropTarget(graph, px, py, excludeIds, scopeId, options = {}) {
|
|
47
|
+
const scope = graph.nodes.get(scopeId ?? graph.rootId);
|
|
48
|
+
if (!scope) return null;
|
|
49
|
+
return dropTargetIn(graph, px, py, scope, excludeIds, dropRules(options), /* @__PURE__ */ new Map());
|
|
50
|
+
}
|
|
51
|
+
//#endregion
|
|
52
|
+
export { hitTestDropTarget };
|
|
53
|
+
|
|
54
|
+
//# sourceMappingURL=drop.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"drop.js","names":[],"sources":["../../src/hit-test/drop.ts"],"sourcesContent":["// Where dragged layers land: the frame, section, component, or instance under the point.\nimport type { SceneGraph } from '../graph'\nimport type { NodeType, SceneNode } from '../types'\nimport { containsPoint } from './geometry'\n\n/** Which layers take a drop, and which are only looked through, as in Figma. */\ninterface DropRules {\n takes: (node: SceneNode) => boolean\n passThrough: ReadonlySet<NodeType>\n}\n\nconst LAYER_TARGETS = new Set<NodeType>(['FRAME', 'SECTION', 'COMPONENT', 'INSTANCE'])\nconst COMPONENT_TARGETS = new Set<NodeType>(['FRAME', 'SECTION', 'INSTANCE'])\n\nfunction dropRules(options: DropTargetOptions): DropRules {\n const { componentSetIds } = options\n if (!componentSetIds) {\n return {\n takes: (node) => LAYER_TARGETS.has(node.type),\n passThrough: new Set(['GROUP', 'COMPONENT_SET'])\n }\n }\n // Components never go into other components, and a set only takes back its own variants.\n return {\n takes: (node) =>\n COMPONENT_TARGETS.has(node.type) ||\n (node.type === 'COMPONENT_SET' && componentSetIds.has(node.id)),\n passThrough: new Set(['GROUP'])\n }\n}\n\nfunction dropTargetIn(\n graph: SceneGraph,\n px: number,\n py: number,\n parent: SceneNode,\n excludeIds: ReadonlySet<string>,\n rules: DropRules,\n transformCache: Map<string, boolean>\n): SceneNode | null {\n // A clipped-away part of a child is not under the cursor.\n if (\n parent.type !== 'CANVAS' &&\n parent.clipsContent &&\n !containsPoint(px, py, parent, graph, transformCache)\n )\n return null\n\n for (let i = parent.childIds.length - 1; i >= 0; i--) {\n const childId = parent.childIds[i]\n if (excludeIds.has(childId)) continue\n const child = graph.nodes.get(childId)\n if (!child || child.internalOnly || !child.visible || child.locked) continue\n const target = rules.takes(child)\n if (!target && !rules.passThrough.has(child.type)) continue\n\n const deeper = dropTargetIn(graph, px, py, child, excludeIds, rules, transformCache)\n if (deeper) return deeper\n if (target && containsPoint(px, py, child, graph, transformCache)) return child\n }\n\n return null\n}\n\nexport interface DropTargetOptions {\n /**\n * Set when the dropped layers are all main components: the component sets they come from.\n * Components then go only into frames, sections, instances, and those sets.\n */\n componentSetIds?: ReadonlySet<string>\n}\n\n/**\n * The topmost unlocked frame, section, component, or instance under the point, in its rotated\n * shape and inside its clipping ancestors. Groups and component sets are looked through but never\n * returned, except that a set takes dropped components; locked layers and their contents are\n * skipped.\n */\nexport function hitTestDropTarget(\n graph: SceneGraph,\n px: number,\n py: number,\n excludeIds: ReadonlySet<string>,\n scopeId?: string,\n options: DropTargetOptions = {}\n): SceneNode | null {\n const scope = graph.nodes.get(scopeId ?? graph.rootId)\n if (!scope) return null\n return dropTargetIn(graph, px, py, scope, excludeIds, dropRules(options), new Map())\n}\n"],"mappings":";;AAWA,MAAM,gCAAgB,IAAI,IAAc;CAAC;CAAS;CAAW;CAAa;AAAU,CAAC;AACrF,MAAM,oCAAoB,IAAI,IAAc;CAAC;CAAS;CAAW;AAAU,CAAC;AAE5E,SAAS,UAAU,SAAuC;CACxD,MAAM,EAAE,oBAAoB;CAC5B,IAAI,CAAC,iBACH,OAAO;EACL,QAAQ,SAAS,cAAc,IAAI,KAAK,IAAI;EAC5C,6BAAa,IAAI,IAAI,CAAC,SAAS,eAAe,CAAC;CACjD;CAGF,OAAO;EACL,QAAQ,SACN,kBAAkB,IAAI,KAAK,IAAI,KAC9B,KAAK,SAAS,mBAAmB,gBAAgB,IAAI,KAAK,EAAE;EAC/D,6BAAa,IAAI,IAAI,CAAC,OAAO,CAAC;CAChC;AACF;AAEA,SAAS,aACP,OACA,IACA,IACA,QACA,YACA,OACA,gBACkB;CAElB,IACE,OAAO,SAAS,YAChB,OAAO,gBACP,CAAC,cAAc,IAAI,IAAI,QAAQ,OAAO,cAAc,GAEpD,OAAO;CAET,KAAK,IAAI,IAAI,OAAO,SAAS,SAAS,GAAG,KAAK,GAAG,KAAK;EACpD,MAAM,UAAU,OAAO,SAAS;EAChC,IAAI,WAAW,IAAI,OAAO,GAAG;EAC7B,MAAM,QAAQ,MAAM,MAAM,IAAI,OAAO;EACrC,IAAI,CAAC,SAAS,MAAM,gBAAgB,CAAC,MAAM,WAAW,MAAM,QAAQ;EACpE,MAAM,SAAS,MAAM,MAAM,KAAK;EAChC,IAAI,CAAC,UAAU,CAAC,MAAM,YAAY,IAAI,MAAM,IAAI,GAAG;EAEnD,MAAM,SAAS,aAAa,OAAO,IAAI,IAAI,OAAO,YAAY,OAAO,cAAc;EACnF,IAAI,QAAQ,OAAO;EACnB,IAAI,UAAU,cAAc,IAAI,IAAI,OAAO,OAAO,cAAc,GAAG,OAAO;CAC5E;CAEA,OAAO;AACT;;;;;;;AAgBA,SAAgB,kBACd,OACA,IACA,IACA,YACA,SACA,UAA6B,CAAC,GACZ;CAClB,MAAM,QAAQ,MAAM,MAAM,IAAI,WAAW,MAAM,MAAM;CACrD,IAAI,CAAC,OAAO,OAAO;CACnB,OAAO,aAAa,OAAO,IAAI,IAAI,OAAO,YAAY,UAAU,OAAO,mBAAG,IAAI,IAAI,CAAC;AACrF"}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import Matrix from "../matrix.js";
|
|
2
|
+
import { getWorldMatrix } from "../coordinate.js";
|
|
3
|
+
import { geometryBlobPolylines } from "../geometry.js";
|
|
4
|
+
//#region src/hit-test/geometry.ts
|
|
5
|
+
/**
|
|
6
|
+
* How far from a zero-width or zero-height layer, in canvas units, a point still lands on it, so
|
|
7
|
+
* a flat line or an empty box stays clickable; matches a connector line's slop (`connectors/hit.ts`).
|
|
8
|
+
*/
|
|
9
|
+
const DEGENERATE_AXIS_SLOP = 4;
|
|
10
|
+
function hasTransformedAncestor(node, graph, cache) {
|
|
11
|
+
const cached = cache.get(node.id);
|
|
12
|
+
if (cached !== void 0) return cached;
|
|
13
|
+
const parent = node.parentId ? graph.getNode(node.parentId) : void 0;
|
|
14
|
+
const transformed = node.rotation !== 0 || node.flipX || node.flipY || (parent ? hasTransformedAncestor(parent, graph, cache) : false);
|
|
15
|
+
cache.set(node.id, transformed);
|
|
16
|
+
return transformed;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Where the point lies in the node's own coordinates: its offset from the position when nothing
|
|
20
|
+
* above it is turned, else through the inverse world matrix. Null when that cannot be inverted.
|
|
21
|
+
*/
|
|
22
|
+
function localPoint(px, py, node, graph, transformCache) {
|
|
23
|
+
if (!hasTransformedAncestor(node, graph, transformCache)) {
|
|
24
|
+
const absolute = graph.getAbsolutePosition(node.id);
|
|
25
|
+
return {
|
|
26
|
+
x: px - absolute.x,
|
|
27
|
+
y: py - absolute.y
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
const inv = Matrix.invert(getWorldMatrix(node, graph));
|
|
31
|
+
if (!inv) return null;
|
|
32
|
+
const [x, y] = Matrix.mapPoints(inv, [px, py]);
|
|
33
|
+
return {
|
|
34
|
+
x,
|
|
35
|
+
y
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
/** Whether the point lies in the node's box, grown by `pad` canvas units on every side. */
|
|
39
|
+
function containsPoint(px, py, node, graph, transformCache, pad = 0) {
|
|
40
|
+
const local = localPoint(px, py, node, graph, transformCache);
|
|
41
|
+
if (!local) return false;
|
|
42
|
+
const padX = node.width > 0 ? pad : Math.max(pad, DEGENERATE_AXIS_SLOP);
|
|
43
|
+
const padY = node.height > 0 ? pad : Math.max(pad, DEGENERATE_AXIS_SLOP);
|
|
44
|
+
return local.x >= -padX && local.x <= node.width + padX && local.y >= -padY && local.y <= node.height + padY;
|
|
45
|
+
}
|
|
46
|
+
/** Whether a rounded rectangle of `width` × `height` holds the point. */
|
|
47
|
+
function roundedRectContains(point, width, height, radius) {
|
|
48
|
+
if (point.x < 0 || point.y < 0 || point.x > width || point.y > height) return false;
|
|
49
|
+
const r = Math.max(0, Math.min(radius, width / 2, height / 2));
|
|
50
|
+
const cx = Math.min(Math.max(point.x, r), width - r);
|
|
51
|
+
const cy = Math.min(Math.max(point.y, r), height - r);
|
|
52
|
+
return Math.hypot(point.x - cx, point.y - cy) <= r;
|
|
53
|
+
}
|
|
54
|
+
/** Whether the point is inside a closed polyline, by the even-odd rule. */
|
|
55
|
+
function insidePolyline(polyline, x, y) {
|
|
56
|
+
let inside = false;
|
|
57
|
+
for (let i = 0, j = polyline.length - 1; i < polyline.length; j = i++) {
|
|
58
|
+
const a = polyline[i];
|
|
59
|
+
const b = polyline[j];
|
|
60
|
+
if (a.y > y === b.y > y) continue;
|
|
61
|
+
if (x < (b.x - a.x) * (y - a.y) / (b.y - a.y) + a.x) inside = !inside;
|
|
62
|
+
}
|
|
63
|
+
return inside;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Whether a mask lets the point through, by the outline it draws: an ellipse's curve, a rounded
|
|
67
|
+
* rectangle's corners, or a vector's filled path. Other shapes clip by their box.
|
|
68
|
+
*/
|
|
69
|
+
function maskContainsPoint(px, py, mask, graph, transformCache) {
|
|
70
|
+
if (!(mask.type === "ELLIPSE" && mask.width > 0 && mask.height > 0 || mask.type === "RECTANGLE" && mask.cornerRadius > 0 || mask.type === "VECTOR" && mask.fillGeometry.length > 0)) return containsPoint(px, py, mask, graph, transformCache);
|
|
71
|
+
const local = localPoint(px, py, mask, graph, transformCache);
|
|
72
|
+
if (!local) return false;
|
|
73
|
+
if (mask.type === "ELLIPSE") {
|
|
74
|
+
const rx = mask.width / 2;
|
|
75
|
+
const ry = mask.height / 2;
|
|
76
|
+
const dx = (local.x - rx) / rx;
|
|
77
|
+
const dy = (local.y - ry) / ry;
|
|
78
|
+
return dx * dx + dy * dy <= 1;
|
|
79
|
+
}
|
|
80
|
+
if (mask.type === "RECTANGLE") return roundedRectContains(local, mask.width, mask.height, mask.cornerRadius);
|
|
81
|
+
return geometryBlobPolylines(mask.fillGeometry).some((points) => insidePolyline(points, local.x, local.y));
|
|
82
|
+
}
|
|
83
|
+
//#endregion
|
|
84
|
+
export { containsPoint, localPoint, maskContainsPoint };
|
|
85
|
+
|
|
86
|
+
//# sourceMappingURL=geometry.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"geometry.js","names":[],"sources":["../../src/hit-test/geometry.ts"],"sourcesContent":["// Where a point lies against a layer's box and a mask's outline, in the layer's own coordinates.\nimport { getWorldMatrix } from '../coordinate'\nimport { geometryBlobPolylines } from '../geometry'\nimport type { SceneGraph } from '../graph'\nimport Matrix from '../matrix'\nimport type { Vector } from '../primitives'\nimport type { SceneNode } from '../types'\n\n/**\n * How far from a zero-width or zero-height layer, in canvas units, a point still lands on it, so\n * a flat line or an empty box stays clickable; matches a connector line's slop (`connectors/hit.ts`).\n */\nconst DEGENERATE_AXIS_SLOP = 4\n\nfunction hasTransformedAncestor(\n node: SceneNode,\n graph: SceneGraph,\n cache: Map<string, boolean>\n): boolean {\n const cached = cache.get(node.id)\n if (cached !== undefined) return cached\n const parent = node.parentId ? graph.getNode(node.parentId) : undefined\n const transformed =\n node.rotation !== 0 ||\n node.flipX ||\n node.flipY ||\n (parent ? hasTransformedAncestor(parent, graph, cache) : false)\n cache.set(node.id, transformed)\n return transformed\n}\n\n/**\n * Where the point lies in the node's own coordinates: its offset from the position when nothing\n * above it is turned, else through the inverse world matrix. Null when that cannot be inverted.\n */\nexport function localPoint(\n px: number,\n py: number,\n node: SceneNode,\n graph: SceneGraph,\n transformCache: Map<string, boolean>\n): Vector | null {\n if (!hasTransformedAncestor(node, graph, transformCache)) {\n const absolute = graph.getAbsolutePosition(node.id)\n return { x: px - absolute.x, y: py - absolute.y }\n }\n const inv = Matrix.invert(getWorldMatrix(node, graph))\n if (!inv) return null\n const [x, y] = Matrix.mapPoints(inv, [px, py])\n return { x, y }\n}\n\n/** Whether the point lies in the node's box, grown by `pad` canvas units on every side. */\nexport function containsPoint(\n px: number,\n py: number,\n node: SceneNode,\n graph: SceneGraph,\n transformCache: Map<string, boolean>,\n pad = 0\n): boolean {\n const local = localPoint(px, py, node, graph, transformCache)\n if (!local) return false\n const padX = node.width > 0 ? pad : Math.max(pad, DEGENERATE_AXIS_SLOP)\n const padY = node.height > 0 ? pad : Math.max(pad, DEGENERATE_AXIS_SLOP)\n return (\n local.x >= -padX &&\n local.x <= node.width + padX &&\n local.y >= -padY &&\n local.y <= node.height + padY\n )\n}\n\n/** Whether a rounded rectangle of `width` × `height` holds the point. */\nfunction roundedRectContains(\n point: Vector,\n width: number,\n height: number,\n radius: number\n): boolean {\n if (point.x < 0 || point.y < 0 || point.x > width || point.y > height) return false\n const r = Math.max(0, Math.min(radius, width / 2, height / 2))\n const cx = Math.min(Math.max(point.x, r), width - r)\n const cy = Math.min(Math.max(point.y, r), height - r)\n return Math.hypot(point.x - cx, point.y - cy) <= r\n}\n\n/** Whether the point is inside a closed polyline, by the even-odd rule. */\nfunction insidePolyline(polyline: readonly Vector[], x: number, y: number): boolean {\n let inside = false\n for (let i = 0, j = polyline.length - 1; i < polyline.length; j = i++) {\n const a = polyline[i]\n const b = polyline[j]\n if (a.y > y === b.y > y) continue\n if (x < ((b.x - a.x) * (y - a.y)) / (b.y - a.y) + a.x) inside = !inside\n }\n return inside\n}\n\n/**\n * Whether a mask lets the point through, by the outline it draws: an ellipse's curve, a rounded\n * rectangle's corners, or a vector's filled path. Other shapes clip by their box.\n */\nexport function maskContainsPoint(\n px: number,\n py: number,\n mask: SceneNode,\n graph: SceneGraph,\n transformCache: Map<string, boolean>\n): boolean {\n const outlined =\n (mask.type === 'ELLIPSE' && mask.width > 0 && mask.height > 0) ||\n (mask.type === 'RECTANGLE' && mask.cornerRadius > 0) ||\n (mask.type === 'VECTOR' && mask.fillGeometry.length > 0)\n if (!outlined) return containsPoint(px, py, mask, graph, transformCache)\n const local = localPoint(px, py, mask, graph, transformCache)\n if (!local) return false\n if (mask.type === 'ELLIPSE') {\n const rx = mask.width / 2\n const ry = mask.height / 2\n const dx = (local.x - rx) / rx\n const dy = (local.y - ry) / ry\n return dx * dx + dy * dy <= 1\n }\n if (mask.type === 'RECTANGLE') {\n return roundedRectContains(local, mask.width, mask.height, mask.cornerRadius)\n }\n return geometryBlobPolylines(mask.fillGeometry).some((points) =>\n insidePolyline(points, local.x, local.y)\n )\n}\n"],"mappings":"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"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { SceneGraph } from "../graph.js";
|
|
2
|
+
import { SceneNode } from "../types.js";
|
|
3
|
+
//#region src/hit-test/masks.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* The masks that clip each child, by index, as the renderer groups them (`core/canvas/masks.ts`):
|
|
6
|
+
* a run of visible masks clips the layers after it up to the next visible mask. Empty when the
|
|
7
|
+
* container has no visible mask.
|
|
8
|
+
*/
|
|
9
|
+
declare function clippingMasks(graph: SceneGraph, childIds: readonly string[]): SceneNode[][];
|
|
10
|
+
/** Whether a point lies outside every mask of a run that clips a layer, so the layer hides it. */
|
|
11
|
+
declare function isPointMaskedOut(graph: SceneGraph, masks: readonly SceneNode[] | undefined, px: number, py: number): boolean;
|
|
12
|
+
//#endregion
|
|
13
|
+
export { clippingMasks, isPointMaskedOut };
|
|
14
|
+
//# sourceMappingURL=masks.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"masks.d.ts","names":[],"sources":["../../src/hit-test/masks.ts"],"mappings":";;;;;;;;iBAcgB,cAAc,OAAO,YAAY,8BAA8B;;iBAmC/D,iBACd,OAAO,YACP,gBAAgB,yBAChB,YACA"}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { maskContainsPoint } from "./geometry.js";
|
|
2
|
+
//#region src/hit-test/masks.ts
|
|
3
|
+
function isVisibleMask(node) {
|
|
4
|
+
return node !== void 0 && node.visible && node.isMask;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* The masks that clip each child, by index, as the renderer groups them (`core/canvas/masks.ts`):
|
|
8
|
+
* a run of visible masks clips the layers after it up to the next visible mask. Empty when the
|
|
9
|
+
* container has no visible mask.
|
|
10
|
+
*/
|
|
11
|
+
function clippingMasks(graph, childIds) {
|
|
12
|
+
const children = childIds.map((childId) => graph.nodes.get(childId));
|
|
13
|
+
if (!children.some(isVisibleMask)) return [];
|
|
14
|
+
const result = [];
|
|
15
|
+
let run = [];
|
|
16
|
+
let previousWasMask = false;
|
|
17
|
+
for (const child of children) {
|
|
18
|
+
if (isVisibleMask(child)) {
|
|
19
|
+
run = previousWasMask ? [...run, child] : [child];
|
|
20
|
+
previousWasMask = true;
|
|
21
|
+
result.push([]);
|
|
22
|
+
continue;
|
|
23
|
+
}
|
|
24
|
+
previousWasMask = false;
|
|
25
|
+
result.push(run);
|
|
26
|
+
}
|
|
27
|
+
return result;
|
|
28
|
+
}
|
|
29
|
+
/** Whether the masks that clip a layer leave the point outside every one of their outlines. */
|
|
30
|
+
function isMaskedOut(px, py, masks, graph, transformCache) {
|
|
31
|
+
if (!masks) return false;
|
|
32
|
+
return masks.length > 0 && !masks.some((mask) => maskContainsPoint(px, py, mask, graph, transformCache));
|
|
33
|
+
}
|
|
34
|
+
/** Whether a point lies outside every mask of a run that clips a layer, so the layer hides it. */
|
|
35
|
+
function isPointMaskedOut(graph, masks, px, py) {
|
|
36
|
+
return isMaskedOut(px, py, masks, graph, /* @__PURE__ */ new Map());
|
|
37
|
+
}
|
|
38
|
+
//#endregion
|
|
39
|
+
export { clippingMasks, isMaskedOut, isPointMaskedOut };
|
|
40
|
+
|
|
41
|
+
//# sourceMappingURL=masks.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"masks.js","names":[],"sources":["../../src/hit-test/masks.ts"],"sourcesContent":["// The masks that clip a container's children, and whether they hide a point.\nimport type { SceneGraph } from '../graph'\nimport type { SceneNode } from '../types'\nimport { maskContainsPoint } from './geometry'\n\nfunction isVisibleMask(node: SceneNode | undefined): node is SceneNode {\n return node !== undefined && node.visible && node.isMask\n}\n\n/**\n * The masks that clip each child, by index, as the renderer groups them (`core/canvas/masks.ts`):\n * a run of visible masks clips the layers after it up to the next visible mask. Empty when the\n * container has no visible mask.\n */\nexport function clippingMasks(graph: SceneGraph, childIds: readonly string[]): SceneNode[][] {\n const children = childIds.map((childId) => graph.nodes.get(childId))\n if (!children.some(isVisibleMask)) return []\n const result: SceneNode[][] = []\n let run: SceneNode[] = []\n let previousWasMask = false\n for (const child of children) {\n if (isVisibleMask(child)) {\n run = previousWasMask ? [...run, child] : [child]\n previousWasMask = true\n result.push([])\n continue\n }\n previousWasMask = false\n result.push(run)\n }\n return result\n}\n\n/** Whether the masks that clip a layer leave the point outside every one of their outlines. */\nexport function isMaskedOut(\n px: number,\n py: number,\n masks: readonly SceneNode[] | undefined,\n graph: SceneGraph,\n transformCache: Map<string, boolean>\n): boolean {\n if (!masks) return false\n return (\n masks.length > 0 &&\n !masks.some((mask) => maskContainsPoint(px, py, mask, graph, transformCache))\n )\n}\n\n/** Whether a point lies outside every mask of a run that clips a layer, so the layer hides it. */\nexport function isPointMaskedOut(\n graph: SceneGraph,\n masks: readonly SceneNode[] | undefined,\n px: number,\n py: number\n): boolean {\n return isMaskedOut(px, py, masks, graph, new Map())\n}\n"],"mappings":";;AAKA,SAAS,cAAc,MAAgD;CACrE,OAAO,SAAS,KAAA,KAAa,KAAK,WAAW,KAAK;AACpD;;;;;;AAOA,SAAgB,cAAc,OAAmB,UAA4C;CAC3F,MAAM,WAAW,SAAS,KAAK,YAAY,MAAM,MAAM,IAAI,OAAO,CAAC;CACnE,IAAI,CAAC,SAAS,KAAK,aAAa,GAAG,OAAO,CAAC;CAC3C,MAAM,SAAwB,CAAC;CAC/B,IAAI,MAAmB,CAAC;CACxB,IAAI,kBAAkB;CACtB,KAAK,MAAM,SAAS,UAAU;EAC5B,IAAI,cAAc,KAAK,GAAG;GACxB,MAAM,kBAAkB,CAAC,GAAG,KAAK,KAAK,IAAI,CAAC,KAAK;GAChD,kBAAkB;GAClB,OAAO,KAAK,CAAC,CAAC;GACd;EACF;EACA,kBAAkB;EAClB,OAAO,KAAK,GAAG;CACjB;CACA,OAAO;AACT;;AAGA,SAAgB,YACd,IACA,IACA,OACA,OACA,gBACS;CACT,IAAI,CAAC,OAAO,OAAO;CACnB,OACE,MAAM,SAAS,KACf,CAAC,MAAM,MAAM,SAAS,kBAAkB,IAAI,IAAI,MAAM,OAAO,cAAc,CAAC;AAEhF;;AAGA,SAAgB,iBACd,OACA,OACA,IACA,IACS;CACT,OAAO,YAAY,IAAI,IAAI,OAAO,uBAAO,IAAI,IAAI,CAAC;AACpD"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"paint.d.ts","names":[],"sources":["../../src/hit-test/paint.ts"],"mappings":";;iBAqBgB,WAAW,MAAM"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
//#region src/hit-test/paint.ts
|
|
2
|
+
/**
|
|
3
|
+
* Whether a paint shows, so its container's empty area counts when a click reaches past
|
|
4
|
+
* containers: a paint at 0% opacity, or a gradient whose stops are all transparent, does not,
|
|
5
|
+
* while 1% does, and the layer's own opacity does not matter.
|
|
6
|
+
*/
|
|
7
|
+
function isHitPaint(paint) {
|
|
8
|
+
if (!paint.visible || !(paint.opacity > 0)) return false;
|
|
9
|
+
if (paint.type === "SOLID") return paint.color.a > 0;
|
|
10
|
+
if (paint.type === "GRADIENT_LINEAR" || paint.type === "GRADIENT_RADIAL" || paint.type === "GRADIENT_ANGULAR" || paint.type === "GRADIENT_DIAMOND") return (paint.gradientStops ?? []).some((stop) => stop.color.a > 0);
|
|
11
|
+
return true;
|
|
12
|
+
}
|
|
13
|
+
function showsPaint(node) {
|
|
14
|
+
return node.fills.some(isHitPaint) || node.strokes.some(isHitPaint);
|
|
15
|
+
}
|
|
16
|
+
//#endregion
|
|
17
|
+
export { showsPaint };
|
|
18
|
+
|
|
19
|
+
//# sourceMappingURL=paint.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"paint.js","names":[],"sources":["../../src/hit-test/paint.ts"],"sourcesContent":["// Which layers show a paint where a pointer lands, so their area takes it.\nimport type { SceneNode } from '../types'\n\n/**\n * Whether a paint shows, so its container's empty area counts when a click reaches past\n * containers: a paint at 0% opacity, or a gradient whose stops are all transparent, does not,\n * while 1% does, and the layer's own opacity does not matter.\n */\nfunction isHitPaint(paint: SceneNode['fills'][number]): boolean {\n if (!paint.visible || !(paint.opacity > 0)) return false\n if (paint.type === 'SOLID') return paint.color.a > 0\n if (\n paint.type === 'GRADIENT_LINEAR' ||\n paint.type === 'GRADIENT_RADIAL' ||\n paint.type === 'GRADIENT_ANGULAR' ||\n paint.type === 'GRADIENT_DIAMOND'\n )\n return (paint.gradientStops ?? []).some((stop) => stop.color.a > 0)\n return true\n}\n\nexport function showsPaint(node: SceneNode): boolean {\n return node.fills.some(isHitPaint) || node.strokes.some(isHitPaint)\n}\n"],"mappings":";;;;;;AAQA,SAAS,WAAW,OAA4C;CAC9D,IAAI,CAAC,MAAM,WAAW,EAAE,MAAM,UAAU,IAAI,OAAO;CACnD,IAAI,MAAM,SAAS,SAAS,OAAO,MAAM,MAAM,IAAI;CACnD,IACE,MAAM,SAAS,qBACf,MAAM,SAAS,qBACf,MAAM,SAAS,sBACf,MAAM,SAAS,oBAEf,QAAQ,MAAM,iBAAiB,CAAC,EAAA,CAAG,MAAM,SAAS,KAAK,MAAM,IAAI,CAAC;CACpE,OAAO;AACT;AAEA,SAAgB,WAAW,MAA0B;CACnD,OAAO,KAAK,MAAM,KAAK,UAAU,KAAK,KAAK,QAAQ,KAAK,UAAU;AACpE"}
|
package/dist/hit-test.d.ts
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
import { Rect } from "./primitives/index.js";
|
|
2
|
+
import { clippingMasks, isPointMaskedOut } from "./hit-test/masks.js";
|
|
3
|
+
import { DropTargetOptions, hitTestDropTarget } from "./hit-test/drop.js";
|
|
4
|
+
import { showsPaint } from "./hit-test/paint.js";
|
|
2
5
|
import { SceneGraph } from "./graph.js";
|
|
3
6
|
import { SceneNode } from "./types.js";
|
|
4
7
|
//#region src/hit-test.d.ts
|
|
@@ -32,20 +35,6 @@ declare function hitTestOpenContainer(graph: SceneGraph, px: number, py: number,
|
|
|
32
35
|
declare function hitTestDeepInRect(graph: SceneGraph, rect: Rect, scopeId: string): SceneNode[];
|
|
33
36
|
/** Whether the point lies inside the node's rotated and flipped bounds. */
|
|
34
37
|
declare function isPointInNode(graph: SceneGraph, nodeId: string, px: number, py: number): boolean;
|
|
35
|
-
interface DropTargetOptions {
|
|
36
|
-
/**
|
|
37
|
-
* Set when the dropped layers are all main components: the component sets they come from.
|
|
38
|
-
* Components then go only into frames, sections, instances, and those sets.
|
|
39
|
-
*/
|
|
40
|
-
componentSetIds?: ReadonlySet<string>;
|
|
41
|
-
}
|
|
42
|
-
/**
|
|
43
|
-
* The topmost unlocked frame, section, component, or instance under the point, in its rotated
|
|
44
|
-
* shape and inside its clipping ancestors. Groups and component sets are looked through but never
|
|
45
|
-
* returned, except that a set takes dropped components; locked layers and their contents are
|
|
46
|
-
* skipped.
|
|
47
|
-
*/
|
|
48
|
-
declare function hitTestDropTarget(graph: SceneGraph, px: number, py: number, excludeIds: ReadonlySet<string>, scopeId?: string, options?: DropTargetOptions): SceneNode | null;
|
|
49
38
|
//#endregion
|
|
50
|
-
export { DropTargetOptions, hitTest, hitTestDeep, hitTestDeepInRect, hitTestDropTarget, hitTestOpenContainer, hitTestSelectable, isOpenContainer, isPointInNode };
|
|
39
|
+
export { type DropTargetOptions, clippingMasks, hitTest, hitTestDeep, hitTestDeepInRect, hitTestDropTarget, hitTestOpenContainer, hitTestSelectable, isOpenContainer, isPointInNode, isPointMaskedOut, showsPaint };
|
|
51
40
|
//# sourceMappingURL=hit-test.d.ts.map
|
package/dist/hit-test.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hit-test.d.ts","names":[],"sources":["../src/hit-test.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"hit-test.d.ts","names":[],"sources":["../src/hit-test.ts"],"mappings":";;;;;;;iBA6NgB,QACd,OAAO,YACP,YACA,YACA,mBACC;iBAKa,YACd,OAAO,YACP,YACA,YACA,kBACA,eACC;;;;;;;;;iBA6Ea,kBACd,OAAO,YACP,YACA,YACA,iBACA,aAAa,qBACb,cAEA;;AAAA,mBACC;;iBA6Ba,gBAAgB,OAAO,YAAY;;;;;iBASnC,qBACd,OAAO,YACP,YACA,YACA,kBACC;;;;;;;;iBA0Ea,kBAAkB,OAAO,YAAY,MAAM,MAAM,kBAAkB;;iBASnE,cAAc,OAAO,YAAY,gBAAgB,YAAY"}
|
package/dist/hit-test.js
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
import Matrix from "./matrix.js";
|
|
2
2
|
import { getAxisAlignedBoundsAt, getNodeLocalMatrix, getWorldMatrix } from "./coordinate.js";
|
|
3
|
-
import { geometryBlobPolylines } from "./geometry.js";
|
|
4
3
|
import { hitsConnectorLine } from "./connectors/hit.js";
|
|
4
|
+
import { containsPoint } from "./hit-test/geometry.js";
|
|
5
|
+
import { clippingMasks, isMaskedOut, isPointMaskedOut } from "./hit-test/masks.js";
|
|
6
|
+
import { showsPaint } from "./hit-test/paint.js";
|
|
5
7
|
import { drawnChildIds } from "./layout/draw-order.js";
|
|
8
|
+
import { hitTestDropTarget } from "./hit-test/drop.js";
|
|
6
9
|
//#region src/hit-test.ts
|
|
7
10
|
const CONTAINER_TYPES = /* @__PURE__ */ new Set([
|
|
8
11
|
"CANVAS",
|
|
@@ -15,25 +18,6 @@ const CONTAINER_TYPES = /* @__PURE__ */ new Set([
|
|
|
15
18
|
]);
|
|
16
19
|
const OPAQUE_CONTAINER_TYPES = /* @__PURE__ */ new Set(["COMPONENT", "INSTANCE"]);
|
|
17
20
|
/**
|
|
18
|
-
* How far from a zero-width or zero-height layer, in canvas units, a point still lands on it, so
|
|
19
|
-
* a flat line or an empty box stays clickable; matches a connector line's slop (`connectors/hit.ts`).
|
|
20
|
-
*/
|
|
21
|
-
const DEGENERATE_AXIS_SLOP = 4;
|
|
22
|
-
/**
|
|
23
|
-
* Whether a paint shows, so its container's empty area counts when a click reaches past
|
|
24
|
-
* containers: a paint at 0% opacity, or a gradient whose stops are all transparent, does not,
|
|
25
|
-
* while 1% does, and the layer's own opacity does not matter.
|
|
26
|
-
*/
|
|
27
|
-
function isHitPaint(paint) {
|
|
28
|
-
if (!paint.visible || !(paint.opacity > 0)) return false;
|
|
29
|
-
if (paint.type === "SOLID") return paint.color.a > 0;
|
|
30
|
-
if (paint.type === "GRADIENT_LINEAR" || paint.type === "GRADIENT_RADIAL" || paint.type === "GRADIENT_ANGULAR" || paint.type === "GRADIENT_DIAMOND") return (paint.gradientStops ?? []).some((stop) => stop.color.a > 0);
|
|
31
|
-
return true;
|
|
32
|
-
}
|
|
33
|
-
function showsPaint(node) {
|
|
34
|
-
return node.fills.some(isHitPaint) || node.strokes.some(isHitPaint);
|
|
35
|
-
}
|
|
36
|
-
/**
|
|
37
21
|
* Whether a container's empty area takes the click. Figma counts it only where a paint shows,
|
|
38
22
|
* except a main component's, which always counts, and an instance's when the click is a plain one.
|
|
39
23
|
*/
|
|
@@ -54,105 +38,6 @@ function isTopLevel(graph, node) {
|
|
|
54
38
|
function ownsItsEmptyArea(graph, node) {
|
|
55
39
|
return node.type === "FRAME" && node.layoutMode !== "NONE" && isTopLevel(graph, node);
|
|
56
40
|
}
|
|
57
|
-
function hasTransformedAncestor(node, graph, cache) {
|
|
58
|
-
const cached = cache.get(node.id);
|
|
59
|
-
if (cached !== void 0) return cached;
|
|
60
|
-
const parent = node.parentId ? graph.getNode(node.parentId) : void 0;
|
|
61
|
-
const transformed = node.rotation !== 0 || node.flipX || node.flipY || (parent ? hasTransformedAncestor(parent, graph, cache) : false);
|
|
62
|
-
cache.set(node.id, transformed);
|
|
63
|
-
return transformed;
|
|
64
|
-
}
|
|
65
|
-
/**
|
|
66
|
-
* Where the point lies in the node's own coordinates: its offset from the position when nothing
|
|
67
|
-
* above it is turned, else through the inverse world matrix. Null when that cannot be inverted.
|
|
68
|
-
*/
|
|
69
|
-
function localPoint(px, py, node, graph, transformCache) {
|
|
70
|
-
if (!hasTransformedAncestor(node, graph, transformCache)) {
|
|
71
|
-
const absolute = graph.getAbsolutePosition(node.id);
|
|
72
|
-
return {
|
|
73
|
-
x: px - absolute.x,
|
|
74
|
-
y: py - absolute.y
|
|
75
|
-
};
|
|
76
|
-
}
|
|
77
|
-
const inv = Matrix.invert(getWorldMatrix(node, graph));
|
|
78
|
-
if (!inv) return null;
|
|
79
|
-
const [x, y] = Matrix.mapPoints(inv, [px, py]);
|
|
80
|
-
return {
|
|
81
|
-
x,
|
|
82
|
-
y
|
|
83
|
-
};
|
|
84
|
-
}
|
|
85
|
-
/** Whether the point lies in the node's box, grown by `pad` canvas units on every side. */
|
|
86
|
-
function containsPoint(px, py, node, graph, transformCache, pad = 0) {
|
|
87
|
-
const local = localPoint(px, py, node, graph, transformCache);
|
|
88
|
-
if (!local) return false;
|
|
89
|
-
const padX = node.width > 0 ? pad : Math.max(pad, DEGENERATE_AXIS_SLOP);
|
|
90
|
-
const padY = node.height > 0 ? pad : Math.max(pad, DEGENERATE_AXIS_SLOP);
|
|
91
|
-
return local.x >= -padX && local.x <= node.width + padX && local.y >= -padY && local.y <= node.height + padY;
|
|
92
|
-
}
|
|
93
|
-
/** Whether a rounded rectangle of `width` × `height` holds the point. */
|
|
94
|
-
function roundedRectContains(point, width, height, radius) {
|
|
95
|
-
if (point.x < 0 || point.y < 0 || point.x > width || point.y > height) return false;
|
|
96
|
-
const r = Math.max(0, Math.min(radius, width / 2, height / 2));
|
|
97
|
-
const cx = Math.min(Math.max(point.x, r), width - r);
|
|
98
|
-
const cy = Math.min(Math.max(point.y, r), height - r);
|
|
99
|
-
return Math.hypot(point.x - cx, point.y - cy) <= r;
|
|
100
|
-
}
|
|
101
|
-
/** Whether the point is inside a closed polyline, by the even-odd rule. */
|
|
102
|
-
function insidePolyline(polyline, x, y) {
|
|
103
|
-
let inside = false;
|
|
104
|
-
for (let i = 0, j = polyline.length - 1; i < polyline.length; j = i++) {
|
|
105
|
-
const a = polyline[i];
|
|
106
|
-
const b = polyline[j];
|
|
107
|
-
if (a.y > y === b.y > y) continue;
|
|
108
|
-
if (x < (b.x - a.x) * (y - a.y) / (b.y - a.y) + a.x) inside = !inside;
|
|
109
|
-
}
|
|
110
|
-
return inside;
|
|
111
|
-
}
|
|
112
|
-
/**
|
|
113
|
-
* Whether a mask lets the point through, by the outline it draws: an ellipse's curve, a rounded
|
|
114
|
-
* rectangle's corners, or a vector's filled path. Other shapes clip by their box.
|
|
115
|
-
*/
|
|
116
|
-
function maskContainsPoint(px, py, mask, graph, transformCache) {
|
|
117
|
-
if (!(mask.type === "ELLIPSE" && mask.width > 0 && mask.height > 0 || mask.type === "RECTANGLE" && mask.cornerRadius > 0 || mask.type === "VECTOR" && mask.fillGeometry.length > 0)) return containsPoint(px, py, mask, graph, transformCache);
|
|
118
|
-
const local = localPoint(px, py, mask, graph, transformCache);
|
|
119
|
-
if (!local) return false;
|
|
120
|
-
if (mask.type === "ELLIPSE") {
|
|
121
|
-
const rx = mask.width / 2;
|
|
122
|
-
const ry = mask.height / 2;
|
|
123
|
-
const dx = (local.x - rx) / rx;
|
|
124
|
-
const dy = (local.y - ry) / ry;
|
|
125
|
-
return dx * dx + dy * dy <= 1;
|
|
126
|
-
}
|
|
127
|
-
if (mask.type === "RECTANGLE") return roundedRectContains(local, mask.width, mask.height, mask.cornerRadius);
|
|
128
|
-
return geometryBlobPolylines(mask.fillGeometry).some((points) => insidePolyline(points, local.x, local.y));
|
|
129
|
-
}
|
|
130
|
-
function isVisibleMask(node) {
|
|
131
|
-
return node !== void 0 && node.visible && node.isMask;
|
|
132
|
-
}
|
|
133
|
-
/**
|
|
134
|
-
* The masks that clip each child, by index, as the renderer groups them (`core/canvas/masks.ts`):
|
|
135
|
-
* a run of visible masks clips the layers after it up to the next visible mask. Empty when the
|
|
136
|
-
* container has no visible mask.
|
|
137
|
-
*/
|
|
138
|
-
function clippingMasks(graph, childIds) {
|
|
139
|
-
const children = childIds.map((childId) => graph.nodes.get(childId));
|
|
140
|
-
if (!children.some(isVisibleMask)) return [];
|
|
141
|
-
const result = [];
|
|
142
|
-
let run = [];
|
|
143
|
-
let previousWasMask = false;
|
|
144
|
-
for (const child of children) {
|
|
145
|
-
if (isVisibleMask(child)) {
|
|
146
|
-
run = previousWasMask ? [...run, child] : [child];
|
|
147
|
-
previousWasMask = true;
|
|
148
|
-
result.push([]);
|
|
149
|
-
continue;
|
|
150
|
-
}
|
|
151
|
-
previousWasMask = false;
|
|
152
|
-
result.push(run);
|
|
153
|
-
}
|
|
154
|
-
return result;
|
|
155
|
-
}
|
|
156
41
|
/**
|
|
157
42
|
* What a click inside a container reports. `opaque` is an instance or component whose body takes
|
|
158
43
|
* the click without looking through it; every other container looks through to its children first.
|
|
@@ -182,11 +67,6 @@ function hitTestContainer(graph, px, py, child, childId, deep, transformCache, p
|
|
|
182
67
|
function standIn(container, hit, deep) {
|
|
183
68
|
return container.locked || !deep ? container : hit;
|
|
184
69
|
}
|
|
185
|
-
/** Whether the masks that clip a layer leave the point outside every one of their outlines. */
|
|
186
|
-
function isMaskedOut(px, py, masks, graph, transformCache) {
|
|
187
|
-
if (!masks) return false;
|
|
188
|
-
return masks.length > 0 && !masks.some((mask) => maskContainsPoint(px, py, mask, graph, transformCache));
|
|
189
|
-
}
|
|
190
70
|
/**
|
|
191
71
|
* The topmost layer under the point among a container's children. `pad`, in canvas units, grows
|
|
192
72
|
* each leaf layer's box, as Figma takes a click a few screen pixels outside a shape.
|
|
@@ -391,55 +271,7 @@ function isPointInNode(graph, nodeId, px, py) {
|
|
|
391
271
|
const node = graph.nodes.get(nodeId);
|
|
392
272
|
return node !== void 0 && containsPoint(px, py, node, graph, /* @__PURE__ */ new Map());
|
|
393
273
|
}
|
|
394
|
-
const LAYER_TARGETS = /* @__PURE__ */ new Set([
|
|
395
|
-
"FRAME",
|
|
396
|
-
"SECTION",
|
|
397
|
-
"COMPONENT",
|
|
398
|
-
"INSTANCE"
|
|
399
|
-
]);
|
|
400
|
-
const COMPONENT_TARGETS = /* @__PURE__ */ new Set([
|
|
401
|
-
"FRAME",
|
|
402
|
-
"SECTION",
|
|
403
|
-
"INSTANCE"
|
|
404
|
-
]);
|
|
405
|
-
function dropRules(options) {
|
|
406
|
-
const { componentSetIds } = options;
|
|
407
|
-
if (!componentSetIds) return {
|
|
408
|
-
takes: (node) => LAYER_TARGETS.has(node.type),
|
|
409
|
-
passThrough: /* @__PURE__ */ new Set(["GROUP", "COMPONENT_SET"])
|
|
410
|
-
};
|
|
411
|
-
return {
|
|
412
|
-
takes: (node) => COMPONENT_TARGETS.has(node.type) || node.type === "COMPONENT_SET" && componentSetIds.has(node.id),
|
|
413
|
-
passThrough: /* @__PURE__ */ new Set(["GROUP"])
|
|
414
|
-
};
|
|
415
|
-
}
|
|
416
|
-
function dropTargetIn(graph, px, py, parent, excludeIds, rules, transformCache) {
|
|
417
|
-
if (parent.type !== "CANVAS" && parent.clipsContent && !containsPoint(px, py, parent, graph, transformCache)) return null;
|
|
418
|
-
for (let i = parent.childIds.length - 1; i >= 0; i--) {
|
|
419
|
-
const childId = parent.childIds[i];
|
|
420
|
-
if (excludeIds.has(childId)) continue;
|
|
421
|
-
const child = graph.nodes.get(childId);
|
|
422
|
-
if (!child || child.internalOnly || !child.visible || child.locked) continue;
|
|
423
|
-
const target = rules.takes(child);
|
|
424
|
-
if (!target && !rules.passThrough.has(child.type)) continue;
|
|
425
|
-
const deeper = dropTargetIn(graph, px, py, child, excludeIds, rules, transformCache);
|
|
426
|
-
if (deeper) return deeper;
|
|
427
|
-
if (target && containsPoint(px, py, child, graph, transformCache)) return child;
|
|
428
|
-
}
|
|
429
|
-
return null;
|
|
430
|
-
}
|
|
431
|
-
/**
|
|
432
|
-
* The topmost unlocked frame, section, component, or instance under the point, in its rotated
|
|
433
|
-
* shape and inside its clipping ancestors. Groups and component sets are looked through but never
|
|
434
|
-
* returned, except that a set takes dropped components; locked layers and their contents are
|
|
435
|
-
* skipped.
|
|
436
|
-
*/
|
|
437
|
-
function hitTestDropTarget(graph, px, py, excludeIds, scopeId, options = {}) {
|
|
438
|
-
const scope = graph.nodes.get(scopeId ?? graph.rootId);
|
|
439
|
-
if (!scope) return null;
|
|
440
|
-
return dropTargetIn(graph, px, py, scope, excludeIds, dropRules(options), /* @__PURE__ */ new Map());
|
|
441
|
-
}
|
|
442
274
|
//#endregion
|
|
443
|
-
export { hitTest, hitTestDeep, hitTestDeepInRect, hitTestDropTarget, hitTestOpenContainer, hitTestSelectable, isOpenContainer, isPointInNode };
|
|
275
|
+
export { clippingMasks, hitTest, hitTestDeep, hitTestDeepInRect, hitTestDropTarget, hitTestOpenContainer, hitTestSelectable, isOpenContainer, isPointInNode, isPointMaskedOut, showsPaint };
|
|
444
276
|
|
|
445
277
|
//# sourceMappingURL=hit-test.js.map
|