Skip to content

Instantly share code, notes, and snippets.

@romannurik
Last active February 24, 2022 16:08
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save romannurik/1a9bdc4132b69c4a274ab2c8f5a8de96 to your computer and use it in GitHub Desktop.
Save romannurik/1a9bdc4132b69c4a274ab2c8f5a8de96 to your computer and use it in GitHub Desktop.
Figma + Scripter scripts
// Makes a copy of all selected components (how is this not built into Figma?)
let newSelection = [];
for (let component of figma.currentPage.selection.filter(node => node.type == 'COMPONENT')) {
let clone = component.clone()
clone.x += clone.width;
newSelection.push(clone);
}
figma.currentPage.selection = newSelection;
// Moved to Artboard Tricks plugin
// A Figma way of doing Paste and Swap ::shrug::
// Ideally this would use the actual clipboard, but
// Figma doesn't give access to that yet.
(async() => {
const COPY_ID_KEY = 'copied-thing';
let stamp = await figma.clientStorage.getAsync(COPY_ID_KEY);
print(stamp);
if (stamp) {
paste();
} else {
copy();
}
function copy() {
if (!figma.currentPage.selection.length) {
figma.notify("Select one thing to copy");
return;
}
figma.clientStorage.setAsync(COPY_ID_KEY,
figma.currentPage.selection[0].id);
figma.notify("Copied! Select item to replace and re-run.");
}
function paste() {
figma.clientStorage.setAsync(COPY_ID_KEY, null);
if (!figma.currentPage.selection.length) {
figma.notify("No items selected, resetting.");
return;
}
let stampNode = figma.root.findOne(node => node.id == stamp)! as SceneNode;
let clonedStamp = stampNode.clone();
let newSelection: SceneNode[] = [];
for (let targetNode of figma.currentPage.selection) {
let clone = clonedStamp.clone();
let parent = targetNode.parent!;
let indexInParent = parent.children.indexOf(targetNode);
parent.insertChild(indexInParent, clone);
clone.x = targetNode.x;
clone.y = targetNode.y;
targetNode.remove();
newSelection.push(clone);
}
figma.currentPage.selection = newSelection;
clonedStamp.remove();
figma.notify("Swapped!");
}
})();
// Resize selected layers to the given width
interface Target {
width?: number;
height?: number;
scale?: number;
};
let target: Target = {scale: 3};
for (let node of figma.currentPage.selection) {
let scale = 1;
if (target.scale) {
scale = target.scale;
} else if (target.width) {
scale = target.width / node.width;
} else if (target.height) {
scale = target.height / node.height;
}
node.rescale(scale);
// node.resizeWithoutConstraints(
// node.width * scale,
// node.height * scale,
// )
if (node.type == 'TEXT') {
node.fontSize = (node.fontSize as number) * scale;
node.strokeWeight *= scale;
node.effects = node.effects.map(e => {
let effect = {...e};
effect.radius *= scale;
if ('offset' in effect) {
effect.offset = {
x: effect.offset.x * scale,
y: effect.offset.y * scale
}
}
return effect as Effect;
});
}
// Select the parent nodes of all the currently selected nodes
let parents = new Set(figma.currentPage.selection
.map(node => node.parent)
.filter(node => !!node && node.type != 'PAGE')) as Set<SceneNode>;
figma.currentPage.selection = [...parents];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment