Fix Obsidian write conflicts
This commit is contained in:
parent
2c3d71b181
commit
ba060cf770
1 changed files with 21 additions and 5 deletions
|
|
@ -13,19 +13,26 @@ export class ObsidianFileSystemOperations implements FileSystemOperations {
|
||||||
}
|
}
|
||||||
|
|
||||||
public async read(path: RelativePath): Promise<Uint8Array> {
|
public async read(path: RelativePath): Promise<Uint8Array> {
|
||||||
|
path = normalizePath(path);
|
||||||
const view = this.workspace.getActiveViewOfType(MarkdownView);
|
const view = this.workspace.getActiveViewOfType(MarkdownView);
|
||||||
if (view?.file?.path === path) {
|
if (view?.file?.path === path) {
|
||||||
return new TextEncoder().encode(view.editor.getValue());
|
return new TextEncoder().encode(view.editor.getValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
return new Uint8Array(
|
return new Uint8Array(await this.vault.adapter.readBinary(path));
|
||||||
await this.vault.adapter.readBinary(normalizePath(path))
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async write(path: RelativePath, content: Uint8Array): Promise<void> {
|
public async write(path: RelativePath, content: Uint8Array): Promise<void> {
|
||||||
|
path = normalizePath(path);
|
||||||
|
|
||||||
|
const view = this.workspace.getActiveViewOfType(MarkdownView);
|
||||||
|
if (view?.file?.path === path) {
|
||||||
|
view.editor.setValue(new TextDecoder().decode(content));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
return this.vault.adapter.writeBinary(
|
return this.vault.adapter.writeBinary(
|
||||||
normalizePath(path),
|
path,
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
|
||||||
content.buffer as ArrayBuffer
|
content.buffer as ArrayBuffer
|
||||||
);
|
);
|
||||||
|
|
@ -35,7 +42,16 @@ export class ObsidianFileSystemOperations implements FileSystemOperations {
|
||||||
path: RelativePath,
|
path: RelativePath,
|
||||||
updater: (currentContent: string) => string
|
updater: (currentContent: string) => string
|
||||||
): Promise<string> {
|
): Promise<string> {
|
||||||
return this.vault.adapter.process(normalizePath(path), updater);
|
path = normalizePath(path);
|
||||||
|
|
||||||
|
const view = this.workspace.getActiveViewOfType(MarkdownView);
|
||||||
|
if (view?.file?.path === path) {
|
||||||
|
const result = updater(view.editor.getValue());
|
||||||
|
view.editor.setValue(result);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.vault.adapter.process(path, updater);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async getFileSize(path: RelativePath): Promise<number> {
|
public async getFileSize(path: RelativePath): Promise<number> {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue