Skip to content

Commit

Permalink
add set commands to calculation input
Browse files Browse the repository at this point in the history
  • Loading branch information
stephtr committed Apr 1, 2023
1 parent 50ea695 commit d54f6a6
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
13 changes: 11 additions & 2 deletions website/src/lib/calculator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
calculate,
CalculationOptions,
initializeCalculationModule,
setOption,
} from './calculatorModule';
import { History } from './history';
import { Settings } from './settings';
Expand Down Expand Up @@ -111,12 +112,20 @@ export class Calculator {
private pendingCalculationOnceLoaded: string | null = null;

submitCalculation(input: string) {
this.submittedListeners.forEach((l) => l(input));
const isSetCommand = input.startsWith('set ');
if (!isSetCommand) {
this.submittedListeners.forEach((l) => l(input));
}
if (!this.isLoaded) {
this.pendingCalculationOnceLoaded = input;
return;
}
this.history.add(this.calculate(input));

if (isSetCommand) {
setOption(input.slice(4));
} else {
this.history.add(this.calculate(input));
}
}

constructor() {
Expand Down
27 changes: 21 additions & 6 deletions website/src/lib/calculatorWidget.svelte
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<script lang="ts">
import { slide } from 'svelte/transition';
import type { Calculator } from './calculator';
import { setOption } from './calculatorModule';
import {
generateSuggestions,
getLastWord,
type MatchedSuggestion,
type Suggestion,
} from './suggestions';
import Suggestions from './suggestionsWidget.svelte';
Expand All @@ -16,6 +16,11 @@
let currentInput = '';
let currentResult = '';
export function updateCurrentResult(_: any, ensureResult = false) {
const isSetCommand = currentInput.startsWith('set ');
if (isSetCommand) {
currentResult = '';
return;
}
currentResult = '';
if (currentInput !== '' && calculator.isLoaded) {
try {
Expand Down Expand Up @@ -46,8 +51,18 @@
let scrollSuggestionIntoView: (suggestion: string) => void;
function submitCalculationFromInput() {
if (currentInput === '') return; // nothing to calculate
function submitCalculationFromInput(submittedByBlur: boolean) {
const isSetCommand = currentInput.startsWith('set ');
if (currentInput === '' || (isSetCommand && submittedByBlur)) return; // nothing to calculate
if (isSetCommand) {
const success = setOption(currentInput.slice(4));
if (success) {
currentInput = '';
} else {
currentResult = 'Error';
}
return;
}
if (submitOnBlur) {
calculator.submitCalculation(currentInput);
currentInput = '';
Expand All @@ -65,7 +80,7 @@
selectedSuggestion,
);
} else {
submitCalculationFromInput();
submitCalculationFromInput(false);
}
suggestions = [];
}
Expand Down Expand Up @@ -171,7 +186,7 @@
/** loads a calculation */
export function selectCalculation(calc: string) {
submitCalculationFromInput();
submitCalculationFromInput(true);
currentInput = calc;
inputElement.focus();
setTimeout(() => updateCurrentResult(currentInput, true), 10);
Expand Down Expand Up @@ -205,7 +220,7 @@
setTimeout(() => {
if (!windowBluring && !suggestionBluring && !selectBluring) {
if (submitOnBlur) {
submitCalculationFromInput();
submitCalculationFromInput(true);
}
suggestions = [];
backedUpInputSelectionStart = null;
Expand Down

0 comments on commit d54f6a6

Please sign in to comment.