@xiping/react-components 1.0.52 → 1.0.54
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/components/image-compare/ImageCompare.d.ts +6 -0
- package/dist/cjs/components/image-compare/ImageCompare.js +1 -1
- package/dist/cjs/components/subtitle-player/CurrentMode.d.ts +2 -0
- package/dist/cjs/components/subtitle-player/CurrentMode.js +2 -2
- package/dist/cjs/components/subtitle-player/LyricsMode.d.ts +2 -0
- package/dist/cjs/components/subtitle-player/LyricsMode.js +2 -2
- package/dist/cjs/components/subtitle-player/SubtitlePlayer.d.ts +34 -0
- package/dist/cjs/components/subtitle-player/SubtitlePlayer.js +1 -1
- package/dist/cjs/components/subtitle-player/utils.d.ts +1 -0
- package/dist/cjs/components/subtitle-player/utils.js +1 -1
- package/dist/cjs/react-components.css +1 -1
- package/dist/es/components/image-compare/ImageCompare.d.ts +6 -0
- package/dist/es/components/image-compare/ImageCompare.js +86 -67
- package/dist/es/components/subtitle-player/CurrentMode.d.ts +2 -0
- package/dist/es/components/subtitle-player/CurrentMode.js +25 -22
- package/dist/es/components/subtitle-player/LyricsMode.d.ts +2 -0
- package/dist/es/components/subtitle-player/LyricsMode.js +46 -43
- package/dist/es/components/subtitle-player/SubtitlePlayer.d.ts +34 -0
- package/dist/es/components/subtitle-player/SubtitlePlayer.js +146 -98
- package/dist/es/components/subtitle-player/utils.d.ts +1 -0
- package/dist/es/components/subtitle-player/utils.js +7 -6
- package/dist/es/react-components.css +1 -1
- package/package.json +16 -16
- package/dist/cjs/components/toast/index.d.ts +0 -4
- package/dist/cjs/components/translate/Translate.d.ts +0 -5
- package/dist/es/components/toast/index.d.ts +0 -4
- package/dist/es/components/translate/Translate.d.ts +0 -5
|
@@ -1,52 +1,54 @@
|
|
|
1
|
-
import { jsx as
|
|
2
|
-
import { useMemo as
|
|
3
|
-
import { parseSRT as
|
|
4
|
-
import { srtToJson as
|
|
5
|
-
import
|
|
6
|
-
import { CurrentMode as
|
|
7
|
-
import { LyricsMode as
|
|
8
|
-
import { getCurrentSubtitleEntry as
|
|
1
|
+
import { jsx as S, jsxs as z } from "react/jsx-runtime";
|
|
2
|
+
import { useMemo as h, useRef as F, useState as k, useEffect as G, useCallback as u } from "react";
|
|
3
|
+
import { parseSRT as w, parseVTT as v } from "../../packages/subtitle/lib/src/parser.js";
|
|
4
|
+
import { srtToJson as V, vttToJson as q } from "../../packages/subtitle/lib/src/json-converter.js";
|
|
5
|
+
import K from "clsx";
|
|
6
|
+
import { CurrentMode as Q } from "./CurrentMode.js";
|
|
7
|
+
import { LyricsMode as U } from "./LyricsMode.js";
|
|
8
|
+
import { getCurrentSubtitleEntry as X, timeStringToSeconds as A } from "./utils.js";
|
|
9
9
|
/* empty css */
|
|
10
|
-
const
|
|
11
|
-
subtitles:
|
|
12
|
-
currentTime:
|
|
13
|
-
mode:
|
|
14
|
-
className:
|
|
10
|
+
const ot = ({
|
|
11
|
+
subtitles: p,
|
|
12
|
+
currentTime: f,
|
|
13
|
+
mode: m = "current",
|
|
14
|
+
className: D = "",
|
|
15
15
|
style: J,
|
|
16
16
|
onWordHoverChange: a,
|
|
17
|
-
renderWordOverlay:
|
|
17
|
+
renderWordOverlay: d,
|
|
18
|
+
onWordClick: y,
|
|
19
|
+
onSubtitleClick: T
|
|
18
20
|
}) => {
|
|
19
|
-
const
|
|
20
|
-
() => Array.isArray(
|
|
21
|
-
[
|
|
22
|
-
),
|
|
23
|
-
|
|
21
|
+
const i = h(
|
|
22
|
+
() => Array.isArray(p) ? p : [p],
|
|
23
|
+
[p]
|
|
24
|
+
), l = F(null), [s, E] = k(null), [R, M] = k([]);
|
|
25
|
+
G(() => {
|
|
24
26
|
let r = !0;
|
|
25
|
-
async function
|
|
27
|
+
async function c() {
|
|
26
28
|
try {
|
|
27
|
-
const
|
|
28
|
-
|
|
29
|
+
const e = await Promise.all(
|
|
30
|
+
i.map(async (t) => {
|
|
29
31
|
try {
|
|
30
|
-
const n =
|
|
32
|
+
const n = t.language || "zh", o = t.type === "srt" ? await V(t.content, n) : await q(t.content, n);
|
|
31
33
|
return {
|
|
32
34
|
entries: JSON.parse(o),
|
|
33
|
-
label:
|
|
35
|
+
label: t.label
|
|
34
36
|
};
|
|
35
37
|
} catch (n) {
|
|
36
38
|
return console.error("字幕解析失败:", n), {
|
|
37
|
-
entries:
|
|
38
|
-
label:
|
|
39
|
+
entries: t.type === "srt" ? w(t.content) : v(t.content),
|
|
40
|
+
label: t.label
|
|
39
41
|
};
|
|
40
42
|
}
|
|
41
43
|
})
|
|
42
44
|
);
|
|
43
|
-
r &&
|
|
44
|
-
} catch (
|
|
45
|
-
if (console.error("加载字幕失败:",
|
|
46
|
-
const
|
|
45
|
+
r && M(e);
|
|
46
|
+
} catch (e) {
|
|
47
|
+
if (console.error("加载字幕失败:", e), r) {
|
|
48
|
+
const t = i.map((n) => {
|
|
47
49
|
try {
|
|
48
50
|
return {
|
|
49
|
-
entries: n.type === "srt" ?
|
|
51
|
+
entries: n.type === "srt" ? w(n.content) : v(n.content),
|
|
50
52
|
label: n.label
|
|
51
53
|
};
|
|
52
54
|
} catch (o) {
|
|
@@ -56,35 +58,35 @@ const H = ({
|
|
|
56
58
|
};
|
|
57
59
|
}
|
|
58
60
|
});
|
|
59
|
-
|
|
61
|
+
M(t);
|
|
60
62
|
}
|
|
61
63
|
}
|
|
62
64
|
}
|
|
63
|
-
return
|
|
65
|
+
return c(), () => {
|
|
64
66
|
r = !1;
|
|
65
67
|
};
|
|
66
|
-
}, [
|
|
67
|
-
const
|
|
68
|
+
}, [i]);
|
|
69
|
+
const g = h(() => R.length > 0 ? R : i.map((r) => {
|
|
68
70
|
try {
|
|
69
71
|
return {
|
|
70
|
-
entries: r.type === "srt" ?
|
|
72
|
+
entries: r.type === "srt" ? w(r.content) : v(r.content),
|
|
71
73
|
label: r.label
|
|
72
74
|
};
|
|
73
|
-
} catch (
|
|
74
|
-
return console.error("字幕解析失败:",
|
|
75
|
+
} catch (c) {
|
|
76
|
+
return console.error("字幕解析失败:", c), {
|
|
75
77
|
entries: [],
|
|
76
78
|
label: r.label
|
|
77
79
|
};
|
|
78
80
|
}
|
|
79
|
-
}), [
|
|
80
|
-
entry:
|
|
81
|
-
label:
|
|
81
|
+
}), [R, i]), L = h(() => g.map(({ entries: r, label: c }) => ({
|
|
82
|
+
entry: X(r, f),
|
|
83
|
+
label: c,
|
|
82
84
|
entries: r
|
|
83
85
|
// 保留所有条目用于歌词模式
|
|
84
|
-
})), [
|
|
86
|
+
})), [g, f]), W = h(() => {
|
|
85
87
|
const r = [];
|
|
86
|
-
|
|
87
|
-
|
|
88
|
+
g.forEach(({ entries: t, label: n }) => {
|
|
89
|
+
t.forEach((o) => {
|
|
88
90
|
r.push({
|
|
89
91
|
entry: o,
|
|
90
92
|
label: n,
|
|
@@ -92,86 +94,132 @@ const H = ({
|
|
|
92
94
|
endTime: A(o.endTime)
|
|
93
95
|
});
|
|
94
96
|
});
|
|
95
|
-
}), r.sort((
|
|
96
|
-
const
|
|
97
|
-
let
|
|
98
|
-
for (const
|
|
99
|
-
if (
|
|
100
|
-
|
|
97
|
+
}), r.sort((t, n) => t.startTime - n.startTime);
|
|
98
|
+
const c = [];
|
|
99
|
+
let e = [];
|
|
100
|
+
for (const t of r)
|
|
101
|
+
if (e.length === 0)
|
|
102
|
+
e.push(t);
|
|
101
103
|
else {
|
|
102
|
-
const n =
|
|
103
|
-
|
|
104
|
+
const n = e[e.length - 1];
|
|
105
|
+
t.startTime <= n.endTime ? e.push(t) : (c.push(e), e = [t]);
|
|
104
106
|
}
|
|
105
|
-
return
|
|
106
|
-
}, [
|
|
107
|
-
(r,
|
|
108
|
-
const
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
) :
|
|
114
|
-
...
|
|
107
|
+
return e.length > 0 && c.push(e), c;
|
|
108
|
+
}, [g]), b = u(
|
|
109
|
+
(r, c) => {
|
|
110
|
+
const e = r.currentTarget.getBoundingClientRect(), t = l.current?.getBoundingClientRect(), n = t !== void 0 ? new DOMRect(
|
|
111
|
+
e.left - t.left,
|
|
112
|
+
e.top - t.top,
|
|
113
|
+
e.width,
|
|
114
|
+
e.height
|
|
115
|
+
) : e, o = {
|
|
116
|
+
...c,
|
|
115
117
|
rect: n,
|
|
116
118
|
element: r.currentTarget
|
|
117
119
|
};
|
|
118
|
-
|
|
120
|
+
E(o), a?.(o);
|
|
119
121
|
},
|
|
120
122
|
[a]
|
|
121
|
-
),
|
|
122
|
-
|
|
123
|
-
}, [a]),
|
|
124
|
-
(r,
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
123
|
+
), x = u(() => {
|
|
124
|
+
E(null), a?.(null);
|
|
125
|
+
}, [a]), B = u(
|
|
126
|
+
(r, c) => {
|
|
127
|
+
if (!y) return;
|
|
128
|
+
r.stopPropagation();
|
|
129
|
+
const e = r.currentTarget.getBoundingClientRect(), t = l.current?.getBoundingClientRect(), n = t !== void 0 ? new DOMRect(
|
|
130
|
+
e.left - t.left,
|
|
131
|
+
e.top - t.top,
|
|
132
|
+
e.width,
|
|
133
|
+
e.height
|
|
134
|
+
) : e, o = {
|
|
135
|
+
type: "word",
|
|
136
|
+
...c,
|
|
137
|
+
rect: n,
|
|
138
|
+
element: r.currentTarget
|
|
139
|
+
};
|
|
140
|
+
y(o);
|
|
141
|
+
},
|
|
142
|
+
[y]
|
|
143
|
+
), C = u(
|
|
144
|
+
(r, c, e) => {
|
|
145
|
+
if (!T || r.target.closest(".xiping-subtitle-word"))
|
|
146
|
+
return;
|
|
147
|
+
const n = r.currentTarget.getBoundingClientRect(), o = l.current?.getBoundingClientRect(), j = o !== void 0 ? new DOMRect(
|
|
148
|
+
n.left - o.left,
|
|
149
|
+
n.top - o.top,
|
|
150
|
+
n.width,
|
|
151
|
+
n.height
|
|
152
|
+
) : n, _ = {
|
|
153
|
+
type: "subtitle",
|
|
154
|
+
entry: c,
|
|
155
|
+
label: e,
|
|
156
|
+
rect: j,
|
|
157
|
+
element: r.currentTarget
|
|
158
|
+
};
|
|
159
|
+
T(_);
|
|
160
|
+
},
|
|
161
|
+
[T]
|
|
162
|
+
), N = u(
|
|
163
|
+
(r, c) => (e, t, n) => ({
|
|
164
|
+
onMouseEnter: (o) => b(o, {
|
|
165
|
+
word: e,
|
|
166
|
+
wordIndex: t,
|
|
128
167
|
lineIndex: n,
|
|
129
168
|
entry: r,
|
|
130
|
-
label:
|
|
169
|
+
label: c
|
|
131
170
|
}),
|
|
132
|
-
onMouseLeave:
|
|
171
|
+
onMouseLeave: x,
|
|
172
|
+
onClick: (o) => B(o, {
|
|
173
|
+
word: e,
|
|
174
|
+
wordIndex: t,
|
|
175
|
+
lineIndex: n,
|
|
176
|
+
entry: r,
|
|
177
|
+
label: c
|
|
178
|
+
})
|
|
133
179
|
}),
|
|
134
|
-
[
|
|
135
|
-
),
|
|
180
|
+
[b, x, B]
|
|
181
|
+
), O = d && s ? /* @__PURE__ */ S(
|
|
136
182
|
"div",
|
|
137
183
|
{
|
|
138
184
|
className: "xiping-subtitle-player__hover-overlay",
|
|
139
185
|
style: {
|
|
140
|
-
left:
|
|
141
|
-
top:
|
|
186
|
+
left: s.rect.x + s.rect.width / 2,
|
|
187
|
+
top: s.rect.y + s.rect.height
|
|
142
188
|
},
|
|
143
|
-
children:
|
|
189
|
+
children: d(s)
|
|
144
190
|
}
|
|
145
|
-
) : null,
|
|
146
|
-
return /* @__PURE__ */
|
|
191
|
+
) : null, P = !!(d || a);
|
|
192
|
+
return /* @__PURE__ */ z(
|
|
147
193
|
"div",
|
|
148
194
|
{
|
|
149
|
-
ref:
|
|
150
|
-
className:
|
|
195
|
+
ref: l,
|
|
196
|
+
className: K(
|
|
151
197
|
"xiping-subtitle-player",
|
|
152
|
-
|
|
153
|
-
|
|
198
|
+
m === "lyrics" ? "xiping-subtitle-player--lyrics" : "xiping-subtitle-player--current",
|
|
199
|
+
D
|
|
154
200
|
),
|
|
155
201
|
style: J,
|
|
156
202
|
children: [
|
|
157
|
-
|
|
158
|
-
|
|
203
|
+
m === "current" && /* @__PURE__ */ S(
|
|
204
|
+
Q,
|
|
159
205
|
{
|
|
160
206
|
currentEntries: L,
|
|
161
|
-
wordHoverFactory:
|
|
162
|
-
enableWordHover:
|
|
163
|
-
overlayNode:
|
|
207
|
+
wordHoverFactory: N,
|
|
208
|
+
enableWordHover: P,
|
|
209
|
+
overlayNode: O,
|
|
210
|
+
onSubtitleClick: C
|
|
164
211
|
}
|
|
165
212
|
),
|
|
166
|
-
|
|
167
|
-
|
|
213
|
+
m === "lyrics" && /* @__PURE__ */ S(
|
|
214
|
+
U,
|
|
168
215
|
{
|
|
169
|
-
groupedEntriesByTime:
|
|
170
|
-
currentTime:
|
|
171
|
-
wordHoverFactory:
|
|
172
|
-
enableWordHover:
|
|
173
|
-
overlayNode:
|
|
174
|
-
containerRef:
|
|
216
|
+
groupedEntriesByTime: W,
|
|
217
|
+
currentTime: f,
|
|
218
|
+
wordHoverFactory: N,
|
|
219
|
+
enableWordHover: P,
|
|
220
|
+
overlayNode: O,
|
|
221
|
+
containerRef: l,
|
|
222
|
+
onSubtitleClick: C
|
|
175
223
|
}
|
|
176
224
|
)
|
|
177
225
|
]
|
|
@@ -179,6 +227,6 @@ const H = ({
|
|
|
179
227
|
);
|
|
180
228
|
};
|
|
181
229
|
export {
|
|
182
|
-
|
|
183
|
-
|
|
230
|
+
ot as SubtitlePlayer,
|
|
231
|
+
ot as default
|
|
184
232
|
};
|
|
@@ -18,6 +18,7 @@ export declare function getCurrentSubtitleEntry(entries: SubtitleEntry[], curren
|
|
|
18
18
|
export type WordHoverHandlerFactory = (word: string, wordIndex: number, lineIndex: number) => {
|
|
19
19
|
onMouseEnter: React.MouseEventHandler<HTMLSpanElement>;
|
|
20
20
|
onMouseLeave: React.MouseEventHandler<HTMLSpanElement>;
|
|
21
|
+
onClick?: React.MouseEventHandler<HTMLSpanElement>;
|
|
21
22
|
} | undefined;
|
|
22
23
|
/**
|
|
23
24
|
* 将文本按照分词结果分割并渲染,并返回消耗到的分词下标
|
|
@@ -24,28 +24,29 @@ function T(t, e, n, r, a) {
|
|
|
24
24
|
const s = [];
|
|
25
25
|
let i = 0;
|
|
26
26
|
for (let o = n; o < e.length; o++) {
|
|
27
|
-
const c = e[o],
|
|
28
|
-
if (
|
|
27
|
+
const c = e[o], f = t.substring(i).indexOf(c);
|
|
28
|
+
if (f === -1)
|
|
29
29
|
return {
|
|
30
30
|
nodes: s.length > 0 ? s : t,
|
|
31
31
|
nextWordIndex: o
|
|
32
32
|
};
|
|
33
|
-
const u = i +
|
|
33
|
+
const u = i + f;
|
|
34
34
|
if (u > i) {
|
|
35
35
|
const g = t.substring(i, u);
|
|
36
36
|
g && s.push(
|
|
37
37
|
/* @__PURE__ */ d("span", { className: "xiping-subtitle-word-before", children: g }, `before-${r}-${o}`)
|
|
38
38
|
);
|
|
39
39
|
}
|
|
40
|
-
const
|
|
40
|
+
const l = a?.(c, o, r);
|
|
41
41
|
if (s.push(
|
|
42
42
|
/* @__PURE__ */ d(
|
|
43
43
|
"span",
|
|
44
44
|
{
|
|
45
45
|
className: "xiping-subtitle-word",
|
|
46
46
|
"data-word": c,
|
|
47
|
-
onMouseEnter:
|
|
48
|
-
onMouseLeave:
|
|
47
|
+
onMouseEnter: l?.onMouseEnter,
|
|
48
|
+
onMouseLeave: l?.onMouseLeave,
|
|
49
|
+
onClick: l?.onClick,
|
|
49
50
|
children: c
|
|
50
51
|
},
|
|
51
52
|
`word-${r}-${o}`
|