-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
双击单词不选中尾部空格 #12944
Comments
有问题的话先不采纳了。这个和操作系统有关,Mac 上双击就不会选中。 |
我觉得可以判断一下,只在 Windows 和 浏览器 上运行(Linux不清楚是怎样)
感觉这个应该行了: (() => {
document.addEventListener('dblclick', function(event) {
// 判断当前双击的元素是否在 .protyle-wysiwyg 元素内
const targetElement = event.target.closest('.protyle-wysiwyg');
if (!targetElement) {
return; // 如果不在指定元素内,则直接返回
}
const selection = window.getSelection();
const selectedText = selection.toString();
// 如果没有选中任何有效文本,则直接返回
if (!selectedText.trim()) {
return;
}
// 处理选中的文本
const range = selection.getRangeAt(0);
const endContainer = range.endContainer;
// 如果选中的文本以空白字符结尾,去掉末尾所有空白字符
const whitespaceMatch = selectedText.match(/\s+$/);
if (whitespaceMatch) {
const whitespaceCount = whitespaceMatch[0].length;
const endOffset = Math.max(0, range.endOffset - whitespaceCount); // 确保不小于0
// 确保结束偏移量不超过文本节点的长度
const newEndOffset = Math.min(endOffset, endContainer.length);
// 设置新的范围
range.setEnd(endContainer, newEndOffset);
selection.removeAllRanges();
selection.addRange(range);
}
});
})(); |
用插件或代码片段吧。 |
3 tasks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
问题反馈:
解决思路:
https://ld246.com/article/1729491169237 提供了一种解决思路可以参考(缺陷是有时候会不适用于末尾有多个空格的情况,但我不能稳定复现):
The text was updated successfully, but these errors were encountered: