From bcd7628c0aa0371d156e27c0810dbd515d886628 Mon Sep 17 00:00:00 2001 From: garrettmills Date: Sun, 10 Apr 2022 03:59:37 -0500 Subject: [PATCH] Add logic for (de)serializing pages --- src/App.vue | 2 +- src/pages/Editor.vue | 113 ++++++++++++++++++++++++++++++++++++++----- src/support/page.ts | 17 +++++++ src/support/parse.ts | 13 +++++ src/support/types.ts | 39 +++++++++++++++ 5 files changed, 171 insertions(+), 13 deletions(-) diff --git a/src/App.vue b/src/App.vue index c8edac5..a153b3b 100644 --- a/src/App.vue +++ b/src/App.vue @@ -63,7 +63,7 @@ Dark.set(true) Crystal Math Worktable - + diff --git a/src/pages/Editor.vue b/src/pages/Editor.vue index 12ae705..a448107 100644 --- a/src/pages/Editor.vue +++ b/src/pages/Editor.vue @@ -18,7 +18,12 @@ import { stepX, stepY } from '../support/const' import { checkLoggedIn, loggedOut } from '../support/auth' import router from '../router' -const math = new MathPage(uuidv4()); +const props = defineProps<{ + pageId?: number, +}>() + +const math = ref(new MathPage(uuidv4())); +const pageTitle = ref('New Page'); const statements = ref([]); const evaluation = ref(); const statementsKey = ref(uuidv4()); @@ -99,9 +104,9 @@ const openEditFunctionModal = () => { } const updateStatements = () => { - statements.value = math.getStatements(); + statements.value = math.value.getStatements(); try { - evaluation.value = math.evaluate() + evaluation.value = math.value.evaluate() const variableValues: ({ name: string, value: string })[] = [] for (const name in evaluation.value!.variables) { @@ -124,7 +129,7 @@ const updateStatements = () => { variableListingRows.value = variableValues const functionValues: ({ name: string, value: string })[] = [] - for (const stmt of math.functions()) { + for (const stmt of math.value.functions()) { const node = stmt.parse() as math.FunctionAssignmentNode functionValues.push({ name: node.name, @@ -145,19 +150,19 @@ onMounted(() => { }) const saveNewVariable = (stmt: MathStatement) => { - math.addStatement(stmt) + math.value.addStatement(stmt) newVariableModalOpen.value = false updateStatements() }; const saveNewExpression = (stmt: MathStatement) => { - math.addStatement(stmt) + math.value.addStatement(stmt) newExpressionModalOpen.value = false updateStatements() }; const saveNewFunction = (stmt: MathStatement) => { - math.addStatement(stmt) + math.value.addStatement(stmt) newFunctionModalOpen.value = false updateStatements() } @@ -175,7 +180,7 @@ const editStatement = (stmt: MathStatement) => { } const removeStatement = (stmt: MathStatement) => { - math.removeStatement(stmt.id) + math.value.removeStatement(stmt.id) updateStatements() } @@ -323,6 +328,79 @@ onMounted(() => { } }) +const serialize = () => { + return { + title: pageTitle.value, + page: math.value.serialize(), + textBoxes: richTextStatements.value.map(x => x.serialize()), + chartBoxes: chartBoxes.value.map(x => x.serialize()), + imageBoxes: images.value.map(x => x.serialize()), + } +} + +const editorPageId = ref() +const saveEditorPage = async () => { + const params = { + serialData: JSON.stringify(serialize()), + } as any + + if ( editorPageId.value ) { + params.pageId = editorPageId.value + } + + const response = await fetch('/api/editor/page', { + method: 'POST', + headers: { + 'Accept': 'application/json', + 'Content-Type': 'application/json' + }, + body: JSON.stringify(params), + }) + + const result = (await response.json()) as unknown as any + if (!result.success) { + return alert('Failed to save page: ' + result.message) + } + + editorPageId.value = result.data.pageId +} + +const loadEditorPage = async () => { + if ( !editorPageId.value ) { + return + } + + const pageId = editorPageId.value + const response = await fetch(`/api/editor/page?pageId=${pageId}`, { + method: 'GET', + headers: { + 'Accept': 'application/json', + }, + }) + + const result = (await response.json()) as unknown as any + if (!result.success) { + return alert('Failed to load page: ' + result.message) + } + + const serialData = result.data.serialData + const serialized = JSON.parse(serialData) as any + math.value = MathPage.deserialize(serialized.page) + richTextStatements.value = serialized.textBoxes.map((box: any) => RichTextBox.deserialize(box)) + chartBoxes.value = serialized.chartBoxes.map((box: any) => ChartBox.deserialize(box)) + images.value = serialized.imageBoxes.map((box: any) => ImageContainer.deserialize(box)) + pageTitle.value = serialized.title ?? 'New Page' + updateStatements() + chartBoxKey.value = uuidv4() +} + +onMounted(() => { + if ( props.pageId ) { + editorPageId.value = props.pageId + loadEditorPage() + } +}) +