Skip to content

Commit 7456e72

Browse files
authored
Extracted placeholder image url to reduce duplicated code. (#176)
* Extracted placeholder image url to reduce duplicated code. * refactor to how images are built in HTML builder * linter fix
1 parent ff72476 commit 7456e72

File tree

6 files changed

+28
-39
lines changed

6 files changed

+28
-39
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export const PLACEHOLDER_IMAGE_DOMAIN = "https://placehold.co";
2+
3+
export const getPlaceholderImage = (w: number, h = -1) => {
4+
const _w = w.toFixed(0);
5+
const _h = (h < 0 ? w : h).toFixed(0);
6+
return `${PLACEHOLDER_IMAGE_DOMAIN}/${_w}x${_h}`;
7+
};

packages/backend/src/flutter/builderImpl/flutterColor.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
} from "../../common/numToAutoFixed";
77
import { retrieveTopFill } from "../../common/retrieveFill";
88
import { nearestValue } from "../../tailwind/conversionTables";
9+
import { getPlaceholderImage } from "../../common/images";
910

1011
/**
1112
* Retrieve the SOLID color for Flutter when existent, otherwise ""
@@ -56,9 +57,7 @@ export const flutterBoxDecorationColor = (
5657
export const flutterDecorationImage = (node: SceneNode, fill: ImagePaint) => {
5758
addWarning("Image fills are replaced with placeholders");
5859
return generateWidgetCode("DecorationImage", {
59-
image: `NetworkImage("https://placehold.co/${node.width.toFixed(
60-
0,
61-
)}x${node.height.toFixed(0)}")`,
60+
image: `NetworkImage("${getPlaceholderImage(node.width, node.height)}")`,
6261
fit: fitToBoxFit(fill),
6362
});
6463
};

packages/backend/src/flutter/flutterMain.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
import { commonSortChildrenWhenInferredAutoLayout } from "../common/commonChildrenOrder";
1515
import { PluginSettings } from "types";
1616
import { addWarning } from "../common/commonConversionWarnings";
17+
import { getPlaceholderImage } from "../common/images";
1718

1819
let localSettings: PluginSettings;
1920
let previousExecutionCache: string[];
@@ -148,9 +149,7 @@ const flutterContainer = (node: SceneNode, child: string): string => {
148149
let image = "";
149150
if ("fills" in node && retrieveTopFill(node.fills)?.type === "IMAGE") {
150151
addWarning("Image fills are replaced with placeholders");
151-
image = `Image.network("https://placehold.co/${node.width.toFixed(
152-
0,
153-
)}x${node.height.toFixed(0)}")`;
152+
image = `Image.network("${getPlaceholderImage(node.width, node.height)}")`;
154153
}
155154

156155
if (child.length > 0) {

packages/backend/src/html/htmlMain.ts

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { addWarning } from "../common/commonConversionWarnings";
99
import { PluginSettings, HTMLPreview, AltNode, HTMLSettings } from "types";
1010
import { renderAndAttachSVG } from "../altNodes/altNodeUtils";
1111
import { getVisibleNodes } from "../common/nodeVisibility";
12+
import { getPlaceholderImage } from "../common/images";
1213

1314
const selfClosingTags = ["img"];
1415

@@ -211,7 +212,10 @@ const htmlFrame = async (
211212
}
212213
};
213214

214-
const htmlAsset = (node: SceneNode, settings: HTMLSettings): string => {
215+
const htmlAsset = async (
216+
node: SceneNode,
217+
settings: HTMLSettings,
218+
): Promise<string> => {
215219
if (!("opacity" in node) || !("layoutAlign" in node) || !("fills" in node)) {
216220
return "";
217221
}
@@ -220,21 +224,13 @@ const htmlAsset = (node: SceneNode, settings: HTMLSettings): string => {
220224
.commonPositionStyles()
221225
.commonShapeStyles();
222226

223-
let tag = "div";
224-
let src = "";
225227
if (retrieveTopFill(node.fills)?.type === "IMAGE") {
226228
addWarning("Image fills are replaced with placeholders");
227-
tag = "img";
228-
src = ` src="https://placehold.co/$${node.width.toFixed(
229-
0,
230-
)}x${node.height.toFixed(0)}"`;
229+
const imgUrl = getPlaceholderImage(node.width, node.height);
230+
return `\n<img${builder.build()} src="${imgUrl}" />`;
231231
}
232232

233-
if (tag === "div") {
234-
return `\n<div${builder.build()}${src}></div>`;
235-
}
236-
237-
return `\n<${tag}${builder.build()}${src} />`;
233+
return `\n<div${builder.build()}></div>`;
238234
};
239235

240236
// properties named propSomething always take care of ","
@@ -266,20 +262,13 @@ const htmlContainer = (
266262
let src = "";
267263
if (retrieveTopFill(node.fills)?.type === "IMAGE") {
268264
addWarning("Image fills are replaced with placeholders");
265+
const imageURL = getPlaceholderImage(node.width, node.height);
269266
if (!("children" in node) || node.children.length === 0) {
270267
tag = "img";
271-
src = ` src="https://placehold.co/${node.width.toFixed(
272-
0,
273-
)}x${node.height.toFixed(0)}"`;
268+
src = ` src="${imageURL}"`;
274269
} else {
275270
builder.addStyles(
276-
formatWithJSX(
277-
"background-image",
278-
settings.jsx,
279-
`url(https://placehold.co/${node.width.toFixed(
280-
0,
281-
)}x${node.height.toFixed(0)})`,
282-
),
271+
formatWithJSX("background-image", settings.jsx, `url(${imageURL})`),
283272
);
284273
}
285274
}

packages/backend/src/swiftui/builderImpl/swiftuiColor.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { gradientAngle } from "../../common/color";
33
import { nearestValue } from "../../tailwind/conversionTables";
44
import { numberToFixedString } from "../../common/numToAutoFixed";
55
import { addWarning } from "../../common/commonConversionWarnings";
6+
import { getPlaceholderImage } from "../../common/images";
67

78
export const swiftUISolidColor = (fill: Paint): string => {
89
if (fill && fill.type === "SOLID") {
@@ -60,9 +61,7 @@ export const swiftuiBackground = (
6061
return swiftuiGradient(fill);
6162
} else if (fill?.type === "IMAGE") {
6263
addWarning("Image fills are replaced with placeholders");
63-
return `AsyncImage(url: URL(string: "https://placehold.co/${node.width.toFixed(
64-
0,
65-
)}x${node.height.toFixed(0)}"))`;
64+
return `AsyncImage(url: URL(string: "${getPlaceholderImage(node.width, node.height)}"))`;
6665
}
6766

6867
return "";

packages/backend/src/tailwind/tailwindMain.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { AltNode, PluginSettings, TailwindSettings } from "types";
88
import { addWarning } from "../common/commonConversionWarnings";
99
import { renderAndAttachSVG } from "../altNodes/altNodeUtils";
1010
import { getVisibleNodes } from "../common/nodeVisibility";
11+
import { getPlaceholderImage } from "../common/images";
1112

1213
export let localTailwindSettings: PluginSettings;
1314

@@ -232,17 +233,12 @@ export const tailwindContainer = (
232233
let src = "";
233234
if (retrieveTopFill(node.fills)?.type === "IMAGE") {
234235
addWarning("Image fills are replaced with placeholders");
236+
const imageURL = getPlaceholderImage(node.width, node.height);
235237
if (!("children" in node) || node.children.length === 0) {
236238
tag = "img";
237-
src = ` src="https://placehold.co/${node.width.toFixed(
238-
0,
239-
)}x${node.height.toFixed(0)}"`;
239+
src = ` src="${imageURL}"`;
240240
} else {
241-
builder.addAttributes(
242-
`bg-[url(https://placehold.co/${node.width.toFixed(
243-
0,
244-
)}x${node.height.toFixed(0)})]`,
245-
);
241+
builder.addAttributes(`bg-[url(${imageURL})]`);
246242
}
247243
}
248244

0 commit comments

Comments
 (0)