public/code.js
/**
* Allows you to copy and paste props between nodes.
*
* @param source - The node you want to copy from
* @param target - The node or object you want to paste to
* @param args - Either options or a callback.
* @returns A node or object with the properties copied over
*/
Allows you to copy and paste props between nodes.
Allows you to copy and paste props between nodes .
function copyPaste(source, target, ...args) {
    var targetIsEmpty;
    if (target && Object.keys(target).length === 0 && target.constructor === Object) {
        targetIsEmpty = true;
    }
    var options;
    if (typeof args[0] === 'function')
        ;
    if (typeof args[1] === 'function')
        ;
    if (typeof args[0] === 'object' && typeof args[0] !== 'function')
        options = args[0];
    if (typeof args[0] === 'object' && typeof args[0] !== 'function')
        options = args[0];
    if (!options)
        options = {};
    const { include, exclude, withoutRelations, removeConflicts } = options;
    let allowlist = nodeProps.filter(function (el) {
        return !readOnly.includes(el);
    });
    if (include) {
        allowlist = include.filter(function (el) {
            return !readOnly.includes(el);
        });
    }
    if (exclude) {
        allowlist = allowlist.filter(function (el) {
            return !exclude.concat(readOnly).includes(el);
        });
    }
    if (target && !targetIsEmpty) {
        allowlist = allowlist.filter(function (el) {
            return !['id', 'type'].includes(el);
        });
    }
    var obj = {};
    if (targetIsEmpty) {
        if (obj.id === undefined) {
            obj.id = source.id;
        }
        if (obj.type === undefined) {
            obj.type = source.type;
        }
        if (source.key)
            obj.key = source.key;
    }
    const props = Object.entries(Object.getOwnPropertyDescriptors(source.__proto__));
    for (const [key, value] of props) {
        if (allowlist.includes(key)) {
            try {
                if (typeof obj[key] === 'symbol') {
                    obj[key] = 'Mixed';
                }
                else {
                    obj[key] = value.get.call(source);
                }
            }
            catch (err) {
                obj[key] = undefined;
            }
        }
    }
    if (!removeConflicts) {
        !obj.fillStyleId && obj.fills ? delete obj.fillStyleId : delete obj.fills;
        !obj.strokeStyleId && obj.strokes ? delete obj.strokeStyleId : delete obj.strokes;
        !obj.backgroundStyleId && obj.backgrounds ? delete obj.backgroundStyleId : delete obj.backgrounds;
        !obj.effectStyleId && obj.effects ? delete obj.effectStyleId : delete obj.effects;
        !obj.gridStyleId && obj.layoutGrids ? delete obj.gridStyleId : delete obj.layoutGrids;
        if (obj.textStyleId) {
            delete obj.fontName;
            delete obj.fontSize;
            delete obj.letterSpacing;
            delete obj.lineHeight;
            delete obj.paragraphIndent;
            delete obj.paragraphSpacing;
            delete obj.textCase;
            delete obj.textDecoration;
        }
        else {
            delete obj.textStyleId;
        }
        if (obj.cornerRadius !== figma.mixed) {
            delete obj.topLeftRadius;
            delete obj.topRightRadius;
            delete obj.bottomLeftRadius;
            delete obj.bottomRightRadius;
        }
        else {
            delete obj.cornerRadius;
        }
    }
    if (targetIsEmpty) {
        if (source.parent && !withoutRelations) {
            obj.parent = { id: source.parent.id, type: source.parent.type };
        }
    }
    if (targetIsEmpty) {
        if (source.type === "FRAME" || source.type === "COMPONENT" || source.type === "COMPONENT_SET" || source.type === "PAGE" || source.type === 'GROUP' || source.type === 'INSTANCE' || source.type === 'DOCUMENT' || source.type === 'BOOLEAN_OPERATION') {
            if (source.children && !withoutRelations) {
                obj.children = source.children.map((child) => copyPaste(child, {}, { withoutRelations }));
            }
        }
        if (source.type === "INSTANCE") {
            if (source.mainComponent && !withoutRelations) {
                obj.masterComponent = copyPaste(source.mainComponent, {}, { withoutRelations });
            }
        }
    }
    Object.assign(target, obj);
    return obj;
}
function copyPaste ( source , target , ... args ) { var targetIsEmpty ; if ( target && Object . keys ( target ) . length === 0 && target . constructor === Object ) { targetIsEmpty = true ; } var options ; if ( typeof args [ 0 ] === 'function' ) ; if ( typeof args [ 1 ] === 'function' ) ; if ( typeof args [ 0 ] === 'object' && typeof args [ 0 ] !== 'function' ) options = args [ 0 ] ; if ( typeof args [ 0 ] === 'object' && typeof args [ 0 ] !== 'function' ) options = args [ 0 ] ; if ( ! options ) options = { } ; const { include , exclude , withoutRelations , removeConflicts } = options ; let allowlist = nodeProps . filter ( function ( el ) { return ! readOnly . includes ( el ) ; } ) ; if ( include ) { allowlist = include . filter ( function ( el ) { return ! readOnly . includes ( el ) ; } ) ; } if ( exclude ) { allowlist = allowlist . filter ( function ( el ) { return ! exclude . concat ( readOnly ) . includes ( el ) ; } ) ; } if ( target && ! targetIsEmpty ) { allowlist = allowlist . filter ( function ( el ) { return ! [ 'id' , 'type' ] . includes ( el ) ; } ) ; } var obj = { } ; if ( targetIsEmpty ) { if ( obj . id === undefined ) { obj . id = source . id ; } if ( obj . type === undefined ) { obj . type = source . type ; } if ( source . key ) obj . key = source . key ; } const props = Object . entries ( Object . getOwnPropertyDescriptors ( source . __proto__ ) ) ; for ( const [ key , value ] of props ) { if ( allowlist . includes ( key ) ) { try { if ( typeof obj [ key ] === 'symbol' ) { obj [ key ] = 'Mixed' ; } else { obj [ key ] = value . get . call ( source ) ; } } catch ( err ) { obj [ key ] = undefined ; } } } if ( ! removeConflicts ) { ! obj . fillStyleId && obj . fills ? delete obj . fillStyleId : delete obj . fills ; ! obj . strokeStyleId && obj . strokes ? delete obj . strokeStyleId : delete obj . strokes ; ! obj . backgroundStyleId && obj . backgrounds ? delete obj . backgroundStyleId : delete obj . backgrounds ; ! obj . effectStyleId && obj . effects ? delete obj . effectStyleId : delete obj . effects ; ! obj . gridStyleId && obj . layoutGrids ? delete obj . gridStyleId : delete obj . layoutGrids ; if ( obj . textStyleId ) { delete obj . fontName ; delete obj . fontSize ; delete obj . letterSpacing ; delete obj . lineHeight ; delete obj . paragraphIndent ; delete obj . paragraphSpacing ; delete obj . textCase ; delete obj . textDecoration ; } else { delete obj . textStyleId ; } if ( obj . cornerRadius !== figma . mixed ) { delete obj . topLeftRadius ; delete obj . topRightRadius ; delete obj . bottomLeftRadius ; delete obj . bottomRightRadius ; } else { delete obj . cornerRadius ; } } if ( targetIsEmpty ) { if ( source . parent && ! withoutRelations ) { obj . parent = { id : source . parent . id , type : source . parent . type } ; } } if ( targetIsEmpty ) { if ( source . type === "FRAME" || source . type === "COMPONENT" || source . type === "COMPONENT_SET" || source . type === "PAGE" || source . type === 'GROUP' || source . type === 'INSTANCE' || source . type === 'DOCUMENT' || source . type === 'BOOLEAN_OPERATION' ) { if ( source . children && ! withoutRelations ) { obj . children = source . children . map ( ( child ) => copyPaste ( child , { } , { withoutRelations } ) ) ; } } if ( source . type === "INSTANCE" ) { if ( source . mainComponent && ! withoutRelations ) { obj . masterComponent = copyPaste ( source . mainComponent , { } , { withoutRelations } ) ; } } } Object . assign ( target , obj ) ; return obj ; }
Allows you to copy and paste props between nodes.
Allows you to copy and paste props between nodes .
// const props = Object.entries(Object.getOwnPropertyDescriptors(source.__proto__)) // If include supplied, include copy across these properties and their values if they exist // If exclude supplied then don't copy over the values of these properties // target supplied, don't copy over the values of these properties // Needs building in // if (callback) { //     callback(obj) // } // else { // } // Only applicable to objects because these properties cannot be set on nodes // Only applicable to objects because these properties cannot be set on nodes