gpt4 book ai didi

javascript - 使用 javascript 查找呈现的换行符

转载 作者:行者123 更新时间:2023-11-30 07:49:28 25 4
gpt4 key购买 nike

我有这种情况:

div { width: 200px }
<div> example example example example example</div>

文本在填充 <div> 的整个宽度时自动跳转到下一行.

使用 javascript 我怎样才能在上面一行中得到呈现的内容?

注意:字符串中没有换行符

上述片段的预期结果:

"example example example"对应第 1 行和 "example example"对应第2行

最佳答案

您可以使用 Range API及其方便 getBoundingClientRect()方法来确定哪个字符标记 TextNode 中的扣押。

请注意,每次调整窗口大小时/布局发生变化时,这显然需要重新计算。

function getLineBreaks(node) {
// we only deal with TextNodes
if(!node || !node.parentNode || node.nodeType !== 3)
return [];
// our Range object form which we'll get the characters positions
const range = document.createRange();
// here we'll store all our lines
const lines = [];
// begin at the first char
range.setStart(node, 0);
// initial position
let prevBottom = range.getBoundingClientRect().bottom;
let str = node.textContent;
let current = 1; // we already got index 0
let lastFound = 0;
let bottom = 0;
// iterate over all characters
while(current <= str.length) {
// move our cursor
range.setStart(node, current);
if(current < str.length -1)
range.setEnd(node, current+1);
bottom = range.getBoundingClientRect().bottom;
if(bottom > prevBottom) { // line break
lines.push(
str.substr(lastFound , current - lastFound) // text content
);
prevBottom = bottom;
lastFound = current;
}
current++;
}
// push the last line
lines.push(str.substr(lastFound));

return lines;
}

console.log(getLineBreaks(document.querySelector('.test').childNodes[0]));
div.test {
width: 50px;
margin-bottom: 100px;
word-break: break-all;
}

body>.as-console-wrapper{max-height:100px}
<div class="test">This is some quite long content that will wrap in multiple lines</div>

如果您需要每行的相对 y 位置:

function getLineBreaks(node) {
// we only deal with TextNodes
if(!node || !node.parentNode || node.nodeType !== 3)
return [];
// our Range object form which we'll get the characters positions
const range = document.createRange();
// here we'll store all our lines
const lines = [];
// begin at the first character
range.setStart(node, 0);
// get the position of the parent node so we can have relative positions later
let contTop = node.parentNode.getBoundingClientRect().top;
// initial position
let prevBottom = range.getBoundingClientRect().bottom;
let str = node.textContent;
let current = 1; // we already got index 0
let lastFound = 0;
let bottom = 0;
// iterate over all characters
while(current <= str.length) {
// move our cursor
range.setStart(node, current);
if(current < str.length - 1)
range.setEnd(node, current+1); // wrap it (for Chrome...)
bottom = range.getBoundingClientRect().bottom;
if(bottom > prevBottom) { // line break
lines.push({
y: prevBottom - (contTop || 0), // relative bottom
text: str.substr(lastFound , current - lastFound) // text content
});
prevBottom = bottom;
lastFound = current;
}
current++;
}
// push the last line
lines.push({
y: bottom - (contTop || 0),
text: str.substr(lastFound)
});

return lines;
}

console.log(getLineBreaks(document.querySelector('.test').childNodes[0]));
div.test {
width: 50px;
margin-bottom: 100px;
}

body>.as-console-wrapper{max-height:100px}
<div class="test">This is some quite long content that will wrap in multiple lines</div>

对于那些需要它来处理元素而不是单个文本节点的人,这里是一个重写,它很可能会失败(例如,使用 RTL 方向)但应该没问题大多数情况。

function getLineBreaks(elem) {
// our Range object form which we'll get the characters positions
const range = document.createRange();
// here we'll store all our lines
const lines = [];
const nodes = grabTextNodes(elem);
let left = 0;
// get the position of the parent node so we can have relative positions later
let contTop = nodes[0].parentNode.getBoundingClientRect().top;
// initial position
let prevLeft = null;
let lineText = "";
let startRange = null;
for (const node of nodes) {
let nodeText = node.textContent;
const textLength = nodeText.length;
let rangeIndex = 0;
let textIndex = 0;
while (rangeIndex <= textLength) {
range.setStart(node, rangeIndex);
if (rangeIndex < textLength - 1) {
range.setEnd(node, rangeIndex + 1); // wrap the range (for Chrome...)
}
left = range.getBoundingClientRect().right;
if (prevLeft === null) { // first pass
prevLeft = left;
startRange = range.cloneRange();
} else if (left < prevLeft) { // line break
// store the current line content
lineText += nodeText.slice(0, textIndex);
startRange.setEnd(range.endContainer, range.endOffset);
const {
bottom
} = startRange.getBoundingClientRect();
lines.push({
y: bottom - contTop,
text: lineText
});
// start a new line
prevLeft = left;
lineText = "";
nodeText = nodeText.slice(textIndex);
textIndex = 0;
startRange = range.cloneRange();
}
rangeIndex++;
textIndex++;
prevLeft = left;
}
// add the remaining text from this node into the current line content
lineText += nodeText;
}
// push the last line
startRange.setEnd(range.endContainer, range.endOffset);
const { bottom } = startRange.getBoundingClientRect();
lines.push({
y: bottom - contTop,
text: lineText
});
return lines;
}

console.log(getLineBreaks(document.querySelector('.test')));

function grabTextNodes(elem) {
const walker = document.createTreeWalker(elem, NodeFilter.SHOW_TEXT, null);
const nodes = [];
while (walker.nextNode()) {
nodes.push(walker.currentNode);
}
return nodes;
}
div.test {
width: 150px;
margin-bottom: 100px;
}

.red {
color: red;
}
<div class="test"><span class="red">This</span> is some quite long content that will wrap in <span class="red">mutiple</span> lines..</div>

关于javascript - 使用 javascript 查找呈现的换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55604798/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com