update at 2026-02-08 18:28:39
This commit is contained in:
36
miniprogram/utils/core/text-layout.js
Normal file
36
miniprogram/utils/core/text-layout.js
Normal file
@@ -0,0 +1,36 @@
|
||||
const MAX_CHARS_PER_LINE = 45
|
||||
|
||||
function normalizeLineBreaks(text) {
|
||||
return String(text || '').replace(/\r\n?/g, '\n')
|
||||
}
|
||||
|
||||
function wrapTextByChars(text, maxCharsPerLine = MAX_CHARS_PER_LINE) {
|
||||
if (maxCharsPerLine <= 0) {
|
||||
return normalizeLineBreaks(text)
|
||||
}
|
||||
|
||||
const normalized = normalizeLineBreaks(text)
|
||||
const lines = normalized.split('\n')
|
||||
const wrappedLines = []
|
||||
|
||||
for (const line of lines) {
|
||||
const chars = Array.from(line)
|
||||
|
||||
if (!chars.length) {
|
||||
wrappedLines.push('')
|
||||
continue
|
||||
}
|
||||
|
||||
for (let i = 0; i < chars.length; i += maxCharsPerLine) {
|
||||
wrappedLines.push(chars.slice(i, i + maxCharsPerLine).join(''))
|
||||
}
|
||||
}
|
||||
|
||||
return wrappedLines.join('\n')
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
MAX_CHARS_PER_LINE,
|
||||
normalizeLineBreaks,
|
||||
wrapTextByChars,
|
||||
}
|
||||
Reference in New Issue
Block a user