Skip to content

Commit

Permalink
Remove contenteditable event listener
Browse files Browse the repository at this point in the history
  • Loading branch information
khaitruong922 committed Jul 26, 2024
1 parent 914472d commit 618b53c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
28 changes: 24 additions & 4 deletions ext/js/dom/dom-data-binder.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,18 +175,18 @@ export class DOMDataBinder {
const metadata = this._createElementMetadata(element);
if (typeof metadata === 'undefined') { return void 0; }
const type = this._getNormalizedElementType(element);
const eventType = this._getEventType(element);
/** @type {import('dom-data-binder').ElementObserver<T>} */
const observer = {
element,
type,
value: null,
hasValue: false,
eventType,
onChange: null,
metadata,
};
observer.onChange = this._onElementChange.bind(this, observer);

const eventType = type === 'contenteditable' ? 'focusout' : 'change';
element.addEventListener(eventType, observer.onChange, false);

void this._updateTasks.enqueue(observer, {all: false});
Expand All @@ -200,7 +200,7 @@ export class DOMDataBinder {
*/
_removeObserver(element, observer) {
if (observer.onChange === null) { return; }
element.removeEventListener('change', observer.onChange, false);
element.removeEventListener(observer.eventType, observer.onChange, false);
observer.onChange = null;
}

Expand Down Expand Up @@ -285,12 +285,24 @@ export class DOMDataBinder {
return null;
}


/**
* @param {Element} element
* @returns {import('dom-data-binder').EventType}
*/
_getEventType(element) {
if (this._isContentEditable(element)) {
return 'focusout';
}
return 'change';
}

/**
* @param {Element} element
* @returns {import('dom-data-binder').NormalizedElementType}
*/
_getNormalizedElementType(element) {
if (element instanceof HTMLElement && element.isContentEditable) {
if (this._isContentEditable(element)) {
return 'contenteditable';
}
switch (element.nodeName.toUpperCase()) {
Expand All @@ -314,4 +326,12 @@ export class DOMDataBinder {
}
return null;
}

/**
* @param {Element} element
* @returns {boolean}
*/
_isContentEditable(element) {
return element instanceof HTMLElement && element.isContentEditable;
}
}
2 changes: 1 addition & 1 deletion ext/js/language/translator.js
Original file line number Diff line number Diff line change
Expand Up @@ -1777,7 +1777,7 @@ export class Translator {
let score = Number.MIN_SAFE_INTEGER;
let dictionaryIndex = Number.MAX_SAFE_INTEGER;
let dictionaryPriority = Number.MIN_SAFE_INTEGER;
const dictionaryAlias = "";
const dictionaryAlias = '';
let maxOriginalTextLength = 0;
let isPrimary = false;
/** @type {import('dictionary').TermDefinition[]} */
Expand Down
3 changes: 3 additions & 0 deletions types/ext/dom-data-binder.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export type ElementObserver<T = unknown> = {
type: NormalizedElementType;
value: unknown;
hasValue: boolean;
eventType: string;
onChange: null | (() => void);
metadata: T;
};
Expand All @@ -55,6 +56,8 @@ export type SettingChangedEvent = CustomEvent<SettingChangedEventData>;

export type NormalizedElementType = 'textarea' | 'select' | 'text' | 'checkbox' | 'number' | 'contenteditable' | null;

export type EventType = 'change' | 'focusout'

Check failure on line 59 in types/ext/dom-data-binder.d.ts

View workflow job for this annotation

GitHub Actions / Static Analysis

Missing semicolon

export type UpdateTaskValue = {all: boolean};

export type AssignTaskValue = {value: ValueType};
Expand Down

0 comments on commit 618b53c

Please sign in to comment.