This commit is contained in:
2025-07-17 12:00:46 +08:00
parent 3c7bdd8ac9
commit 07692a2a29
2 changed files with 38 additions and 55 deletions

View File

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