@naersen/tts-rich-text 0.11.0 → 0.12.1

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,48 +1,48 @@
1
- const f = {
1
+ const h = {
2
2
  DELETE_NODE: "deleteNode",
3
3
  CONVERT_TO_TEXT: "convertToText"
4
- }, pe = 256, fe = 256, g = {
4
+ }, $t = 256, Dt = 256, p = {
5
5
  stop: {
6
6
  label: "停顿",
7
7
  icon: "editor-stop",
8
8
  type: "stop",
9
- deleteMode: f.DELETE_NODE
9
+ deleteMode: h.DELETE_NODE
10
10
  },
11
11
  customStop: {
12
12
  label: "长停顿",
13
13
  icon: "editor-stop-long",
14
14
  type: "customStop",
15
- deleteMode: f.DELETE_NODE
15
+ deleteMode: h.DELETE_NODE
16
16
  },
17
17
  continue: {
18
18
  label: "连续",
19
19
  icon: "editor-continue",
20
20
  type: "continue",
21
- deleteMode: f.CONVERT_TO_TEXT
21
+ deleteMode: h.CONVERT_TO_TEXT
22
22
  },
23
23
  pinyin: {
24
24
  label: "拼音标注",
25
25
  icon: "editor-pinyin",
26
26
  type: "pinyin",
27
- deleteMode: f.CONVERT_TO_TEXT
27
+ deleteMode: h.CONVERT_TO_TEXT
28
28
  },
29
29
  replace: {
30
30
  label: "替换",
31
31
  icon: "editor-replace",
32
32
  type: "replace",
33
- deleteMode: f.CONVERT_TO_TEXT
33
+ deleteMode: h.CONVERT_TO_TEXT
34
34
  },
35
35
  language: {
36
36
  label: "语种标记",
37
37
  icon: "editor-language",
38
38
  type: "language",
39
- deleteMode: f.CONVERT_TO_TEXT
39
+ deleteMode: h.CONVERT_TO_TEXT
40
40
  },
41
41
  tone: {
42
42
  label: "语气",
43
43
  icon: "editor-tone",
44
44
  type: "tone",
45
- deleteMode: f.DELETE_NODE
45
+ deleteMode: h.DELETE_NODE
46
46
  },
47
47
  number: {
48
48
  label: "数字读法",
@@ -68,9 +68,9 @@ const f = {
68
68
  label: "忽略读音",
69
69
  icon: "editor-ingore",
70
70
  type: "ignore",
71
- deleteMode: f.CONVERT_TO_TEXT
71
+ deleteMode: h.CONVERT_TO_TEXT
72
72
  }
73
- }, P = [
73
+ }, j = [
74
74
  { label: "笑声", value: "笑声", tag: "[laughter]" },
75
75
  { label: "叹气", value: "叹气", tag: "[sigh]" },
76
76
  { label: "吸气", value: "吸气", tag: "[sniff]" },
@@ -85,21 +85,34 @@ const f = {
85
85
  { label: "惊讶(哇)", value: "惊讶(哇)", tag: "[surprise-wa]" },
86
86
  { label: "惊讶(哟)", value: "惊讶(哟)", tag: "[surprise-yo]" },
87
87
  { label: "不满", value: "不满", tag: "[dissatisfaction-hnn]" }
88
- ], _ = [
88
+ ], R = [
89
89
  { value: "zh", label: "中文" },
90
90
  { value: "en", label: "英语" },
91
- { value: "jp", label: "日语" },
92
- { value: "kr", label: "韩语" },
91
+ { value: "ja", label: "日语" },
92
+ { value: "ko", label: "韩语" },
93
93
  { value: "de", label: "德语" },
94
94
  { value: "fr", label: "法语" },
95
95
  { value: "es", label: "西班牙语" },
96
96
  { value: "it", label: "意大利语" },
97
97
  { value: "pt", label: "葡萄牙语" },
98
98
  { value: "ru", label: "俄语" }
99
- ], S = [
99
+ ], z = { jp: "ja", kr: "ko" }, _ = (t) => typeof t == "string" ? z[t] ?? t : t, G = (t) => {
100
+ var o, s;
101
+ const e = (o = t.attrs) == null ? void 0 : o.payload, n = t.type === "customTag" && ((s = t.attrs) == null ? void 0 : s.categ) === "language" && _(e == null ? void 0 : e.code) !== (e == null ? void 0 : e.code);
102
+ return {
103
+ ...t,
104
+ ...n && {
105
+ attrs: {
106
+ ...t.attrs,
107
+ payload: { ...e, code: _(e.code) }
108
+ }
109
+ },
110
+ ...t.content && { content: t.content.map(G) }
111
+ };
112
+ }, X = [
100
113
  { label: "停顿 0.5 秒", value: 0.5 },
101
114
  { label: "停顿 1 秒", value: 1 }
102
- ], he = {
115
+ ], Bt = {
103
116
  SENTENCE: "sentence",
104
117
  CUSTOM_TAG: "customTag",
105
118
  /**
@@ -107,16 +120,16 @@ const f = {
107
120
  * 工具栏与右键面板都要用它来主动收起气泡,避免两个浮层叠在一起。
108
121
  */
109
122
  BUBBLE_MENU_KEY: "ttsRichTextBubbleMenu"
110
- }, Ee = (e) => {
111
- var t;
112
- return ((t = g[e]) == null ? void 0 : t.deleteMode) === f.DELETE_NODE;
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
- languages: _,
123
+ }, kt = (t) => {
124
+ var e;
125
+ return ((e = p[t]) == null ? void 0 : e.deleteMode) === h.DELETE_NODE;
126
+ }, wt = (t) => t === p.stop.type || t === p.customStop.type, Ht = (t) => t === p.customStop.type ? { time: X[0].value } : {}, Vt = (t, e) => t === p.customStop.type ? `${e == null ? void 0 : e.time} 秒` : "", M = Object.keys(p), m = {
127
+ tools: M.filter((t) => t !== p.tone.type),
128
+ languages: R,
116
129
  maxPinyinLength: 4,
117
130
  maxNumberLength: 16,
118
131
  enablePreview: !0
119
- }, v = {
132
+ }, Y = {
120
133
  1: {
121
134
  tools: [
122
135
  "customStop",
@@ -129,20 +142,20 @@ const f = {
129
142
  "upload",
130
143
  "ignore"
131
144
  ],
132
- languages: _.filter((e) => ["zh", "en"].includes(e.value))
145
+ languages: R.filter((t) => ["zh", "en"].includes(t.value))
133
146
  },
134
- 2: E,
135
- 3: { tools: N.filter((e) => e !== g.language.type) }
136
- }, be = (e, t = {}) => {
137
- const n = v[e] ?? E;
147
+ 2: m,
148
+ 3: { tools: M.filter((t) => t !== p.language.type) }
149
+ }, Ut = (t, e = {}) => {
150
+ const n = Y[t] ?? m;
138
151
  return {
139
- ...E,
152
+ ...m,
140
153
  ...n,
141
- ...t,
142
- tools: t.tools ?? n.tools ?? E.tools,
143
- languages: t.languages ?? n.languages ?? E.languages
154
+ ...e,
155
+ tools: e.tools ?? n.tools ?? m.tools,
156
+ languages: e.languages ?? n.languages ?? m.languages
144
157
  };
145
- }, _e = () => ({
158
+ }, jt = () => ({
146
159
  type: "doc",
147
160
  content: [
148
161
  {
@@ -150,13 +163,13 @@ const f = {
150
163
  content: []
151
164
  }
152
165
  ]
153
- }), F = /[\u4e00-\u9fff]/, M = /\s/g, D = 0.28, H = 0.14, Ne = (e, t = 1) => {
154
- const n = Number.isFinite(t) && t > 0 ? t : 1;
155
- return Array.from(e.replace(M, "")).reduce(
156
- (s, a) => s + (F.test(a) ? D : H),
166
+ }), K = /[\u4e00-\u9fff]/, W = /\s/g, q = 0.28, J = 0.14, zt = (t, e = 1) => {
167
+ const n = Number.isFinite(e) && e > 0 ? e : 1;
168
+ return Array.from(t.replace(W, "")).reduce(
169
+ (s, a) => s + (K.test(a) ? q : J),
157
170
  0
158
171
  ) / n;
159
- }, x = {
172
+ }, O = {
160
173
  0: "零",
161
174
  1: "一",
162
175
  2: "二",
@@ -167,7 +180,7 @@ const f = {
167
180
  7: "七",
168
181
  8: "八",
169
182
  9: "九"
170
- }, w = {
183
+ }, Z = {
171
184
  0: "洞",
172
185
  1: "幺",
173
186
  2: "二",
@@ -178,49 +191,49 @@ const f = {
178
191
  7: "七",
179
192
  8: "八",
180
193
  9: "九"
181
- }, B = ["", "十", "百", "千"], U = ["", "万", "亿", "万亿"];
182
- function k(e) {
183
- return e ? e.split("").map((t) => x[t] || t).join("") : "";
194
+ }, Q = ["", "十", "百", "千"], tt = ["", "万", "亿", "万亿"];
195
+ function et(t) {
196
+ return t ? t.split("").map((e) => O[e] || e).join("") : "";
184
197
  }
185
- function xe(e) {
186
- return e ? e.split("").map((t) => w[t] || t).join("") : "";
198
+ function Gt(t) {
199
+ return t ? t.split("").map((e) => Z[e] || e).join("") : "";
187
200
  }
188
- function $(e) {
189
- const t = e.length;
201
+ function nt(t) {
202
+ const e = t.length;
190
203
  let n = "", o = !1;
191
- for (let s = 0; s < t; s++) {
192
- const a = e[s], r = t - 1 - s;
193
- a === "0" ? !o && n !== "" && (o = !0) : (o && (n += "零", o = !1), n += x[a] + B[r]);
204
+ for (let s = 0; s < e; s++) {
205
+ const a = t[s], r = e - 1 - s;
206
+ a === "0" ? !o && n !== "" && (o = !0) : (o && (n += "零", o = !1), n += O[a] + Q[r]);
194
207
  }
195
208
  return n.startsWith("一十") && n.length === 2 && (n = n.substring(1)), n;
196
209
  }
197
- function L(e) {
198
- if (!e) return "";
199
- if (e === "0" || (e = e.replace(/^0+/, "") || "0", e === "0")) return "零";
200
- let t = "";
201
- const n = e.length, o = [];
210
+ function P(t) {
211
+ if (!t) return "";
212
+ if (t === "0" || (t = t.replace(/^0+/, "") || "0", t === "0")) return "零";
213
+ let e = "";
214
+ const n = t.length, o = [];
202
215
  for (let s = n; s > 0; s -= 4) {
203
216
  const a = Math.max(0, s - 4);
204
- o.unshift(e.substring(a, s));
217
+ o.unshift(t.substring(a, s));
205
218
  }
206
219
  for (let s = 0; s < o.length; s++) {
207
- const a = o[s], r = $(a);
220
+ const a = o[s], r = nt(a);
208
221
  if (r !== "") {
209
- const l = o.length - 1 - s;
210
- t += r + U[l];
222
+ const i = o.length - 1 - s;
223
+ e += r + tt[i];
211
224
  }
212
225
  }
213
- return t;
226
+ return e;
214
227
  }
215
- function V(e) {
216
- return L(e);
228
+ function ot(t) {
229
+ return P(t);
217
230
  }
218
- const Le = [
219
- { label: "基数读法", func: k },
231
+ const Xt = [
232
+ { label: "基数读法", func: et },
220
233
  // { label: '位数读法', func: toPhoneReading },
221
- { label: "位数读法", func: L },
222
- { label: "口语读法", func: V }
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 = {
234
+ { label: "位数读法", func: P },
235
+ { label: "口语读法", func: ot }
236
+ ], rt = /[\u0000-\u0008\u000B-\u001F\u007F-\u009F\u00AD\u200B-\u200F\u202A-\u202E\u2060-\u206F\uFE00-\uFE0F\uFEFF\uFFFD\uE000-\uF8FF]/g, st = /[\t\f\v\u00A0\u1680\u2000-\u200A\u202F\u205F\u3000]/g, C = {
224
237
  "‘": "'",
225
238
  "’": "'",
226
239
  "‚": "'",
@@ -252,63 +265,63 @@ const Le = [
252
265
  "︔": ";",
253
266
  "︕": "!",
254
267
  "︖": "?"
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, `
268
+ }, at = new RegExp(`[${Object.keys(C).join("")}]`, "g"), it = (t) => t.replace(/[\uFF21-\uFF3A\uFF41-\uFF5A\uFF10-\uFF19]/g, (e) => String.fromCharCode(e.charCodeAt(0) - 65248)), lt = (t) => it(t).replace(/\r\n?/g, `
256
269
  `).replace(/[\u2028\u2029]/g, `
257
- `).replace(X, "").replace(G, " ").replace(j, (t) => y[t] || "").replace(/ *\n */g, `
270
+ `).replace(rt, "").replace(st, " ").replace(at, (e) => C[e] || "").replace(/ *\n */g, `
258
271
  `).replace(/\n{3,}/g, `
259
272
 
260
- `), ye = (e) => {
261
- if (typeof window > "u" || !e)
262
- return e;
263
- const n = new DOMParser().parseFromString(e, "text/html"), o = n.createTreeWalker(n.body, NodeFilter.SHOW_TEXT);
273
+ `), Yt = (t) => {
274
+ if (typeof window > "u" || !t)
275
+ return t;
276
+ const n = new DOMParser().parseFromString(t, "text/html"), o = n.createTreeWalker(n.body, NodeFilter.SHOW_TEXT);
264
277
  let s = o.nextNode();
265
278
  for (; s; )
266
- s.textContent = q(s.textContent || ""), s = o.nextNode();
279
+ s.textContent = lt(s.textContent || ""), s = o.nextNode();
267
280
  return n.body.innerHTML;
268
- }, W = {
281
+ }, ct = {
269
282
  minSegmentLength: 32,
270
283
  maxSegmentLength: 128,
271
284
  rebalanceRatio: 0.4
272
- }, R = /[一-鿿㐀-䶿豈-﫿 -〿＀-￯]/, K = /[。!?!?\.]/, Y = /[;;]/, J = /[,,、::]/, Z = /[。!?!?,,、;;::]/, I = "", O = "", h = (e) => {
273
- let t = 0;
274
- for (const n of e)
275
- t += R.test(n) ? 2 : 1;
276
- return t;
277
- }, Q = (e) => e.replace(/\r/g, "").split(`
278
- `).map((t) => t.trim()).filter(Boolean).join(" ").replace(/\s+/g, " ").trim(), ee = (e) => {
279
- let t = e, n = "";
280
- for (; n !== t; )
281
- n = t, t = t.replace(/(\d),(\d)/g, `$1${I}$2`);
282
- for (n = ""; n !== t; )
283
- n = t, t = t.replace(/(\d)\.(\d)/g, `$1${O}$2`);
284
- return t;
285
- }, m = (e) => e.split(I).join(",").split(O).join("."), te = (e, t, n, o, s) => {
285
+ }, y = /[一-鿿㐀-䶿豈-﫿 -〿＀-￯]/, ut = /[。!?!?\.]/, gt = /[;;]/, pt = /[,,、::]/, ht = /[。!?!?,,、;;::]/, A = "", v = "", d = (t) => {
286
+ let e = 0;
287
+ for (const n of t)
288
+ e += y.test(n) ? 2 : 1;
289
+ return e;
290
+ }, ft = (t) => t.replace(/\r/g, "").split(`
291
+ `).map((e) => e.trim()).filter(Boolean).join(" ").replace(/\s+/g, " ").trim(), Et = (t) => {
292
+ let e = t, n = "";
293
+ for (; n !== e; )
294
+ n = e, e = e.replace(/(\d),(\d)/g, `$1${A}$2`);
295
+ for (n = ""; n !== e; )
296
+ n = e, e = e.replace(/(\d)\.(\d)/g, `$1${v}$2`);
297
+ return e;
298
+ }, L = (t) => t.split(A).join(",").split(v).join("."), Tt = (t, e, n, o, s) => {
286
299
  let a = -1, r = Number.POSITIVE_INFINITY;
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]))
300
+ const i = Math.max(1, n), c = Math.min(t.length, o);
301
+ for (let l = i; l <= c; l += 1) {
302
+ if (!s.test(t[l - 1]))
290
303
  continue;
291
- const u = Math.abs(c - t);
292
- if (u < r && (r = u, a = c), c > t && a >= 0)
304
+ const u = Math.abs(l - e);
305
+ if (u < r && (r = u, a = l), l > e && a >= 0)
293
306
  break;
294
307
  }
295
308
  return a;
296
- }, ne = (e, t, n, o) => {
309
+ }, dt = (t, e, n, o) => {
297
310
  let s = -1, a = Number.POSITIVE_INFINITY;
298
- const r = Math.max(1, n), l = Math.min(e.length, o);
299
- for (let i = r; i <= l; i += 1) {
300
- if (e[i - 1] !== " ")
311
+ const r = Math.max(1, n), i = Math.min(t.length, o);
312
+ for (let c = r; c <= i; c += 1) {
313
+ if (t[c - 1] !== " ")
301
314
  continue;
302
- const c = Math.abs(i - t);
303
- if (c < a && (a = c, s = i), i > t && s >= 0)
315
+ const l = Math.abs(c - e);
316
+ if (l < a && (a = l, s = c), c > e && s >= 0)
304
317
  break;
305
318
  }
306
319
  return s;
307
- }, C = (e, t, n, o, s) => {
320
+ }, F = (t, e, n, o, s) => {
308
321
  for (const a of s) {
309
- const r = te(
310
- e,
322
+ const r = Tt(
311
323
  t,
324
+ e,
312
325
  n,
313
326
  o,
314
327
  a
@@ -316,262 +329,382 @@ const Le = [
316
329
  if (r >= 0)
317
330
  return r;
318
331
  }
319
- return ne(e, t, n, o);
320
- }, oe = (e, t) => {
332
+ return dt(t, e, n, o);
333
+ }, mt = (t, e) => {
321
334
  const n = [];
322
- let o = e;
323
- for (; o && h(o) > t; ) {
324
- const s = h(o), a = Math.floor(s / 2);
325
- let r = 0, l = 0, i = 0;
326
- for (let p = 0; p < o.length; p += 1)
327
- if (i += R.test(o[p]) ? 2 : 1, !r && i >= a && (r = p + 1), i >= t) {
328
- l = p + 1;
335
+ let o = t;
336
+ for (; o && d(o) > e; ) {
337
+ const s = d(o), a = Math.floor(s / 2);
338
+ let r = 0, i = 0, c = 0;
339
+ for (let g = 0; g < o.length; g += 1)
340
+ if (c += y.test(o[g]) ? 2 : 1, !r && c >= a && (r = g + 1), c >= e) {
341
+ i = g + 1;
329
342
  break;
330
343
  }
331
- l || (l = o.length), r || (r = Math.floor(o.length / 2));
332
- const c = Math.max(1, Math.floor(r / 3));
333
- let u = C(o, r, c, l, [
334
- Y,
335
- J
344
+ i || (i = o.length), r || (r = Math.floor(o.length / 2));
345
+ const l = Math.max(1, Math.floor(r / 3));
346
+ let u = F(o, r, l, i, [
347
+ gt,
348
+ pt
336
349
  ]);
337
350
  (u < 0 || u >= o.length) && (u = r), n.push(o.slice(0, u)), o = o.slice(u);
338
351
  }
339
352
  return o.trim() && n.push(o), n;
340
- }, re = (e, t) => {
341
- if (e.length <= 1)
342
- return e;
343
- const n = e[e.length - 1], o = e.reduce((p, T) => p + T.length, 0) / e.length, s = Math.floor(o * t);
353
+ }, bt = (t, e) => {
354
+ if (t.length <= 1)
355
+ return t;
356
+ const n = t[t.length - 1], o = t.reduce((g, E) => g + E.length, 0) / t.length, s = Math.floor(o * e);
344
357
  if (n.length >= s)
345
- return e;
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
- Z
358
+ return t;
359
+ const r = `${t[t.length - 2]}${n}`, i = Math.floor(r.length / 2), c = Math.max(1, Math.floor(i / 3)), l = F(r, i, c, r.length, [
360
+ ht
348
361
  ]);
349
- if (c <= 0 || c >= r.length)
350
- return e;
351
- const u = [...e];
352
- return u[u.length - 2] = r.slice(0, c), u[u.length - 1] = r.slice(c), u.filter(Boolean);
353
- }, se = (e, t = {}) => {
362
+ if (l <= 0 || l >= r.length)
363
+ return t;
364
+ const u = [...t];
365
+ return u[u.length - 2] = r.slice(0, l), u[u.length - 1] = r.slice(l), u.filter(Boolean);
366
+ }, _t = (t, e = {}) => {
354
367
  const n = {
355
- ...W,
356
- ...t
357
- }, o = Q(e);
368
+ ...ct,
369
+ ...e
370
+ }, o = ft(t);
358
371
  if (!o)
359
372
  return [];
360
- const s = ee(o);
361
- if (h(s) <= n.minSegmentLength)
362
- return [m(s)];
373
+ const s = Et(o);
374
+ if (d(s) <= n.minSegmentLength)
375
+ return [L(s)];
363
376
  const a = [], r = s.split(new RegExp("(?<=[。!?!?\\.])"));
364
- let l = "";
365
- for (const c of r) {
366
- if (!c)
377
+ let i = "";
378
+ for (const l of r) {
379
+ if (!l)
367
380
  continue;
368
- l += c;
369
- const u = l[l.length - 1] ?? "";
370
- h(l) >= n.minSegmentLength && K.test(u) && (a.push(l), l = "");
381
+ i += l;
382
+ const u = i[i.length - 1] ?? "";
383
+ d(i) >= n.minSegmentLength && ut.test(u) && (a.push(i), i = "");
371
384
  }
372
- l && (a.length > 0 && h(l) < n.minSegmentLength ? a[a.length - 1] += l : a.push(l));
373
- const i = a.flatMap((c) => h(c) <= n.maxSegmentLength ? [c] : oe(c, n.maxSegmentLength));
374
- return re(i, n.rebalanceRatio).map(
375
- m
385
+ i && (a.length > 0 && d(i) < n.minSegmentLength ? a[a.length - 1] += i : a.push(i));
386
+ const c = a.flatMap((l) => d(l) <= n.maxSegmentLength ? [l] : mt(l, n.maxSegmentLength));
387
+ return bt(c, n.rebalanceRatio).map(
388
+ L
376
389
  );
377
- }, A = (e) => e.type === "text" ? e.text || "" : (e.content || []).map(A).join(""), le = (e) => {
378
- const t = e.content || [];
390
+ }, T = {
391
+ minSegmentLength: 32,
392
+ maxSegmentLength: 64,
393
+ rebalanceRatio: 0.4
394
+ }, $ = /[一-鿿㐀-䶿豈-﫿 -〿＀-￯]/, D = /[。!?!?\.]/, B = /[;;]/, k = /[,,::]/, Nt = /[、]/, w = "", H = "", f = (t) => {
395
+ let e = 0;
396
+ for (const n of t)
397
+ e += $.test(n) ? 2 : 1;
398
+ return e;
399
+ }, Lt = (t) => t.replace(/\r/g, "").split(`
400
+ `).map((e) => e.trim()).filter(Boolean).join(" ").replace(/\s+/g, " ").trim(), It = (t) => {
401
+ let e = t, n = "";
402
+ for (; n !== e; )
403
+ n = e, e = e.replace(/(\d),(\d)/g, `$1${w}$2`);
404
+ for (n = ""; n !== e; )
405
+ n = e, e = e.replace(/(\d)\.(\d)/g, `$1${H}$2`);
406
+ return e;
407
+ }, I = (t) => t.split(w).join(",").split(H).join("."), N = (t, e) => {
408
+ let n = 0;
409
+ for (let o = 0; o < t.length; o += 1)
410
+ if (n += $.test(t[o]) ? 2 : 1, n >= e)
411
+ return o + 1;
412
+ return t.length;
413
+ }, S = (t, e, n, o, s) => {
414
+ let a = -1, r = Number.POSITIVE_INFINITY;
415
+ const i = Math.max(1, n), c = Math.min(t.length - 1, o);
416
+ for (let l = i; l <= c; l += 1) {
417
+ if (!s.test(t[l - 1]))
418
+ continue;
419
+ const u = Math.abs(l - e);
420
+ if (u < r && (r = u, a = l), l > e && a >= 0)
421
+ break;
422
+ }
423
+ return a;
424
+ }, St = (t, e, n, o) => {
425
+ let s = -1, a = Number.POSITIVE_INFINITY;
426
+ const r = Math.max(1, n), i = Math.min(t.length - 1, o);
427
+ for (let c = r; c <= i; c += 1) {
428
+ if (t[c - 1] !== " ")
429
+ continue;
430
+ const l = Math.abs(c - e);
431
+ if (l < a && (a = l, s = c), c > e && s >= 0)
432
+ break;
433
+ }
434
+ return s;
435
+ }, V = (t, e, n, o, s) => {
436
+ for (const r of s) {
437
+ const i = S(
438
+ t,
439
+ e,
440
+ n,
441
+ o,
442
+ r
443
+ );
444
+ if (i >= 0)
445
+ return i;
446
+ }
447
+ const a = St(t, e, n, o);
448
+ return a >= 0 ? a : S(
449
+ t,
450
+ e,
451
+ n,
452
+ o,
453
+ Nt
454
+ );
455
+ }, xt = (t, e) => {
456
+ const n = [];
457
+ let o = t.trim();
458
+ for (; o && f(o) > e; ) {
459
+ const s = f(o), a = Math.min(Math.floor(s / 2), e), r = N(o, a), i = Math.max(1, Math.floor(r / 3)), c = Math.max(
460
+ r + 1,
461
+ N(o, e)
462
+ );
463
+ let l = V(o, r, i, c, [
464
+ B,
465
+ k
466
+ ]);
467
+ (l < 0 || l >= o.length) && (l = r), n.push(o.slice(0, l).trim()), o = o.slice(l).trim();
468
+ }
469
+ return o && n.push(o), n;
470
+ }, Rt = (t, e) => {
471
+ if (t.length <= 1)
472
+ return t;
473
+ const n = t[t.length - 1], o = t.reduce((E, b) => E + f(b), 0) / t.length, s = Math.floor(o * e);
474
+ if (f(n) >= s)
475
+ return t;
476
+ const r = `${t[t.length - 2]}${n}`, i = Math.floor(f(r) / 2), c = N(r, i), l = Math.max(1, Math.floor(c / 3));
477
+ let u = V(r, c, l, r.length, [
478
+ D,
479
+ B,
480
+ k
481
+ ]);
482
+ if (u <= 0 || u >= r.length)
483
+ return t;
484
+ const g = [...t];
485
+ return g[g.length - 2] = r.slice(0, u).trim(), g[g.length - 1] = r.slice(u).trim(), g.filter(Boolean);
486
+ }, Mt = (t) => {
487
+ const e = Lt(t);
488
+ if (!e)
489
+ return [];
490
+ const n = It(e);
491
+ if (f(n) <= T.minSegmentLength)
492
+ return [I(n)];
493
+ const o = [], s = n.split(new RegExp("(?<=[。!?!?\\.])"));
494
+ let a = "";
495
+ for (const i of s) {
496
+ if (!i)
497
+ continue;
498
+ a += i;
499
+ const c = a[a.length - 1] ?? "";
500
+ f(a) >= T.minSegmentLength && D.test(c) && (o.push(a.trim()), a = "");
501
+ }
502
+ a && (o.length > 0 && f(a) < T.minSegmentLength ? o[o.length - 1] += a : o.push(a.trim()));
503
+ const r = o.flatMap((i) => f(i) <= T.maxSegmentLength ? [i] : xt(i, T.maxSegmentLength));
504
+ return Rt(
505
+ r,
506
+ T.rebalanceRatio
507
+ ).map(I);
508
+ }, Ot = (t, e = {}) => e.strategy === "legacy" ? Mt(t) : _t(t, e), U = (t) => t.type === "text" ? t.text || "" : (t.content || []).map(U).join(""), Pt = (t) => {
509
+ const e = t.content || [];
379
510
  let n = "";
380
511
  const o = [], s = [], a = {
381
- [g.stop.type]: ({ position: r, actionCode: l }) => {
512
+ [p.stop.type]: ({ position: r, actionCode: i }) => {
382
513
  o.push({
383
514
  action: "insert",
384
515
  model: "control",
385
516
  payload: "。 ",
386
517
  position: r,
387
518
  text: "",
388
- actionCode: l
519
+ actionCode: i
389
520
  });
390
521
  },
391
- [g.customStop.type]: ({ payload: r, position: l, actionCode: i }) => {
522
+ [p.customStop.type]: ({ payload: r, position: i, actionCode: c }) => {
392
523
  o.push({
393
524
  action: "insert",
394
525
  model: "control",
395
526
  payload: `[stop:${r == null ? void 0 : r.time}]`,
396
- position: l,
527
+ position: i,
397
528
  text: "",
398
- actionCode: i
529
+ actionCode: c
399
530
  });
400
531
  },
401
- [g.continue.type]: ({ position: r, text: l, actionCode: i }) => {
532
+ [p.continue.type]: ({ position: r, text: i, actionCode: c }) => {
402
533
  o.push({
403
534
  action: "replace",
404
535
  model: "replace",
405
- payload: `《${l}》`,
536
+ payload: `《${i}》`,
406
537
  position: r,
407
- text: l,
408
- actionCode: i
538
+ text: i,
539
+ actionCode: c
409
540
  });
410
541
  },
411
- [g.replace.type]: ({ payload: r, position: l, text: i, actionCode: c }) => {
542
+ [p.replace.type]: ({ payload: r, position: i, text: c, actionCode: l }) => {
412
543
  o.push({
413
544
  action: "replace",
414
545
  model: "replace",
415
546
  payload: `${r == null ? void 0 : r.text}`,
416
- position: l,
417
- text: i,
418
- actionCode: c
547
+ position: i,
548
+ text: c,
549
+ actionCode: l
419
550
  });
420
551
  },
421
- [g.pinyin.type]: ({ payload: r, position: l, text: i, actionCode: c }) => {
552
+ [p.pinyin.type]: ({ payload: r, position: i, text: c, actionCode: l }) => {
422
553
  o.push({
423
554
  action: "replace",
424
555
  model: "pinyin",
425
556
  payload: `${r == null ? void 0 : r.text}`,
426
- position: l,
427
- text: i,
428
- actionCode: c
557
+ position: i,
558
+ text: c,
559
+ actionCode: l
429
560
  });
430
561
  },
431
- [g.language.type]: ({ payload: r, position: l, text: i, actionCode: c }) => {
562
+ [p.language.type]: ({ payload: r, position: i, text: c, actionCode: l }) => {
432
563
  o.push({
433
564
  action: "language",
434
565
  model: "language",
435
- payload: `${r == null ? void 0 : r.code}`,
436
- position: l,
437
- text: i,
438
- actionCode: c
566
+ payload: `${_(r == null ? void 0 : r.code)}`,
567
+ position: i,
568
+ text: c,
569
+ actionCode: l
439
570
  });
440
571
  },
441
- [g.tone.type]: ({ payload: r, position: l, actionCode: i }) => {
572
+ [p.tone.type]: ({ payload: r, position: i, actionCode: c }) => {
442
573
  var u;
443
- const c = (u = P.find((p) => p.value === (r == null ? void 0 : r.code))) == null ? void 0 : u.tag;
444
- c && o.push({
574
+ const l = (u = j.find((g) => g.value === (r == null ? void 0 : r.code))) == null ? void 0 : u.tag;
575
+ l && o.push({
445
576
  action: "insert",
446
577
  model: "control",
447
- payload: c,
448
- position: l,
578
+ payload: l,
579
+ position: i,
449
580
  text: "",
450
- actionCode: i
581
+ actionCode: c
451
582
  });
452
583
  },
453
- [g.ignore.type]: ({ position: r, text: l, actionCode: i }) => {
584
+ [p.ignore.type]: ({ position: r, text: i, actionCode: c }) => {
454
585
  o.push({
455
586
  action: "replace",
456
587
  model: "replace",
457
588
  payload: " ",
458
589
  position: r,
459
- text: l,
460
- actionCode: i
590
+ text: i,
591
+ actionCode: c
461
592
  });
462
593
  }
463
594
  };
464
- return t.forEach((r) => {
465
- var T, d;
595
+ return e.forEach((r) => {
596
+ var E, b;
466
597
  if (r.type === "text") {
467
598
  n += r.text || "";
468
599
  return;
469
600
  }
470
601
  if (r.type !== "customTag") return;
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];
602
+ const i = (E = r.attrs) == null ? void 0 : E.categ, c = (b = r.attrs) == null ? void 0 : b.payload, l = n.length, u = U(r), g = a[i];
472
603
  u && s.push({
473
- start: c,
474
- end: c + u.length
475
- }), p && p({
476
- payload: i,
477
- position: c,
604
+ start: l,
605
+ end: l + u.length
606
+ }), g && g({
607
+ payload: c,
608
+ position: l,
478
609
  text: u,
479
- actionCode: l
610
+ actionCode: i
480
611
  }), n += u;
481
612
  }), {
482
613
  text: n,
483
614
  protectedRanges: s,
484
615
  playments: o
485
616
  };
486
- }, b = (e) => e.type === "text" || e.type === "customTag", ae = (e) => {
487
- const t = e.content || [];
488
- if (e.type === "paragraph") return [e];
489
- if (e.type === "doc")
490
- return t.filter((o) => o.type === "paragraph");
491
- const n = t.filter((o) => o.type === "paragraph");
492
- return n.length ? n : t.some(b) ? [
617
+ }, x = (t) => t.type === "text" || t.type === "customTag", Ct = (t) => {
618
+ const e = t.content || [];
619
+ if (t.type === "paragraph") return [t];
620
+ if (t.type === "doc")
621
+ return e.filter((o) => o.type === "paragraph");
622
+ const n = e.filter((o) => o.type === "paragraph");
623
+ return n.length ? n : e.some(x) ? [
493
624
  {
494
625
  type: "paragraph",
495
- content: t.filter(b)
626
+ content: e.filter(x)
496
627
  }
497
628
  ] : [];
498
- }, ie = (e, t) => {
629
+ }, yt = (t, e) => {
499
630
  let n = 0;
500
- return t.map((o) => {
501
- const s = e.indexOf(o, n), a = s >= 0 ? s : n, r = a + o.length;
631
+ return e.map((o) => {
632
+ const s = t.indexOf(o, n), a = s >= 0 ? s : n, r = a + o.length;
502
633
  return n = r, {
503
634
  text: o,
504
635
  start: a,
505
636
  end: r
506
637
  };
507
638
  });
508
- }, ce = (e, t) => e.reduce((n, o) => {
509
- if (t.some(
639
+ }, At = (t, e) => t.reduce((n, o) => {
640
+ if (e.some(
510
641
  (a) => o.start > a.start && o.start < a.end
511
642
  ) && n.length) {
512
643
  const a = n[n.length - 1];
513
644
  return a.text += o.text, a.end = o.end, n;
514
645
  }
515
646
  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, 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
- e.protectedRanges || []
647
+ }, []), vt = (t, e, n, o) => t.position >= e && t.position < n ? !0 : !t.text && o && t.position === n, Ft = (t, e) => {
648
+ const n = Ot(t.text, e);
649
+ if (n.length <= 1) return [t];
650
+ const o = At(
651
+ yt(t.text, n),
652
+ t.protectedRanges || []
522
653
  );
523
654
  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
655
+ const r = a === o.length - 1, i = t.playments.filter(
656
+ (c) => vt(c, s.start, s.end, r)
657
+ ).map((c) => ({
658
+ ...c,
659
+ position: c.position - s.start
529
660
  }));
530
661
  return {
531
662
  text: s.text,
532
- playments: l
663
+ playments: i
533
664
  };
534
665
  });
535
- }, Re = (e, t = {}) => {
666
+ }, Kt = (t, e = {}) => {
536
667
  const n = {
537
668
  playments: [],
538
669
  sentences: []
539
670
  };
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);
671
+ return Ct(t).forEach((s) => {
672
+ const a = Pt(s);
673
+ !a.text && !a.playments.length || Ft(a, e).forEach(
674
+ ({ text: r, playments: i }) => {
675
+ n.sentences.push(r), n.playments.push(i);
545
676
  }
546
677
  );
547
678
  }), n;
548
679
  };
549
680
  export {
550
- f as DeleteMode,
551
- he as EditorConst,
552
- _ as LanguageOptions,
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,
558
- g as ToolConfig,
559
- N as allToolTypes,
560
- Re as convertToTtsFormat,
561
- Ne as estimateTtsDuration,
562
- de as getDefaultLockPayload,
563
- _e as getInitContent,
564
- me as getLockPayloadLabel,
565
- h as getVisualLength,
566
- Ee as isInsertTagType,
567
- Te as isLockableTagType,
568
- be as resolveTtsEditorFeatures,
569
- ye as sanitizePastedHtml,
570
- q as sanitizePastedText,
571
- se as splitTextToSentences,
572
- k as toCardinal,
573
- V as toColloquial,
574
- xe as toPhoneReading,
575
- L as toPositional,
576
- le as transformParagraph
681
+ h as DeleteMode,
682
+ Bt as EditorConst,
683
+ R as LanguageOptions,
684
+ Dt as MAX_LANGUAGE_SELECTION_LENGTH,
685
+ $t as MAX_REPLACEMENT_TEXT_LENGTH,
686
+ Xt as NumberToTextTools,
687
+ X as StopOptions,
688
+ j as ToneOptions,
689
+ p as ToolConfig,
690
+ M as allToolTypes,
691
+ Kt as convertToTtsFormat,
692
+ zt as estimateTtsDuration,
693
+ Ht as getDefaultLockPayload,
694
+ jt as getInitContent,
695
+ Vt as getLockPayloadLabel,
696
+ d as getVisualLength,
697
+ kt as isInsertTagType,
698
+ wt as isLockableTagType,
699
+ _ as normalizeLanguageCode,
700
+ G as normalizeTtsContent,
701
+ Ut as resolveTtsEditorFeatures,
702
+ Yt as sanitizePastedHtml,
703
+ lt as sanitizePastedText,
704
+ Ot as splitTextToSentences,
705
+ et as toCardinal,
706
+ ot as toColloquial,
707
+ Gt as toPhoneReading,
708
+ P as toPositional,
709
+ Pt as transformParagraph
577
710
  };