@wbiokr/arrow 1.0.0 → 1.0.2
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/README.md +95 -8
- package/dist/bundle.cjs.js +8 -8
- package/dist/bundle.cjs.js.map +1 -1
- package/dist/bundle.esm.js +175 -149
- package/dist/bundle.esm.js.map +1 -1
- package/package.json +1 -1
package/dist/bundle.esm.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { Keybinding as
|
|
2
|
-
const
|
|
1
|
+
import { Keybinding as A } from "@wbiokr/keybinding";
|
|
2
|
+
const E = `
|
|
3
3
|
.ae-d2-container {
|
|
4
4
|
position: relative;
|
|
5
5
|
width: 1000px;
|
|
@@ -14,7 +14,7 @@ const S = `
|
|
|
14
14
|
position: absolute;
|
|
15
15
|
left: 0;
|
|
16
16
|
top: 0;
|
|
17
|
-
z-index:
|
|
17
|
+
z-index: 10;
|
|
18
18
|
}
|
|
19
19
|
|
|
20
20
|
|
|
@@ -75,7 +75,7 @@ const S = `
|
|
|
75
75
|
border: 1px solid #ddd;
|
|
76
76
|
border-radius: 8px;
|
|
77
77
|
box-shadow: 0 2px 12px rgba(0,0,0,0.15);
|
|
78
|
-
z-index:
|
|
78
|
+
z-index: 20;
|
|
79
79
|
pointer-events: auto;
|
|
80
80
|
min-width: 140px;
|
|
81
81
|
}
|
|
@@ -132,25 +132,25 @@ const S = `
|
|
|
132
132
|
}
|
|
133
133
|
`;
|
|
134
134
|
if (typeof document < "u") {
|
|
135
|
-
const
|
|
136
|
-
|
|
135
|
+
const v = document.createElement("style");
|
|
136
|
+
v.textContent = E, document.head.appendChild(v);
|
|
137
137
|
}
|
|
138
|
-
class
|
|
139
|
-
constructor(t,
|
|
140
|
-
this.container = typeof t == "string" ? document.querySelector(t) : t, this.svg = null, this.arrows = [], this.drawing = !1, this.current = null, this.selected = null, this.mode = null, this.lastClickTime = 0, this.lastClickPos = null, this._shouldBlockClick = !1, this._defaultColor = "#2b8cff", this.isCtrl =
|
|
138
|
+
class M {
|
|
139
|
+
constructor(t, e = {}) {
|
|
140
|
+
this.container = typeof t == "string" ? document.querySelector(t) : t, this.svg = null, this.arrows = [], this.drawing = !1, this.current = null, this.selected = null, this.mode = null, this.lastClickTime = 0, this.lastClickPos = null, this._shouldBlockClick = !1, this._defaultColor = "#2b8cff", this.isCtrl = e.isCtrl || !1, this.stopPropagation = e.stopPropagation || !1, this.preventDefault = e.preventDefault || !1, this.onDrawEnd = e.onDrawEnd || null, this.onRemove = e.onRemove || null, this.onChange = e.onChange || null, this._transformMode = null, this._transformHandle = null, this._transformStartPos = null, this._transformStartPoints = null, this._transformCenter = null, this._transformStartAngle = null, this._rotationOffset = 0, this._toolbar = null, this._strokeWidth = 8, this._curveRate = 0, this._createSVG(), this._initKeybindings(), this._initEvents();
|
|
141
141
|
}
|
|
142
142
|
_createSVG() {
|
|
143
|
-
getComputedStyle(this.container).position === "static" && (this.container.style.position = "relative"), this.svg = document.createElementNS("http://www.w3.org/2000/svg", "svg"), this.svg.setAttribute("class", "ae-d2-svg"), this.container.appendChild(this.svg);
|
|
144
|
-
const
|
|
145
|
-
this.svg.appendChild(
|
|
143
|
+
typeof getComputedStyle < "u" && getComputedStyle(this.container).position === "static" && (this.container.style.position = "relative"), this.svg = document.createElementNS("http://www.w3.org/2000/svg", "svg"), this.svg.setAttribute("class", "ae-d2-svg"), this.container.appendChild(this.svg);
|
|
144
|
+
const t = document.createElementNS("http://www.w3.org/2000/svg", "defs");
|
|
145
|
+
this.svg.appendChild(t);
|
|
146
146
|
const e = document.createElementNS("http://www.w3.org/2000/svg", "marker");
|
|
147
147
|
e.setAttribute("id", "ae-arrowhead"), e.setAttribute("markerWidth", "10"), e.setAttribute("markerHeight", "10"), e.setAttribute("refX", "9"), e.setAttribute("refY", "5"), e.setAttribute("orient", "auto");
|
|
148
|
-
const
|
|
149
|
-
|
|
148
|
+
const s = document.createElementNS("http://www.w3.org/2000/svg", "polygon");
|
|
149
|
+
s.setAttribute("points", "0,0 10,5 0,10 2,5"), s.setAttribute("fill", this._defaultColor), e.appendChild(s), t.appendChild(e);
|
|
150
150
|
}
|
|
151
151
|
_createToolbar() {
|
|
152
152
|
this._toolbar && this._toolbar.remove(), this._toolbar = document.createElement("div"), this._toolbar.className = "ae-toolbar";
|
|
153
|
-
const t = this.selected.color || this._defaultColor,
|
|
153
|
+
const t = this.selected.color || this._defaultColor, e = this.selected.strokeWidth || this._strokeWidth, s = this.selected.curveRate ?? this.selected.cornerRoundness ?? 0;
|
|
154
154
|
this._toolbar.innerHTML = `
|
|
155
155
|
<div class="ae-toolbar-drag-handle">:::</div>
|
|
156
156
|
<div class="ae-toolbar-item">
|
|
@@ -159,44 +159,44 @@ class L {
|
|
|
159
159
|
</div>
|
|
160
160
|
<div class="ae-toolbar-item">
|
|
161
161
|
<span class="ae-toolbar-label">粗细</span>
|
|
162
|
-
<input type="range" class="ae-stroke-input" min="1" max="60" value="${
|
|
163
|
-
<span class="ae-toolbar-value">${
|
|
162
|
+
<input type="range" class="ae-stroke-input" min="1" max="60" value="${e}" />
|
|
163
|
+
<span class="ae-toolbar-value">${e}px</span>
|
|
164
164
|
</div>
|
|
165
165
|
<div class="ae-toolbar-item">
|
|
166
166
|
<span class="ae-toolbar-label">曲线率</span>
|
|
167
|
-
<input type="range" class="ae-round-input" min="0" max="1" step="0.1" value="${
|
|
167
|
+
<input type="range" class="ae-round-input" min="0" max="1" step="0.1" value="${s}" />
|
|
168
168
|
</div>
|
|
169
|
-
`, this._toolbar.querySelector(".ae-color-input").addEventListener("input", (
|
|
170
|
-
this.selected && (this.selected.color =
|
|
169
|
+
`, this._toolbar.querySelector(".ae-color-input").addEventListener("input", (r) => {
|
|
170
|
+
this.selected && (this.selected.color = r.target.value, this._defaultColor = r.target.value, this._updateMarkerColor(r.target.value), this.render(), this.onChange && this.onChange({ type: "color", arrow: this.selected, value: r.target.value, arrows: this.getData() }));
|
|
171
171
|
});
|
|
172
|
-
const
|
|
173
|
-
|
|
174
|
-
this.selected && (this.selected.strokeWidth = parseInt(
|
|
175
|
-
}), this._toolbar.querySelector(".ae-round-input").addEventListener("input", (
|
|
176
|
-
this.selected && (this.selected.curveRate = parseFloat(
|
|
172
|
+
const i = this._toolbar.querySelector(".ae-stroke-input"), n = this._toolbar.querySelector(".ae-stroke-input + .ae-toolbar-value");
|
|
173
|
+
i.addEventListener("input", (r) => {
|
|
174
|
+
this.selected && (this.selected.strokeWidth = parseInt(r.target.value), n.textContent = r.target.value + "px", this.render(), this.onChange && this.onChange({ type: "strokeWidth", arrow: this.selected, value: parseInt(r.target.value), arrows: this.getData() }));
|
|
175
|
+
}), this._toolbar.querySelector(".ae-round-input").addEventListener("input", (r) => {
|
|
176
|
+
this.selected && (this.selected.curveRate = parseFloat(r.target.value), this.render(), this.onChange && this.onChange({ type: "curveRate", arrow: this.selected, value: parseFloat(r.target.value), arrows: this.getData() }));
|
|
177
177
|
});
|
|
178
178
|
const l = this._toolbar.querySelector(".ae-toolbar-drag-handle");
|
|
179
|
-
this._setupToolbarDrag(l), this._toolbar.addEventListener("mousedown", (
|
|
180
|
-
|
|
181
|
-
}), this._toolbar.addEventListener("mouseup", (
|
|
182
|
-
|
|
179
|
+
this._setupToolbarDrag(l), this._toolbar.addEventListener("mousedown", (r) => {
|
|
180
|
+
r.stopPropagation();
|
|
181
|
+
}), this._toolbar.addEventListener("mouseup", (r) => {
|
|
182
|
+
r.stopPropagation();
|
|
183
183
|
}), this.container.appendChild(this._toolbar);
|
|
184
184
|
}
|
|
185
185
|
// 工具条拖拽设置
|
|
186
186
|
_setupToolbarDrag(t) {
|
|
187
|
-
let
|
|
187
|
+
let e = !1, s = { x: 0, y: 0 };
|
|
188
188
|
t.addEventListener("mousedown", (o) => {
|
|
189
|
-
|
|
190
|
-
const
|
|
191
|
-
|
|
189
|
+
e = !0, this._toolbarDragging = !0;
|
|
190
|
+
const i = this._toolbar.getBoundingClientRect();
|
|
191
|
+
s.x = o.clientX - i.left, s.y = o.clientY - i.top, o.preventDefault(), o.stopPropagation();
|
|
192
192
|
const n = (l) => {
|
|
193
|
-
if (!
|
|
194
|
-
const
|
|
195
|
-
let h = l.clientX -
|
|
193
|
+
if (!e) return;
|
|
194
|
+
const r = this.container.getBoundingClientRect();
|
|
195
|
+
let h = l.clientX - r.left - s.x, c = l.clientY - r.top - s.y;
|
|
196
196
|
const d = this._toolbar.offsetWidth, u = this._toolbar.offsetHeight;
|
|
197
|
-
h = Math.max(0, Math.min(h,
|
|
197
|
+
h = Math.max(0, Math.min(h, r.width - d)), c = Math.max(0, Math.min(c, r.height - u)), this._toolbar.style.left = h + "px", this._toolbar.style.top = c + "px";
|
|
198
198
|
}, a = (l) => {
|
|
199
|
-
|
|
199
|
+
e = !1, this._toolbarDragging = !1, document.removeEventListener("mousemove", n), document.removeEventListener("mouseup", a), this._toolbar.removeEventListener("mouseup", a), l.preventDefault(), l.stopPropagation();
|
|
200
200
|
};
|
|
201
201
|
document.addEventListener("mousemove", n), document.addEventListener("mouseup", a), this._toolbar.addEventListener("mouseup", a);
|
|
202
202
|
});
|
|
@@ -204,11 +204,11 @@ class L {
|
|
|
204
204
|
_showToolbar() {
|
|
205
205
|
if (this.selected) {
|
|
206
206
|
this._toolbar || this._createToolbar(), this._toolbar.style.display = "flex";
|
|
207
|
-
const t = this.selected.color || this._defaultColor,
|
|
208
|
-
o && (o.value = t),
|
|
209
|
-
const l = this._getArrowBBox(this.selected),
|
|
207
|
+
const t = this.selected.color || this._defaultColor, e = this.selected.strokeWidth || this._strokeWidth, s = this.selected.curveRate ?? this.selected.cornerRoundness ?? 0, o = this._toolbar.querySelector(".ae-color-input"), i = this._toolbar.querySelector(".ae-stroke-input"), n = this._toolbar.querySelector(".ae-stroke-input + .ae-toolbar-value"), a = this._toolbar.querySelector(".ae-round-input");
|
|
208
|
+
o && (o.value = t), i && (i.value = e, n.textContent = e + "px"), a && (a.value = s);
|
|
209
|
+
const l = this._getArrowBBox(this.selected), r = this._toolbar.offsetWidth || 150, h = this._toolbar.offsetHeight || 120;
|
|
210
210
|
let c = l.maxX + 20;
|
|
211
|
-
c +
|
|
211
|
+
c + r > this.container.offsetWidth && (c = l.minX - r - 20);
|
|
212
212
|
let d = l.centerY - h / 2;
|
|
213
213
|
d < 0 && (d = 0), d + h > this.container.offsetHeight && (d = this.container.offsetHeight - h), this._toolbar.style.left = c + "px", this._toolbar.style.top = d + "px";
|
|
214
214
|
}
|
|
@@ -217,14 +217,18 @@ class L {
|
|
|
217
217
|
this._toolbar && (this._toolbar.style.display = "none");
|
|
218
218
|
}
|
|
219
219
|
_updateMarkerColor(t) {
|
|
220
|
-
const
|
|
221
|
-
|
|
220
|
+
const e = this.svg.querySelector("#ae-arrowhead polygon");
|
|
221
|
+
e && e.setAttribute("fill", t);
|
|
222
222
|
}
|
|
223
223
|
_initKeybindings() {
|
|
224
|
-
this._kb = new
|
|
224
|
+
this._kb = new A("arrow-editor"), this._kb.on("enter", () => {
|
|
225
225
|
this._handleEnter();
|
|
226
226
|
}), this._kb.on("esc", () => {
|
|
227
227
|
this._handleEsc();
|
|
228
|
+
}), this._kb.on("delete", () => {
|
|
229
|
+
this._handleDelete();
|
|
230
|
+
}), this._kb.on("backspace", () => {
|
|
231
|
+
this._handleDelete();
|
|
228
232
|
});
|
|
229
233
|
}
|
|
230
234
|
_initEvents() {
|
|
@@ -232,128 +236,132 @@ class L {
|
|
|
232
236
|
if (this._toolbar && (t.target === this._toolbar || this._toolbar.contains(t.target)))
|
|
233
237
|
return;
|
|
234
238
|
this.preventDefault && t.preventDefault(), this.stopPropagation && t.stopPropagation();
|
|
235
|
-
const
|
|
236
|
-
this._handleClick(
|
|
239
|
+
const e = this._mousePos(t);
|
|
240
|
+
this._handleClick(e, t);
|
|
237
241
|
}, this.container.onmousemove = (t) => {
|
|
238
242
|
this.preventDefault && t.preventDefault(), this.stopPropagation && t.stopPropagation();
|
|
239
|
-
const
|
|
240
|
-
this._handleMouseMove(t,
|
|
243
|
+
const e = this._mousePos(t);
|
|
244
|
+
this._handleMouseMove(t, e);
|
|
241
245
|
}, this.container.onmousedown = (t) => {
|
|
242
246
|
if (!(this._toolbar && (t.target === this._toolbar || this._toolbar.contains(t.target))) && (this.preventDefault && t.preventDefault(), this.stopPropagation && t.stopPropagation(), this.selected)) {
|
|
243
|
-
const
|
|
244
|
-
if (
|
|
245
|
-
this._transformMode =
|
|
246
|
-
const
|
|
247
|
-
if (this._transformStartPos =
|
|
248
|
-
const o = this._transformCenter,
|
|
249
|
-
this._transformStartAngle = Math.atan2(n,
|
|
247
|
+
const e = t.target.getAttribute("data-handle");
|
|
248
|
+
if (e) {
|
|
249
|
+
this._transformMode = e, this._transformHandle = e;
|
|
250
|
+
const s = this._mousePos(t);
|
|
251
|
+
if (this._transformStartPos = s, this._transformStartPoints = this.selected.points.map((o) => ({ x: o.x, y: o.y })), this._transformCenter = this._getArrowBBox(this.selected), e === "rotate") {
|
|
252
|
+
const o = this._transformCenter, i = s.x - o.centerX, n = s.y - o.centerY;
|
|
253
|
+
this._transformStartAngle = Math.atan2(n, i), this._rotationOffset = 0;
|
|
250
254
|
}
|
|
251
255
|
t.stopPropagation();
|
|
252
256
|
return;
|
|
253
257
|
}
|
|
254
258
|
this.mode = "move";
|
|
255
259
|
}
|
|
256
|
-
}, this.container.onmouseup = () => {
|
|
257
|
-
this.mode = null, this._transformMode = null, this._transformHandle = null;
|
|
260
|
+
}, this.container.onmouseup = (t) => {
|
|
261
|
+
this._transformMode && this.selected && this.onChange && this.onChange({ type: this._transformMode, arrow: this.selected, arrows: this.getData() }), this.mode = null, this._transformMode = null, this._transformHandle = null;
|
|
258
262
|
};
|
|
259
263
|
}
|
|
260
264
|
_mousePos(t) {
|
|
261
|
-
const
|
|
265
|
+
const e = this.container.getBoundingClientRect();
|
|
262
266
|
return {
|
|
263
|
-
x: t.clientX -
|
|
264
|
-
y: t.clientY -
|
|
267
|
+
x: t.clientX - e.left,
|
|
268
|
+
y: t.clientY - e.top
|
|
265
269
|
};
|
|
266
270
|
}
|
|
267
|
-
_generatePath(t,
|
|
271
|
+
_generatePath(t, e = 0) {
|
|
268
272
|
if (t.length < 2) return "";
|
|
269
|
-
if (
|
|
273
|
+
if (e === 0) {
|
|
270
274
|
let o = `M ${t[0].x} ${t[0].y}`;
|
|
271
|
-
for (let
|
|
272
|
-
o += ` L ${t[
|
|
275
|
+
for (let i = 1; i < t.length; i++)
|
|
276
|
+
o += ` L ${t[i].x} ${t[i].y}`;
|
|
273
277
|
return o;
|
|
274
278
|
}
|
|
275
|
-
let
|
|
279
|
+
let s = `M ${t[0].x} ${t[0].y}`;
|
|
276
280
|
for (let o = 0; o < t.length - 1; o++) {
|
|
277
|
-
const
|
|
278
|
-
if (
|
|
281
|
+
const i = t[o], n = t[o + 1], a = n.x - i.x, l = n.y - i.y, r = Math.hypot(a, l);
|
|
282
|
+
if (r === 0) continue;
|
|
279
283
|
let h = a, c = l;
|
|
280
284
|
if (o > 0) {
|
|
281
285
|
const m = t[o - 1];
|
|
282
|
-
h =
|
|
286
|
+
h = i.x - m.x, c = i.y - m.y;
|
|
283
287
|
}
|
|
284
288
|
let d = a, u = l;
|
|
285
289
|
if (o < t.length - 2) {
|
|
286
290
|
const m = t[o + 2];
|
|
287
291
|
d = m.x - n.x, u = m.y - n.y;
|
|
288
292
|
}
|
|
289
|
-
const p = Math.hypot(h, c) || 1, f = Math.hypot(d, u) || 1,
|
|
290
|
-
|
|
293
|
+
const p = Math.hypot(h, c) || 1, f = Math.hypot(d, u) || 1, w = h / p, y = c / p, _ = d / f, b = u / f, g = r * e * 0.3, x = i.x + w * g, k = i.y + y * g, C = n.x - _ * g, P = n.y - b * g;
|
|
294
|
+
s += ` C ${x} ${k} ${C} ${P} ${n.x} ${n.y}`;
|
|
291
295
|
}
|
|
292
|
-
return
|
|
296
|
+
return s;
|
|
293
297
|
}
|
|
294
|
-
_generateArrowHead(t,
|
|
298
|
+
_generateArrowHead(t, e = 8) {
|
|
295
299
|
if (t.length < 2) return "";
|
|
296
|
-
const
|
|
300
|
+
const s = t[t.length - 1];
|
|
297
301
|
let o = t[t.length - 2];
|
|
298
|
-
t.length >= 3 && Math.hypot(
|
|
299
|
-
const
|
|
302
|
+
t.length >= 3 && Math.hypot(s.x - o.x, s.y - o.y) < 5 && (o = t[t.length - 3]);
|
|
303
|
+
const i = s.x - o.x, n = s.y - o.y, a = Math.hypot(i, n);
|
|
300
304
|
if (a < 1) return "";
|
|
301
|
-
const l =
|
|
302
|
-
return `M ${
|
|
303
|
-
}
|
|
304
|
-
_createArrowGroup(t,
|
|
305
|
-
const
|
|
306
|
-
|
|
307
|
-
const o = t.color || this._defaultColor,
|
|
308
|
-
a.setAttribute("class", "ae-arrow-path"), a.style.stroke = o, a.style.strokeWidth =
|
|
305
|
+
const l = i / a, r = n / a, h = s.x, c = s.y, d = e / 8, u = 40 * d, p = 20 * d, f = 20 * d, w = s.x + l * f, y = s.y + r * f, _ = s.x - l * (u - f), b = s.y - r * (u - f), g = -r, x = l, k = _ + g * p, C = b + x * p, P = _ - g * p, m = b - x * p;
|
|
306
|
+
return `M ${w} ${y} L ${k} ${C} L ${h} ${c} L ${P} ${m} Z`;
|
|
307
|
+
}
|
|
308
|
+
_createArrowGroup(t, e) {
|
|
309
|
+
const s = document.createElementNS("http://www.w3.org/2000/svg", "g");
|
|
310
|
+
s.setAttribute("data-index", e);
|
|
311
|
+
const o = t.color || this._defaultColor, i = t.strokeWidth || this._strokeWidth, n = t.curveRate ?? t.cornerRoundness ?? 0, a = document.createElementNS("http://www.w3.org/2000/svg", "path");
|
|
312
|
+
a.setAttribute("class", "ae-arrow-path"), a.style.stroke = o, a.style.strokeWidth = i + "px", a.setAttribute("d", this._generatePath(t.points, n)), s.appendChild(a);
|
|
309
313
|
const l = document.createElementNS("http://www.w3.org/2000/svg", "path");
|
|
310
|
-
if (l.setAttribute("fill", o), l.setAttribute("d", this._generateArrowHead(t.points,
|
|
311
|
-
const
|
|
312
|
-
h.setAttribute("class", "ae-transform-box"), h.setAttribute("x",
|
|
314
|
+
if (l.setAttribute("fill", o), l.setAttribute("d", this._generateArrowHead(t.points, i)), s.appendChild(l), t === this.selected) {
|
|
315
|
+
const r = this._getArrowBBox(t), h = document.createElementNS("http://www.w3.org/2000/svg", "rect");
|
|
316
|
+
h.setAttribute("class", "ae-transform-box"), h.setAttribute("x", r.minX - 10), h.setAttribute("y", r.minY - 10), h.setAttribute("width", r.width + 20), h.setAttribute("height", r.height + 20), s.appendChild(h);
|
|
313
317
|
const c = document.createElementNS("http://www.w3.org/2000/svg", "circle");
|
|
314
|
-
c.setAttribute("class", "ae-transform-handle"), c.setAttribute("cx",
|
|
315
|
-
const d =
|
|
316
|
-
p.setAttribute("class", "ae-rotate-line"), p.setAttribute("x1",
|
|
318
|
+
c.setAttribute("class", "ae-transform-handle"), c.setAttribute("cx", r.maxX + 10), c.setAttribute("cy", r.maxY + 10), c.setAttribute("r", 6), c.setAttribute("data-handle", "scale"), s.appendChild(c);
|
|
319
|
+
const d = r.minY - 30, u = r.centerX, p = document.createElementNS("http://www.w3.org/2000/svg", "line");
|
|
320
|
+
p.setAttribute("class", "ae-rotate-line"), p.setAttribute("x1", r.centerX), p.setAttribute("y1", r.minY - 10), p.setAttribute("x2", u), p.setAttribute("y2", d), s.appendChild(p);
|
|
317
321
|
const f = document.createElementNS("http://www.w3.org/2000/svg", "circle");
|
|
318
|
-
f.setAttribute("class", "ae-rotate-handle"), f.setAttribute("cx", u), f.setAttribute("cy", d), f.setAttribute("r", 8), f.setAttribute("data-handle", "rotate"),
|
|
322
|
+
f.setAttribute("class", "ae-rotate-handle"), f.setAttribute("cx", u), f.setAttribute("cy", d), f.setAttribute("r", 8), f.setAttribute("data-handle", "rotate"), s.appendChild(f);
|
|
319
323
|
}
|
|
320
|
-
return
|
|
324
|
+
return s;
|
|
321
325
|
}
|
|
322
326
|
render() {
|
|
323
|
-
if (this.svg.querySelectorAll("g").forEach((t) => t.remove()), this.arrows.forEach((t,
|
|
324
|
-
const
|
|
325
|
-
this.svg.appendChild(
|
|
327
|
+
if (this.svg.querySelectorAll("g").forEach((t) => t.remove()), this.arrows.forEach((t, e) => {
|
|
328
|
+
const s = this._createArrowGroup(t, e);
|
|
329
|
+
this.svg.appendChild(s);
|
|
326
330
|
}), this.current) {
|
|
327
331
|
const t = this._createArrowGroup(this.current, this.arrows.length);
|
|
328
332
|
this.svg.appendChild(t);
|
|
329
333
|
}
|
|
330
334
|
}
|
|
331
335
|
_isDoubleClick(t) {
|
|
332
|
-
const
|
|
333
|
-
return this.lastClickTime =
|
|
336
|
+
const e = Date.now(), s = e - this.lastClickTime;
|
|
337
|
+
return this.lastClickTime = e, s < 300 && this.lastClickPos && Math.hypot(t.x - this.lastClickPos.x, t.y - this.lastClickPos.y) < 10 ? (this.lastClickPos = null, !0) : (this.lastClickPos = t, !1);
|
|
334
338
|
}
|
|
335
|
-
_handleClick(t,
|
|
336
|
-
if (!(this.isCtrl && !
|
|
339
|
+
_handleClick(t, e) {
|
|
340
|
+
if (!(this.isCtrl && !e.ctrlKey)) {
|
|
337
341
|
if (this._shouldBlockClick) {
|
|
338
342
|
this._shouldBlockClick = !1;
|
|
339
343
|
return;
|
|
340
344
|
}
|
|
341
345
|
if (this._isDoubleClick(t)) {
|
|
342
|
-
this.drawing && this.current && this.current.points.length >= 2
|
|
346
|
+
if (this.drawing && this.current && this.current.points.length >= 2) {
|
|
347
|
+
this.drawing = !1, this.arrows.push(this.current);
|
|
348
|
+
const s = this.current;
|
|
349
|
+
this.current = null, this.svg.classList.remove("ae-d2-svg-fixed"), this.render(), this.onDrawEnd && this.onDrawEnd({ arrow: s, arrows: this.getData() });
|
|
350
|
+
}
|
|
343
351
|
return;
|
|
344
352
|
}
|
|
345
353
|
if (this.selected) {
|
|
346
|
-
for (const
|
|
347
|
-
if (this._hitArrow(
|
|
354
|
+
for (const s of this.arrows)
|
|
355
|
+
if (this._hitArrow(s, t)) {
|
|
348
356
|
this.render();
|
|
349
357
|
return;
|
|
350
358
|
}
|
|
351
359
|
this.selected = null, this._hideToolbar(), this.render();
|
|
352
360
|
return;
|
|
353
361
|
}
|
|
354
|
-
for (const
|
|
355
|
-
if (this._hitArrow(
|
|
356
|
-
this.selected =
|
|
362
|
+
for (const s of this.arrows)
|
|
363
|
+
if (this._hitArrow(s, t)) {
|
|
364
|
+
this.selected = s, this._showToolbar(), this.render();
|
|
357
365
|
return;
|
|
358
366
|
}
|
|
359
367
|
this.drawing ? this.current.points.push(t) : (this.drawing = !0, this.current = {
|
|
@@ -363,31 +371,41 @@ class L {
|
|
|
363
371
|
}
|
|
364
372
|
}
|
|
365
373
|
_handleEnter() {
|
|
366
|
-
this.drawing && this.current && this.current.points.length >= 2
|
|
374
|
+
if (this.drawing && this.current && this.current.points.length >= 2) {
|
|
375
|
+
this.drawing = !1, this.arrows.push(this.current);
|
|
376
|
+
const t = this.current;
|
|
377
|
+
this.current = null, this.svg.classList.remove("ae-d2-svg-fixed"), this.render(), this.onDrawEnd && this.onDrawEnd({ arrow: t, arrows: this.getData() });
|
|
378
|
+
}
|
|
367
379
|
}
|
|
368
380
|
_handleEsc() {
|
|
369
381
|
this.drawing && (this.drawing = !1, this.current = null, this.svg.classList.remove("ae-d2-svg-fixed")), this.selected && (this.selected = null), this._hideColorPicker(), this._hideToolbar(), this.render();
|
|
370
382
|
}
|
|
383
|
+
_handleDelete() {
|
|
384
|
+
if (this.selected) {
|
|
385
|
+
const t = this.selected, e = this.arrows.indexOf(this.selected);
|
|
386
|
+
e > -1 && this.arrows.splice(e, 1), this.selected = null, this._hideToolbar(), this.render(), this.onRemove && this.onRemove({ arrow: t, arrows: this.getData() });
|
|
387
|
+
}
|
|
388
|
+
}
|
|
371
389
|
_showColorPicker(t) {
|
|
372
390
|
if (this._showingColorPicker) {
|
|
373
391
|
this._hideColorPicker();
|
|
374
392
|
return;
|
|
375
393
|
}
|
|
376
394
|
this._showingColorPicker = !0;
|
|
377
|
-
const
|
|
378
|
-
|
|
379
|
-
const
|
|
380
|
-
|
|
395
|
+
const e = document.createElement("div");
|
|
396
|
+
e.className = "ae-color-picker-popup";
|
|
397
|
+
const s = document.createElement("span");
|
|
398
|
+
s.textContent = "选择颜色", s.style.cssText = "font-size: 12px; color: #666;";
|
|
381
399
|
const o = document.createElement("input");
|
|
382
|
-
o.type = "color", o.value = this.selected.color || this._defaultColor, o.showPicker(),
|
|
383
|
-
const
|
|
384
|
-
|
|
385
|
-
this.selected.color = h.target.value, this._defaultColor = h.target.value, this.render();
|
|
400
|
+
o.type = "color", o.value = this.selected.color || this._defaultColor, o.showPicker(), e.appendChild(s), e.appendChild(o);
|
|
401
|
+
const i = this.container.getBoundingClientRect(), n = this._getArrowBBox(this.selected), a = i.left + n.minX - 50, l = i.top + n.maxY + 15;
|
|
402
|
+
e.style.left = a + "px", e.style.top = l + "px", o.addEventListener("input", (h) => {
|
|
403
|
+
this.selected.color = h.target.value, this._defaultColor = h.target.value, this.render(), this.onChange && this.onChange({ type: "color", arrow: this.selected, value: h.target.value, arrows: this.getData() });
|
|
386
404
|
});
|
|
387
|
-
const
|
|
388
|
-
|
|
405
|
+
const r = (h) => {
|
|
406
|
+
e.contains(h.target) || (this._hideColorPicker(), document.removeEventListener("click", r));
|
|
389
407
|
};
|
|
390
|
-
document.body.appendChild(
|
|
408
|
+
document.body.appendChild(e), this._colorPickerPopup = e, document.addEventListener("click", r);
|
|
391
409
|
}
|
|
392
410
|
_hideColorPicker() {
|
|
393
411
|
this._colorPickerPopup && (this._colorPickerPopup.remove(), this._colorPickerPopup = null), this._showingColorPicker = !1;
|
|
@@ -396,62 +414,66 @@ class L {
|
|
|
396
414
|
deleteSelected() {
|
|
397
415
|
this.selected && (this.arrows = this.arrows.filter((t) => t !== this.selected), this.selected = null, this.render());
|
|
398
416
|
}
|
|
399
|
-
_handleMouseMove(t,
|
|
417
|
+
_handleMouseMove(t, e) {
|
|
400
418
|
if (!this._toolbarDragging && !(this._toolbar && (t.target === this._toolbar || this._toolbar.contains(t.target)))) {
|
|
401
419
|
if (this.isCtrl && !t.ctrlKey) {
|
|
402
|
-
this.drawing && this.current && this.current.points.length >= 2
|
|
420
|
+
if (this.drawing && this.current && this.current.points.length >= 2) {
|
|
421
|
+
this.drawing = !1, this.arrows.push(this.current);
|
|
422
|
+
const s = this.current;
|
|
423
|
+
this.current = null, this.svg.classList.remove("ae-d2-svg-fixed"), this.render(), this.onDrawEnd && this.onDrawEnd({ arrow: s, arrows: this.getData() });
|
|
424
|
+
}
|
|
403
425
|
return;
|
|
404
426
|
}
|
|
405
|
-
if (this.drawing && (this.current.points[this.current.points.length - 1] =
|
|
406
|
-
this._transformMode === "rotate" ? this._handleRotate(
|
|
427
|
+
if (this.drawing && (this.current.points[this.current.points.length - 1] = e, this.render()), this._transformMode && this.selected) {
|
|
428
|
+
this._transformMode === "rotate" ? this._handleRotate(e) : this._transformMode === "scale" && this._handleScale(e), this.render();
|
|
407
429
|
return;
|
|
408
430
|
}
|
|
409
431
|
if (this.selected && this.mode === "move") {
|
|
410
|
-
const
|
|
411
|
-
(
|
|
412
|
-
|
|
432
|
+
const s = t.movementX, o = t.movementY;
|
|
433
|
+
(s !== 0 || o !== 0) && (this._shouldBlockClick = !0), this.selected.points.forEach((i) => {
|
|
434
|
+
i.x += s, i.y += o;
|
|
413
435
|
}), this.render();
|
|
414
436
|
}
|
|
415
437
|
}
|
|
416
438
|
}
|
|
417
439
|
_getArrowBBox(t) {
|
|
418
|
-
const
|
|
419
|
-
let
|
|
420
|
-
for (const a of
|
|
421
|
-
|
|
440
|
+
const e = t.points;
|
|
441
|
+
let s = 1 / 0, o = 1 / 0, i = -1 / 0, n = -1 / 0;
|
|
442
|
+
for (const a of e)
|
|
443
|
+
s = Math.min(s, a.x), o = Math.min(o, a.y), i = Math.max(i, a.x), n = Math.max(n, a.y);
|
|
422
444
|
return {
|
|
423
|
-
minX:
|
|
445
|
+
minX: s,
|
|
424
446
|
minY: o,
|
|
425
|
-
maxX:
|
|
447
|
+
maxX: i,
|
|
426
448
|
maxY: n,
|
|
427
|
-
width:
|
|
449
|
+
width: i - s,
|
|
428
450
|
height: n - o,
|
|
429
|
-
centerX: (
|
|
451
|
+
centerX: (s + i) / 2,
|
|
430
452
|
centerY: (o + n) / 2
|
|
431
453
|
};
|
|
432
454
|
}
|
|
433
455
|
_handleRotate(t) {
|
|
434
|
-
const
|
|
456
|
+
const e = this._transformCenter, s = e.centerX, o = e.centerY, i = Math.atan2(t.y - o, t.x - s), n = Math.atan2(this._transformStartPos.y - o, this._transformStartPos.x - s), a = i - n, l = Math.cos(a), r = Math.sin(a);
|
|
435
457
|
this.selected.points.forEach((h, c) => {
|
|
436
|
-
const d = this._transformStartPoints[c].x -
|
|
437
|
-
h.x =
|
|
458
|
+
const d = this._transformStartPoints[c].x - s, u = this._transformStartPoints[c].y - o;
|
|
459
|
+
h.x = s + d * l - u * r, h.y = o + d * r + u * l;
|
|
438
460
|
});
|
|
439
461
|
}
|
|
440
462
|
_handleScale(t) {
|
|
441
|
-
const
|
|
463
|
+
const e = this._transformCenter, s = e.centerX, o = e.centerY, i = this._transformStartPoints, n = this._transformStartPos, a = t.x - s, l = t.y - o, r = n.x - s, h = n.y - o, c = Math.hypot(a, l), d = Math.hypot(r, h), u = c / d;
|
|
442
464
|
this.selected.points.forEach((p, f) => {
|
|
443
|
-
p.x =
|
|
465
|
+
p.x = s + (i[f].x - s) * u, p.y = o + (i[f].y - o) * u;
|
|
444
466
|
});
|
|
445
467
|
}
|
|
446
|
-
_hitArrow(t,
|
|
447
|
-
for (let
|
|
448
|
-
if (this._dist(
|
|
468
|
+
_hitArrow(t, e) {
|
|
469
|
+
for (let s = 0; s < t.points.length - 1; s++)
|
|
470
|
+
if (this._dist(e, t.points[s], t.points[s + 1]) < 15)
|
|
449
471
|
return !0;
|
|
450
472
|
return !1;
|
|
451
473
|
}
|
|
452
|
-
_dist(t,
|
|
453
|
-
const o =
|
|
454
|
-
return Math.hypot(t.x - l, t.y -
|
|
474
|
+
_dist(t, e, s) {
|
|
475
|
+
const o = s.x - e.x, i = s.y - e.y, n = ((t.x - e.x) * o + (t.y - e.y) * i) / (o * o + i * i), a = Math.max(0, Math.min(1, n)), l = e.x + a * o, r = e.y + a * i;
|
|
476
|
+
return Math.hypot(t.x - l, t.y - r);
|
|
455
477
|
}
|
|
456
478
|
// 获取所有箭头数据
|
|
457
479
|
getData() {
|
|
@@ -464,19 +486,23 @@ class L {
|
|
|
464
486
|
}
|
|
465
487
|
// 设置箭头数据
|
|
466
488
|
setData(t) {
|
|
467
|
-
this.arrows = t.map((
|
|
468
|
-
points:
|
|
469
|
-
curveRate:
|
|
470
|
-
strokeWidth:
|
|
471
|
-
color:
|
|
489
|
+
this.arrows = t.map((e) => ({
|
|
490
|
+
points: e.points,
|
|
491
|
+
curveRate: e.curveRate ?? 0,
|
|
492
|
+
strokeWidth: e.strokeWidth ?? 8,
|
|
493
|
+
color: e.color
|
|
472
494
|
})), this.render();
|
|
473
495
|
}
|
|
474
496
|
// 清空所有箭头
|
|
475
497
|
clear() {
|
|
476
498
|
this.arrows = [], this.drawing = !1, this.current = null, this.selected = null, this.render();
|
|
477
499
|
}
|
|
500
|
+
// 销毁编辑器,移除所有事件和元素
|
|
501
|
+
destroy() {
|
|
502
|
+
this._kb && this._kb.destroy && this._kb.destroy(), this.container.onclick = null, this.container.onmousemove = null, this.container.onmousedown = null, this.container.onmouseup = null, this.container.ondblclick = null, this.svg && (this.svg.remove(), this.svg = null), this._toolbar && (this._toolbar.remove(), this._toolbar = null), this._colorPickerPopup && (this._colorPickerPopup.remove(), this._colorPickerPopup = null), this.arrows = [], this.drawing = !1, this.current = null, this.selected = null, this.mode = null, this._transformMode = null, this._transformHandle = null;
|
|
503
|
+
}
|
|
478
504
|
}
|
|
479
505
|
export {
|
|
480
|
-
|
|
506
|
+
M as ArrowEditor
|
|
481
507
|
};
|
|
482
508
|
//# sourceMappingURL=bundle.esm.js.map
|