Skip to content

Commit

Permalink
Send event if selection is lost for the input
Browse files Browse the repository at this point in the history
  • Loading branch information
gohabereg committed Sep 2, 2024
1 parent 165bb92 commit ad4b7ba
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
4 changes: 3 additions & 1 deletion packages/dom-adapters/src/CaretAdapter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export class CaretAdapter extends EventTarget {
*
* @param index - new caret index
*/
public updateIndex(index: Index): void {
public updateIndex(index: Index | null): void {
this.#userCaret.update(index);
}

Expand Down Expand Up @@ -119,6 +119,8 @@ export class CaretAdapter extends EventTarget {
*/
#onSelectionChange(selection: Selection | null): void {
if (!selection) {
this.updateIndex(null);

return;
}

Expand Down
25 changes: 25 additions & 0 deletions packages/dom-adapters/src/utils/useSelectionChange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ export type InputWithCaret = HTMLElement;
* Utility composable that watches for document "selection change" event and delegates the provided callbacks to subscribers.
*/
export const useSelectionChange = createSingleton(() => {
/**
* Stores the last input that was related to the selection.

Check warning on line 35 in packages/dom-adapters/src/utils/useSelectionChange.ts

View workflow job for this annotation

GitHub Actions / lint

Trailing spaces not allowed
* We need that to send update when selection is moved to another input.
*/
let lastRelatedInput: HTMLElement | null = null;

/**
* Used to iterate over all inputs and check if selection is related to them.
*/
Expand Down Expand Up @@ -72,9 +78,28 @@ export const useSelectionChange = createSingleton(() => {
inputsWatched.forEach((input) => {
const subscriber = subscribers.get(input);

let isRelatedInputFound = false;

if (subscriber && isSelectionRelatedToInput(selection, input)) {
lastRelatedInput = input;
isRelatedInputFound = true;

subscriber.callback.call(subscriber.context, selection);
}

/**
* If no related input found or input is changed, we need to notify subscriber selection is out of it's input
*/
if (!isRelatedInputFound || input !== lastRelatedInput) {
subscriber?.callback.call(subscriber.context, null);
}

/**
* If no related input found, we need to reset last related input
*/
if (!isRelatedInputFound) {
lastRelatedInput = null;
}
});
}

Expand Down
4 changes: 2 additions & 2 deletions packages/model/src/CaretManagement/Caret/Caret.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export class Caret extends EventBus {
*
* @param index - new caret index
*/
public update(index: Index): void {
public update(index: Index | null): void {
this.#index = index;

this.dispatchEvent(new CaretUpdatedEvent(this));
Expand All @@ -82,7 +82,7 @@ export class Caret extends EventBus {
public toJSON(): CaretSerialized {
return {
id: this.id,
index: this.index?.serialize(),
index: this.index !== null ? this.index.serialize() : null,
} as CaretSerialized;
}
}

0 comments on commit ad4b7ba

Please sign in to comment.