1 line
9.6 KiB
JavaScript
1 line
9.6 KiB
JavaScript
"use strict";var e=function(e){return e&&e.__esModule?e["default"]:e},t=function(){function e(e,t){for(var a in t){var n=t[a];n.configurable=!0,n.value&&(n.writable=!0)}Object.defineProperties(e,t)}return function(t,a,n){return a&&e(t.prototype,a),n&&e(t,n),t}}(),a=function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")},n=e(require("lodash")),i=e(require("../../common/clipboard")),r={NORMAL:"NORMAL",TOGGLE:"TOGGLE"},o=function(){function e(t){a(this,e),this.animationModel=t,this.reset()}return t(e,{reset:{value:function(){this.animationModel.state.selection.keyframes={selected:{}},this.anchor={layerIndex:null,keyframeIndex:null}}},validateSelection:{value:function(){var e=this;for(var t in this.getState().selected){if(null===this.animationModel.getLayer(t))return void delete this.getState().selected[t];this.getState().selected[t]=n.filter(this.getState().selected[t],function(t){return t.time<e.animationModel.getDuration()})}}},canDeleteKeyframes:{value:function(){var e=this,t=!1;return this.forEachSelected(!0,!0,function(a,n){e.animationModel.keyframeExists(a,n)===!0&&(t=!0)}),t}},canPasteKeyframes:{value:function(e){var t=this;if("undefined"==typeof i.animationData)return!1;var a=JSON.parse(i.animationData);if("keyframes"!==a.type)return!1;var n=this.animationModel.getLayerIndex(e),r=a.value,o=!1;return r.forEach(function(e){var a=e.layerIndex+n;0>a||a>=t.animationModel.getLayerCount()-1||e.layerType!==t.animationModel.getLayerByIndex(a).type&&(o=!0)}),o===!1}},forEachLayerSelected:{value:function(e,t){var a=this,i=[];for(var r in this.getState().selected)i.push(r);i=n.sortBy(i,function(t){return e?a.animationModel.getLayerIndex(t):-a.animationModel.getLayerIndex(t)}),i.forEach(function(e){t(e)})}},forEachSelected:{value:function(e,t,a){var i=this;this.forEachLayerSelected(e,function(e){t?n.forEach(i.getState().selected[e],function(t,n){a(e,t,n)}):n.forEachRight(i.getState().selected[e],function(t,n){a(e,t,n)})})}},getState:{value:function(){return this.animationModel.state.selection.keyframes}},includesKeyframes:{value:function(){var e=this;for(var t in this.getState().selected){var a=function(t){for(var a=e.animationModel.getLayer(t),i=e.getState().selected[t],r=0;r<i.length;r++){var o=function(e){return-1!==n.findIndex(a.keyframes,function(t){return t.time===i[e]})?{v:{v:!0}}:void 0}(r);if("object"==typeof o)return o.v}}(t);if("object"==typeof a)return a.v}return!1}},getSelectionLayerType:{value:function(){if(this.isMultiSelect())throw new Error("Don't call KeyframeSelection.getSelectionLayerType() when KeyframeSelection.isMultiSelect() is true");var e=this.getCurrentKeyframe().layerId;return e?this.animationModel.getLayer(e).type:void 0}},getCurrentKeyframe:{value:function(){if(!this.isMultiSelect()){for(var e in this.getState().selected)if(this.getState().selected[e].length)return{keyframeIndex:this.getState().selected[e][0],layerId:e};var t=this.animationModel.getSelectedLayer();return{keyframeIndex:this.animationModel.uiState.currentTime,layerId:t?t.id:void 0}}throw new Error("KeyframeSelection.getCurrentKeyframe() should only be called when there aren't multiple keyframes selected")}},isSingleSelect:{value:function(){return!this.isMultiSelect()}},isMultiSelect:{value:function(){var e=0;for(var t in this.getState().selected)if(e+=this.getState().selected[t].length,e>=2)return!0;return!1}},isAnchor:{value:function(e,t){return this.animationModel.getLayerIndex(e)===this.getState().anchor.layerIndex&&t===this.getState().anchor.keyframeIndex}},isSelected:{value:function(e,t){return"undefined"==typeof this.getState().selected[e]?!1:-1!==n.findIndex(this.getState().selected[e],function(e){return e===t})}},onClick:{value:function(e,t,a){this.onMouseDown(e,t,a)}},onMouseDown:{value:function(e,t,a,n){1!==e.nativeEvent.which&&(e.preventDefault(),e.stopPropagation());var i=r.NORMAL;if("win32"===process.platform?e.nativeEvent.ctrlKey&&(i=r.TOGGLE):e.nativeEvent.metaKey&&(i=r.TOGGLE),"click"!==e.type||!this.isSingleSelect()&&i!==r.TOGGLE){var o=this.isSelected(t,a);if("click"===e.type||i!==r.NORMAL||e.nativeEvent.shiftKey!==!1||!o){i===r.NORMAL&&("click"===e.type||e.nativeEvent.shiftKey===!0||o===!1&&"click"!==e.type&&e.nativeEvent.shiftKey!==!0)&&(this.animationModel.setKeyframe(a),this.animationModel.setSelection(t),this._clearSelection(),n=!0);var l=this.animationModel.getLayerIndex(t);if(e.nativeEvent.shiftKey===!1)this._addKeyframe(t,a,i)&&(n=!0);else for(var d=Math.min(this.anchor.layerIndex,l),s=Math.max(this.anchor.layerIndex,l),c=d;s>=c;c++)for(var f=Math.min(this.anchor.keyframeIndex,a),h=Math.max(this.anchor.keyframeIndex,a),u=f;h>=u;u++)this._addKeyframe(this.animationModel.getLayerByIndex(c).id,u,i)&&(n=!0);e.nativeEvent.shiftKey===!1&&(this.anchor.layerIndex=l,this.anchor.keyframeIndex=a);var y=this.animationModel.getLayerByIndex(l);y!==this.animationModel.getSelectedLayer()&&(this.animationModel.setSelection(t),n=!0),n&&(this.animationModel.addRedoUndoEntry(),this.animationModel.emit("ondatachanged"))}}}},clearSelection:{value:function(){this._clearSelection()&&(this.animationModel.addRedoUndoEntry(),this.animationModel.emit("ondatachanged"))}},selectAll:{value:function(){this._clearSelection();for(var e=0;e<this.animationModel.state.keyframes.layers.length;e++){var t=this.animationModel.state.keyframes.layers[e];this.getState().selected[t.id]=[];for(var a=0;a<this.animationModel.state.keyframes.duration;a++)this.getState().selected[t.id][a]=a}this.animationModel.emit("ondatachanged"),this.animationModel.addRedoUndoEntry()}},deleteKeyframes:{value:function(){var e=this._deleteKeyframes();this._clearSelection()&&(e=!0),e&&(this.animationModel.syncOverrides(),this.animationModel.addRedoUndoEntry(),this.animationModel.emit("ondatachanged"),this.animationModel.emit("onpropertieschanged"))}},moveSelection:{value:function(e,t,a){var n=this,i=this,r=t.keyframeIndex-e.keyframeIndex,o=this.animationModel.getLayerIndex(t.layerId)-this.animationModel.getLayerIndex(e.layerId),l=!1,d={};if(this.forEachLayerSelected(!0,function(e){var t=n.animationModel.getLayerIndex(e),a=t+o;return 0>a||a>=n.animationModel.getLayerCount()?void(d[e]=!0):void(n.animationModel.getLayerByIndex(t).type!==n.animationModel.getLayerByIndex(a).type&&(l=!0))}),l!==!0){var s=0>r,c=0>o;if(this.forEachSelected(c,s,function(e,t,l){var d=n.animationModel.getLayerIndex(e)+o,s=n.animationModel.getLayerByIndex(d).id,c=t+r;n.animationModel.keyframeExists(e,t)&&(d>=0&&d<=i.animationModel.getLayerCount()-1&&c>=0&&c<=i.animationModel.getDuration()-1?i.animationModel.moveKeyframe(e,t,s,c,!0,a):a||i.animationModel._deleteKeyByTime(e,t));var f=n.getState().selected[e];f[l]=f[l]+r}),t.layerId!==e.layerId){for(var f in d)delete this.getState().selected[f];var h={};for(var f in this.getState().selected){var u=this.animationModel.getLayerIndex(f);h[this.animationModel.getLayerByIndex(u+o).id]=this.getState().selected[f]}this.getState().selected=h}this.animationModel.setSelection(t.layerId),this.animationModel.addRedoUndoEntry(),this.animationModel.emit("ondatachanged"),this.animationModel.emit("onpropertieschanged")}}},cutSelectedKeyframes:{value:function(){this.copySelectedKeyframes(),this.deleteKeyframes()}},copySelectedKeyframes:{value:function(){var e=this,t=void 0;if(this.isMultiSelect())t=this.getState().selected;else{var a=this.getCurrentKeyframe();if(void 0===a.layerId)return;t={},t[a.layerId]=[a.keyframeIndex]}var r=this.animationModel.getDuration()-1,o=this.animationModel.getLayerCount()-1;for(var l in t)o=Math.min(o,this.animationModel.getLayerIndex(l)),t[l].forEach(function(e){r=Math.min(r,e)});var d=[];for(var l in t)!function(a){var i={layerType:e.animationModel.getLayer(a).type,layerIndex:e.animationModel.getLayerIndex(a)-o,keyframes:[]};t[a].forEach(function(t){var o=e.animationModel.getKeyframe(a,t);o?i.keyframes.push({keyframeIndex:t-r,value:n.cloneDeep(o.value)}):i.keyframes.push({keyframeIndex:t-r,value:null})}),d.push(i)}(l);if(d=n.sortBy(d,function(e){return e.layerIndex}),d.length){var s={type:"keyframes",value:d};i.animationData=JSON.stringify(s)}}},pasteKeyframes:{value:function(e,t){var a=this;if(this.canPasteKeyframes(e,t)!==!1){var n=this._clearSelection(),o=JSON.parse(i.animationData).value,l=this.animationModel.getLayerIndex(e);o.forEach(function(e){var i=e.layerIndex+l;0>i||i>a.animationModel.getLayerCount()-1||e.keyframes.forEach(function(e){var o=e.keyframeIndex+t;if(o>=0||o<a.animationModel.getDuration()){var l=a.animationModel.getLayerByIndex(i);null!==e.value&&a.animationModel.upsertKeyframe(l,o,e.value),a._addKeyframe(l.id,o,r.NORMAL),n=!0}})}),n&&(this.animationModel.syncOverrides(),this.animationModel.emit("ondatachanged"),this.animationModel.emit("onpropertieschanged"),this.animationModel.addRedoUndoEntry())}}},_clearSelection:{value:function(){var e=!1;for(var t in this.getState().selected)this.getState().selected[t].length&&(e=!0);return this.getState().selected={},e}},_addKeyframe:{value:function(e,t,a){var i=a===r.TOGGLE&&this.isSelected(e,t),o=void 0;if(i){if("undefined"==typeof this.getState().selected[e])return!1;o=this.getState().selected[e].length,n.remove(this.getState().selected[e],function(e){return e===t})}else{if("undefined"==typeof this.getState().selected[e])return this.getState().selected[e]=[t],!0;o=this.getState().selected[e].length,this.getState().selected[e].push(t),this.getState().selected[e].sort(),this.getState().selected[e]=n.uniq(this.getState().selected[e])}return o!==this.getState().selected[e].length}},_deleteKeyframes:{value:function(){var e=this;for(var t in this.getState().selected)!function(t){var a=e.getState().selected[t],i=e.animationModel.getLayer(t);i.keyframes=n.filter(i.keyframes,function(e){return-1===n.findIndex(a,function(t){return t===e.time})})}(t)}}}),e}();module.exports=o; |