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

fix(web): better element inputmode management 📴 #7395

Merged
merged 6 commits into from
Oct 17, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 56 additions & 1 deletion web/source/dom/domManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ namespace com.keyman.dom {
*/
enablementObserver: MutationObserver;

/**
* Tracks changes in inputmode state.
*/
inputModeObserver: MutationObserver;

/**
* Tracks a list of event-listening elements.
*
Expand Down Expand Up @@ -99,6 +104,9 @@ namespace com.keyman.dom {
if(this.attachmentObserver) {
this.attachmentObserver.disconnect();
}
if(this.inputModeObserver) {
this.inputModeObserver.disconnect();
}

for(let input of this.inputList) {
this.disableInputElement(input);
Expand Down Expand Up @@ -233,6 +241,7 @@ namespace com.keyman.dom {

if(!this.isAttached(Pelem)) {
this.setupElementAttachment(Pelem);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to be done again in enableInputElement(), called on line 247, albeit in a more nuanced manner (handling iframes)

Pelem._kmwAttachment.inputMode = Pelem.inputMode ?? 'text';
Pelem.inputMode = 'none';
}

Expand Down Expand Up @@ -264,7 +273,13 @@ namespace com.keyman.dom {
*/
disableTouchElement(Pelem: HTMLElement) {
// Do not check for the element being officially disabled - it's also used for detachment.
Pelem.inputMode = 'text';
const intendedInputMode = Pelem._kmwAttachment.inputMode;

this.disableInputModeObserver();
Pelem.inputMode = intendedInputMode;
this.enableInputModeObserver();

this.setupNonKMWTouchElement(Pelem);
}

/**
Expand Down Expand Up @@ -907,6 +922,30 @@ namespace com.keyman.dom {
}
}.bind(this);

_InputModeObserverCore = function(this: DOMManager, mutations: MutationRecord[]) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a comment explaining the purpose of this function? Also I think we should be naming it _inputModeObserverCore?

Copy link
Contributor Author

@jahorton jahorton Oct 17, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The other observer core functions have similar capitalization:

  • _AutoAttachObserverCore
  • _EnablementMutationObserverCore

Should I change them, too?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I change them, too?

It's probably good sanitisation to iteratively cleanup to javaScript camelCasing preference, so yeah, if it's not too big a yak shave, sure!

const keyman = com.keyman.singleton;
// Prevent infinite recursion from any changes / updates made within the observation handler.
this.disableInputModeObserver();
try {
for(const mutation of mutations) {
const target = mutation.target as HTMLElement;
if(!(this as DOMManager).isAttached(target)) {
continue;
}

const newValue = target.inputMode;

target._kmwAttachment.inputMode = newValue;

if(keyman.util.device.touchable) {
target.inputMode = 'none';
}
}
} finally {
this.enableInputModeObserver();
}
}.bind(this);

/**
* Function _MutationAdditionObserved
* Scope Private
Expand Down Expand Up @@ -1611,6 +1650,12 @@ namespace com.keyman.dom {
observationConfig = { subtree: true, attributes: true, attributeOldValue: true, attributeFilter: ['class', 'readonly']};
this.enablementObserver = new MutationObserver(this._EnablementMutationObserverCore);
this.enablementObserver.observe(observationTarget, observationConfig);

/**
* Setup of handlers for dynamic detection of change in inputMode state.
*/
this.inputModeObserver = new MutationObserver(this._InputModeObserverCore);
this.enableInputModeObserver();
} else {
console.warn("Your browser is outdated and does not support MutationObservers, a web feature " +
"needed by KeymanWeb to support dynamically-added elements.");
Expand All @@ -1630,6 +1675,16 @@ namespace com.keyman.dom {
return Promise.resolve();
}.bind(this);

enableInputModeObserver() {
const observationTarget = document.querySelector('body');
const observationConfig = { subtree: true, attributes: true, attributeFilter: ['inputmode']};
this.inputModeObserver.observe(observationTarget, observationConfig);
}

disableInputModeObserver() {
this.inputModeObserver.disconnect();
}

/**
* Initialize the desktop user interface as soon as it is ready
*/
Expand Down
7 changes: 6 additions & 1 deletion web/source/kmwtypedefs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,17 @@ namespace com.keyman {

/**
* Tracks if the control has an aliased control for touch functionality.
*
*
* Future note - could be changed to track the DOMEventHandler instance used by this control;
* this may be useful for an eventual hybrid touch/non-touch implementation.
*/
touchEnabled: boolean;

/**
* Tracks the inputmode originally set by the webpage.
*/
inputMode?: string;

constructor(eleInterface: dom.targets.OutputTarget, kbd: string, touch?: boolean) {
this.interface = eleInterface;
this.keyboard = kbd;
Expand Down