@arcblock/terminal 3.1.24 → 3.1.26

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/lib/Player.js CHANGED
@@ -1,223 +1,233 @@
1
- import { jsxs as S, jsx as a } from "react/jsx-runtime";
2
- import { useRef as g, useReducer as H, useCallback as f, useEffect as q } from "react";
3
- import l from "prop-types";
4
- import F from "lodash/isUndefined";
1
+ import { jsxs as g, jsx as t } from "react/jsx-runtime";
2
+ import { useRef as h, useReducer as H, useCallback as d, useEffect as q } from "react";
3
+ import c from "prop-types";
4
+ import S from "lodash/isUndefined";
5
5
  import P from "lodash/noop";
6
6
  import Y from "./Terminal.js";
7
7
  import { PlayerRoot as _ } from "./styles.js";
8
- import { defaultOptions as U, formatFrames as X, defaultState as $, findFrameAt as D, getPlayerClass as G, getFrameClass as K, formatTime as Q } from "./util.js";
8
+ import { defaultOptions as U, formatFrames as X, defaultState as $, findFrameAt as z, getPlayerClass as G, getFrameClass as K, formatTime as Q } from "./util.js";
9
9
  const se = 8;
10
- function V({ ...z }) {
11
- const s = Object.assign({}, z);
12
- F(s.onComplete) && (s.onComplete = P), F(s.onStart) && (s.onStart = P), F(s.onStop) && (s.onStop = P), F(s.onPause) && (s.onPause = P), F(s.onTick) && (s.onTick = P), F(s.onJump) && (s.onJump = P);
13
- const r = Object.assign({}, U, s.options), { frames: c, totalDuration: h } = X(s.frames, r), J = {
14
- cols: r.cols,
15
- rows: r.rows,
16
- cursorStyle: r.cursorStyle,
17
- cursorBlink: r.cursorBlink ?? !0,
18
- fontFamily: r.fontFamily,
19
- fontSize: r.fontSize,
20
- lineHeight: r.lineHeight,
21
- letterSpacing: r.letterSpacing,
10
+ function V({ ...J }) {
11
+ const s = Object.assign({}, J);
12
+ S(s.onComplete) && (s.onComplete = P), S(s.onStart) && (s.onStart = P), S(s.onStop) && (s.onStop = P), S(s.onPause) && (s.onPause = P), S(s.onTick) && (s.onTick = P), S(s.onJump) && (s.onJump = P);
13
+ const e = Object.assign({}, U, s.options), { frames: o, totalDuration: v } = X(s.frames, e), I = {
14
+ cols: e.cols,
15
+ rows: e.rows,
16
+ cursorStyle: e.cursorStyle,
17
+ cursorBlink: e.cursorBlink ?? !0,
18
+ fontFamily: e.fontFamily,
19
+ fontSize: e.fontSize,
20
+ lineHeight: e.lineHeight,
21
+ letterSpacing: e.letterSpacing,
22
22
  allowTransparency: !0,
23
23
  scrollback: 0,
24
- theme: r.enableTheme ? r.theme : {}
25
- }, I = (e, n) => {
26
- switch (n.type) {
24
+ theme: e.enableTheme ? e.theme : {}
25
+ }, O = (r, a) => {
26
+ switch (a.type) {
27
27
  case "jump":
28
- return { ...e, ...n.payload };
28
+ return { ...r, ...a.payload };
29
29
  case "start":
30
- return { ...e, isStarted: !0, lastTickTime: Date.now() };
30
+ return { ...r, isStarted: !0, lastTickTime: Date.now() };
31
31
  case "play":
32
- return { ...e, isPlaying: !0, lastTickTime: Date.now(), ...n.payload };
32
+ return { ...r, isPlaying: !0, lastTickTime: Date.now(), ...a.payload };
33
33
  case "pause":
34
- return { ...e, isPlaying: !1, ...n.payload };
34
+ return { ...r, isPlaying: !1, ...a.payload };
35
35
  case "tickStart":
36
- return { ...e, isRendering: !0, lastTickTime: Date.now(), ...n.payload };
36
+ return { ...r, isRendering: !0, lastTickTime: Date.now(), ...a.payload };
37
37
  case "tickEnd":
38
- return { ...e, isRendering: !1, lastTickTime: Date.now(), ...n.payload };
38
+ return { ...r, isRendering: !1, lastTickTime: Date.now(), ...a.payload };
39
39
  case "reset":
40
- return { ...e, currentFrame: -1, currentTime: 0, ...n.payload };
40
+ return { ...r, currentFrame: -1, currentTime: 0, ...a.payload };
41
41
  default:
42
- return { ...e, lastTickTime: Date.now(), ...n.payload };
42
+ return { ...r, lastTickTime: Date.now(), ...a.payload };
43
43
  }
44
- }, i = g(null), N = g(null), v = g(null), d = g(null), p = g(null), A = g(!1), [t, u] = H(I, $), j = f(
45
- (e) => new Promise((n) => {
46
- const o = c[e];
47
- o && o.content && i.current ? (t.requireReset && i.current.reset(), i.current.write(o.content, () => {
48
- n();
49
- })) : n();
44
+ }, i = h(null), w = h(null), F = h(null), f = h(null), p = h(null), A = h(!1), [n, u] = H(O, $), j = d(
45
+ (r) => new Promise((a) => {
46
+ const l = o[r];
47
+ l && l.content && i.current ? (n.requireReset && i.current.reset(), i.current.write(l.content, () => {
48
+ a();
49
+ })) : a();
50
50
  }),
51
- [c, t.requireReset]
52
- ), E = f(
53
- (e) => {
54
- const n = c[e];
55
- n && n.content && i.current && (t.requireReset && i.current.reset(), i.current.write(n.content));
51
+ [o, n.requireReset]
52
+ ), E = d(
53
+ (r) => {
54
+ const a = o[r];
55
+ a && a.content && i.current && (n.requireReset && i.current.reset(), i.current.write(a.content));
56
56
  },
57
- [c, t.requireReset]
58
- ), m = f(
59
- (e) => {
60
- typeof s[e] == "function" && s[e]({ state: t, frames: c, options: r });
57
+ [o, n.requireReset]
58
+ ), m = d(
59
+ (r) => {
60
+ typeof s[r] == "function" && s[r]({ state: n, frames: o, options: e });
61
61
  },
62
- [s, t, c, r]
63
- ), R = f(
64
- (e) => {
62
+ [s, n, o, e]
63
+ ), R = d(
64
+ (r) => {
65
65
  if (!i.current) return;
66
66
  i.current.reset();
67
- const n = D(c, e);
68
- if (n >= 0)
69
- for (let o = 0; o <= n; o++)
70
- E(o);
67
+ const a = z(o, r);
68
+ if (a >= 0)
69
+ for (let l = 0; l <= a; l++)
70
+ E(l);
71
71
  },
72
- [c, E]
73
- ), M = f(
74
- (e) => {
75
- if (!N.current || !i.current || !t.isStarted)
72
+ [o, E]
73
+ ), L = d(
74
+ (r) => {
75
+ if (!w.current || !i.current || !n.isStarted)
76
76
  return !1;
77
- const n = N.current.getBoundingClientRect().width, o = e.nativeEvent.offsetX, T = Math.floor(h * o / n), b = D(c, T), w = t.isPlaying;
77
+ const a = w.current.getBoundingClientRect().width, l = r.nativeEvent.offsetX, T = Math.floor(v * l / a), b = z(o, T), N = n.isPlaying;
78
78
  return u({
79
79
  type: "jump",
80
80
  payload: {
81
81
  currentTime: T,
82
82
  currentFrame: b,
83
- isPlaying: w
83
+ isPlaying: N
84
84
  // Keep the same playing state
85
85
  }
86
86
  }), p.current = null, R(T), m("onJump"), !1;
87
87
  },
88
- [t.isStarted, t.isPlaying, h, c, R, m]
89
- ), O = f(() => (t.currentFrame >= c.length - 1 ? (i.current.resize(), u({ type: "reset", payload: { currentFrame: -1, currentTime: 0 } }), i.current && i.current.reset(), p.current = null, u({ type: "play", payload: { currentFrame: -1, currentTime: 0 } })) : (p.current = null, u({ type: "play" })), m("onPlay"), !1), [t.currentFrame, c.length, m]), k = f(() => (t.isStarted === !1 && (i.current.resize(), u({ type: "start" }), i.current && i.current.reset()), p.current = null, u({ type: "play", payload: { currentFrame: -1, currentTime: 0 } }), m("onStart"), !1), [t.isStarted, m]), L = f(() => (u({ type: "pause" }), m("onPause"), !1), [m]);
88
+ [n.isStarted, n.isPlaying, v, o, R, m]
89
+ ), M = d(() => (n.currentFrame >= o.length - 1 ? (i.current.resize(), u({ type: "reset", payload: { currentFrame: -1, currentTime: 0 } }), i.current && i.current.reset(), p.current = null, u({ type: "play", payload: { currentFrame: -1, currentTime: 0 } })) : (p.current = null, u({ type: "play" })), m("onPlay"), !1), [n.currentFrame, o.length, m]), k = d(() => (n.isStarted === !1 && (i.current.resize(), u({ type: "start" }), i.current && i.current.reset()), p.current = null, u({ type: "play", payload: { currentFrame: -1, currentTime: 0 } }), m("onStart"), !1), [n.isStarted, m]), D = d(() => (u({ type: "pause" }), m("onPause"), !1), [m]);
90
90
  q(() => {
91
- if (!v.current || !r.autoplay || A.current)
91
+ if (!F.current || !e.autoplay || A.current)
92
92
  return;
93
- const e = new IntersectionObserver(
94
- (n) => {
95
- const [o] = n;
96
- o.isIntersecting && !t.isStarted && (k(), A.current = !0, e.unobserve(v.current), e.disconnect());
93
+ const r = new IntersectionObserver(
94
+ (a) => {
95
+ const [l] = a;
96
+ l.isIntersecting && !n.isStarted && (k(), A.current = !0, r.unobserve(F.current), r.disconnect());
97
97
  },
98
98
  {
99
- threshold: 0.1,
100
- // Trigger when 10% of the element is visible
101
- rootMargin: "0px 0px 0px 0px"
102
- // Add some margin to avoid triggering too early
99
+ threshold: e.autoplayThreshold,
100
+ // 可配置的可见度阈值
101
+ rootMargin: e.autoplayRootMargin
102
+ // 可配置的根边距
103
103
  }
104
104
  );
105
- return e.observe(v.current), () => {
106
- e.disconnect();
105
+ return r.observe(F.current), () => {
106
+ r.disconnect();
107
107
  };
108
- }, [r.autoplay, t.isStarted, k]), q(() => {
109
- i.current && (i.current.focus(), r.autoplay || R(Math.min(Math.abs(r.thumbnailTime), h)));
108
+ }, [e.autoplay, e.autoplayThreshold, e.autoplayRootMargin, n.isStarted, k]), q(() => {
109
+ i.current && (i.current.focus(), e.autoplay || R(Math.min(Math.abs(e.thumbnailTime), v)));
110
110
  }, []);
111
- const x = g(t);
112
- x.current = t;
113
- const B = f(async () => {
114
- const e = x.current;
115
- if (!e.isPlaying || e.isRendering)
111
+ const x = h(n);
112
+ x.current = n;
113
+ const C = d(async () => {
114
+ const r = x.current;
115
+ if (!r.isPlaying || r.isRendering)
116
116
  return;
117
- const n = performance.now();
117
+ const a = performance.now();
118
118
  if (!p.current) {
119
- const y = e.currentFrame >= 0 && c[e.currentFrame]?.startTime || 0;
120
- p.current = n - y;
119
+ const y = r.currentFrame >= 0 && o[r.currentFrame]?.startTime || 0;
120
+ p.current = a - y;
121
121
  }
122
- const o = n - p.current, { currentFrame: T } = e, b = T + 1;
123
- if (T >= c.length - 1) {
124
- if (m("onComplete"), r.repeat) {
125
- p.current = n, u({ type: "reset", payload: { currentTime: 0, currentFrame: -1 } });
122
+ const l = a - p.current, { currentFrame: T } = r, b = T + 1;
123
+ if (T >= o.length - 1) {
124
+ if (m("onComplete"), e.repeat) {
125
+ p.current = a, u({ type: "reset", payload: { currentTime: 0, currentFrame: -1 } });
126
126
  return;
127
127
  }
128
128
  const y = {
129
- currentTime: h,
130
- currentFrame: c.length - 1,
129
+ currentTime: v,
130
+ currentFrame: o.length - 1,
131
131
  requireReset: !0,
132
132
  isStarted: !1
133
133
  };
134
134
  u({ type: "pause", payload: y }), p.current = null;
135
135
  return;
136
136
  }
137
- const w = c[b];
138
- if (!w) {
139
- t.isPlaying && (d.current = requestAnimationFrame(B));
137
+ const N = o[b];
138
+ if (!N) {
139
+ n.isPlaying && (f.current = requestAnimationFrame(C));
140
140
  return;
141
141
  }
142
- const C = w.startTime || 0;
143
- if (o >= C) {
142
+ const B = N.startTime || 0;
143
+ if (l >= B) {
144
144
  u({
145
145
  type: "tickStart",
146
146
  payload: {
147
- currentTime: C,
147
+ currentTime: B,
148
148
  currentFrame: b
149
149
  }
150
150
  });
151
151
  try {
152
152
  await j(b);
153
153
  const y = {
154
- currentTime: C,
154
+ currentTime: B,
155
155
  currentFrame: b
156
156
  };
157
- e.requireReset && (y.requireReset = !1), u({ type: "tickEnd", payload: y }), m("onTick");
157
+ r.requireReset && (y.requireReset = !1), u({ type: "tickEnd", payload: y }), m("onTick");
158
158
  } catch (y) {
159
159
  console.error("Frame rendering error:", y);
160
160
  }
161
161
  } else
162
- u({ type: "tick", payload: { currentTime: o } });
163
- }, [c, j, m, r.repeat, h]);
162
+ u({ type: "tick", payload: { currentTime: l } });
163
+ }, [o, j, m, e.repeat, v]);
164
164
  return q(() => {
165
- let e = !0;
166
- const n = async () => {
167
- for (; e && x.current.isPlaying; )
168
- await new Promise((o) => {
169
- d.current = requestAnimationFrame(o);
170
- }), e && x.current.isPlaying && await B();
165
+ let r = !0;
166
+ const a = async () => {
167
+ for (; r && x.current.isPlaying; )
168
+ await new Promise((l) => {
169
+ f.current = requestAnimationFrame(l);
170
+ }), r && x.current.isPlaying && await C();
171
171
  };
172
- return t.isPlaying ? n() : (d.current && (cancelAnimationFrame(d.current), d.current = null), p.current = null), () => {
173
- e = !1, d.current && (cancelAnimationFrame(d.current), d.current = null);
172
+ return n.isPlaying ? a() : (f.current && (cancelAnimationFrame(f.current), f.current = null), p.current = null), () => {
173
+ r = !1, f.current && (cancelAnimationFrame(f.current), f.current = null);
174
174
  };
175
- }, [t.isPlaying, B]), r.controls && (r.frameBox.title = null, r.frameBox.type = null, r.frameBox.style = {}, r.theme?.background === "transparent" ? r.frameBox.style.background = "black" : r.theme?.background && (r.frameBox.style.background = r.theme.background), r.frameBox.style.padding = "10px", r.frameBox.style.paddingBottom = "40px"), /* @__PURE__ */ S(_, { className: G(r, t), ref: v, children: [
176
- /* @__PURE__ */ a("div", { className: "cover", onClick: k }),
177
- /* @__PURE__ */ a("div", { className: "start", onClick: k, children: /* @__PURE__ */ S("svg", { style: { enableBackground: "new 0 0 30 30" }, viewBox: "0 0 30 30", children: [
178
- /* @__PURE__ */ a("polygon", { points: "6.583,3.186 5,4.004 5,15 26,15 26.483,14.128 " }),
179
- /* @__PURE__ */ a("polygon", { points: "6.583,26.814 5,25.996 5,15 26,15 26.483,15.872 " }),
180
- /* @__PURE__ */ a("circle", { cx: "26", cy: "15", r: "1" }),
181
- /* @__PURE__ */ a("circle", { cx: "6", cy: "4", r: "1" }),
182
- /* @__PURE__ */ a("circle", { cx: "6", cy: "26", r: "1" })
183
- ] }) }),
184
- /* @__PURE__ */ a("div", { className: "terminal", children: /* @__PURE__ */ S("div", { className: K(r), style: r.frameBox.style || {}, children: [
185
- /* @__PURE__ */ S("div", { className: "terminal-titlebar", children: [
186
- /* @__PURE__ */ S("div", { className: "buttons", children: [
187
- /* @__PURE__ */ a("div", { className: "close-button" }),
188
- /* @__PURE__ */ a("div", { className: "minimize-button" }),
189
- /* @__PURE__ */ a("div", { className: "maximize-button" })
175
+ }, [n.isPlaying, C]), e.controls && (e.frameBox.title = null, e.frameBox.type = null, e.frameBox.style = {}, e.theme?.background === "transparent" ? e.frameBox.style.background = "black" : e.theme?.background && (e.frameBox.style.background = e.theme.background), e.frameBox.style.padding = "10px", e.frameBox.style.paddingBottom = "40px"), /* @__PURE__ */ g(_, { className: G(e, n), ref: F, children: [
176
+ /* @__PURE__ */ t("div", { className: "cover", onClick: k }),
177
+ /* @__PURE__ */ t("div", { className: "start", onClick: k, children: /* @__PURE__ */ t("div", { className: "start-button", children: /* @__PURE__ */ g("svg", { viewBox: "0 0 30 30", children: [
178
+ /* @__PURE__ */ t("polygon", { points: "6.583,3.186 5,4.004 5,15 26,15 26.483,14.128 " }),
179
+ /* @__PURE__ */ t("polygon", { points: "6.583,26.814 5,25.996 5,15 26,15 26.483,15.872 " }),
180
+ /* @__PURE__ */ t("circle", { cx: "26", cy: "15", r: "1" }),
181
+ /* @__PURE__ */ t("circle", { cx: "6", cy: "4", r: "1" }),
182
+ /* @__PURE__ */ t("circle", { cx: "6", cy: "26", r: "1" })
183
+ ] }) }) }),
184
+ n.isPlaying && n.isStarted && /* @__PURE__ */ t("div", { className: "hover-overlay", onClick: D, children: /* @__PURE__ */ t("div", { className: "hover-pause-button", children: /* @__PURE__ */ t("span", { className: "pause-icon" }) }) }),
185
+ !n.isPlaying && n.isStarted && /* @__PURE__ */ t("div", { className: "pause-overlay", onClick: M, children: /* @__PURE__ */ t("div", { className: "overlay-play-button", children: /* @__PURE__ */ g("svg", { viewBox: "0 0 30 30", children: [
186
+ /* @__PURE__ */ t("polygon", { points: "6.583,3.186 5,4.004 5,15 26,15 26.483,14.128 " }),
187
+ /* @__PURE__ */ t("polygon", { points: "6.583,26.814 5,25.996 5,15 26,15 26.483,15.872 " }),
188
+ /* @__PURE__ */ t("circle", { cx: "26", cy: "15", r: "1" }),
189
+ /* @__PURE__ */ t("circle", { cx: "6", cy: "4", r: "1" }),
190
+ /* @__PURE__ */ t("circle", { cx: "6", cy: "26", r: "1" })
191
+ ] }) }) }),
192
+ /* @__PURE__ */ t("div", { className: "terminal", children: /* @__PURE__ */ g("div", { className: K(e), style: e.frameBox.style || {}, children: [
193
+ /* @__PURE__ */ g("div", { className: "terminal-titlebar", children: [
194
+ /* @__PURE__ */ g("div", { className: "buttons", children: [
195
+ /* @__PURE__ */ t("div", { className: "close-button" }),
196
+ /* @__PURE__ */ t("div", { className: "minimize-button" }),
197
+ /* @__PURE__ */ t("div", { className: "maximize-button" })
190
198
  ] }),
191
- /* @__PURE__ */ a("div", { className: "title", children: r.frameBox.title || "" })
199
+ /* @__PURE__ */ t("div", { className: "title", children: e.frameBox.title || "" })
192
200
  ] }),
193
- /* @__PURE__ */ a("div", { className: "terminal-body", children: /* @__PURE__ */ a(Y, { ref: i, options: J }) })
201
+ /* @__PURE__ */ t("div", { className: "terminal-body", children: /* @__PURE__ */ t(Y, { ref: i, options: I }) })
194
202
  ] }) }),
195
- /* @__PURE__ */ S("div", { className: "controller", children: [
196
- !t.isPlaying && t.isStarted && /* @__PURE__ */ a("div", { className: "play", onClick: O, title: "Play", children: /* @__PURE__ */ a("span", { className: "icon" }) }),
197
- t.isPlaying && /* @__PURE__ */ a("div", { className: "pause", onClick: L, title: "Pause", children: /* @__PURE__ */ a("span", { className: "icon" }) }),
198
- !t.isPlaying && !t.isStarted && /* @__PURE__ */ a("div", { className: "play", onClick: k, title: "Start", children: /* @__PURE__ */ a("span", { className: "icon" }) }),
199
- /* @__PURE__ */ a("div", { className: "timer", children: Q(t.currentTime) }),
200
- /* @__PURE__ */ a("div", { className: "progressbar-wrapper", children: /* @__PURE__ */ a("div", { className: "progressbar", ref: N, onClick: M, children: /* @__PURE__ */ a("div", { className: "progress", style: { width: `${t.currentTime / h * 100}%` } }) }) })
203
+ /* @__PURE__ */ g("div", { className: "controller", children: [
204
+ !n.isPlaying && n.isStarted && /* @__PURE__ */ t("div", { className: "play", onClick: M, title: "Play", children: /* @__PURE__ */ t("span", { className: "icon" }) }),
205
+ n.isPlaying && /* @__PURE__ */ t("div", { className: "pause", onClick: D, title: "Pause", children: /* @__PURE__ */ t("span", { className: "icon" }) }),
206
+ !n.isPlaying && !n.isStarted && /* @__PURE__ */ t("div", { className: "play", onClick: k, title: "Start", children: /* @__PURE__ */ t("span", { className: "icon" }) }),
207
+ /* @__PURE__ */ t("div", { className: "timer", children: Q(n.currentTime) }),
208
+ /* @__PURE__ */ t("div", { className: "progressbar-wrapper", children: /* @__PURE__ */ t("div", { className: "progressbar", ref: w, onClick: L, children: /* @__PURE__ */ t("div", { className: "progress", style: { width: `${n.currentTime / v * 100}%` } }) }) })
201
209
  ] })
202
210
  ] });
203
211
  }
204
212
  V.propTypes = {
205
- frames: l.array.isRequired,
206
- options: l.shape({
207
- autoplay: l.bool,
208
- repeat: l.bool,
209
- controls: l.bool,
210
- frameBox: l.object,
211
- theme: l.object,
212
- cols: l.number,
213
- rows: l.number
213
+ frames: c.array.isRequired,
214
+ options: c.shape({
215
+ autoplay: c.bool,
216
+ repeat: c.bool,
217
+ controls: c.bool,
218
+ frameBox: c.object,
219
+ theme: c.object,
220
+ cols: c.number,
221
+ rows: c.number,
222
+ autoplayThreshold: c.number,
223
+ autoplayRootMargin: c.string
214
224
  }).isRequired,
215
- onComplete: l.func,
216
- onStart: l.func,
217
- onStop: l.func,
218
- onPause: l.func,
219
- onTick: l.func,
220
- onJump: l.func
225
+ onComplete: c.func,
226
+ onStart: c.func,
227
+ onStop: c.func,
228
+ onPause: c.func,
229
+ onTick: c.func,
230
+ onJump: c.func
221
231
  };
222
232
  export {
223
233
  se as PLAYER_FRAME_DELAY,