Strange issue with Italian keyboard layout
See original GitHub issueDescription
Hello, we are using mathlive with the react wrapper by ShaMan123 https://github.com/ShaMan123/react-math-view and we are experiencing a strange issue with the keyboard mappings using the Italian keyboard layout.
I’ve prepared a minimal environment reproducing our setup here: https://glitch.com/edit/#!/confirmed-bristle-digestion?path=src%2Fapp.jsx%3A56%3A45
Steps to Reproduce
In the test environment (https://confirmed-bristle-digestion.glitch.me):
-
configure your machine with the Italian keyboard layout
-
type
y ^ 2 -
-
the layout is recognized as
spanish
(which is fine as it is functionally equivalent to the Italian layout, at least for mathlive) -
click the “Re-mount component” link in the page (this simulates the remount of the mathlive component)
-
focus the mathfield and delete all the text ( either using
Ctrl+A and backspace
or with multiplebackspace
) -
type again
y ^ 2 -
Actual Behavior
The second time you type y ^ 2 -
, the -
is mapped as if the keyboard layout was set to english and produces a fraction as output.
(note: the layout chosen by mathlive mapping logic is indeed english
)
Expected Behavior
The keyboard layout should be correctly identified also the 2nd time we type y ^ 2 -
Possible solution
We tried to analyze the issue and found that here https://github.com/arnog/mathlive/blob/d459adf1756d1b459362733d1a49f52adee64ca9/src/editor/keyboard-layout.ts#L657 the layouts sorting is done in-place and it doesn’t account for the initial layouts ordering. We added some logic that keeps the ordering stable with regard to the initial ordering (ie if two layouts have the same score they are sorted using the initial layouts order) and this seems to solve our issue.
src/editor/keyboard-layout.ts | 10 +++++++++-
1 file changed, 9 insertions(+), 1 deletion(-)
diff --git a/src/editor/keyboard-layout.ts b/src/editor/keyboard-layout.ts
index f9548e94..595085f7 100644
--- a/src/editor/keyboard-layout.ts
+++ b/src/editor/keyboard-layout.ts
@@ -492,6 +492,7 @@ const BASE_LAYOUT_MAPPING = {
};
const gKeyboardLayouts: KeyboardLayout[] = [];
+const gKeyboardLayoutsIDs: KeyboardLayoutId[] = [];
let gKeyboardLayout: KeyboardLayout | undefined;
@@ -512,6 +513,7 @@ export function platform(): 'apple' | 'windows' | 'linux' {
export function register(layout: KeyboardLayout): void {
if (!layout.platform || layout.platform === platform()) {
gKeyboardLayouts.push(layout);
+ gKeyboardLayoutsIDs.push(layout.id);
}
}
@@ -654,7 +656,13 @@ export function validateKeyboardLayout(evt?: KeyboardEvent): void {
}
}
- gKeyboardLayouts.sort((a, b) => b.score - a.score);
+ gKeyboardLayouts.sort((a, b) =>
+ (b.score === a.score) ?
+ // sort by original order when the score is equal
+ gKeyboardLayoutsIDs.indexOf(a.id) - gKeyboardLayoutsIDs.indexOf(b.id) :
+ // sort by decreasing score
+ b.score - a.score
+ );
}
export function setKeyboardLayoutLocale(locale: string): void {
We could open a PR with this solution but we would really appreciate your feedback first, since we don’t know if this is an isolated issue or if it’s part of a bigger problem and we still have doubt on the root cause of this issue.
Thank you, have a nice day!
Environment
MathLive version: 0.68.1
Operating System: macOS, Windows, Linux
Browser: Chrome, Firefox
Issue Analytics
- State:
- Created 2 years ago
- Reactions:2
- Comments:6 (2 by maintainers)
Top GitHub Comments
Thanks for reporting this and for taking the time to write an excellent report and to build a reproducible case.
Your fix can work, but I don’t think it’s quite right.
I think the problem is that when the mathfield is remounted the keyboard layouts get re-registered, the new ones override the previous ones and the score they had accumulated. Instead, the keyboard layout registration should avoid registering keyboards that are already known, i.e. in
register()
, ifgKeyboardLayouts
already has a matching ID, skip the registration.Could you give that a try? I unfortunately have some problems fully debugging through your example and it might be easier for you to make that change and validate that it works. If it does, I’d be happy to merge a PR for it.
@arnog Sure, see #1194