fix: obsidian 1.9.0 support for format on save

This commit is contained in:
Dylan Armstrong 2025-05-26 13:59:05 +02:00
parent 3573e4a4ef
commit fbdfcaee0e
No known key found for this signature in database
6 changed files with 348 additions and 523 deletions

View file

@ -7,5 +7,5 @@
"isDesktopOnly": false,
"minAppVersion": "0.15.0",
"name": "Format Automatically with Prettier",
"version": "0.1.7"
"version": "0.1.8"
}

View file

@ -6,18 +6,17 @@
},
"description": "This is a formatting plugin for Obsidian (https://obsidian.md)",
"devDependencies": {
"@codemirror/view": "^6.36.5",
"@dylanarmstrong/eslint-config": "^0.8.0",
"@eslint/js": "^9.23.0",
"@types/node": "^22.13.17",
"@eslint/js": "^9.27.0",
"@types/node": "^22.15.21",
"builtin-modules": "5.0.0",
"esbuild": "0.25.2",
"eslint": "^9.23.0",
"eslint-config-prettier": "^10.1.1",
"eslint-plugin-unicorn": "^58.0.0",
"esbuild": "0.25.4",
"eslint": "^9.27.0",
"eslint-config-prettier": "^10.1.5",
"eslint-plugin-unicorn": "^59.0.1",
"tslib": "2.8.1",
"typescript": "5.8.2",
"typescript-eslint": "^8.29.0"
"typescript": "5.8.3",
"typescript-eslint": "^8.32.1"
},
"keywords": [
"obsidian",

File diff suppressed because it is too large Load diff

View file

@ -1,89 +1,18 @@
// Modified from https://github.com/platers/obsidian-linter/blob/8fbaa410907b87965e471ca46bb2ec6fb354b05e/src/typings/obsidian-ex.d.ts
import 'obsidian';
import { Command } from 'obsidian';
import { EditorView } from '@codemirror/view';
export interface ObsidianCommandInterface {
executeCommandById(id: string): void;
commands: {
'editor:save-file': {
callback(): void;
};
};
listCommands(): Command[];
}
// allows for the removal of the any cast by defining some extra properties for Typescript so it knows these properties exist
declare module 'obsidian' {
export interface ViewStateResult {
/**
* Set this to true to indicate that there is a state change which should be recorded in the navigation history.
* @public
*/
history: boolean;
}
interface FileView {
/**
* @public
*/
allowNoFile: boolean;
/**
* File views can be navigated by default.
* @inheritDoc
* @public
*/
navigation: boolean;
/**
* @public
*/
getDisplayText(): string;
/**
* @public
*/
onload(): void;
/**
* @public
*/
getState(): unknown;
/**
* @public
*/
setState(state: unknown, result: ViewStateResult): Promise<void>;
}
interface Workspace {
getActiveFileView: () => FileView;
}
interface App {
commands: ObsidianCommandInterface;
dom: {
appContainerEl: HTMLElement;
commands: {
commands: {
'editor:save-file': {
callback?: () => void;
checkCallback?: (checking: boolean) => void;
};
};
};
workspace: Workspace;
}
interface Vault {
getConfig(id: string): boolean;
}
interface Editor {
/**
* CodeMirror editor instance
*/
cm?: EditorView;
}
}
declare global {
interface Window {
CodeMirrorAdapter: {
commands: {
save(): void;
};
};
}
}

View file

@ -38,7 +38,8 @@ const fromOffset = (offset: number, text: string): EditorPosition => {
export default class PrettierPlugin extends Plugin {
settings: PrettierPluginSettings;
originalCallback: () => void | undefined;
originalCheckCallback?: (checking: boolean) => boolean | void;
originalCallback?: () => void;
private async format() {
const editor = this.app.workspace.activeEditor?.editor;
@ -85,9 +86,18 @@ export default class PrettierPlugin extends Plugin {
}
this.originalCallback = handleSave.callback;
this.originalCheckCallback = handleSave.checkCallback;
handleSave.checkCallback = (checking: boolean) => {
// If checkCallback is defined (i.e. obsidian > 1.9.0)
if (this.originalCheckCallback) {
this.originalCheckCallback.apply(handleSave, [checking]);
} else if (this.originalCallback) {
// Otherwise call original callback method.
// Note: this won't be called normally if checkCallback is defined
this.originalCallback.apply(handleSave);
}
handleSave.callback = () => {
this.originalCallback?.apply(handleSave);
this.onFileSave();
};
}
@ -95,14 +105,16 @@ export default class PrettierPlugin extends Plugin {
private revertSave() {
const handleSave = this.getSave();
if (!handleSave || !this.originalCallback) {
if (!handleSave || !this.originalCheckCallback) {
return;
}
handleSave.callback = this.originalCallback;
handleSave.checkCallback = this.originalCheckCallback;
}
async onload() {
// eslint-disable-next-line no-console
console.log('loading prettier-format');
await this.loadSettings();
this.patchSave();

View file

@ -1,17 +1,17 @@
{
"compilerOptions": {
"allowJs": true,
"baseUrl": ".",
"importHelpers": true,
"inlineSourceMap": true,
"inlineSources": true,
"isolatedModules": true,
"lib": ["DOM", "ES5", "ES6", "ES7"],
"module": "ESNext",
"moduleResolution": "node",
"noImplicitAny": true,
"strictNullChecks": true,
"target": "ES6"
},
"include": ["**/*.ts"]
"compilerOptions": {
"allowJs": true,
"baseUrl": ".",
"importHelpers": true,
"inlineSourceMap": true,
"inlineSources": true,
"isolatedModules": true,
"lib": ["DOM", "ES5", "ES6", "ES7"],
"module": "ESNext",
"moduleResolution": "node",
"noImplicitAny": true,
"strictNullChecks": true,
"target": "ES6"
},
"include": ["**/*.ts"]
}