This commit is contained in:
2025-07-17 15:08:52 +08:00
parent 5a85fbed53
commit d362cdfbd0
2 changed files with 29 additions and 31 deletions

View File

@@ -173,33 +173,30 @@
}, 300); }, 300);
}); });
// 英文截断 // 英文截断
// 1. 替换 为特殊空白占位符 function processContent() {
function convertNbspToSpace() { $('#rendered-content').html(function(i, html) {
$('.ql-editor').contents().each(function() { // 处理空行
if (this.nodeType === 3) { // 文本节点 html = html.replace(/(<(p|h[1-6]|div)[^>]*>)\s*(&nbsp;)*\s*(<\/\2>)/g, function(match, p1, p2, p3, p4) {
const $span = $('<span>').addClass('space-replaced'); return p1 + '&nbsp;' + p4;
$(this).replaceWith(
$span.html(this.nodeValue.replace(/&nbsp;/g, '<span class="space-char"> </span>'))
);
}
});
}
// 2. 初始化处理
convertNbspToSpace();
// 3. 监听动态内容变化
if (typeof MutationObserver !== 'undefined') {
const observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
$(mutation.addedNodes).find('.ql-editor').each(convertNbspToSpace);
});
});
observer.observe($('.ql-container')[0], {
childList: true,
subtree: true
}); });
// 替换其他&nbsp;为普通空格,但保留数字后的&nbsp;(如版本号)
html = html.replace(/([^0-9])&nbsp;/g, '$1 ');
// 处理英文单词间的多个空格
html = html.replace(/([a-zA-Z])\s+([a-zA-Z])/g, '$1 $2');
return html;
});
// 添加CSS类而不是直接内联样式
$('#rendered-content').addClass('word-wrap-optimized');
} }
// 初始处理
processContent();
// 如果内容是异步加载的可以在加载完成后调用processContent()
}); });
</script> </script>
{/block} {/block}

View File

@@ -170,8 +170,9 @@ body {
-moz-tab-size: 4; -moz-tab-size: 4;
text-align: left; text-align: left;
word-wrap: break-word; word-wrap: break-word;
overflow-wrap: anywhere; overflow-wrap: break-word;
white-space: normal !important; word-break: normal;
white-space: normal;
} }
.ql-editor>* { .ql-editor>* {
@@ -184,10 +185,10 @@ body {
margin: 0; margin: 0;
padding: 0; padding: 0;
counter-reset: list-1 list-2 list-3 list-4 list-5 list-6 list-7 list-8 list-9; counter-reset: list-1 list-2 list-3 list-4 list-5 list-6 list-7 list-8 list-9;
word-break: normal; word-wrap: break-word;
overflow-wrap: anywhere; /* 更智能的换行方式 */ overflow-wrap: break-word;
display: inline-block; /* 创建换行上下文 */ word-break: keep-all;
max-width: 100%; white-space: normal;
} }
/* 移动端优化 */ /* 移动端优化 */
@media (max-width: 768px) { @media (max-width: 768px) {