Skip to content

Commit

Permalink
fix: Fixed #2176 virtual keyboard bug
Browse files Browse the repository at this point in the history
  • Loading branch information
arnog committed Nov 16, 2023
1 parent d2799ac commit c0b06b2
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 25 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
- **#2159** Runtime error in sandboxed mode when in an iframe from different
origin
- **#2175** Addressed some rendering issues with Safar where a fraction inside a `\left...\right` was vertically offset.

- **#2176** Using the `[hide-keyboard]` virtual keycap would cause a runtime error.

## 0.96.0 (2023-11-14)

Expand Down
7 changes: 4 additions & 3 deletions src/editor/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,14 +162,15 @@ export function perform(
* Perform a command, but:
* * focus the mathfield
* * provide haptic and audio feedback
* This is used by the virtual keyboard when command keys (delete, arrows, etc..)
* are pressed.
* This is used by the virtual keyboard when command keys (delete, arrows,
* etc..) are pressed.
*/

function performWithFeedback(
mathfield: MathfieldPrivate,
mathfield: MathfieldPrivate | undefined,
selector: SelectorPrivate
): boolean {
if (!mathfield) return false;
mathfield.focus();

if (MathfieldElement.keypressVibration && canVibrate())
Expand Down
2 changes: 1 addition & 1 deletion src/virtual-keyboard/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -752,7 +752,7 @@ const KEYCAP_SHORTCUTS: Record<string, Partial<VirtualKeyboardKeycap>> = {
},
'[hide-keyboard]': {
class: 'action',
command: ['performWithFeedback', 'hideVirtualKeyboard'],
command: ['hideVirtualKeyboard'],
width: 1.5,
label:
'<svg class=svg-glyph-lg><use xlink:href=#svg-keyboard-down /></svg>',
Expand Down
11 changes: 9 additions & 2 deletions src/virtual-keyboard/virtual-keyboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -834,17 +834,24 @@ export class VirtualKeyboard implements VirtualKeyboardInterface, EventTarget {
): boolean {
let selector: SelectorPrivate;
let args: string[] = [];
let target = getCommandTarget(command);

if (isArray(command)) {
selector = command[0];
if (selector === 'performWithFeedback') {
command = command.slice(1) as [SelectorPrivate, ...any[]];
target = getCommandTarget(command);
}
args = command.slice(1);
} else selector = command;

// Convert kebab case (like-this) to camel case (likeThis).
selector = selector.replace(/-\w/g, (m) =>
m[1].toUpperCase()
) as SelectorPrivate;
if (getCommandTarget(command) === 'virtual-keyboard')
return COMMANDS[selector]!.fn(...args);

if (target === 'virtual-keyboard')
return COMMANDS[selector]!.fn(undefined, ...args);

this.sendMessage('execute-command', { command });
return false;
Expand Down
37 changes: 19 additions & 18 deletions test/smoke/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -151,24 +151,25 @@ <h2>Latex to Speakable Text</h2>
/^[a-zA-Z]+$/.test(s) ? "function" : "unknown",
};

// mathVirtualKeyboard.layouts = {
// rows: [
// [
// { width: 1.5, latex: '\\ce{H2O}' },
// '+',
// '-',
// '\\times',
// '\\frac{#@}{#?}',
// '=',
// '.',
// '(',
// ')',
// '\\sqrt{#0}',
// '#@^{#?}',
// ],
// ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0'],
// ],
// };
mathVirtualKeyboard.layouts = {
rows: [
[
"[hide-keyboard]",
{ width: 1.5, latex: "\\ce{H2O}" },
"+",
"-",
"\\times",
"\\frac{#@}{#?}",
"=",
".",
"(",
")",
"\\sqrt{#0}",
"#@^{#?}",
],
["1", "2", "3", "4", "5", "6", "7", "8", "9", "0"],
],
};
// makeVirtualKeyboard([
// '\\operatorname*{arg~max}_#?}',
// '\\%',
Expand Down

0 comments on commit c0b06b2

Please sign in to comment.