@applemusic-like-lyrics/core 0.4.1 → 0.4.2
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/amll-core.cjs +47 -21
- package/dist/amll-core.cjs.map +1 -1
- package/dist/amll-core.mjs +47 -21
- package/dist/amll-core.mjs.map +1 -1
- package/dist/style.css +6 -2
- package/package.json +1 -1
package/dist/amll-core.mjs
CHANGED
|
@@ -3126,6 +3126,13 @@ const NORMAL_BREAK_PENALTY_RATIO = .5;
|
|
|
3126
3126
|
*/
|
|
3127
3127
|
const SPACE_BREAK_REWARD_RATIO = .4;
|
|
3128
3128
|
/**
|
|
3129
|
+
* 在标点符号处断开的奖励比例
|
|
3130
|
+
*
|
|
3131
|
+
* 比空格更高以便优先一点在标点处换行
|
|
3132
|
+
*/
|
|
3133
|
+
const PUNCTUATION_BREAK_REWARD_RATIO = .6;
|
|
3134
|
+
const PUNCTUATION_REGEX = /[,.;:!?,。;:!?、)】》」』’”)[\]}>~…]$/;
|
|
3135
|
+
/**
|
|
3129
3136
|
* 计算平均行长度的断点位置
|
|
3130
3137
|
* @param children 子节点信息
|
|
3131
3138
|
* @param containerWidth 容器可用内容宽度
|
|
@@ -3166,9 +3173,13 @@ function calcBalancedBreaks(children, containerWidth, fullText, segmenter) {
|
|
|
3166
3173
|
else continue;
|
|
3167
3174
|
else lineCost = (containerWidth - w) ** 2;
|
|
3168
3175
|
let breakPenalty = 0;
|
|
3169
|
-
if (j < n)
|
|
3170
|
-
|
|
3171
|
-
|
|
3176
|
+
if (j < n) {
|
|
3177
|
+
const prevChild = children[j - 1];
|
|
3178
|
+
if (PUNCTUATION_REGEX.test(prevChild.text)) breakPenalty = -((containerWidth * PUNCTUATION_BREAK_REWARD_RATIO) ** 2);
|
|
3179
|
+
else if (prevChild.isSpace) breakPenalty = -((containerWidth * SPACE_BREAK_REWARD_RATIO) ** 2);
|
|
3180
|
+
else if (cjkBoundaries.has(charOffsets[j])) breakPenalty = PENALTY_CJK;
|
|
3181
|
+
else breakPenalty = PENALTY_NORMAL;
|
|
3182
|
+
}
|
|
3172
3183
|
const totalCost = lineCost + breakPenalty + dp[j];
|
|
3173
3184
|
if (totalCost < dp[i]) {
|
|
3174
3185
|
dp[i] = totalCost;
|
|
@@ -3193,13 +3204,9 @@ function getMeasurementContext() {
|
|
|
3193
3204
|
/**
|
|
3194
3205
|
* 用于平衡歌词行在换行后的各行长度
|
|
3195
3206
|
*/
|
|
3196
|
-
var LineBalancer = class
|
|
3207
|
+
var LineBalancer = class {
|
|
3197
3208
|
isBalancing = false;
|
|
3198
3209
|
lastBalancedContainerWidth = -1;
|
|
3199
|
-
/**
|
|
3200
|
-
* 防止误差导致的意外换行
|
|
3201
|
-
*/
|
|
3202
|
-
static SAFE_WIDTH_PADDING = 25;
|
|
3203
3210
|
constructor(mainElement) {
|
|
3204
3211
|
this.mainElement = mainElement;
|
|
3205
3212
|
}
|
|
@@ -3226,32 +3233,49 @@ var LineBalancer = class LineBalancer {
|
|
|
3226
3233
|
adapter.resetDOM();
|
|
3227
3234
|
const prevWhiteSpace = this.mainElement.style.whiteSpace;
|
|
3228
3235
|
this.mainElement.style.whiteSpace = "nowrap";
|
|
3236
|
+
const parentElement = this.mainElement.parentElement;
|
|
3237
|
+
let prevTransform = "";
|
|
3238
|
+
let transformChanged = false;
|
|
3239
|
+
if (parentElement) {
|
|
3240
|
+
prevTransform = parentElement.style.transform;
|
|
3241
|
+
if (prevTransform && prevTransform !== "none") {
|
|
3242
|
+
parentElement.style.transform = "none";
|
|
3243
|
+
transformChanged = true;
|
|
3244
|
+
}
|
|
3245
|
+
}
|
|
3246
|
+
let lockAcquired = false;
|
|
3229
3247
|
try {
|
|
3230
|
-
const
|
|
3231
|
-
|
|
3232
|
-
|
|
3233
|
-
|
|
3234
|
-
|
|
3248
|
+
const { childInfos, fullText } = adapter.buildChildInfos();
|
|
3249
|
+
let layoutWidth = childInfos.reduce((sum, c) => sum + c.width, 0);
|
|
3250
|
+
if (adapter.needsCalibration) {
|
|
3251
|
+
const range = document.createRange();
|
|
3252
|
+
range.selectNodeContents(this.mainElement);
|
|
3253
|
+
const visualWidth = range.getBoundingClientRect().width;
|
|
3254
|
+
if (layoutWidth > 0 && visualWidth > 0) {
|
|
3255
|
+
const scale = visualWidth / layoutWidth;
|
|
3256
|
+
for (const info of childInfos) info.width *= scale;
|
|
3257
|
+
}
|
|
3258
|
+
layoutWidth = visualWidth;
|
|
3259
|
+
}
|
|
3260
|
+
const safeContainerWidth = Math.max(1, containerWidth);
|
|
3261
|
+
if (layoutWidth <= safeContainerWidth) {
|
|
3235
3262
|
this.lastBalancedContainerWidth = containerWidth;
|
|
3236
3263
|
return;
|
|
3237
3264
|
}
|
|
3238
|
-
const { childInfos, fullText } = adapter.buildChildInfos();
|
|
3239
|
-
const measuredTotal = childInfos.reduce((sum, c) => sum + c.width, 0);
|
|
3240
|
-
if (measuredTotal > 0 && lineWidth > 0) {
|
|
3241
|
-
const scale = lineWidth / measuredTotal;
|
|
3242
|
-
for (const info of childInfos) info.width *= scale;
|
|
3243
|
-
}
|
|
3244
3265
|
const breaks = calcBalancedBreaks(childInfos, safeContainerWidth, fullText, wordSegmenter);
|
|
3245
3266
|
if (breaks.length === 0) {
|
|
3246
3267
|
this.lastBalancedContainerWidth = containerWidth;
|
|
3247
3268
|
return;
|
|
3248
3269
|
}
|
|
3249
3270
|
this.isBalancing = true;
|
|
3271
|
+
lockAcquired = true;
|
|
3250
3272
|
adapter.applyBreaks(breaks, childInfos);
|
|
3251
3273
|
this.lastBalancedContainerWidth = containerWidth;
|
|
3252
3274
|
this.isBalancing = false;
|
|
3253
3275
|
} finally {
|
|
3254
3276
|
this.mainElement.style.whiteSpace = prevWhiteSpace;
|
|
3277
|
+
if (transformChanged && parentElement) parentElement.style.transform = prevTransform;
|
|
3278
|
+
if (lockAcquired) this.isBalancing = false;
|
|
3255
3279
|
}
|
|
3256
3280
|
}
|
|
3257
3281
|
balanceDynamicLineBreaks(containerWidth, wordSegmenter) {
|
|
@@ -3300,7 +3324,8 @@ var LineBalancer = class LineBalancer {
|
|
|
3300
3324
|
const breakIndex = breaks[i];
|
|
3301
3325
|
if (breakIndex >= 0 && breakIndex < infoToNode.length) this.mainElement.insertBefore(document.createElement("br"), infoToNode[breakIndex]);
|
|
3302
3326
|
}
|
|
3303
|
-
}
|
|
3327
|
+
},
|
|
3328
|
+
needsCalibration: false
|
|
3304
3329
|
}, wordSegmenter);
|
|
3305
3330
|
}
|
|
3306
3331
|
balanceNonDynamicLineBreaks(containerWidth, computedStyle, wordSegmenter) {
|
|
@@ -3343,7 +3368,8 @@ var LineBalancer = class LineBalancer {
|
|
|
3343
3368
|
fragment.appendChild(document.createTextNode(childInfos[i].text));
|
|
3344
3369
|
}
|
|
3345
3370
|
this.mainElement.appendChild(fragment);
|
|
3346
|
-
}
|
|
3371
|
+
},
|
|
3372
|
+
needsCalibration: true
|
|
3347
3373
|
}, wordSegmenter);
|
|
3348
3374
|
}
|
|
3349
3375
|
};
|