Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix layers #40

Merged
merged 3 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions lib/backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,15 @@ figma.ui.onmessage = (msg) => {
render(
msg.bbox.split(",").map((b: string) => parseFloat(b)),
{ overlays: msg.overlays }
).catch((e) => {
progress(e.message, { error: true });
});
)
.catch((e) => {
progress(e.message, { error: true });
})
.finally(() => {
figma.ui.postMessage({
type: "loaded",
});
});
}
}
};
Expand Down Expand Up @@ -271,6 +277,7 @@ async function render(bbox: BBOX, options: Options = {}) {
for (const feature of features) {
drawn++;
progress(`Drawing (${drawn} / ${features.length} elements)`);
await new Promise<void>((resolve) => resolve());

const name = feature.properties?.name;
switch (feature.geometry.type) {
Expand Down Expand Up @@ -432,6 +439,7 @@ async function render(bbox: BBOX, options: Options = {}) {
const features = grouped.get(group);
if (!features) continue;
for (let feature of features) {
await new Promise<void>((resolve) => resolve());
const name = feature.properties?.name;
if (!(feature.geometry.type === "LineString" && name)) continue;
if (labeledNames.has(name) || name.length > MAX_NAME_LENGTH) continue;
Expand Down Expand Up @@ -549,6 +557,7 @@ async function render(bbox: BBOX, options: Options = {}) {
const features = grouped.get(group);
if (!features) continue;
for (let feature of features) {
await new Promise<void>((resolve) => resolve());
const name = feature.properties?.name;
if (!(feature.geometry.type === "Polygon" && name)) continue;
if (labeledNames.has(name) || name.length > MAX_NAME_LENGTH) continue;
Expand Down
25 changes: 23 additions & 2 deletions lib/ui.css
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ label div {
padding-bottom: 5px;
}

input[type=text] {
input[type="text"] {
padding: 5px;
border: 1px solid var(--figma-color-border, #777);
border-radius: 4px;
Expand Down Expand Up @@ -87,7 +87,7 @@ p {

#controls #message {
flex: auto;
text-align:center;
text-align: center;
text-overflow: truncate;
font-size: 14px;
}
Expand All @@ -99,3 +99,24 @@ p {
#controls {
padding-top: 10px;
}

.loading {
animation: pulse 1s infinite;
}

@keyframes pulse {
0% {
transform: scale(0.95);
box-shadow: 0 0 0 0 rgba(0, 0, 0, 0.7);
}

70% {
transform: scale(1);
box-shadow: 0 0 0 5px rgba(0, 0, 0, 0);
}

100% {
transform: scale(0.95);
box-shadow: 0 0 0 0 rgba(0, 0, 0, 0);
}
}
14 changes: 12 additions & 2 deletions lib/ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const leafletMap = L.map(mapElement, {
zoomControl: false,
}).setView({ lat: 37.500258, lng: -77.49663 }, 15);

const layersControl = L.control.layers({}, {}).addTo(leafletMap);
const layersControl = L.control.layers({}, {});

L.tileLayer("https://tile.openstreetmap.org/{z}/{x}/{y}.png", {
attribution:
Expand Down Expand Up @@ -53,6 +53,10 @@ leafletMap.on("moveend", () => {

addEventListener("message", (evt) => {
switch (evt.data?.pluginMessage?.type) {
case "loaded": {
captureButton.classList.remove("loading");
break;
}
case "ratio": {
const { width, height } = evt.data?.pluginMessage || {};
setRatio(width, height);
Expand Down Expand Up @@ -115,6 +119,8 @@ document.getElementById("add-overlay")!.onclick = () => {
(layer as any).name = file.name;
layer.addTo(leafletMap);
layersControl.addOverlay(layer, file.name);
// This is hopefully idempotent
layersControl.addTo(leafletMap);
});
})
.catch((e) => {
Expand All @@ -123,7 +129,11 @@ document.getElementById("add-overlay")!.onclick = () => {
});
};

document.getElementById("capture")!.onclick = () => {
const captureButton = document.getElementById("capture")!;

captureButton.onclick = () => {
captureButton.classList.add("loading");

let overlays: any[] = [];

leafletMap.eachLayer((layer) => {
Expand Down