Implement adding expression and var decls
This commit is contained in:
parent
18252912a9
commit
4a3e92b910
@ -6,12 +6,11 @@ import Katex from './Katex.vue'
|
|||||||
|
|
||||||
const props = defineProps<{
|
const props = defineProps<{
|
||||||
statement: MathStatement,
|
statement: MathStatement,
|
||||||
evaluation: EvaluationResult,
|
evaluation?: EvaluationResult,
|
||||||
renderVersion: number,
|
|
||||||
}>()
|
}>()
|
||||||
|
|
||||||
const getValueStatement = (): Maybe<MathStatement> => {
|
const getValueStatement = (): Maybe<MathStatement> => {
|
||||||
const value = props.evaluation.statements[props.statement.id]
|
const value = props.evaluation?.statements?.[props.statement.id]
|
||||||
if ( value ) {
|
if ( value ) {
|
||||||
return MathStatement.temp(String(value))
|
return MathStatement.temp(String(value))
|
||||||
}
|
}
|
||||||
|
@ -1,3 +1,60 @@
|
|||||||
|
<script setup lang="ts">
|
||||||
|
import { ref } from 'vue'
|
||||||
|
import { v4 as uuidv4 } from 'uuid'
|
||||||
|
import WrapperBox from '../components/WrapperBox.vue'
|
||||||
|
import {MathPage} from '../support/page'
|
||||||
|
import {MathStatement} from '../support/parse'
|
||||||
|
import Katex from '../components/Katex.vue'
|
||||||
|
import {EvaluationResult, StatementID} from '../types'
|
||||||
|
import Statement from '../components/Statement.vue'
|
||||||
|
import VarDeclEditor from './VarDeclEditor.vue'
|
||||||
|
import ExpressionEditor from './ExpressionEditor.vue'
|
||||||
|
|
||||||
|
const math = new MathPage(uuidv4())
|
||||||
|
const statements = ref<MathStatement[]>([])
|
||||||
|
const evaluation = ref<EvaluationResult|undefined>()
|
||||||
|
const statementsKey = ref<string>(uuidv4())
|
||||||
|
|
||||||
|
const leftDrawerOpen = ref(false);
|
||||||
|
|
||||||
|
function toggleLeftDrawer() {
|
||||||
|
leftDrawerOpen.value = !leftDrawerOpen.value;
|
||||||
|
}
|
||||||
|
|
||||||
|
const newVariableModalOpen = ref(false)
|
||||||
|
const openNewVariableDeclModal = () => {
|
||||||
|
newVariableModalOpen.value = true
|
||||||
|
}
|
||||||
|
|
||||||
|
const newExpressionModalOpen = ref(false)
|
||||||
|
const openNewExpressionModal = () => {
|
||||||
|
newExpressionModalOpen.value = true
|
||||||
|
}
|
||||||
|
|
||||||
|
const updateStatements = () => {
|
||||||
|
statements.value = math.getStatements()
|
||||||
|
try {
|
||||||
|
evaluation.value = math.evaluate()
|
||||||
|
} catch (_) {
|
||||||
|
evaluation.value = undefined
|
||||||
|
}
|
||||||
|
statementsKey.value = uuidv4()
|
||||||
|
}
|
||||||
|
|
||||||
|
const saveNewVariable = (stmt: MathStatement) => {
|
||||||
|
math.addStatement(stmt)
|
||||||
|
newVariableModalOpen.value = false
|
||||||
|
updateStatements()
|
||||||
|
}
|
||||||
|
|
||||||
|
const saveNewExpression = (stmt: MathStatement) => {
|
||||||
|
math.addStatement(stmt)
|
||||||
|
newExpressionModalOpen.value = false
|
||||||
|
updateStatements()
|
||||||
|
}
|
||||||
|
|
||||||
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<q-layout view="hHh Lpr fff">
|
<q-layout view="hHh Lpr fff">
|
||||||
<q-header reveal bordered class="bg-primary text-white" height-hint="98">
|
<q-header reveal bordered class="bg-primary text-white" height-hint="98">
|
||||||
@ -32,7 +89,49 @@
|
|||||||
</q-drawer>
|
</q-drawer>
|
||||||
|
|
||||||
<q-page-container>
|
<q-page-container>
|
||||||
<WrapperBox />
|
<!-- <WrapperBox />-->
|
||||||
|
|
||||||
|
<Draggable
|
||||||
|
v-for="statement in statements"
|
||||||
|
:grid="[25, 25]"
|
||||||
|
>
|
||||||
|
<Statement
|
||||||
|
:key="statementsKey"
|
||||||
|
:statement="statement"
|
||||||
|
:evaluation="evaluation"
|
||||||
|
/>
|
||||||
|
</Draggable>
|
||||||
|
|
||||||
|
<q-dialog v-model="newVariableModalOpen">
|
||||||
|
<VarDeclEditor
|
||||||
|
v-on:save="s => saveNewVariable(s)"
|
||||||
|
/>
|
||||||
|
</q-dialog>
|
||||||
|
|
||||||
|
<q-dialog v-model="newExpressionModalOpen">
|
||||||
|
<ExpressionEditor
|
||||||
|
v-on:save="s => saveNewExpression(s)"
|
||||||
|
/>
|
||||||
|
</q-dialog>
|
||||||
|
|
||||||
|
<q-page-sticky position="bottom-right" :offset="[32, 32]">
|
||||||
|
<q-fab color="primary" icon="add" direction="left">
|
||||||
|
<q-fab-action
|
||||||
|
color="secondary"
|
||||||
|
label="x"
|
||||||
|
label-style="font-family: serif; font-size: 1.4em; padding: 0"
|
||||||
|
title="Add a variable declaration"
|
||||||
|
@click="() => openNewVariableDeclModal()"
|
||||||
|
/>
|
||||||
|
<q-fab-action
|
||||||
|
color="secondary"
|
||||||
|
icon="code"
|
||||||
|
label-style="font-family: serif; font-size: 1.4em; padding: 0"
|
||||||
|
title="Add an expression"
|
||||||
|
@click="() => openNewExpressionModal()"
|
||||||
|
/>
|
||||||
|
</q-fab>
|
||||||
|
</q-page-sticky>
|
||||||
</q-page-container>
|
</q-page-container>
|
||||||
|
|
||||||
<q-footer reveal elevated class="bg-grey-8 text-white">
|
<q-footer reveal elevated class="bg-grey-8 text-white">
|
||||||
@ -44,14 +143,3 @@
|
|||||||
</q-footer>
|
</q-footer>
|
||||||
</q-layout>
|
</q-layout>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup lang="ts">
|
|
||||||
import { ref } from "vue";
|
|
||||||
import WrapperBox from "../components/WrapperBox.vue";
|
|
||||||
|
|
||||||
const leftDrawerOpen = ref(false);
|
|
||||||
|
|
||||||
function toggleLeftDrawer() {
|
|
||||||
leftDrawerOpen.value = !leftDrawerOpen.value;
|
|
||||||
}
|
|
||||||
</script>
|
|
||||||
|
78
src/pages/ExpressionEditor.vue
Normal file
78
src/pages/ExpressionEditor.vue
Normal file
@ -0,0 +1,78 @@
|
|||||||
|
<script setup lang="ts">
|
||||||
|
import {defineEmits, ref} from 'vue'
|
||||||
|
import {MathStatement} from '../support/parse'
|
||||||
|
import Katex from '../components/Katex.vue'
|
||||||
|
import { v4 as uuidv4 } from 'uuid'
|
||||||
|
import {StatementID} from '../types'
|
||||||
|
|
||||||
|
const emit = defineEmits<{
|
||||||
|
(eventName: 'save', statement: MathStatement): void
|
||||||
|
}>()
|
||||||
|
|
||||||
|
const expressionPreview = ref(MathStatement.temp(''))
|
||||||
|
const expressionPreviewKey = ref(uuidv4())
|
||||||
|
const expressionError = ref<string|undefined>(undefined)
|
||||||
|
const expressionValue = ref<string>('')
|
||||||
|
|
||||||
|
const updateExpressionPreview = () => {
|
||||||
|
const previewStmt = MathStatement.temp(expressionValue.value)
|
||||||
|
if ( previewStmt.isValid() ) {
|
||||||
|
expressionPreview.value = MathStatement.temp(expressionValue.value)
|
||||||
|
expressionPreviewKey.value = uuidv4()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const validateExpression = () => {
|
||||||
|
const stmt = MathStatement.temp(expressionValue.value)
|
||||||
|
if ( !stmt.isValid() ) {
|
||||||
|
return 'The expression is invalid'
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( stmt.defines().length > 0 ) {
|
||||||
|
return 'Expressions cannot declare variables'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const saveExpression = () => {
|
||||||
|
expressionError.value = validateExpression()
|
||||||
|
if ( expressionError.value ) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
emit('save', new MathStatement(
|
||||||
|
uuidv4() as StatementID,
|
||||||
|
expressionValue.value
|
||||||
|
))
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<template>
|
||||||
|
<q-card>
|
||||||
|
<q-card-section>
|
||||||
|
<div style="display: flex; justify-content: center">
|
||||||
|
<Katex
|
||||||
|
:key="expressionPreviewKey"
|
||||||
|
:statement="expressionPreview"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</q-card-section>
|
||||||
|
<q-card-section v-if="expressionError">
|
||||||
|
<div style="color: darkred; font-weight: bold">{{ expressionError }}</div>
|
||||||
|
</q-card-section>
|
||||||
|
<q-card-section>
|
||||||
|
<q-input
|
||||||
|
v-model="expressionValue"
|
||||||
|
v-on:update:model-value="() => updateExpressionPreview()"
|
||||||
|
outlined
|
||||||
|
autogrow
|
||||||
|
type="textarea"
|
||||||
|
autofocus
|
||||||
|
label="Expression"
|
||||||
|
/>
|
||||||
|
</q-card-section>
|
||||||
|
<q-card-actions align="right" class="text-primary">
|
||||||
|
<q-btn flat label="Cancel" v-close-popup></q-btn>
|
||||||
|
<q-btn flat label="Save" @click="() => saveExpression()"></q-btn>
|
||||||
|
</q-card-actions>
|
||||||
|
</q-card>
|
||||||
|
</template>
|
@ -10,7 +10,6 @@
|
|||||||
const stmt1Id = page.addRaw('x = y+3/4')
|
const stmt1Id = page.addRaw('x = y+3/4')
|
||||||
const stmt2Id = page.addRaw('y = 9')
|
const stmt2Id = page.addRaw('y = 9')
|
||||||
const evaluation = page.evaluate()
|
const evaluation = page.evaluate()
|
||||||
let renderVersion = 0
|
|
||||||
|
|
||||||
const stmt = page.getStatement(stmt1Id)
|
const stmt = page.getStatement(stmt1Id)
|
||||||
console.log({page, stmt1Id})
|
console.log({page, stmt1Id})
|
||||||
@ -69,6 +68,5 @@
|
|||||||
v-on:edit="() => stmt ? editStatement(stmt) : {}"
|
v-on:edit="() => stmt ? editStatement(stmt) : {}"
|
||||||
:statement="stmt"
|
:statement="stmt"
|
||||||
:evaluation="evaluation"
|
:evaluation="evaluation"
|
||||||
:render-version="renderVersion"
|
|
||||||
/>
|
/>
|
||||||
</template>
|
</template>
|
||||||
|
94
src/pages/VarDeclEditor.vue
Normal file
94
src/pages/VarDeclEditor.vue
Normal file
@ -0,0 +1,94 @@
|
|||||||
|
<script setup lang="ts">
|
||||||
|
import {ref} from 'vue'
|
||||||
|
import {MathStatement} from '../support/parse'
|
||||||
|
import {v4 as uuidv4} from 'uuid'
|
||||||
|
import Katex from '../components/Katex.vue'
|
||||||
|
import {StatementID} from '../types'
|
||||||
|
|
||||||
|
const emit = defineEmits<{
|
||||||
|
(eventName: 'save', statement: MathStatement): void,
|
||||||
|
}>()
|
||||||
|
|
||||||
|
const newVariablePreview = ref(MathStatement.temp('? = ?'))
|
||||||
|
let newVariablePreviewKey = ref(uuidv4())
|
||||||
|
const newVariableModalError = ref<string|undefined>(undefined)
|
||||||
|
const newVariableName = ref('')
|
||||||
|
const newVariableValue = ref('')
|
||||||
|
|
||||||
|
const updateNewVariablePreview = () => {
|
||||||
|
const previewStmt = MathStatement.temp(`${newVariableName.value} = ${newVariableValue.value}`)
|
||||||
|
if ( previewStmt.isValid() ) {
|
||||||
|
newVariablePreview.value = MathStatement.temp(`${newVariableName.value} = ${newVariableValue.value}`)
|
||||||
|
newVariablePreviewKey.value = uuidv4()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const validateNewVariable = () => {
|
||||||
|
if ( !newVariableName.value ) {
|
||||||
|
return 'Missing variable name'
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( !newVariableValue.value ) {
|
||||||
|
return 'Missing variable value'
|
||||||
|
}
|
||||||
|
|
||||||
|
const stmt = MathStatement.temp(`${newVariableName.value} = ${newVariableValue.value}`)
|
||||||
|
if ( !stmt.isValid() ) {
|
||||||
|
return 'Assignment is invalid'
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( !stmt.isNotRecursive() ) {
|
||||||
|
return 'A variable may not reference itself'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const saveNewVariable = () => {
|
||||||
|
newVariableModalError.value = validateNewVariable()
|
||||||
|
if ( newVariableModalError.value ) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
emit('save', new MathStatement(
|
||||||
|
uuidv4() as StatementID,
|
||||||
|
`${newVariableName.value} = ${newVariableValue.value}`
|
||||||
|
))
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<template>
|
||||||
|
<q-card>
|
||||||
|
<q-card-section>
|
||||||
|
<div style="display: flex; justify-content: center">
|
||||||
|
<Katex
|
||||||
|
:key="newVariablePreviewKey"
|
||||||
|
:statement="newVariablePreview"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</q-card-section>
|
||||||
|
<q-card-section v-if="newVariableModalError">
|
||||||
|
<div style="color: darkred; font-weight: bold">{{ newVariableModalError }}</div>
|
||||||
|
</q-card-section>
|
||||||
|
<q-card-section>
|
||||||
|
<q-input
|
||||||
|
v-model="newVariableName"
|
||||||
|
v-on:update:model-value="() => updateNewVariablePreview()"
|
||||||
|
outlined
|
||||||
|
autofocus
|
||||||
|
label="Variable name"
|
||||||
|
/>
|
||||||
|
<br>
|
||||||
|
<q-input
|
||||||
|
v-model="newVariableValue"
|
||||||
|
v-on:update:model-value="() => updateNewVariablePreview()"
|
||||||
|
outlined
|
||||||
|
type="textarea"
|
||||||
|
autogrow
|
||||||
|
label="Value"
|
||||||
|
/>
|
||||||
|
</q-card-section>
|
||||||
|
<q-card-actions align="right" class="text-primary">
|
||||||
|
<q-btn flat label="Cancel" v-close-popup></q-btn>
|
||||||
|
<q-btn flat label="Save" @click="() => saveNewVariable()"></q-btn>
|
||||||
|
</q-card-actions>
|
||||||
|
</q-card>
|
||||||
|
</template>
|
@ -16,6 +16,11 @@ export class MathPage {
|
|||||||
public readonly id: string,
|
public readonly id: string,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
|
/** Get all defined statements. */
|
||||||
|
getStatements(): MathStatement[] {
|
||||||
|
return Object.values(this.statements)
|
||||||
|
}
|
||||||
|
|
||||||
/** Get a statement by ID if it exists. */
|
/** Get a statement by ID if it exists. */
|
||||||
getStatement(id: StatementID): Maybe<MathStatement> {
|
getStatement(id: StatementID): Maybe<MathStatement> {
|
||||||
return this.statements[id]
|
return this.statements[id]
|
||||||
|
@ -324,4 +324,18 @@ export class MathStatement {
|
|||||||
uses(): math.SymbolNode[] {
|
uses(): math.SymbolNode[] {
|
||||||
return (new RValSymbolWalk()).walk(this.parse())
|
return (new RValSymbolWalk()).walk(this.parse())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Returns true if the definition is correctly non-recursive. */
|
||||||
|
isNotRecursive(): boolean {
|
||||||
|
const uses = this.uses()
|
||||||
|
const defines = this.defines()
|
||||||
|
|
||||||
|
for ( const define of defines ) {
|
||||||
|
if ( uses.some(x => x.name === define.name) ) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -9,6 +9,9 @@ export default defineConfig({
|
|||||||
'/api': {
|
'/api': {
|
||||||
target: 'http://localhost:8000/',
|
target: 'http://localhost:8000/',
|
||||||
},
|
},
|
||||||
|
'/auth': {
|
||||||
|
target: 'http://localhost:8000/',
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
plugins: [
|
plugins: [
|
||||||
|
Loading…
Reference in New Issue
Block a user