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

Trim text selected by "Select all" #18

Open
pintassilgo opened this issue Mar 23, 2022 · 1 comment
Open

Trim text selected by "Select all" #18

pintassilgo opened this issue Mar 23, 2022 · 1 comment

Comments

@pintassilgo
Copy link

It's common that text copied after using "Select all" feature has spaces before, after or both. For instance, if you hold left-click over "dragselectlinktext" link above (in "nkestrel / dragselectlinktext") copy the selection and paste it elsewhere, the result is " dragselectlinktext ", with space at both ends, while it's obvious it should be just "dragselectlinktext".

I think the extension should process the selection and trim it.

@pintassilgo
Copy link
Author

pintassilgo commented Mar 24, 2022

May not fix it in all cases, but at first glance this simple change works:

function selectAll(element, multiSelect) {
let selection = window.getSelection();
if (!multiSelect) {
selection.removeAllRanges();
}
let range = document.createRange();
range.selectNodeContents(element);
selection.addRange(range);
selectedAll = true;
}

function selectAll(element, multiSelect) {
  let selection = window.getSelection();
  if (!multiSelect) {
    selection.removeAllRanges();
  }
  let range = document.createRange();
  if (element.children.length)
    range.selectNodeContents(element);
  else
    range.selectNodeContents(element.firstChild);

  const text = range.toString();
  const startOffset = text.length - text.trimStart().length;
  const endOffset = text.length - text.trimEnd().length;

  if (startOffset)
    range.setStart(range.startContainer, range.startOffset + startOffset);

  if (endOffset)
    range.setEnd(range.endContainer, range.endOffset - endOffset);

  selection.addRange(range);
  selectedAll = true;
}

Change based in this code.

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

1 participant