Skip to content

Commit 2f3c300

Browse files
split positioning logic
1 parent 63ca334 commit 2f3c300

1 file changed

Lines changed: 155 additions & 102 deletions

File tree

  • packages/designto-token/token-layout

packages/designto-token/token-layout/index.ts

Lines changed: 155 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { nodes, ReflectSceneNodeType } from "@design-sdk/core";
22
import { layoutAlignToReflectMainAxisSize } from "@design-sdk/figma-node-conversion";
3+
import type { Constraints } from "@design-sdk/figma-types";
34
import * as core from "@reflect-ui/core";
45
import {
56
Axis,
@@ -278,7 +279,7 @@ function stackChild({
278279
container: nodes.ReflectSceneNode;
279280
wchild: core.Widget;
280281
}) {
281-
const constraint = {
282+
let constraint = {
282283
left: undefined,
283284
top: undefined,
284285
right: undefined,
@@ -290,16 +291,11 @@ function stackChild({
290291
const _unwrappedChild: IWHStyleWidget = unwrappedChild(
291292
child
292293
) as IWHStyleWidget;
293-
const wh = {
294+
let wh = {
294295
width: _unwrappedChild.width,
295296
height: _unwrappedChild.height,
296297
};
297298

298-
const _l = ogchild.x;
299-
const _r = container.width - (ogchild.x + ogchild.width);
300-
const _t = ogchild.y;
301-
const _b = container.height - (ogchild.y + ogchild.height);
302-
303299
/**
304300
* "MIN": Left or Top
305301
* "MAX": Right or Bottom
@@ -320,101 +316,25 @@ function stackChild({
320316
);
321317
// throw `${ogchild.toString()} has no constraints. this can happen when node under group item tokenization is incomplete. this is engine's error.`;
322318
} else {
323-
switch (ogchild.constraints.horizontal) {
324-
case "MIN":
325-
constraint.left = _l;
326-
break;
327-
case "MAX":
328-
constraint.right = _r;
329-
break;
330-
case "SCALE": /** scale fallbacks to stretch */
331-
case "STRETCH":
332-
constraint.left = _l;
333-
constraint.right = _r;
334-
wh.width = undefined; // no fixed width
335-
break;
336-
case "CENTER":
337-
const half_w = ogchild.width / 2;
338-
const centerdiff =
339-
// center of og
340-
half_w +
341-
ogchild.x -
342-
// center of frame
343-
container.width / 2;
344-
constraint.left = <Calculation>{
345-
type: "calc",
346-
operations: <Operation>{
347-
type: "op",
348-
left: {
349-
type: "calc",
350-
operations: <Operation>{
351-
type: "op",
352-
left: "50%",
353-
op: "+",
354-
right: centerdiff,
355-
},
356-
},
357-
op: "-", // this part is different
358-
right: half_w,
359-
},
360-
};
361-
// --- we can also specify the right, but left is enough.
362-
// constraint.right = <Calculation>{
363-
// type: "calc",
364-
// operations: {
365-
// left: {
366-
// type: "calc",
367-
// operations: { left: "50%", op: "+", right: centerdiff },
368-
// },
369-
// op: "+", // this part is different
370-
// right: half,
371-
// },
372-
// };
373-
break;
374-
}
375-
switch (ogchild.constraints.vertical) {
376-
case "MIN":
377-
constraint.top = _t;
378-
break;
379-
case "MAX":
380-
constraint.bottom = _b;
381-
break;
382-
case "SCALE": /** scale fallbacks to stretch */
383-
case "STRETCH":
384-
constraint.top = _t;
385-
constraint.bottom = _b;
386-
wh.height = undefined;
387-
break;
388-
case "CENTER":
389-
const half_height = ogchild.height / 2;
390-
const container_snapshot_center = container.height / 2;
391-
const child_snapshot_center = half_height + ogchild.y;
392-
393-
const centerdiff =
394-
// center of og
395-
child_snapshot_center -
396-
// center of frame
397-
container_snapshot_center;
398-
399-
constraint.top = <Calculation>{
400-
type: "calc",
401-
operations: <Operation>{
402-
type: "op",
403-
left: {
404-
type: "calc",
405-
operations: <Operation>{
406-
type: "op",
407-
left: "50%",
408-
op: "+",
409-
right: centerdiff,
410-
},
411-
},
412-
op: "-", // this part is different
413-
right: half_height,
414-
},
415-
};
416-
break;
417-
}
319+
const _l = ogchild.x;
320+
const _r = container.width - (ogchild.x + ogchild.width);
321+
const _t = ogchild.y;
322+
const _b = container.height - (ogchild.y + ogchild.height);
323+
324+
const res = handlePositioning({
325+
constraints: ogchild.constraints,
326+
pos: { l: _l, t: _t, b: _b, r: _r, x: ogchild.x, y: ogchild.y },
327+
width: ogchild.width,
328+
height: ogchild.height,
329+
containerWidth: container.width,
330+
containerHeight: container.height,
331+
});
332+
333+
constraint = res.constraint;
334+
wh = {
335+
...wh,
336+
...res.wh,
337+
};
418338
}
419339

420340
// console.log("positioning based on constraints", { wh, constraint, child });
@@ -430,6 +350,139 @@ function stackChild({
430350
});
431351
}
432352

353+
/**
354+
* calculates the position & constraints based on the input.
355+
* @param
356+
* @returns
357+
*/
358+
function handlePositioning({
359+
constraints,
360+
pos,
361+
width,
362+
height,
363+
containerWidth,
364+
containerHeight,
365+
}: {
366+
constraints: Constraints;
367+
pos: { l: number; r: number; t: number; b: number; x: number; y: number };
368+
width: number;
369+
height: number;
370+
containerWidth: number;
371+
containerHeight: number;
372+
}): {
373+
constraint;
374+
wh: {
375+
width?: number;
376+
height?: number;
377+
};
378+
} {
379+
const constraint = {
380+
left: undefined,
381+
top: undefined,
382+
right: undefined,
383+
bottom: undefined,
384+
};
385+
const wh = { width, height };
386+
387+
switch (constraints.horizontal) {
388+
case "MIN":
389+
constraint.left = pos.l;
390+
break;
391+
case "MAX":
392+
constraint.right = pos.r;
393+
break;
394+
case "SCALE": /** scale fallbacks to stretch */
395+
case "STRETCH":
396+
constraint.left = pos.l;
397+
constraint.right = pos.r;
398+
wh.width = undefined; // no fixed width
399+
break;
400+
case "CENTER":
401+
const half_w = width / 2;
402+
const centerdiff =
403+
// center of og
404+
half_w +
405+
pos.x -
406+
// center of frame
407+
containerWidth / 2;
408+
constraint.left = <Calculation>{
409+
type: "calc",
410+
operations: <Operation>{
411+
type: "op",
412+
left: {
413+
type: "calc",
414+
operations: <Operation>{
415+
type: "op",
416+
left: "50%",
417+
op: "+",
418+
right: centerdiff,
419+
},
420+
},
421+
op: "-", // this part is different
422+
right: half_w,
423+
},
424+
};
425+
// --- we can also specify the right, but left is enough.
426+
// constraint.right = <Calculation>{
427+
// type: "calc",
428+
// operations: {
429+
// left: {
430+
// type: "calc",
431+
// operations: { left: "50%", op: "+", right: centerdiff },
432+
// },
433+
// op: "+", // this part is different
434+
// right: half,
435+
// },
436+
// };
437+
break;
438+
}
439+
switch (constraints.vertical) {
440+
case "MIN":
441+
constraint.top = pos.t;
442+
break;
443+
case "MAX":
444+
constraint.bottom = pos.b;
445+
break;
446+
case "SCALE": /** scale fallbacks to stretch */
447+
case "STRETCH":
448+
constraint.top = pos.t;
449+
constraint.bottom = pos.b;
450+
wh.height = undefined;
451+
break;
452+
case "CENTER":
453+
const half_height = height / 2;
454+
const container_snapshot_center = containerHeight / 2;
455+
const child_snapshot_center = half_height + pos.y;
456+
457+
const centerdiff =
458+
// center of og
459+
child_snapshot_center -
460+
// center of frame
461+
container_snapshot_center;
462+
463+
constraint.top = <Calculation>{
464+
type: "calc",
465+
operations: <Operation>{
466+
type: "op",
467+
left: {
468+
type: "calc",
469+
operations: <Operation>{
470+
type: "op",
471+
left: "50%",
472+
op: "+",
473+
right: centerdiff,
474+
},
475+
},
476+
op: "-", // this part is different
477+
right: half_height,
478+
},
479+
};
480+
break;
481+
}
482+
483+
return { constraint, wh };
484+
}
485+
433486
function fromGroup(
434487
group: nodes.ReflectGroupNode,
435488
children: RuntimeChildrenInput,

0 commit comments

Comments
 (0)