This commit is contained in:
Thomas 2022-04-09 18:08:04 -05:00
commit ee49593055
8 changed files with 289 additions and 9 deletions

View File

@ -6,12 +6,11 @@ import Katex from './Katex.vue'
const props = defineProps<{
statement: MathStatement,
evaluation: EvaluationResult,
renderVersion: number,
evaluation?: EvaluationResult,
}>()
const getValueStatement = (): Maybe<MathStatement> => {
const value = props.evaluation.statements[props.statement.id]
const value = props.evaluation?.statements?.[props.statement.id]
if ( value ) {
return MathStatement.temp(String(value))
}
@ -48,7 +47,7 @@ computed(() => value = getValueStatement())
</style>
<template>
<div class="math-statement">
<div class="math-statement" style="background: white">
<div class="content">
<Katex :statement="statement" size="big"/>
<div class="result" v-if="value">

View File

@ -1,14 +1,58 @@
<script setup lang="ts">
import { ref } from "vue";
import WrapperBox from "../components/WrapperBox.vue";
import TextBox from "../components/TextBox.vue";
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'
import TextBox from '../components/TextBox.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()
}
/*
Rich Text Stuff
*/
@ -67,6 +111,51 @@ function richUpdateValue() {
</q-drawer>
<q-page-container id="editor" style="display: flex">
<!-- <WrapperBox />-->
<span v-for="statement in statements">
<Draggable
:grid="[25, 25]"
>
<Statement
:key="statementsKey"
:statement="statement"
:evaluation="evaluation"
/>
</Draggable>
</span>
<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-dialog v-model="richEditModal">
<q-card>
<q-editor v-model="richEditExpression" min-height="5rem" />

View 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>

View File

@ -10,7 +10,6 @@
const stmt1Id = page.addRaw('x = y+3/4')
const stmt2Id = page.addRaw('y = 9')
const evaluation = page.evaluate()
let renderVersion = 0
const stmt = page.getStatement(stmt1Id)
console.log({page, stmt1Id})
@ -69,6 +68,5 @@
v-on:edit="() => stmt ? editStatement(stmt) : {}"
:statement="stmt"
:evaluation="evaluation"
:render-version="renderVersion"
/>
</template>

View 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>

View File

@ -16,6 +16,11 @@ export class MathPage {
public readonly id: string,
) {}
/** Get all defined statements. */
getStatements(): MathStatement[] {
return Object.values(this.statements)
}
/** Get a statement by ID if it exists. */
getStatement(id: StatementID): Maybe<MathStatement> {
return this.statements[id]

View File

@ -324,4 +324,18 @@ export class MathStatement {
uses(): math.SymbolNode[] {
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
}
}

View File

@ -9,6 +9,9 @@ export default defineConfig({
'/api': {
target: 'http://localhost:8000/',
},
'/auth': {
target: 'http://localhost:8000/',
},
},
},
plugins: [