1
This commit is contained in:
@@ -173,33 +173,30 @@
|
||||
}, 300);
|
||||
});
|
||||
// 英文截断
|
||||
// 1. 替换 为特殊空白占位符
|
||||
function convertNbspToSpace() {
|
||||
$('.ql-editor').contents().each(function() {
|
||||
if (this.nodeType === 3) { // 文本节点
|
||||
const $span = $('<span>').addClass('space-replaced');
|
||||
$(this).replaceWith(
|
||||
$span.html(this.nodeValue.replace(/ /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
|
||||
function processContent() {
|
||||
$('#rendered-content').html(function(i, html) {
|
||||
// 处理空行
|
||||
html = html.replace(/(<(p|h[1-6]|div)[^>]*>)\s*( )*\s*(<\/\2>)/g, function(match, p1, p2, p3, p4) {
|
||||
return p1 + ' ' + p4;
|
||||
});
|
||||
|
||||
// 替换其他 为普通空格,但保留数字后的 (如版本号)
|
||||
html = html.replace(/([^0-9]) /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>
|
||||
{/block}
|
||||
Reference in New Issue
Block a user