Skip to content

Commit

Permalink
fix: do not capture drag events when double-clicking
Browse files Browse the repository at this point in the history
  • Loading branch information
BearToCode committed Nov 11, 2024
1 parent 4dbf7e0 commit bd45020
Showing 1 changed file with 22 additions and 20 deletions.
42 changes: 22 additions & 20 deletions packages/carta-md/src/lib/internal/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,26 +73,28 @@ export class InputEnhancer {
const currentChar = this.textarea.value.at(cursor);
// Prevent the browser from selecting the space after the word on double clicks, if any
if (e.detail == 2 && currentChar != '\n' && currentChar != ' ') {
e.preventDefault();
// Select the actual word/special chars
const isWordChar = this.isWordCharacter(this.textarea.value[cursor]);
let startPosition = cursor,
endPosition = cursor;

while (
startPosition >= 0 &&
this.isWordCharacter(this.textarea.value[startPosition]) == isWordChar &&
this.textarea.value[startPosition] != ' '
)
startPosition--;
while (
endPosition < this.textarea.value.length &&
this.isWordCharacter(this.textarea.value[endPosition]) == isWordChar &&
this.textarea.value[endPosition] != ' '
)
endPosition++;

this.textarea.setSelectionRange(startPosition + 1, endPosition);
// Do not use e.preventDefault() as it will also capture following drag events
requestAnimationFrame(() => {
// Select the actual word/special chars
const isWordChar = this.isWordCharacter(this.textarea.value[cursor]);
let startPosition = cursor,
endPosition = cursor;

while (
startPosition >= 0 &&
this.isWordCharacter(this.textarea.value[startPosition]) == isWordChar &&
this.textarea.value[startPosition] != ' '
)
startPosition--;
while (
endPosition < this.textarea.value.length &&
this.isWordCharacter(this.textarea.value[endPosition]) == isWordChar &&
this.textarea.value[endPosition] != ' '
)
endPosition++;

this.textarea.setSelectionRange(startPosition + 1, endPosition);
});
}
}

Expand Down

0 comments on commit bd45020

Please sign in to comment.