sketch-hq / SketchAPI

The JavaScript plugin library embedded in Sketch
https://developer.sketch.com/reference/api
MIT License
842 stars 126 forks source link

APIs missing: Layer Pins and Size properties #885

Closed matteogratton closed 1 year ago

matteogratton commented 2 years ago

The properties are not currently available in the APIs.

I created a function to handle them, but it would be nice to have some better management for this.

Here's my function:

function setResizingConstraint(
    layer,
    pinProperties = [false, false, false, false],
    sizeProperties = [false, false]
) {
    let flagMap = ["1", "1", "1", "1", "1", "1"];

    let leftPin = pinProperties[0];
    if (leftPin === true) {
        flagMap[3] = "0";
    }
    let rightPin = pinProperties[1];
    if (rightPin === true) {
        flagMap[5] = "0";
    }
    let topPin = pinProperties[2];
    if (topPin === true) {
        flagMap[0] = "0";
    }
    let bottomPin = pinProperties[3];
    if (bottomPin === true) {
        flagMap[2] = "0";
    }

    let fixedWidth = sizeProperties[0];
    if (fixedWidth === true) {
        flagMap[4] = "0";
    }
    let fixedHeight = sizeProperties[1];
    if (fixedHeight === true) {
        flagMap[1] = "0";
    }

    let result =
        flagMap[0] +
        flagMap[1] +
        flagMap[2] +
        flagMap[3] +
        flagMap[4] +
        flagMap[5];

    layer.sketchObject.setResizingConstraint(parseInt(result, 2));
}

My function does have some problems, as it doesn't check for possible issues: it is impossible to set both the pin properties for left and right to true and also the size property in width to true. Same for up-bottom and height.

It would be nice to have something like: layer.pin: [true, true, false, false] and layer.fixSize: [true, false]