@naersen/tts-rich-text 0.4.0 → 0.6.0

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/core.js CHANGED
@@ -4,49 +4,65 @@ const f = {
4
4
  }, h = {
5
5
  stop: {
6
6
  label: "停顿",
7
+ icon: "editor-stop",
7
8
  type: "stop",
8
9
  deleteMode: f.DELETE_NODE
9
10
  },
10
11
  customStop: {
11
12
  label: "长停顿",
13
+ icon: "editor-stop-long",
12
14
  type: "customStop",
13
15
  deleteMode: f.DELETE_NODE
14
16
  },
15
17
  continue: {
16
18
  label: "连续",
19
+ icon: "editor-continue",
17
20
  type: "continue",
18
21
  deleteMode: f.CONVERT_TO_TEXT
19
22
  },
20
23
  pinyin: {
21
24
  label: "拼音标注",
25
+ icon: "editor-pinyin",
22
26
  type: "pinyin",
23
27
  deleteMode: f.CONVERT_TO_TEXT
24
28
  },
25
29
  replace: {
26
30
  label: "替换",
31
+ icon: "editor-replace",
27
32
  type: "replace",
28
33
  deleteMode: f.CONVERT_TO_TEXT
29
34
  },
30
35
  language: {
31
36
  label: "语种标记",
37
+ icon: "editor-language",
32
38
  type: "language",
33
39
  deleteMode: f.CONVERT_TO_TEXT
34
40
  },
35
41
  number: {
36
42
  label: "数字读法",
43
+ icon: "editor-number",
37
44
  type: "number"
38
45
  },
39
46
  formula: {
40
47
  label: "读公式",
48
+ icon: "editor-formula",
41
49
  type: "formula"
42
50
  },
43
51
  aiRewrite: {
44
52
  label: "AI 改写",
53
+ icon: "editor-ai-rewrite",
45
54
  type: "aiRewrite"
46
55
  },
47
56
  upload: {
48
57
  label: "上传文稿",
58
+ icon: "editor-upload",
49
59
  type: "upload"
60
+ },
61
+ ignore: {
62
+ label: "忽略读音",
63
+ icon: "editor-ingore",
64
+ type: "ignore",
65
+ deleteMode: f.DELETE_NODE
50
66
  }
51
67
  }, _ = [
52
68
  { value: "zh", label: "中文" },
@@ -59,7 +75,7 @@ const f = {
59
75
  { value: "it", label: "意大利语" },
60
76
  { value: "pt", label: "葡萄牙语" },
61
77
  { value: "ru", label: "俄语" }
62
- ], le = [
78
+ ], ie = [
63
79
  { label: "停顿 0.5 秒", value: 0.5 },
64
80
  { label: "停顿 1 秒", value: 1 }
65
81
  ], S = Object.keys(h), d = {
@@ -85,7 +101,7 @@ const f = {
85
101
  )
86
102
  },
87
103
  2: d
88
- }, ae = (e, o = {}) => {
104
+ }, le = (e, o = {}) => {
89
105
  const t = C[e] ?? d;
90
106
  return {
91
107
  ...d,
@@ -94,7 +110,7 @@ const f = {
94
110
  tools: o.tools ?? t.tools ?? d.tools,
95
111
  languages: o.languages ?? t.languages ?? d.languages
96
112
  };
97
- }, ie = () => ({
113
+ }, ae = () => ({
98
114
  type: "doc",
99
115
  content: [
100
116
  {
@@ -102,7 +118,7 @@ const f = {
102
118
  content: []
103
119
  }
104
120
  ]
105
- }), I = {
121
+ }), y = {
106
122
  0: "零",
107
123
  1: "一",
108
124
  2: "二",
@@ -126,7 +142,7 @@ const f = {
126
142
  9: "九"
127
143
  }, A = ["", "十", "百", "千"], D = ["", "万", "亿", "万亿"];
128
144
  function w(e) {
129
- return e ? e.split("").map((o) => I[o] || o).join("") : "";
145
+ return e ? e.split("").map((o) => y[o] || o).join("") : "";
130
146
  }
131
147
  function ce(e) {
132
148
  return e ? e.split("").map((o) => v[o] || o).join("") : "";
@@ -135,36 +151,36 @@ function V(e) {
135
151
  const o = e.length;
136
152
  let t = "", n = !1;
137
153
  for (let s = 0; s < o; s++) {
138
- const i = e[s], r = o - 1 - s;
139
- i === "0" ? !n && t !== "" && (n = !0) : (n && (t += "零", n = !1), t += I[i] + A[r]);
154
+ const a = e[s], r = o - 1 - s;
155
+ a === "0" ? !n && t !== "" && (n = !0) : (n && (t += "零", n = !1), t += y[a] + A[r]);
140
156
  }
141
157
  return t.startsWith("一十") && t.length === 2 && (t = t.substring(1)), t;
142
158
  }
143
- function F(e) {
159
+ function I(e) {
144
160
  if (!e) return "";
145
161
  if (e === "0" || (e = e.replace(/^0+/, "") || "0", e === "0")) return "零";
146
162
  let o = "";
147
163
  const t = e.length, n = [];
148
164
  for (let s = t; s > 0; s -= 4) {
149
- const i = Math.max(0, s - 4);
150
- n.unshift(e.substring(i, s));
165
+ const a = Math.max(0, s - 4);
166
+ n.unshift(e.substring(a, s));
151
167
  }
152
168
  for (let s = 0; s < n.length; s++) {
153
- const i = n[s], r = V(i);
169
+ const a = n[s], r = V(a);
154
170
  if (r !== "") {
155
- const l = n.length - 1 - s;
156
- o += r + D[l];
171
+ const i = n.length - 1 - s;
172
+ o += r + D[i];
157
173
  }
158
174
  }
159
175
  return o;
160
176
  }
161
177
  function $(e) {
162
- return F(e);
178
+ return I(e);
163
179
  }
164
180
  const ue = [
165
181
  { label: "基数读法", func: w },
166
182
  // { label: '位数读法', func: toPhoneReading },
167
- { label: "位数读法", func: F },
183
+ { label: "位数读法", func: I },
168
184
  { label: "口语读法", func: $ }
169
185
  ], B = /[\u0000-\u0008\u000B-\u001F\u007F-\u009F\u00AD\u200B-\u200F\u202A-\u202E\u2060-\u206F\uFE00-\uFE0F\uFEFF\uFFFD\uE000-\uF8FF]/g, j = /[\t\f\v\u00A0\u1680\u2000-\u200A\u202F\u205F\u3000]/g, L = {
170
186
  "‘": "'",
@@ -218,10 +234,10 @@ const ue = [
218
234
  minSegmentLength: 32,
219
235
  maxSegmentLength: 128,
220
236
  rebalanceRatio: 0.4
221
- }, y = /[一-鿿㐀-䶿豈-﫿 -〿＀-￯]/, X = /[。!?!?\.]/, G = /[;;]/, W = /[,,、::]/, K = /[。!?!?,,、;;::\.]/, O = "", R = "", p = (e) => {
237
+ }, F = /[一-鿿㐀-䶿豈-﫿 -〿＀-￯]/, X = /[。!?!?\.]/, G = /[;;]/, W = /[,,、::]/, K = /[。!?!?,,、;;::\.]/, O = "", R = "", p = (e) => {
222
238
  let o = 0;
223
239
  for (const t of e)
224
- o += y.test(t) ? 2 : 1;
240
+ o += F.test(t) ? 2 : 1;
225
241
  return o;
226
242
  }, Y = (e) => e.replace(/\r/g, "").split(`
227
243
  `).map((o) => o.trim()).filter(Boolean).join(" ").replace(/\s+/g, " ").trim(), q = (e) => {
@@ -234,28 +250,28 @@ const ue = [
234
250
  }, b = (e) => e.split(O).join(",").split(R).join("."), E = (e, o) => {
235
251
  let t = 0;
236
252
  for (let n = 0; n < e.length; n += 1)
237
- if (t += y.test(e[n]) ? 2 : 1, t >= o)
253
+ if (t += F.test(e[n]) ? 2 : 1, t >= o)
238
254
  return n + 1;
239
255
  return e.length;
240
256
  }, x = (e, o, t, n, s) => {
241
- let i = -1, r = Number.POSITIVE_INFINITY;
242
- const l = Math.max(1, t), c = Math.min(e.length, n);
243
- for (let a = l; a <= c; a += 1) {
244
- if (!s.test(e[a - 1]))
257
+ let a = -1, r = Number.POSITIVE_INFINITY;
258
+ const i = Math.max(1, t), c = Math.min(e.length, n);
259
+ for (let l = i; l <= c; l += 1) {
260
+ if (!s.test(e[l - 1]))
245
261
  continue;
246
- const u = Math.abs(a - o);
247
- if (u < r && (r = u, i = a), a > o && i >= 0)
262
+ const u = Math.abs(l - o);
263
+ if (u < r && (r = u, a = l), l > o && a >= 0)
248
264
  break;
249
265
  }
250
- return i;
266
+ return a;
251
267
  }, P = (e, o, t, n) => {
252
- let s = -1, i = Number.POSITIVE_INFINITY;
253
- const r = Math.max(1, t), l = Math.min(e.length, n);
254
- for (let c = r; c <= l; c += 1) {
268
+ let s = -1, a = Number.POSITIVE_INFINITY;
269
+ const r = Math.max(1, t), i = Math.min(e.length, n);
270
+ for (let c = r; c <= i; c += 1) {
255
271
  if (e[c - 1] !== " ")
256
272
  continue;
257
- const a = Math.abs(c - o);
258
- if (a < i && (i = a, s = c), c > o && s >= 0)
273
+ const l = Math.abs(c - o);
274
+ if (l < a && (a = l, s = c), c > o && s >= 0)
259
275
  break;
260
276
  }
261
277
  return s;
@@ -263,52 +279,52 @@ const ue = [
263
279
  const t = [];
264
280
  let n = e.trim();
265
281
  for (; n && p(n) > o; ) {
266
- const s = p(n), i = Math.min(
282
+ const s = p(n), a = Math.min(
267
283
  Math.floor(s / 2),
268
284
  o
269
- ), r = E(n, i), l = Math.max(1, Math.floor(r / 3)), c = Math.max(
285
+ ), r = E(n, a), i = Math.max(1, Math.floor(r / 3)), c = Math.max(
270
286
  r + 1,
271
287
  E(n, o)
272
288
  );
273
- let a = x(
289
+ let l = x(
274
290
  n,
275
291
  r,
276
- l,
292
+ i,
277
293
  c,
278
294
  G
279
295
  );
280
- a < 0 && (a = x(
296
+ l < 0 && (l = x(
281
297
  n,
282
298
  r,
283
- l,
299
+ i,
284
300
  c,
285
301
  W
286
- )), a < 0 && (a = P(
302
+ )), l < 0 && (l = P(
287
303
  n,
288
304
  r,
289
- l,
305
+ i,
290
306
  c
291
- )), (a < 0 || a >= n.length) && (a = r), t.push(n.slice(0, a).trim()), n = n.slice(a).trim();
307
+ )), (l < 0 || l >= n.length) && (l = r), t.push(n.slice(0, l).trim()), n = n.slice(l).trim();
292
308
  }
293
309
  return n && t.push(n), t;
294
310
  }, Z = (e, o) => {
295
311
  if (e.length <= 1)
296
312
  return e;
297
- const t = e[e.length - 1], n = e.reduce((T, m) => T + p(m), 0) / e.length, s = Math.floor(n * o);
313
+ const t = e[e.length - 1], n = e.reduce((m, T) => m + p(T), 0) / e.length, s = Math.floor(n * o);
298
314
  if (p(t) >= s)
299
315
  return e;
300
- const r = `${e[e.length - 2]}${t}`, l = Math.floor(p(r) / 2), c = E(r, l), a = Math.max(1, Math.floor(c / 3));
316
+ const r = `${e[e.length - 2]}${t}`, i = Math.floor(p(r) / 2), c = E(r, i), l = Math.max(1, Math.floor(c / 3));
301
317
  let u = x(
302
318
  r,
303
319
  c,
304
- a,
320
+ l,
305
321
  r.length,
306
322
  K
307
323
  );
308
324
  if (u < 0 && (u = P(
309
325
  r,
310
326
  c,
311
- a,
327
+ l,
312
328
  r.length
313
329
  )), u <= 0 || u >= r.length)
314
330
  return e;
@@ -324,17 +340,17 @@ const ue = [
324
340
  const s = q(n);
325
341
  if (p(s) <= t.minSegmentLength)
326
342
  return [b(s)];
327
- const i = [], r = s.split(new RegExp("(?<=[。!?!?\\.])"));
328
- let l = "";
329
- for (const a of r) {
330
- if (!a)
343
+ const a = [], r = s.split(new RegExp("(?<=[。!?!?\\.])"));
344
+ let i = "";
345
+ for (const l of r) {
346
+ if (!l)
331
347
  continue;
332
- l += a;
333
- const u = l[l.length - 1] ?? "";
334
- p(l) >= t.minSegmentLength && X.test(u) && (i.push(l.trim()), l = "");
348
+ i += l;
349
+ const u = i[i.length - 1] ?? "";
350
+ p(i) >= t.minSegmentLength && X.test(u) && (a.push(i.trim()), i = "");
335
351
  }
336
- l && (i.length > 0 && p(l) < t.minSegmentLength ? i[i.length - 1] += l : i.push(l.trim()));
337
- const c = i.flatMap((a) => p(a) <= t.maxSegmentLength ? [a] : J(a, t.maxSegmentLength));
352
+ i && (a.length > 0 && p(i) < t.minSegmentLength ? a[a.length - 1] += i : a.push(i.trim()));
353
+ const c = a.flatMap((l) => p(l) <= t.maxSegmentLength ? [l] : J(l, t.maxSegmentLength));
338
354
  return Z(
339
355
  c,
340
356
  t.rebalanceRatio
@@ -342,84 +358,108 @@ const ue = [
342
358
  }, M = (e) => e.type === "text" ? e.text || "" : (e.content || []).map(M).join(""), ee = (e) => {
343
359
  const o = e.content || [];
344
360
  let t = "";
345
- const n = [], s = [], i = {
346
- [h.stop.type]: ({ position: r, actionCode: l }) => {
361
+ const n = [], s = [], a = {
362
+ [h.stop.type]: ({ position: r, actionCode: i }) => {
347
363
  n.push({
348
364
  action: "insert",
349
365
  model: "control",
350
366
  payload: "。 ",
351
367
  position: r,
352
368
  text: "",
353
- actionCode: l
369
+ actionCode: i
354
370
  });
355
371
  },
356
- [h.customStop.type]: ({ payload: r, position: l, actionCode: c }) => {
372
+ [h.customStop.type]: ({ payload: r, position: i, actionCode: c }) => {
357
373
  n.push({
358
374
  action: "insert",
359
375
  model: "control",
360
376
  payload: `[stop:${r == null ? void 0 : r.time}]`,
361
- position: l,
377
+ position: i,
362
378
  text: "",
363
379
  actionCode: c
364
380
  });
365
381
  },
366
- [h.continue.type]: ({ position: r, text: l, actionCode: c }) => {
382
+ [h.continue.type]: ({ position: r, text: i, actionCode: c }) => {
367
383
  n.push({
368
384
  action: "replace",
369
385
  model: "replace",
370
- payload: `《${l}》`,
386
+ payload: `《${i}》`,
371
387
  position: r,
372
- text: l,
388
+ text: i,
373
389
  actionCode: c
374
390
  });
375
391
  },
376
- [h.replace.type]: ({ payload: r, position: l, text: c, actionCode: a }) => {
392
+ [h.replace.type]: ({
393
+ payload: r,
394
+ position: i,
395
+ text: c,
396
+ actionCode: l
397
+ }) => {
377
398
  n.push({
378
399
  action: "replace",
379
400
  model: "replace",
380
401
  payload: `${r == null ? void 0 : r.text}`,
381
- position: l,
402
+ position: i,
382
403
  text: c,
383
- actionCode: a
404
+ actionCode: l
384
405
  });
385
406
  },
386
- [h.pinyin.type]: ({ payload: r, position: l, text: c, actionCode: a }) => {
407
+ [h.pinyin.type]: ({ payload: r, position: i, text: c, actionCode: l }) => {
387
408
  n.push({
388
409
  action: "replace",
389
410
  model: "pinyin",
390
411
  payload: `${r == null ? void 0 : r.text}`,
391
- position: l,
412
+ position: i,
392
413
  text: c,
393
- actionCode: a
414
+ actionCode: l
394
415
  });
395
416
  },
396
- [h.language.type]: ({ payload: r, position: l, text: c, actionCode: a }) => {
417
+ [h.language.type]: ({
418
+ payload: r,
419
+ position: i,
420
+ text: c,
421
+ actionCode: l
422
+ }) => {
397
423
  n.push({
398
424
  action: "language",
399
425
  model: "language",
400
426
  payload: `${r == null ? void 0 : r.code}`,
401
- position: l,
427
+ position: i,
402
428
  text: c,
403
- actionCode: a
429
+ actionCode: l
430
+ });
431
+ },
432
+ [h.ignore.type]: ({
433
+ position: r,
434
+ text: i,
435
+ actionCode: c
436
+ }) => {
437
+ n.push({
438
+ action: "replace",
439
+ model: "replace",
440
+ payload: " ",
441
+ position: r,
442
+ text: i,
443
+ actionCode: c
404
444
  });
405
445
  }
406
446
  };
407
447
  return o.forEach((r) => {
408
- var T, m;
448
+ var m, T;
409
449
  if (r.type === "text") {
410
450
  t += r.text || "";
411
451
  return;
412
452
  }
413
453
  if (r.type !== "customTag") return;
414
- const l = (T = r.attrs) == null ? void 0 : T.categ, c = (m = r.attrs) == null ? void 0 : m.payload, a = t.length, u = M(r), g = i[l];
454
+ const i = (m = r.attrs) == null ? void 0 : m.categ, c = (T = r.attrs) == null ? void 0 : T.payload, l = t.length, u = M(r), g = a[i];
415
455
  u && s.push({
416
- start: a,
417
- end: a + u.length
456
+ start: l,
457
+ end: l + u.length
418
458
  }), g && g({
419
459
  payload: c,
420
- position: a,
460
+ position: l,
421
461
  text: u,
422
- actionCode: l
462
+ actionCode: i
423
463
  }), t += u;
424
464
  }), {
425
465
  text: t,
@@ -431,7 +471,9 @@ const ue = [
431
471
  if (e.type === "paragraph") return [e];
432
472
  if (e.type === "doc")
433
473
  return o.filter((n) => n.type === "paragraph");
434
- const t = o.filter((n) => n.type === "paragraph");
474
+ const t = o.filter(
475
+ (n) => n.type === "paragraph"
476
+ );
435
477
  return t.length ? t : o.some(N) ? [
436
478
  {
437
479
  type: "paragraph",
@@ -441,19 +483,19 @@ const ue = [
441
483
  }, ne = (e, o) => {
442
484
  let t = 0;
443
485
  return o.map((n) => {
444
- const s = e.indexOf(n, t), i = s >= 0 ? s : t, r = i + n.length;
486
+ const s = e.indexOf(n, t), a = s >= 0 ? s : t, r = a + n.length;
445
487
  return t = r, {
446
488
  text: n,
447
- start: i,
489
+ start: a,
448
490
  end: r
449
491
  };
450
492
  });
451
493
  }, oe = (e, o) => e.reduce((t, n) => {
452
494
  if (o.some(
453
- (i) => n.start > i.start && n.start < i.end
495
+ (a) => n.start > a.start && n.start < a.end
454
496
  ) && t.length) {
455
- const i = t[t.length - 1];
456
- return i.text += n.text, i.end = n.end, t;
497
+ const a = t[t.length - 1];
498
+ return a.text += n.text, a.end = n.end, t;
457
499
  }
458
500
  return t.push({ ...n }), t;
459
501
  }, []), re = (e, o, t, n) => e.position >= o && e.position < t ? !0 : !e.text && n && e.position === t, se = (e) => {
@@ -464,11 +506,16 @@ const ue = [
464
506
  e.protectedRanges || []
465
507
  );
466
508
  return t.map((n, s) => {
467
- const i = s === t.length - 1, r = e.playments.filter(
468
- (l) => re(l, n.start, n.end, i)
469
- ).map((l) => ({
470
- ...l,
471
- position: l.position - n.start
509
+ const a = s === t.length - 1, r = e.playments.filter(
510
+ (i) => re(
511
+ i,
512
+ n.start,
513
+ n.end,
514
+ a
515
+ )
516
+ ).map((i) => ({
517
+ ...i,
518
+ position: i.position - n.start
472
519
  }));
473
520
  return {
474
521
  text: n.text,
@@ -482,8 +529,8 @@ const ue = [
482
529
  };
483
530
  return te(e).forEach((n) => {
484
531
  const s = ee(n);
485
- !s.text && !s.playments.length || se(s).forEach(({ text: i, playments: r }) => {
486
- o.sentences.push(i), o.playments.push(r);
532
+ !s.text && !s.playments.length || se(s).forEach(({ text: a, playments: r }) => {
533
+ o.sentences.push(a), o.playments.push(r);
487
534
  });
488
535
  }), o;
489
536
  };
@@ -491,19 +538,19 @@ export {
491
538
  f as DeleteMode,
492
539
  _ as LanguageOptions,
493
540
  ue as NumberToTextTools,
494
- le as StopOptions,
541
+ ie as StopOptions,
495
542
  h as ToolConfig,
496
543
  S as allToolTypes,
497
544
  ge as convertToTtsFormat,
498
- ie as getInitContent,
545
+ ae as getInitContent,
499
546
  p as getVisualLength,
500
- ae as resolveTtsEditorFeatures,
547
+ le as resolveTtsEditorFeatures,
501
548
  pe as sanitizePastedHtml,
502
549
  H as sanitizePastedText,
503
550
  Q as splitTextToSentences,
504
551
  w as toCardinal,
505
552
  $ as toColloquial,
506
553
  ce as toPhoneReading,
507
- F as toPositional,
554
+ I as toPositional,
508
555
  ee as transformParagraph
509
556
  };
@@ -1,4 +1,8 @@
1
1
  export { useEditorContext, useEditorProvider, useRichTextEditor, type RichTextEditor, type UseRichTextEditorOptions } from '../components/tts-rich-text/editor/useRichTextEditor';
2
2
  export { createCustomTagNode, CustomTagName } from '../components/tts-rich-text/editor/nodes/tag-node';
3
3
  export { createTtsCharacterCount } from '../components/tts-rich-text/editor/plugins/tts-character-count';
4
+ export { useSSETool, type UseSseToolOptions } from '../components/tts-rich-text/editor/useSSETool';
5
+ export { useTestAudioTool } from '../components/tts-rich-text/editor/useTestAudioTool';
6
+ export { globalAudioControlKey, useGlobalAudioControl } from '../components/tts-rich-text/hooks/useGlobalAudioControl';
7
+ export { useSynthesizeAudioTool } from '../components/tts-rich-text/hooks/useSynthesizeAudioTool';
4
8
  export { useVoiceChangeConfirm } from '../components/tts-rich-text/hooks/useVoiceChangeConfirm';