This repository has been archived by the owner on Feb 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
5,934 additions
and
321 deletions.
There are no files selected for viewing
186 changes: 185 additions & 1 deletion
186
SeparateShapes-plugin.sketchplugin/Contents/Sketch/separate-shapes.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,185 @@ | ||
var that=this;function __skpm_run(e,t){that.context=t;var r=function(e){var t={};function r(n){if(t[n])return t[n].exports;var a=t[n]={i:n,l:!1,exports:{}};return e[n].call(a.exports,a,a.exports,r),a.l=!0,a.exports}return r.m=e,r.c=t,r.d=function(e,t,n){r.o(e,t)||Object.defineProperty(e,t,{configurable:!1,enumerable:!0,get:n})},r.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return r.d(t,"a",t),t},r.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},r.p="",r(r.s=0)}([function(e,t){Object.defineProperty(t,"__esModule",{value:!0});var r="-",n="∩",a=function(e,t,r,n){if(!((c=e)&&c.isKindOfClass(NSNull)||!c)){e.setStyle(t.style());for(var a=e.splitPathsIntoShapes(),o=a.count(),u=new Array(o),s=o-1;s>=0;s--){var i=a[s];i.setName(t.name()+" "+n+" "+r.name()+(s>0?" "+s:"")),u[s]=i}return u}var c},o=function(e,t,o){var u=e.bezierPathWithTransforms(),s=t.bezierPathWithTransforms(),i=null;switch(o){case r:i=u.booleanSubtractWith(s);break;case n:i=u.booleanIntersectWith(s)}if(i){var c=MSShapeGroup.shapeWithBezierPath(i);switch(o){case r:return a(c,e,t,o);case n:return a(c,t,e,o)}}return null};t.default=function(e){var t=e.selection,u=t.count();if(u>=2){var s=[],i=function(e){for(var t=e[0],n="",a=e.count()-1;a>0;a--)t=MSShapeGroup.shapeWithBezierPath(t.bezierPathWithTransforms().booleanSubtractWith(e[a].bezierPathWithTransforms())),n+=(""!=n?" "+r+" ":"")+e[a].name();return t.setName(n),t.setStyle(e[0].style()),t}(t);s.push(a(i,t[0],i,r));for(var c=1;c<u;c++)s.push(o(t[c],t[0],r)),s.push(o(t[0],t[c],n)),t[c].removeFromParent();s=[].concat.apply([],s.filter(function(e){return e})),t[0].parentGroup().insertLayers_afterLayer(s,t[0]),e.document.currentPage().changeSelectionBySelectingLayers(s),t[0].removeFromParent()}else e.document.showMessage("Select more than one shape")}}]);"default"===e&&"function"==typeof r?r(t):r[e](t)}that.onRun=__skpm_run.bind(this,"default"); | ||
var that = this; | ||
function __skpm_run (key, context) { | ||
that.context = context; | ||
|
||
var exports = | ||
/******/ (function(modules) { // webpackBootstrap | ||
/******/ // The module cache | ||
/******/ var installedModules = {}; | ||
/******/ | ||
/******/ // The require function | ||
/******/ function __webpack_require__(moduleId) { | ||
/******/ | ||
/******/ // Check if module is in cache | ||
/******/ if(installedModules[moduleId]) { | ||
/******/ return installedModules[moduleId].exports; | ||
/******/ } | ||
/******/ // Create a new module (and put it into the cache) | ||
/******/ var module = installedModules[moduleId] = { | ||
/******/ i: moduleId, | ||
/******/ l: false, | ||
/******/ exports: {} | ||
/******/ }; | ||
/******/ | ||
/******/ // Execute the module function | ||
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__); | ||
/******/ | ||
/******/ // Flag the module as loaded | ||
/******/ module.l = true; | ||
/******/ | ||
/******/ // Return the exports of the module | ||
/******/ return module.exports; | ||
/******/ } | ||
/******/ | ||
/******/ | ||
/******/ // expose the modules object (__webpack_modules__) | ||
/******/ __webpack_require__.m = modules; | ||
/******/ | ||
/******/ // expose the module cache | ||
/******/ __webpack_require__.c = installedModules; | ||
/******/ | ||
/******/ // define getter function for harmony exports | ||
/******/ __webpack_require__.d = function(exports, name, getter) { | ||
/******/ if(!__webpack_require__.o(exports, name)) { | ||
/******/ Object.defineProperty(exports, name, { | ||
/******/ configurable: false, | ||
/******/ enumerable: true, | ||
/******/ get: getter | ||
/******/ }); | ||
/******/ } | ||
/******/ }; | ||
/******/ | ||
/******/ // getDefaultExport function for compatibility with non-harmony modules | ||
/******/ __webpack_require__.n = function(module) { | ||
/******/ var getter = module && module.__esModule ? | ||
/******/ function getDefault() { return module['default']; } : | ||
/******/ function getModuleExports() { return module; }; | ||
/******/ __webpack_require__.d(getter, 'a', getter); | ||
/******/ return getter; | ||
/******/ }; | ||
/******/ | ||
/******/ // Object.prototype.hasOwnProperty.call | ||
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); }; | ||
/******/ | ||
/******/ // __webpack_public_path__ | ||
/******/ __webpack_require__.p = ""; | ||
/******/ | ||
/******/ // Load entry module and return exports | ||
/******/ return __webpack_require__(__webpack_require__.s = 0); | ||
/******/ }) | ||
/************************************************************************/ | ||
/******/ ([ | ||
/* 0 */ | ||
/***/ (function(module, exports) { | ||
|
||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
var isNullOrNil = function isNullOrNil(value) { | ||
if (value && value.isKindOfClass(NSNull)) { | ||
return true; | ||
} else if (value) { | ||
return false; | ||
} | ||
return true; | ||
}; | ||
|
||
var ops = { | ||
SUBTRACT: "-", | ||
INTERSECT: "∩" | ||
}; | ||
|
||
var splitAndStylePath = function splitAndStylePath(sg, shape1, shape2, op) { | ||
if (!isNullOrNil(sg)) { | ||
sg.setStyle(shape1.style()); | ||
var splitNS = sg.splitPathsIntoShapes(); | ||
var count = splitNS.count(); | ||
var splitJS = new Array(count); | ||
for (var i = count - 1; i >= 0; i--) { | ||
var shape = splitNS[i]; | ||
shape.setName(shape1.name() + " " + op + " " + shape2.name() + (i > 0 ? " " + i : "")); | ||
splitJS[i] = shape; | ||
} | ||
return splitJS; | ||
} | ||
}; | ||
|
||
var shapeGroupFromOp = function shapeGroupFromOp(shape1, shape2, op) { | ||
var path1 = shape1.pathInFrameWithTransforms(), | ||
path2 = shape2.pathInFrameWithTransforms(), | ||
newPath = null; | ||
switch (op) { | ||
case ops.SUBTRACT: | ||
newPath = path1.booleanSubtractWith(path2); | ||
break; | ||
case ops.INTERSECT: | ||
newPath = path1.booleanIntersectWith(path2); | ||
break; | ||
} | ||
if (newPath) { | ||
var sg = MSShapeGroup.shapeWithBezierPath(newPath); | ||
switch (op) { | ||
case ops.SUBTRACT: | ||
return splitAndStylePath(sg, shape1, shape2, op); | ||
break; | ||
case ops.INTERSECT: | ||
return splitAndStylePath(sg, shape2, shape1, op); | ||
break; | ||
} | ||
} | ||
return null; | ||
}; | ||
|
||
var baseShapeBySubtractingOthers = function baseShapeBySubtractingOthers(selection) { | ||
var shape = selection[0]; | ||
var layers = selection.count(); | ||
var name = ""; | ||
|
||
for (var i = layers - 1; i > 0; i--) { | ||
shape = MSShapeGroup.shapeWithBezierPath(shape.pathInFrameWithTransforms().booleanSubtractWith(selection[i].pathInFrameWithTransforms())); | ||
name += (name != "" ? " " + ops.SUBTRACT + " " : "") + selection[i].name(); | ||
} | ||
shape.setName(name); | ||
shape.setStyle(selection[0].style()); | ||
return shape; | ||
}; | ||
|
||
var separate = function separate(context) { | ||
var selection = context.selection; | ||
var layers = selection.count(); | ||
|
||
if (layers >= 2) { | ||
var separatedShapeGroups = []; | ||
|
||
var baseShape = baseShapeBySubtractingOthers(selection, layers); | ||
separatedShapeGroups.push(splitAndStylePath(baseShape, selection[0], baseShape, ops.SUBTRACT)); | ||
|
||
for (var i = 1; i < layers; i++) { | ||
separatedShapeGroups.push(shapeGroupFromOp(selection[i], selection[0], ops.SUBTRACT)); | ||
separatedShapeGroups.push(shapeGroupFromOp(selection[0], selection[i], ops.INTERSECT)); | ||
selection[i].removeFromParent(); | ||
} | ||
separatedShapeGroups = [].concat.apply([], separatedShapeGroups.filter(function (layer) { | ||
return layer; | ||
})); | ||
|
||
selection[0].parentGroup().insertLayers_afterLayer(separatedShapeGroups, selection[0]); | ||
context.document.currentPage().changeSelectionBySelectingLayers(separatedShapeGroups); | ||
|
||
selection[0].removeFromParent(); | ||
} else { | ||
context.document.showMessage("Select more than one shape"); | ||
} | ||
}; | ||
|
||
exports["default"] = separate; | ||
|
||
/***/ }) | ||
/******/ ]); | ||
if (key === 'default' && typeof exports === 'function') { | ||
exports(context); | ||
} else { | ||
exports[key](context); | ||
} | ||
} | ||
that['onRun'] = __skpm_run.bind(this, 'default') |
Oops, something went wrong.