Skip to content

Commit 00bdf02

Browse files
Fixed invalid canvas file if empty. Reduced risk of corrupting canvas
1 parent 7b399cd commit 00bdf02

File tree

4 files changed

+19
-12
lines changed

4 files changed

+19
-12
lines changed

src/canvas-extensions/advanced-styles/edge-styles.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,13 @@ export default class EdgeStylesExtension extends CanvasExtension {
8585
for (const edge of selectedEdges) {
8686
const edgeData = edge.getData()
8787

88+
const newStyleAttributes = { ...edgeData.styleAttributes }
89+
if (value !== null) newStyleAttributes[attribute.datasetKey] = value
90+
else delete newStyleAttributes[attribute.datasetKey]
91+
8892
edge.setData({
8993
...edgeData,
90-
styleAttributes: {
91-
...edgeData.styleAttributes,
92-
[attribute.datasetKey]: value
93-
}
94+
styleAttributes: newStyleAttributes
9495
})
9596
}
9697

src/canvas-extensions/advanced-styles/node-styles.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,13 @@ export default class NodeStylesExtension extends CanvasExtension {
4747
// Only apply the attribute if the node type is allowed
4848
if (attribute.nodeTypes && !attribute.nodeTypes.includes(nodeData.type)) continue
4949

50+
const newStyleAttributes = { ...nodeData.styleAttributes }
51+
if (value !== null) newStyleAttributes[attribute.datasetKey] = value
52+
else delete newStyleAttributes[attribute.datasetKey]
53+
5054
node.setData({
5155
...nodeData,
52-
styleAttributes: {
53-
...nodeData.styleAttributes,
54-
[attribute.datasetKey]: value
55-
}
56+
styleAttributes: newStyleAttributes
5657
})
5758
}
5859

src/canvas-extensions/portals-canvas-extension.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,10 @@ export default class PortalsCanvasExtension extends CanvasExtension {
363363
return addedData
364364
}
365365

366-
const portalFileData = JSON.parse(await this.plugin.app.vault.cachedRead(portalFile))
366+
const portalFileDataString = await this.plugin.app.vault.cachedRead(portalFile)
367+
if (portalFileDataString === '') return addedData
368+
369+
const portalFileData = JSON.parse(portalFileDataString) as CanvasData
367370
if (!portalFileData) {
368371
portalNodeData.portalToFile = undefined
369372
return addedData
@@ -412,6 +415,8 @@ export default class PortalsCanvasExtension extends CanvasExtension {
412415
const toRefNode = Object.entries(portalNodeData.portalIdMaps.nodeIdMap)
413416
.find(([_refNodeId, nodeId]) => nodeId === edgeDataFromPortal.toNode)?.[0]
414417

418+
if (!fromRefNode || !toRefNode) continue
419+
415420
addedData.edges.push({
416421
...edgeDataFromPortal,
417422
id: refEdgeId,

src/core/canvas-patcher.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,17 @@ export default class CanvasPatcher {
4545
return JSON.stringify(canvasData, null, 2)
4646
},
4747
setViewData: (next: any) => function (json: string, ...args: any) {
48-
let validJson = json
48+
let validJson = json !== '' ? json : '{}'
4949
let parsedJson
5050

5151
// Check for SyntaxError
52-
try { parsedJson = JSON.parse(json) }
52+
try { parsedJson = JSON.parse(validJson) }
5353
catch (e) {
5454
// Invalid JSON
5555
that.plugin.createFileSnapshot(this.file.path, json)
5656

5757
// Try to parse it with trailing commas
58-
parsedJson = JSONC.parse(json)
58+
parsedJson = JSONC.parse(validJson)
5959
validJson = JSON.stringify(parsedJson, null, 2)
6060
}
6161

0 commit comments

Comments
 (0)