Skip to content

Commit

Permalink
feat: add Ctrl-E keyboard shortcut to insert scientific notation
Browse files Browse the repository at this point in the history
  • Loading branch information
mgreminger committed Aug 25, 2024
1 parent 5a187f5 commit 5cb7a21
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 37 deletions.
4 changes: 4 additions & 0 deletions src/KeyboardShortcuts.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@
<td class="first-column"><span class="key">/</span></td>
<td>Divide</td>
</tr>
<tr>
<td class="first-column"><span class="key">{modifier}</span> + <span class="key">E</span></td>
<td>Insert *10^ for scientific notation</td>
</tr>
<tr>
<td class="first-column"><span class="key">&lt;</span> then <span class="key">=</span></td>
<td>Insert less than or equal to symbol (&le;)</td>
Expand Down
94 changes: 57 additions & 37 deletions src/MathField.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -70,45 +70,65 @@
}
function handleKeyDown(e: KeyboardEvent) {
if (e.key === 'Tab' && !e.shiftKey) {
e.preventDefault();
let hasPlaceholder = false;
let startingPosition: number | undefined;
if ( !mathLiveField.value.includes('placeholder') ) {
startingPosition = mathLiveField.position;
mathLiveField.executeCommand('moveAfterParent');
} else {
hasPlaceholder = true
}
if (hasPlaceholder || startingPosition === mathLiveField.position) {
mathLiveField.executeCommand('moveToNextPlaceholder');
}
} else if (e.key === '|') {
e.preventDefault();
mathLiveField.executeCommand(['insert', '|']);
} else if (e.key === 'Enter') {
if (!mathLiveField.shadowRoot.querySelector(".ui-menu-container")) {
switch (e.key) {
case 'Tab':
if(!e.shiftKey) {
e.preventDefault();
let hasPlaceholder = false;
let startingPosition: number | undefined;
if ( !mathLiveField.value.includes('placeholder') ) {
startingPosition = mathLiveField.position;
mathLiveField.executeCommand('moveAfterParent');
} else {
hasPlaceholder = true
}
if (hasPlaceholder || startingPosition === mathLiveField.position) {
mathLiveField.executeCommand('moveToNextPlaceholder');
}
}
break;
case '|':
e.preventDefault();
if ($activeMathField?.pendingNewLatex && !e.shiftKey && !e[$modifierKey]) {
$activeMathField.setPendingLatex();
} else if(e.shiftKey) {
dispatch('shiftEnter');
} else if(e[$modifierKey]) {
dispatch('modifierEnter');
} else {
dispatch('enter');
mathLiveField.executeCommand(['insert', '|']);
break;
case 'Enter':
if (!mathLiveField.shadowRoot.querySelector(".ui-menu-container")) {
e.preventDefault();
if ($activeMathField?.pendingNewLatex && !e.shiftKey && !e[$modifierKey]) {
$activeMathField.setPendingLatex();
} else if(e.shiftKey) {
dispatch('shiftEnter');
} else if(e[$modifierKey]) {
dispatch('modifierEnter');
} else {
dispatch('enter');
}
}
}
} else if (e.key === '*' && e[$modifierKey]) {
e.preventDefault();
mathLiveField.executeCommand(['insert', '\\times']);
} else if (e.key === "'") {
e.preventDefault();
mathLiveField.executeCommand(['insert', '^{\\mathrm{T}}']);
} else if (e.key === "F10" && e.shiftKey) {
e.preventDefault();
//@ts-ignore
mathLiveField.showMenu();
break;
case '*':
if (e[$modifierKey]) {
e.preventDefault();
mathLiveField.executeCommand(['insert', '\\times']);
}
break;
case "'":
e.preventDefault();
mathLiveField.executeCommand(['insert', '^{\\mathrm{T}}']);
break;
case "F10":
if(e.shiftKey) {
e.preventDefault();
//@ts-ignore
mathLiveField.showMenu();
}
break;
case "e":
case "E":
if (e[$modifierKey]) {
e.preventDefault();
mathLiveField.executeCommand(['insert', '#@\\cdot10^{#?}']);
}
break;
}
}
Expand Down
30 changes: 30 additions & 0 deletions tests/test_latex_scientific_notation.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,33 @@ test('Test latex scientific notation order of operations', async () => {
content = await page.textContent('#result-units-5');
expect(content).toBe('');
});

test('Test latex scientific notation keyboard shortcut and virtual keyboard', async () => {
const modifierKey = (await page.evaluate('window.modifierKey') )=== "metaKey" ? "Meta" : "Control";

await page.locator('#cell-0 >> math-field.editable').type("2");
await page.locator('#cell-0 >> math-field.editable').press(`${modifierKey}+e`);
await page.locator('#cell-0 >> math-field.editable').type("3");
await page.locator('#cell-0 >> math-field.editable').press("Tab");
await page.locator('#cell-0 >> math-field.editable').type("[mm]=");

await page.locator('#add-math-cell').click();
await page.locator('button').filter({ hasText: '⋅10x⋅10x' }).click();
await page.locator('#cell-1 >> math-field.editable').type("-30");
await page.locator('#cell-1 >> math-field.editable').press("Tab");
await page.locator('#cell-1 >> math-field.editable').type("-3");
await page.locator('#cell-1 >> math-field.editable').press("Tab");
await page.locator('#cell-1 >> math-field.editable').type("[km]=");

await page.waitForSelector('text=Updating...', {state: 'detached'});

let content = await page.textContent('#result-value-0');
expect(parseLatexFloat(content)).toBeCloseTo(2, precision);
content = await page.textContent('#result-units-0');
expect(content).toBe('m');

content = await page.textContent('#result-value-1');
expect(parseLatexFloat(content)).toBeCloseTo(-30, precision);
content = await page.textContent('#result-units-1');
expect(content).toBe('m');
});

0 comments on commit 5cb7a21

Please sign in to comment.