@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.
@@ -1,5 +1,5 @@
1
- import { Keybinding as P } from "@wbiokr/keybinding";
2
- const S = `
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: 100000;
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: 1000000;
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 y = document.createElement("style");
136
- y.textContent = S, document.head.appendChild(y);
135
+ const v = document.createElement("style");
136
+ v.textContent = E, document.head.appendChild(v);
137
137
  }
138
- class L {
139
- constructor(t, s = {}) {
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 = s.isCtrl || !1, this.stopPropagation = s.stopPropagation || !1, this.preventDefault = s.preventDefault || !1, 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();
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 s = document.createElementNS("http://www.w3.org/2000/svg", "defs");
145
- this.svg.appendChild(s);
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 o = document.createElementNS("http://www.w3.org/2000/svg", "polygon");
149
- o.setAttribute("points", "0,0 10,5 0,10 2,5"), o.setAttribute("fill", this._defaultColor), e.appendChild(o), s.appendChild(e);
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, s = this.selected.strokeWidth || this._strokeWidth, e = this.selected.curveRate ?? this.selected.cornerRoundness ?? 0;
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="${s}" />
163
- <span class="ae-toolbar-value">${s}px</span>
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="${e}" />
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", (i) => {
170
- this.selected && (this.selected.color = i.target.value, this._defaultColor = i.target.value, this._updateMarkerColor(i.target.value), this.render());
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 r = this._toolbar.querySelector(".ae-stroke-input"), n = this._toolbar.querySelector(".ae-stroke-input + .ae-toolbar-value");
173
- r.addEventListener("input", (i) => {
174
- this.selected && (this.selected.strokeWidth = parseInt(i.target.value), n.textContent = i.target.value + "px", this.render());
175
- }), this._toolbar.querySelector(".ae-round-input").addEventListener("input", (i) => {
176
- this.selected && (this.selected.curveRate = parseFloat(i.target.value), this.render());
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", (i) => {
180
- i.stopPropagation();
181
- }), this._toolbar.addEventListener("mouseup", (i) => {
182
- i.stopPropagation();
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 s = !1, e = { x: 0, y: 0 };
187
+ let e = !1, s = { x: 0, y: 0 };
188
188
  t.addEventListener("mousedown", (o) => {
189
- s = !0, this._toolbarDragging = !0;
190
- const r = this._toolbar.getBoundingClientRect();
191
- e.x = o.clientX - r.left, e.y = o.clientY - r.top, o.preventDefault(), o.stopPropagation();
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 (!s) return;
194
- const i = this.container.getBoundingClientRect();
195
- let h = l.clientX - i.left - e.x, c = l.clientY - i.top - e.y;
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, i.width - d)), c = Math.max(0, Math.min(c, i.height - u)), this._toolbar.style.left = h + "px", this._toolbar.style.top = c + "px";
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
- s = !1, this._toolbarDragging = !1, document.removeEventListener("mousemove", n), document.removeEventListener("mouseup", a), this._toolbar.removeEventListener("mouseup", a), l.preventDefault(), l.stopPropagation();
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, s = this.selected.strokeWidth || this._strokeWidth, e = this.selected.curveRate ?? this.selected.cornerRoundness ?? 0, o = this._toolbar.querySelector(".ae-color-input"), r = 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), r && (r.value = s, n.textContent = s + "px"), a && (a.value = e);
209
- const l = this._getArrowBBox(this.selected), i = this._toolbar.offsetWidth || 150, h = this._toolbar.offsetHeight || 120;
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 + i > this.container.offsetWidth && (c = l.minX - i - 20);
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 s = this.svg.querySelector("#ae-arrowhead polygon");
221
- s && s.setAttribute("fill", t);
220
+ const e = this.svg.querySelector("#ae-arrowhead polygon");
221
+ e && e.setAttribute("fill", t);
222
222
  }
223
223
  _initKeybindings() {
224
- this._kb = new P("arrow-editor"), this._kb.on("enter", () => {
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 s = this._mousePos(t);
236
- this._handleClick(s, t);
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 s = this._mousePos(t);
240
- this._handleMouseMove(t, s);
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 s = t.target.getAttribute("data-handle");
244
- if (s) {
245
- this._transformMode = s, this._transformHandle = s;
246
- const e = this._mousePos(t);
247
- if (this._transformStartPos = e, this._transformStartPoints = this.selected.points.map((o) => ({ x: o.x, y: o.y })), this._transformCenter = this._getArrowBBox(this.selected), s === "rotate") {
248
- const o = this._transformCenter, r = e.x - o.centerX, n = e.y - o.centerY;
249
- this._transformStartAngle = Math.atan2(n, r), this._rotationOffset = 0;
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 s = this.container.getBoundingClientRect();
265
+ const e = this.container.getBoundingClientRect();
262
266
  return {
263
- x: t.clientX - s.left,
264
- y: t.clientY - s.top
267
+ x: t.clientX - e.left,
268
+ y: t.clientY - e.top
265
269
  };
266
270
  }
267
- _generatePath(t, s = 0) {
271
+ _generatePath(t, e = 0) {
268
272
  if (t.length < 2) return "";
269
- if (s === 0) {
273
+ if (e === 0) {
270
274
  let o = `M ${t[0].x} ${t[0].y}`;
271
- for (let r = 1; r < t.length; r++)
272
- o += ` L ${t[r].x} ${t[r].y}`;
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 e = `M ${t[0].x} ${t[0].y}`;
279
+ let s = `M ${t[0].x} ${t[0].y}`;
276
280
  for (let o = 0; o < t.length - 1; o++) {
277
- const r = t[o], n = t[o + 1], a = n.x - r.x, l = n.y - r.y, i = Math.hypot(a, l);
278
- if (i === 0) continue;
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 = r.x - m.x, c = r.y - m.y;
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, v = h / p, w = c / p, b = d / f, _ = u / f, g = i * s * 0.3, x = r.x + v * g, k = r.y + w * g, C = n.x - b * g, A = n.y - _ * g;
290
- e += ` C ${x} ${k} ${C} ${A} ${n.x} ${n.y}`;
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 e;
296
+ return s;
293
297
  }
294
- _generateArrowHead(t, s = 8) {
298
+ _generateArrowHead(t, e = 8) {
295
299
  if (t.length < 2) return "";
296
- const e = t[t.length - 1];
300
+ const s = t[t.length - 1];
297
301
  let o = t[t.length - 2];
298
- t.length >= 3 && Math.hypot(e.x - o.x, e.y - o.y) < 5 && (o = t[t.length - 3]);
299
- const r = e.x - o.x, n = e.y - o.y, a = Math.hypot(r, n);
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 = r / a, i = n / a, h = e.x, c = e.y, d = s / 8, u = 40 * d, p = 20 * d, f = 20 * d, v = e.x + l * f, w = e.y + i * f, b = e.x - l * (u - f), _ = e.y - i * (u - f), g = -i, x = l, k = b + g * p, C = _ + x * p, A = b - g * p, m = _ - x * p;
302
- return `M ${v} ${w} L ${k} ${C} L ${h} ${c} L ${A} ${m} Z`;
303
- }
304
- _createArrowGroup(t, s) {
305
- const e = document.createElementNS("http://www.w3.org/2000/svg", "g");
306
- e.setAttribute("data-index", s);
307
- const o = t.color || this._defaultColor, r = t.strokeWidth || this._strokeWidth, n = t.curveRate ?? t.cornerRoundness ?? 0, a = document.createElementNS("http://www.w3.org/2000/svg", "path");
308
- a.setAttribute("class", "ae-arrow-path"), a.style.stroke = o, a.style.strokeWidth = r + "px", a.setAttribute("d", this._generatePath(t.points, n)), e.appendChild(a);
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, r)), e.appendChild(l), t === this.selected) {
311
- const i = this._getArrowBBox(t), h = document.createElementNS("http://www.w3.org/2000/svg", "rect");
312
- h.setAttribute("class", "ae-transform-box"), h.setAttribute("x", i.minX - 10), h.setAttribute("y", i.minY - 10), h.setAttribute("width", i.width + 20), h.setAttribute("height", i.height + 20), e.appendChild(h);
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", i.maxX + 10), c.setAttribute("cy", i.maxY + 10), c.setAttribute("r", 6), c.setAttribute("data-handle", "scale"), e.appendChild(c);
315
- const d = i.minY - 30, u = i.centerX, p = document.createElementNS("http://www.w3.org/2000/svg", "line");
316
- p.setAttribute("class", "ae-rotate-line"), p.setAttribute("x1", i.centerX), p.setAttribute("y1", i.minY - 10), p.setAttribute("x2", u), p.setAttribute("y2", d), e.appendChild(p);
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"), e.appendChild(f);
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 e;
324
+ return s;
321
325
  }
322
326
  render() {
323
- if (this.svg.querySelectorAll("g").forEach((t) => t.remove()), this.arrows.forEach((t, s) => {
324
- const e = this._createArrowGroup(t, s);
325
- this.svg.appendChild(e);
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 s = Date.now(), e = s - this.lastClickTime;
333
- return this.lastClickTime = s, e < 300 && this.lastClickPos && Math.hypot(t.x - this.lastClickPos.x, t.y - this.lastClickPos.y) < 10 ? (this.lastClickPos = null, !0) : (this.lastClickPos = t, !1);
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, s) {
336
- if (!(this.isCtrl && !s.ctrlKey)) {
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 && (this.drawing = !1, this.arrows.push(this.current), this.current = null, this.svg.classList.remove("ae-d2-svg-fixed"), this.render());
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 e of this.arrows)
347
- if (this._hitArrow(e, t)) {
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 e of this.arrows)
355
- if (this._hitArrow(e, t)) {
356
- this.selected = e, this._showToolbar(), this.render();
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 && (this.drawing = !1, this.arrows.push(this.current), this.current = null, this.svg.classList.remove("ae-d2-svg-fixed"), this.render());
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 s = document.createElement("div");
378
- s.className = "ae-color-picker-popup";
379
- const e = document.createElement("span");
380
- e.textContent = "选择颜色", e.style.cssText = "font-size: 12px; color: #666;";
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(), s.appendChild(e), s.appendChild(o);
383
- const r = this.container.getBoundingClientRect(), n = this._getArrowBBox(this.selected), a = r.left + n.minX - 50, l = r.top + n.maxY + 15;
384
- s.style.left = a + "px", s.style.top = l + "px", o.addEventListener("input", (h) => {
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 i = (h) => {
388
- s.contains(h.target) || (this._hideColorPicker(), document.removeEventListener("click", i));
405
+ const r = (h) => {
406
+ e.contains(h.target) || (this._hideColorPicker(), document.removeEventListener("click", r));
389
407
  };
390
- document.body.appendChild(s), this._colorPickerPopup = s, document.addEventListener("click", i);
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, s) {
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 && (this.drawing = !1, this.arrows.push(this.current), this.current = null, this.svg.classList.remove("ae-d2-svg-fixed"), this.render());
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] = s, this.render()), this._transformMode && this.selected) {
406
- this._transformMode === "rotate" ? this._handleRotate(s) : this._transformMode === "scale" && this._handleScale(s), this.render();
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 e = t.movementX, o = t.movementY;
411
- (e !== 0 || o !== 0) && (this._shouldBlockClick = !0), this.selected.points.forEach((r) => {
412
- r.x += e, r.y += o;
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 s = t.points;
419
- let e = 1 / 0, o = 1 / 0, r = -1 / 0, n = -1 / 0;
420
- for (const a of s)
421
- e = Math.min(e, a.x), o = Math.min(o, a.y), r = Math.max(r, a.x), n = Math.max(n, a.y);
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: e,
445
+ minX: s,
424
446
  minY: o,
425
- maxX: r,
447
+ maxX: i,
426
448
  maxY: n,
427
- width: r - e,
449
+ width: i - s,
428
450
  height: n - o,
429
- centerX: (e + r) / 2,
451
+ centerX: (s + i) / 2,
430
452
  centerY: (o + n) / 2
431
453
  };
432
454
  }
433
455
  _handleRotate(t) {
434
- const s = this._transformCenter, e = s.centerX, o = s.centerY, r = Math.atan2(t.y - o, t.x - e), n = Math.atan2(this._transformStartPos.y - o, this._transformStartPos.x - e), a = r - n, l = Math.cos(a), i = Math.sin(a);
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 - e, u = this._transformStartPoints[c].y - o;
437
- h.x = e + d * l - u * i, h.y = o + d * i + u * l;
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 s = this._transformCenter, e = s.centerX, o = s.centerY, r = this._transformStartPoints, n = this._transformStartPos, a = t.x - e, l = t.y - o, i = n.x - e, h = n.y - o, c = Math.hypot(a, l), d = Math.hypot(i, h), u = c / d;
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 = e + (r[f].x - e) * u, p.y = o + (r[f].y - o) * u;
465
+ p.x = s + (i[f].x - s) * u, p.y = o + (i[f].y - o) * u;
444
466
  });
445
467
  }
446
- _hitArrow(t, s) {
447
- for (let e = 0; e < t.points.length - 1; e++)
448
- if (this._dist(s, t.points[e], t.points[e + 1]) < 15)
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, s, e) {
453
- const o = e.x - s.x, r = e.y - s.y, n = ((t.x - s.x) * o + (t.y - s.y) * r) / (o * o + r * r), a = Math.max(0, Math.min(1, n)), l = s.x + a * o, i = s.y + a * r;
454
- return Math.hypot(t.x - l, t.y - i);
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((s) => ({
468
- points: s.points,
469
- curveRate: s.curveRate ?? 0,
470
- strokeWidth: s.strokeWidth ?? 8,
471
- color: s.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
- L as ArrowEditor
506
+ M as ArrowEditor
481
507
  };
482
508
  //# sourceMappingURL=bundle.esm.js.map