@naersen/tts-rich-text 0.10.3 → 0.11.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
@@ -1,7 +1,7 @@
1
1
  const f = {
2
2
  DELETE_NODE: "deleteNode",
3
3
  CONVERT_TO_TEXT: "convertToText"
4
- }, g = {
4
+ }, pe = 256, fe = 256, g = {
5
5
  stop: {
6
6
  label: "停顿",
7
7
  icon: "editor-stop",
@@ -70,7 +70,7 @@ const f = {
70
70
  type: "ignore",
71
71
  deleteMode: f.CONVERT_TO_TEXT
72
72
  }
73
- }, S = [
73
+ }, P = [
74
74
  { label: "笑声", value: "笑声", tag: "[laughter]" },
75
75
  { label: "叹气", value: "叹气", tag: "[sigh]" },
76
76
  { label: "吸气", value: "吸气", tag: "[sniff]" },
@@ -96,10 +96,10 @@ const f = {
96
96
  { value: "it", label: "意大利语" },
97
97
  { value: "pt", label: "葡萄牙语" },
98
98
  { value: "ru", label: "俄语" }
99
- ], v = [
99
+ ], S = [
100
100
  { label: "停顿 0.5 秒", value: 0.5 },
101
101
  { label: "停顿 1 秒", value: 1 }
102
- ], pe = {
102
+ ], he = {
103
103
  SENTENCE: "sentence",
104
104
  CUSTOM_TAG: "customTag",
105
105
  /**
@@ -107,16 +107,16 @@ const f = {
107
107
  * 工具栏与右键面板都要用它来主动收起气泡,避免两个浮层叠在一起。
108
108
  */
109
109
  BUBBLE_MENU_KEY: "ttsRichTextBubbleMenu"
110
- }, fe = (e) => {
110
+ }, Ee = (e) => {
111
111
  var t;
112
112
  return ((t = g[e]) == null ? void 0 : t.deleteMode) === f.DELETE_NODE;
113
- }, he = (e) => e === g.stop.type || e === g.customStop.type, de = (e) => e === g.customStop.type ? { time: v[0].value } : {}, Te = (e, t) => e === g.customStop.type ? `${t == null ? void 0 : t.time} 秒` : "", x = Object.keys(g), d = {
114
- tools: x.filter((e) => e !== g.tone.type),
113
+ }, Te = (e) => e === g.stop.type || e === g.customStop.type, de = (e) => e === g.customStop.type ? { time: S[0].value } : {}, me = (e, t) => e === g.customStop.type ? `${t == null ? void 0 : t.time} 秒` : "", N = Object.keys(g), E = {
114
+ tools: N.filter((e) => e !== g.tone.type),
115
115
  languages: _,
116
116
  maxPinyinLength: 4,
117
117
  maxNumberLength: 16,
118
118
  enablePreview: !0
119
- }, F = {
119
+ }, v = {
120
120
  1: {
121
121
  tools: [
122
122
  "customStop",
@@ -131,18 +131,18 @@ const f = {
131
131
  ],
132
132
  languages: _.filter((e) => ["zh", "en"].includes(e.value))
133
133
  },
134
- 2: d,
135
- 3: { tools: x.filter((e) => e !== g.language.type) }
136
- }, Ee = (e, t = {}) => {
137
- const n = F[e] ?? d;
134
+ 2: E,
135
+ 3: { tools: N.filter((e) => e !== g.language.type) }
136
+ }, be = (e, t = {}) => {
137
+ const n = v[e] ?? E;
138
138
  return {
139
- ...d,
139
+ ...E,
140
140
  ...n,
141
141
  ...t,
142
- tools: t.tools ?? n.tools ?? d.tools,
143
- languages: t.languages ?? n.languages ?? d.languages
142
+ tools: t.tools ?? n.tools ?? E.tools,
143
+ languages: t.languages ?? n.languages ?? E.languages
144
144
  };
145
- }, me = () => ({
145
+ }, _e = () => ({
146
146
  type: "doc",
147
147
  content: [
148
148
  {
@@ -150,13 +150,13 @@ const f = {
150
150
  content: []
151
151
  }
152
152
  ]
153
- }), A = /[\u4e00-\u9fff]/, M = /\s/g, D = 0.28, w = 0.14, be = (e, t = 1) => {
153
+ }), F = /[\u4e00-\u9fff]/, M = /\s/g, D = 0.28, H = 0.14, Ne = (e, t = 1) => {
154
154
  const n = Number.isFinite(t) && t > 0 ? t : 1;
155
155
  return Array.from(e.replace(M, "")).reduce(
156
- (s, a) => s + (A.test(a) ? D : w),
156
+ (s, a) => s + (F.test(a) ? D : H),
157
157
  0
158
158
  ) / n;
159
- }, N = {
159
+ }, x = {
160
160
  0: "零",
161
161
  1: "一",
162
162
  2: "二",
@@ -167,7 +167,7 @@ const f = {
167
167
  7: "七",
168
168
  8: "八",
169
169
  9: "九"
170
- }, B = {
170
+ }, w = {
171
171
  0: "洞",
172
172
  1: "幺",
173
173
  2: "二",
@@ -178,23 +178,23 @@ const f = {
178
178
  7: "七",
179
179
  8: "八",
180
180
  9: "九"
181
- }, H = ["", "十", "百", "千"], k = ["", "万", "亿", "万亿"];
182
- function U(e) {
183
- return e ? e.split("").map((t) => N[t] || t).join("") : "";
181
+ }, B = ["", "十", "百", "千"], U = ["", "万", "亿", "万亿"];
182
+ function k(e) {
183
+ return e ? e.split("").map((t) => x[t] || t).join("") : "";
184
184
  }
185
- function _e(e) {
186
- return e ? e.split("").map((t) => B[t] || t).join("") : "";
185
+ function xe(e) {
186
+ return e ? e.split("").map((t) => w[t] || t).join("") : "";
187
187
  }
188
188
  function $(e) {
189
189
  const t = e.length;
190
190
  let n = "", o = !1;
191
191
  for (let s = 0; s < t; s++) {
192
192
  const a = e[s], r = t - 1 - s;
193
- a === "0" ? !o && n !== "" && (o = !0) : (o && (n += "零", o = !1), n += N[a] + H[r]);
193
+ a === "0" ? !o && n !== "" && (o = !0) : (o && (n += "零", o = !1), n += x[a] + B[r]);
194
194
  }
195
195
  return n.startsWith("一十") && n.length === 2 && (n = n.substring(1)), n;
196
196
  }
197
- function y(e) {
197
+ function L(e) {
198
198
  if (!e) return "";
199
199
  if (e === "0" || (e = e.replace(/^0+/, "") || "0", e === "0")) return "零";
200
200
  let t = "";
@@ -207,20 +207,20 @@ function y(e) {
207
207
  const a = o[s], r = $(a);
208
208
  if (r !== "") {
209
209
  const l = o.length - 1 - s;
210
- t += r + k[l];
210
+ t += r + U[l];
211
211
  }
212
212
  }
213
213
  return t;
214
214
  }
215
215
  function V(e) {
216
- return y(e);
216
+ return L(e);
217
217
  }
218
- const xe = [
219
- { label: "基数读法", func: U },
218
+ const Le = [
219
+ { label: "基数读法", func: k },
220
220
  // { label: '位数读法', func: toPhoneReading },
221
- { label: "位数读法", func: y },
221
+ { label: "位数读法", func: L },
222
222
  { label: "口语读法", func: V }
223
- ], j = /[\u0000-\u0008\u000B-\u001F\u007F-\u009F\u00AD\u200B-\u200F\u202A-\u202E\u2060-\u206F\uFE00-\uFE0F\uFEFF\uFFFD\uE000-\uF8FF]/g, X = /[\t\f\v\u00A0\u1680\u2000-\u200A\u202F\u205F\u3000]/g, R = {
223
+ ], X = /[\u0000-\u0008\u000B-\u001F\u007F-\u009F\u00AD\u200B-\u200F\u202A-\u202E\u2060-\u206F\uFE00-\uFE0F\uFEFF\uFFFD\uE000-\uF8FF]/g, G = /[\t\f\v\u00A0\u1680\u2000-\u200A\u202F\u205F\u3000]/g, y = {
224
224
  "‘": "'",
225
225
  "’": "'",
226
226
  "‚": "'",
@@ -252,12 +252,12 @@ const xe = [
252
252
  "︔": ";",
253
253
  "︕": "!",
254
254
  "︖": "?"
255
- }, z = new RegExp(`[${Object.keys(R).join("")}]`, "g"), G = (e) => e.replace(/[\uFF21-\uFF3A\uFF41-\uFF5A\uFF10-\uFF19]/g, (t) => String.fromCharCode(t.charCodeAt(0) - 65248)), q = (e) => G(e).replace(/\r\n?/g, `
255
+ }, j = new RegExp(`[${Object.keys(y).join("")}]`, "g"), z = (e) => e.replace(/[\uFF21-\uFF3A\uFF41-\uFF5A\uFF10-\uFF19]/g, (t) => String.fromCharCode(t.charCodeAt(0) - 65248)), q = (e) => z(e).replace(/\r\n?/g, `
256
256
  `).replace(/[\u2028\u2029]/g, `
257
- `).replace(j, "").replace(X, " ").replace(z, (t) => R[t] || "").replace(/ *\n */g, `
257
+ `).replace(X, "").replace(G, " ").replace(j, (t) => y[t] || "").replace(/ *\n */g, `
258
258
  `).replace(/\n{3,}/g, `
259
259
 
260
- `), Ne = (e) => {
260
+ `), ye = (e) => {
261
261
  if (typeof window > "u" || !e)
262
262
  return e;
263
263
  const n = new DOMParser().parseFromString(e, "text/html"), o = n.createTreeWalker(n.body, NodeFilter.SHOW_TEXT);
@@ -269,10 +269,10 @@ const xe = [
269
269
  minSegmentLength: 32,
270
270
  maxSegmentLength: 128,
271
271
  rebalanceRatio: 0.4
272
- }, L = /[一-鿿㐀-䶿豈-﫿 -〿＀-￯]/, K = /[。!?!?\.]/, Y = /[;;]/, J = /[,,、::]/, Z = /[。!?!?,,、;;::]/, I = "", O = "", h = (e) => {
272
+ }, R = /[一-鿿㐀-䶿豈-﫿 -〿＀-￯]/, K = /[。!?!?\.]/, Y = /[;;]/, J = /[,,、::]/, Z = /[。!?!?,,、;;::]/, I = "", O = "", h = (e) => {
273
273
  let t = 0;
274
274
  for (const n of e)
275
- t += L.test(n) ? 2 : 1;
275
+ t += R.test(n) ? 2 : 1;
276
276
  return t;
277
277
  }, Q = (e) => e.replace(/\r/g, "").split(`
278
278
  `).map((t) => t.trim()).filter(Boolean).join(" ").replace(/\s+/g, " ").trim(), ee = (e) => {
@@ -284,23 +284,23 @@ const xe = [
284
284
  return t;
285
285
  }, m = (e) => e.split(I).join(",").split(O).join("."), te = (e, t, n, o, s) => {
286
286
  let a = -1, r = Number.POSITIVE_INFINITY;
287
- const l = Math.max(1, n), c = Math.min(e.length, o);
288
- for (let i = l; i <= c; i += 1) {
289
- if (!s.test(e[i - 1]))
287
+ const l = Math.max(1, n), i = Math.min(e.length, o);
288
+ for (let c = l; c <= i; c += 1) {
289
+ if (!s.test(e[c - 1]))
290
290
  continue;
291
- const u = Math.abs(i - t);
292
- if (u < r && (r = u, a = i), i > t && a >= 0)
291
+ const u = Math.abs(c - t);
292
+ if (u < r && (r = u, a = c), c > t && a >= 0)
293
293
  break;
294
294
  }
295
295
  return a;
296
296
  }, ne = (e, t, n, o) => {
297
297
  let s = -1, a = Number.POSITIVE_INFINITY;
298
298
  const r = Math.max(1, n), l = Math.min(e.length, o);
299
- for (let c = r; c <= l; c += 1) {
300
- if (e[c - 1] !== " ")
299
+ for (let i = r; i <= l; i += 1) {
300
+ if (e[i - 1] !== " ")
301
301
  continue;
302
- const i = Math.abs(c - t);
303
- if (i < a && (a = i, s = c), c > t && s >= 0)
302
+ const c = Math.abs(i - t);
303
+ if (c < a && (a = c, s = i), i > t && s >= 0)
304
304
  break;
305
305
  }
306
306
  return s;
@@ -322,15 +322,15 @@ const xe = [
322
322
  let o = e;
323
323
  for (; o && h(o) > t; ) {
324
324
  const s = h(o), a = Math.floor(s / 2);
325
- let r = 0, l = 0, c = 0;
325
+ let r = 0, l = 0, i = 0;
326
326
  for (let p = 0; p < o.length; p += 1)
327
- if (c += L.test(o[p]) ? 2 : 1, !r && c >= a && (r = p + 1), c >= t) {
327
+ if (i += R.test(o[p]) ? 2 : 1, !r && i >= a && (r = p + 1), i >= t) {
328
328
  l = p + 1;
329
329
  break;
330
330
  }
331
331
  l || (l = o.length), r || (r = Math.floor(o.length / 2));
332
- const i = Math.max(1, Math.floor(r / 3));
333
- let u = C(o, r, i, l, [
332
+ const c = Math.max(1, Math.floor(r / 3));
333
+ let u = C(o, r, c, l, [
334
334
  Y,
335
335
  J
336
336
  ]);
@@ -343,13 +343,13 @@ const xe = [
343
343
  const n = e[e.length - 1], o = e.reduce((p, T) => p + T.length, 0) / e.length, s = Math.floor(o * t);
344
344
  if (n.length >= s)
345
345
  return e;
346
- const r = `${e[e.length - 2]}${n}`, l = Math.floor(r.length / 2), c = Math.max(1, Math.floor(l / 3)), i = C(r, l, c, r.length, [
346
+ const r = `${e[e.length - 2]}${n}`, l = Math.floor(r.length / 2), i = Math.max(1, Math.floor(l / 3)), c = C(r, l, i, r.length, [
347
347
  Z
348
348
  ]);
349
- if (i <= 0 || i >= r.length)
349
+ if (c <= 0 || c >= r.length)
350
350
  return e;
351
351
  const u = [...e];
352
- return u[u.length - 2] = r.slice(0, i), u[u.length - 1] = r.slice(i), u.filter(Boolean);
352
+ return u[u.length - 2] = r.slice(0, c), u[u.length - 1] = r.slice(c), u.filter(Boolean);
353
353
  }, se = (e, t = {}) => {
354
354
  const n = {
355
355
  ...W,
@@ -362,19 +362,19 @@ const xe = [
362
362
  return [m(s)];
363
363
  const a = [], r = s.split(new RegExp("(?<=[。!?!?\\.])"));
364
364
  let l = "";
365
- for (const i of r) {
366
- if (!i)
365
+ for (const c of r) {
366
+ if (!c)
367
367
  continue;
368
- l += i;
368
+ l += c;
369
369
  const u = l[l.length - 1] ?? "";
370
370
  h(l) >= n.minSegmentLength && K.test(u) && (a.push(l), l = "");
371
371
  }
372
372
  l && (a.length > 0 && h(l) < n.minSegmentLength ? a[a.length - 1] += l : a.push(l));
373
- const c = a.flatMap((i) => h(i) <= n.maxSegmentLength ? [i] : oe(i, n.maxSegmentLength));
374
- return re(c, n.rebalanceRatio).map(
373
+ const i = a.flatMap((c) => h(c) <= n.maxSegmentLength ? [c] : oe(c, n.maxSegmentLength));
374
+ return re(i, n.rebalanceRatio).map(
375
375
  m
376
376
  );
377
- }, P = (e) => e.type === "text" ? e.text || "" : (e.content || []).map(P).join(""), le = (e) => {
377
+ }, A = (e) => e.type === "text" ? e.text || "" : (e.content || []).map(A).join(""), le = (e) => {
378
378
  const t = e.content || [];
379
379
  let n = "";
380
380
  const o = [], s = [], a = {
@@ -388,93 +388,93 @@ const xe = [
388
388
  actionCode: l
389
389
  });
390
390
  },
391
- [g.customStop.type]: ({ payload: r, position: l, actionCode: c }) => {
391
+ [g.customStop.type]: ({ payload: r, position: l, actionCode: i }) => {
392
392
  o.push({
393
393
  action: "insert",
394
394
  model: "control",
395
395
  payload: `[stop:${r == null ? void 0 : r.time}]`,
396
396
  position: l,
397
397
  text: "",
398
- actionCode: c
398
+ actionCode: i
399
399
  });
400
400
  },
401
- [g.continue.type]: ({ position: r, text: l, actionCode: c }) => {
401
+ [g.continue.type]: ({ position: r, text: l, actionCode: i }) => {
402
402
  o.push({
403
403
  action: "replace",
404
404
  model: "replace",
405
405
  payload: `《${l}》`,
406
406
  position: r,
407
407
  text: l,
408
- actionCode: c
408
+ actionCode: i
409
409
  });
410
410
  },
411
- [g.replace.type]: ({ payload: r, position: l, text: c, actionCode: i }) => {
411
+ [g.replace.type]: ({ payload: r, position: l, text: i, actionCode: c }) => {
412
412
  o.push({
413
413
  action: "replace",
414
414
  model: "replace",
415
415
  payload: `${r == null ? void 0 : r.text}`,
416
416
  position: l,
417
- text: c,
418
- actionCode: i
417
+ text: i,
418
+ actionCode: c
419
419
  });
420
420
  },
421
- [g.pinyin.type]: ({ payload: r, position: l, text: c, actionCode: i }) => {
421
+ [g.pinyin.type]: ({ payload: r, position: l, text: i, actionCode: c }) => {
422
422
  o.push({
423
423
  action: "replace",
424
424
  model: "pinyin",
425
425
  payload: `${r == null ? void 0 : r.text}`,
426
426
  position: l,
427
- text: c,
428
- actionCode: i
427
+ text: i,
428
+ actionCode: c
429
429
  });
430
430
  },
431
- [g.language.type]: ({ payload: r, position: l, text: c, actionCode: i }) => {
431
+ [g.language.type]: ({ payload: r, position: l, text: i, actionCode: c }) => {
432
432
  o.push({
433
433
  action: "language",
434
434
  model: "language",
435
435
  payload: `${r == null ? void 0 : r.code}`,
436
436
  position: l,
437
- text: c,
438
- actionCode: i
437
+ text: i,
438
+ actionCode: c
439
439
  });
440
440
  },
441
- [g.tone.type]: ({ payload: r, position: l, actionCode: c }) => {
441
+ [g.tone.type]: ({ payload: r, position: l, actionCode: i }) => {
442
442
  var u;
443
- const i = (u = S.find((p) => p.value === (r == null ? void 0 : r.code))) == null ? void 0 : u.tag;
444
- i && o.push({
443
+ const c = (u = P.find((p) => p.value === (r == null ? void 0 : r.code))) == null ? void 0 : u.tag;
444
+ c && o.push({
445
445
  action: "insert",
446
446
  model: "control",
447
- payload: i,
447
+ payload: c,
448
448
  position: l,
449
449
  text: "",
450
- actionCode: c
450
+ actionCode: i
451
451
  });
452
452
  },
453
- [g.ignore.type]: ({ position: r, text: l, actionCode: c }) => {
453
+ [g.ignore.type]: ({ position: r, text: l, actionCode: i }) => {
454
454
  o.push({
455
455
  action: "replace",
456
456
  model: "replace",
457
457
  payload: " ",
458
458
  position: r,
459
459
  text: l,
460
- actionCode: c
460
+ actionCode: i
461
461
  });
462
462
  }
463
463
  };
464
464
  return t.forEach((r) => {
465
- var T, E;
465
+ var T, d;
466
466
  if (r.type === "text") {
467
467
  n += r.text || "";
468
468
  return;
469
469
  }
470
470
  if (r.type !== "customTag") return;
471
- const l = (T = r.attrs) == null ? void 0 : T.categ, c = (E = r.attrs) == null ? void 0 : E.payload, i = n.length, u = P(r), p = a[l];
471
+ const l = (T = r.attrs) == null ? void 0 : T.categ, i = (d = r.attrs) == null ? void 0 : d.payload, c = n.length, u = A(r), p = a[l];
472
472
  u && s.push({
473
- start: i,
474
- end: i + u.length
473
+ start: c,
474
+ end: c + u.length
475
475
  }), p && p({
476
- payload: c,
477
- position: i,
476
+ payload: i,
477
+ position: c,
478
478
  text: u,
479
479
  actionCode: l
480
480
  }), n += u;
@@ -513,61 +513,65 @@ const xe = [
513
513
  return a.text += o.text, a.end = o.end, n;
514
514
  }
515
515
  return n.push({ ...o }), n;
516
- }, []), ue = (e, t, n, o) => e.position >= t && e.position < n ? !0 : !e.text && o && e.position === n, ge = (e) => {
517
- const t = se(e.text);
518
- if (t.length <= 1) return [e];
519
- const n = ce(
520
- ie(e.text, t),
516
+ }, []), ue = (e, t, n, o) => e.position >= t && e.position < n ? !0 : !e.text && o && e.position === n, ge = (e, t) => {
517
+ const n = se(e.text, t);
518
+ if (n.length <= 1) return [e];
519
+ const o = ce(
520
+ ie(e.text, n),
521
521
  e.protectedRanges || []
522
522
  );
523
- return n.map((o, s) => {
524
- const a = s === n.length - 1, r = e.playments.filter(
525
- (l) => ue(l, o.start, o.end, a)
526
- ).map((l) => ({
527
- ...l,
528
- position: l.position - o.start
523
+ return o.map((s, a) => {
524
+ const r = a === o.length - 1, l = e.playments.filter(
525
+ (i) => ue(i, s.start, s.end, r)
526
+ ).map((i) => ({
527
+ ...i,
528
+ position: i.position - s.start
529
529
  }));
530
530
  return {
531
- text: o.text,
532
- playments: r
531
+ text: s.text,
532
+ playments: l
533
533
  };
534
534
  });
535
- }, ye = (e) => {
536
- const t = {
535
+ }, Re = (e, t = {}) => {
536
+ const n = {
537
537
  playments: [],
538
538
  sentences: []
539
539
  };
540
- return ae(e).forEach((o) => {
541
- const s = le(o);
542
- !s.text && !s.playments.length || ge(s).forEach(({ text: a, playments: r }) => {
543
- t.sentences.push(a), t.playments.push(r);
544
- });
545
- }), t;
540
+ return ae(e).forEach((s) => {
541
+ const a = le(s);
542
+ !a.text && !a.playments.length || ge(a, t).forEach(
543
+ ({ text: r, playments: l }) => {
544
+ n.sentences.push(r), n.playments.push(l);
545
+ }
546
+ );
547
+ }), n;
546
548
  };
547
549
  export {
548
550
  f as DeleteMode,
549
- pe as EditorConst,
551
+ he as EditorConst,
550
552
  _ as LanguageOptions,
551
- xe as NumberToTextTools,
552
- v as StopOptions,
553
- S as ToneOptions,
553
+ fe as MAX_LANGUAGE_SELECTION_LENGTH,
554
+ pe as MAX_REPLACEMENT_TEXT_LENGTH,
555
+ Le as NumberToTextTools,
556
+ S as StopOptions,
557
+ P as ToneOptions,
554
558
  g as ToolConfig,
555
- x as allToolTypes,
556
- ye as convertToTtsFormat,
557
- be as estimateTtsDuration,
559
+ N as allToolTypes,
560
+ Re as convertToTtsFormat,
561
+ Ne as estimateTtsDuration,
558
562
  de as getDefaultLockPayload,
559
- me as getInitContent,
560
- Te as getLockPayloadLabel,
563
+ _e as getInitContent,
564
+ me as getLockPayloadLabel,
561
565
  h as getVisualLength,
562
- fe as isInsertTagType,
563
- he as isLockableTagType,
564
- Ee as resolveTtsEditorFeatures,
565
- Ne as sanitizePastedHtml,
566
+ Ee as isInsertTagType,
567
+ Te as isLockableTagType,
568
+ be as resolveTtsEditorFeatures,
569
+ ye as sanitizePastedHtml,
566
570
  q as sanitizePastedText,
567
571
  se as splitTextToSentences,
568
- U as toCardinal,
572
+ k as toCardinal,
569
573
  V as toColloquial,
570
- _e as toPhoneReading,
571
- y as toPositional,
574
+ xe as toPhoneReading,
575
+ L as toPositional,
572
576
  le as transformParagraph
573
577
  };