This commit is contained in:
2025-07-17 12:00:46 +08:00
parent 5e9ace5832
commit 5f5c7d7cf4
2 changed files with 38 additions and 55 deletions

View File

@@ -173,54 +173,33 @@
}, 300); }, 300);
}); });
// 英文截断 // 英文截断
// 1. 替换所有 为普通空格(保留连续空格) // 1. 替换 为特殊空白占位符
$('.ql-editor').html(function(_, html) { function convertNbspToSpace() {
return html.replace(/ /g, ' '); $('.ql-editor').contents().each(function() {
}); if (this.nodeType === 3) { // 文本节点
const $span = $('<span>').addClass('space-replaced');
// 2. 处理特殊换行情况 $(this).replaceWith(
function applySmartWrapping() { $span.html(this.nodeValue.replace(/&nbsp;/g, '<span class="space-char"> </span>'))
$('.ql-editor').find('*').contents().filter(function() { );
return this.nodeType === 3 && this.nodeValue.trim().length > 0; }
}).replaceWith(function() {
return $('<span>').addClass('wrapped-text').text(this.nodeValue);
});
$('.wrapped-text').each(function() {
const $this = $(this);
let text = $this.text();
// 处理超长连续字符串20字符以上
text = text.replace(/([^\s]{20,})/g, function(match) {
return match.replace(/(.{10})/g, '$1&#8203;');
}); });
}
// 保护特定术语不换行 // 2. 初始化处理
const protectedTerms = ['launch parameters', 'specific files']; convertNbspToSpace();
protectedTerms.forEach(term => {
text = text.replace(new RegExp(term, 'gi'), term.replace(/ /g, '&nbsp;')); // 3. 监听动态内容变化
if (typeof MutationObserver !== 'undefined') {
const observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
$(mutation.addedNodes).find('.ql-editor').each(convertNbspToSpace);
}); });
$this.html(text);
});
}
// 初始处理
applySmartWrapping();
// 3. 监听动态内容变化
if (typeof MutationObserver !== 'undefined') {
const observer = new MutationObserver(function(mutations) {
mutations.forEach(function() {
applySmartWrapping();
}); });
}); observer.observe($('.ql-container')[0], {
observer.observe($('.ql-editor')[0], {
childList: true, childList: true,
subtree: true, subtree: true
characterData: true });
}); }
}
}); });
</script> </script>
{/block} {/block}

View File

@@ -144,7 +144,21 @@ body {
margin: 0; margin: 0;
padding: 0 padding: 0
} }
/* 空白占位符样式 */
.space-char {
display: inline;
white-space: pre; /* 保留空白 */
position: relative;
padding-right: 0.15em; /* 视觉间距补偿 */
}
.space-char:after {
content: " ";
visibility: hidden;
}
.space-replaced {
display: inline;
white-space: normal;
}
.ql-editor { .ql-editor {
box-sizing: border-box; box-sizing: border-box;
line-height: 1.42; line-height: 1.42;
@@ -157,19 +171,9 @@ body {
text-align: left; text-align: left;
word-wrap: break-word; word-wrap: break-word;
overflow-wrap: anywhere; overflow-wrap: anywhere;
word-break: normal;
white-space: normal !important; white-space: normal !important;
}
}
.word-wrapper {
display: inline-block;
max-width: 100%;
}
/* 保护重要术语不换行 */
.protected-term {
white-space: nowrap;
display: inline-block;
}
.ql-editor>* { .ql-editor>* {
cursor: text; cursor: text;
word-break: normal; word-break: normal;