Skip to content

Commit 7c2fc3e

Browse files
committed
Fix type issue by branding unknowns, enabling fill-translate
1 parent c6cf01a commit 7c2fc3e

5 files changed

Lines changed: 29 additions & 13 deletions

File tree

src/renderer/layers/fill.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ export const render = (
2626
1,
2727
);
2828

29-
// resolver.resolve(
30-
// feature.paint?.["fill-translate"] ?? layer.paint?.["fill-translate"],
31-
// (value) =>
32-
// path.setAttribute("transform", `translate(${value.x} ${value.y})`),
33-
// );
29+
resolver.resolve(
30+
feature.paint?.["fill-translate"] ?? layer.paint?.["fill-translate"],
31+
(value) =>
32+
path.setAttribute("transform", `translate(${value[0]} ${value[1]})`),
33+
);
3434

3535
// TODO: move to parent styles
3636
path.setAttribute("stroke", "none");

src/style/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as fill from "./layers/fill.js";
44
import * as line from "./layers/line.js";
55
import type * as schema from "./schema.js";
66

7-
export const style = (spec: z.input<typeof schema.style>): Style => {
7+
export const style = (spec: z.output<typeof schema.style>): Style => {
88
return {
99
background: getBackground(spec),
1010
layers: spec.layers.map((layer) => ({ name: layer.id })),
@@ -55,7 +55,7 @@ export const style = (spec: z.input<typeof schema.style>): Style => {
5555
};
5656
};
5757

58-
const getBackground = (spec: z.input<typeof schema.style>): string | null => {
58+
const getBackground = (spec: z.output<typeof schema.style>): string | null => {
5959
const background = spec.layers.find((layer) => layer.type === "background")
6060
?.paint?.["background-color"];
6161
if (typeof background === "string") {

src/style/layers/fill.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,34 @@ export const prepare = (
2727
features: [{ geometry }],
2828
paint: {
2929
"fill-color": color(layer),
30-
"fill-translate": undefined,
30+
"fill-translate": translate(layer),
3131
"fill-opacity": undefined,
3232
},
3333
layout: {},
3434
};
3535
};
3636

3737
const color = (
38-
layer: z.input<typeof schema.fillLayer>,
38+
layer: z.output<typeof schema.fillLayer>,
3939
): PreparedFeatureValue<string> => {
4040
if (typeof layer.paint?.["fill-color"] !== "string") {
4141
return undefined;
4242
}
4343
return { type: "constant", value: layer.paint["fill-color"] };
4444
};
45+
46+
const translate = (
47+
layer: z.output<typeof schema.fillLayer>,
48+
): PreparedFeatureValue<[number, number]> => {
49+
if (!Array.isArray(layer.paint?.["fill-translate"])) {
50+
return undefined;
51+
}
52+
const [x, y] = layer.paint["fill-translate"];
53+
if (typeof x !== "number" || typeof y !== "number") {
54+
return undefined;
55+
}
56+
return {
57+
type: "constant",
58+
value: [x, y],
59+
};
60+
};

src/style/layers/line.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export const prepare = (
3636
};
3737

3838
const color = (
39-
layer: z.input<typeof schema.lineLayer>,
39+
layer: z.output<typeof schema.lineLayer>,
4040
): PreparedFeatureValue<string> => {
4141
if (typeof layer.paint?.["line-color"] !== "string") {
4242
return undefined;
@@ -45,7 +45,7 @@ const color = (
4545
};
4646

4747
const width = (
48-
layer: z.input<typeof schema.lineLayer>,
48+
layer: z.output<typeof schema.lineLayer>,
4949
): PreparedFeatureValue<number> => {
5050
if (layer.paint?.["line-width"] === undefined) return undefined;
5151
if (typeof layer.paint?.["line-width"] === "number") {

src/style/schema.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ export const fn = z.union([
55
z.object({ stops: z.array(z.tuple([z.number(), z.number()])) }),
66
]);
77

8-
export const filter = z.unknown();
8+
export const filter = z.unknown().brand<"Filter">();
99

10-
export const expression = z.array(z.unknown());
10+
export const expression = z.array(z.unknown()).brand<"Expression">();
1111

1212
export const vectorSource = z
1313
.object({

0 commit comments

Comments
 (0)