Skip to content

Commit d17a3ae

Browse files
committed
Refactor handleNode to improve line break detection logic
- Removed the inline checkBreakLineHappened function and exported forceBreakLineTags and forceBreakLineTagsUpper for better modularity. - Introduced a new import for checkBreakLineHappened to enhance code organization and maintainability.
1 parent 61292fc commit d17a3ae

File tree

2 files changed

+51
-49
lines changed

2 files changed

+51
-49
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { forceBreakLineTagsUpper, forceBreakLineTags } from './handleNode';
2+
3+
export const checkBreakLineHappened = (node: HTMLElement) => {
4+
const computedStyle = window.getComputedStyle(node);
5+
if (computedStyle.display === 'none' || computedStyle.visibility === 'hidden' || computedStyle.opacity === '0') {
6+
return false;
7+
}
8+
9+
if (forceBreakLineTagsUpper.includes(node.nodeName) || forceBreakLineTags.includes(node.nodeName)) {
10+
return true;
11+
}
12+
13+
// 获取文本内容
14+
const textContent = node.textContent?.trim();
15+
if (!textContent) return false;
16+
17+
// 方法1: 检查 white-space 属性
18+
if (computedStyle.whiteSpace === 'nowrap') {
19+
return false; // 强制不换行,所以没有折行
20+
}
21+
22+
// 方法2: 检查元素高度是否大于行高
23+
const lineHeight = parseFloat(computedStyle.lineHeight) || parseFloat(computedStyle.fontSize) * 1.2;
24+
const elementHeight = node.offsetHeight;
25+
26+
// 如果元素高度明显大于行高,说明发生了折行
27+
if (elementHeight > lineHeight * 1.5) {
28+
return true;
29+
}
30+
31+
// 方法3: 检查是否有换行符
32+
if (textContent.includes('\n') || textContent.includes('\r')) {
33+
return true;
34+
}
35+
36+
// 方法4: 检查文本长度和容器宽度的比例
37+
const elementWidth = node.offsetWidth;
38+
const fontSize = parseFloat(computedStyle.fontSize);
39+
const estimatedCharWidth = fontSize * 0.6; // 估算每个字符的平均宽度
40+
const estimatedTextWidth = textContent.length * estimatedCharWidth;
41+
42+
// 如果估算的文本宽度明显大于容器宽度,很可能发生折行
43+
if (estimatedTextWidth > elementWidth * 1.2) {
44+
return true;
45+
}
46+
47+
return false;
48+
};

pages/content/src/handleNode.ts

Lines changed: 3 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { checkBreakLineHappened } from './checkBreakLineHappened';
12
import { getPriority } from './getPriority';
23
import { state } from './state';
34
import { ignoreHref, isChinese, isUrl, queryWS, formatTranslation } from '@extension/shared';
@@ -11,55 +12,8 @@ const checkingTypeUpper = checkingTypeLower.map(item => item.toUpperCase());
1112
const checkingTypes = checkingTypeLower.concat(checkingTypeUpper);
1213
let tick = 0;
1314

14-
const forceBreakLineTags = ['ul', 'ol', 'li'];
15-
const forceBreakLineTagsUpper = forceBreakLineTags.map(item => item.toUpperCase());
16-
17-
const checkBreakLineHappened = (node: HTMLElement) => {
18-
const computedStyle = window.getComputedStyle(node);
19-
if (computedStyle.display === 'none' || computedStyle.visibility === 'hidden' || computedStyle.opacity === '0') {
20-
return false;
21-
}
22-
23-
if (forceBreakLineTagsUpper.includes(node.nodeName) || forceBreakLineTags.includes(node.nodeName)) {
24-
return true;
25-
}
26-
27-
// 获取文本内容
28-
const textContent = node.textContent?.trim();
29-
if (!textContent) return false;
30-
31-
// 方法1: 检查 white-space 属性
32-
if (computedStyle.whiteSpace === 'nowrap') {
33-
return false; // 强制不换行,所以没有折行
34-
}
35-
36-
// 方法2: 检查元素高度是否大于行高
37-
const lineHeight = parseFloat(computedStyle.lineHeight) || parseFloat(computedStyle.fontSize) * 1.2;
38-
const elementHeight = node.offsetHeight;
39-
40-
// 如果元素高度明显大于行高,说明发生了折行
41-
if (elementHeight > lineHeight * 1.5) {
42-
return true;
43-
}
44-
45-
// 方法3: 检查是否有换行符
46-
if (textContent.includes('\n') || textContent.includes('\r')) {
47-
return true;
48-
}
49-
50-
// 方法4: 检查文本长度和容器宽度的比例
51-
const elementWidth = node.offsetWidth;
52-
const fontSize = parseFloat(computedStyle.fontSize);
53-
const estimatedCharWidth = fontSize * 0.6; // 估算每个字符的平均宽度
54-
const estimatedTextWidth = textContent.length * estimatedCharWidth;
55-
56-
// 如果估算的文本宽度明显大于容器宽度,很可能发生折行
57-
if (estimatedTextWidth > elementWidth * 1.2) {
58-
return true;
59-
}
60-
61-
return false;
62-
};
15+
export const forceBreakLineTags = ['ul', 'ol', 'li'];
16+
export const forceBreakLineTagsUpper = forceBreakLineTags.map(item => item.toUpperCase());
6317

6418
export const handleNode = (_node: Node): boolean => {
6519
const currentUrl = window.location.href;

0 commit comments

Comments
 (0)