Skip to content

Commit a37a992

Browse files
Improved auto-fix of corrupted files.
1 parent c1ff06a commit a37a992

File tree

4 files changed

+17
-16
lines changed

4 files changed

+17
-16
lines changed

src/@types/Obsidian.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ declare module "obsidian" {
1818
*/
1919
lastEvent: UserEvent | null
2020

21+
internalPlugins: any
22+
2123
// Custom
2224
/** @public */
2325
workspace: Workspace & ExtendedWorkspace

src/core/canvas-patcher.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ export default class CanvasPatcher {
3737
) as CanvasView
3838

3939
// Patch canvas view
40-
const fixBrokenCanvasFiles = this.plugin.settings.getSetting('fixBrokenCanvasFiles')
4140
PatchHelper.patchObjectPrototype(this.plugin, canvasView, {
4241
getViewData: (_next: any) => function (..._args: any) {
4342
const canvasData = this.canvas.getData()
@@ -52,10 +51,12 @@ export default class CanvasPatcher {
5251
// Check for SyntaxError
5352
try { parsedJson = JSON.parse(json) }
5453
catch (e) {
55-
if (fixBrokenCanvasFiles) {
56-
parsedJson = JSONC.parse(json)
57-
validJson = JSON.stringify(parsedJson, null, 2)
58-
}
54+
// Invalid JSON
55+
that.plugin.createFileSnapshot(this.file.path, json)
56+
57+
// Try to parse it with trailing commas
58+
parsedJson = JSONC.parse(json)
59+
validJson = JSON.stringify(parsedJson, null, 2)
5960
}
6061

6162
const result = next.call(this, validJson, ...args)

src/main.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,16 +88,23 @@ export default class AdvancedCanvasPlugin extends Plugin {
8888

8989
onunload() {}
9090

91-
getCurrentCanvasView(): CanvasView|null {
91+
getCurrentCanvasView(): CanvasView | null {
9292
const canvasView = this.app.workspace.getActiveViewOfType(ItemView)
9393
if (canvasView?.getViewType() !== 'canvas') return null
9494
return canvasView as CanvasView
9595
}
9696

97-
getCurrentCanvas(): Canvas|null {
97+
getCurrentCanvas(): Canvas | null {
9898
return this.getCurrentCanvasView()?.canvas || null
9999
}
100100

101+
createFileSnapshot(path: string, content: string) {
102+
const fileRecoveryPlugin = this.app.internalPlugins.plugins['file-recovery']?.instance
103+
if (!fileRecoveryPlugin) return
104+
105+
fileRecoveryPlugin.forceAdd(path, content)
106+
}
107+
101108
// this.app.plugins.plugins["advanced-canvas"].enableDebugMode()
102109
enableDebugMode() {
103110
if (this.debugHelper) return

src/settings.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ export interface AdvancedCanvasPluginSettings {
1515
defaultFileNodeWidth: number
1616
defaultFileNodeHeight: number
1717

18-
fixBrokenCanvasFiles: boolean
1918
performanceOptimizationEnabled: boolean
2019

2120
nodeStylingFeatureEnabled: boolean
@@ -68,7 +67,6 @@ export const DEFAULT_SETTINGS: Partial<AdvancedCanvasPluginSettings> = {
6867
defaultFileNodeWidth: 400,
6968
defaultFileNodeHeight: 400,
7069

71-
fixBrokenCanvasFiles: false,
7270
performanceOptimizationEnabled: false,
7371

7472
nodeStylingFeatureEnabled: true,
@@ -221,13 +219,6 @@ export class AdvancedCanvasPluginSettingTab extends PluginSettingTab {
221219
.onChange(async (value) => await this.settingsManager.setSetting({ defaultFileNodeHeight: Math.max(1, parseInt(value)) }))
222220
)
223221

224-
this.createFeatureHeading(
225-
containerEl,
226-
"Fix broken canvas files",
227-
"Fix broken canvas files by removing trailing commas. (This is a workaround for a bug in Obsidian. It happens if you edit an advanced canvas file without the plugin enabled. I suggest you to only enable this if you encounter the error: Failed to open \"\")",
228-
'fixBrokenCanvasFiles'
229-
)
230-
231222
this.createFeatureHeading(
232223
containerEl,
233224
"Performance optimization",

0 commit comments

Comments
 (0)