Skip to content

Commit

Permalink
Fixed hot reload of portals causing duplicate history entry
Browse files Browse the repository at this point in the history
  • Loading branch information
Developer-Mike committed Oct 19, 2024
1 parent 6953a2a commit 315a1f0
Showing 1 changed file with 20 additions and 14 deletions.
34 changes: 20 additions & 14 deletions src/canvas-extensions/portals-canvas-extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,26 @@ export default class PortalsCanvasExtension extends CanvasExtension {
isEnabled() { return 'portalsFeatureEnabled' as const }

init() {
this.plugin.registerEvent(this.plugin.app.vault.on('modify', (file: TFile) => {
const canvases = this.plugin.app.workspace.getLeavesOfType('canvas').map(leaf => (leaf.view as CanvasView).canvas)

for (const canvas of canvases) {
const hasPortalsToFile = canvas.getData().nodes.filter(nodeData =>
nodeData.type === 'file' &&
nodeData.portalToFile === file.path
).length > 0

// Update whole canvas data
if (hasPortalsToFile) {
canvas.setData(canvas.getData())

// Maintain history
canvas.history.current--
canvas.history.data.pop()
}
}
}))

this.plugin.registerEvent(this.plugin.app.workspace.on(
CanvasEvent.PopupMenuCreated,
(canvas: Canvas) => this.updatePopupMenu(canvas)
Expand Down Expand Up @@ -57,20 +77,6 @@ export default class PortalsCanvasExtension extends CanvasExtension {
})
}
))

this.plugin.registerEvent(this.plugin.app.vault.on('modify', (file: TFile) => {
const canvases = this.plugin.app.workspace.getLeavesOfType('canvas').map(leaf => (leaf.view as CanvasView).canvas)

for (const canvas of canvases) {
const hasPortalsToFile = canvas.getData().nodes.filter(nodeData =>
nodeData.type === 'file' &&
nodeData.portalToFile === file.path
).length > 0

// Update whole canvas data
if (hasPortalsToFile) canvas.setData(canvas.getData())
}
}))
}

private updatePopupMenu(canvas: Canvas) {
Expand Down

0 comments on commit 315a1f0

Please sign in to comment.