From 760be42773572f6186499c7b02a42177d0b5a39c Mon Sep 17 00:00:00 2001 From: Timofey Arkusha Date: Mon, 27 Nov 2023 14:45:43 +0300 Subject: [PATCH] =?UTF-8?q?=D0=92=D0=B5=D1=80=D0=BD=D1=83=D0=BB=D1=81?= =?UTF-8?q?=D1=8F=20=D0=BD=D0=B0=20number?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/App.tsx | 33 +++++++++++++++++++++++++++------ src/index.css | 10 ++++++++-- src/math.ts | 2 +- src/mathPlugin.ts | 40 ++++++++++++++++++++++++++++++++-------- 4 files changed, 68 insertions(+), 17 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index e42fab4..5e1c49c 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -6,23 +6,44 @@ export const App = () => { } const testText = ` +# Welcome to calculator (написать норм приветствие и описать принцип работы калькулятора) +# Lines starting with # are ignored + +# Basic math +2 + 2 +6 - 3 +10 / 4 +5 * 1.5 2 + 2 * 2 -sqrt(3^2 + 4^2) -5cm + 0.2 m in inch -cos(45 deg) +(2 + 2) * 2 0.1 + 0.2 +# Units +10 inch in cm +2 feet to m +15 degC to degF +3 tbsp to tsp +9 km/h to m/s + +# Functions +sqrt(3^2 + 4^2) +cos(pi rad) +sin(30 deg) + +# Variables a = 25 b = a * 2 -pow2(x) = x ^ 2 -pow2(6) - 2 * 2 last + 1 +# Function declaration +pow2(x) = x ^ 2 +pow2(6) + f(x) = (sin(x) + cos(x/2)) * 5 +# Matrices a = [1, 2, 3; 2+2, 5, 6] a[2, 3] a[1:2, 2] diff --git a/src/index.css b/src/index.css index 332f420..4fada80 100644 --- a/src/index.css +++ b/src/index.css @@ -1,12 +1,14 @@ html { - line-height: 1.5; + font-size: 16px; + line-height: 1.4; tab-size: 4; font-feature-settings: normal; font-variation-settings: normal; } body { - margin: 0; + max-width: 800px; + margin: auto; height: 100vh; } @@ -16,6 +18,10 @@ body { height: 100%; } +#root .cm-editor-mount .cm-editor { + outline: none; +} + .math-result { color: rgb(80, 80, 80); margin-left: 8px; diff --git a/src/math.ts b/src/math.ts index ff37d72..6caed26 100644 --- a/src/math.ts +++ b/src/math.ts @@ -1,3 +1,3 @@ import { create, all } from 'mathjs' -export const math = create(all, { number: 'BigNumber', precision: 16 }) +export const math = create(all) diff --git a/src/mathPlugin.ts b/src/mathPlugin.ts index 9b8e20a..68bf19c 100644 --- a/src/mathPlugin.ts +++ b/src/mathPlugin.ts @@ -33,17 +33,34 @@ const createDecorations = (view: EditorView) => { try { const result = parser.evaluate(text) - if (result?.isBigNumber || result?.type === 'Unit') { + if (result === undefined) { + continue + } + + console.log(result) + + if (typeof result === 'number') { parser.set('last', result) + parser.set('prev', result) + parser.set('previous', result) + + decoration = Decoration.widget({ + widget: new MathResult(math.format(result, { precision: 12 })), + side: 1, + }) + } else if (result?.type === 'Unit') { + parser.set('last', result) + parser.set('prev', result) + parser.set('previous', result) decoration = Decoration.widget({ widget: new MathResult(result.toString()), side: 1, }) - } - - if (result?.type === 'DenseMatrix') { + } else if (result?.type === 'DenseMatrix') { parser.set('last', result) + parser.set('prev', result) + parser.set('previous', result) const size = result.size() @@ -58,15 +75,22 @@ const createDecorations = (view: EditorView) => { side: 1, }) } - } - - if (typeof result === 'function') { + } else if (typeof result === 'function') { decoration = Decoration.widget({ widget: new MathResult(result.syntax), side: 1, }) + } else if (result.isResultSet) { + decoration = Decoration.widget({ + widget: new MathResult(result.entries[0].toString()), + side: 1, + }) + } else { + console.log(result) } - } catch (e) {} + } catch (e) { + console.error(e) + } if (decoration) { widgets.push(decoration.range(to))