gpt4 book ai didi

javascript - 在javascript中单击文本时如何获取字符位置

转载 作者:数据小太阳 更新时间:2023-10-29 04:38:02 26 4
gpt4 key购买 nike

我有这个函数可以在你点击文本时获取光标的位置,它只适用于等宽字符,这很好,但它显然不适用于更宽的字符,比如中文或日文。

    function get_char_pos(point) {
var prompt_len = self.find('.prompt').text().length;
var size = get_char_size();
var width = size.width;
var height = size.height;
var offset = self.offset();
var col = Math.floor((point.x - offset.left) / width);
var row = Math.floor((point.y - offset.top) / height);
var lines = get_splited_command_line(command);
var try_pos;
if (row > 0 && lines.length > 1) {
try_pos = col + lines.slice(0, row).reduce(function(sum, line) {
return sum + line.length;
}, 0);
} else {
try_pos = col - prompt_len;
}
// tabs are 4 spaces and newline don't show up in results
var text = command.replace(/\t/g, '\x00\x00\x00\x00').replace(/\n/, '');
var before = text.slice(0, try_pos);
var len = before.replace(/\x00{4}/g, '\t').replace(/\x00+/, '').length;
return len > command.length ? command.length : len;
}

我尝试使用 wcwidth 库创建一个函数(对于更宽的字符返回 2,对于普通字母返回 1)但是它不能正常工作,这里是带有演示的代码:

var self = $('pre');
var offset = self.offset();
var command = 'チトシタテイトチトシイスチトシタテイトチトシイスチトシタテイトチトシイス\nfoo bar baz\nfoo bar baz\nチトシタテイトチトシイ';
self.html(command);
function get_char_size() {
var span = $('<span>&nbsp;</span>').appendTo(self);
var rect = span[0].getBoundingClientRect();
span.remove();
return rect;
}
var length = wcwidth;
// mock
function get_splited_command_line(string) {
return string.split('\n');
}
function get_char_pos(point) {
var size = get_char_size();
var width = size.width;
var height = size.height;
var offset = self.offset();
var col_count = Math.floor((point.x - offset.left) / width);
var row = Math.floor((point.y - offset.top) / height);
var lines = get_splited_command_line(command);
var line = lines[row];
var col = 0;
var i = col_count;
while (i > 0) {
i -= length(line[col]);
col++;
}
var try_pos;
if (row > 0 && lines.length > 1) {
try_pos = col + lines.slice(0, row).reduce(function(sum, line) {
return sum + length(line);
}, 0);
} else {
try_pos = col;
}
// tabs are 4 spaces and newline don't show up in results
var text = command.replace(/\t/g, '\x00\x00\x00\x00').replace(/\n/, '');
var before = text.slice(0, try_pos);
var len = before.replace(/\x00{4}/g, '\t').replace(/\x00+/, '').length;
var command_len = command.length;
return len > command_len ? command_len : len;
}
self.click(function(e) {
var pos = get_char_pos({
x: e.pageX,
y: e.pageY
});
self.html(command.substring(0, pos-1) + '<span>' +
command[pos] + '</span>' +
command.substring(pos+1));
});
span {
color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://rawgit.com/jcubic/leash/master/lib/wcwidth.js"></script>
<pre></pre>

我不能为每个字母使用跨度,因为我将文本分成 3 个跨度,分别位于光标之前、光标和光标之后。并且我也有可能在容器中或不在容器中的样式跨度。

感谢任何有关修复此功能的帮助。

解决方案:

这是我基于@Will 的代码,我在我的代码中使用了多个元素(出于某种原因,当您单击只有一个字符的元素时,chrome 会出现问题,以防焦点不在超过文本的长度):

    function get_focus_offset() {
var sel;
if ((sel = window.getSelection()) && (sel.focusNode !== null)) {
return sel.focusOffset;
}
}
function get_char_pos(e) {
var focus = get_focus_offset();
if ($.isNumeric(focus)) {
var node = $(e.target);
// [role="presentation"] is my direct children that have
// siblings that are other nodes with text
var parent = node.closest('[role="presentation"]');
var len = node.text().length;
focus = len === 1 ? 0 : Math.min(focus, len);
return focus + parent.prevUntil('.prompt').text_length() +
node.prevAll().text_length();
} else {
return command.length;
}
}

更新:如果您点击字符的前半部分,则点击会出现问题,但当您点击另一半时,它会选择下一个字符,所以我最终采用了每个元素一个字符的方法.

最佳答案

window.addEventListener('DOMContentLoaded', () => {
document.querySelectorAll('.charPosition').forEach(el => {
let characters = el['innerText'].split('');
el.innerHTML = '';
characters.forEach(char => {
let span = document.createElement('span');
span.innerText = char;
span.addEventListener('click', function () {
let position = 0;
let el = this;
while (el.previousSibling !== null) {
position++;
el = el.previousSibling;
}
console.log(this.innerHTML + ':' + position);
});
el.appendChild(span);
});
});
});
<div class="charPosition">Lorem ipsum dolor sit amet, consectetur adipiscing elit.</div>

关于javascript - 在javascript中单击文本时如何获取字符位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45332637/

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