Skip to content
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

Closed
TCOTC opened this issue Oct 27, 2024 · 3 comments
Closed

双击单词不选中尾部空格 #12944

TCOTC opened this issue Oct 27, 2024 · 3 comments
Assignees

Comments

@TCOTC
Copy link
Contributor

TCOTC commented Oct 27, 2024

问题反馈:

解决思路:

https://ld246.com/article/1729491169237 提供了一种解决思路可以参考(缺陷是有时候会不适用于末尾有多个空格的情况,但我不能稳定复现):

(()=>{
    document.addEventListener('dblclick', function() {
        const selection = window.getSelection();
        const selectedText = selection.toString();
  
        // 如果选中的文本后面有空格,就处理掉末尾空格
        if (selectedText != " " && selectedText.endsWith(' ')) {
            const range = selection.getRangeAt(0);
            const startOffset = range.startOffset;
            const endOffset = range.endOffset - 1; // 移除末尾空格
  
            // 设置新的范围
            range.setStart(range.startContainer, startOffset);
            range.setEnd(range.startContainer, endOffset);
            selection.removeAllRanges();
            selection.addRange(range);
        }
    });
})()
@TCOTC TCOTC changed the title 双击单词会选中尾部空格 双击单词不选中尾部空格 Oct 27, 2024
@Vanessa219
Copy link
Member

有问题的话先不采纳了。这个和操作系统有关,Mac 上双击就不会选中。
该特性可以作为插件提供给需要的人。

@TCOTC
Copy link
Contributor Author

TCOTC commented Nov 10, 2024

这个和操作系统有关,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);
        }
    });
})();

@Vanessa219
Copy link
Member

用插件或代码片段吧。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants