@moondraft/import 0.2.0 → 0.4.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/CHANGELOG.md +26 -0
- package/SOURCE.md +138 -1
- package/dist/adapter/convert.d.ts +2 -0
- package/dist/adapter/convert.js +192 -85
- package/dist/adapter/figma-handoff.d.ts +24 -2
- package/dist/adapter/figma-handoff.js +39 -18
- package/dist/adapter/figma.js +2 -1
- package/dist/adapter/file-components.d.ts +20 -1
- package/dist/adapter/file-components.js +91 -12
- package/dist/adapter/file-resources.d.ts +5 -0
- package/dist/adapter/file-resources.js +286 -40
- package/dist/adapter/file-typography.d.ts +6 -0
- package/dist/adapter/file-typography.js +44 -0
- package/dist/adapter/link.d.ts +8 -1
- package/dist/adapter/link.js +219 -105
- package/dist/adapter/payload.js +2 -2
- package/dist/adapter/variables.d.ts +1 -1
- package/dist/figma-file.d.ts +1 -0
- package/dist/figma-file.js +1 -0
- package/dist/figma.d.ts +1 -0
- package/dist/figma.js +41 -19
- package/dist/report.d.ts +37 -6
- package/dist/report.js +91 -18
- package/dist/taco/figma-import/figFile.js +3 -0
- package/dist/taco/figma-import/import/carry.d.ts +11 -1
- package/dist/taco/figma-import/import/carry.js +8 -0
- package/dist/taco/figma-import/import/converters/effects.d.ts +3 -13
- package/dist/taco/figma-import/import/converters/effects.js +4 -34
- package/dist/taco/figma-import/import/converters/exportSettings.js +26 -0
- package/dist/taco/figma-import/import/converters/frame.d.ts +1 -1
- package/dist/taco/figma-import/import/converters/frame.js +8 -7
- package/dist/taco/figma-import/import/converters/grid.d.ts +12 -0
- package/dist/taco/figma-import/import/converters/grid.js +97 -0
- package/dist/taco/figma-import/import/converters/group.d.ts +1 -0
- package/dist/taco/figma-import/import/converters/group.js +5 -2
- package/dist/taco/figma-import/import/converters/image.d.ts +1 -0
- package/dist/taco/figma-import/import/converters/image.js +38 -5
- package/dist/taco/figma-import/import/converters/layout.d.ts +5 -3
- package/dist/taco/figma-import/import/converters/layout.js +26 -16
- package/dist/taco/figma-import/import/converters/rectangle.js +2 -2
- package/dist/taco/figma-import/import/converters/section.js +2 -2
- package/dist/taco/figma-import/import/converters/shared.d.ts +2 -1
- package/dist/taco/figma-import/import/converters/shared.js +8 -5
- package/dist/taco/figma-import/import/converters/text.d.ts +24 -0
- package/dist/taco/figma-import/import/converters/text.js +121 -31
- package/dist/taco/figma-import/import/instances.d.ts +19 -3
- package/dist/taco/figma-import/import/instances.js +337 -60
- package/dist/taco/figma-import/import/types.d.ts +8 -0
- package/dist/taco/figma-import/import/variables.d.ts +2 -0
- package/dist/taco/figma-import/import/variables.js +28 -1
- package/dist/taco/figma-import/import/variantTrees.js +2 -0
- package/dist/taco/figma-import/import/walk.d.ts +9 -2
- package/dist/taco/figma-import/import/walk.js +69 -30
- package/dist/taco/figma-import/kiwi/message.js +52 -10
- package/dist/taco/model/codec.d.ts +1 -1
- package/dist/taco/model/codec.js +40 -6
- package/dist/taco/model/instance-resolver.js +3 -1
- package/dist/taco/model/types.d.ts +36 -6
- package/dist/taco/model/types.js +4 -0
- package/dist/taco/svg-import/style/computed.js +1 -0
- package/package.json +3 -3
package/dist/adapter/convert.js
CHANGED
|
@@ -28,7 +28,10 @@ import { EXPRESSION_TYPES } from '../taco/model/expressions/types.js';
|
|
|
28
28
|
import { expressionColorToHex } from '../taco/model/expressions/values.js';
|
|
29
29
|
import { withoutTextMeasurement } from '../taco/model/text-reads.js';
|
|
30
30
|
import { createInstanceResolveCache, resolveComponent, resolveInstance } from '../taco/model/instance-resolver.js';
|
|
31
|
+
import { applyFileTypography } from './file-typography.js';
|
|
31
32
|
import { linkWithLayers, unmark } from './link.js';
|
|
33
|
+
import { boundTextExpression, layerComponents } from './file-components.js';
|
|
34
|
+
const supportsGrid = 'layoutData.gridCell' in editorSchema.Properties;
|
|
32
35
|
// Figma stores float32 sizes. Comparing after layout or instance scaling can
|
|
33
36
|
// also include rounding from the arithmetic that produced the two sizes.
|
|
34
37
|
function sameFigmaSize(a, b, absoluteTolerance = 0) {
|
|
@@ -75,6 +78,8 @@ function fittedGroupAxis(size, children, axis) {
|
|
|
75
78
|
if (minimum <= far)
|
|
76
79
|
return { extent: far, minimum };
|
|
77
80
|
}
|
|
81
|
+
const supportsVideo = 'mediaType' in editorSchema.AssetSchema.properties &&
|
|
82
|
+
editorSchema.firstError(editorSchema.PaintSchema, { k: 'video', asset: 'clip' }) === null;
|
|
78
83
|
/** Converts a reader colour to the RGBA precision used by imported paints.
|
|
79
84
|
* @param hex - Source hex colour or expression colour.
|
|
80
85
|
* @returns Normalized channels, or null for a non-colour value.
|
|
@@ -106,20 +111,31 @@ export function runStyle(s) {
|
|
|
106
111
|
...(features.length > 0 && { features: Object.fromEntries(features.map(([tag, value]) => [tag, Number(value)])) }),
|
|
107
112
|
...(s.fontVariations && { variations: s.fontVariations }), size, lineHeight: +(size * (s.lineHeight ?? 1.2)).toFixed(3),
|
|
108
113
|
weight, italic: !!s.italic, underline: !!s.underline, ...(s.strikethrough ? { strikethrough: true } : {}),
|
|
114
|
+
...(s.textCase && s.textCase !== 'original' && { textCase: s.textCase }),
|
|
109
115
|
color: rgba(s.color) ?? [0, 0, 0, 1], letterSpacing: s.letterSpacing ?? 0,
|
|
110
116
|
};
|
|
111
117
|
}
|
|
112
118
|
const EXPORT_QUALITY = { low: 0.5, medium: 0.75, high: 0.92 };
|
|
119
|
+
/** Most export rows the editor keeps on one layer (its schema's `exports` limit). */
|
|
120
|
+
const MAX_EXPORTS = 32;
|
|
113
121
|
/** A source layer's export rows as the editor's `exports`: the format, the constraint as a scale, a width or a
|
|
114
|
-
* height, the suffix, and for JPEG the quality as a number. The rest of a row
|
|
115
|
-
*
|
|
116
|
-
|
|
122
|
+
* height, the suffix, and for JPEG the quality as a number. The rest of a row has no counterpart; `lost` hears of
|
|
123
|
+
* each field that differs from what the editor exports anyway (rows past the editor's limit, basic resampling, a
|
|
124
|
+
* drawing of overlapping layers, SVG id attributes). */
|
|
125
|
+
function exportsOf(rows, lost) {
|
|
117
126
|
if (!rows?.length)
|
|
118
127
|
return undefined;
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
128
|
+
for (let n = rows.length - MAX_EXPORTS; n > 0; n--)
|
|
129
|
+
lost(`export setting beyond the editor's ${MAX_EXPORTS}-row limit`);
|
|
130
|
+
return rows.slice(0, MAX_EXPORTS).map(({ format, constraint, suffix, quality, resampling, ignoreOverlappingLayers, includeIdAttribute }) => {
|
|
131
|
+
if (resampling !== 'detailed' && format !== 'svg')
|
|
132
|
+
lost('export setting resampling (basic)');
|
|
133
|
+
if (!ignoreOverlappingLayers && format !== 'pdf')
|
|
134
|
+
lost('export setting contents only (off: overlapping layers drawn)');
|
|
135
|
+
if (includeIdAttribute && format === 'svg')
|
|
136
|
+
lost('export setting SVG id attributes (always)');
|
|
137
|
+
return { format, [constraint.type]: constraint.value, suffix, ...(format === 'jpeg' && { quality: EXPORT_QUALITY[quality] }) };
|
|
138
|
+
});
|
|
123
139
|
}
|
|
124
140
|
/**
|
|
125
141
|
* Converts one page of a Taco document to Moondraft records.
|
|
@@ -146,7 +162,7 @@ export function figmaClipboardPage(doc, instanceSources) {
|
|
|
146
162
|
* @internal
|
|
147
163
|
*/
|
|
148
164
|
export function allPages(doc, options) {
|
|
149
|
-
const file = { expanded: new Map(), nestedBooleans: new Map(), omittedLinks: new Map() };
|
|
165
|
+
const file = { expanded: new Map(), nestedBooleans: new Map(), omittedLinks: new Map(), expandedReasons: new Map() };
|
|
150
166
|
return { ...convert(doc, doc.pageOrder.map((id) => doc.pages[id]), options, true, file, true), ...file };
|
|
151
167
|
}
|
|
152
168
|
// The boolean group operations (FrameNode.boolean) a live Figma boolean operation carries.
|
|
@@ -250,8 +266,8 @@ function expand(doc, pages, options, explicit, file, deferHyperlinks) {
|
|
|
250
266
|
// A nested instance layer stands for its instance node, any other for its source.
|
|
251
267
|
const layerNodeId = (layer) => layer.instance?.instanceId ?? layer.sourceNodeId;
|
|
252
268
|
// Override fields the size rules read; the rest of an override map comes from the resolved layer.
|
|
253
|
-
const SIZE_OVERRIDES = new Set(['sizing', 'padding', 'textStyle', 'lineBoxOnly', 'frame', 'x', 'y', 'width', 'height', 'resizing', 'layoutPositioning', 'constraints', 'layout', 'variableModes', 'variableBindings', 'expressions']);
|
|
254
|
-
const SIZE_BINDINGS = new Set(['width', 'height', 'gap', 'paddingTop', 'paddingRight', 'paddingBottom', 'paddingLeft', 'fontFamily']);
|
|
269
|
+
const SIZE_OVERRIDES = new Set(['sizing', 'padding', 'gridCell', 'textStyle', 'lineBoxOnly', 'frame', 'x', 'y', 'width', 'height', 'resizing', 'layoutPositioning', 'constraints', 'layout', 'variableModes', 'variableBindings', 'expressions']);
|
|
270
|
+
const SIZE_BINDINGS = new Set(['width', 'height', 'gap', 'columnGap', 'rowGap', 'paddingTop', 'paddingRight', 'paddingBottom', 'paddingLeft', 'fontFamily']);
|
|
255
271
|
function sizeOverrides(o) {
|
|
256
272
|
const out = {};
|
|
257
273
|
for (const [k, v] of Object.entries(o)) {
|
|
@@ -541,12 +557,15 @@ function expand(doc, pages, options, explicit, file, deferHyperlinks) {
|
|
|
541
557
|
return out;
|
|
542
558
|
}
|
|
543
559
|
function boxAt(scopes, id) {
|
|
560
|
+
let inner;
|
|
544
561
|
for (const { boxes, prefix, fillScale } of scopes ?? []) {
|
|
545
562
|
const box = boxes.get(prefix + id);
|
|
546
|
-
if (box)
|
|
563
|
+
if (box && !box.inner)
|
|
547
564
|
return { box, fillScale };
|
|
565
|
+
if (box)
|
|
566
|
+
inner ??= fillScale === undefined ? { box } : { box, fillScale };
|
|
548
567
|
}
|
|
549
|
-
return
|
|
568
|
+
return inner;
|
|
550
569
|
}
|
|
551
570
|
function descendSlots(map, instanceId) {
|
|
552
571
|
const out = {}, prefix = `${instanceId};`;
|
|
@@ -659,7 +678,7 @@ function expand(doc, pages, options, explicit, file, deferHyperlinks) {
|
|
|
659
678
|
continue; // a name (a layer renamed in an instance) is kept
|
|
660
679
|
if (k === 'propertyValues')
|
|
661
680
|
out.propertyValues = { ...out.propertyValues, ...v };
|
|
662
|
-
else if (['textStyle', 'variableBindings', 'variableModes', 'expressions', 'style', 'sizing', 'padding', 'layout'].includes(k))
|
|
681
|
+
else if (['textStyle', 'variableBindings', 'variableModes', 'expressions', 'style', 'sizing', 'padding', 'layout', 'gridCell'].includes(k))
|
|
663
682
|
out[k] = { ...out[k], ...v };
|
|
664
683
|
else
|
|
665
684
|
out[k] = v;
|
|
@@ -671,13 +690,19 @@ function expand(doc, pages, options, explicit, file, deferHyperlinks) {
|
|
|
671
690
|
}
|
|
672
691
|
}
|
|
673
692
|
// ---- Paints and colours.
|
|
693
|
+
const posters = new Map();
|
|
674
694
|
const stops = (s) => s.map((x) => [Math.min(1, Math.max(0, x.offset)), rgba(x.color) ?? [0, 0, 0, 1]]);
|
|
675
695
|
// Whether a model draws a fill: its compact one, or a shown paint in look.fills.
|
|
676
696
|
const shownFills = (m) => !!m.fill || !!m.look?.fills?.some((p) => p.visible !== false);
|
|
677
697
|
// A paint for the wire (packages/editor/src/renderer/wire.ts paint()), or null when dropped. A hidden paint keeps its
|
|
678
698
|
// place in the list (visible: false), as Figma keeps it; one that would be dropped is left out without counting.
|
|
679
699
|
// tile: the scale of an image fill Figma tiles (figma-import's metadata.figma.tiles, see taco/.../carry.ts).
|
|
700
|
+
// A paint's blend mode goes on the wire as its code (blend; normal is none).
|
|
680
701
|
function paint(p, tile) {
|
|
702
|
+
if (p.blendMode !== undefined && p.blendMode !== 'normal') {
|
|
703
|
+
const q = paint({ ...p, blendMode: undefined }, tile);
|
|
704
|
+
return q && { ...q, blend: BLEND_MODES.indexOf(p.blendMode) };
|
|
705
|
+
}
|
|
681
706
|
if (p.visible === false) {
|
|
682
707
|
const had = new Map(dropped), q = paint({ ...p, visible: true }, tile);
|
|
683
708
|
dropped.clear();
|
|
@@ -712,11 +737,25 @@ function expand(doc, pages, options, explicit, file, deferHyperlinks) {
|
|
|
712
737
|
return { k: 'angular', cx: p.center?.x ?? 0.5, cy: p.center?.y ?? 0.5, start: p.angle ?? 0, stops: stops(p.stops), o };
|
|
713
738
|
if (p.kind === 'image') {
|
|
714
739
|
const asset = doc.assets?.[p.assetId];
|
|
715
|
-
if (asset?.kind !== 'image' || asset.source?.kind !== 'data') {
|
|
740
|
+
if ((asset?.kind !== 'image' && asset?.kind !== 'video') || asset.source?.kind !== 'data') {
|
|
716
741
|
drop('image fill: asset missing or not inline data');
|
|
717
742
|
return null;
|
|
718
743
|
}
|
|
719
|
-
|
|
744
|
+
let kind = asset.kind;
|
|
745
|
+
if (kind === 'video' && !supportsVideo) {
|
|
746
|
+
if (!posters.has(p.assetId)) {
|
|
747
|
+
const header = typeof asset.poster === 'string' && /^data:(image\/[a-z0-9.+-]+);base64,/i.exec(asset.poster);
|
|
748
|
+
if (!header) {
|
|
749
|
+
drop('Video fill requires a newer editor');
|
|
750
|
+
return null;
|
|
751
|
+
}
|
|
752
|
+
posters.set(p.assetId, { id: p.assetId, kind: 'image', width: asset.width, height: asset.height,
|
|
753
|
+
mimeType: header[1], source: { kind: 'data', data: asset.poster.slice(header[0].length) } });
|
|
754
|
+
}
|
|
755
|
+
approximate('Video fill requires a newer editor, kept its poster');
|
|
756
|
+
kind = 'image';
|
|
757
|
+
}
|
|
758
|
+
return { k: kind, asset: p.assetId, fit: { contain: 0, fit: 0, cover: 1, fill: 2, stretch: 2 }[p.fit] ?? 1, o };
|
|
720
759
|
}
|
|
721
760
|
drop(`paint ${p.kind}`);
|
|
722
761
|
return null;
|
|
@@ -851,6 +890,26 @@ function expand(doc, pages, options, explicit, file, deferHyperlinks) {
|
|
|
851
890
|
if (o.layout)
|
|
852
891
|
eff.layout = { ...n.layout, ...o.layout };
|
|
853
892
|
const figma = n.metadata?.figma;
|
|
893
|
+
if (eff.layout?.kind === 'grid') {
|
|
894
|
+
const { columns, ...grid } = eff.layout;
|
|
895
|
+
if (supportsGrid)
|
|
896
|
+
eff.layout = { ...grid, columnTracks: grid.columnTracks ?? Array.from({ length: columns }, () => ({ kind: 'fill' })) };
|
|
897
|
+
else {
|
|
898
|
+
for (const field of Object.keys(grid))
|
|
899
|
+
drop(`Grid layout ${field} requires a newer editor`);
|
|
900
|
+
eff.layout = { kind: 'absolute' };
|
|
901
|
+
}
|
|
902
|
+
}
|
|
903
|
+
const cell = place ? place.gridCell : o.gridCell ? { ...figma?.gridCell, ...o.gridCell } : figma?.gridCell;
|
|
904
|
+
if (cell) {
|
|
905
|
+
if (supportsGrid)
|
|
906
|
+
eff.gridCell = cell;
|
|
907
|
+
else {
|
|
908
|
+
delete eff.gridCell;
|
|
909
|
+
for (const field of Object.keys(cell))
|
|
910
|
+
drop(`Grid child ${field} requires a newer editor`);
|
|
911
|
+
}
|
|
912
|
+
}
|
|
854
913
|
if (figma?.fillSizing)
|
|
855
914
|
eff.resizing = { ...figma.fillSizing, ...eff.resizing };
|
|
856
915
|
if (figma?.groupConstraints)
|
|
@@ -865,6 +924,11 @@ function expand(doc, pages, options, explicit, file, deferHyperlinks) {
|
|
|
865
924
|
for (const k of ['fills', 'strokes', 'shadows', 'backgroundBlur', 'layerBlur', 'cornerRadii', 'cornerRadius', 'cornerSmoothing'])
|
|
866
925
|
if (o[k] !== undefined)
|
|
867
926
|
style[k] = o[k];
|
|
927
|
+
// A .fig text layer's or group's shadows and blurs, which Taco's model keeps on shapes and frames only
|
|
928
|
+
// (metadata.figma.effects, taco patch 0049).
|
|
929
|
+
if ((n.type === 'text' || n.type === 'group') && figma?.effects)
|
|
930
|
+
for (const [k, v] of Object.entries(figma.effects))
|
|
931
|
+
style[k] ??= v;
|
|
868
932
|
if (L) {
|
|
869
933
|
// The resolver writes a bound or computed radius as one cornerRadius; a layer that stores per-corner radii keeps
|
|
870
934
|
// them as four equal ones, as the converter writes them.
|
|
@@ -895,8 +959,9 @@ function expand(doc, pages, options, explicit, file, deferHyperlinks) {
|
|
|
895
959
|
}
|
|
896
960
|
const allBindings = { ...n.variableBindings, ...o.variableBindings };
|
|
897
961
|
// A literal instance gap replaces the main's binding. A binding authored on the same override still wins.
|
|
898
|
-
|
|
899
|
-
|
|
962
|
+
for (const gap of ['gap', 'columnGap', 'rowGap'])
|
|
963
|
+
if (o.layout?.[gap] !== undefined && !o.variableBindings?.[gap])
|
|
964
|
+
delete allBindings[gap];
|
|
900
965
|
const bindings = L ? Object.fromEntries(Object.entries(allBindings).filter(([k]) => SIZE_BINDINGS.has(k) || (place && k === 'opacity'))) : allBindings;
|
|
901
966
|
for (const key of ['fillColor', 'strokeColor', 'textColor']) {
|
|
902
967
|
if (!bindings[key] || (explicit && key !== 'textColor'))
|
|
@@ -943,8 +1008,8 @@ function expand(doc, pages, options, explicit, file, deferHyperlinks) {
|
|
|
943
1008
|
eff[key] = v;
|
|
944
1009
|
else if (key === 'opacity')
|
|
945
1010
|
eff.opacity = v;
|
|
946
|
-
else if (key === 'gap')
|
|
947
|
-
eff.layout = { ...eff.layout,
|
|
1011
|
+
else if (key === 'gap' || key === 'columnGap' || key === 'rowGap')
|
|
1012
|
+
eff.layout = { ...eff.layout, [key]: v };
|
|
948
1013
|
else if (pad)
|
|
949
1014
|
eff.layout = { ...eff.layout, padding: { ...eff.layout?.padding, [pad[1].toLowerCase()]: v } };
|
|
950
1015
|
else if (key === 'cornerRadius') {
|
|
@@ -1030,7 +1095,7 @@ function expand(doc, pages, options, explicit, file, deferHyperlinks) {
|
|
|
1030
1095
|
common.visible = false;
|
|
1031
1096
|
active.id = common.id;
|
|
1032
1097
|
// The layer's export settings (an instance's own go to its expanded root, never its component's).
|
|
1033
|
-
const exports = place ? place.exports : exportsOf(eff.exportSettings);
|
|
1098
|
+
const exports = place ? place.exports : exportsOf(eff.exportSettings, drop);
|
|
1034
1099
|
if (exports)
|
|
1035
1100
|
common.exports = exports;
|
|
1036
1101
|
// The node's blend mode (an instance's applies to its expanded root, which takes this common record).
|
|
@@ -1089,7 +1154,7 @@ function expand(doc, pages, options, explicit, file, deferHyperlinks) {
|
|
|
1089
1154
|
if (eff.opacity !== undefined && (setOpacity || sameVariant || n.opacity !== storedOpacity(doc.nodes[target], stored, ctx.modes)))
|
|
1090
1155
|
inner.variantOpacity = { id: target, opacity: common.opacity };
|
|
1091
1156
|
note('instances expanded');
|
|
1092
|
-
const placed = { name: common.name, blendMode: eff.blendMode, exports, stored: root.frame, frame: { ...root.frame, ...frame, width: eff.width ?? frame.width, height: eff.height ?? frame.height, x: eff.x ?? frame.x, y: eff.y ?? frame.y }, rotation, sizing: eff.sizing, resizing: eff.resizing, layoutPositioning: eff.layoutPositioning, constraints: eff.constraints, variableModes: { ...n.variableModes, ...o.variableModes }, layer, ...(ownKeys.length ? { moved: ownKeys } : {}) };
|
|
1157
|
+
const placed = { name: common.name, blendMode: eff.blendMode, exports, stored: root.frame, frame: { ...root.frame, ...frame, width: eff.width ?? frame.width, height: eff.height ?? frame.height, x: eff.x ?? frame.x, y: eff.y ?? frame.y }, rotation, gridCell: eff.gridCell, sizing: eff.sizing, resizing: eff.resizing, layoutPositioning: eff.layoutPositioning, constraints: eff.constraints, variableModes: { ...n.variableModes, ...o.variableModes }, layer, ...(ownKeys.length ? { moved: ownKeys } : {}) };
|
|
1093
1158
|
if (ownKeys.length)
|
|
1094
1159
|
inner.moved = { id: target, style: Object.fromEntries(ownKeys.map((k) => [k, r.style?.[k]])) };
|
|
1095
1160
|
// A swapped component (an override or expression changed componentId) keeps the instance's size only when that
|
|
@@ -1341,13 +1406,18 @@ function expand(doc, pages, options, explicit, file, deferHyperlinks) {
|
|
|
1341
1406
|
if ((!place || ((place.trial || place.swapped) && k in (eff.variantSize ?? {}))) && eff[k] !== undefined)
|
|
1342
1407
|
f[k] = eff[k];
|
|
1343
1408
|
// A path follows its constraint in shapeNode, from its stored place (moved here too, it would move twice).
|
|
1344
|
-
if (kept) {
|
|
1409
|
+
if (kept && !kept.inner) {
|
|
1345
1410
|
const [a, b, c, d, x, y] = kept.transform;
|
|
1346
1411
|
const { width, height } = kept;
|
|
1347
1412
|
Object.assign(f, { width, height, x: x + ((a - 1) * width + c * height) / 2, y: y + (b * width + (d - 1) * height) / 2 });
|
|
1348
1413
|
}
|
|
1349
|
-
else
|
|
1350
|
-
|
|
1414
|
+
else {
|
|
1415
|
+
if (rs && !place && f.width !== undefined && n.shape !== 'path')
|
|
1416
|
+
follow(f, eff.constraints, rs);
|
|
1417
|
+
// A text Figma measured at its size there (`inner`) keeps that size, placed by its constraints.
|
|
1418
|
+
if (kept)
|
|
1419
|
+
Object.assign(f, { width: kept.width, height: kept.height });
|
|
1420
|
+
}
|
|
1351
1421
|
// Absolute children follow constraints; auto-layout children are positioned below.
|
|
1352
1422
|
// A size from the component's own expressions is the variant's size, which its children were placed in.
|
|
1353
1423
|
const from = place?.stored ?? { ...frame, ...eff.variantSize }, free = !eff.layout || eff.layout.kind === 'absolute';
|
|
@@ -1373,7 +1443,7 @@ function expand(doc, pages, options, explicit, file, deferHyperlinks) {
|
|
|
1373
1443
|
const node = { ...eff, sizing: { width: n.layout?.widthSizing, height: n.layout?.heightSizing },
|
|
1374
1444
|
resizing: { ...eff.resizing, ...place.resizing } };
|
|
1375
1445
|
const item = { model: m, node, children: [] };
|
|
1376
|
-
const fixed = !
|
|
1446
|
+
const fixed = (!node.layout || node.layout.kind === 'absolute') ||
|
|
1377
1447
|
(sizingOf(node, 'width') !== 'hug' && sizingOf(node, 'height') !== 'hug');
|
|
1378
1448
|
if (fixed && !L.children.some(containsInstance)) {
|
|
1379
1449
|
layoutFrame(item);
|
|
@@ -1381,7 +1451,7 @@ function expand(doc, pages, options, explicit, file, deferHyperlinks) {
|
|
|
1381
1451
|
}
|
|
1382
1452
|
}
|
|
1383
1453
|
appearance(m, style, 'frame', n.metadata?.figma);
|
|
1384
|
-
if (n.metadata?.figma?.strokesIncluded === true && (eff.layout
|
|
1454
|
+
if (n.metadata?.figma?.strokesIncluded === true && (eff.layout && eff.layout.kind !== 'absolute')) {
|
|
1385
1455
|
eff.layout = { ...eff.layout, strokesIncluded: true };
|
|
1386
1456
|
}
|
|
1387
1457
|
const r = radii(style);
|
|
@@ -1538,7 +1608,7 @@ function expand(doc, pages, options, explicit, file, deferHyperlinks) {
|
|
|
1538
1608
|
}
|
|
1539
1609
|
m.layoutData = layoutData(item.node, item.instance);
|
|
1540
1610
|
const flow = m.layoutData.layout?.kind;
|
|
1541
|
-
if (n.type === 'frame' && n.metadata?.figma?.firstOnTop === true && (flow
|
|
1611
|
+
if (n.type === 'frame' && n.metadata?.figma?.firstOnTop === true && (flow && flow !== 'absolute'))
|
|
1542
1612
|
m.firstOnTop = true;
|
|
1543
1613
|
// Figma lays out again an instance it re-hugs, nested instances and all, and stores the result: where the stored
|
|
1544
1614
|
// main-axis size is not what its children's stored sizes add up to, the nested ones were laid out anew and the
|
|
@@ -1589,7 +1659,7 @@ function expand(doc, pages, options, explicit, file, deferHyperlinks) {
|
|
|
1589
1659
|
// keeps an instance root at its placed size, so what hugging gives back is its children's extent (p75 Overview
|
|
1590
1660
|
// Cards: a 60 px stored body whose texts hug to 52 re-hugs, as Figma did).
|
|
1591
1661
|
for (const [axis, key] of [['width', 'w'], ['height', 'h']]) {
|
|
1592
|
-
if (item.node.sizing?.[axis] === undefined && item.node.resizing?.[axis] === 'fixed' && item.node.layout?.[`${axis}Sizing`] === 'hug' && Math.abs((item.instance ? hugged(item, axis) ?? m[key] : m[key]) - f[axis]) < 0.01 && !item.remeasured) {
|
|
1662
|
+
if (flow !== 'grid' && item.node.sizing?.[axis] === undefined && item.node.resizing?.[axis] === 'fixed' && item.node.layout?.[`${axis}Sizing`] === 'hug' && Math.abs((item.instance ? hugged(item, axis) ?? m[key] : m[key]) - f[axis]) < 0.01 && !item.remeasured) {
|
|
1593
1663
|
m.layoutData.sizing = { ...m.layoutData.sizing, [axis]: 'fixed' };
|
|
1594
1664
|
(item.frozen ??= []).push(axis);
|
|
1595
1665
|
}
|
|
@@ -1602,7 +1672,7 @@ function expand(doc, pages, options, explicit, file, deferHyperlinks) {
|
|
|
1602
1672
|
const main = lay?.kind === 'horizontal' ? 'width' : 'height';
|
|
1603
1673
|
const hugAxes = ['width', 'height'].filter((axis) => (item.node.sizing?.[axis] ?? lay?.[`${axis}Sizing`]) === 'hug');
|
|
1604
1674
|
const fromStart = hugAxes.every((axis) => [undefined, 'start'].includes(axis === main ? lay?.justify : lay?.align));
|
|
1605
|
-
if (!ctx.comps.length && !place && hugAxes.length && fromStart) {
|
|
1675
|
+
if (flow !== 'grid' && !ctx.comps.length && !place && hugAxes.length && fromStart) {
|
|
1606
1676
|
m.keepSize = true;
|
|
1607
1677
|
if (Math.abs(m.w - f.width) > 0.01 || Math.abs(m.h - f.height) > 0.01)
|
|
1608
1678
|
approximate('page-level hug frames laid out at other than their stored size');
|
|
@@ -1622,7 +1692,8 @@ function expand(doc, pages, options, explicit, file, deferHyperlinks) {
|
|
|
1622
1692
|
if (n.type === 'text') {
|
|
1623
1693
|
const model = textNode(n, L ? { ...eff, ...resolvedText(r, full, eff, copiedColours) } : eff, o, common, f, rotation, style, ctx.modes);
|
|
1624
1694
|
if (kept) {
|
|
1625
|
-
|
|
1695
|
+
if (!kept.inner)
|
|
1696
|
+
model.t = [...kept.transform];
|
|
1626
1697
|
model.h = kept.height;
|
|
1627
1698
|
if (model.autoResize)
|
|
1628
1699
|
model.keepSize = true;
|
|
@@ -1759,7 +1830,7 @@ function expand(doc, pages, options, explicit, file, deferHyperlinks) {
|
|
|
1759
1830
|
return o.padding !== undefined || o.layout?.padding !== undefined;
|
|
1760
1831
|
if (key === 'direction' || key === 'layout.kind')
|
|
1761
1832
|
return o.layout?.kind !== undefined;
|
|
1762
|
-
if (['gap', 'align', 'justify', 'wrap', 'wrapGap'].includes(key))
|
|
1833
|
+
if (['gap', 'columnGap', 'rowGap', 'align', 'justify', 'wrap', 'wrapGap'].includes(key))
|
|
1763
1834
|
return o.layout?.[key] !== undefined;
|
|
1764
1835
|
if (key === 'layout.widthSizing' || key === 'resizing.width')
|
|
1765
1836
|
return o.sizing?.width !== undefined;
|
|
@@ -1956,7 +2027,7 @@ function expand(doc, pages, options, explicit, file, deferHyperlinks) {
|
|
|
1956
2027
|
eff['style.cornerRadii'] = num ? { topLeft: v, topRight: v, bottomRight: v, bottomLeft: v } : v;
|
|
1957
2028
|
return;
|
|
1958
2029
|
}
|
|
1959
|
-
if (['gap', 'padding', 'align', 'justify', 'direction'].includes(key)) {
|
|
2030
|
+
if (['gap', 'columnGap', 'rowGap', 'padding', 'align', 'justify', 'direction'].includes(key)) {
|
|
1960
2031
|
// Expressions spell the stored 'space-between' as the enum case .space_between.
|
|
1961
2032
|
if (key === 'justify' && v === 'space_between')
|
|
1962
2033
|
v = 'space-between';
|
|
@@ -1990,7 +2061,7 @@ function expand(doc, pages, options, explicit, file, deferHyperlinks) {
|
|
|
1990
2061
|
? { ...p, ...linearEndpoints(p.angle ?? 180, localBox(m)) } : resolveLinearCall(p, () => localBox(m));
|
|
1991
2062
|
function fillPaint(p, tile, m, cropLayerBox, cropPaintBox) {
|
|
1992
2063
|
const converted = paint(paintInBox(p, m), tile);
|
|
1993
|
-
const crop = converted?.k === 'image' && imageCropOf(p);
|
|
2064
|
+
const crop = (converted?.k === 'image' || converted?.k === 'video') && imageCropOf(p);
|
|
1994
2065
|
if (!crop)
|
|
1995
2066
|
return converted;
|
|
1996
2067
|
const placement = imageCropPlacement(crop, cropPaintBox ?? localBox(m), cropLayerBox);
|
|
@@ -2005,7 +2076,7 @@ function expand(doc, pages, options, explicit, file, deferHyperlinks) {
|
|
|
2005
2076
|
function appearance(m, style, kind, carried = {}, cropLayerBox) {
|
|
2006
2077
|
const tiles = new Map(Array.isArray(carried.tiles) ? carried.tiles : []);
|
|
2007
2078
|
const fills = (style.fills ?? []).map((p, i) => fillPaint(p, tiles.get(i), m, cropLayerBox)).filter(Boolean), look = {};
|
|
2008
|
-
if (fills.length === 1 && fills[0].k === 'solid' && fills[0].o === 1 && fills[0].visible !== false)
|
|
2079
|
+
if (fills.length === 1 && fills[0].k === 'solid' && fills[0].o === 1 && fills[0].visible !== false && !fills[0].blend)
|
|
2009
2080
|
m.fill = fills[0].c;
|
|
2010
2081
|
else if (fills.length)
|
|
2011
2082
|
look.fills = fills;
|
|
@@ -2022,10 +2093,10 @@ function expand(doc, pages, options, explicit, file, deferHyperlinks) {
|
|
|
2022
2093
|
m.stroke = p.k === 'solid' ? p.c : [0, 0, 0, 1];
|
|
2023
2094
|
m.strokeWidth = s.width;
|
|
2024
2095
|
const st = { position: { center: 0, inside: 1, outside: 2 }[s.position] ?? 0, join: { miter: 0, bevel: 1, round: 2 }[s.join] ?? null, cap: { none: 0, round: 1, square: 2 }[s.dashCap] ?? 0 };
|
|
2025
|
-
if (p.k !== 'solid' || p.o !== 1) {
|
|
2096
|
+
if (p.k !== 'solid' || p.o !== 1 || p.blend) {
|
|
2026
2097
|
st.paint = p;
|
|
2027
2098
|
if (p.k !== 'solid')
|
|
2028
|
-
note(`sent: ${p.k}${p.k === 'image' ? '' : ' gradient'} strokes`);
|
|
2099
|
+
note(`sent: ${p.k}${p.k === 'image' || p.k === 'video' ? '' : ' gradient'} strokes`);
|
|
2029
2100
|
}
|
|
2030
2101
|
if (s.dash?.length)
|
|
2031
2102
|
st.dash = s.dash;
|
|
@@ -2065,7 +2136,7 @@ function expand(doc, pages, options, explicit, file, deferHyperlinks) {
|
|
|
2065
2136
|
look.backgroundBlur = { radius: style.backgroundBlur };
|
|
2066
2137
|
for (const p of fills)
|
|
2067
2138
|
if (p.k !== 'solid')
|
|
2068
|
-
note(`sent: ${p.k}${p.k === 'image' ? '' : ' gradient'} fills`);
|
|
2139
|
+
note(`sent: ${p.k}${p.k === 'image' || p.k === 'video' ? '' : ' gradient'} fills`);
|
|
2069
2140
|
if (shadows.length)
|
|
2070
2141
|
note('sent: nodes with shadows');
|
|
2071
2142
|
if (look.layerBlur)
|
|
@@ -2105,6 +2176,47 @@ function expand(doc, pages, options, explicit, file, deferHyperlinks) {
|
|
|
2105
2176
|
const endpoints = new Set(segments.flatMap(s => [s.a, s.b]));
|
|
2106
2177
|
return netBounds({ vertices: net.vertices.filter(v => endpoints.has(v.id)), segments });
|
|
2107
2178
|
}
|
|
2179
|
+
// A component's vector network is read once for every layer that shows it: instances and the trial layouts that size
|
|
2180
|
+
// them convert the same source many times. Converted networks are values that layout and linking replace, never edit.
|
|
2181
|
+
const sharedNets = new Map();
|
|
2182
|
+
function pathNet(vn) {
|
|
2183
|
+
const r4 = (x) => (Number.isInteger(x) ? x : +x.toFixed(4)) || 0;
|
|
2184
|
+
let at;
|
|
2185
|
+
// An elliptical arc (svg-import keeps SVG's) becomes cubics of at most 90 degrees, through new vertices; its id
|
|
2186
|
+
// stands for them all in the regions' loops (`parts`).
|
|
2187
|
+
const vertices = vn.vertices.map((v) => ({ id: v.id, x: r4(v.x), y: r4(v.y) })), segments = [], parts = new Map();
|
|
2188
|
+
let arcs = 0, lostSegments = 0;
|
|
2189
|
+
for (const s of vn.segments) {
|
|
2190
|
+
if (s.kind === 'elliptical-arc')
|
|
2191
|
+
at ??= new Map(vn.vertices.map((v) => [v.id, [v.x, v.y]]));
|
|
2192
|
+
if (s.kind === 'elliptical-arc' && at?.has(s.start) && at.has(s.end)) {
|
|
2193
|
+
const { points, handles } = arcCubics(at.get(s.start), at.get(s.end), s.radiusX, s.radiusY, s.xAxisRotation ?? 0, !!s.largeArc, !!s.sweep);
|
|
2194
|
+
if (handles.length) {
|
|
2195
|
+
arcs++;
|
|
2196
|
+
const ids = points.map((_, i) => `${s.id}~v${i + 1}`);
|
|
2197
|
+
points.forEach(([x, y], i) => vertices.push({ id: ids[i], x: r4(x), y: r4(y) }));
|
|
2198
|
+
const ends = [s.start, ...ids, s.end], own = handles.map((_, i) => (i ? `${s.id}~${i}` : s.id));
|
|
2199
|
+
handles.forEach(([ha, hb], i) => segments.push({ id: own[i], a: ends[i], b: ends[i + 1], ha: ha.map(r4), hb: hb.map(r4) }));
|
|
2200
|
+
parts.set(s.id, own);
|
|
2201
|
+
continue;
|
|
2202
|
+
}
|
|
2203
|
+
}
|
|
2204
|
+
else if (s.kind !== 'line' && s.kind !== 'cubic')
|
|
2205
|
+
lostSegments++;
|
|
2206
|
+
segments.push({ id: s.id, a: s.start, b: s.end, ha: s.kind === 'cubic' ? [s.startHandle.dx, s.startHandle.dy].map(r4) : [0, 0], hb: s.kind === 'cubic' ? [s.endHandle.dx, s.endHandle.dy].map(r4) : [0, 0] });
|
|
2207
|
+
}
|
|
2208
|
+
const seg = new Map(segments.map((s) => [s.id, s]));
|
|
2209
|
+
const regions = (vn.regions ?? []).filter((r) => r.loops.length);
|
|
2210
|
+
const outlines = regions.map((r) => ({ w: r.windingRule === 'evenodd' ? 1 : 0, loops: r.loops.map((l) => {
|
|
2211
|
+
const refs = l.segments.flatMap((x) => {
|
|
2212
|
+
const rev = x.direction === 'reverse', ids = parts.get(x.segmentId) ?? [x.segmentId];
|
|
2213
|
+
return (rev ? [...ids].reverse() : ids).map((id) => [id, rev]);
|
|
2214
|
+
});
|
|
2215
|
+
const end = ([id, rev]) => (rev ? seg.get(id).a : seg.get(id).b), start = ([id, rev]) => (rev ? seg.get(id).b : seg.get(id).a);
|
|
2216
|
+
return { refs, open: start(refs[0]) !== end(refs[refs.length - 1]) };
|
|
2217
|
+
}) }));
|
|
2218
|
+
return { vertices, segments, next: vertices.length + segments.length, regions: outlines, fills: regions.map((r) => r.fills), lostSegments, arcs };
|
|
2219
|
+
}
|
|
2108
2220
|
function shapeNode(n, eff, common, f, rotation, style, rs, kept, children) {
|
|
2109
2221
|
let net, t, box, shape;
|
|
2110
2222
|
let regionFills = [];
|
|
@@ -2118,45 +2230,17 @@ function expand(doc, pages, options, explicit, file, deferHyperlinks) {
|
|
|
2118
2230
|
return m;
|
|
2119
2231
|
}
|
|
2120
2232
|
if (n.shape === 'path') {
|
|
2121
|
-
const vn = n.vectorNetwork
|
|
2122
|
-
|
|
2123
|
-
|
|
2124
|
-
|
|
2125
|
-
|
|
2126
|
-
|
|
2127
|
-
for (const s of vn.segments) {
|
|
2128
|
-
if (s.kind === 'elliptical-arc')
|
|
2129
|
-
at ??= new Map(vn.vertices.map((v) => [v.id, [v.x, v.y]]));
|
|
2130
|
-
if (s.kind === 'elliptical-arc' && at?.has(s.start) && at.has(s.end)) {
|
|
2131
|
-
const { points, handles } = arcCubics(at.get(s.start), at.get(s.end), s.radiusX, s.radiusY, s.xAxisRotation ?? 0, !!s.largeArc, !!s.sweep);
|
|
2132
|
-
if (handles.length) {
|
|
2133
|
-
arcs++;
|
|
2134
|
-
const ids = points.map((_, i) => `${s.id}~v${i + 1}`);
|
|
2135
|
-
points.forEach(([x, y], i) => vertices.push({ id: ids[i], x: r4(x), y: r4(y) }));
|
|
2136
|
-
const ends = [s.start, ...ids, s.end], own = handles.map((_, i) => (i ? `${s.id}~${i}` : s.id));
|
|
2137
|
-
handles.forEach(([ha, hb], i) => segments.push({ id: own[i], a: ends[i], b: ends[i + 1], ha: ha.map(r4), hb: hb.map(r4) }));
|
|
2138
|
-
parts.set(s.id, own);
|
|
2139
|
-
continue;
|
|
2140
|
-
}
|
|
2141
|
-
}
|
|
2142
|
-
else if (s.kind !== 'line' && s.kind !== 'cubic')
|
|
2143
|
-
drop('path segment kind');
|
|
2144
|
-
segments.push({ id: s.id, a: s.start, b: s.end, ha: s.kind === 'cubic' ? [s.startHandle.dx, s.startHandle.dy].map(r4) : [0, 0], hb: s.kind === 'cubic' ? [s.endHandle.dx, s.endHandle.dy].map(r4) : [0, 0] });
|
|
2145
|
-
}
|
|
2146
|
-
if (arcs)
|
|
2233
|
+
const vn = n.vectorNetwork;
|
|
2234
|
+
const shared = sharedNets.get(vn) ?? pathNet(vn);
|
|
2235
|
+
sharedNets.set(vn, shared);
|
|
2236
|
+
for (let k = 0; k < shared.lostSegments; k++)
|
|
2237
|
+
drop('path segment kind');
|
|
2238
|
+
if (shared.arcs)
|
|
2147
2239
|
approximate('elliptical arcs split into cubics');
|
|
2148
|
-
|
|
2149
|
-
|
|
2150
|
-
|
|
2151
|
-
|
|
2152
|
-
const refs = l.segments.flatMap((x) => {
|
|
2153
|
-
const rev = x.direction === 'reverse', ids = parts.get(x.segmentId) ?? [x.segmentId];
|
|
2154
|
-
return (rev ? [...ids].reverse() : ids).map((id) => [id, rev]);
|
|
2155
|
-
});
|
|
2156
|
-
const end = ([id, rev]) => (rev ? seg.get(id).a : seg.get(id).b), start = ([id, rev]) => (rev ? seg.get(id).b : seg.get(id).a);
|
|
2157
|
-
return { refs, open: start(refs[0]) !== end(refs[refs.length - 1]) };
|
|
2158
|
-
}) }));
|
|
2159
|
-
regionFills = regions.map((r) => r.fills);
|
|
2240
|
+
// Each model owns its network and region records (region fills are its own); the geometry is shared.
|
|
2241
|
+
net = { vertices: shared.vertices, segments: shared.segments, loops: [], next: shared.next,
|
|
2242
|
+
regions: shared.regions.map(({ w, loops }) => ({ w, loops })) };
|
|
2243
|
+
regionFills = shared.fills;
|
|
2160
2244
|
t = (kept?.transform ?? eff.exprTransform ?? n.transform ?? [1, 0, 0, 1, f.x ?? 0, f.y ?? 0]).map((v) => +v.toFixed(6));
|
|
2161
2245
|
if (kept?.pathScale) {
|
|
2162
2246
|
let [sx, sy] = kept.pathScale;
|
|
@@ -2380,7 +2464,7 @@ function expand(doc, pages, options, explicit, file, deferHyperlinks) {
|
|
|
2380
2464
|
// node's expressions.
|
|
2381
2465
|
const weights = new Map();
|
|
2382
2466
|
// What sizes a run's glyphs (not its colour or decoration).
|
|
2383
|
-
const faceOf = (s) => JSON.stringify([s.fontFamily, s.fontStyle, s.fontVariations, s.openTypeFeatures, s.fontSize ?? 16, s.fontWeight ?? 400, !!s.italic, s.lineHeight ?? 1.2, s.letterSpacing ?? 0]);
|
|
2467
|
+
const faceOf = (s) => JSON.stringify([s.fontFamily, s.fontStyle, s.fontVariations, s.openTypeFeatures, s.textCase === 'original' ? undefined : s.textCase, s.fontSize ?? 16, s.fontWeight ?? 400, !!s.italic, s.lineHeight ?? 1.2, s.letterSpacing ?? 0]);
|
|
2384
2468
|
const runColour = (run) => ({ ...doc.theme?.textStyles?.[run.styleId], ...run.overrides }).color;
|
|
2385
2469
|
const hexOf = (c) => (typeof c === 'string' ? c : c?.hex)?.toLowerCase();
|
|
2386
2470
|
// A text's paragraphs: stored, or set as characters (each line one paragraph in the first run's style).
|
|
@@ -2509,8 +2593,10 @@ function expand(doc, pages, options, explicit, file, deferHyperlinks) {
|
|
|
2509
2593
|
}
|
|
2510
2594
|
if (explicit && r.styleId && doc.theme?.textStyles?.[r.styleId])
|
|
2511
2595
|
Object.assign(want, { textStyle: r.styleId });
|
|
2512
|
-
if (
|
|
2513
|
-
Object.assign(want, {
|
|
2596
|
+
if (file && r.fileTypography) {
|
|
2597
|
+
Object.assign(want, { fileTypography: r.fileTypography, ...(r.fileOwnTypography && { fileOwnTypography: r.fileOwnTypography }) });
|
|
2598
|
+
applyFileTypography(want, r.fileTypography, !!r.filePercentTracking, id => resolveVariable(id, modes, doc.variableAliases));
|
|
2599
|
+
}
|
|
2514
2600
|
if (file && (r.fileTextColor !== undefined || r.filePlainTextColor !== undefined)) {
|
|
2515
2601
|
const field = r.filePlainTextColor !== undefined ? 'filePlainTextColor' : 'fileTextColor';
|
|
2516
2602
|
const ref = r[field];
|
|
@@ -2554,7 +2640,8 @@ function expand(doc, pages, options, explicit, file, deferHyperlinks) {
|
|
|
2554
2640
|
let h = f.height, storedHeight;
|
|
2555
2641
|
const first = (eff.paragraphs ?? n.paragraphs)?.[0]?.runs?.[0], stored = n.paragraphs?.[0]?.runs?.[0];
|
|
2556
2642
|
if (first && stored && styles.length && ['height', 'width-and-height'].includes(eff.autoResize)) {
|
|
2557
|
-
const base = { ...doc.theme?.textStyles?.[stored.styleId], ...stored.overrides }
|
|
2643
|
+
const base = { ...doc.theme?.textStyles?.[stored.styleId], ...stored.overrides };
|
|
2644
|
+
const was = stored.fileLineHeightPx ?? (base.fontSize ?? 16) * (base.lineHeight ?? 1.2);
|
|
2558
2645
|
const now = boxLine || Math.max(...styles.map((x) => x.lineHeight));
|
|
2559
2646
|
// Changed text: estimate its line count from an average Inter advance (0.55 em), as no shaper runs here.
|
|
2560
2647
|
const storedText = (n.paragraphs ?? []).map((p) => (p.runs ?? []).map((r) => r.text ?? '').join('')).join('\n'), text = lines.join('\n');
|
|
@@ -2604,6 +2691,7 @@ function expand(doc, pages, options, explicit, file, deferHyperlinks) {
|
|
|
2604
2691
|
s.lineHeight = +(s.size * trimmed).toFixed(3);
|
|
2605
2692
|
approximate('leading trim (cap height) taken from a box at the cap height');
|
|
2606
2693
|
}
|
|
2694
|
+
appearance(m, { shadows: style.shadows, layerBlur: style.layerBlur, backgroundBlur: style.backgroundBlur }, 'text', n.metadata?.figma);
|
|
2607
2695
|
models.push(m);
|
|
2608
2696
|
return m;
|
|
2609
2697
|
}
|
|
@@ -2715,7 +2803,7 @@ function expand(doc, pages, options, explicit, file, deferHyperlinks) {
|
|
|
2715
2803
|
}
|
|
2716
2804
|
// Image assets the models use, listed in out.assets (and in the report, with their bytes).
|
|
2717
2805
|
const assets = {}, used = new Set(), files = [];
|
|
2718
|
-
const collectImage = (p) => { if (p?.k === 'image')
|
|
2806
|
+
const collectImage = (p) => { if (p?.k === 'image' || p?.k === 'video')
|
|
2719
2807
|
used.add(p.asset); };
|
|
2720
2808
|
for (const m of models) {
|
|
2721
2809
|
for (const p of m.look?.fills ?? [])
|
|
@@ -2726,9 +2814,11 @@ function expand(doc, pages, options, explicit, file, deferHyperlinks) {
|
|
|
2726
2814
|
collectImage(p);
|
|
2727
2815
|
}
|
|
2728
2816
|
for (const id of [...used].sort()) {
|
|
2729
|
-
const a =
|
|
2817
|
+
const a = posters.get(id) ?? doc.assets[id];
|
|
2818
|
+
const src = options.assetSrc?.(id, a) ?? `assets/${id}.${a.mimeType === 'image/jpeg' ? 'jpg' : a.mimeType.split('/')[1]}`;
|
|
2730
2819
|
files.push({ id, src, mimeType: a.mimeType, data: a.source.data });
|
|
2731
|
-
assets[id] = { src, w: a.width, h: a.height
|
|
2820
|
+
assets[id] = { src, w: a.width, h: a.height, ...(a.kind === 'video' && { mediaType: 'video',
|
|
2821
|
+
...(a.poster && { poster: a.poster }), ...(a.duration !== undefined && { duration: a.duration }) }) };
|
|
2732
2822
|
}
|
|
2733
2823
|
const out = { leading: 'half', ...(!explicit && { page: pages[0].name }), ...(options.source !== undefined ? { source: options.source } : {}), models, ...(used.size ? { assets } : {}) };
|
|
2734
2824
|
return { out, affected, nestedBooleans, counts: { dropped, expressionErrors, notes, approximations, weights }, depth: depthMax, files };
|
|
@@ -2737,14 +2827,30 @@ function convert(doc, pages, options, explicit, file, deferHyperlinks = false) {
|
|
|
2737
2827
|
// Drop the resolver, trial layouts and pre-layout geometry before indexing linked layers.
|
|
2738
2828
|
const { out, affected, nestedBooleans, counts, depth, files } = expand(doc, pages, options, explicit, file, deferHyperlinks);
|
|
2739
2829
|
const models = out.models;
|
|
2740
|
-
|
|
2830
|
+
// A text's expression counts where the file's components bind it (file-components.ts).
|
|
2831
|
+
let owners;
|
|
2832
|
+
const binding = (id, field, main) => {
|
|
2833
|
+
const expression = doc.nodes[id]?.expressions?.[field];
|
|
2834
|
+
if (field !== 'characters')
|
|
2835
|
+
return expression;
|
|
2836
|
+
owners ??= layerComponents(Object.values(doc.components), (id) => doc.nodes[id]?.children);
|
|
2837
|
+
return boundTextExpression(expression, owners.get(id), main);
|
|
2838
|
+
};
|
|
2839
|
+
const linked = options.link === false ? null : linkWithLayers(out, true, file && binding, !!file);
|
|
2741
2840
|
if (linked && !explicit) {
|
|
2742
|
-
// Clipboard mains synthesized from expansions get their presets from the source definition.
|
|
2841
|
+
// Clipboard mains synthesized from expansions get their presets from the source definition. A variant set's main
|
|
2842
|
+
// is the set's root showing one variant, a frame of the root's: the main is that variant, so it takes the
|
|
2843
|
+
// variant's presets and the frame keeps none.
|
|
2844
|
+
const lost = (what) => counts.dropped.set(what, (counts.dropped.get(what) ?? 0) + 1);
|
|
2743
2845
|
for (const main of out.models) {
|
|
2744
2846
|
if (main.kind !== 'frame' || main.parent !== '@components' || main.component !== main.id)
|
|
2745
2847
|
continue;
|
|
2746
2848
|
const source = doc.nodes[main.sourceId ?? ''];
|
|
2747
|
-
const
|
|
2849
|
+
const set = source !== undefined && Object.values(doc.components).some(c => c.rootId === source.id && c.properties?.some((p) => p.kind === 'enum'));
|
|
2850
|
+
const variant = set ? out.models.find(m => m.parent === main.id && (source.children ?? []).includes(m.sourceId ?? '')) : undefined;
|
|
2851
|
+
const exports = variant ? variant.exports : exportsOf(source?.exportSettings, lost);
|
|
2852
|
+
if (variant)
|
|
2853
|
+
delete variant.exports;
|
|
2748
2854
|
if (exports)
|
|
2749
2855
|
main.exports = exports;
|
|
2750
2856
|
}
|
|
@@ -2764,6 +2870,7 @@ function convert(doc, pages, options, explicit, file, deferHyperlinks = false) {
|
|
|
2764
2870
|
}
|
|
2765
2871
|
if (file && linked) {
|
|
2766
2872
|
file.omittedLinks = linked.omittedLinks;
|
|
2873
|
+
file.expandedReasons = linked.expandedReasons;
|
|
2767
2874
|
const { expanded } = file;
|
|
2768
2875
|
// Only appearance and text are needed after linking. The resource stage may replace
|
|
2769
2876
|
// paints on a surviving layer, so retain the pre-link look and stroke containers.
|
|
@@ -3,10 +3,32 @@ import type { DocumentJSON } from '@moondraft/editor/schema';
|
|
|
3
3
|
import type { FigmaTree, FigmaTreeNode } from '../taco/figma-import/import/tree.ts';
|
|
4
4
|
import type { readFileMessage } from '../taco/figma-import/import/walk.ts';
|
|
5
5
|
import type { FigmaImportResult } from '../taco/figma-import/index.ts';
|
|
6
|
+
declare const feature: {
|
|
7
|
+
readonly annotations: 'developer annotations are not imported';
|
|
8
|
+
readonly measurements: 'developer measurements are not imported';
|
|
9
|
+
readonly sectionStatus: 'section status is not imported';
|
|
10
|
+
readonly sectionStatusInfo: 'section status is not imported';
|
|
11
|
+
readonly devStatus: 'section status is not imported';
|
|
12
|
+
};
|
|
13
|
+
type Field = keyof typeof feature;
|
|
14
|
+
/**
|
|
15
|
+
* Finds the instances whose overrides hold active annotations or measurements. Read it before the instances' override
|
|
16
|
+
* data is released.
|
|
17
|
+
* @param tree - The reader's Figma tree.
|
|
18
|
+
* @returns The handoff fields set by each such instance's overrides, by Figma ID.
|
|
19
|
+
*/
|
|
20
|
+
export declare function handoffOverrides(tree: FigmaTree): Map<string, readonly Field[]>;
|
|
6
21
|
/** Keeps handoff-bearing clipboard layers distinct when merging variant trees. */
|
|
7
22
|
export declare function handoffSources(tree: FigmaTree): ReadonlySet<string>;
|
|
8
|
-
/**
|
|
9
|
-
|
|
23
|
+
/**
|
|
24
|
+
* Installs Figma handoff metadata on the layers that survived conversion and reports any omitted fields: those of
|
|
25
|
+
* layers left out (`leftOut`, named in the report) and the handoff that instances set in overrides (`overrides`), which
|
|
26
|
+
* is not kept.
|
|
27
|
+
*/
|
|
28
|
+
export declare function figmaHandoff(result: FigmaImportResult, sources: ReturnType<typeof readFileMessage>['sources'], doc: DocumentJSON, options: {
|
|
10
29
|
instanceSources?: ReadonlyMap<string, string>;
|
|
11
30
|
setSources?: ReadonlyMap<string, FigmaTreeNode>;
|
|
31
|
+
leftOut: ReadonlySet<string>;
|
|
32
|
+
overrides: ReadonlyMap<string, readonly Field[]>;
|
|
12
33
|
}): ImportReportEntry[];
|
|
34
|
+
export {};
|