@naersen/tts-rich-text 0.11.0 → 0.12.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/CHANGELOG.md CHANGED
@@ -2,6 +2,21 @@
2
2
 
3
3
  本项目遵循 [语义化版本](https://semver.org/lang/zh-CN/)。
4
4
 
5
+ ## 0.12.0 - 2026-11-19
6
+
7
+ ### 新增
8
+
9
+ - 分句配置新增 `strategy: 'default' | 'legacy'`。默认策略和旧版策略采用完全独立的算法实现,公开入口仅负责选择策略。
10
+ - `options.sentenceSplit: { strategy: 'legacy' }` 完整采用旧版算法,固定参数为 `32 / 64 / 0.4`,包括视觉长度切点、标点优先级、尾段平衡及分句边界空格处理,无需额外设置长度参数。
11
+ - 独立调用 `splitTextToSentences()`、`convertToTtsFormat()` 时同样支持选择策略。
12
+ - Demo 新增“分句策略”选择器,普通及组合布局可切换默认版和旧版,右侧 TTS 输出同步更新,正文保持不变。
13
+
14
+ ### 升级提示
15
+
16
+ - 未指定策略或指定 `'default'` 时,保持 0.11.0 的算法及参数配置方式。
17
+ - `'legacy'` 不支持参数覆盖,TypeScript 会拒绝与长度参数混用;JavaScript 调用方混入的参数也不会改变旧版固定行为。
18
+ - 旧版算法会删除分句边界的空格,64 为拆分阈值,并非严格输出上限。需要保留边界空格的项目应继续使用默认策略。
19
+
5
20
  ## 0.11.0 - 2026-11-16
6
21
 
7
22
  ### 新增
package/README.md CHANGED
@@ -195,6 +195,17 @@ const getPlaybackSpeed = () => selectedVoice.value.speed / 50
195
195
 
196
196
  ### 分句策略
197
197
 
198
+ `options.sentenceSplit.strategy` 支持两套独立算法:省略或传入 `'default'` 时使用默认策略,保持现有行为及长度参数配置;传入 `'legacy'` 时完整使用旧版算法,固定参数为 `32 / 64 / 0.4`,无需也不支持额外传入长度参数。
199
+
200
+ ```vue
201
+ <tts-rich-text-editor
202
+ v-model="documentValue"
203
+ :options="{ sentenceSplit: { strategy: 'legacy' } }"
204
+ />
205
+ ```
206
+
207
+ `TtsEditorRoot` 同样支持该配置;独立转换可调用 `convertToTtsFormat(documentValue, { strategy: 'legacy' })`。旧版保留自己的视觉长度切点、标点优先级、尾段平衡和分句边界 `trim()` 行为,因此可能删除边界空格,64 也不是严格输出上限。默认算法和旧版算法分别维护,入口仅负责策略选择。以下说明适用于默认策略。
208
+
198
209
  `convertToTtsFormat()` 使用 `splitTextToSentences()` 生成 `sentences`。默认按视觉长度计算,中文、全角字符计 2,其他字符计 1;句末标点分割的最小段宽度为 32,超长段拆分阈值为 128(约 64 个汉字)。短句累积达到 32 且以句末标点结尾时输出,不足 32 的尾部残段并入前一段。
199
210
 
200
211
  超长内容以段中点为目标,在目标字符位置的三分之一到视觉宽度达到 128 的位置之间寻找切点。优先选择分号,其次是同级的逗号、顿号、冒号,最后是空格;没有自然切点时在中点硬切。按参考文档,上界之前未达到视觉中点时回退到字符中点,因此 128 是拆分阈值和搜索上界,不保证所有输出段都不超过 128。
@@ -0,0 +1,7 @@
1
+ export interface SentenceSplitOptions {
2
+ minSegmentLength?: number;
3
+ maxSegmentLength?: number;
4
+ rebalanceRatio?: number;
5
+ }
6
+ export declare const getVisualLength: (text: string) => number;
7
+ export declare const splitTextToSentences: (text: string, options?: SentenceSplitOptions) => string[];
@@ -0,0 +1,2 @@
1
+ export declare const getVisualLength: (text: string) => number;
2
+ export declare const splitTextToSentences: (text: string) => string[];
@@ -1,7 +1,11 @@
1
- export interface SentenceSplitOptions {
2
- minSegmentLength?: number;
3
- maxSegmentLength?: number;
4
- rebalanceRatio?: number;
5
- }
6
- export declare const getVisualLength: (text: string) => number;
1
+ import { SentenceSplitOptions as DefaultSentenceSplitOptions } from './split-sentence-default';
2
+ export { getVisualLength } from './split-sentence-default';
3
+ export type SentenceSplitOptions = (DefaultSentenceSplitOptions & {
4
+ strategy?: 'default';
5
+ }) | {
6
+ strategy: 'legacy';
7
+ minSegmentLength?: never;
8
+ maxSegmentLength?: never;
9
+ rebalanceRatio?: never;
10
+ };
7
11
  export declare const splitTextToSentences: (text: string, options?: SentenceSplitOptions) => string[];
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
+ }, ye = 256, ve = 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
+ }, U = [
74
74
  { label: "笑声", value: "笑声", tag: "[laughter]" },
75
75
  { label: "叹气", value: "叹气", tag: "[sigh]" },
76
76
  { label: "吸气", value: "吸气", tag: "[sniff]" },
@@ -85,7 +85,7 @@ 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
+ ], x = [
89
89
  { value: "zh", label: "中文" },
90
90
  { value: "en", label: "英语" },
91
91
  { value: "jp", label: "日语" },
@@ -96,10 +96,10 @@ const f = {
96
96
  { value: "it", label: "意大利语" },
97
97
  { value: "pt", label: "葡萄牙语" },
98
98
  { value: "ru", label: "俄语" }
99
- ], S = [
99
+ ], j = [
100
100
  { label: "停顿 0.5 秒", value: 0.5 },
101
101
  { label: "停顿 1 秒", value: 1 }
102
- ], he = {
102
+ ], Fe = {
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
- }, Ee = (e) => {
110
+ }, $e = (e) => {
111
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: _,
112
+ return ((t = p[e]) == null ? void 0 : t.deleteMode) === h.DELETE_NODE;
113
+ }, De = (e) => e === p.stop.type || e === p.customStop.type, Be = (e) => e === p.customStop.type ? { time: j[0].value } : {}, we = (e, t) => e === p.customStop.type ? `${t == null ? void 0 : t.time} 秒` : "", R = Object.keys(p), m = {
114
+ tools: R.filter((e) => e !== p.tone.type),
115
+ languages: x,
116
116
  maxPinyinLength: 4,
117
117
  maxNumberLength: 16,
118
118
  enablePreview: !0
119
- }, v = {
119
+ }, z = {
120
120
  1: {
121
121
  tools: [
122
122
  "customStop",
@@ -129,20 +129,20 @@ const f = {
129
129
  "upload",
130
130
  "ignore"
131
131
  ],
132
- languages: _.filter((e) => ["zh", "en"].includes(e.value))
132
+ languages: x.filter((e) => ["zh", "en"].includes(e.value))
133
133
  },
134
- 2: E,
135
- 3: { tools: N.filter((e) => e !== g.language.type) }
136
- }, be = (e, t = {}) => {
137
- const n = v[e] ?? E;
134
+ 2: m,
135
+ 3: { tools: R.filter((e) => e !== p.language.type) }
136
+ }, He = (e, t = {}) => {
137
+ const n = z[e] ?? m;
138
138
  return {
139
- ...E,
139
+ ...m,
140
140
  ...n,
141
141
  ...t,
142
- tools: t.tools ?? n.tools ?? E.tools,
143
- languages: t.languages ?? n.languages ?? E.languages
142
+ tools: t.tools ?? n.tools ?? m.tools,
143
+ languages: t.languages ?? n.languages ?? m.languages
144
144
  };
145
- }, _e = () => ({
145
+ }, Ve = () => ({
146
146
  type: "doc",
147
147
  content: [
148
148
  {
@@ -150,13 +150,13 @@ const f = {
150
150
  content: []
151
151
  }
152
152
  ]
153
- }), F = /[\u4e00-\u9fff]/, M = /\s/g, D = 0.28, H = 0.14, Ne = (e, t = 1) => {
153
+ }), G = /[\u4e00-\u9fff]/, X = /\s/g, Y = 0.28, K = 0.14, ke = (e, t = 1) => {
154
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),
155
+ return Array.from(e.replace(X, "")).reduce(
156
+ (l, s) => l + (G.test(s) ? Y : K),
157
157
  0
158
158
  ) / n;
159
- }, x = {
159
+ }, M = {
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
- }, w = {
170
+ }, W = {
171
171
  0: "洞",
172
172
  1: "幺",
173
173
  2: "二",
@@ -178,49 +178,49 @@ const f = {
178
178
  7: "七",
179
179
  8: "八",
180
180
  9: "九"
181
- }, B = ["", "十", "百", "千"], U = ["", "万", "亿", "万亿"];
182
- function k(e) {
183
- return e ? e.split("").map((t) => x[t] || t).join("") : "";
181
+ }, q = ["", "十", "百", "千"], J = ["", "万", "亿", "万亿"];
182
+ function Z(e) {
183
+ return e ? e.split("").map((t) => M[t] || t).join("") : "";
184
184
  }
185
- function xe(e) {
186
- return e ? e.split("").map((t) => w[t] || t).join("") : "";
185
+ function Ue(e) {
186
+ return e ? e.split("").map((t) => W[t] || t).join("") : "";
187
187
  }
188
- function $(e) {
188
+ function Q(e) {
189
189
  const t = e.length;
190
190
  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]);
191
+ for (let l = 0; l < t; l++) {
192
+ const s = e[l], r = t - 1 - l;
193
+ s === "0" ? !o && n !== "" && (o = !0) : (o && (n += "零", o = !1), n += M[s] + q[r]);
194
194
  }
195
195
  return n.startsWith("一十") && n.length === 2 && (n = n.substring(1)), n;
196
196
  }
197
- function L(e) {
197
+ function O(e) {
198
198
  if (!e) return "";
199
199
  if (e === "0" || (e = e.replace(/^0+/, "") || "0", e === "0")) return "零";
200
200
  let t = "";
201
201
  const n = e.length, o = [];
202
- for (let s = n; s > 0; s -= 4) {
203
- const a = Math.max(0, s - 4);
204
- o.unshift(e.substring(a, s));
202
+ for (let l = n; l > 0; l -= 4) {
203
+ const s = Math.max(0, l - 4);
204
+ o.unshift(e.substring(s, l));
205
205
  }
206
- for (let s = 0; s < o.length; s++) {
207
- const a = o[s], r = $(a);
206
+ for (let l = 0; l < o.length; l++) {
207
+ const s = o[l], r = Q(s);
208
208
  if (r !== "") {
209
- const l = o.length - 1 - s;
210
- t += r + U[l];
209
+ const i = o.length - 1 - l;
210
+ t += r + J[i];
211
211
  }
212
212
  }
213
213
  return t;
214
214
  }
215
- function V(e) {
216
- return L(e);
215
+ function ee(e) {
216
+ return O(e);
217
217
  }
218
- const Le = [
219
- { label: "基数读法", func: k },
218
+ const je = [
219
+ { label: "基数读法", func: Z },
220
220
  // { 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 = {
221
+ { label: "位数读法", func: O },
222
+ { label: "口语读法", func: ee }
223
+ ], te = /[\u0000-\u0008\u000B-\u001F\u007F-\u009F\u00AD\u200B-\u200F\u202A-\u202E\u2060-\u206F\uFE00-\uFE0F\uFEFF\uFFFD\uE000-\uF8FF]/g, ne = /[\t\f\v\u00A0\u1680\u2000-\u200A\u202F\u205F\u3000]/g, P = {
224
224
  "‘": "'",
225
225
  "’": "'",
226
226
  "‚": "'",
@@ -252,326 +252,444 @@ const Le = [
252
252
  "︔": ";",
253
253
  "︕": "!",
254
254
  "︖": "?"
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, `
255
+ }, oe = new RegExp(`[${Object.keys(P).join("")}]`, "g"), re = (e) => e.replace(/[\uFF21-\uFF3A\uFF41-\uFF5A\uFF10-\uFF19]/g, (t) => String.fromCharCode(t.charCodeAt(0) - 65248)), se = (e) => re(e).replace(/\r\n?/g, `
256
256
  `).replace(/[\u2028\u2029]/g, `
257
- `).replace(X, "").replace(G, " ").replace(j, (t) => y[t] || "").replace(/ *\n */g, `
257
+ `).replace(te, "").replace(ne, " ").replace(oe, (t) => P[t] || "").replace(/ *\n */g, `
258
258
  `).replace(/\n{3,}/g, `
259
259
 
260
- `), ye = (e) => {
260
+ `), ze = (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);
264
- let s = o.nextNode();
265
- for (; s; )
266
- s.textContent = q(s.textContent || ""), s = o.nextNode();
264
+ let l = o.nextNode();
265
+ for (; l; )
266
+ l.textContent = se(l.textContent || ""), l = o.nextNode();
267
267
  return n.body.innerHTML;
268
- }, W = {
268
+ }, ie = {
269
269
  minSegmentLength: 32,
270
270
  maxSegmentLength: 128,
271
271
  rebalanceRatio: 0.4
272
- }, R = /[一-鿿㐀-䶿豈-﫿 -〿＀-￯]/, K = /[。!?!?\.]/, Y = /[;;]/, J = /[,,、::]/, Z = /[。!?!?,,、;;::]/, I = "", O = "", h = (e) => {
272
+ }, A = /[一-鿿㐀-䶿豈-﫿 -〿＀-￯]/, le = /[。!?!?\.]/, ae = /[;;]/, ce = /[,,、::]/, ue = /[。!?!?,,、;;::]/, C = "", y = "", T = (e) => {
273
273
  let t = 0;
274
274
  for (const n of e)
275
- t += R.test(n) ? 2 : 1;
275
+ t += A.test(n) ? 2 : 1;
276
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) => {
277
+ }, ge = (e) => e.replace(/\r/g, "").split(`
278
+ `).map((t) => t.trim()).filter(Boolean).join(" ").replace(/\s+/g, " ").trim(), pe = (e) => {
279
279
  let t = e, n = "";
280
280
  for (; n !== t; )
281
- n = t, t = t.replace(/(\d),(\d)/g, `$1${I}$2`);
281
+ n = t, t = t.replace(/(\d),(\d)/g, `$1${C}$2`);
282
282
  for (n = ""; n !== t; )
283
- n = t, t = t.replace(/(\d)\.(\d)/g, `$1${O}$2`);
283
+ n = t, t = t.replace(/(\d)\.(\d)/g, `$1${y}$2`);
284
284
  return t;
285
- }, m = (e) => e.split(I).join(",").split(O).join("."), te = (e, t, n, o, s) => {
286
- 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]))
285
+ }, N = (e) => e.split(C).join(",").split(y).join("."), he = (e, t, n, o, l) => {
286
+ let s = -1, r = Number.POSITIVE_INFINITY;
287
+ const i = Math.max(1, n), c = Math.min(e.length, o);
288
+ for (let a = i; a <= c; a += 1) {
289
+ if (!l.test(e[a - 1]))
290
290
  continue;
291
- const u = Math.abs(c - t);
292
- if (u < r && (r = u, a = c), c > t && a >= 0)
291
+ const u = Math.abs(a - t);
292
+ if (u < r && (r = u, s = a), a > t && s >= 0)
293
293
  break;
294
294
  }
295
- return a;
296
- }, ne = (e, t, n, o) => {
297
- 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] !== " ")
295
+ return s;
296
+ }, fe = (e, t, n, o) => {
297
+ let l = -1, s = Number.POSITIVE_INFINITY;
298
+ const r = Math.max(1, n), i = Math.min(e.length, o);
299
+ for (let c = r; c <= i; c += 1) {
300
+ if (e[c - 1] !== " ")
301
301
  continue;
302
- const c = Math.abs(i - t);
303
- if (c < a && (a = c, s = i), i > t && s >= 0)
302
+ const a = Math.abs(c - t);
303
+ if (a < s && (s = a, l = c), c > t && l >= 0)
304
304
  break;
305
305
  }
306
- return s;
307
- }, C = (e, t, n, o, s) => {
308
- for (const a of s) {
309
- const r = te(
306
+ return l;
307
+ }, v = (e, t, n, o, l) => {
308
+ for (const s of l) {
309
+ const r = he(
310
310
  e,
311
311
  t,
312
312
  n,
313
313
  o,
314
- a
314
+ s
315
315
  );
316
316
  if (r >= 0)
317
317
  return r;
318
318
  }
319
- return ne(e, t, n, o);
320
- }, oe = (e, t) => {
319
+ return fe(e, t, n, o);
320
+ }, Ee = (e, t) => {
321
321
  const n = [];
322
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;
323
+ for (; o && T(o) > t; ) {
324
+ const l = T(o), s = Math.floor(l / 2);
325
+ let r = 0, i = 0, c = 0;
326
+ for (let g = 0; g < o.length; g += 1)
327
+ if (c += A.test(o[g]) ? 2 : 1, !r && c >= s && (r = g + 1), c >= t) {
328
+ i = g + 1;
329
329
  break;
330
330
  }
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
331
+ i || (i = o.length), r || (r = Math.floor(o.length / 2));
332
+ const a = Math.max(1, Math.floor(r / 3));
333
+ let u = v(o, r, a, i, [
334
+ ae,
335
+ ce
336
336
  ]);
337
337
  (u < 0 || u >= o.length) && (u = r), n.push(o.slice(0, u)), o = o.slice(u);
338
338
  }
339
339
  return o.trim() && n.push(o), n;
340
- }, re = (e, t) => {
340
+ }, de = (e, t) => {
341
341
  if (e.length <= 1)
342
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);
344
- if (n.length >= s)
343
+ const n = e[e.length - 1], o = e.reduce((g, E) => g + E.length, 0) / e.length, l = Math.floor(o * t);
344
+ if (n.length >= l)
345
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
346
+ const r = `${e[e.length - 2]}${n}`, i = Math.floor(r.length / 2), c = Math.max(1, Math.floor(i / 3)), a = v(r, i, c, r.length, [
347
+ ue
348
348
  ]);
349
- if (c <= 0 || c >= r.length)
349
+ if (a <= 0 || a >= r.length)
350
350
  return e;
351
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 = {}) => {
352
+ return u[u.length - 2] = r.slice(0, a), u[u.length - 1] = r.slice(a), u.filter(Boolean);
353
+ }, Te = (e, t = {}) => {
354
354
  const n = {
355
- ...W,
355
+ ...ie,
356
356
  ...t
357
- }, o = Q(e);
357
+ }, o = ge(e);
358
358
  if (!o)
359
359
  return [];
360
- const s = ee(o);
361
- if (h(s) <= n.minSegmentLength)
362
- return [m(s)];
363
- const a = [], r = s.split(new RegExp("(?<=[。!?!?\\.])"));
364
- let l = "";
365
- for (const c of r) {
366
- if (!c)
360
+ const l = pe(o);
361
+ if (T(l) <= n.minSegmentLength)
362
+ return [N(l)];
363
+ const s = [], r = l.split(new RegExp("(?<=[。!?!?\\.])"));
364
+ let i = "";
365
+ for (const a of r) {
366
+ if (!a)
367
+ continue;
368
+ i += a;
369
+ const u = i[i.length - 1] ?? "";
370
+ T(i) >= n.minSegmentLength && le.test(u) && (s.push(i), i = "");
371
+ }
372
+ i && (s.length > 0 && T(i) < n.minSegmentLength ? s[s.length - 1] += i : s.push(i));
373
+ const c = s.flatMap((a) => T(a) <= n.maxSegmentLength ? [a] : Ee(a, n.maxSegmentLength));
374
+ return de(c, n.rebalanceRatio).map(
375
+ N
376
+ );
377
+ }, d = {
378
+ minSegmentLength: 32,
379
+ maxSegmentLength: 64,
380
+ rebalanceRatio: 0.4
381
+ }, F = /[一-鿿㐀-䶿豈-﫿 -〿＀-￯]/, $ = /[。!?!?\.]/, D = /[;;]/, B = /[,,::]/, me = /[、]/, w = "", H = "", f = (e) => {
382
+ let t = 0;
383
+ for (const n of e)
384
+ t += F.test(n) ? 2 : 1;
385
+ return t;
386
+ }, be = (e) => e.replace(/\r/g, "").split(`
387
+ `).map((t) => t.trim()).filter(Boolean).join(" ").replace(/\s+/g, " ").trim(), _e = (e) => {
388
+ let t = e, n = "";
389
+ for (; n !== t; )
390
+ n = t, t = t.replace(/(\d),(\d)/g, `$1${w}$2`);
391
+ for (n = ""; n !== t; )
392
+ n = t, t = t.replace(/(\d)\.(\d)/g, `$1${H}$2`);
393
+ return t;
394
+ }, L = (e) => e.split(w).join(",").split(H).join("."), _ = (e, t) => {
395
+ let n = 0;
396
+ for (let o = 0; o < e.length; o += 1)
397
+ if (n += F.test(e[o]) ? 2 : 1, n >= t)
398
+ return o + 1;
399
+ return e.length;
400
+ }, I = (e, t, n, o, l) => {
401
+ let s = -1, r = Number.POSITIVE_INFINITY;
402
+ const i = Math.max(1, n), c = Math.min(e.length - 1, o);
403
+ for (let a = i; a <= c; a += 1) {
404
+ if (!l.test(e[a - 1]))
405
+ continue;
406
+ const u = Math.abs(a - t);
407
+ if (u < r && (r = u, s = a), a > t && s >= 0)
408
+ break;
409
+ }
410
+ return s;
411
+ }, Ne = (e, t, n, o) => {
412
+ let l = -1, s = Number.POSITIVE_INFINITY;
413
+ const r = Math.max(1, n), i = Math.min(e.length - 1, o);
414
+ for (let c = r; c <= i; c += 1) {
415
+ if (e[c - 1] !== " ")
367
416
  continue;
368
- l += c;
369
- const u = l[l.length - 1] ?? "";
370
- h(l) >= n.minSegmentLength && K.test(u) && (a.push(l), l = "");
417
+ const a = Math.abs(c - t);
418
+ if (a < s && (s = a, l = c), c > t && l >= 0)
419
+ break;
371
420
  }
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
421
+ return l;
422
+ }, V = (e, t, n, o, l) => {
423
+ for (const r of l) {
424
+ const i = I(
425
+ e,
426
+ t,
427
+ n,
428
+ o,
429
+ r
430
+ );
431
+ if (i >= 0)
432
+ return i;
433
+ }
434
+ const s = Ne(e, t, n, o);
435
+ return s >= 0 ? s : I(
436
+ e,
437
+ t,
438
+ n,
439
+ o,
440
+ me
376
441
  );
377
- }, A = (e) => e.type === "text" ? e.text || "" : (e.content || []).map(A).join(""), le = (e) => {
442
+ }, Le = (e, t) => {
443
+ const n = [];
444
+ let o = e.trim();
445
+ for (; o && f(o) > t; ) {
446
+ const l = f(o), s = Math.min(Math.floor(l / 2), t), r = _(o, s), i = Math.max(1, Math.floor(r / 3)), c = Math.max(
447
+ r + 1,
448
+ _(o, t)
449
+ );
450
+ let a = V(o, r, i, c, [
451
+ D,
452
+ B
453
+ ]);
454
+ (a < 0 || a >= o.length) && (a = r), n.push(o.slice(0, a).trim()), o = o.slice(a).trim();
455
+ }
456
+ return o && n.push(o), n;
457
+ }, Ie = (e, t) => {
458
+ if (e.length <= 1)
459
+ return e;
460
+ const n = e[e.length - 1], o = e.reduce((E, b) => E + f(b), 0) / e.length, l = Math.floor(o * t);
461
+ if (f(n) >= l)
462
+ return e;
463
+ const r = `${e[e.length - 2]}${n}`, i = Math.floor(f(r) / 2), c = _(r, i), a = Math.max(1, Math.floor(c / 3));
464
+ let u = V(r, c, a, r.length, [
465
+ $,
466
+ D,
467
+ B
468
+ ]);
469
+ if (u <= 0 || u >= r.length)
470
+ return e;
471
+ const g = [...e];
472
+ return g[g.length - 2] = r.slice(0, u).trim(), g[g.length - 1] = r.slice(u).trim(), g.filter(Boolean);
473
+ }, Se = (e) => {
474
+ const t = be(e);
475
+ if (!t)
476
+ return [];
477
+ const n = _e(t);
478
+ if (f(n) <= d.minSegmentLength)
479
+ return [L(n)];
480
+ const o = [], l = n.split(new RegExp("(?<=[。!?!?\\.])"));
481
+ let s = "";
482
+ for (const i of l) {
483
+ if (!i)
484
+ continue;
485
+ s += i;
486
+ const c = s[s.length - 1] ?? "";
487
+ f(s) >= d.minSegmentLength && $.test(c) && (o.push(s.trim()), s = "");
488
+ }
489
+ s && (o.length > 0 && f(s) < d.minSegmentLength ? o[o.length - 1] += s : o.push(s.trim()));
490
+ const r = o.flatMap((i) => f(i) <= d.maxSegmentLength ? [i] : Le(i, d.maxSegmentLength));
491
+ return Ie(
492
+ r,
493
+ d.rebalanceRatio
494
+ ).map(L);
495
+ }, xe = (e, t = {}) => t.strategy === "legacy" ? Se(e) : Te(e, t), k = (e) => e.type === "text" ? e.text || "" : (e.content || []).map(k).join(""), Re = (e) => {
378
496
  const t = e.content || [];
379
497
  let n = "";
380
- const o = [], s = [], a = {
381
- [g.stop.type]: ({ position: r, actionCode: l }) => {
498
+ const o = [], l = [], s = {
499
+ [p.stop.type]: ({ position: r, actionCode: i }) => {
382
500
  o.push({
383
501
  action: "insert",
384
502
  model: "control",
385
503
  payload: "。 ",
386
504
  position: r,
387
505
  text: "",
388
- actionCode: l
506
+ actionCode: i
389
507
  });
390
508
  },
391
- [g.customStop.type]: ({ payload: r, position: l, actionCode: i }) => {
509
+ [p.customStop.type]: ({ payload: r, position: i, actionCode: c }) => {
392
510
  o.push({
393
511
  action: "insert",
394
512
  model: "control",
395
513
  payload: `[stop:${r == null ? void 0 : r.time}]`,
396
- position: l,
514
+ position: i,
397
515
  text: "",
398
- actionCode: i
516
+ actionCode: c
399
517
  });
400
518
  },
401
- [g.continue.type]: ({ position: r, text: l, actionCode: i }) => {
519
+ [p.continue.type]: ({ position: r, text: i, actionCode: c }) => {
402
520
  o.push({
403
521
  action: "replace",
404
522
  model: "replace",
405
- payload: `《${l}》`,
523
+ payload: `《${i}》`,
406
524
  position: r,
407
- text: l,
408
- actionCode: i
525
+ text: i,
526
+ actionCode: c
409
527
  });
410
528
  },
411
- [g.replace.type]: ({ payload: r, position: l, text: i, actionCode: c }) => {
529
+ [p.replace.type]: ({ payload: r, position: i, text: c, actionCode: a }) => {
412
530
  o.push({
413
531
  action: "replace",
414
532
  model: "replace",
415
533
  payload: `${r == null ? void 0 : r.text}`,
416
- position: l,
417
- text: i,
418
- actionCode: c
534
+ position: i,
535
+ text: c,
536
+ actionCode: a
419
537
  });
420
538
  },
421
- [g.pinyin.type]: ({ payload: r, position: l, text: i, actionCode: c }) => {
539
+ [p.pinyin.type]: ({ payload: r, position: i, text: c, actionCode: a }) => {
422
540
  o.push({
423
541
  action: "replace",
424
542
  model: "pinyin",
425
543
  payload: `${r == null ? void 0 : r.text}`,
426
- position: l,
427
- text: i,
428
- actionCode: c
544
+ position: i,
545
+ text: c,
546
+ actionCode: a
429
547
  });
430
548
  },
431
- [g.language.type]: ({ payload: r, position: l, text: i, actionCode: c }) => {
549
+ [p.language.type]: ({ payload: r, position: i, text: c, actionCode: a }) => {
432
550
  o.push({
433
551
  action: "language",
434
552
  model: "language",
435
553
  payload: `${r == null ? void 0 : r.code}`,
436
- position: l,
437
- text: i,
438
- actionCode: c
554
+ position: i,
555
+ text: c,
556
+ actionCode: a
439
557
  });
440
558
  },
441
- [g.tone.type]: ({ payload: r, position: l, actionCode: i }) => {
559
+ [p.tone.type]: ({ payload: r, position: i, actionCode: c }) => {
442
560
  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({
561
+ const a = (u = U.find((g) => g.value === (r == null ? void 0 : r.code))) == null ? void 0 : u.tag;
562
+ a && o.push({
445
563
  action: "insert",
446
564
  model: "control",
447
- payload: c,
448
- position: l,
565
+ payload: a,
566
+ position: i,
449
567
  text: "",
450
- actionCode: i
568
+ actionCode: c
451
569
  });
452
570
  },
453
- [g.ignore.type]: ({ position: r, text: l, actionCode: i }) => {
571
+ [p.ignore.type]: ({ position: r, text: i, actionCode: c }) => {
454
572
  o.push({
455
573
  action: "replace",
456
574
  model: "replace",
457
575
  payload: " ",
458
576
  position: r,
459
- text: l,
460
- actionCode: i
577
+ text: i,
578
+ actionCode: c
461
579
  });
462
580
  }
463
581
  };
464
582
  return t.forEach((r) => {
465
- var T, d;
583
+ var E, b;
466
584
  if (r.type === "text") {
467
585
  n += r.text || "";
468
586
  return;
469
587
  }
470
588
  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];
472
- u && s.push({
473
- start: c,
474
- end: c + u.length
475
- }), p && p({
476
- payload: i,
477
- position: c,
589
+ const i = (E = r.attrs) == null ? void 0 : E.categ, c = (b = r.attrs) == null ? void 0 : b.payload, a = n.length, u = k(r), g = s[i];
590
+ u && l.push({
591
+ start: a,
592
+ end: a + u.length
593
+ }), g && g({
594
+ payload: c,
595
+ position: a,
478
596
  text: u,
479
- actionCode: l
597
+ actionCode: i
480
598
  }), n += u;
481
599
  }), {
482
600
  text: n,
483
- protectedRanges: s,
601
+ protectedRanges: l,
484
602
  playments: o
485
603
  };
486
- }, b = (e) => e.type === "text" || e.type === "customTag", ae = (e) => {
604
+ }, S = (e) => e.type === "text" || e.type === "customTag", Me = (e) => {
487
605
  const t = e.content || [];
488
606
  if (e.type === "paragraph") return [e];
489
607
  if (e.type === "doc")
490
608
  return t.filter((o) => o.type === "paragraph");
491
609
  const n = t.filter((o) => o.type === "paragraph");
492
- return n.length ? n : t.some(b) ? [
610
+ return n.length ? n : t.some(S) ? [
493
611
  {
494
612
  type: "paragraph",
495
- content: t.filter(b)
613
+ content: t.filter(S)
496
614
  }
497
615
  ] : [];
498
- }, ie = (e, t) => {
616
+ }, Oe = (e, t) => {
499
617
  let n = 0;
500
618
  return t.map((o) => {
501
- const s = e.indexOf(o, n), a = s >= 0 ? s : n, r = a + o.length;
619
+ const l = e.indexOf(o, n), s = l >= 0 ? l : n, r = s + o.length;
502
620
  return n = r, {
503
621
  text: o,
504
- start: a,
622
+ start: s,
505
623
  end: r
506
624
  };
507
625
  });
508
- }, ce = (e, t) => e.reduce((n, o) => {
626
+ }, Pe = (e, t) => e.reduce((n, o) => {
509
627
  if (t.some(
510
- (a) => o.start > a.start && o.start < a.end
628
+ (s) => o.start > s.start && o.start < s.end
511
629
  ) && n.length) {
512
- const a = n[n.length - 1];
513
- return a.text += o.text, a.end = o.end, n;
630
+ const s = n[n.length - 1];
631
+ return s.text += o.text, s.end = o.end, n;
514
632
  }
515
633
  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);
634
+ }, []), Ae = (e, t, n, o) => e.position >= t && e.position < n ? !0 : !e.text && o && e.position === n, Ce = (e, t) => {
635
+ const n = xe(e.text, t);
518
636
  if (n.length <= 1) return [e];
519
- const o = ce(
520
- ie(e.text, n),
637
+ const o = Pe(
638
+ Oe(e.text, n),
521
639
  e.protectedRanges || []
522
640
  );
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
641
+ return o.map((l, s) => {
642
+ const r = s === o.length - 1, i = e.playments.filter(
643
+ (c) => Ae(c, l.start, l.end, r)
644
+ ).map((c) => ({
645
+ ...c,
646
+ position: c.position - l.start
529
647
  }));
530
648
  return {
531
- text: s.text,
532
- playments: l
649
+ text: l.text,
650
+ playments: i
533
651
  };
534
652
  });
535
- }, Re = (e, t = {}) => {
653
+ }, Ge = (e, t = {}) => {
536
654
  const n = {
537
655
  playments: [],
538
656
  sentences: []
539
657
  };
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);
658
+ return Me(e).forEach((l) => {
659
+ const s = Re(l);
660
+ !s.text && !s.playments.length || Ce(s, t).forEach(
661
+ ({ text: r, playments: i }) => {
662
+ n.sentences.push(r), n.playments.push(i);
545
663
  }
546
664
  );
547
665
  }), n;
548
666
  };
549
667
  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
668
+ h as DeleteMode,
669
+ Fe as EditorConst,
670
+ x as LanguageOptions,
671
+ ve as MAX_LANGUAGE_SELECTION_LENGTH,
672
+ ye as MAX_REPLACEMENT_TEXT_LENGTH,
673
+ je as NumberToTextTools,
674
+ j as StopOptions,
675
+ U as ToneOptions,
676
+ p as ToolConfig,
677
+ R as allToolTypes,
678
+ Ge as convertToTtsFormat,
679
+ ke as estimateTtsDuration,
680
+ Be as getDefaultLockPayload,
681
+ Ve as getInitContent,
682
+ we as getLockPayloadLabel,
683
+ T as getVisualLength,
684
+ $e as isInsertTagType,
685
+ De as isLockableTagType,
686
+ He as resolveTtsEditorFeatures,
687
+ ze as sanitizePastedHtml,
688
+ se as sanitizePastedText,
689
+ xe as splitTextToSentences,
690
+ Z as toCardinal,
691
+ ee as toColloquial,
692
+ Ue as toPhoneReading,
693
+ O as toPositional,
694
+ Re as transformParagraph
577
695
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naersen/tts-rich-text",
3
- "version": "0.11.0",
3
+ "version": "0.12.0",
4
4
  "description": "Reusable Vue 3 rich-text editor for TTS workflows",
5
5
  "type": "module",
6
6
  "files": [