Initial commit with Sveltekit + format files

This commit is contained in:
pheralb
2025-08-21 10:26:07 +01:00
parent ca4f397e0a
commit 459457a7e1
97 changed files with 3892 additions and 9893 deletions
+18 -15
View File
@@ -1,37 +1,40 @@
declare const SITE_URL: string
declare const SITE_URL: string;
figma.showUI(`<script>window.location.href = '${SITE_URL}'</script>`, {
width: 400,
height: 700,
})
});
figma.ui.onmessage = async (message, props) => {
if (!SITE_URL.includes(props.origin)) {
return
return;
}
switch (message.type) {
case 'EVAL': {
const fn = eval.call(null, message.code)
case "EVAL": {
const fn = eval.call(null, message.code);
try {
const result = await fn(figma, message.params)
const result = await fn(figma, message.params);
figma.ui.postMessage({
type: 'EVAL_RESULT',
type: "EVAL_RESULT",
result,
id: message.id,
})
});
} catch (e) {
figma.ui.postMessage({
type: 'EVAL_REJECT',
error: typeof e === 'string' ? e : e && typeof e === 'object' && 'message' in e ? e.message : null,
type: "EVAL_REJECT",
error:
typeof e === "string"
? e
: e && typeof e === "object" && "message" in e
? e.message
: null,
id: message.id,
})
});
}
break
break;
}
}
}
};
+3 -3
View File
@@ -7,12 +7,12 @@ export function copyToClipboard(value: string) {
// @ts-ignore
window.copy(value);
} else {
const area = document.createElement('textarea');
const area = document.createElement("textarea");
document.body.appendChild(area);
area.value = value;
// area.focus();
area.select();
const result = document.execCommand('copy');
const result = document.execCommand("copy");
document.body.removeChild(area);
if (!result) {
throw new Error();
@@ -23,4 +23,4 @@ export function copyToClipboard(value: string) {
return false;
}
return true;
}
}
+37 -23
View File
@@ -23,7 +23,7 @@
* ```
*/
class FigmaAPI {
private id = 0
private id = 0;
/**
* Run a function in the Figma plugin context. The function cannot reference
@@ -31,50 +31,64 @@ class FigmaAPI {
* serializable. If you need to pass in variables, you can do so by passing
* them as the second parameter.
*/
run<T, U>(fn: (figma: PluginAPI, params: U) => Promise<T> | T, params?: U): Promise<T> {
run<T, U>(
fn: (figma: PluginAPI, params: U) => Promise<T> | T,
params?: U,
): Promise<T> {
return new Promise((resolve, reject) => {
const id = this.id++
const id = this.id++;
const cb = (event: MessageEvent) => {
if (event.origin !== 'https://www.figma.com' && event.origin !== 'https://staging.figma.com') {
return
if (
event.origin !== "https://www.figma.com" &&
event.origin !== "https://staging.figma.com"
) {
return;
}
if (event.data.pluginMessage?.type === 'EVAL_RESULT') {
if (event.data.pluginMessage?.type === "EVAL_RESULT") {
if (event.data.pluginMessage.id === id) {
window.removeEventListener('message', cb)
resolve(event.data.pluginMessage.result)
window.removeEventListener("message", cb);
resolve(event.data.pluginMessage.result);
}
}
if (event.data.pluginMessage?.type === 'EVAL_REJECT') {
if (event.data.pluginMessage?.type === "EVAL_REJECT") {
if (event.data.pluginMessage.id === id) {
window.removeEventListener('message', cb)
const message = event.data.pluginMessage.error
reject(new Error(typeof message === 'string' ? message : 'An error occurred in FigmaAPI.run()'))
window.removeEventListener("message", cb);
const message = event.data.pluginMessage.error;
reject(
new Error(
typeof message === "string"
? message
: "An error occurred in FigmaAPI.run()",
),
);
}
}
}
window.addEventListener('message', cb)
};
window.addEventListener("message", cb);
const msg = {
pluginMessage: {
type: 'EVAL',
type: "EVAL",
code: fn.toString(),
id,
params,
},
pluginId: '*',
}
pluginId: "*",
};
;['https://www.figma.com', 'https://staging.figma.com'].forEach((origin) => {
["https://www.figma.com", "https://staging.figma.com"].forEach(
(origin) => {
try {
parent.postMessage(msg, origin)
parent.postMessage(msg, origin);
} catch (e) {
console.error(e)
console.error(e);
}
})
})
},
);
});
}
}
export const figmaAPI = new FigmaAPI()
export const figmaAPI = new FigmaAPI();
+10 -10
View File
@@ -1,22 +1,22 @@
import { figmaAPI } from './figma-api'
import { figmaAPI } from "./figma-api";
export async function insertSVG(svgString: string) {
if (!svgString) return
if (!svgString) return;
figmaAPI.run(
async (figma, { svgString }: { svgString: string }) => {
const node = figma.createNodeFromSvg(svgString)
const selectedNode = figma.currentPage.selection[0]
const node = figma.createNodeFromSvg(svgString);
const selectedNode = figma.currentPage.selection[0];
if (selectedNode) {
node.x = selectedNode.x + selectedNode.width + 20
node.y = selectedNode.y
node.x = selectedNode.x + selectedNode.width + 20;
node.y = selectedNode.y;
}
figma.currentPage.appendChild(node)
figma.currentPage.selection = [node]
figma.viewport.scrollAndZoomIntoView([node])
figma.currentPage.appendChild(node);
figma.currentPage.selection = [node];
figma.viewport.scrollAndZoomIntoView([node]);
},
{ svgString },
)
);
}