-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMagnetizedGroup.jsx
91 lines (74 loc) · 2.98 KB
/
MagnetizedGroup.jsx
1
function MagnetizedGroup(items){ this.items = items; this.state = this.calculateState(); this.handler = this.getObjectChangeHandler();};MagnetizedGroup.prototype.calculateState = function calculateState(){ var state = []; for(var index = 0; index < this.items.length; index++){ var item = this.items[index]; if(!item || !item.isValid || !("paths" in item)) continue; state.push({ item:item, points:Array.prototype.concat.apply([],item.paths.everyItem().entirePath), cornerOptions: CornerOptUtils.extract(item,{}) }); } return state;};MagnetizedGroup.prototype.magnetize = function magnetize(){ app.addEventListener("afterSelectionAttributeChanged", this.handler);};MagnetizedGroup.prototype.demagnetize = function demagnetize(){ app.removeEventListener("afterSelectionAttributeChanged", this.handler);};MagnetizedGroup.prototype.remove = function remove(index){ this.items.length > index && this.items.splice(index, 1);};MagnetizedGroup.prototype.add = function add(item){ this.items.push(item);};MagnetizedGroup.prototype.rearrange = function rearrange(from, to){ this.items.splice(to, 0 , this.items.splice(from, 1)[0]);};MagnetizedGroup.prototype.getObjectChangeHandler = function getObjectChangeHandler(){ var context = this; return function(){ var changedObject = context.detectChangedObject(context.state); if(changedObject){ for(var itemIndex = 0; itemIndex < context.items.length; itemIndex++){ var item = context.items[itemIndex]; item != changedObject && context.copyPath(changedObject, item); } } context.state = context.calculateState(); }}MagnetizedGroup.prototype.detectChangedObject = function detectChangedObject(lastState){ for(var index = 0; index < lastState.length; index++){ var itemState = lastState[index]; var item = itemState.item; if(!item || !item.isValid){ continue; } var currentPoints = Array.prototype.concat.apply([],item.paths.everyItem().entirePath); if(!arrayEquivalent(currentPoints, itemState.points) || CornerOptUtils.changed(item, itemState.cornerOptions)){ return item; } } return null;};MagnetizedGroup.prototype.copyPath = function copyPath(from, to){ if(from && to && from.isValid && to.isValid){ var pathPoints = from.paths.everyItem().entirePath; var asCollection = Array.prototype.concat.apply([], pathPoints); var cornerOptions = CornerOptUtils.extract(from, {}); var pathType = to.paths.firstItem().pathType; try{ to.convertShape(ConvertShapeOptions.CONVERT_TO_RECTANGLE); }catch(err){}; to.paths.everyItem().pathType = pathType; to.paths.everyItem().entirePath = asCollection; CornerOptUtils.inject(to, cornerOptions); }}