@leafer-in/editor 1.0.0-rc.7 → 1.0.0-rc.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/editor.esm.js +25 -60
- package/dist/editor.esm.min.js +1 -1
- package/dist/editor.js +25 -60
- package/dist/editor.min.js +1 -1
- package/package.json +4 -4
- package/src/Editor.ts +7 -5
- package/src/decorator/data.ts +4 -1
- package/src/display/EditBox.ts +4 -19
- package/src/display/EditSelect.ts +4 -2
- package/src/editor/target.ts +4 -5
- package/src/event/EditorEvent.ts +3 -0
- package/src/helper/EditDataHelper.ts +1 -1
- package/src/helper/EditorHelper.ts +2 -31
- package/types/index.d.ts +7 -7
package/dist/editor.esm.js
CHANGED
|
@@ -72,8 +72,11 @@ function targetAttr(fn) {
|
|
|
72
72
|
const privateKey = '_' + key;
|
|
73
73
|
defineKey(target, key, {
|
|
74
74
|
get() { return this[privateKey]; },
|
|
75
|
-
set(value) {
|
|
76
|
-
this[privateKey]
|
|
75
|
+
set(value) {
|
|
76
|
+
const old = this[privateKey];
|
|
77
|
+
if (old !== value)
|
|
78
|
+
this[privateKey] = value, fn(this, old);
|
|
79
|
+
}
|
|
77
80
|
});
|
|
78
81
|
};
|
|
79
82
|
}
|
|
@@ -304,10 +307,12 @@ class EditSelect extends Group {
|
|
|
304
307
|
selectList.push(item); });
|
|
305
308
|
list.forEach(item => { if (!this.originList.has(item))
|
|
306
309
|
selectList.push(item); });
|
|
307
|
-
editor.
|
|
310
|
+
if (selectList.length !== editor.list.length || editor.list.some((child, index) => child !== selectList[index])) {
|
|
311
|
+
editor.target = selectList;
|
|
312
|
+
}
|
|
308
313
|
}
|
|
309
314
|
else {
|
|
310
|
-
editor.target = this.originList;
|
|
315
|
+
editor.target = this.originList.list;
|
|
311
316
|
if (editor.leafList.length)
|
|
312
317
|
editor.update();
|
|
313
318
|
}
|
|
@@ -497,7 +502,7 @@ const EditDataHelper = {
|
|
|
497
502
|
return { origin, skewX, skewY };
|
|
498
503
|
},
|
|
499
504
|
getAround(around, altKey) {
|
|
500
|
-
return (altKey && !around) ?
|
|
505
|
+
return (altKey && !around) ? 'center' : around;
|
|
501
506
|
},
|
|
502
507
|
getRotateDirection(direction, rotation, totalDirection = 8) {
|
|
503
508
|
direction = (direction + Math.round(rotation / (360 / totalDirection))) % totalDirection;
|
|
@@ -602,13 +607,13 @@ class EditBox extends Group {
|
|
|
602
607
|
const { width, height } = bounds;
|
|
603
608
|
const { rect, circle, resizePoints, rotatePoints, resizeLines } = this;
|
|
604
609
|
const { middlePoint, resizeable, rotateable, stroke, strokeWidth } = config;
|
|
605
|
-
const points = this.getDirection8Points(bounds);
|
|
606
610
|
const pointsStyle = this.getPointsStyle();
|
|
607
611
|
const middlePointsStyle = this.getMiddlePointsStyle();
|
|
608
612
|
this.visible = list[0] && !list[0].locked;
|
|
609
|
-
let point, style, rotateP, resizeP, resizeL;
|
|
613
|
+
let point = {}, style, rotateP, resizeP, resizeL;
|
|
610
614
|
for (let i = 0; i < 8; i++) {
|
|
611
|
-
|
|
615
|
+
AroundHelper.toPoint(AroundHelper.directionData[i], bounds, point);
|
|
616
|
+
style = this.getPointStyle((i % 2) ? middlePointsStyle[((i - 1) / 2) % middlePointsStyle.length] : pointsStyle[(i / 2) % pointsStyle.length]);
|
|
612
617
|
resizeP = resizePoints[i], rotateP = rotatePoints[i], resizeL = resizeLines[Math.floor(i / 2)];
|
|
613
618
|
resizeP.set(style);
|
|
614
619
|
resizeP.set(point), rotateP.set(point), resizeL.set(point);
|
|
@@ -680,19 +685,6 @@ class EditBox extends Group {
|
|
|
680
685
|
const { middlePoint } = this.editor.config;
|
|
681
686
|
return middlePoint instanceof Array ? middlePoint : (middlePoint ? [middlePoint] : this.getPointsStyle());
|
|
682
687
|
}
|
|
683
|
-
getDirection8Points(bounds) {
|
|
684
|
-
const { x, y, width, height } = bounds;
|
|
685
|
-
return [
|
|
686
|
-
{ x, y },
|
|
687
|
-
{ x: x + width / 2, y },
|
|
688
|
-
{ x: x + width, y },
|
|
689
|
-
{ x: x + width, y: y + height / 2 },
|
|
690
|
-
{ x: x + width, y: y + height },
|
|
691
|
-
{ x: x + width / 2, y: y + height },
|
|
692
|
-
{ x, y: y + height },
|
|
693
|
-
{ x, y: y + height / 2 }
|
|
694
|
-
];
|
|
695
|
-
}
|
|
696
688
|
onDragStart(e) {
|
|
697
689
|
this.dragging = true;
|
|
698
690
|
if (e.target.name === 'rect')
|
|
@@ -938,7 +930,7 @@ function simulate(editor) {
|
|
|
938
930
|
simulateTarget.reset({ x: (x - worldX) / scaleX, y: (y - worldY) / scaleY, width: width / scaleX, height: height / scaleY });
|
|
939
931
|
}
|
|
940
932
|
|
|
941
|
-
function onTarget(editor) {
|
|
933
|
+
function onTarget(editor, oldValue) {
|
|
942
934
|
const { target } = editor;
|
|
943
935
|
if (target) {
|
|
944
936
|
editor.leafList = target instanceof LeafList ? target : new LeafList(target instanceof Array ? target : target);
|
|
@@ -946,7 +938,7 @@ function onTarget(editor) {
|
|
|
946
938
|
else {
|
|
947
939
|
editor.leafList.reset();
|
|
948
940
|
}
|
|
949
|
-
editor.emitEvent(new EditorEvent(EditorEvent.SELECT, { editor }));
|
|
941
|
+
editor.emitEvent(new EditorEvent(EditorEvent.SELECT, { editor, value: target, oldValue }));
|
|
950
942
|
if (editor.hasTarget) {
|
|
951
943
|
editor.waitLeafer(() => {
|
|
952
944
|
if (editor.multiple)
|
|
@@ -961,8 +953,8 @@ function onTarget(editor) {
|
|
|
961
953
|
editor.removeTargetEvents();
|
|
962
954
|
}
|
|
963
955
|
}
|
|
964
|
-
function onHover(editor) {
|
|
965
|
-
editor.emitEvent(new EditorEvent(EditorEvent.HOVER, { editor }));
|
|
956
|
+
function onHover(editor, oldValue) {
|
|
957
|
+
editor.emitEvent(new EditorEvent(EditorEvent.HOVER, { editor, value: editor.hoverTarget, oldValue }));
|
|
966
958
|
}
|
|
967
959
|
|
|
968
960
|
const order = (a, b) => a.parent.children.indexOf(a) - b.parent.children.indexOf(b);
|
|
@@ -1008,44 +1000,15 @@ const EditorHelper = {
|
|
|
1008
1000
|
toTop(list) {
|
|
1009
1001
|
list.sort(order);
|
|
1010
1002
|
list.forEach(leaf => {
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
if (parent) {
|
|
1014
|
-
const { children } = parent;
|
|
1015
|
-
const top = children.length - 1;
|
|
1016
|
-
const zIndexOfTop = children[top].__.zIndex;
|
|
1017
|
-
const current = children.indexOf(leaf);
|
|
1018
|
-
if (current !== top) {
|
|
1019
|
-
children.splice(current, 1);
|
|
1020
|
-
children.push(leaf);
|
|
1021
|
-
zIndex = zIndexOfTop + 1;
|
|
1022
|
-
}
|
|
1023
|
-
else {
|
|
1024
|
-
zIndex = zIndexOfTop;
|
|
1025
|
-
}
|
|
1026
|
-
leaf.zIndex = zIndex;
|
|
1027
|
-
}
|
|
1003
|
+
if (leaf.parent)
|
|
1004
|
+
leaf.parent.add(leaf);
|
|
1028
1005
|
});
|
|
1029
1006
|
},
|
|
1030
1007
|
toBottom(list) {
|
|
1031
1008
|
list.sort(reverseOrder);
|
|
1032
1009
|
list.forEach(leaf => {
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
if (parent) {
|
|
1036
|
-
const { children } = parent;
|
|
1037
|
-
const zIndexOfBottom = children[0].__.zIndex;
|
|
1038
|
-
const current = children.indexOf(leaf);
|
|
1039
|
-
if (current !== 0) {
|
|
1040
|
-
children.splice(current, 1);
|
|
1041
|
-
children.unshift(leaf);
|
|
1042
|
-
zIndex = zIndexOfBottom - 1;
|
|
1043
|
-
}
|
|
1044
|
-
else {
|
|
1045
|
-
zIndex = zIndexOfBottom;
|
|
1046
|
-
}
|
|
1047
|
-
leaf.zIndex = zIndex;
|
|
1048
|
-
}
|
|
1010
|
+
if (leaf.parent)
|
|
1011
|
+
leaf.parent.addAt(leaf, 0);
|
|
1049
1012
|
});
|
|
1050
1013
|
}
|
|
1051
1014
|
};
|
|
@@ -1206,13 +1169,15 @@ class Editor extends Group {
|
|
|
1206
1169
|
this.editTool.onSkew(event);
|
|
1207
1170
|
this.emitEvent(event);
|
|
1208
1171
|
}
|
|
1209
|
-
group() {
|
|
1172
|
+
group(group) {
|
|
1210
1173
|
if (this.multiple)
|
|
1211
|
-
this.target = EditorHelper.group(this.list, this.element);
|
|
1174
|
+
this.target = EditorHelper.group(this.list, this.element, group);
|
|
1175
|
+
return this.target;
|
|
1212
1176
|
}
|
|
1213
1177
|
ungroup() {
|
|
1214
1178
|
if (this.list.length)
|
|
1215
1179
|
this.target = EditorHelper.ungroup(this.list);
|
|
1180
|
+
return this.list;
|
|
1216
1181
|
}
|
|
1217
1182
|
lock() {
|
|
1218
1183
|
this.list.forEach(leaf => leaf.locked = true);
|
package/dist/editor.esm.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{Event as t,defineKey as e,MatrixHelper as i,UI as s,Paint as o,Group as n,Rect as r,Bounds as a,LeafList as h,PointerEvent as l,DragEvent as d,MoveEvent as c,ZoomEvent as g,PointHelper as u,AroundHelper as f,Box as p,Line as y,Matrix as x,DataHelper as w,RotateEvent as v,MathHelper as k,RenderEvent as m,KeyEvent as _,Creator as b}from"@leafer-ui/core";function E(t,e,i,s){var o,n=arguments.length,r=n<3?e:null===s?s=Object.getOwnPropertyDescriptor(e,i):s;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)r=Reflect.decorate(t,e,i,s);else for(var a=t.length-1;a>=0;a--)(o=t[a])&&(r=(n<3?o(r):n>3?o(e,i,r):o(e,i))||r);return n>3&&r&&Object.defineProperty(e,i,r),r}"function"==typeof SuppressedError&&SuppressedError;class L extends t{constructor(t,e){super(t),e&&Object.assign(this,e)}}L.SELECT="editor.select",L.HOVER="editor.hover";class S extends L{constructor(t,e){super(t,e)}}S.MOVE="editor.move";class T extends L{constructor(t,e){super(t,e)}}T.SCALE="editor.scale";class O extends L{constructor(t,e){super(t,e)}}O.ROTATE="editor.rotate";class P extends L{constructor(t,e){super(t,e)}}function D(t){return(i,s)=>{const o="_"+s;e(i,s,{get(){return this[o]},set(e){this[o]!==e&&(this[o]=e,t(this))}})}}P.SKEW="editor.skew";const A=i.get(),{abs:z}=Math,{copy:R,scale:B}=i;class M extends s{constructor(){super(),this.list=[],this.hittable=!1,this.strokeAlign="center"}setTarget(t,e){const{stroke:i,strokeWidth:s}=e;this.set({stroke:i,strokeWidth:s}),this.target=t}__draw(t,e){const{list:i}=this;if(i.length){let s;const{stroke:n,strokeWidth:r}=this.__,{bounds:a}=e;for(let h=0;h<i.length;h++)if(s=i[h],a&&a.hit(s.__world,e.matrix)){let i;if("scale"===s.__.editSize){const o=z(s.__world.scaleX),n=z(s.__world.scaleY);if(o!==n){R(A,s.__world),B(A,1/o,1/n),t.setWorld(A,e.matrix),t.beginPath(),this.__.strokeWidth=r;const{x:a,y:h,width:l,height:d}=s.__layout.boxBounds;t.rect(a*o,h*n,l*o,d*n),i=!0}}i||(t.setWorld(s.__world,e.matrix),t.beginPath(),s.__.__pathForRender?s.__drawRenderPath(t):s.__drawPathByBox(t),this.__.strokeWidth=r/z(s.__world.scaleX)),"string"==typeof n?o.stroke(n,this,t,e):o.strokes(n,this,t,e)}this.__.strokeWidth=r}}destroy(){this.target=null,super.destroy()}}E([D((function(t){const e=t.target;t.list=e?e instanceof Array?e:[e]:[],t.forceUpdate()}))],M.prototype,"target",void 0);class I extends n{constructor(t){super(t),this.strokeArea=new r({strokeAlign:"center"}),this.fillArea=new r,this.visible=this.hittable=!1,this.addMany(this.fillArea,this.strokeArea)}setStyle(t,e){const{visible:i,stroke:s,strokeWidth:o}=t;this.visible=i,this.strokeArea.reset(Object.assign({stroke:s,strokeWidth:o},e||{})),this.fillArea.reset({visible:!e,fill:s,opacity:.2})}setBounds(t){this.strokeArea.set(t),this.fillArea.set(t)}}var W;!function(t){t[t.No=0]="No",t[t.Yes=1]="Yes",t[t.NoAndSkip=2]="NoAndSkip",t[t.YesAndSkip=3]="YesAndSkip"}(W||(W={}));const{No:Y,Yes:X,NoAndSkip:C,YesAndSkip:F}=W,K={findOne:t=>t.list.find((t=>t.editable)),findBounds(t,e){if(t.__.hittable&&!t.__.locked&&e.hit(t.__world)){if(t.__.editable){if(t.isBranch&&!t.__.hitChildren)return t.__.hitSelf?F:C;if(t.isFrame)return e.includes(t.__layout.boxBounds,t.__world)?F:Y;if(e.hit(t.__layout.boxBounds,t.__world)&&t.__.hitSelf)return X}return Y}return t.isBranch?C:Y}},{findOne:j}=K;class N extends n{get dragging(){return!!this.originList}get running(){return this.editor.hittable&&this.editor.config.selector}get isMoveMode(){return this.app&&this.app.interaction.moveMode}constructor(t){super(),this.hoverStroker=new M,this.targetStroker=new M,this.bounds=new a,this.selectArea=new I,this.__eventIds=[],this.editor=t,this.addMany(this.targetStroker,this.hoverStroker,this.selectArea),this.__listenEvents()}onHover(){const{editor:t}=this;if(!this.running||this.dragging||t.dragging)this.hoverStroker.target=null;else{const{stroke:e,strokeWidth:i,hover:s}=t.config;this.hoverStroker.setTarget(s?this.editor.hoverTarget:null,{stroke:e,strokeWidth:i})}}onSelect(){if(this.running){const{config:t,list:e}=this.editor,{stroke:i,strokeWidth:s}=t;this.targetStroker.setTarget(e,{stroke:i,strokeWidth:Math.max(1,s/2)}),this.hoverStroker.target=null}}update(){this.running&&this.targetStroker.forceUpdate()}onPointerMove(t){if(this.running&&!this.isMoveMode){const e=t.shiftKey?this.findDeepOne(t):j(t.path);this.editor.hoverTarget=this.editor.hasItem(e)?null:e}this.isMoveMode&&(this.editor.hoverTarget=null)}onBeforeDown(t){if(this.running&&!this.isMoveMode&&!t.middle){const e=this.lastDownLeaf=j(t.path);e?(t.shiftKey?this.editor.shiftItem(e):this.editor.target=e,this.editor.updateLayout(),e.leafer.interaction.updateDownData()):this.allow(t.target)&&(t.shiftKey||(this.editor.target=null))}}onTap(t){if(this.running&&t.shiftKey&&!t.middle&&!this.lastDownLeaf){const e=this.findDeepOne(t);e&&this.editor.shiftItem(e)}else this.isMoveMode&&(this.editor.target=null);this.lastDownLeaf=null}onDragStart(t){if(this.running&&this.allowDrag(t)){const{editor:e}=this,{stroke:i,strokeWidth:s,area:o}=e.config,{x:n,y:r}=t.getInner(this);this.bounds.set(n,r),this.selectArea.setStyle({visible:!0,stroke:i,strokeWidth:s,x:n,y:r},o),this.selectArea.setBounds(this.bounds.get()),this.originList=e.leafList.clone()}}onDrag(t){if(this.editor.dragging)this.onDragEnd();else if(this.dragging){const{editor:e}=this,i=t.getInnerTotal(this),s=this.bounds.clone().unsign(),o=new h(e.app.find(K.findBounds,s));if(this.bounds.width=i.x,this.bounds.height=i.y,this.selectArea.setBounds(s.get()),o.length){const t=[];this.originList.forEach((e=>{o.has(e)||t.push(e)})),o.forEach((e=>{this.originList.has(e)||t.push(e)})),e.target=t}else e.target=this.originList,e.leafList.length&&e.update()}}onDragEnd(){this.dragging&&(this.originList=null,this.selectArea.visible=!1)}onAutoMove(t){if(this.dragging){const{x:e,y:i}=t.getLocalMove(this);this.bounds.x+=e,this.bounds.y+=i}}allow(t){return t.leafer!==this.editor.leafer}allowDrag(t){return!(!this.editor.config.boxSelect||t.target.draggable)&&(!this.editor.hasTarget&&this.allow(t.target)||t.shiftKey&&!j(t.path))}findDeepOne(t){const e={exclude:new h(this.editor.editBox.rect)};return j(t.target.leafer.interaction.findPath(t,e))}__listenEvents(){const{editor:t}=this;t.waitLeafer((()=>{const{app:e}=t;e.selector.proxy=t,this.__eventIds=[t.on_(L.HOVER,this.onHover,this),t.on_(L.SELECT,this.onSelect,this),e.on_(l.MOVE,this.onPointerMove,this),e.on_(l.BEFORE_DOWN,this.onBeforeDown,this),e.on_(l.TAP,this.onTap,this),e.on_(d.START,this.onDragStart,this),e.on_(d.DRAG,this.onDrag,this),e.on_(d.END,this.onDragEnd,this),e.on_(c.MOVE,this.onAutoMove,this),e.on_([g.ZOOM,c.MOVE],(()=>{this.editor.hoverTarget=null}))]}))}__removeListenEvents(){this.__eventIds&&(this.off_(this.__eventIds),this.__eventIds.length=0)}destroy(){this.editor=this.originList=this.lastDownLeaf=null,this.__removeListenEvents(),super.destroy()}}var V;!function(t){t[t.topLeft=0]="topLeft",t[t.top=1]="top",t[t.topRight=2]="topRight",t[t.right=3]="right",t[t.bottomRight=4]="bottomRight",t[t.bottom=5]="bottom",t[t.bottomLeft=6]="bottomLeft",t[t.left=7]="left"}(V||(V={}));const{topLeft:G,top:H,topRight:U,right:Z,bottomRight:q,bottom:J,bottomLeft:Q,left:$}=V,{toPoint:tt}=f,et={getScaleData(t,e,i,s,o){let n,r=1,a=1;const{width:h,height:l}=t;o&&(i.x*=2,i.y*=2);const d=(-i.y+l)/l,c=(i.x+h)/h,g=(i.y+l)/l,u=(-i.x+h)/h;switch(e){case H:a=d,n={x:.5,y:1};break;case Z:r=c,n={x:0,y:.5};break;case J:a=g,n={x:.5,y:0};break;case $:r=u,n={x:1,y:.5};break;case G:a=d,r=u,n={x:1,y:1};break;case U:a=d,r=c,n={x:0,y:1};break;case q:a=g,r=c,n={x:0,y:0};break;case Q:a=g,r=u,n={x:1,y:0}}return s&&(1!==r?a=r:r=a),tt(o||n,t,n),{origin:n,scaleX:r,scaleY:a,direction:e,lockRatio:s,around:o}},getRotateData(t,e,i,s,o){let n;switch(e){case G:n={x:1,y:1};break;case U:n={x:0,y:1};break;case q:n={x:0,y:0};break;case Q:n={x:1,y:0};break;default:n={x:.5,y:.5}}return tt(o||n,t,n),{origin:n,rotation:u.getRotation(s,n,i)}},getSkewData(t,e,i,s){let o,n,r=0,a=0;switch(e){case H:n={x:.5,y:0},o={x:.5,y:1},r=1;break;case J:n={x:.5,y:1},o={x:.5,y:0},r=1;break;case $:n={x:0,y:.5},o={x:1,y:.5},a=1;break;case Z:n={x:1,y:.5},o={x:0,y:.5},a=1}const{x:h,y:l,width:d,height:c}=t;n.x=h+n.x*d,n.y=l+n.y*c,tt(s||o,t,o);const g=u.getRotation(n,o,{x:n.x+(r?i.x:0),y:n.y+(a?i.y:0)});return r?r=-g:a=g,{origin:o,skewX:r,skewY:a}},getAround:(t,e)=>e&&!t?f.center:t,getRotateDirection:(t,e,i=8)=>((t=(t+Math.round(e/(360/i)))%i)<0&&(t+=i),t)},{topLeft:it,top:st,topRight:ot,right:nt,bottomRight:rt,bottom:at,bottomLeft:ht,left:lt}=V;function dt(t,e){const{editBox:i}=t,s=i.enterPoint;if(!s||!t.hasTarget||!i.visible)return;let{rotation:o}=i,{resizeCursor:n,rotateCursor:r,resizeable:a,rotateable:h}=t.config;const{direction:l,pointType:d}=s;i.enterPoint=s;const c="resize"===d;if(c&&h&&(e.metaKey||e.ctrlKey||!a)&&(n=r),i.flipped){const{flippedX:t,flippedY:e}=i;gt(n=[...n],t,e),gt(r=[...r],e,t),i.flippedOne&&(o=-o)}const g=et.getRotateDirection(l,o);s.cursor=c?n[g]:r[g]}function ct(t){t.editBox.rect.cursor=t.config.moveCursor}function gt(t,e,i){if(e){const e=t[st],i=t[it],s=t[ot];t[st]=t[at],t[it]=t[ht],t[ot]=t[rt],t[at]=e,t[ht]=i,t[rt]=s}if(i){const e=t[lt],i=t[it],s=t[ht];t[lt]=t[nt],t[it]=t[ot],t[ht]=t[rt],t[nt]=e,t[ot]=i,t[rt]=s}}class ut extends p{}const ft=["top","right","bottom","left"];class pt extends n{get flipped(){return this.flippedX||this.flippedY}get flippedX(){return this.scaleX<0}get flippedY(){return this.scaleY<0}get flippedOne(){return this.scaleX*this.scaleY<0}constructor(t){super(),this.rect=new p({name:"rect",hitFill:"all",hitStroke:"none",strokeAlign:"center",hitRadius:5}),this.circle=new ut({name:"circle",strokeAlign:"outside",around:"center",cursor:"crosshair",hitRadius:5}),this.buttons=new n({around:"center",hitSelf:!1}),this.resizePoints=[],this.rotatePoints=[],this.resizeLines=[],this.__eventIds=[],this.editor=t,this.visible=!1,this.create(),this.__listenEvents()}create(){let t,e,i;const{resizePoints:s,rotatePoints:o,resizeLines:n,rect:r,circle:a,buttons:h}=this,l=[{x:1,y:1},{x:.5,y:1},{x:0,y:1},{x:0,y:.5},{x:0,y:0},{x:.5,y:0},{x:1,y:0},{x:1,y:.5}];for(let r=0;r<8;r++)t=new ut({around:l[r],width:15,height:15,hitFill:"all"}),o.push(t),this.listenPointEvents(t,"rotate",r),r%2&&(e=new ut({name:"resize-line",around:"center",width:10,height:10,hitFill:"all"}),n.push(e),this.listenPointEvents(e,"resize",r)),i=new ut({name:"resize-point",around:"center",strokeAlign:"outside",hitRadius:5}),s.push(i),this.listenPointEvents(i,"resize",r);h.add(a),this.listenPointEvents(a,"rotate",2),this.addMany(...o,r,h,...n,...s)}update(t){const{config:e,list:i}=this.editor,{width:s,height:o}=t,{rect:n,circle:r,resizePoints:a,rotatePoints:h,resizeLines:l}=this,{middlePoint:d,resizeable:c,rotateable:g,stroke:u,strokeWidth:f}=e,p=this.getDirection8Points(t),y=this.getPointsStyle(),x=this.getMiddlePointsStyle();let w,v,k,m,_;this.visible=i[0]&&!i[0].locked;for(let t=0;t<8;t++)w=p[t],v=this.getPointStyle(t%2?x[(t-1)/2%x.length]:y[t/2%y.length]),m=a[t],k=h[t],_=l[Math.floor(t/2)],m.set(v),m.set(w),k.set(w),_.set(w),m.visible=_.visible=c||g,k.visible=g&&c,t%2?(m.visible=k.visible=!!d,(t+1)/2%2?(_.width=s,m.width>s-30&&(m.visible=!1)):(_.height=o,m.rotation=90,m.width>o-30&&(m.visible=!1))):m.rotation=t/2*90;r.visible=g&&!!e.rotatePoint,r.set(this.getPointStyle(e.rotatePoint||y[0])),n.set(Object.assign({stroke:u,strokeWidth:f},e.rect||{})),n.set(Object.assign(Object.assign({},t),{visible:!0})),this.layoutButtons()}layoutButtons(){const{buttons:t,resizePoints:e}=this,{buttonsDirection:i,buttonsFixed:s,buttonsMargin:o,middlePoint:n}=this.editor.config,{flippedX:r,flippedY:a}=this;let h=ft.indexOf(i);(h%2&&r||(h+1)%2&&a)&&s&&(h=(h+2)%4);const l=s?et.getRotateDirection(h,this.flippedOne?this.rotation:-this.rotation,4):h,d=e[2*l+1],c=l%2,g=l&&3!==l?1:-1,u=(o+(h%2?(n?d.width:0)+t.boxBounds.width:(n?d.height:0)+t.boxBounds.height)/2)*g;c?(t.x=d.x+u,t.y=d.y):(t.x=d.x,t.y=d.y+u),s&&(t.rotation=90*(l-h),t.scaleX=r?-1:1,t.scaleY=a?-1:1)}getPointStyle(t){const{stroke:e,strokeWidth:i,pointFill:s,pointSize:o,pointRadius:n}=this.editor.config,r={fill:s,stroke:e,strokeWidth:i,width:o,height:o,cornerRadius:n};return t?Object.assign(r,t):r}getPointsStyle(){const{point:t}=this.editor.config;return t instanceof Array?t:[t]}getMiddlePointsStyle(){const{middlePoint:t}=this.editor.config;return t instanceof Array?t:t?[t]:this.getPointsStyle()}getDirection8Points(t){const{x:e,y:i,width:s,height:o}=t;return[{x:e,y:i},{x:e+s/2,y:i},{x:e+s,y:i},{x:e+s,y:i+o/2},{x:e+s,y:i+o},{x:e+s/2,y:i+o},{x:e,y:i+o},{x:e,y:i+o/2}]}onDragStart(t){this.dragging=!0,"rect"===t.target.name&&(this.editor.opacity=this.editor.config.hideOnMove?0:1)}onDragEnd(t){this.dragging=!1,"rect"===t.target.name&&(this.editor.opacity=1)}onDrag(t){const{editor:e}=this;"rotate"===t.current.pointType||t.metaKey||t.ctrlKey||!e.config.resizeable?e.config.rotateable&&e.onRotate(t):e.onScale(t)}onArrow(t){if(this.editor.hasTarget){const e={x:0,y:0},i=t.shiftKey?10:1;switch(t.code){case"ArrowDown":e.y=i;break;case"ArrowUp":e.y=-i;break;case"ArrowLeft":e.x=-i;break;case"ArrowRight":e.x=i}(e.x||e.y)&&this.editor.move(e.x,e.y)}}onDoubleClick(){const{editor:t}=this;t.single&&t.element.isBranch}listenPointEvents(t,e,i){const{editor:s}=this;t.direction=i,t.pointType=e,t.on_(d.START,this.onDragStart,this),t.on_(d.DRAG,this.onDrag,this),t.on_(d.END,this.onDragEnd,this),t.on_(l.LEAVE,(()=>this.enterPoint=null)),"circle"!==t.name&&t.on_(l.ENTER,(e=>{this.enterPoint=t,dt(s,e)}))}__listenEvents(){const{rect:t,editor:e}=this;this.__eventIds=[e.on_(L.SELECT,(()=>{this.visible=e.hasTarget})),t.on_(d.START,this.onDragStart,this),t.on_(d.DRAG,e.onMove,e),t.on_(d.END,this.onDragEnd,this),t.on_(l.ENTER,(()=>ct(e))),t.on_(l.DOUBLE_CLICK,this.onDoubleClick,this)]}__removeListenEvents(){this.off_(this.__eventIds),this.__eventIds.length=0}destroy(){this.editor=null,this.__removeListenEvents(),super.destroy()}}const yt={editSize:"auto",stroke:"#836DFF",strokeWidth:2,pointFill:"#FFFFFF",pointSize:8,pointRadius:16,rotateGap:45,buttonsDirection:"bottom",buttonsMargin:12,moveCursor:"move",resizeCursor:["nwse-resize","ns-resize","nesw-resize","ew-resize","nwse-resize","ns-resize","nesw-resize","ew-resize"],rotateCursor:["ne-resize","e-resize","se-resize","s-resize","sw-resize","w-resize","nw-resize","n-resize"],selector:!0,hover:!0,boxSelect:!0,resizeable:!0,rotateable:!0,skewable:!0};class xt{constructor(){this.tag="EditTool"}onMove(t){const{moveX:e,moveY:i,editor:s}=t,{app:o,list:n}=s;o.lockLayout(),n.forEach((t=>{const s=t.getLocalPoint({x:e,y:i},null,!0);t.move(s.x,s.y)})),o.unlockLayout()}onScale(t){const{scaleX:e,scaleY:i,transform:s,worldOrigin:o,editor:n}=t,{app:r,list:a}=n;r.lockLayout(),a.forEach((t=>{const r="size"===n.getEditSize(t);s?t.transform(s,r):t.scaleOf(t.getInnerPoint(o),e,i,r)})),r.unlockLayout()}onRotate(t){const{rotation:e,worldOrigin:i,editor:s}=t,{app:o,list:n}=s;o.lockLayout(),n.forEach((t=>{t.rotateOf(t.getInnerPoint(i),e)})),o.unlockLayout()}onSkew(t){const{skewX:e,skewY:i,transform:s,worldOrigin:o,editor:n}=t,{app:r,list:a}=n;r.lockLayout(),a.forEach((t=>{const r="size"===n.getEditSize(t);s?t.transform(s,r):t.skewOf(t.getInnerPoint(o),e,i,r)})),r.unlockLayout()}update(t){const{simulateTarget:e,element:i}=t;t.multiple&&e.parent.updateLayout();const{x:s,y:o,scaleX:n,scaleY:r,rotation:a,skewX:h,skewY:l,width:d,height:c}=i.getLayoutBounds("box",t,!0);t.editBox.set({x:s,y:o,scaleX:n,scaleY:r,rotation:a,skewX:h,skewY:l}),t.editBox.update({x:0,y:0,width:d,height:c})}}xt.list=[];const{left:wt,right:vt}=V;class kt extends xt{constructor(){super(...arguments),this.tag="LineEditTool",this.scaleOfEvent=!0}onScaleWithDrag(t){const{drag:e,direction:i,lockRatio:s,around:o}=t,n=t.target,r={x:0,y:0},{toPoint:a}=n;n.rotation=0;let{x:h,y:l}=e.getInnerMove(n);s&&(Math.abs(h)>Math.abs(l)?l=0:h=0),i===wt?(r.x+=h,r.y+=l,o&&(a.x-=h,a.y-=l)):(o&&(r.x-=h,r.y-=l),a.x+=h,a.y+=l),n.getLocalPointByInner(r,null,null,!0),n.getLocalPointByInner(a,null,null,!0),n.x=r.x,n.y=r.y,n.getInnerPointByLocal(a,null,null,!0),n.toPoint=a}onSkew(t){}update(t){const{rotatePoints:e,resizeLines:i,resizePoints:s}=t.editBox;super.update(t);for(let t=0;t<8;t++)t<4&&(i[t].visible=!1),s[t].visible=e[t].visible=t===wt||t===vt}}const mt=(t,e)=>t.parent.children.indexOf(t)-e.parent.children.indexOf(e),_t=(t,e)=>e.parent.children.indexOf(e)-t.parent.children.indexOf(t),bt={group(t,e,i){t.sort(_t);const{app:s,parent:o}=t[0];i||(i=new n),o.addAt(i,o.children.indexOf(t[0])),t.sort(mt);const r=new x(e.worldTransform);return r.divideParent(o.worldTransform),i.setTransform(r),i.editable=!0,i.hitChildren=!1,s.lockLayout(),t.forEach((t=>t.dropTo(i))),s.unlockLayout(),i},ungroup(t){const{app:e}=t[0],i=[];return e.lockLayout(),t.forEach((t=>{if(t.isBranch){const{parent:e,children:s}=t;for(;s.length;)i.push(s[0]),s[0].dropTo(e,e.children.indexOf(t));t.remove()}else i.push(t)})),e.unlockLayout(),i},toTop(t){t.sort(mt),t.forEach((t=>{let e;const{parent:i}=t;if(i){const{children:s}=i,o=s.length-1,n=s[o].__.zIndex,r=s.indexOf(t);r!==o?(s.splice(r,1),s.push(t),e=n+1):e=n,t.zIndex=e}}))},toBottom(t){t.sort(_t),t.forEach((t=>{let e;const{parent:i}=t;if(i){const{children:s}=i,o=s[0].__.zIndex,n=s.indexOf(t);0!==n?(s.splice(n,1),s.unshift(t),e=o-1):e=o,t.zIndex=e}}))}};class Et extends n{get list(){return this.leafList.list}get hasTarget(){return!!this.list.length}get multiple(){return this.list.length>1}get single(){return 1===this.list.length}get element(){return this.multiple?this.simulateTarget:this.list[0]}get buttons(){return this.editBox.buttons}get dragging(){return this.editBox.dragging}constructor(t,e){super(e),this.config=yt,this.leafList=new h,this.simulateTarget=new r({visible:!1}),this.editBox=new pt(this),this.selector=new N(this),this.targetEventIds=[],t&&(this.config=w.default(t,this.config)),this.addMany(this.selector,this.editBox)}hasItem(t){return this.leafList.has(t)}addItem(t){this.hasItem(t)||t.locked||(this.leafList.add(t),this.target=this.leafList.list)}removeItem(t){this.hasItem(t)&&(this.leafList.remove(t),this.target=this.leafList.list)}shiftItem(t){this.hasItem(t)?this.removeItem(t):this.addItem(t)}update(){this.target&&(this.editTool.update(this),this.selector.update())}updateEditTool(){this.editTool=function(t){if(1===t.length){const e=t[0];return e instanceof y&&!e.points?new kt:new xt}return new xt}(this.list)}getEditSize(t){let{editSize:e}=this.config;return"auto"===e?t.editSize:e}onMove(t){const e=t.getLocalMove(this.element);t.shiftKey&&(Math.abs(e.x)>Math.abs(e.y)?e.y=0:e.x=0),this.move(e.x,e.y)}onScale(t){const{element:e}=this,{direction:i}=t.current;let{around:s,lockRatio:o}=this.config;t.shiftKey&&(o=!0);const n=et.getScaleData(e.boxBounds,i,t.getInnerMove(e),o,et.getAround(s,t.altKey));this.editTool.onScaleWithDrag?(n.drag=t,this.scaleWithDrag(n)):this.scaleOf(n.origin,n.scaleX,n.scaleY)}onRotate(t){const{skewable:e,around:i,rotateGap:s}=this.config,{direction:o,name:n}=t.current;if(e&&"resize-line"===n)return this.onSkew(t);const{element:r,editBox:a}=this;let h,l;if(t instanceof v)l=t.rotation,h=r.getInnerPoint(t);else{const e={x:t.x-t.moveX,y:t.y-t.moveY},s=et.getRotateData(r.boxBounds,o,t.getInner(r),r.getInnerPoint(e),t.shiftKey?null:i||"center");l=s.rotation,h=s.origin}l=k.getGapRotation(l,s,r.rotation),l&&(a.flippedOne&&(l=-l),this.rotateOf(h,l))}onSkew(t){const{element:e}=this,{around:i}=this.config,{origin:s,skewX:o,skewY:n}=et.getSkewData(e.boxBounds,t.current.direction,t.getInnerMove(e),et.getAround(i,t.altKey));(o||n)&&this.skewOf(s,o,n)}move(t,e){const{element:i}=this,s=i.getWorldPointByLocal({x:t,y:e},null,!0),o=new S(S.MOVE,{target:i,editor:this,moveX:s.x,moveY:s.y});this.editTool.onMove(o),this.emitEvent(o),this.multiple&&i.move(t,e)}scaleWithDrag(t){const{element:e}=this,i=e.getWorldPoint(t.origin),s=new T(T.SCALE,Object.assign(Object.assign({},t),{target:e,editor:this,worldOrigin:i}));this.editTool.onScaleWithDrag(s),this.emitEvent(s)}scaleOf(t,e,i=e,s){const{element:o}=this,n=o.getWorldPoint(t);let r;if(this.multiple){const s=Object.assign({},o.localTransform);o.scaleOf(t,e,i),r=new x(o.localTransform).divide(s)}const a=new T(T.SCALE,{target:o,editor:this,worldOrigin:n,scaleX:e,scaleY:i,transform:r});this.editTool.onScale(a),this.emitEvent(a)}rotateOf(t,e){const{element:i}=this,s=i.getWorldPoint(t),o=new O(O.ROTATE,{target:i,editor:this,worldOrigin:s,rotation:e});this.editTool.onRotate(o),this.emitEvent(o),this.multiple&&i.rotateOf(t,e)}skewOf(t,e,i=0,s){const{element:o}=this,n=o.getWorldPoint(t);let r;if(this.multiple){const s=Object.assign({},o.localTransform);o.skewOf(t,e,i),r=new x(o.localTransform).divide(s)}const a=new P(P.SKEW,{target:o,editor:this,skewX:e,skewY:i,transform:r,worldOrigin:n});this.editTool.onSkew(a),this.emitEvent(a)}group(){this.multiple&&(this.target=bt.group(this.list,this.element))}ungroup(){this.list.length&&(this.target=bt.ungroup(this.list))}lock(){this.list.forEach((t=>t.locked=!0)),this.update()}unlock(){this.list.forEach((t=>t.locked=!1)),this.update()}toTop(){this.list.length&&(bt.toTop(this.list),this.leafList.update())}toBottom(){this.list.length&&(bt.toBottom(this.list),this.leafList.update())}listenTargetEvents(){if(!this.targetEventIds.length){const{leafer:t}=this.list[0];this.targetEventIds=[t.on_(m.START,this.update,this),t.on_([_.HOLD,_.UP],(t=>{dt(this,t)})),t.on_(_.DOWN,this.editBox.onArrow,this.editBox)]}}removeTargetEvents(){const{targetEventIds:t}=this;t.length&&(this.off_(t),t.length=0)}destroy(){this.destroyed||(this.simulateTarget.destroy(),this.target=this.hoverTarget=this.simulateTarget=null,super.destroy())}}E([D((function(t){t.emitEvent(new L(L.HOVER,{editor:t}))}))],Et.prototype,"hoverTarget",void 0),E([D((function(t){const{target:e}=t;e?t.leafList=e instanceof h?e:new h(e):t.leafList.reset(),t.emitEvent(new L(L.SELECT,{editor:t})),t.hasTarget?t.waitLeafer((()=>{t.multiple&&function(t){const{simulateTarget:e,leafList:i}=t,{x:s,y:o,width:n,height:r}=(new a).setListWithFn(i.list,(t=>t.worldBoxBounds)),h=e.parent=i.list[0].leafer.zoomLayer,{scaleX:l,scaleY:d,e:c,f:g}=h.__world;e.reset({x:(s-c)/l,y:(o-g)/d,width:n/l,height:r/d})}(t),ct(t),t.updateEditTool(),t.update(),t.listenTargetEvents()})):t.removeTargetEvents()}))],Et.prototype,"target",void 0),b.editor=function(t){return new Et(t)};export{pt as EditBox,et as EditDataHelper,ut as EditPoint,N as EditSelect,K as EditSelectHelper,xt as EditTool,Et as Editor,L as EditorEvent,bt as EditorHelper,S as EditorMoveEvent,O as EditorRotateEvent,T as EditorScaleEvent,P as EditorSkewEvent,kt as LineEditTool,I as SelectArea,M as Stroker};
|
|
1
|
+
import{Event as t,defineKey as e,MatrixHelper as i,UI as s,Paint as o,Group as n,Rect as r,Bounds as a,LeafList as h,PointerEvent as l,DragEvent as d,MoveEvent as c,ZoomEvent as g,PointHelper as u,AroundHelper as f,Box as p,Line as y,Matrix as w,DataHelper as v,RotateEvent as k,MathHelper as x,RenderEvent as m,KeyEvent as b,Creator as _}from"@leafer-ui/core";function E(t,e,i,s){var o,n=arguments.length,r=n<3?e:null===s?s=Object.getOwnPropertyDescriptor(e,i):s;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)r=Reflect.decorate(t,e,i,s);else for(var a=t.length-1;a>=0;a--)(o=t[a])&&(r=(n<3?o(r):n>3?o(e,i,r):o(e,i))||r);return n>3&&r&&Object.defineProperty(e,i,r),r}"function"==typeof SuppressedError&&SuppressedError;class L extends t{constructor(t,e){super(t),e&&Object.assign(this,e)}}L.SELECT="editor.select",L.HOVER="editor.hover";class S extends L{constructor(t,e){super(t,e)}}S.MOVE="editor.move";class T extends L{constructor(t,e){super(t,e)}}T.SCALE="editor.scale";class O extends L{constructor(t,e){super(t,e)}}O.ROTATE="editor.rotate";class P extends L{constructor(t,e){super(t,e)}}function D(t){return(i,s)=>{const o="_"+s;e(i,s,{get(){return this[o]},set(e){const i=this[o];i!==e&&(this[o]=e,t(this,i))}})}}P.SKEW="editor.skew";const A=i.get(),{abs:R}=Math,{copy:z,scale:B}=i;class M extends s{constructor(){super(),this.list=[],this.hittable=!1,this.strokeAlign="center"}setTarget(t,e){const{stroke:i,strokeWidth:s}=e;this.set({stroke:i,strokeWidth:s}),this.target=t}__draw(t,e){const{list:i}=this;if(i.length){let s;const{stroke:n,strokeWidth:r}=this.__,{bounds:a}=e;for(let h=0;h<i.length;h++)if(s=i[h],a&&a.hit(s.__world,e.matrix)){let i;if("scale"===s.__.editSize){const o=R(s.__world.scaleX),n=R(s.__world.scaleY);if(o!==n){z(A,s.__world),B(A,1/o,1/n),t.setWorld(A,e.matrix),t.beginPath(),this.__.strokeWidth=r;const{x:a,y:h,width:l,height:d}=s.__layout.boxBounds;t.rect(a*o,h*n,l*o,d*n),i=!0}}i||(t.setWorld(s.__world,e.matrix),t.beginPath(),s.__.__pathForRender?s.__drawRenderPath(t):s.__drawPathByBox(t),this.__.strokeWidth=r/R(s.__world.scaleX)),"string"==typeof n?o.stroke(n,this,t,e):o.strokes(n,this,t,e)}this.__.strokeWidth=r}}destroy(){this.target=null,super.destroy()}}E([D((function(t){const e=t.target;t.list=e?e instanceof Array?e:[e]:[],t.forceUpdate()}))],M.prototype,"target",void 0);class I extends n{constructor(t){super(t),this.strokeArea=new r({strokeAlign:"center"}),this.fillArea=new r,this.visible=this.hittable=!1,this.addMany(this.fillArea,this.strokeArea)}setStyle(t,e){const{visible:i,stroke:s,strokeWidth:o}=t;this.visible=i,this.strokeArea.reset(Object.assign({stroke:s,strokeWidth:o},e||{})),this.fillArea.reset({visible:!e,fill:s,opacity:.2})}setBounds(t){this.strokeArea.set(t),this.fillArea.set(t)}}var W;!function(t){t[t.No=0]="No",t[t.Yes=1]="Yes",t[t.NoAndSkip=2]="NoAndSkip",t[t.YesAndSkip=3]="YesAndSkip"}(W||(W={}));const{No:Y,Yes:X,NoAndSkip:C,YesAndSkip:F}=W,K={findOne:t=>t.list.find((t=>t.editable)),findBounds(t,e){if(t.__.hittable&&!t.__.locked&&e.hit(t.__world)){if(t.__.editable){if(t.isBranch&&!t.__.hitChildren)return t.__.hitSelf?F:C;if(t.isFrame)return e.includes(t.__layout.boxBounds,t.__world)?F:Y;if(e.hit(t.__layout.boxBounds,t.__world)&&t.__.hitSelf)return X}return Y}return t.isBranch?C:Y}},{findOne:j}=K;class N extends n{get dragging(){return!!this.originList}get running(){return this.editor.hittable&&this.editor.config.selector}get isMoveMode(){return this.app&&this.app.interaction.moveMode}constructor(t){super(),this.hoverStroker=new M,this.targetStroker=new M,this.bounds=new a,this.selectArea=new I,this.__eventIds=[],this.editor=t,this.addMany(this.targetStroker,this.hoverStroker,this.selectArea),this.__listenEvents()}onHover(){const{editor:t}=this;if(!this.running||this.dragging||t.dragging)this.hoverStroker.target=null;else{const{stroke:e,strokeWidth:i,hover:s}=t.config;this.hoverStroker.setTarget(s?this.editor.hoverTarget:null,{stroke:e,strokeWidth:i})}}onSelect(){if(this.running){const{config:t,list:e}=this.editor,{stroke:i,strokeWidth:s}=t;this.targetStroker.setTarget(e,{stroke:i,strokeWidth:Math.max(1,s/2)}),this.hoverStroker.target=null}}update(){this.running&&this.targetStroker.forceUpdate()}onPointerMove(t){if(this.running&&!this.isMoveMode){const e=t.shiftKey?this.findDeepOne(t):j(t.path);this.editor.hoverTarget=this.editor.hasItem(e)?null:e}this.isMoveMode&&(this.editor.hoverTarget=null)}onBeforeDown(t){if(this.running&&!this.isMoveMode&&!t.middle){const e=this.lastDownLeaf=j(t.path);e?(t.shiftKey?this.editor.shiftItem(e):this.editor.target=e,this.editor.updateLayout(),e.leafer.interaction.updateDownData()):this.allow(t.target)&&(t.shiftKey||(this.editor.target=null))}}onTap(t){if(this.running&&t.shiftKey&&!t.middle&&!this.lastDownLeaf){const e=this.findDeepOne(t);e&&this.editor.shiftItem(e)}else this.isMoveMode&&(this.editor.target=null);this.lastDownLeaf=null}onDragStart(t){if(this.running&&this.allowDrag(t)){const{editor:e}=this,{stroke:i,strokeWidth:s,area:o}=e.config,{x:n,y:r}=t.getInner(this);this.bounds.set(n,r),this.selectArea.setStyle({visible:!0,stroke:i,strokeWidth:s,x:n,y:r},o),this.selectArea.setBounds(this.bounds.get()),this.originList=e.leafList.clone()}}onDrag(t){if(this.editor.dragging)this.onDragEnd();else if(this.dragging){const{editor:e}=this,i=t.getInnerTotal(this),s=this.bounds.clone().unsign(),o=new h(e.app.find(K.findBounds,s));if(this.bounds.width=i.x,this.bounds.height=i.y,this.selectArea.setBounds(s.get()),o.length){const t=[];this.originList.forEach((e=>{o.has(e)||t.push(e)})),o.forEach((e=>{this.originList.has(e)||t.push(e)})),(t.length!==e.list.length||e.list.some(((e,i)=>e!==t[i])))&&(e.target=t)}else e.target=this.originList.list,e.leafList.length&&e.update()}}onDragEnd(){this.dragging&&(this.originList=null,this.selectArea.visible=!1)}onAutoMove(t){if(this.dragging){const{x:e,y:i}=t.getLocalMove(this);this.bounds.x+=e,this.bounds.y+=i}}allow(t){return t.leafer!==this.editor.leafer}allowDrag(t){return!(!this.editor.config.boxSelect||t.target.draggable)&&(!this.editor.hasTarget&&this.allow(t.target)||t.shiftKey&&!j(t.path))}findDeepOne(t){const e={exclude:new h(this.editor.editBox.rect)};return j(t.target.leafer.interaction.findPath(t,e))}__listenEvents(){const{editor:t}=this;t.waitLeafer((()=>{const{app:e}=t;e.selector.proxy=t,this.__eventIds=[t.on_(L.HOVER,this.onHover,this),t.on_(L.SELECT,this.onSelect,this),e.on_(l.MOVE,this.onPointerMove,this),e.on_(l.BEFORE_DOWN,this.onBeforeDown,this),e.on_(l.TAP,this.onTap,this),e.on_(d.START,this.onDragStart,this),e.on_(d.DRAG,this.onDrag,this),e.on_(d.END,this.onDragEnd,this),e.on_(c.MOVE,this.onAutoMove,this),e.on_([g.ZOOM,c.MOVE],(()=>{this.editor.hoverTarget=null}))]}))}__removeListenEvents(){this.__eventIds&&(this.off_(this.__eventIds),this.__eventIds.length=0)}destroy(){this.editor=this.originList=this.lastDownLeaf=null,this.__removeListenEvents(),super.destroy()}}var V;!function(t){t[t.topLeft=0]="topLeft",t[t.top=1]="top",t[t.topRight=2]="topRight",t[t.right=3]="right",t[t.bottomRight=4]="bottomRight",t[t.bottom=5]="bottom",t[t.bottomLeft=6]="bottomLeft",t[t.left=7]="left"}(V||(V={}));const{topLeft:G,top:H,topRight:U,right:Z,bottomRight:q,bottom:J,bottomLeft:Q,left:$}=V,{toPoint:tt}=f,et={getScaleData(t,e,i,s,o){let n,r=1,a=1;const{width:h,height:l}=t;o&&(i.x*=2,i.y*=2);const d=(-i.y+l)/l,c=(i.x+h)/h,g=(i.y+l)/l,u=(-i.x+h)/h;switch(e){case H:a=d,n={x:.5,y:1};break;case Z:r=c,n={x:0,y:.5};break;case J:a=g,n={x:.5,y:0};break;case $:r=u,n={x:1,y:.5};break;case G:a=d,r=u,n={x:1,y:1};break;case U:a=d,r=c,n={x:0,y:1};break;case q:a=g,r=c,n={x:0,y:0};break;case Q:a=g,r=u,n={x:1,y:0}}return s&&(1!==r?a=r:r=a),tt(o||n,t,n),{origin:n,scaleX:r,scaleY:a,direction:e,lockRatio:s,around:o}},getRotateData(t,e,i,s,o){let n;switch(e){case G:n={x:1,y:1};break;case U:n={x:0,y:1};break;case q:n={x:0,y:0};break;case Q:n={x:1,y:0};break;default:n={x:.5,y:.5}}return tt(o||n,t,n),{origin:n,rotation:u.getRotation(s,n,i)}},getSkewData(t,e,i,s){let o,n,r=0,a=0;switch(e){case H:n={x:.5,y:0},o={x:.5,y:1},r=1;break;case J:n={x:.5,y:1},o={x:.5,y:0},r=1;break;case $:n={x:0,y:.5},o={x:1,y:.5},a=1;break;case Z:n={x:1,y:.5},o={x:0,y:.5},a=1}const{x:h,y:l,width:d,height:c}=t;n.x=h+n.x*d,n.y=l+n.y*c,tt(s||o,t,o);const g=u.getRotation(n,o,{x:n.x+(r?i.x:0),y:n.y+(a?i.y:0)});return r?r=-g:a=g,{origin:o,skewX:r,skewY:a}},getAround:(t,e)=>e&&!t?"center":t,getRotateDirection:(t,e,i=8)=>((t=(t+Math.round(e/(360/i)))%i)<0&&(t+=i),t)},{topLeft:it,top:st,topRight:ot,right:nt,bottomRight:rt,bottom:at,bottomLeft:ht,left:lt}=V;function dt(t,e){const{editBox:i}=t,s=i.enterPoint;if(!s||!t.hasTarget||!i.visible)return;let{rotation:o}=i,{resizeCursor:n,rotateCursor:r,resizeable:a,rotateable:h}=t.config;const{direction:l,pointType:d}=s;i.enterPoint=s;const c="resize"===d;if(c&&h&&(e.metaKey||e.ctrlKey||!a)&&(n=r),i.flipped){const{flippedX:t,flippedY:e}=i;gt(n=[...n],t,e),gt(r=[...r],e,t),i.flippedOne&&(o=-o)}const g=et.getRotateDirection(l,o);s.cursor=c?n[g]:r[g]}function ct(t){t.editBox.rect.cursor=t.config.moveCursor}function gt(t,e,i){if(e){const e=t[st],i=t[it],s=t[ot];t[st]=t[at],t[it]=t[ht],t[ot]=t[rt],t[at]=e,t[ht]=i,t[rt]=s}if(i){const e=t[lt],i=t[it],s=t[ht];t[lt]=t[nt],t[it]=t[ot],t[ht]=t[rt],t[nt]=e,t[ot]=i,t[rt]=s}}class ut extends p{}const ft=["top","right","bottom","left"];class pt extends n{get flipped(){return this.flippedX||this.flippedY}get flippedX(){return this.scaleX<0}get flippedY(){return this.scaleY<0}get flippedOne(){return this.scaleX*this.scaleY<0}constructor(t){super(),this.rect=new p({name:"rect",hitFill:"all",hitStroke:"none",strokeAlign:"center",hitRadius:5}),this.circle=new ut({name:"circle",strokeAlign:"outside",around:"center",cursor:"crosshair",hitRadius:5}),this.buttons=new n({around:"center",hitSelf:!1}),this.resizePoints=[],this.rotatePoints=[],this.resizeLines=[],this.__eventIds=[],this.editor=t,this.visible=!1,this.create(),this.__listenEvents()}create(){let t,e,i;const{resizePoints:s,rotatePoints:o,resizeLines:n,rect:r,circle:a,buttons:h}=this,l=[{x:1,y:1},{x:.5,y:1},{x:0,y:1},{x:0,y:.5},{x:0,y:0},{x:.5,y:0},{x:1,y:0},{x:1,y:.5}];for(let r=0;r<8;r++)t=new ut({around:l[r],width:15,height:15,hitFill:"all"}),o.push(t),this.listenPointEvents(t,"rotate",r),r%2&&(e=new ut({name:"resize-line",around:"center",width:10,height:10,hitFill:"all"}),n.push(e),this.listenPointEvents(e,"resize",r)),i=new ut({name:"resize-point",around:"center",strokeAlign:"outside",hitRadius:5}),s.push(i),this.listenPointEvents(i,"resize",r);h.add(a),this.listenPointEvents(a,"rotate",2),this.addMany(...o,r,h,...n,...s)}update(t){const{config:e,list:i}=this.editor,{width:s,height:o}=t,{rect:n,circle:r,resizePoints:a,rotatePoints:h,resizeLines:l}=this,{middlePoint:d,resizeable:c,rotateable:g,stroke:u,strokeWidth:p}=e,y=this.getPointsStyle(),w=this.getMiddlePointsStyle();this.visible=i[0]&&!i[0].locked;let v,k,x,m,b={};for(let e=0;e<8;e++)f.toPoint(f.directionData[e],t,b),v=this.getPointStyle(e%2?w[(e-1)/2%w.length]:y[e/2%y.length]),x=a[e],k=h[e],m=l[Math.floor(e/2)],x.set(v),x.set(b),k.set(b),m.set(b),x.visible=m.visible=c||g,k.visible=g&&c,e%2?(x.visible=k.visible=!!d,(e+1)/2%2?(m.width=s,x.width>s-30&&(x.visible=!1)):(m.height=o,x.rotation=90,x.width>o-30&&(x.visible=!1))):x.rotation=e/2*90;r.visible=g&&!!e.rotatePoint,r.set(this.getPointStyle(e.rotatePoint||y[0])),n.set(Object.assign({stroke:u,strokeWidth:p},e.rect||{})),n.set(Object.assign(Object.assign({},t),{visible:!0})),this.layoutButtons()}layoutButtons(){const{buttons:t,resizePoints:e}=this,{buttonsDirection:i,buttonsFixed:s,buttonsMargin:o,middlePoint:n}=this.editor.config,{flippedX:r,flippedY:a}=this;let h=ft.indexOf(i);(h%2&&r||(h+1)%2&&a)&&s&&(h=(h+2)%4);const l=s?et.getRotateDirection(h,this.flippedOne?this.rotation:-this.rotation,4):h,d=e[2*l+1],c=l%2,g=l&&3!==l?1:-1,u=(o+(h%2?(n?d.width:0)+t.boxBounds.width:(n?d.height:0)+t.boxBounds.height)/2)*g;c?(t.x=d.x+u,t.y=d.y):(t.x=d.x,t.y=d.y+u),s&&(t.rotation=90*(l-h),t.scaleX=r?-1:1,t.scaleY=a?-1:1)}getPointStyle(t){const{stroke:e,strokeWidth:i,pointFill:s,pointSize:o,pointRadius:n}=this.editor.config,r={fill:s,stroke:e,strokeWidth:i,width:o,height:o,cornerRadius:n};return t?Object.assign(r,t):r}getPointsStyle(){const{point:t}=this.editor.config;return t instanceof Array?t:[t]}getMiddlePointsStyle(){const{middlePoint:t}=this.editor.config;return t instanceof Array?t:t?[t]:this.getPointsStyle()}onDragStart(t){this.dragging=!0,"rect"===t.target.name&&(this.editor.opacity=this.editor.config.hideOnMove?0:1)}onDragEnd(t){this.dragging=!1,"rect"===t.target.name&&(this.editor.opacity=1)}onDrag(t){const{editor:e}=this;"rotate"===t.current.pointType||t.metaKey||t.ctrlKey||!e.config.resizeable?e.config.rotateable&&e.onRotate(t):e.onScale(t)}onArrow(t){if(this.editor.hasTarget){const e={x:0,y:0},i=t.shiftKey?10:1;switch(t.code){case"ArrowDown":e.y=i;break;case"ArrowUp":e.y=-i;break;case"ArrowLeft":e.x=-i;break;case"ArrowRight":e.x=i}(e.x||e.y)&&this.editor.move(e.x,e.y)}}onDoubleClick(){const{editor:t}=this;t.single&&t.element.isBranch}listenPointEvents(t,e,i){const{editor:s}=this;t.direction=i,t.pointType=e,t.on_(d.START,this.onDragStart,this),t.on_(d.DRAG,this.onDrag,this),t.on_(d.END,this.onDragEnd,this),t.on_(l.LEAVE,(()=>this.enterPoint=null)),"circle"!==t.name&&t.on_(l.ENTER,(e=>{this.enterPoint=t,dt(s,e)}))}__listenEvents(){const{rect:t,editor:e}=this;this.__eventIds=[e.on_(L.SELECT,(()=>{this.visible=e.hasTarget})),t.on_(d.START,this.onDragStart,this),t.on_(d.DRAG,e.onMove,e),t.on_(d.END,this.onDragEnd,this),t.on_(l.ENTER,(()=>ct(e))),t.on_(l.DOUBLE_CLICK,this.onDoubleClick,this)]}__removeListenEvents(){this.off_(this.__eventIds),this.__eventIds.length=0}destroy(){this.editor=null,this.__removeListenEvents(),super.destroy()}}const yt={editSize:"auto",stroke:"#836DFF",strokeWidth:2,pointFill:"#FFFFFF",pointSize:8,pointRadius:16,rotateGap:45,buttonsDirection:"bottom",buttonsMargin:12,moveCursor:"move",resizeCursor:["nwse-resize","ns-resize","nesw-resize","ew-resize","nwse-resize","ns-resize","nesw-resize","ew-resize"],rotateCursor:["ne-resize","e-resize","se-resize","s-resize","sw-resize","w-resize","nw-resize","n-resize"],selector:!0,hover:!0,boxSelect:!0,resizeable:!0,rotateable:!0,skewable:!0};class wt{constructor(){this.tag="EditTool"}onMove(t){const{moveX:e,moveY:i,editor:s}=t,{app:o,list:n}=s;o.lockLayout(),n.forEach((t=>{const s=t.getLocalPoint({x:e,y:i},null,!0);t.move(s.x,s.y)})),o.unlockLayout()}onScale(t){const{scaleX:e,scaleY:i,transform:s,worldOrigin:o,editor:n}=t,{app:r,list:a}=n;r.lockLayout(),a.forEach((t=>{const r="size"===n.getEditSize(t);s?t.transform(s,r):t.scaleOf(t.getInnerPoint(o),e,i,r)})),r.unlockLayout()}onRotate(t){const{rotation:e,worldOrigin:i,editor:s}=t,{app:o,list:n}=s;o.lockLayout(),n.forEach((t=>{t.rotateOf(t.getInnerPoint(i),e)})),o.unlockLayout()}onSkew(t){const{skewX:e,skewY:i,transform:s,worldOrigin:o,editor:n}=t,{app:r,list:a}=n;r.lockLayout(),a.forEach((t=>{const r="size"===n.getEditSize(t);s?t.transform(s,r):t.skewOf(t.getInnerPoint(o),e,i,r)})),r.unlockLayout()}update(t){const{simulateTarget:e,element:i}=t;t.multiple&&e.parent.updateLayout();const{x:s,y:o,scaleX:n,scaleY:r,rotation:a,skewX:h,skewY:l,width:d,height:c}=i.getLayoutBounds("box",t,!0);t.editBox.set({x:s,y:o,scaleX:n,scaleY:r,rotation:a,skewX:h,skewY:l}),t.editBox.update({x:0,y:0,width:d,height:c})}}wt.list=[];const{left:vt,right:kt}=V;class xt extends wt{constructor(){super(...arguments),this.tag="LineEditTool",this.scaleOfEvent=!0}onScaleWithDrag(t){const{drag:e,direction:i,lockRatio:s,around:o}=t,n=t.target,r={x:0,y:0},{toPoint:a}=n;n.rotation=0;let{x:h,y:l}=e.getInnerMove(n);s&&(Math.abs(h)>Math.abs(l)?l=0:h=0),i===vt?(r.x+=h,r.y+=l,o&&(a.x-=h,a.y-=l)):(o&&(r.x-=h,r.y-=l),a.x+=h,a.y+=l),n.getLocalPointByInner(r,null,null,!0),n.getLocalPointByInner(a,null,null,!0),n.x=r.x,n.y=r.y,n.getInnerPointByLocal(a,null,null,!0),n.toPoint=a}onSkew(t){}update(t){const{rotatePoints:e,resizeLines:i,resizePoints:s}=t.editBox;super.update(t);for(let t=0;t<8;t++)t<4&&(i[t].visible=!1),s[t].visible=e[t].visible=t===vt||t===kt}}const mt=(t,e)=>t.parent.children.indexOf(t)-e.parent.children.indexOf(e),bt=(t,e)=>e.parent.children.indexOf(e)-t.parent.children.indexOf(t),_t={group(t,e,i){t.sort(bt);const{app:s,parent:o}=t[0];i||(i=new n),o.addAt(i,o.children.indexOf(t[0])),t.sort(mt);const r=new w(e.worldTransform);return r.divideParent(o.worldTransform),i.setTransform(r),i.editable=!0,i.hitChildren=!1,s.lockLayout(),t.forEach((t=>t.dropTo(i))),s.unlockLayout(),i},ungroup(t){const{app:e}=t[0],i=[];return e.lockLayout(),t.forEach((t=>{if(t.isBranch){const{parent:e,children:s}=t;for(;s.length;)i.push(s[0]),s[0].dropTo(e,e.children.indexOf(t));t.remove()}else i.push(t)})),e.unlockLayout(),i},toTop(t){t.sort(mt),t.forEach((t=>{t.parent&&t.parent.add(t)}))},toBottom(t){t.sort(bt),t.forEach((t=>{t.parent&&t.parent.addAt(t,0)}))}};class Et extends n{get list(){return this.leafList.list}get hasTarget(){return!!this.list.length}get multiple(){return this.list.length>1}get single(){return 1===this.list.length}get element(){return this.multiple?this.simulateTarget:this.list[0]}get buttons(){return this.editBox.buttons}get dragging(){return this.editBox.dragging}constructor(t,e){super(e),this.config=yt,this.leafList=new h,this.simulateTarget=new r({visible:!1}),this.editBox=new pt(this),this.selector=new N(this),this.targetEventIds=[],t&&(this.config=v.default(t,this.config)),this.addMany(this.selector,this.editBox)}hasItem(t){return this.leafList.has(t)}addItem(t){this.hasItem(t)||t.locked||(this.leafList.add(t),this.target=this.leafList.list)}removeItem(t){this.hasItem(t)&&(this.leafList.remove(t),this.target=this.leafList.list)}shiftItem(t){this.hasItem(t)?this.removeItem(t):this.addItem(t)}update(){this.target&&(this.editTool.update(this),this.selector.update())}updateEditTool(){this.editTool=function(t){if(1===t.length){const e=t[0];return e instanceof y&&!e.points?new xt:new wt}return new wt}(this.list)}getEditSize(t){let{editSize:e}=this.config;return"auto"===e?t.editSize:e}onMove(t){const e=t.getLocalMove(this.element);t.shiftKey&&(Math.abs(e.x)>Math.abs(e.y)?e.y=0:e.x=0),this.move(e.x,e.y)}onScale(t){const{element:e}=this,{direction:i}=t.current;let{around:s,lockRatio:o}=this.config;t.shiftKey&&(o=!0);const n=et.getScaleData(e.boxBounds,i,t.getInnerMove(e),o,et.getAround(s,t.altKey));this.editTool.onScaleWithDrag?(n.drag=t,this.scaleWithDrag(n)):this.scaleOf(n.origin,n.scaleX,n.scaleY)}onRotate(t){const{skewable:e,around:i,rotateGap:s}=this.config,{direction:o,name:n}=t.current;if(e&&"resize-line"===n)return this.onSkew(t);const{element:r,editBox:a}=this;let h,l;if(t instanceof k)l=t.rotation,h=r.getInnerPoint(t);else{const e={x:t.x-t.moveX,y:t.y-t.moveY},s=et.getRotateData(r.boxBounds,o,t.getInner(r),r.getInnerPoint(e),t.shiftKey?null:i||"center");l=s.rotation,h=s.origin}l=x.getGapRotation(l,s,r.rotation),l&&(a.flippedOne&&(l=-l),this.rotateOf(h,l))}onSkew(t){const{element:e}=this,{around:i}=this.config,{origin:s,skewX:o,skewY:n}=et.getSkewData(e.boxBounds,t.current.direction,t.getInnerMove(e),et.getAround(i,t.altKey));(o||n)&&this.skewOf(s,o,n)}move(t,e){const{element:i}=this,s=i.getWorldPointByLocal({x:t,y:e},null,!0),o=new S(S.MOVE,{target:i,editor:this,moveX:s.x,moveY:s.y});this.editTool.onMove(o),this.emitEvent(o),this.multiple&&i.move(t,e)}scaleWithDrag(t){const{element:e}=this,i=e.getWorldPoint(t.origin),s=new T(T.SCALE,Object.assign(Object.assign({},t),{target:e,editor:this,worldOrigin:i}));this.editTool.onScaleWithDrag(s),this.emitEvent(s)}scaleOf(t,e,i=e,s){const{element:o}=this,n=o.getWorldPoint(t);let r;if(this.multiple){const s=Object.assign({},o.localTransform);o.scaleOf(t,e,i),r=new w(o.localTransform).divide(s)}const a=new T(T.SCALE,{target:o,editor:this,worldOrigin:n,scaleX:e,scaleY:i,transform:r});this.editTool.onScale(a),this.emitEvent(a)}rotateOf(t,e){const{element:i}=this,s=i.getWorldPoint(t),o=new O(O.ROTATE,{target:i,editor:this,worldOrigin:s,rotation:e});this.editTool.onRotate(o),this.emitEvent(o),this.multiple&&i.rotateOf(t,e)}skewOf(t,e,i=0,s){const{element:o}=this,n=o.getWorldPoint(t);let r;if(this.multiple){const s=Object.assign({},o.localTransform);o.skewOf(t,e,i),r=new w(o.localTransform).divide(s)}const a=new P(P.SKEW,{target:o,editor:this,skewX:e,skewY:i,transform:r,worldOrigin:n});this.editTool.onSkew(a),this.emitEvent(a)}group(t){return this.multiple&&(this.target=_t.group(this.list,this.element,t)),this.target}ungroup(){return this.list.length&&(this.target=_t.ungroup(this.list)),this.list}lock(){this.list.forEach((t=>t.locked=!0)),this.update()}unlock(){this.list.forEach((t=>t.locked=!1)),this.update()}toTop(){this.list.length&&(_t.toTop(this.list),this.leafList.update())}toBottom(){this.list.length&&(_t.toBottom(this.list),this.leafList.update())}listenTargetEvents(){if(!this.targetEventIds.length){const{leafer:t}=this.list[0];this.targetEventIds=[t.on_(m.START,this.update,this),t.on_([b.HOLD,b.UP],(t=>{dt(this,t)})),t.on_(b.DOWN,this.editBox.onArrow,this.editBox)]}}removeTargetEvents(){const{targetEventIds:t}=this;t.length&&(this.off_(t),t.length=0)}destroy(){this.destroyed||(this.simulateTarget.destroy(),this.target=this.hoverTarget=this.simulateTarget=null,super.destroy())}}E([D((function(t,e){t.emitEvent(new L(L.HOVER,{editor:t,value:t.hoverTarget,oldValue:e}))}))],Et.prototype,"hoverTarget",void 0),E([D((function(t,e){const{target:i}=t;i?t.leafList=i instanceof h?i:new h(i):t.leafList.reset(),t.emitEvent(new L(L.SELECT,{editor:t,value:i,oldValue:e})),t.hasTarget?t.waitLeafer((()=>{t.multiple&&function(t){const{simulateTarget:e,leafList:i}=t,{x:s,y:o,width:n,height:r}=(new a).setListWithFn(i.list,(t=>t.worldBoxBounds)),h=e.parent=i.list[0].leafer.zoomLayer,{scaleX:l,scaleY:d,e:c,f:g}=h.__world;e.reset({x:(s-c)/l,y:(o-g)/d,width:n/l,height:r/d})}(t),ct(t),t.updateEditTool(),t.update(),t.listenTargetEvents()})):t.removeTargetEvents()}))],Et.prototype,"target",void 0),_.editor=function(t){return new Et(t)};export{pt as EditBox,et as EditDataHelper,ut as EditPoint,N as EditSelect,K as EditSelectHelper,wt as EditTool,Et as Editor,L as EditorEvent,_t as EditorHelper,S as EditorMoveEvent,O as EditorRotateEvent,T as EditorScaleEvent,P as EditorSkewEvent,xt as LineEditTool,I as SelectArea,M as Stroker};
|
package/dist/editor.js
CHANGED
|
@@ -74,8 +74,11 @@ this.LeaferIN.editor = (function (exports, core) {
|
|
|
74
74
|
const privateKey = '_' + key;
|
|
75
75
|
core.defineKey(target, key, {
|
|
76
76
|
get() { return this[privateKey]; },
|
|
77
|
-
set(value) {
|
|
78
|
-
this[privateKey]
|
|
77
|
+
set(value) {
|
|
78
|
+
const old = this[privateKey];
|
|
79
|
+
if (old !== value)
|
|
80
|
+
this[privateKey] = value, fn(this, old);
|
|
81
|
+
}
|
|
79
82
|
});
|
|
80
83
|
};
|
|
81
84
|
}
|
|
@@ -306,10 +309,12 @@ this.LeaferIN.editor = (function (exports, core) {
|
|
|
306
309
|
selectList.push(item); });
|
|
307
310
|
list.forEach(item => { if (!this.originList.has(item))
|
|
308
311
|
selectList.push(item); });
|
|
309
|
-
editor.
|
|
312
|
+
if (selectList.length !== editor.list.length || editor.list.some((child, index) => child !== selectList[index])) {
|
|
313
|
+
editor.target = selectList;
|
|
314
|
+
}
|
|
310
315
|
}
|
|
311
316
|
else {
|
|
312
|
-
editor.target = this.originList;
|
|
317
|
+
editor.target = this.originList.list;
|
|
313
318
|
if (editor.leafList.length)
|
|
314
319
|
editor.update();
|
|
315
320
|
}
|
|
@@ -499,7 +504,7 @@ this.LeaferIN.editor = (function (exports, core) {
|
|
|
499
504
|
return { origin, skewX, skewY };
|
|
500
505
|
},
|
|
501
506
|
getAround(around, altKey) {
|
|
502
|
-
return (altKey && !around) ?
|
|
507
|
+
return (altKey && !around) ? 'center' : around;
|
|
503
508
|
},
|
|
504
509
|
getRotateDirection(direction, rotation, totalDirection = 8) {
|
|
505
510
|
direction = (direction + Math.round(rotation / (360 / totalDirection))) % totalDirection;
|
|
@@ -604,13 +609,13 @@ this.LeaferIN.editor = (function (exports, core) {
|
|
|
604
609
|
const { width, height } = bounds;
|
|
605
610
|
const { rect, circle, resizePoints, rotatePoints, resizeLines } = this;
|
|
606
611
|
const { middlePoint, resizeable, rotateable, stroke, strokeWidth } = config;
|
|
607
|
-
const points = this.getDirection8Points(bounds);
|
|
608
612
|
const pointsStyle = this.getPointsStyle();
|
|
609
613
|
const middlePointsStyle = this.getMiddlePointsStyle();
|
|
610
614
|
this.visible = list[0] && !list[0].locked;
|
|
611
|
-
let point, style, rotateP, resizeP, resizeL;
|
|
615
|
+
let point = {}, style, rotateP, resizeP, resizeL;
|
|
612
616
|
for (let i = 0; i < 8; i++) {
|
|
613
|
-
|
|
617
|
+
core.AroundHelper.toPoint(core.AroundHelper.directionData[i], bounds, point);
|
|
618
|
+
style = this.getPointStyle((i % 2) ? middlePointsStyle[((i - 1) / 2) % middlePointsStyle.length] : pointsStyle[(i / 2) % pointsStyle.length]);
|
|
614
619
|
resizeP = resizePoints[i], rotateP = rotatePoints[i], resizeL = resizeLines[Math.floor(i / 2)];
|
|
615
620
|
resizeP.set(style);
|
|
616
621
|
resizeP.set(point), rotateP.set(point), resizeL.set(point);
|
|
@@ -682,19 +687,6 @@ this.LeaferIN.editor = (function (exports, core) {
|
|
|
682
687
|
const { middlePoint } = this.editor.config;
|
|
683
688
|
return middlePoint instanceof Array ? middlePoint : (middlePoint ? [middlePoint] : this.getPointsStyle());
|
|
684
689
|
}
|
|
685
|
-
getDirection8Points(bounds) {
|
|
686
|
-
const { x, y, width, height } = bounds;
|
|
687
|
-
return [
|
|
688
|
-
{ x, y },
|
|
689
|
-
{ x: x + width / 2, y },
|
|
690
|
-
{ x: x + width, y },
|
|
691
|
-
{ x: x + width, y: y + height / 2 },
|
|
692
|
-
{ x: x + width, y: y + height },
|
|
693
|
-
{ x: x + width / 2, y: y + height },
|
|
694
|
-
{ x, y: y + height },
|
|
695
|
-
{ x, y: y + height / 2 }
|
|
696
|
-
];
|
|
697
|
-
}
|
|
698
690
|
onDragStart(e) {
|
|
699
691
|
this.dragging = true;
|
|
700
692
|
if (e.target.name === 'rect')
|
|
@@ -940,7 +932,7 @@ this.LeaferIN.editor = (function (exports, core) {
|
|
|
940
932
|
simulateTarget.reset({ x: (x - worldX) / scaleX, y: (y - worldY) / scaleY, width: width / scaleX, height: height / scaleY });
|
|
941
933
|
}
|
|
942
934
|
|
|
943
|
-
function onTarget(editor) {
|
|
935
|
+
function onTarget(editor, oldValue) {
|
|
944
936
|
const { target } = editor;
|
|
945
937
|
if (target) {
|
|
946
938
|
editor.leafList = target instanceof core.LeafList ? target : new core.LeafList(target instanceof Array ? target : target);
|
|
@@ -948,7 +940,7 @@ this.LeaferIN.editor = (function (exports, core) {
|
|
|
948
940
|
else {
|
|
949
941
|
editor.leafList.reset();
|
|
950
942
|
}
|
|
951
|
-
editor.emitEvent(new EditorEvent(EditorEvent.SELECT, { editor }));
|
|
943
|
+
editor.emitEvent(new EditorEvent(EditorEvent.SELECT, { editor, value: target, oldValue }));
|
|
952
944
|
if (editor.hasTarget) {
|
|
953
945
|
editor.waitLeafer(() => {
|
|
954
946
|
if (editor.multiple)
|
|
@@ -963,8 +955,8 @@ this.LeaferIN.editor = (function (exports, core) {
|
|
|
963
955
|
editor.removeTargetEvents();
|
|
964
956
|
}
|
|
965
957
|
}
|
|
966
|
-
function onHover(editor) {
|
|
967
|
-
editor.emitEvent(new EditorEvent(EditorEvent.HOVER, { editor }));
|
|
958
|
+
function onHover(editor, oldValue) {
|
|
959
|
+
editor.emitEvent(new EditorEvent(EditorEvent.HOVER, { editor, value: editor.hoverTarget, oldValue }));
|
|
968
960
|
}
|
|
969
961
|
|
|
970
962
|
const order = (a, b) => a.parent.children.indexOf(a) - b.parent.children.indexOf(b);
|
|
@@ -1010,44 +1002,15 @@ this.LeaferIN.editor = (function (exports, core) {
|
|
|
1010
1002
|
toTop(list) {
|
|
1011
1003
|
list.sort(order);
|
|
1012
1004
|
list.forEach(leaf => {
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
if (parent) {
|
|
1016
|
-
const { children } = parent;
|
|
1017
|
-
const top = children.length - 1;
|
|
1018
|
-
const zIndexOfTop = children[top].__.zIndex;
|
|
1019
|
-
const current = children.indexOf(leaf);
|
|
1020
|
-
if (current !== top) {
|
|
1021
|
-
children.splice(current, 1);
|
|
1022
|
-
children.push(leaf);
|
|
1023
|
-
zIndex = zIndexOfTop + 1;
|
|
1024
|
-
}
|
|
1025
|
-
else {
|
|
1026
|
-
zIndex = zIndexOfTop;
|
|
1027
|
-
}
|
|
1028
|
-
leaf.zIndex = zIndex;
|
|
1029
|
-
}
|
|
1005
|
+
if (leaf.parent)
|
|
1006
|
+
leaf.parent.add(leaf);
|
|
1030
1007
|
});
|
|
1031
1008
|
},
|
|
1032
1009
|
toBottom(list) {
|
|
1033
1010
|
list.sort(reverseOrder);
|
|
1034
1011
|
list.forEach(leaf => {
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
if (parent) {
|
|
1038
|
-
const { children } = parent;
|
|
1039
|
-
const zIndexOfBottom = children[0].__.zIndex;
|
|
1040
|
-
const current = children.indexOf(leaf);
|
|
1041
|
-
if (current !== 0) {
|
|
1042
|
-
children.splice(current, 1);
|
|
1043
|
-
children.unshift(leaf);
|
|
1044
|
-
zIndex = zIndexOfBottom - 1;
|
|
1045
|
-
}
|
|
1046
|
-
else {
|
|
1047
|
-
zIndex = zIndexOfBottom;
|
|
1048
|
-
}
|
|
1049
|
-
leaf.zIndex = zIndex;
|
|
1050
|
-
}
|
|
1012
|
+
if (leaf.parent)
|
|
1013
|
+
leaf.parent.addAt(leaf, 0);
|
|
1051
1014
|
});
|
|
1052
1015
|
}
|
|
1053
1016
|
};
|
|
@@ -1208,13 +1171,15 @@ this.LeaferIN.editor = (function (exports, core) {
|
|
|
1208
1171
|
this.editTool.onSkew(event);
|
|
1209
1172
|
this.emitEvent(event);
|
|
1210
1173
|
}
|
|
1211
|
-
group() {
|
|
1174
|
+
group(group) {
|
|
1212
1175
|
if (this.multiple)
|
|
1213
|
-
this.target = EditorHelper.group(this.list, this.element);
|
|
1176
|
+
this.target = EditorHelper.group(this.list, this.element, group);
|
|
1177
|
+
return this.target;
|
|
1214
1178
|
}
|
|
1215
1179
|
ungroup() {
|
|
1216
1180
|
if (this.list.length)
|
|
1217
1181
|
this.target = EditorHelper.ungroup(this.list);
|
|
1182
|
+
return this.list;
|
|
1218
1183
|
}
|
|
1219
1184
|
lock() {
|
|
1220
1185
|
this.list.forEach(leaf => leaf.locked = true);
|
package/dist/editor.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
this.LeaferIN=this.LeaferIN||{},this.LeaferIN.editor=function(t,e){"use strict";function i(t,e,i,s){var o,n=arguments.length,r=n<3?e:null===s?s=Object.getOwnPropertyDescriptor(e,i):s;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)r=Reflect.decorate(t,e,i,s);else for(var a=t.length-1;a>=0;a--)(o=t[a])&&(r=(n<3?o(r):n>3?o(e,i,r):o(e,i))||r);return n>3&&r&&Object.defineProperty(e,i,r),r}"function"==typeof SuppressedError&&SuppressedError;class s extends e.Event{constructor(t,e){super(t),e&&Object.assign(this,e)}}s.SELECT="editor.select",s.HOVER="editor.hover";class o extends s{constructor(t,e){super(t,e)}}o.MOVE="editor.move";class n extends s{constructor(t,e){super(t,e)}}n.SCALE="editor.scale";class r extends s{constructor(t,e){super(t,e)}}r.ROTATE="editor.rotate";class a extends s{constructor(t,e){super(t,e)}}function h(t){return(i,s)=>{const o="_"+s;e.defineKey(i,s,{get(){return this[o]},set(e){this[o]!==e&&(this[o]=e,t(this))}})}}a.SKEW="editor.skew";const l=e.MatrixHelper.get(),{abs:d}=Math,{copy:c,scale:g}=e.MatrixHelper;class u extends e.UI{constructor(){super(),this.list=[],this.hittable=!1,this.strokeAlign="center"}setTarget(t,e){const{stroke:i,strokeWidth:s}=e;this.set({stroke:i,strokeWidth:s}),this.target=t}__draw(t,i){const{list:s}=this;if(s.length){let o;const{stroke:n,strokeWidth:r}=this.__,{bounds:a}=i;for(let h=0;h<s.length;h++)if(o=s[h],a&&a.hit(o.__world,i.matrix)){let s;if("scale"===o.__.editSize){const e=d(o.__world.scaleX),n=d(o.__world.scaleY);if(e!==n){c(l,o.__world),g(l,1/e,1/n),t.setWorld(l,i.matrix),t.beginPath(),this.__.strokeWidth=r;const{x:a,y:h,width:d,height:u}=o.__layout.boxBounds;t.rect(a*e,h*n,d*e,u*n),s=!0}}s||(t.setWorld(o.__world,i.matrix),t.beginPath(),o.__.__pathForRender?o.__drawRenderPath(t):o.__drawPathByBox(t),this.__.strokeWidth=r/d(o.__world.scaleX)),"string"==typeof n?e.Paint.stroke(n,this,t,i):e.Paint.strokes(n,this,t,i)}this.__.strokeWidth=r}}destroy(){this.target=null,super.destroy()}}i([h((function(t){const e=t.target;t.list=e?e instanceof Array?e:[e]:[],t.forceUpdate()}))],u.prototype,"target",void 0);class f extends e.Group{constructor(t){super(t),this.strokeArea=new e.Rect({strokeAlign:"center"}),this.fillArea=new e.Rect,this.visible=this.hittable=!1,this.addMany(this.fillArea,this.strokeArea)}setStyle(t,e){const{visible:i,stroke:s,strokeWidth:o}=t;this.visible=i,this.strokeArea.reset(Object.assign({stroke:s,strokeWidth:o},e||{})),this.fillArea.reset({visible:!e,fill:s,opacity:.2})}setBounds(t){this.strokeArea.set(t),this.fillArea.set(t)}}var p;!function(t){t[t.No=0]="No",t[t.Yes=1]="Yes",t[t.NoAndSkip=2]="NoAndSkip",t[t.YesAndSkip=3]="YesAndSkip"}(p||(p={}));const{No:y,Yes:v,NoAndSkip:x,YesAndSkip:w}=p,E={findOne:t=>t.list.find((t=>t.editable)),findBounds(t,e){if(t.__.hittable&&!t.__.locked&&e.hit(t.__world)){if(t.__.editable){if(t.isBranch&&!t.__.hitChildren)return t.__.hitSelf?w:x;if(t.isFrame)return e.includes(t.__layout.boxBounds,t.__world)?w:y;if(e.hit(t.__layout.boxBounds,t.__world)&&t.__.hitSelf)return v}return y}return t.isBranch?x:y}},{findOne:k}=E;class _ extends e.Group{get dragging(){return!!this.originList}get running(){return this.editor.hittable&&this.editor.config.selector}get isMoveMode(){return this.app&&this.app.interaction.moveMode}constructor(t){super(),this.hoverStroker=new u,this.targetStroker=new u,this.bounds=new e.Bounds,this.selectArea=new f,this.__eventIds=[],this.editor=t,this.addMany(this.targetStroker,this.hoverStroker,this.selectArea),this.__listenEvents()}onHover(){const{editor:t}=this;if(!this.running||this.dragging||t.dragging)this.hoverStroker.target=null;else{const{stroke:e,strokeWidth:i,hover:s}=t.config;this.hoverStroker.setTarget(s?this.editor.hoverTarget:null,{stroke:e,strokeWidth:i})}}onSelect(){if(this.running){const{config:t,list:e}=this.editor,{stroke:i,strokeWidth:s}=t;this.targetStroker.setTarget(e,{stroke:i,strokeWidth:Math.max(1,s/2)}),this.hoverStroker.target=null}}update(){this.running&&this.targetStroker.forceUpdate()}onPointerMove(t){if(this.running&&!this.isMoveMode){const e=t.shiftKey?this.findDeepOne(t):k(t.path);this.editor.hoverTarget=this.editor.hasItem(e)?null:e}this.isMoveMode&&(this.editor.hoverTarget=null)}onBeforeDown(t){if(this.running&&!this.isMoveMode&&!t.middle){const e=this.lastDownLeaf=k(t.path);e?(t.shiftKey?this.editor.shiftItem(e):this.editor.target=e,this.editor.updateLayout(),e.leafer.interaction.updateDownData()):this.allow(t.target)&&(t.shiftKey||(this.editor.target=null))}}onTap(t){if(this.running&&t.shiftKey&&!t.middle&&!this.lastDownLeaf){const e=this.findDeepOne(t);e&&this.editor.shiftItem(e)}else this.isMoveMode&&(this.editor.target=null);this.lastDownLeaf=null}onDragStart(t){if(this.running&&this.allowDrag(t)){const{editor:e}=this,{stroke:i,strokeWidth:s,area:o}=e.config,{x:n,y:r}=t.getInner(this);this.bounds.set(n,r),this.selectArea.setStyle({visible:!0,stroke:i,strokeWidth:s,x:n,y:r},o),this.selectArea.setBounds(this.bounds.get()),this.originList=e.leafList.clone()}}onDrag(t){if(this.editor.dragging)this.onDragEnd();else if(this.dragging){const{editor:i}=this,s=t.getInnerTotal(this),o=this.bounds.clone().unsign(),n=new e.LeafList(i.app.find(E.findBounds,o));if(this.bounds.width=s.x,this.bounds.height=s.y,this.selectArea.setBounds(o.get()),n.length){const t=[];this.originList.forEach((e=>{n.has(e)||t.push(e)})),n.forEach((e=>{this.originList.has(e)||t.push(e)})),i.target=t}else i.target=this.originList,i.leafList.length&&i.update()}}onDragEnd(){this.dragging&&(this.originList=null,this.selectArea.visible=!1)}onAutoMove(t){if(this.dragging){const{x:e,y:i}=t.getLocalMove(this);this.bounds.x+=e,this.bounds.y+=i}}allow(t){return t.leafer!==this.editor.leafer}allowDrag(t){return!(!this.editor.config.boxSelect||t.target.draggable)&&(!this.editor.hasTarget&&this.allow(t.target)||t.shiftKey&&!k(t.path))}findDeepOne(t){const i={exclude:new e.LeafList(this.editor.editBox.rect)};return k(t.target.leafer.interaction.findPath(t,i))}__listenEvents(){const{editor:t}=this;t.waitLeafer((()=>{const{app:i}=t;i.selector.proxy=t,this.__eventIds=[t.on_(s.HOVER,this.onHover,this),t.on_(s.SELECT,this.onSelect,this),i.on_(e.PointerEvent.MOVE,this.onPointerMove,this),i.on_(e.PointerEvent.BEFORE_DOWN,this.onBeforeDown,this),i.on_(e.PointerEvent.TAP,this.onTap,this),i.on_(e.DragEvent.START,this.onDragStart,this),i.on_(e.DragEvent.DRAG,this.onDrag,this),i.on_(e.DragEvent.END,this.onDragEnd,this),i.on_(e.MoveEvent.MOVE,this.onAutoMove,this),i.on_([e.ZoomEvent.ZOOM,e.MoveEvent.MOVE],(()=>{this.editor.hoverTarget=null}))]}))}__removeListenEvents(){this.__eventIds&&(this.off_(this.__eventIds),this.__eventIds.length=0)}destroy(){this.editor=this.originList=this.lastDownLeaf=null,this.__removeListenEvents(),super.destroy()}}var m;!function(t){t[t.topLeft=0]="topLeft",t[t.top=1]="top",t[t.topRight=2]="topRight",t[t.right=3]="right",t[t.bottomRight=4]="bottomRight",t[t.bottom=5]="bottom",t[t.bottomLeft=6]="bottomLeft",t[t.left=7]="left"}(m||(m={}));const{topLeft:b,top:L,topRight:S,right:T,bottomRight:P,bottom:D,bottomLeft:O,left:A}=m,{toPoint:M}=e.AroundHelper,R={getScaleData(t,e,i,s,o){let n,r=1,a=1;const{width:h,height:l}=t;o&&(i.x*=2,i.y*=2);const d=(-i.y+l)/l,c=(i.x+h)/h,g=(i.y+l)/l,u=(-i.x+h)/h;switch(e){case L:a=d,n={x:.5,y:1};break;case T:r=c,n={x:0,y:.5};break;case D:a=g,n={x:.5,y:0};break;case A:r=u,n={x:1,y:.5};break;case b:a=d,r=u,n={x:1,y:1};break;case S:a=d,r=c,n={x:0,y:1};break;case P:a=g,r=c,n={x:0,y:0};break;case O:a=g,r=u,n={x:1,y:0}}return s&&(1!==r?a=r:r=a),M(o||n,t,n),{origin:n,scaleX:r,scaleY:a,direction:e,lockRatio:s,around:o}},getRotateData(t,i,s,o,n){let r;switch(i){case b:r={x:1,y:1};break;case S:r={x:0,y:1};break;case P:r={x:0,y:0};break;case O:r={x:1,y:0};break;default:r={x:.5,y:.5}}return M(n||r,t,r),{origin:r,rotation:e.PointHelper.getRotation(o,r,s)}},getSkewData(t,i,s,o){let n,r,a=0,h=0;switch(i){case L:r={x:.5,y:0},n={x:.5,y:1},a=1;break;case D:r={x:.5,y:1},n={x:.5,y:0},a=1;break;case A:r={x:0,y:.5},n={x:1,y:.5},h=1;break;case T:r={x:1,y:.5},n={x:0,y:.5},h=1}const{x:l,y:d,width:c,height:g}=t;r.x=l+r.x*c,r.y=d+r.y*g,M(o||n,t,n);const u=e.PointHelper.getRotation(r,n,{x:r.x+(a?s.x:0),y:r.y+(h?s.y:0)});return a?a=-u:h=u,{origin:n,skewX:a,skewY:h}},getAround:(t,i)=>i&&!t?e.AroundHelper.center:t,getRotateDirection:(t,e,i=8)=>((t=(t+Math.round(e/(360/i)))%i)<0&&(t+=i),t)},{topLeft:z,top:B,topRight:I,right:W,bottomRight:Y,bottom:X,bottomLeft:K,left:C}=m;function F(t,e){const{editBox:i}=t,s=i.enterPoint;if(!s||!t.hasTarget||!i.visible)return;let{rotation:o}=i,{resizeCursor:n,rotateCursor:r,resizeable:a,rotateable:h}=t.config;const{direction:l,pointType:d}=s;i.enterPoint=s;const c="resize"===d;if(c&&h&&(e.metaKey||e.ctrlKey||!a)&&(n=r),i.flipped){const{flippedX:t,flippedY:e}=i;N(n=[...n],t,e),N(r=[...r],e,t),i.flippedOne&&(o=-o)}const g=R.getRotateDirection(l,o);s.cursor=c?n[g]:r[g]}function H(t){t.editBox.rect.cursor=t.config.moveCursor}function N(t,e,i){if(e){const e=t[B],i=t[z],s=t[I];t[B]=t[X],t[z]=t[K],t[I]=t[Y],t[X]=e,t[K]=i,t[Y]=s}if(i){const e=t[C],i=t[z],s=t[K];t[C]=t[W],t[z]=t[I],t[K]=t[Y],t[W]=e,t[I]=i,t[Y]=s}}class j extends e.Box{}const G=["top","right","bottom","left"];class V extends e.Group{get flipped(){return this.flippedX||this.flippedY}get flippedX(){return this.scaleX<0}get flippedY(){return this.scaleY<0}get flippedOne(){return this.scaleX*this.scaleY<0}constructor(t){super(),this.rect=new e.Box({name:"rect",hitFill:"all",hitStroke:"none",strokeAlign:"center",hitRadius:5}),this.circle=new j({name:"circle",strokeAlign:"outside",around:"center",cursor:"crosshair",hitRadius:5}),this.buttons=new e.Group({around:"center",hitSelf:!1}),this.resizePoints=[],this.rotatePoints=[],this.resizeLines=[],this.__eventIds=[],this.editor=t,this.visible=!1,this.create(),this.__listenEvents()}create(){let t,e,i;const{resizePoints:s,rotatePoints:o,resizeLines:n,rect:r,circle:a,buttons:h}=this,l=[{x:1,y:1},{x:.5,y:1},{x:0,y:1},{x:0,y:.5},{x:0,y:0},{x:.5,y:0},{x:1,y:0},{x:1,y:.5}];for(let r=0;r<8;r++)t=new j({around:l[r],width:15,height:15,hitFill:"all"}),o.push(t),this.listenPointEvents(t,"rotate",r),r%2&&(e=new j({name:"resize-line",around:"center",width:10,height:10,hitFill:"all"}),n.push(e),this.listenPointEvents(e,"resize",r)),i=new j({name:"resize-point",around:"center",strokeAlign:"outside",hitRadius:5}),s.push(i),this.listenPointEvents(i,"resize",r);h.add(a),this.listenPointEvents(a,"rotate",2),this.addMany(...o,r,h,...n,...s)}update(t){const{config:e,list:i}=this.editor,{width:s,height:o}=t,{rect:n,circle:r,resizePoints:a,rotatePoints:h,resizeLines:l}=this,{middlePoint:d,resizeable:c,rotateable:g,stroke:u,strokeWidth:f}=e,p=this.getDirection8Points(t),y=this.getPointsStyle(),v=this.getMiddlePointsStyle();let x,w,E,k,_;this.visible=i[0]&&!i[0].locked;for(let t=0;t<8;t++)x=p[t],w=this.getPointStyle(t%2?v[(t-1)/2%v.length]:y[t/2%y.length]),k=a[t],E=h[t],_=l[Math.floor(t/2)],k.set(w),k.set(x),E.set(x),_.set(x),k.visible=_.visible=c||g,E.visible=g&&c,t%2?(k.visible=E.visible=!!d,(t+1)/2%2?(_.width=s,k.width>s-30&&(k.visible=!1)):(_.height=o,k.rotation=90,k.width>o-30&&(k.visible=!1))):k.rotation=t/2*90;r.visible=g&&!!e.rotatePoint,r.set(this.getPointStyle(e.rotatePoint||y[0])),n.set(Object.assign({stroke:u,strokeWidth:f},e.rect||{})),n.set(Object.assign(Object.assign({},t),{visible:!0})),this.layoutButtons()}layoutButtons(){const{buttons:t,resizePoints:e}=this,{buttonsDirection:i,buttonsFixed:s,buttonsMargin:o,middlePoint:n}=this.editor.config,{flippedX:r,flippedY:a}=this;let h=G.indexOf(i);(h%2&&r||(h+1)%2&&a)&&s&&(h=(h+2)%4);const l=s?R.getRotateDirection(h,this.flippedOne?this.rotation:-this.rotation,4):h,d=e[2*l+1],c=l%2,g=l&&3!==l?1:-1,u=(o+(h%2?(n?d.width:0)+t.boxBounds.width:(n?d.height:0)+t.boxBounds.height)/2)*g;c?(t.x=d.x+u,t.y=d.y):(t.x=d.x,t.y=d.y+u),s&&(t.rotation=90*(l-h),t.scaleX=r?-1:1,t.scaleY=a?-1:1)}getPointStyle(t){const{stroke:e,strokeWidth:i,pointFill:s,pointSize:o,pointRadius:n}=this.editor.config,r={fill:s,stroke:e,strokeWidth:i,width:o,height:o,cornerRadius:n};return t?Object.assign(r,t):r}getPointsStyle(){const{point:t}=this.editor.config;return t instanceof Array?t:[t]}getMiddlePointsStyle(){const{middlePoint:t}=this.editor.config;return t instanceof Array?t:t?[t]:this.getPointsStyle()}getDirection8Points(t){const{x:e,y:i,width:s,height:o}=t;return[{x:e,y:i},{x:e+s/2,y:i},{x:e+s,y:i},{x:e+s,y:i+o/2},{x:e+s,y:i+o},{x:e+s/2,y:i+o},{x:e,y:i+o},{x:e,y:i+o/2}]}onDragStart(t){this.dragging=!0,"rect"===t.target.name&&(this.editor.opacity=this.editor.config.hideOnMove?0:1)}onDragEnd(t){this.dragging=!1,"rect"===t.target.name&&(this.editor.opacity=1)}onDrag(t){const{editor:e}=this;"rotate"===t.current.pointType||t.metaKey||t.ctrlKey||!e.config.resizeable?e.config.rotateable&&e.onRotate(t):e.onScale(t)}onArrow(t){if(this.editor.hasTarget){const e={x:0,y:0},i=t.shiftKey?10:1;switch(t.code){case"ArrowDown":e.y=i;break;case"ArrowUp":e.y=-i;break;case"ArrowLeft":e.x=-i;break;case"ArrowRight":e.x=i}(e.x||e.y)&&this.editor.move(e.x,e.y)}}onDoubleClick(){const{editor:t}=this;t.single&&t.element.isBranch}listenPointEvents(t,i,s){const{editor:o}=this;t.direction=s,t.pointType=i,t.on_(e.DragEvent.START,this.onDragStart,this),t.on_(e.DragEvent.DRAG,this.onDrag,this),t.on_(e.DragEvent.END,this.onDragEnd,this),t.on_(e.PointerEvent.LEAVE,(()=>this.enterPoint=null)),"circle"!==t.name&&t.on_(e.PointerEvent.ENTER,(e=>{this.enterPoint=t,F(o,e)}))}__listenEvents(){const{rect:t,editor:i}=this;this.__eventIds=[i.on_(s.SELECT,(()=>{this.visible=i.hasTarget})),t.on_(e.DragEvent.START,this.onDragStart,this),t.on_(e.DragEvent.DRAG,i.onMove,i),t.on_(e.DragEvent.END,this.onDragEnd,this),t.on_(e.PointerEvent.ENTER,(()=>H(i))),t.on_(e.PointerEvent.DOUBLE_CLICK,this.onDoubleClick,this)]}__removeListenEvents(){this.off_(this.__eventIds),this.__eventIds.length=0}destroy(){this.editor=null,this.__removeListenEvents(),super.destroy()}}const U={editSize:"auto",stroke:"#836DFF",strokeWidth:2,pointFill:"#FFFFFF",pointSize:8,pointRadius:16,rotateGap:45,buttonsDirection:"bottom",buttonsMargin:12,moveCursor:"move",resizeCursor:["nwse-resize","ns-resize","nesw-resize","ew-resize","nwse-resize","ns-resize","nesw-resize","ew-resize"],rotateCursor:["ne-resize","e-resize","se-resize","s-resize","sw-resize","w-resize","nw-resize","n-resize"],selector:!0,hover:!0,boxSelect:!0,resizeable:!0,rotateable:!0,skewable:!0};class Z{constructor(){this.tag="EditTool"}onMove(t){const{moveX:e,moveY:i,editor:s}=t,{app:o,list:n}=s;o.lockLayout(),n.forEach((t=>{const s=t.getLocalPoint({x:e,y:i},null,!0);t.move(s.x,s.y)})),o.unlockLayout()}onScale(t){const{scaleX:e,scaleY:i,transform:s,worldOrigin:o,editor:n}=t,{app:r,list:a}=n;r.lockLayout(),a.forEach((t=>{const r="size"===n.getEditSize(t);s?t.transform(s,r):t.scaleOf(t.getInnerPoint(o),e,i,r)})),r.unlockLayout()}onRotate(t){const{rotation:e,worldOrigin:i,editor:s}=t,{app:o,list:n}=s;o.lockLayout(),n.forEach((t=>{t.rotateOf(t.getInnerPoint(i),e)})),o.unlockLayout()}onSkew(t){const{skewX:e,skewY:i,transform:s,worldOrigin:o,editor:n}=t,{app:r,list:a}=n;r.lockLayout(),a.forEach((t=>{const r="size"===n.getEditSize(t);s?t.transform(s,r):t.skewOf(t.getInnerPoint(o),e,i,r)})),r.unlockLayout()}update(t){const{simulateTarget:e,element:i}=t;t.multiple&&e.parent.updateLayout();const{x:s,y:o,scaleX:n,scaleY:r,rotation:a,skewX:h,skewY:l,width:d,height:c}=i.getLayoutBounds("box",t,!0);t.editBox.set({x:s,y:o,scaleX:n,scaleY:r,rotation:a,skewX:h,skewY:l}),t.editBox.update({x:0,y:0,width:d,height:c})}}Z.list=[];const{left:q,right:J}=m;class Q extends Z{constructor(){super(...arguments),this.tag="LineEditTool",this.scaleOfEvent=!0}onScaleWithDrag(t){const{drag:e,direction:i,lockRatio:s,around:o}=t,n=t.target,r={x:0,y:0},{toPoint:a}=n;n.rotation=0;let{x:h,y:l}=e.getInnerMove(n);s&&(Math.abs(h)>Math.abs(l)?l=0:h=0),i===q?(r.x+=h,r.y+=l,o&&(a.x-=h,a.y-=l)):(o&&(r.x-=h,r.y-=l),a.x+=h,a.y+=l),n.getLocalPointByInner(r,null,null,!0),n.getLocalPointByInner(a,null,null,!0),n.x=r.x,n.y=r.y,n.getInnerPointByLocal(a,null,null,!0),n.toPoint=a}onSkew(t){}update(t){const{rotatePoints:e,resizeLines:i,resizePoints:s}=t.editBox;super.update(t);for(let t=0;t<8;t++)t<4&&(i[t].visible=!1),s[t].visible=e[t].visible=t===q||t===J}}const $=(t,e)=>t.parent.children.indexOf(t)-e.parent.children.indexOf(e),tt=(t,e)=>e.parent.children.indexOf(e)-t.parent.children.indexOf(t),et={group(t,i,s){t.sort(tt);const{app:o,parent:n}=t[0];s||(s=new e.Group),n.addAt(s,n.children.indexOf(t[0])),t.sort($);const r=new e.Matrix(i.worldTransform);return r.divideParent(n.worldTransform),s.setTransform(r),s.editable=!0,s.hitChildren=!1,o.lockLayout(),t.forEach((t=>t.dropTo(s))),o.unlockLayout(),s},ungroup(t){const{app:e}=t[0],i=[];return e.lockLayout(),t.forEach((t=>{if(t.isBranch){const{parent:e,children:s}=t;for(;s.length;)i.push(s[0]),s[0].dropTo(e,e.children.indexOf(t));t.remove()}else i.push(t)})),e.unlockLayout(),i},toTop(t){t.sort($),t.forEach((t=>{let e;const{parent:i}=t;if(i){const{children:s}=i,o=s.length-1,n=s[o].__.zIndex,r=s.indexOf(t);r!==o?(s.splice(r,1),s.push(t),e=n+1):e=n,t.zIndex=e}}))},toBottom(t){t.sort(tt),t.forEach((t=>{let e;const{parent:i}=t;if(i){const{children:s}=i,o=s[0].__.zIndex,n=s.indexOf(t);0!==n?(s.splice(n,1),s.unshift(t),e=o-1):e=o,t.zIndex=e}}))}};class it extends e.Group{get list(){return this.leafList.list}get hasTarget(){return!!this.list.length}get multiple(){return this.list.length>1}get single(){return 1===this.list.length}get element(){return this.multiple?this.simulateTarget:this.list[0]}get buttons(){return this.editBox.buttons}get dragging(){return this.editBox.dragging}constructor(t,i){super(i),this.config=U,this.leafList=new e.LeafList,this.simulateTarget=new e.Rect({visible:!1}),this.editBox=new V(this),this.selector=new _(this),this.targetEventIds=[],t&&(this.config=e.DataHelper.default(t,this.config)),this.addMany(this.selector,this.editBox)}hasItem(t){return this.leafList.has(t)}addItem(t){this.hasItem(t)||t.locked||(this.leafList.add(t),this.target=this.leafList.list)}removeItem(t){this.hasItem(t)&&(this.leafList.remove(t),this.target=this.leafList.list)}shiftItem(t){this.hasItem(t)?this.removeItem(t):this.addItem(t)}update(){this.target&&(this.editTool.update(this),this.selector.update())}updateEditTool(){this.editTool=function(t){if(1===t.length){const i=t[0];return i instanceof e.Line&&!i.points?new Q:new Z}return new Z}(this.list)}getEditSize(t){let{editSize:e}=this.config;return"auto"===e?t.editSize:e}onMove(t){const e=t.getLocalMove(this.element);t.shiftKey&&(Math.abs(e.x)>Math.abs(e.y)?e.y=0:e.x=0),this.move(e.x,e.y)}onScale(t){const{element:e}=this,{direction:i}=t.current;let{around:s,lockRatio:o}=this.config;t.shiftKey&&(o=!0);const n=R.getScaleData(e.boxBounds,i,t.getInnerMove(e),o,R.getAround(s,t.altKey));this.editTool.onScaleWithDrag?(n.drag=t,this.scaleWithDrag(n)):this.scaleOf(n.origin,n.scaleX,n.scaleY)}onRotate(t){const{skewable:i,around:s,rotateGap:o}=this.config,{direction:n,name:r}=t.current;if(i&&"resize-line"===r)return this.onSkew(t);const{element:a,editBox:h}=this;let l,d;if(t instanceof e.RotateEvent)d=t.rotation,l=a.getInnerPoint(t);else{const e={x:t.x-t.moveX,y:t.y-t.moveY},i=R.getRotateData(a.boxBounds,n,t.getInner(a),a.getInnerPoint(e),t.shiftKey?null:s||"center");d=i.rotation,l=i.origin}d=e.MathHelper.getGapRotation(d,o,a.rotation),d&&(h.flippedOne&&(d=-d),this.rotateOf(l,d))}onSkew(t){const{element:e}=this,{around:i}=this.config,{origin:s,skewX:o,skewY:n}=R.getSkewData(e.boxBounds,t.current.direction,t.getInnerMove(e),R.getAround(i,t.altKey));(o||n)&&this.skewOf(s,o,n)}move(t,e){const{element:i}=this,s=i.getWorldPointByLocal({x:t,y:e},null,!0),n=new o(o.MOVE,{target:i,editor:this,moveX:s.x,moveY:s.y});this.editTool.onMove(n),this.emitEvent(n),this.multiple&&i.move(t,e)}scaleWithDrag(t){const{element:e}=this,i=e.getWorldPoint(t.origin),s=new n(n.SCALE,Object.assign(Object.assign({},t),{target:e,editor:this,worldOrigin:i}));this.editTool.onScaleWithDrag(s),this.emitEvent(s)}scaleOf(t,i,s=i,o){const{element:r}=this,a=r.getWorldPoint(t);let h;if(this.multiple){const o=Object.assign({},r.localTransform);r.scaleOf(t,i,s),h=new e.Matrix(r.localTransform).divide(o)}const l=new n(n.SCALE,{target:r,editor:this,worldOrigin:a,scaleX:i,scaleY:s,transform:h});this.editTool.onScale(l),this.emitEvent(l)}rotateOf(t,e){const{element:i}=this,s=i.getWorldPoint(t),o=new r(r.ROTATE,{target:i,editor:this,worldOrigin:s,rotation:e});this.editTool.onRotate(o),this.emitEvent(o),this.multiple&&i.rotateOf(t,e)}skewOf(t,i,s=0,o){const{element:n}=this,r=n.getWorldPoint(t);let h;if(this.multiple){const o=Object.assign({},n.localTransform);n.skewOf(t,i,s),h=new e.Matrix(n.localTransform).divide(o)}const l=new a(a.SKEW,{target:n,editor:this,skewX:i,skewY:s,transform:h,worldOrigin:r});this.editTool.onSkew(l),this.emitEvent(l)}group(){this.multiple&&(this.target=et.group(this.list,this.element))}ungroup(){this.list.length&&(this.target=et.ungroup(this.list))}lock(){this.list.forEach((t=>t.locked=!0)),this.update()}unlock(){this.list.forEach((t=>t.locked=!1)),this.update()}toTop(){this.list.length&&(et.toTop(this.list),this.leafList.update())}toBottom(){this.list.length&&(et.toBottom(this.list),this.leafList.update())}listenTargetEvents(){if(!this.targetEventIds.length){const{leafer:t}=this.list[0];this.targetEventIds=[t.on_(e.RenderEvent.START,this.update,this),t.on_([e.KeyEvent.HOLD,e.KeyEvent.UP],(t=>{F(this,t)})),t.on_(e.KeyEvent.DOWN,this.editBox.onArrow,this.editBox)]}}removeTargetEvents(){const{targetEventIds:t}=this;t.length&&(this.off_(t),t.length=0)}destroy(){this.destroyed||(this.simulateTarget.destroy(),this.target=this.hoverTarget=this.simulateTarget=null,super.destroy())}}return i([h((function(t){t.emitEvent(new s(s.HOVER,{editor:t}))}))],it.prototype,"hoverTarget",void 0),i([h((function(t){const{target:i}=t;i?t.leafList=i instanceof e.LeafList?i:new e.LeafList(i):t.leafList.reset(),t.emitEvent(new s(s.SELECT,{editor:t})),t.hasTarget?t.waitLeafer((()=>{t.multiple&&function(t){const{simulateTarget:i,leafList:s}=t,{x:o,y:n,width:r,height:a}=(new e.Bounds).setListWithFn(s.list,(t=>t.worldBoxBounds)),h=i.parent=s.list[0].leafer.zoomLayer,{scaleX:l,scaleY:d,e:c,f:g}=h.__world;i.reset({x:(o-c)/l,y:(n-g)/d,width:r/l,height:a/d})}(t),H(t),t.updateEditTool(),t.update(),t.listenTargetEvents()})):t.removeTargetEvents()}))],it.prototype,"target",void 0),e.Creator.editor=function(t){return new it(t)},t.EditBox=V,t.EditDataHelper=R,t.EditPoint=j,t.EditSelect=_,t.EditSelectHelper=E,t.EditTool=Z,t.Editor=it,t.EditorEvent=s,t.EditorHelper=et,t.EditorMoveEvent=o,t.EditorRotateEvent=r,t.EditorScaleEvent=n,t.EditorSkewEvent=a,t.LineEditTool=Q,t.SelectArea=f,t.Stroker=u,t}({},LeaferUI);
|
|
1
|
+
this.LeaferIN=this.LeaferIN||{},this.LeaferIN.editor=function(t,e){"use strict";function i(t,e,i,s){var o,n=arguments.length,r=n<3?e:null===s?s=Object.getOwnPropertyDescriptor(e,i):s;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)r=Reflect.decorate(t,e,i,s);else for(var a=t.length-1;a>=0;a--)(o=t[a])&&(r=(n<3?o(r):n>3?o(e,i,r):o(e,i))||r);return n>3&&r&&Object.defineProperty(e,i,r),r}"function"==typeof SuppressedError&&SuppressedError;class s extends e.Event{constructor(t,e){super(t),e&&Object.assign(this,e)}}s.SELECT="editor.select",s.HOVER="editor.hover";class o extends s{constructor(t,e){super(t,e)}}o.MOVE="editor.move";class n extends s{constructor(t,e){super(t,e)}}n.SCALE="editor.scale";class r extends s{constructor(t,e){super(t,e)}}r.ROTATE="editor.rotate";class a extends s{constructor(t,e){super(t,e)}}function h(t){return(i,s)=>{const o="_"+s;e.defineKey(i,s,{get(){return this[o]},set(e){const i=this[o];i!==e&&(this[o]=e,t(this,i))}})}}a.SKEW="editor.skew";const l=e.MatrixHelper.get(),{abs:d}=Math,{copy:c,scale:g}=e.MatrixHelper;class u extends e.UI{constructor(){super(),this.list=[],this.hittable=!1,this.strokeAlign="center"}setTarget(t,e){const{stroke:i,strokeWidth:s}=e;this.set({stroke:i,strokeWidth:s}),this.target=t}__draw(t,i){const{list:s}=this;if(s.length){let o;const{stroke:n,strokeWidth:r}=this.__,{bounds:a}=i;for(let h=0;h<s.length;h++)if(o=s[h],a&&a.hit(o.__world,i.matrix)){let s;if("scale"===o.__.editSize){const e=d(o.__world.scaleX),n=d(o.__world.scaleY);if(e!==n){c(l,o.__world),g(l,1/e,1/n),t.setWorld(l,i.matrix),t.beginPath(),this.__.strokeWidth=r;const{x:a,y:h,width:d,height:u}=o.__layout.boxBounds;t.rect(a*e,h*n,d*e,u*n),s=!0}}s||(t.setWorld(o.__world,i.matrix),t.beginPath(),o.__.__pathForRender?o.__drawRenderPath(t):o.__drawPathByBox(t),this.__.strokeWidth=r/d(o.__world.scaleX)),"string"==typeof n?e.Paint.stroke(n,this,t,i):e.Paint.strokes(n,this,t,i)}this.__.strokeWidth=r}}destroy(){this.target=null,super.destroy()}}i([h((function(t){const e=t.target;t.list=e?e instanceof Array?e:[e]:[],t.forceUpdate()}))],u.prototype,"target",void 0);class f extends e.Group{constructor(t){super(t),this.strokeArea=new e.Rect({strokeAlign:"center"}),this.fillArea=new e.Rect,this.visible=this.hittable=!1,this.addMany(this.fillArea,this.strokeArea)}setStyle(t,e){const{visible:i,stroke:s,strokeWidth:o}=t;this.visible=i,this.strokeArea.reset(Object.assign({stroke:s,strokeWidth:o},e||{})),this.fillArea.reset({visible:!e,fill:s,opacity:.2})}setBounds(t){this.strokeArea.set(t),this.fillArea.set(t)}}var p;!function(t){t[t.No=0]="No",t[t.Yes=1]="Yes",t[t.NoAndSkip=2]="NoAndSkip",t[t.YesAndSkip=3]="YesAndSkip"}(p||(p={}));const{No:v,Yes:y,NoAndSkip:w,YesAndSkip:x}=p,E={findOne:t=>t.list.find((t=>t.editable)),findBounds(t,e){if(t.__.hittable&&!t.__.locked&&e.hit(t.__world)){if(t.__.editable){if(t.isBranch&&!t.__.hitChildren)return t.__.hitSelf?x:w;if(t.isFrame)return e.includes(t.__layout.boxBounds,t.__world)?x:v;if(e.hit(t.__layout.boxBounds,t.__world)&&t.__.hitSelf)return y}return v}return t.isBranch?w:v}},{findOne:k}=E;class m extends e.Group{get dragging(){return!!this.originList}get running(){return this.editor.hittable&&this.editor.config.selector}get isMoveMode(){return this.app&&this.app.interaction.moveMode}constructor(t){super(),this.hoverStroker=new u,this.targetStroker=new u,this.bounds=new e.Bounds,this.selectArea=new f,this.__eventIds=[],this.editor=t,this.addMany(this.targetStroker,this.hoverStroker,this.selectArea),this.__listenEvents()}onHover(){const{editor:t}=this;if(!this.running||this.dragging||t.dragging)this.hoverStroker.target=null;else{const{stroke:e,strokeWidth:i,hover:s}=t.config;this.hoverStroker.setTarget(s?this.editor.hoverTarget:null,{stroke:e,strokeWidth:i})}}onSelect(){if(this.running){const{config:t,list:e}=this.editor,{stroke:i,strokeWidth:s}=t;this.targetStroker.setTarget(e,{stroke:i,strokeWidth:Math.max(1,s/2)}),this.hoverStroker.target=null}}update(){this.running&&this.targetStroker.forceUpdate()}onPointerMove(t){if(this.running&&!this.isMoveMode){const e=t.shiftKey?this.findDeepOne(t):k(t.path);this.editor.hoverTarget=this.editor.hasItem(e)?null:e}this.isMoveMode&&(this.editor.hoverTarget=null)}onBeforeDown(t){if(this.running&&!this.isMoveMode&&!t.middle){const e=this.lastDownLeaf=k(t.path);e?(t.shiftKey?this.editor.shiftItem(e):this.editor.target=e,this.editor.updateLayout(),e.leafer.interaction.updateDownData()):this.allow(t.target)&&(t.shiftKey||(this.editor.target=null))}}onTap(t){if(this.running&&t.shiftKey&&!t.middle&&!this.lastDownLeaf){const e=this.findDeepOne(t);e&&this.editor.shiftItem(e)}else this.isMoveMode&&(this.editor.target=null);this.lastDownLeaf=null}onDragStart(t){if(this.running&&this.allowDrag(t)){const{editor:e}=this,{stroke:i,strokeWidth:s,area:o}=e.config,{x:n,y:r}=t.getInner(this);this.bounds.set(n,r),this.selectArea.setStyle({visible:!0,stroke:i,strokeWidth:s,x:n,y:r},o),this.selectArea.setBounds(this.bounds.get()),this.originList=e.leafList.clone()}}onDrag(t){if(this.editor.dragging)this.onDragEnd();else if(this.dragging){const{editor:i}=this,s=t.getInnerTotal(this),o=this.bounds.clone().unsign(),n=new e.LeafList(i.app.find(E.findBounds,o));if(this.bounds.width=s.x,this.bounds.height=s.y,this.selectArea.setBounds(o.get()),n.length){const t=[];this.originList.forEach((e=>{n.has(e)||t.push(e)})),n.forEach((e=>{this.originList.has(e)||t.push(e)})),(t.length!==i.list.length||i.list.some(((e,i)=>e!==t[i])))&&(i.target=t)}else i.target=this.originList.list,i.leafList.length&&i.update()}}onDragEnd(){this.dragging&&(this.originList=null,this.selectArea.visible=!1)}onAutoMove(t){if(this.dragging){const{x:e,y:i}=t.getLocalMove(this);this.bounds.x+=e,this.bounds.y+=i}}allow(t){return t.leafer!==this.editor.leafer}allowDrag(t){return!(!this.editor.config.boxSelect||t.target.draggable)&&(!this.editor.hasTarget&&this.allow(t.target)||t.shiftKey&&!k(t.path))}findDeepOne(t){const i={exclude:new e.LeafList(this.editor.editBox.rect)};return k(t.target.leafer.interaction.findPath(t,i))}__listenEvents(){const{editor:t}=this;t.waitLeafer((()=>{const{app:i}=t;i.selector.proxy=t,this.__eventIds=[t.on_(s.HOVER,this.onHover,this),t.on_(s.SELECT,this.onSelect,this),i.on_(e.PointerEvent.MOVE,this.onPointerMove,this),i.on_(e.PointerEvent.BEFORE_DOWN,this.onBeforeDown,this),i.on_(e.PointerEvent.TAP,this.onTap,this),i.on_(e.DragEvent.START,this.onDragStart,this),i.on_(e.DragEvent.DRAG,this.onDrag,this),i.on_(e.DragEvent.END,this.onDragEnd,this),i.on_(e.MoveEvent.MOVE,this.onAutoMove,this),i.on_([e.ZoomEvent.ZOOM,e.MoveEvent.MOVE],(()=>{this.editor.hoverTarget=null}))]}))}__removeListenEvents(){this.__eventIds&&(this.off_(this.__eventIds),this.__eventIds.length=0)}destroy(){this.editor=this.originList=this.lastDownLeaf=null,this.__removeListenEvents(),super.destroy()}}var b;!function(t){t[t.topLeft=0]="topLeft",t[t.top=1]="top",t[t.topRight=2]="topRight",t[t.right=3]="right",t[t.bottomRight=4]="bottomRight",t[t.bottom=5]="bottom",t[t.bottomLeft=6]="bottomLeft",t[t.left=7]="left"}(b||(b={}));const{topLeft:_,top:L,topRight:S,right:T,bottomRight:P,bottom:D,bottomLeft:O,left:A}=b,{toPoint:M}=e.AroundHelper,R={getScaleData(t,e,i,s,o){let n,r=1,a=1;const{width:h,height:l}=t;o&&(i.x*=2,i.y*=2);const d=(-i.y+l)/l,c=(i.x+h)/h,g=(i.y+l)/l,u=(-i.x+h)/h;switch(e){case L:a=d,n={x:.5,y:1};break;case T:r=c,n={x:0,y:.5};break;case D:a=g,n={x:.5,y:0};break;case A:r=u,n={x:1,y:.5};break;case _:a=d,r=u,n={x:1,y:1};break;case S:a=d,r=c,n={x:0,y:1};break;case P:a=g,r=c,n={x:0,y:0};break;case O:a=g,r=u,n={x:1,y:0}}return s&&(1!==r?a=r:r=a),M(o||n,t,n),{origin:n,scaleX:r,scaleY:a,direction:e,lockRatio:s,around:o}},getRotateData(t,i,s,o,n){let r;switch(i){case _:r={x:1,y:1};break;case S:r={x:0,y:1};break;case P:r={x:0,y:0};break;case O:r={x:1,y:0};break;default:r={x:.5,y:.5}}return M(n||r,t,r),{origin:r,rotation:e.PointHelper.getRotation(o,r,s)}},getSkewData(t,i,s,o){let n,r,a=0,h=0;switch(i){case L:r={x:.5,y:0},n={x:.5,y:1},a=1;break;case D:r={x:.5,y:1},n={x:.5,y:0},a=1;break;case A:r={x:0,y:.5},n={x:1,y:.5},h=1;break;case T:r={x:1,y:.5},n={x:0,y:.5},h=1}const{x:l,y:d,width:c,height:g}=t;r.x=l+r.x*c,r.y=d+r.y*g,M(o||n,t,n);const u=e.PointHelper.getRotation(r,n,{x:r.x+(a?s.x:0),y:r.y+(h?s.y:0)});return a?a=-u:h=u,{origin:n,skewX:a,skewY:h}},getAround:(t,e)=>e&&!t?"center":t,getRotateDirection:(t,e,i=8)=>((t=(t+Math.round(e/(360/i)))%i)<0&&(t+=i),t)},{topLeft:B,top:z,topRight:I,right:W,bottomRight:Y,bottom:X,bottomLeft:K,left:C}=b;function F(t,e){const{editBox:i}=t,s=i.enterPoint;if(!s||!t.hasTarget||!i.visible)return;let{rotation:o}=i,{resizeCursor:n,rotateCursor:r,resizeable:a,rotateable:h}=t.config;const{direction:l,pointType:d}=s;i.enterPoint=s;const c="resize"===d;if(c&&h&&(e.metaKey||e.ctrlKey||!a)&&(n=r),i.flipped){const{flippedX:t,flippedY:e}=i;N(n=[...n],t,e),N(r=[...r],e,t),i.flippedOne&&(o=-o)}const g=R.getRotateDirection(l,o);s.cursor=c?n[g]:r[g]}function H(t){t.editBox.rect.cursor=t.config.moveCursor}function N(t,e,i){if(e){const e=t[z],i=t[B],s=t[I];t[z]=t[X],t[B]=t[K],t[I]=t[Y],t[X]=e,t[K]=i,t[Y]=s}if(i){const e=t[C],i=t[B],s=t[K];t[C]=t[W],t[B]=t[I],t[K]=t[Y],t[W]=e,t[I]=i,t[Y]=s}}class j extends e.Box{}const G=["top","right","bottom","left"];class V extends e.Group{get flipped(){return this.flippedX||this.flippedY}get flippedX(){return this.scaleX<0}get flippedY(){return this.scaleY<0}get flippedOne(){return this.scaleX*this.scaleY<0}constructor(t){super(),this.rect=new e.Box({name:"rect",hitFill:"all",hitStroke:"none",strokeAlign:"center",hitRadius:5}),this.circle=new j({name:"circle",strokeAlign:"outside",around:"center",cursor:"crosshair",hitRadius:5}),this.buttons=new e.Group({around:"center",hitSelf:!1}),this.resizePoints=[],this.rotatePoints=[],this.resizeLines=[],this.__eventIds=[],this.editor=t,this.visible=!1,this.create(),this.__listenEvents()}create(){let t,e,i;const{resizePoints:s,rotatePoints:o,resizeLines:n,rect:r,circle:a,buttons:h}=this,l=[{x:1,y:1},{x:.5,y:1},{x:0,y:1},{x:0,y:.5},{x:0,y:0},{x:.5,y:0},{x:1,y:0},{x:1,y:.5}];for(let r=0;r<8;r++)t=new j({around:l[r],width:15,height:15,hitFill:"all"}),o.push(t),this.listenPointEvents(t,"rotate",r),r%2&&(e=new j({name:"resize-line",around:"center",width:10,height:10,hitFill:"all"}),n.push(e),this.listenPointEvents(e,"resize",r)),i=new j({name:"resize-point",around:"center",strokeAlign:"outside",hitRadius:5}),s.push(i),this.listenPointEvents(i,"resize",r);h.add(a),this.listenPointEvents(a,"rotate",2),this.addMany(...o,r,h,...n,...s)}update(t){const{config:i,list:s}=this.editor,{width:o,height:n}=t,{rect:r,circle:a,resizePoints:h,rotatePoints:l,resizeLines:d}=this,{middlePoint:c,resizeable:g,rotateable:u,stroke:f,strokeWidth:p}=i,v=this.getPointsStyle(),y=this.getMiddlePointsStyle();this.visible=s[0]&&!s[0].locked;let w,x,E,k,m={};for(let i=0;i<8;i++)e.AroundHelper.toPoint(e.AroundHelper.directionData[i],t,m),w=this.getPointStyle(i%2?y[(i-1)/2%y.length]:v[i/2%v.length]),E=h[i],x=l[i],k=d[Math.floor(i/2)],E.set(w),E.set(m),x.set(m),k.set(m),E.visible=k.visible=g||u,x.visible=u&&g,i%2?(E.visible=x.visible=!!c,(i+1)/2%2?(k.width=o,E.width>o-30&&(E.visible=!1)):(k.height=n,E.rotation=90,E.width>n-30&&(E.visible=!1))):E.rotation=i/2*90;a.visible=u&&!!i.rotatePoint,a.set(this.getPointStyle(i.rotatePoint||v[0])),r.set(Object.assign({stroke:f,strokeWidth:p},i.rect||{})),r.set(Object.assign(Object.assign({},t),{visible:!0})),this.layoutButtons()}layoutButtons(){const{buttons:t,resizePoints:e}=this,{buttonsDirection:i,buttonsFixed:s,buttonsMargin:o,middlePoint:n}=this.editor.config,{flippedX:r,flippedY:a}=this;let h=G.indexOf(i);(h%2&&r||(h+1)%2&&a)&&s&&(h=(h+2)%4);const l=s?R.getRotateDirection(h,this.flippedOne?this.rotation:-this.rotation,4):h,d=e[2*l+1],c=l%2,g=l&&3!==l?1:-1,u=(o+(h%2?(n?d.width:0)+t.boxBounds.width:(n?d.height:0)+t.boxBounds.height)/2)*g;c?(t.x=d.x+u,t.y=d.y):(t.x=d.x,t.y=d.y+u),s&&(t.rotation=90*(l-h),t.scaleX=r?-1:1,t.scaleY=a?-1:1)}getPointStyle(t){const{stroke:e,strokeWidth:i,pointFill:s,pointSize:o,pointRadius:n}=this.editor.config,r={fill:s,stroke:e,strokeWidth:i,width:o,height:o,cornerRadius:n};return t?Object.assign(r,t):r}getPointsStyle(){const{point:t}=this.editor.config;return t instanceof Array?t:[t]}getMiddlePointsStyle(){const{middlePoint:t}=this.editor.config;return t instanceof Array?t:t?[t]:this.getPointsStyle()}onDragStart(t){this.dragging=!0,"rect"===t.target.name&&(this.editor.opacity=this.editor.config.hideOnMove?0:1)}onDragEnd(t){this.dragging=!1,"rect"===t.target.name&&(this.editor.opacity=1)}onDrag(t){const{editor:e}=this;"rotate"===t.current.pointType||t.metaKey||t.ctrlKey||!e.config.resizeable?e.config.rotateable&&e.onRotate(t):e.onScale(t)}onArrow(t){if(this.editor.hasTarget){const e={x:0,y:0},i=t.shiftKey?10:1;switch(t.code){case"ArrowDown":e.y=i;break;case"ArrowUp":e.y=-i;break;case"ArrowLeft":e.x=-i;break;case"ArrowRight":e.x=i}(e.x||e.y)&&this.editor.move(e.x,e.y)}}onDoubleClick(){const{editor:t}=this;t.single&&t.element.isBranch}listenPointEvents(t,i,s){const{editor:o}=this;t.direction=s,t.pointType=i,t.on_(e.DragEvent.START,this.onDragStart,this),t.on_(e.DragEvent.DRAG,this.onDrag,this),t.on_(e.DragEvent.END,this.onDragEnd,this),t.on_(e.PointerEvent.LEAVE,(()=>this.enterPoint=null)),"circle"!==t.name&&t.on_(e.PointerEvent.ENTER,(e=>{this.enterPoint=t,F(o,e)}))}__listenEvents(){const{rect:t,editor:i}=this;this.__eventIds=[i.on_(s.SELECT,(()=>{this.visible=i.hasTarget})),t.on_(e.DragEvent.START,this.onDragStart,this),t.on_(e.DragEvent.DRAG,i.onMove,i),t.on_(e.DragEvent.END,this.onDragEnd,this),t.on_(e.PointerEvent.ENTER,(()=>H(i))),t.on_(e.PointerEvent.DOUBLE_CLICK,this.onDoubleClick,this)]}__removeListenEvents(){this.off_(this.__eventIds),this.__eventIds.length=0}destroy(){this.editor=null,this.__removeListenEvents(),super.destroy()}}const U={editSize:"auto",stroke:"#836DFF",strokeWidth:2,pointFill:"#FFFFFF",pointSize:8,pointRadius:16,rotateGap:45,buttonsDirection:"bottom",buttonsMargin:12,moveCursor:"move",resizeCursor:["nwse-resize","ns-resize","nesw-resize","ew-resize","nwse-resize","ns-resize","nesw-resize","ew-resize"],rotateCursor:["ne-resize","e-resize","se-resize","s-resize","sw-resize","w-resize","nw-resize","n-resize"],selector:!0,hover:!0,boxSelect:!0,resizeable:!0,rotateable:!0,skewable:!0};class Z{constructor(){this.tag="EditTool"}onMove(t){const{moveX:e,moveY:i,editor:s}=t,{app:o,list:n}=s;o.lockLayout(),n.forEach((t=>{const s=t.getLocalPoint({x:e,y:i},null,!0);t.move(s.x,s.y)})),o.unlockLayout()}onScale(t){const{scaleX:e,scaleY:i,transform:s,worldOrigin:o,editor:n}=t,{app:r,list:a}=n;r.lockLayout(),a.forEach((t=>{const r="size"===n.getEditSize(t);s?t.transform(s,r):t.scaleOf(t.getInnerPoint(o),e,i,r)})),r.unlockLayout()}onRotate(t){const{rotation:e,worldOrigin:i,editor:s}=t,{app:o,list:n}=s;o.lockLayout(),n.forEach((t=>{t.rotateOf(t.getInnerPoint(i),e)})),o.unlockLayout()}onSkew(t){const{skewX:e,skewY:i,transform:s,worldOrigin:o,editor:n}=t,{app:r,list:a}=n;r.lockLayout(),a.forEach((t=>{const r="size"===n.getEditSize(t);s?t.transform(s,r):t.skewOf(t.getInnerPoint(o),e,i,r)})),r.unlockLayout()}update(t){const{simulateTarget:e,element:i}=t;t.multiple&&e.parent.updateLayout();const{x:s,y:o,scaleX:n,scaleY:r,rotation:a,skewX:h,skewY:l,width:d,height:c}=i.getLayoutBounds("box",t,!0);t.editBox.set({x:s,y:o,scaleX:n,scaleY:r,rotation:a,skewX:h,skewY:l}),t.editBox.update({x:0,y:0,width:d,height:c})}}Z.list=[];const{left:q,right:J}=b;class Q extends Z{constructor(){super(...arguments),this.tag="LineEditTool",this.scaleOfEvent=!0}onScaleWithDrag(t){const{drag:e,direction:i,lockRatio:s,around:o}=t,n=t.target,r={x:0,y:0},{toPoint:a}=n;n.rotation=0;let{x:h,y:l}=e.getInnerMove(n);s&&(Math.abs(h)>Math.abs(l)?l=0:h=0),i===q?(r.x+=h,r.y+=l,o&&(a.x-=h,a.y-=l)):(o&&(r.x-=h,r.y-=l),a.x+=h,a.y+=l),n.getLocalPointByInner(r,null,null,!0),n.getLocalPointByInner(a,null,null,!0),n.x=r.x,n.y=r.y,n.getInnerPointByLocal(a,null,null,!0),n.toPoint=a}onSkew(t){}update(t){const{rotatePoints:e,resizeLines:i,resizePoints:s}=t.editBox;super.update(t);for(let t=0;t<8;t++)t<4&&(i[t].visible=!1),s[t].visible=e[t].visible=t===q||t===J}}const $=(t,e)=>t.parent.children.indexOf(t)-e.parent.children.indexOf(e),tt=(t,e)=>e.parent.children.indexOf(e)-t.parent.children.indexOf(t),et={group(t,i,s){t.sort(tt);const{app:o,parent:n}=t[0];s||(s=new e.Group),n.addAt(s,n.children.indexOf(t[0])),t.sort($);const r=new e.Matrix(i.worldTransform);return r.divideParent(n.worldTransform),s.setTransform(r),s.editable=!0,s.hitChildren=!1,o.lockLayout(),t.forEach((t=>t.dropTo(s))),o.unlockLayout(),s},ungroup(t){const{app:e}=t[0],i=[];return e.lockLayout(),t.forEach((t=>{if(t.isBranch){const{parent:e,children:s}=t;for(;s.length;)i.push(s[0]),s[0].dropTo(e,e.children.indexOf(t));t.remove()}else i.push(t)})),e.unlockLayout(),i},toTop(t){t.sort($),t.forEach((t=>{t.parent&&t.parent.add(t)}))},toBottom(t){t.sort(tt),t.forEach((t=>{t.parent&&t.parent.addAt(t,0)}))}};class it extends e.Group{get list(){return this.leafList.list}get hasTarget(){return!!this.list.length}get multiple(){return this.list.length>1}get single(){return 1===this.list.length}get element(){return this.multiple?this.simulateTarget:this.list[0]}get buttons(){return this.editBox.buttons}get dragging(){return this.editBox.dragging}constructor(t,i){super(i),this.config=U,this.leafList=new e.LeafList,this.simulateTarget=new e.Rect({visible:!1}),this.editBox=new V(this),this.selector=new m(this),this.targetEventIds=[],t&&(this.config=e.DataHelper.default(t,this.config)),this.addMany(this.selector,this.editBox)}hasItem(t){return this.leafList.has(t)}addItem(t){this.hasItem(t)||t.locked||(this.leafList.add(t),this.target=this.leafList.list)}removeItem(t){this.hasItem(t)&&(this.leafList.remove(t),this.target=this.leafList.list)}shiftItem(t){this.hasItem(t)?this.removeItem(t):this.addItem(t)}update(){this.target&&(this.editTool.update(this),this.selector.update())}updateEditTool(){this.editTool=function(t){if(1===t.length){const i=t[0];return i instanceof e.Line&&!i.points?new Q:new Z}return new Z}(this.list)}getEditSize(t){let{editSize:e}=this.config;return"auto"===e?t.editSize:e}onMove(t){const e=t.getLocalMove(this.element);t.shiftKey&&(Math.abs(e.x)>Math.abs(e.y)?e.y=0:e.x=0),this.move(e.x,e.y)}onScale(t){const{element:e}=this,{direction:i}=t.current;let{around:s,lockRatio:o}=this.config;t.shiftKey&&(o=!0);const n=R.getScaleData(e.boxBounds,i,t.getInnerMove(e),o,R.getAround(s,t.altKey));this.editTool.onScaleWithDrag?(n.drag=t,this.scaleWithDrag(n)):this.scaleOf(n.origin,n.scaleX,n.scaleY)}onRotate(t){const{skewable:i,around:s,rotateGap:o}=this.config,{direction:n,name:r}=t.current;if(i&&"resize-line"===r)return this.onSkew(t);const{element:a,editBox:h}=this;let l,d;if(t instanceof e.RotateEvent)d=t.rotation,l=a.getInnerPoint(t);else{const e={x:t.x-t.moveX,y:t.y-t.moveY},i=R.getRotateData(a.boxBounds,n,t.getInner(a),a.getInnerPoint(e),t.shiftKey?null:s||"center");d=i.rotation,l=i.origin}d=e.MathHelper.getGapRotation(d,o,a.rotation),d&&(h.flippedOne&&(d=-d),this.rotateOf(l,d))}onSkew(t){const{element:e}=this,{around:i}=this.config,{origin:s,skewX:o,skewY:n}=R.getSkewData(e.boxBounds,t.current.direction,t.getInnerMove(e),R.getAround(i,t.altKey));(o||n)&&this.skewOf(s,o,n)}move(t,e){const{element:i}=this,s=i.getWorldPointByLocal({x:t,y:e},null,!0),n=new o(o.MOVE,{target:i,editor:this,moveX:s.x,moveY:s.y});this.editTool.onMove(n),this.emitEvent(n),this.multiple&&i.move(t,e)}scaleWithDrag(t){const{element:e}=this,i=e.getWorldPoint(t.origin),s=new n(n.SCALE,Object.assign(Object.assign({},t),{target:e,editor:this,worldOrigin:i}));this.editTool.onScaleWithDrag(s),this.emitEvent(s)}scaleOf(t,i,s=i,o){const{element:r}=this,a=r.getWorldPoint(t);let h;if(this.multiple){const o=Object.assign({},r.localTransform);r.scaleOf(t,i,s),h=new e.Matrix(r.localTransform).divide(o)}const l=new n(n.SCALE,{target:r,editor:this,worldOrigin:a,scaleX:i,scaleY:s,transform:h});this.editTool.onScale(l),this.emitEvent(l)}rotateOf(t,e){const{element:i}=this,s=i.getWorldPoint(t),o=new r(r.ROTATE,{target:i,editor:this,worldOrigin:s,rotation:e});this.editTool.onRotate(o),this.emitEvent(o),this.multiple&&i.rotateOf(t,e)}skewOf(t,i,s=0,o){const{element:n}=this,r=n.getWorldPoint(t);let h;if(this.multiple){const o=Object.assign({},n.localTransform);n.skewOf(t,i,s),h=new e.Matrix(n.localTransform).divide(o)}const l=new a(a.SKEW,{target:n,editor:this,skewX:i,skewY:s,transform:h,worldOrigin:r});this.editTool.onSkew(l),this.emitEvent(l)}group(t){return this.multiple&&(this.target=et.group(this.list,this.element,t)),this.target}ungroup(){return this.list.length&&(this.target=et.ungroup(this.list)),this.list}lock(){this.list.forEach((t=>t.locked=!0)),this.update()}unlock(){this.list.forEach((t=>t.locked=!1)),this.update()}toTop(){this.list.length&&(et.toTop(this.list),this.leafList.update())}toBottom(){this.list.length&&(et.toBottom(this.list),this.leafList.update())}listenTargetEvents(){if(!this.targetEventIds.length){const{leafer:t}=this.list[0];this.targetEventIds=[t.on_(e.RenderEvent.START,this.update,this),t.on_([e.KeyEvent.HOLD,e.KeyEvent.UP],(t=>{F(this,t)})),t.on_(e.KeyEvent.DOWN,this.editBox.onArrow,this.editBox)]}}removeTargetEvents(){const{targetEventIds:t}=this;t.length&&(this.off_(t),t.length=0)}destroy(){this.destroyed||(this.simulateTarget.destroy(),this.target=this.hoverTarget=this.simulateTarget=null,super.destroy())}}return i([h((function(t,e){t.emitEvent(new s(s.HOVER,{editor:t,value:t.hoverTarget,oldValue:e}))}))],it.prototype,"hoverTarget",void 0),i([h((function(t,i){const{target:o}=t;o?t.leafList=o instanceof e.LeafList?o:new e.LeafList(o):t.leafList.reset(),t.emitEvent(new s(s.SELECT,{editor:t,value:o,oldValue:i})),t.hasTarget?t.waitLeafer((()=>{t.multiple&&function(t){const{simulateTarget:i,leafList:s}=t,{x:o,y:n,width:r,height:a}=(new e.Bounds).setListWithFn(s.list,(t=>t.worldBoxBounds)),h=i.parent=s.list[0].leafer.zoomLayer,{scaleX:l,scaleY:d,e:c,f:g}=h.__world;i.reset({x:(o-c)/l,y:(n-g)/d,width:r/l,height:a/d})}(t),H(t),t.updateEditTool(),t.update(),t.listenTargetEvents()})):t.removeTargetEvents()}))],it.prototype,"target",void 0),e.Creator.editor=function(t){return new it(t)},t.EditBox=V,t.EditDataHelper=R,t.EditPoint=j,t.EditSelect=m,t.EditSelectHelper=E,t.EditTool=Z,t.Editor=it,t.EditorEvent=s,t.EditorHelper=et,t.EditorMoveEvent=o,t.EditorRotateEvent=r,t.EditorScaleEvent=n,t.EditorSkewEvent=a,t.LineEditTool=Q,t.SelectArea=f,t.Stroker=u,t}({},LeaferUI);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@leafer-in/editor",
|
|
3
|
-
"version": "1.0.0-rc.
|
|
3
|
+
"version": "1.0.0-rc.8",
|
|
4
4
|
"description": "@leafer-in/editor",
|
|
5
5
|
"author": "Chao (Leafer) Wan",
|
|
6
6
|
"license": "MIT",
|
|
@@ -28,10 +28,10 @@
|
|
|
28
28
|
"leaferjs"
|
|
29
29
|
],
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"@leafer-ui/core": "1.0.0-rc.
|
|
31
|
+
"@leafer-ui/core": "1.0.0-rc.8"
|
|
32
32
|
},
|
|
33
33
|
"devDependencies": {
|
|
34
|
-
"@leafer-ui/interface": "1.0.0-rc.
|
|
35
|
-
"@leafer-in/interface": "1.0.0-rc.
|
|
34
|
+
"@leafer-ui/interface": "1.0.0-rc.8",
|
|
35
|
+
"@leafer-in/interface": "1.0.0-rc.8"
|
|
36
36
|
}
|
|
37
37
|
}
|
package/src/Editor.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { IGroupInputData, IUI, IEventListenerId, IPointData, ILeafList, IEditSize } from '@leafer-ui/interface'
|
|
1
|
+
import { IGroupInputData, IUI, IEventListenerId, IPointData, ILeafList, IEditSize, IGroup } from '@leafer-ui/interface'
|
|
2
2
|
import { Group, Rect, DragEvent, RotateEvent, DataHelper, MathHelper, LeafList, Matrix, RenderEvent, KeyEvent } from '@leafer-ui/core'
|
|
3
3
|
|
|
4
4
|
import { IEditBox, IEditPoint, IEditor, IEditorConfig, IEditTool, IEditorScaleEvent } from '@leafer-in/interface'
|
|
@@ -29,7 +29,7 @@ export class Editor extends Group implements IEditor {
|
|
|
29
29
|
public hoverTarget: IUI
|
|
30
30
|
|
|
31
31
|
@targetAttr(onTarget)
|
|
32
|
-
public target: IUI | IUI[]
|
|
32
|
+
public target: IUI | IUI[]
|
|
33
33
|
|
|
34
34
|
public leafList: ILeafList = new LeafList() // from target
|
|
35
35
|
public get list(): IUI[] { return this.leafList.list as IUI[] }
|
|
@@ -238,13 +238,15 @@ export class Editor extends Group implements IEditor {
|
|
|
238
238
|
|
|
239
239
|
// group
|
|
240
240
|
|
|
241
|
-
public group():
|
|
242
|
-
if (this.multiple) this.target = EditorHelper.group(this.list, this.element)
|
|
241
|
+
public group(group?: IGroup): IGroup {
|
|
242
|
+
if (this.multiple) this.target = EditorHelper.group(this.list, this.element, group)
|
|
243
|
+
return this.target as IGroup
|
|
243
244
|
}
|
|
244
245
|
|
|
245
246
|
|
|
246
|
-
public ungroup():
|
|
247
|
+
public ungroup(): IUI[] {
|
|
247
248
|
if (this.list.length) this.target = EditorHelper.ungroup(this.list)
|
|
249
|
+
return this.list
|
|
248
250
|
}
|
|
249
251
|
|
|
250
252
|
// lock
|
package/src/decorator/data.ts
CHANGED
|
@@ -7,7 +7,10 @@ export function targetAttr(fn: IFunction) {
|
|
|
7
7
|
const privateKey = '_' + key
|
|
8
8
|
defineKey(target, key, {
|
|
9
9
|
get() { return (this as IObject)[privateKey] },
|
|
10
|
-
set(value: unknown) {
|
|
10
|
+
set(value: unknown) {
|
|
11
|
+
const old = (this as IObject)[privateKey]
|
|
12
|
+
if (old !== value) (this as IObject)[privateKey] = value, fn(this, old)
|
|
13
|
+
}
|
|
11
14
|
} as ThisType<ILeaf>)
|
|
12
15
|
}
|
|
13
16
|
}
|
package/src/display/EditBox.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { IRect, IAround, IEventListenerId, IBoundsData, IRectInputData, IPointData, IKeyEvent, IGroup, IBox } from '@leafer-ui/interface'
|
|
2
|
-
import { Group, DragEvent, PointerEvent, Box } from '@leafer-ui/core'
|
|
2
|
+
import { Group, DragEvent, PointerEvent, Box, AroundHelper } from '@leafer-ui/core'
|
|
3
3
|
|
|
4
4
|
import { IEditBox, IEditor, IDirection8, IEditPoint, IEditPointType } from '@leafer-in/interface'
|
|
5
5
|
|
|
@@ -78,19 +78,18 @@ export class EditBox extends Group implements IEditBox {
|
|
|
78
78
|
const { rect, circle, resizePoints, rotatePoints, resizeLines } = this
|
|
79
79
|
const { middlePoint, resizeable, rotateable, stroke, strokeWidth } = config
|
|
80
80
|
|
|
81
|
-
const points = this.getDirection8Points(bounds)
|
|
82
81
|
const pointsStyle = this.getPointsStyle()
|
|
83
82
|
const middlePointsStyle = this.getMiddlePointsStyle()
|
|
84
83
|
|
|
85
84
|
this.visible = list[0] && !list[0].locked // check locked
|
|
86
85
|
|
|
87
|
-
let point
|
|
86
|
+
let point = {} as IPointData, style: IRectInputData, rotateP: IRect, resizeP: IRect, resizeL: IRect
|
|
88
87
|
|
|
89
88
|
for (let i = 0; i < 8; i++) {
|
|
90
89
|
|
|
91
|
-
|
|
90
|
+
AroundHelper.toPoint(AroundHelper.directionData[i], bounds, point)
|
|
91
|
+
style = this.getPointStyle((i % 2) ? middlePointsStyle[((i - 1) / 2) % middlePointsStyle.length] : pointsStyle[(i / 2) % pointsStyle.length])
|
|
92
92
|
resizeP = resizePoints[i], rotateP = rotatePoints[i], resizeL = resizeLines[Math.floor(i / 2)]
|
|
93
|
-
|
|
94
93
|
resizeP.set(style)
|
|
95
94
|
resizeP.set(point), rotateP.set(point), resizeL.set(point)
|
|
96
95
|
|
|
@@ -178,20 +177,6 @@ export class EditBox extends Group implements IEditBox {
|
|
|
178
177
|
return middlePoint instanceof Array ? middlePoint : (middlePoint ? [middlePoint] : this.getPointsStyle())
|
|
179
178
|
}
|
|
180
179
|
|
|
181
|
-
public getDirection8Points(bounds: IBoundsData): IPointData[] {
|
|
182
|
-
const { x, y, width, height } = bounds
|
|
183
|
-
return [ // topLeft, top, topRight, right, bottomRight, bottom, bottomLeft, left
|
|
184
|
-
{ x, y },
|
|
185
|
-
{ x: x + width / 2, y },
|
|
186
|
-
{ x: x + width, y },
|
|
187
|
-
{ x: x + width, y: y + height / 2 },
|
|
188
|
-
{ x: x + width, y: y + height },
|
|
189
|
-
{ x: x + width / 2, y: y + height },
|
|
190
|
-
{ x, y: y + height },
|
|
191
|
-
{ x, y: y + height / 2 }
|
|
192
|
-
]
|
|
193
|
-
}
|
|
194
|
-
|
|
195
180
|
// drag
|
|
196
181
|
|
|
197
182
|
protected onDragStart(e: DragEvent): void {
|
|
@@ -151,11 +151,13 @@ export class EditSelect extends Group implements IEditSelect {
|
|
|
151
151
|
this.originList.forEach(item => { if (!list.has(item)) selectList.push(item) })
|
|
152
152
|
list.forEach(item => { if (!this.originList.has(item)) selectList.push(item) })
|
|
153
153
|
|
|
154
|
-
editor.
|
|
154
|
+
if (selectList.length !== editor.list.length || editor.list.some((child, index) => child !== selectList[index])) {
|
|
155
|
+
editor.target = selectList as IUI[]
|
|
156
|
+
}
|
|
155
157
|
|
|
156
158
|
} else {
|
|
157
159
|
|
|
158
|
-
editor.target = this.originList
|
|
160
|
+
editor.target = this.originList.list as IUI[]
|
|
159
161
|
if (editor.leafList.length) editor.update()
|
|
160
162
|
|
|
161
163
|
}
|
package/src/editor/target.ts
CHANGED
|
@@ -7,7 +7,7 @@ import { updateMoveCursor } from './cursor'
|
|
|
7
7
|
import { EditorEvent } from '../event/EditorEvent'
|
|
8
8
|
|
|
9
9
|
|
|
10
|
-
export function onTarget(editor: IEditor): void {
|
|
10
|
+
export function onTarget(editor: IEditor, oldValue: IUI | IUI[]): void {
|
|
11
11
|
const { target } = editor
|
|
12
12
|
if (target) {
|
|
13
13
|
editor.leafList = target instanceof LeafList ? target : new LeafList(target instanceof Array ? target : target as IUI)
|
|
@@ -15,7 +15,7 @@ export function onTarget(editor: IEditor): void {
|
|
|
15
15
|
editor.leafList.reset()
|
|
16
16
|
}
|
|
17
17
|
|
|
18
|
-
editor.emitEvent(new EditorEvent(EditorEvent.SELECT, { editor }))
|
|
18
|
+
editor.emitEvent(new EditorEvent(EditorEvent.SELECT, { editor, value: target, oldValue }))
|
|
19
19
|
|
|
20
20
|
if (editor.hasTarget) {
|
|
21
21
|
editor.waitLeafer(() => {
|
|
@@ -31,8 +31,7 @@ export function onTarget(editor: IEditor): void {
|
|
|
31
31
|
}
|
|
32
32
|
|
|
33
33
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
editor.emitEvent(new EditorEvent(EditorEvent.HOVER, { editor }))
|
|
34
|
+
export function onHover(editor: IEditor, oldValue: IUI): void {
|
|
35
|
+
editor.emitEvent(new EditorEvent(EditorEvent.HOVER, { editor, value: editor.hoverTarget, oldValue }))
|
|
37
36
|
|
|
38
37
|
}
|
package/src/event/EditorEvent.ts
CHANGED
|
@@ -12,6 +12,9 @@ export class EditorEvent extends Event implements IEditorEvent {
|
|
|
12
12
|
declare readonly target: IUI
|
|
13
13
|
readonly editor: IEditor
|
|
14
14
|
|
|
15
|
+
readonly value: IUI | IUI[]
|
|
16
|
+
readonly oldValue: IUI | IUI[]
|
|
17
|
+
|
|
15
18
|
readonly worldOrigin: IPointData
|
|
16
19
|
declare readonly origin: IPointData
|
|
17
20
|
|
|
@@ -137,7 +137,7 @@ export const EditDataHelper = {
|
|
|
137
137
|
|
|
138
138
|
|
|
139
139
|
getAround(around: IAround, altKey: boolean): IAround {
|
|
140
|
-
return (altKey && !around) ?
|
|
140
|
+
return (altKey && !around) ? 'center' : around
|
|
141
141
|
},
|
|
142
142
|
|
|
143
143
|
getRotateDirection(direction: number, rotation: number, totalDirection = 8): number {
|
|
@@ -52,43 +52,14 @@ export const EditorHelper = {
|
|
|
52
52
|
toTop(list: IUI[]): void {
|
|
53
53
|
list.sort(order)
|
|
54
54
|
list.forEach(leaf => {
|
|
55
|
-
|
|
56
|
-
const { parent } = leaf
|
|
57
|
-
if (parent) {
|
|
58
|
-
const { children } = parent
|
|
59
|
-
const top = children.length - 1
|
|
60
|
-
const zIndexOfTop = children[top].__.zIndex
|
|
61
|
-
const current = children.indexOf(leaf)
|
|
62
|
-
if (current !== top) {
|
|
63
|
-
children.splice(current, 1)
|
|
64
|
-
children.push(leaf)
|
|
65
|
-
zIndex = zIndexOfTop + 1
|
|
66
|
-
} else {
|
|
67
|
-
zIndex = zIndexOfTop
|
|
68
|
-
}
|
|
69
|
-
leaf.zIndex = zIndex
|
|
70
|
-
}
|
|
55
|
+
if (leaf.parent) leaf.parent.add(leaf)
|
|
71
56
|
})
|
|
72
57
|
},
|
|
73
58
|
|
|
74
59
|
toBottom(list: IUI[]): void {
|
|
75
60
|
list.sort(reverseOrder)
|
|
76
61
|
list.forEach(leaf => {
|
|
77
|
-
|
|
78
|
-
const { parent } = leaf
|
|
79
|
-
if (parent) {
|
|
80
|
-
const { children } = parent
|
|
81
|
-
const zIndexOfBottom = children[0].__.zIndex
|
|
82
|
-
const current = children.indexOf(leaf)
|
|
83
|
-
if (current !== 0) {
|
|
84
|
-
children.splice(current, 1)
|
|
85
|
-
children.unshift(leaf)
|
|
86
|
-
zIndex = zIndexOfBottom - 1
|
|
87
|
-
} else {
|
|
88
|
-
zIndex = zIndexOfBottom
|
|
89
|
-
}
|
|
90
|
-
leaf.zIndex = zIndex
|
|
91
|
-
}
|
|
62
|
+
if (leaf.parent) leaf.parent.addAt(leaf, 0)
|
|
92
63
|
})
|
|
93
64
|
}
|
|
94
65
|
|
package/types/index.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { IBounds, ILeafList, IUI, IEventListenerId, ILeaf, IGroupInputData, IEditSize, IPointData, IBox, IGroup, IBoundsData, IRectInputData, IKeyEvent, IRect, ILeaferCanvas, IRenderOptions, IMatrixData, IDragEvent, IAround, AnswerType } from '@leafer-ui/interface';
|
|
1
|
+
import { IBounds, ILeafList, IUI, IEventListenerId, ILeaf, IGroup, IGroupInputData, IEditSize, IPointData, IBox, IBoundsData, IRectInputData, IKeyEvent, IRect, ILeaferCanvas, IRenderOptions, IMatrixData, IDragEvent, IAround, AnswerType } from '@leafer-ui/interface';
|
|
3
2
|
import { Group, PointerEvent, DragEvent, MoveEvent, RotateEvent, Box, UI, Event } from '@leafer-ui/core';
|
|
4
3
|
import { IEditSelect, IEditor, IStroker, ISelectArea, IEditorConfig, IEditBox, IEditTool, IEditorScaleEvent, IEditPoint, IEditPointType, IDirection8, IEditorEvent, IEditorMoveEvent, IEditorRotateEvent, IEditorSkewEvent } from '@leafer-in/interface';
|
|
5
4
|
|
|
@@ -37,7 +36,7 @@ declare class EditSelect extends Group implements IEditSelect {
|
|
|
37
36
|
declare class Editor extends Group implements IEditor {
|
|
38
37
|
config: IEditorConfig;
|
|
39
38
|
hoverTarget: IUI;
|
|
40
|
-
target: IUI | IUI[]
|
|
39
|
+
target: IUI | IUI[];
|
|
41
40
|
leafList: ILeafList;
|
|
42
41
|
get list(): IUI[];
|
|
43
42
|
get hasTarget(): boolean;
|
|
@@ -46,7 +45,7 @@ declare class Editor extends Group implements IEditor {
|
|
|
46
45
|
get element(): IUI;
|
|
47
46
|
simulateTarget: IUI;
|
|
48
47
|
editBox: IEditBox;
|
|
49
|
-
get buttons():
|
|
48
|
+
get buttons(): IGroup;
|
|
50
49
|
editTool: IEditTool;
|
|
51
50
|
selector: EditSelect;
|
|
52
51
|
get dragging(): boolean;
|
|
@@ -68,8 +67,8 @@ declare class Editor extends Group implements IEditor {
|
|
|
68
67
|
scaleOf(origin: IPointData, scaleX: number, scaleY?: number, _resize?: boolean): void;
|
|
69
68
|
rotateOf(origin: IPointData, rotation: number): void;
|
|
70
69
|
skewOf(origin: IPointData, skewX: number, skewY?: number, _resize?: boolean): void;
|
|
71
|
-
group():
|
|
72
|
-
ungroup():
|
|
70
|
+
group(group?: IGroup): IGroup;
|
|
71
|
+
ungroup(): IUI[];
|
|
73
72
|
lock(): void;
|
|
74
73
|
unlock(): void;
|
|
75
74
|
toTop(): void;
|
|
@@ -101,7 +100,6 @@ declare class EditBox extends Group implements IEditBox {
|
|
|
101
100
|
getPointStyle(userStyle?: IRectInputData): IRectInputData;
|
|
102
101
|
getPointsStyle(): IRectInputData[];
|
|
103
102
|
getMiddlePointsStyle(): IRectInputData[];
|
|
104
|
-
getDirection8Points(bounds: IBoundsData): IPointData[];
|
|
105
103
|
protected onDragStart(e: DragEvent): void;
|
|
106
104
|
protected onDragEnd(e: DragEvent): void;
|
|
107
105
|
protected onDrag(e: DragEvent): void;
|
|
@@ -140,6 +138,8 @@ declare class EditorEvent extends Event implements IEditorEvent {
|
|
|
140
138
|
static HOVER: string;
|
|
141
139
|
readonly target: IUI;
|
|
142
140
|
readonly editor: IEditor;
|
|
141
|
+
readonly value: IUI | IUI[];
|
|
142
|
+
readonly oldValue: IUI | IUI[];
|
|
143
143
|
readonly worldOrigin: IPointData;
|
|
144
144
|
readonly origin: IPointData;
|
|
145
145
|
constructor(type: string, data?: IEditorEvent);
|