Storing Node objects in NodeStore for single instance objects

This commit is contained in:
Magnus Åhall 2024-12-19 22:28:13 +01:00
parent d0150145ed
commit 41952df764
5 changed files with 187 additions and 68 deletions

View file

@ -28,18 +28,20 @@ export class NodeUI extends Component {
return
const node = this.node.value
document.title = node.Name
document.title = node.get('Name')
const nodeModified = this.nodeModified.value ? 'node-modified' : ''
return html`
<div id="crumbs">
<div class="crumbs">
<div id="crumbs" onclick=${()=>this.saveNode()}>
<div class="crumbs ${nodeModified}">
<div class="crumb" onclick=${()=>_notes2.current.goToNode(ROOT_NODE)}>Start</div>
<div class="crumb">Minnie</div>
<div class="crumb">Fluffy</div>
<div class="crumb">Chili</div>
</div>
</div>
<div id="name">${node.Name}</div>
<div id="name">${node.get('Name')}</div>
<${NodeContent} key=${node.UUID} node=${node} ref=${this.nodeContent} />
<div id="blank"></div>
`
@ -57,10 +59,6 @@ export class NodeUI extends Component {
html`<div class="crumb" onclick=${() => this.goToNode(node.ID)}>${node.Name}</div>`
).reverse())
let modified = ''
if (this.props.app.nodeModified.value)
modified = 'modified'
// Page to display
let page = ''
@ -145,15 +143,27 @@ export class NodeUI extends Component {
}//}}}
async componentDidMount() {//{{{
_notes2.current.goToNode(this.props.startNode.UUID, true)
_notes2.current.tree.expandToTrunk(this.props.startNode)
}//}}}
setNode(node) {//{{{
this.nodeModified.value = false
this.node.value = node
}//}}}
async saveNode() {//{{{
if (!this.nodeModified.value)
return
await nodeStore.copyToNodesHistory(this.node.value)
// Prepares the node object for saving.
// Sets Updated value to current date and time.
const node = this.node.value
node.save()
await nodeStore.add([node])
this.nodeModified.value = false
}//}}}
keyHandler(evt) {//{{{
return
let handled = true
// All keybindings is Alt+Shift, since the popular browsers at the time (2023) allows to override thees.
@ -163,6 +173,7 @@ export class NodeUI extends Component {
return
switch (evt.key.toUpperCase()) {
/*
case 'C':
this.showPage('node')
break
@ -183,12 +194,14 @@ export class NodeUI extends Component {
this.showPage('node-properties')
break
*/
case 'S':
if (this.page.value == 'node')
if (this.page.value === 'node')
this.saveNode()
else if (this.page.value == 'node-properties')
else if (this.page.value === 'node-properties')
this.nodeProperties.current.save()
break
/*
case 'U':
this.showPage('upload')
@ -197,6 +210,7 @@ export class NodeUI extends Component {
case 'F':
this.showPage('search')
break
*/
default:
handled = false
@ -262,8 +276,7 @@ class NodeContent extends Component {
}//}}}
contentChanged(evt) {//{{{
_notes2.current.nodeUI.current.nodeModified.value = true
const content = evt.target.value
this.props.node.setContent(content)
this.props.node.setContent(evt.target.value)
this.resize()
}//}}}
resize() {//{{{
@ -274,6 +287,11 @@ class NodeContent extends Component {
}
export class Node {
static sort(a, b) {//{{{
if (a.data.Name < b.data.Name) return -1
if (a.data.Name > b.data.Name) return 0
return 0
}//}}}
constructor(nodeData, level) {//{{{
this.Level = level
this.data = nodeData
@ -283,6 +301,10 @@ export class Node {
this._children_fetched = false
this.Children = []
this._content = this.data.Content
this._modified = false
/*
this.RenderMarkdown = signal(nodeData.RenderMarkdown)
this.Markdown = false
@ -298,6 +320,7 @@ export class Node {
// Used to expand the crumbs upon site loading.
*/
}//}}}
get(prop) {//{{{
return this.data[prop]
}//}}}
@ -314,15 +337,17 @@ export class Node {
this.Children = await nodeStore.getTreeNodes(this.UUID, this.Level + 1)
this._children_fetched = true
return this.Children
}//}}}
content() {//{{{
/* TODO - implement crypto
if (this.CryptoKeyID != 0 && !this._decrypted)
this.#decrypt()
*/
return this.data.Content
this.modified = true
return this._content
}//}}}
setContent(new_content) {//{{{
this._content = new_content
/* TODO - implement crypto
@ -334,10 +359,10 @@ export class Node {
this._decrypted = true
*/
}//}}}
static sort(a, b) {//{{{
if (a.data.Name < b.data.Name) return -1
if (a.data.Name > b.data.Name) return 0
return 0
save() {//{{{
this.data.Content = this._content
this.data.Updated = new Date().toISOString()
this._modified = false
}//}}}
}