generated from obsidianmd/obsidian-sample-plugin
-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #147 from decaf-dev/dev
1.19.0
- Loading branch information
Showing
19 changed files
with
532 additions
and
197 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import { App, TFile } from "obsidian"; | ||
import { writable } from "svelte/store"; | ||
|
||
type FileContentStore = Record<string, string | null>; | ||
|
||
interface FileContent { | ||
path: string; | ||
content: string | null; | ||
} | ||
|
||
function createFileContentStore() { | ||
const { subscribe, set, update } = writable<FileContentStore>({}); | ||
|
||
async function load(app: App) { | ||
const promises: Promise<FileContent>[] = []; | ||
|
||
const files = app.vault.getMarkdownFiles(); | ||
for (let file of files) { | ||
promises.push( | ||
(async () => { | ||
const { extension } = file; | ||
if (extension === "md") { | ||
const content = await app.vault.cachedRead(file); | ||
return { | ||
path: file.path, | ||
content, | ||
}; | ||
} | ||
return { | ||
path: file.path, | ||
content: null, | ||
}; | ||
})() | ||
); | ||
} | ||
|
||
const results = await Promise.all(promises); | ||
|
||
const contentForFiles = results.reduce( | ||
(acc: Record<string, string | null>, file) => { | ||
const { path, content } = file; | ||
acc[path] = content; | ||
return acc; | ||
}, | ||
{} | ||
); | ||
set(contentForFiles); | ||
} | ||
|
||
async function handleFileCreate(app: App, file: TFile) { | ||
let content: string | null = null; | ||
if (file.extension === "md") { | ||
content = await app.vault.cachedRead(file); | ||
} | ||
|
||
update((files) => { | ||
// Create a shallow copy of the files object to ensure reactivity | ||
return { ...files, [file.path]: content }; | ||
}); | ||
} | ||
|
||
function handleFileRename(oldPath: string, newPath: string) { | ||
update((files) => { | ||
if (files.hasOwnProperty(oldPath)) { | ||
// Create a shallow copy of the files object | ||
const { [oldPath]: value, ...newFiles } = files; | ||
newFiles[newPath] = value; | ||
return newFiles; | ||
} | ||
// No change if the old path does not exist | ||
return files; | ||
}); | ||
} | ||
|
||
function handleFileModify(path: string, newContent: string | null) { | ||
update((store) => { | ||
return { | ||
...store, | ||
[path]: newContent, | ||
}; | ||
}); | ||
} | ||
|
||
function handleFileDelete(path: string) { | ||
update((files) => { | ||
// Create a shallow copy and delete the property | ||
const { [path]: _, ...newFiles } = files; | ||
return newFiles; | ||
}); | ||
} | ||
|
||
return { | ||
load, | ||
subscribe, | ||
onFileCreate: handleFileCreate, | ||
onFileModify: handleFileModify, | ||
onFileRename: handleFileRename, | ||
onFileDelete: handleFileDelete, | ||
}; | ||
} | ||
|
||
export const fileContentStore = createFileContentStore(); |
Oops, something went wrong.