@8btc/whiteboard 0.0.19-beta.1 → 0.0.19-beta.3

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,141 +1,153 @@
1
1
  import { v4 as _ } from "uuid";
2
- import { N as M } from "../const-Das6lRi-.js";
3
- async function A(t) {
4
- return new Promise((o, e) => {
5
- const n = new Image();
6
- n.onload = () => {
7
- o({ width: n.naturalWidth, height: n.naturalHeight });
8
- }, n.onerror = () => {
9
- e(new Error(`Failed to load image: ${t}`));
10
- }, n.src = t;
2
+ import { N as M } from "../const-CpLk0Av6.js";
3
+ async function S(t) {
4
+ return new Promise((o, n) => {
5
+ const e = new Image();
6
+ e.onload = () => {
7
+ o({ width: e.naturalWidth, height: e.naturalHeight });
8
+ }, e.onerror = () => {
9
+ n(new Error(`Failed to load image: ${t}`));
10
+ }, e.src = t;
11
11
  });
12
12
  }
13
- function B(t) {
13
+ function A(t) {
14
14
  if (!t) return { x: 0, y: 0 };
15
15
  if ((t.getState().nodes || []).length > 0) {
16
- const n = t.getMainLayer();
17
- let r = 1 / 0, i = -1 / 0;
18
- if (n.children.forEach((a) => {
19
- if (a.visible() && a.getClassName() !== "Transformer" && a.hasName(M.selectable)) {
20
- const s = a.getAttrs(), l = s.x || 0, d = s.y || 0, c = s.height || 0;
21
- r = Math.min(r, l), i = Math.max(i, d + c);
16
+ const e = t.getMainLayer();
17
+ let i = 1 / 0, r = -1 / 0;
18
+ if (e.children.forEach((s) => {
19
+ if (s.visible() && s.getClassName() !== "Transformer" && s.hasName(M.selectable)) {
20
+ const a = s.getAttrs(), c = a.x || 0, d = a.y || 0, f = a.height || 0;
21
+ i = Math.min(i, c), r = Math.max(r, d + f);
22
22
  }
23
- }), r !== 1 / 0 && i !== -1 / 0)
24
- return { x: r, y: i };
23
+ }), i !== 1 / 0 && r !== -1 / 0)
24
+ return { x: i, y: r };
25
25
  }
26
- const e = t.getState().viewport;
26
+ const n = t.getState().viewport;
27
27
  return {
28
- x: e.x,
29
- y: e.y
28
+ x: n.x,
29
+ y: n.y
30
30
  };
31
31
  }
32
- async function $(t, o, e, n) {
32
+ async function p(t, o, n, e) {
33
33
  if (!t) return;
34
- const r = Array.isArray(o) ? o : [o];
35
- if (r.length === 0) return;
36
- const i = n?.spacing ?? 20, a = n?.scrollToView ?? !0, s = n?.direction ?? "vertical", l = await Promise.all(r.map((f) => A(f))), d = [], c = [];
37
- let u = e.x, m = e.y;
38
- return r.forEach((f, g) => {
39
- const { width: y, height: h } = l[g], I = _();
40
- d.push({
34
+ const i = Array.isArray(o) ? o : [o];
35
+ if (i.length === 0) return;
36
+ const r = e?.spacing ?? 20, s = e?.scrollToView ?? !0, a = e?.direction ?? "horizontal", c = e?.reuseExisting ?? !0, d = /* @__PURE__ */ new Map();
37
+ c && (t.getState().nodes || []).forEach((y) => {
38
+ y.$_type === "image" && y.$_imageUrl && d.set(y.$_imageUrl, y.id);
39
+ });
40
+ const f = await Promise.all(i.map((l) => S(l))), u = [], m = [];
41
+ let h = n.x, g = n.y;
42
+ return i.forEach((l, y) => {
43
+ if (c && d.has(l)) {
44
+ const x = d.get(l);
45
+ m.push(x);
46
+ return;
47
+ }
48
+ const { width: w, height: $ } = f[y], I = _();
49
+ u.push({
41
50
  id: I,
42
51
  $_type: "image",
43
- x: u,
44
- y: m,
45
- width: y,
46
- height: h,
47
- $_imageUrl: f
48
- }), c.push(I), s === "horizontal" ? u += y + i : m += h + i;
49
- }), t.createNodes(d, !0), t.getMainLayer().draw(), a && t.scrollToContent({ scale: !1, nodeIds: c }), c;
52
+ x: h,
53
+ y: g,
54
+ width: w,
55
+ height: $,
56
+ $_imageUrl: l
57
+ }), m.push(I), a === "horizontal" ? h += w + r : g += $ + r;
58
+ }), u.length > 0 && t.createNodes(u, !0), s && t.scrollToContent({ scale: !1, nodeIds: m }), m;
50
59
  }
51
- async function X(t, o) {
60
+ async function L(t, o) {
52
61
  if (!t) return;
53
- const e = B(t);
54
- return $(t, o, {
55
- x: e.x,
56
- y: e.y + 20
62
+ const n = A(t);
63
+ return p(t, o, {
64
+ x: n.x,
65
+ y: n.y + 20
57
66
  });
58
67
  }
59
- function Y(t, o) {
68
+ function X(t, o) {
60
69
  if (!t) return;
61
- const e = t.getState().selectedNodeIds || [];
62
- if (e.length === 0) return;
63
- let n = 1 / 0, r = 1 / 0, i = -1 / 0, a = -1 / 0;
64
- if (t.getMainLayer().children.forEach((c) => {
65
- if (c.visible() && c.getClassName() !== "Transformer" && e.includes(c.id())) {
66
- const u = c.getAttrs(), m = u.x || 0, f = u.y || 0, g = u.width || 0, y = u.height || 0;
67
- n = Math.min(n, m), r = Math.min(r, f), i = Math.max(i, m + g), a = Math.max(a, f + y);
70
+ const n = t.getState().selectedNodeIds || [];
71
+ if (n.length === 0) return;
72
+ let e = 1 / 0, i = 1 / 0, r = -1 / 0, s = -1 / 0;
73
+ if (t.getMainLayer().children.forEach((f) => {
74
+ if (f.visible() && f.getClassName() !== "Transformer" && n.includes(f.id())) {
75
+ const u = f.getAttrs(), m = u.x || 0, h = u.y || 0, g = u.width || 0, l = u.height || 0;
76
+ e = Math.min(e, m), i = Math.min(i, h), r = Math.max(r, m + g), s = Math.max(s, h + l);
68
77
  }
69
- }), n === 1 / 0 || r === 1 / 0 || i === -1 / 0 || a === -1 / 0)
78
+ }), e === 1 / 0 || i === 1 / 0 || r === -1 / 0 || s === -1 / 0)
70
79
  return;
71
- const l = {
72
- x: i + 20,
73
- y: r,
74
- width: i - n,
75
- height: a - r
80
+ const c = {
81
+ x: r + 20,
82
+ y: i,
83
+ width: r - e,
84
+ height: s - i
76
85
  }, d = {
77
86
  id: _(),
78
- $_type: "image",
79
- x: l.x,
80
- y: l.y,
81
- width: l.width,
82
- height: l.height,
83
- $_imageUrl: o
87
+ $_type: "html",
88
+ x: c.x,
89
+ y: c.y,
90
+ width: c.width,
91
+ height: c.height,
92
+ $_imageUrl: o,
84
93
  // 示例 loading 图片 URL
94
+ $_htmlContent: `
95
+ <img alt="loading" src="${o}" style="width: 100%; height: 100%; object-fit: contain;" />
96
+ `
85
97
  };
86
- return t.createNodes([d], !0), t.scrollToContent({ scale: !1, nodeIds: [d.id] }), d.id;
98
+ return t.createNodes([d], !0), d.id;
87
99
  }
88
- function E(t, o, e) {
100
+ function Y(t, o, n) {
89
101
  if (!t) return;
90
- const n = t.getCanvasNodeById(o);
91
- if (!n) return;
92
- const r = n.getConfig();
93
- if (r.$_type !== "image") return;
94
- const i = Array.isArray(e) ? e : [e];
95
- if (i.length === 0) return;
96
- const a = r.x, s = r.y;
97
- $(t, i, { x: a, y: s }), t.deleteNodes([o]);
102
+ const e = t.getCanvasNodeById(o);
103
+ if (!e) return;
104
+ const i = e.getConfig();
105
+ if (i.$_type !== "html") return;
106
+ const r = Array.isArray(n) ? n : [n];
107
+ if (r.length === 0) return;
108
+ const s = i.x, a = i.y;
109
+ p(t, r, { x: s, y: a }), t.deleteNodes([o]);
98
110
  }
99
- async function T(t, o) {
111
+ async function R(t, o) {
100
112
  if (!t) return null;
101
- const e = t.getCanvasNodeById(o);
102
- if (!e) return null;
103
- const n = e.getConfig();
104
- if (n.$_type !== "image-marker" || !n.$_relativeBox)
113
+ const n = t.getCanvasNodeById(o);
114
+ if (!n) return null;
115
+ const e = n.getConfig();
116
+ if (e.$_type !== "image-marker" || !e.$_relativeBox)
105
117
  return null;
106
- const r = n.$_parentId;
107
- if (!r) return null;
108
- const i = t.getCanvasNodeById(r);
118
+ const i = e.$_parentId;
109
119
  if (!i) return null;
110
- const a = i.getConfig();
111
- if (a.$_type !== "image" || !a.$_imageUrl) return null;
112
- const { start: s, end: l } = n.$_relativeBox, d = s.ratioX, c = s.ratioY, u = l.ratioX - s.ratioX, m = l.ratioY - s.ratioY;
113
- return new Promise((f) => {
120
+ const r = t.getCanvasNodeById(i);
121
+ if (!r) return null;
122
+ const s = r.getConfig();
123
+ if (s.$_type !== "image" || !s.$_imageUrl) return null;
124
+ const { start: a, end: c } = e.$_relativeBox, d = a.ratioX, f = a.ratioY, u = c.ratioX - a.ratioX, m = c.ratioY - a.ratioY;
125
+ return new Promise((h) => {
114
126
  const g = new Image();
115
127
  g.crossOrigin = "anonymous", g.onload = () => {
116
- const y = g.naturalWidth, h = g.naturalHeight, I = d * y, v = c * h, w = u * y, C = m * h, x = document.createElement("canvas");
117
- x.width = w, x.height = C;
118
- const N = x.getContext("2d");
128
+ const l = g.naturalWidth, y = g.naturalHeight, w = d * l, $ = f * y, I = u * l, x = m * y, C = document.createElement("canvas");
129
+ C.width = I, C.height = x;
130
+ const N = C.getContext("2d");
119
131
  if (!N) {
120
- f(null);
132
+ h(null);
121
133
  return;
122
134
  }
123
135
  N.drawImage(
124
136
  g,
125
- I,
126
- v,
127
137
  w,
128
- C,
138
+ $,
139
+ I,
140
+ x,
129
141
  0,
130
142
  0,
131
- w,
132
- C
143
+ I,
144
+ x
133
145
  );
134
- const p = x.toDataURL("image/png");
135
- f(p);
146
+ const v = C.toDataURL("image/png");
147
+ h(v);
136
148
  }, g.onerror = () => {
137
- console.error("Failed to load image:", a.$_imageUrl), f(null);
138
- }, g.src = a.$_imageUrl;
149
+ console.error("Failed to load image:", s.$_imageUrl), h(null);
150
+ }, g.src = s.$_imageUrl;
139
151
  });
140
152
  }
141
153
  function H(t) {
@@ -143,19 +155,19 @@ function H(t) {
143
155
  const o = t.getState().selectedNodeIds || [];
144
156
  if (o.length === 0) return null;
145
157
  if (o.length === 1) {
146
- const e = t.getCanvasNodeById(o[0]);
147
- if (!e) return null;
148
- if (e.getConfig().$_type === "image")
149
- return e.getConfig().$_imageUrl || null;
158
+ const n = t.getCanvasNodeById(o[0]);
159
+ if (!n) return null;
160
+ if (n.getConfig().$_type === "image")
161
+ return n.getConfig().$_imageUrl || null;
150
162
  }
151
163
  return t.exportSelectionAsImage();
152
164
  }
153
165
  export {
154
- B as calculateBottomLeftOfCanvasContent,
155
- T as cropImageBaseOnImageMarker,
166
+ A as calculateBottomLeftOfCanvasContent,
167
+ R as exportMarkedRegion,
156
168
  H as exportSelectionAsImage,
157
- X as insertImageBesideContent,
158
- $ as insertImagesAtPosition,
159
- Y as insertLoadingImageBesideSelection,
160
- E as replaceLoadingImageNode
169
+ L as insertImageBesideContent,
170
+ p as insertImagesAtPosition,
171
+ X as insertLoadingImageBesideSelection,
172
+ Y as replaceLoadingImageNode
161
173
  };
@@ -3,15 +3,16 @@ const i = {
3
3
  selectable: "selectable_intrinsic",
4
4
  rect: "rect_intrinsic",
5
5
  image: "image_intrinsic",
6
- imageMarker: "image_marker_intrinsic"
7
- }, e = {
6
+ imageMarker: "image_marker_intrinsic",
7
+ html: "html_intrinsic"
8
+ }, t = {
8
9
  CORNER_RADIUS: 6,
9
10
  MIN_SIZE: 10
10
- }, n = {
11
+ }, e = {
11
12
  MIN_SIZE: 10
12
13
  };
13
14
  export {
14
- n as I,
15
+ e as I,
15
16
  i as N,
16
- e as R
17
+ t as R
17
18
  };
package/dist/index.d.ts CHANGED
@@ -407,6 +407,7 @@ export declare const NODE_NAMES: {
407
407
  rect: string;
408
408
  image: string;
409
409
  imageMarker: string;
410
+ html: string;
410
411
  };
411
412
 
412
413
  declare interface NodeStyle {
@@ -414,7 +415,7 @@ declare interface NodeStyle {
414
415
  $_fillColor: "theme.fill-color.1" | "theme.fill-color.2" | "theme.fill-color.3";
415
416
  }
416
417
 
417
- export declare type NodeType = "rectangle" | "image" | "image-marker";
418
+ export declare type NodeType = "rectangle" | "image" | "image-marker" | "html";
418
419
 
419
420
  declare type StageConfig = {
420
421
  container: HTMLDivElement;