Skip to content

Commit

Permalink
Merge pull request #147 from decaf-dev/dev
Browse files Browse the repository at this point in the history
1.19.0
  • Loading branch information
decaf-dev authored Jul 6, 2024
2 parents 99065ea + 0d6a6f1 commit f509c8d
Show file tree
Hide file tree
Showing 19 changed files with 532 additions and 197 deletions.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "vault-explorer",
"name": "Vault Explorer",
"version": "1.18.2",
"version": "1.19.0",
"minAppVersion": "1.4.13",
"description": "Explore your vault in visual format",
"author": "DecafDev",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "obsidian-vault-explorer",
"version": "1.18.2",
"version": "1.19.0",
"description": "Explore your vault in visual format",
"main": "main.js",
"scripts": {
Expand Down
4 changes: 2 additions & 2 deletions src/svelte/app/components/feed-card.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
import EventManager from "src/event/event-manager";
import VaultExplorerPlugin from "src/main";
import store from "src/svelte/shared/services/store";
import { formatBearTime } from "../services/time-utils";
import { formatBearTime } from "../services/utils/time-utils";
import Stack from "src/svelte/shared/components/stack.svelte";
import Tag from "src/svelte/shared/components/tag.svelte";
import { removeFrontmatterBlock } from "../services/frontmatter-utils";
import { removeFrontmatterBlock } from "../services/utils/frontmatter-utils";
export let name: string;
export let path: string;
Expand Down
2 changes: 1 addition & 1 deletion src/svelte/app/components/grid-card.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import Wrap from "src/svelte/shared/components/wrap.svelte";
import Stack from "src/svelte/shared/components/stack.svelte";
import { onMount } from "svelte";
import { getScrollAmount } from "../services/scroll-utils";
import { getScrollAmount } from "../services/utils/scroll-utils";
import { WordBreak } from "src/types";
import { HOVER_LINK_SOURCE_ID } from "src/constants";
import EventManager from "src/event/event-manager";
Expand Down
2 changes: 1 addition & 1 deletion src/svelte/app/components/group-tag-list.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import GroupTag from "./group-tag.svelte";
import Stack from "src/svelte/shared/components/stack.svelte";
import ScrollButton from "src/svelte/shared/components/scroll-button.svelte";
import { getScrollAmount } from "../services/scroll-utils";
import { getScrollAmount } from "../services/utils/scroll-utils";
import { onMount } from "svelte";
import store from "src/svelte/shared/services/store";
import VaultExplorerPlugin from "src/main";
Expand Down
6 changes: 6 additions & 0 deletions src/svelte/app/components/sort-filter.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@
item.setChecked(value === "modified-asc");
item.onClick(() => handleValueChange("modified-asc"));
});
menu.addSeparator();
menu.addItem((item) => {
item.setTitle("Random");
item.setChecked(value === "random");
item.onClick(() => handleValueChange("random"));
});
menu.showAtMouseEvent(nativeEvent);
}
</script>
Expand Down
94 changes: 43 additions & 51 deletions src/svelte/app/index.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,17 @@
import { filterBySearch } from "./services/filters/search-filter";
import { filterByTimestamp } from "./services/filters/timestamp-filter";
import { filterByGroups } from "./services/filters/custom/filter-by-groups";
import { formatFileDataForRender } from "./services/render-utils";
import { formatFileDataForRender } from "./services/utils/render-utils";
import _ from "lodash";
import { onMount } from "svelte";
import EventManager from "src/event/event-manager";
import { getDisplayNameForView } from "./services/display-name";
import { getDisplayNameForView } from "./services/utils/display-name-utils";
import {
getStartOfLastWeekMillis,
getStartOfTodayMillis,
getStartOfThisWeekMillis,
} from "../shared/services/time-utils";
import { FileContent, FileRenderData } from "./types";
import { FileRenderData } from "./types";
import Logger from "js-logger";
import SearchFilter from "./components/search-filter.svelte";
import TimestampFilter from "./components/timestamp-filter.svelte";
Expand All @@ -51,6 +51,8 @@
import FeedView from "./components/feed-view.svelte";
import PaginationIndicator from "./components/pagination-indicator.svelte";
import Wrap from "../shared/components/wrap.svelte";
import { randomFileSortStore } from "./services/random-file-sort-store";
import { fileContentStore } from "./services/file-content-store";
// ============================================
// Variables
Expand Down Expand Up @@ -91,7 +93,9 @@
let files: TFile[] = [];
let timeValuesUpdateInterval: NodeJS.Timer | null = null;
let contentForFiles: FileContent[] = [];
let contentCache: Record<string, string | null> = {};
let randomSortCache: Record<string, number> = {};
let dashboardView: TDashboardView = {
isEnabled: false,
Expand Down Expand Up @@ -151,7 +155,16 @@
setTimeValuesUpdateInterval();
}
contentForFiles = await loadContentForFiles(files);
fileContentStore.load(app);
randomFileSortStore.load(files);
});
randomFileSortStore.subscribe((value) => {
randomSortCache = value;
});
fileContentStore.subscribe((value) => {
contentCache = value;
});
onMount(() => {
Expand Down Expand Up @@ -249,6 +262,9 @@
if (data.length > 0 && data[0] instanceof TFile) {
const newFile = data[0] as TFile;
files = [...files, newFile];
randomFileSortStore.onFileCreate(newFile.path);
fileContentStore.onFileCreate(plugin.app, newFile);
}
};
Expand All @@ -267,7 +283,12 @@
});
if (data.length > 0 && typeof data[0] === "string") {
const path = data[0] as string;
//Remove the file from the files array
files = files.filter((file) => file.path !== path);
randomFileSortStore.onFileDelete(path);
fileContentStore.onFileDelete(path);
}
};
Expand All @@ -288,12 +309,16 @@
if (typeof data[0] === "string" && data[1] instanceof TFile) {
const oldPath = data[0] as string;
const updatedFile = data[1] as TFile;
files = files.map((file) => {
if (file.path === oldPath) {
return updatedFile;
}
return file;
});
randomFileSortStore.onFileRename(oldPath, updatedFile.path);
fileContentStore.onFileRename(oldPath, updatedFile.path);
}
};
Expand All @@ -314,16 +339,7 @@
const file = data[0] as TFile;
const content = await plugin.app.vault.cachedRead(file);
const updatedContentForFiles = contentForFiles.map((entry) => {
if (entry.path === file.path) {
return {
path: file.path,
content,
};
}
return entry;
});
contentForFiles = updatedContentForFiles;
fileContentStore.onFileModify(file.path, content);
}
};
Expand Down Expand Up @@ -452,32 +468,6 @@
timeValuesUpdateInterval = setInterval(updateTimeValues, MILLIS_MINUTE);
}
async function loadContentForFiles(files: TFile[]): Promise<FileContent[]> {
const promises: Promise<FileContent>[] = [];
for (let file of files) {
promises.push(
(async () => {
const { extension } = file;
if (extension === "md") {
const content = await plugin.app.vault.cachedRead(file);
return {
path: file.path,
content,
};
}
return {
path: file.path,
content: null,
};
})(),
);
}
const results = await Promise.all(promises);
return results;
}
function updateFrontmatterCacheTime() {
Logger.trace({
fileName: "app/index.ts",
Expand Down Expand Up @@ -664,9 +654,7 @@
const frontmatter =
plugin.app.metadataCache.getFileCache(file)?.frontmatter;
let content =
contentForFiles.find((content) => content.path === path)
?.content ?? "";
const content = contentCache[path];
return filterByGroups(
name,
Expand All @@ -683,9 +671,8 @@
formatted = filteredCustom.map((file) => {
const frontmatter =
plugin.app.metadataCache.getFileCache(file)?.frontmatter;
const content =
contentForFiles.find((content) => content.path === file.path)
?.content ?? null;
const content = contentCache[file.path];
return formatFileDataForRender(
plugin.settings,
file,
Expand Down Expand Up @@ -724,14 +711,19 @@
});
$: renderData = [...filteredTimestamp].sort((a, b) => {
if (sortFilter.value === "file-name-asc") {
const { value } = sortFilter;
if (value === "file-name-asc") {
return a.name.toLowerCase().localeCompare(b.name.toLowerCase());
} else if (sortFilter.value === "file-name-desc") {
} else if (value === "file-name-desc") {
return b.name.toLowerCase().localeCompare(a.name.toLowerCase());
} else if (sortFilter.value === "modified-asc") {
} else if (value === "modified-asc") {
return a.modifiedMillis - b.modifiedMillis;
} else if (sortFilter.value === "modified-desc") {
} else if (value === "modified-desc") {
return b.modifiedMillis - a.modifiedMillis;
} else if (value === "random") {
const sortKeyA = randomSortCache[a.path];
const sortKeyB = randomSortCache[b.path];
return sortKeyA - sortKeyB;
}
return 0;
});
Expand Down
102 changes: 102 additions & 0 deletions src/svelte/app/services/file-content-store.ts
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();
Loading

0 comments on commit f509c8d

Please sign in to comment.