From 95a26e67d5409eca7b0de9e6c046b5f67fbac30f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magnus=20=C3=85hall?= Date: Wed, 10 Jun 2026 16:37:33 +0200 Subject: [PATCH] Better node saving/history --- static/js/app.mjs | 4 --- static/js/page_node.mjs | 56 +++++++++++++++++++++++------------------ static/js/sync.mjs | 3 +-- 3 files changed, 32 insertions(+), 31 deletions(-) diff --git a/static/js/app.mjs b/static/js/app.mjs index 38aebd4..4a1faec 100644 --- a/static/js/app.mjs +++ b/static/js/app.mjs @@ -162,10 +162,6 @@ export class App { const nn = Node.create(name, this.currentNode.UUID) nn.save() - - nodeStore.sendQueue.add(nn) - nodeStore.add([nn]) - }//}}} async goToNode(nodeUUID, dontPush, dontExpand) {//{{{ if (nodeUUID === null || nodeUUID === undefined) diff --git a/static/js/page_node.mjs b/static/js/page_node.mjs index 2005816..8f4feb1 100644 --- a/static/js/page_node.mjs +++ b/static/js/page_node.mjs @@ -115,31 +115,9 @@ export class N2PageNodeUI extends CustomHTMLElement { if (!this.node.isModified()) return - /* The node history is a local store for node history. - * This could be provisioned from the server or cleared if - * deemed unnecessary. - * - * The send queue is what will be sent back to the server - * to have a recorded history of the notes. - * - * A setting to be implemented in the future could be to - * not save the history locally at all. */ - - // The node is still in its old state and will present - // the unmodified content to the node store. - const history = nodeStore.nodesHistory.add(this.node) - - // Prepares the node object for saving. - // Sets Updated value to current date and time. + // node.save takes care of both "nodes" and "nodes_history" stores, also adds it to send queue. + // Sets "Updated" value to current date and time and generates a new history UUID. await this.node.save() - - // Updated node is added to the send queue to be stored on server. - const sendQueue = nodeStore.sendQueue.add(this.node) - - // Updated node is saved to the primary node store. - const nodeStoreAdding = nodeStore.add([this.node]) - - await Promise.all([history, sendQueue, nodeStoreAdding]) }// }}} contentChanged(event) {//{{{ @@ -306,7 +284,7 @@ export class Node { return 0 }//}}} static create(name, parentUUID) {// {{{ - return new Node({ + const node = new Node({ UUID: uuidv7(), Created: (new Date()).toISOString(), Content: '', @@ -315,6 +293,12 @@ export class Node { Markdown: false, History: false, }) + + // Newly created node (not constructed from existing data) is considered modified + // since node.save returns early if it isn't modified. + node._modified = true + + return node }// }}} constructor(nodeData, level) {//{{{ @@ -431,6 +415,28 @@ export class Node { // the ancestry path could be interesting. const ancestors = await nodeStore.getNodeAncestry(this) this.data.Ancestors = ancestors.map(a => a.get('Name')).reverse() + + /* The node history is a local store for node history. + * This could be provisioned from the server or cleared if + * deemed unnecessary. + * + * The send queue is what will be sent back to the server + * to have a recorded history of the notes. + * + * A setting to be implemented in the future could be to + * not save the history locally at all. */ + + // Current node is added to history. It will be duplicated with the "nodes" store + // for simplicity, to hopefully avoid bugs. + const history = nodeStore.nodesHistory.add(this) + + // Updated node is added to the send queue to be stored on server. + const sendQueue = nodeStore.sendQueue.add(this) + + // Updated node is saved to the primary node store. + const nodeStoreAdding = nodeStore.add([this]) + + return Promise.all([history, sendQueue, nodeStoreAdding]) }//}}} } diff --git a/static/js/sync.mjs b/static/js/sync.mjs index 35485eb..b6328aa 100644 --- a/static/js/sync.mjs +++ b/static/js/sync.mjs @@ -89,7 +89,7 @@ export class Sync { nodeStore.setAppState('latest_sync_node', currMax) } catch (e) { - console.log('sync node tree', e) + console.error('sync node tree', e) } finally { syncEnd = Date.now() const duration = (syncEnd - syncStart) / 1000 @@ -235,7 +235,6 @@ export class N2SyncProgress extends CustomHTMLElement { this.elDownloadTransferred.innerText = this.state.nodesDowloaded this.elDownloadTotal.innerText = this.state.nodesToDownload - console.log('setting elUploadTransferred', this.state.nodesUploaded) this.elUploadTransferred.innerText = this.state.nodesUploaded this.elUploadTotal.innerText = this.state.nodesToUpload }//}}}