Skip to content

Commit

Permalink
feat: add simplify/evaluate/solve commands to menu
Browse files Browse the repository at this point in the history
  • Loading branch information
arnog committed Nov 20, 2023
1 parent d453822 commit 0f020b1
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

The menu includes commands to:
- insert and edit matrixes
- evaluate, simplify and solve equations
- change the variant of a symbol (blackboard, fraktur, etc...)
- change the style (italic, bold, etc...) of the selection
- change the color and background color
Expand Down
72 changes: 71 additions & 1 deletion src/editor/default-menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { localize } from 'core/l10n';
import { ModeEditor } from 'editor-mathfield/mode-editor';
import { _Mathfield } from './mathfield';
import { setEnvironment } from 'editor-model/array';
import { TabularEnvironment } from 'public/core-types';
import { TabularEnvironment, VariantStyle } from 'public/core-types';
import { requestUpdate } from 'editor-mathfield/render';
import { removeSuggestion } from 'editor-mathfield/autocomplete';
import { BACKGROUND_COLORS, FOREGROUND_COLORS } from 'core/color';
Expand Down Expand Up @@ -403,6 +403,76 @@ export function getDefaultMenuItems(mf: _Mathfield): MenuItem[] {
{
type: 'divider',
},
{
label: 'Evaluate',
id: 'ce-evaluate',
visible: () => window.MathfieldElement.computeEngine !== null,
onMenuSelect: () => {
const result = mf.expression?.evaluate();
if (mf.model.selectionIsCollapsed) {
mf.model.position = mf.model.lastOffset;
mf.insert(`=${result?.latex ?? ''}`, {
insertionMode: 'insertAfter',
selectionMode: 'item',
});
} else {
mf.insert(result?.latex ?? '', {
insertionMode: 'replaceSelection',
selectionMode: 'item',
});
}
},
},
{
label: 'Simplify',
id: 'ce-simplify',
visible: () => window.MathfieldElement.computeEngine !== null,
onMenuSelect: () => {
const result = mf.expression?.simplify();
if (mf.model.selectionIsCollapsed) {
mf.model.position = mf.model.lastOffset;
mf.insert(`=${result?.latex ?? ''}`, {
insertionMode: 'insertAfter',
selectionMode: 'item',
});
} else {
mf.insert(result?.latex ?? '', {
insertionMode: 'replaceSelection',
selectionMode: 'item',
});
}
},
},
{
label: 'Solve',
id: 'ce-solve',
visible: () =>
window.MathfieldElement.computeEngine !== null &&
mf.expression?.unknowns.length === 1,
onMenuSelect: () => {
const expr = mf.expression!;
const unknown = expr?.unknowns[0];
const results = expr.solve(unknown)?.map((x) => x.latex ?? '');
if (!results) {
mf.model.announce('plonk');
return;
}
mf.insert(
`${unknown}=${
results.length === 1
? results[0]
: '\\left\\lbrace' + results?.join(', ') + '\\right\\rbrace'
}`,
{
insertionMode: 'replaceAll',
selectionMode: 'item',
}
);
},
},
{
type: 'divider',
},
{
label: 'Cut',
onMenuSelect: () => mf.executeCommand('cutToClipboard'),
Expand Down

0 comments on commit 0f020b1

Please sign in to comment.