Skip to content

Commit

Permalink
fix: load more logs in history view on scroll
Browse files Browse the repository at this point in the history
close #807
  • Loading branch information
Vinzent03 committed Jan 4, 2025
1 parent d13dff4 commit a17cb55
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 16 deletions.
3 changes: 2 additions & 1 deletion src/gitManager/gitManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ export abstract class GitManager {
abstract log(
file: string | undefined,
relativeToVault?: boolean,
limit?: number
limit?: number,
ref?: string
): Promise<LogEntry[]>;

abstract getRemoteBranches(remote: string): Promise<string[]>;
Expand Down
9 changes: 7 additions & 2 deletions src/gitManager/isomorphicGit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -768,9 +768,14 @@ export class IsomorphicGit extends GitManager {
).filter((item) => item.remote == remote)[0]?.url;
}

async log(_?: string, __ = true, limit?: number): Promise<LogEntry[]> {
async log(
_?: string,
__ = true,
limit?: number,
ref?: string
): Promise<LogEntry[]> {
const logs = await this.wrapFS(
git.log({ ...this.getRepo(), depth: limit })
git.log({ ...this.getRepo(), depth: limit, ref: ref })
);

return Promise.all(
Expand Down
11 changes: 8 additions & 3 deletions src/gitManager/simpleGit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -708,20 +708,25 @@ export class SimpleGit extends GitManager {
async log(
file: string | undefined,
relativeToVault = true,
limit?: number
limit?: number,
ref?: string
): Promise<(LogEntry & { fileName?: string })[]> {
let path: string | undefined;
if (file) {
path = this.getRelativeRepoPath(file, relativeToVault);
}
const res = await this.git.log({
const opts: Record<string, unknown> = {
file: path,
maxCount: limit,
// Ensures that the changed files are listed for merge commits as well and the commit is not repeated for each parent.
// This only lists the changed files for the first parent.
"--diff-merges": "first-parent",
"--name-status": null,
});
};
if (ref) {
opts[ref] = null;
}
const res = await this.git.log(opts);

return res.all.map<LogEntry>((e) => ({
...e,
Expand Down
9 changes: 4 additions & 5 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ import SplitDiffView from "./ui/diff/splitDiffView";

export default class ObsidianGit extends Plugin {
gitManager: GitManager;
automaticsManager: AutomaticsManager;
automaticsManager = new AutomaticsManager(this);
tools = new Tools(this);
localStorage: LocalStorageSettings;
localStorage = new LocalStorageSettings(this);
settings: ObsidianGitSettings;
settingsTab?: ObsidianGitSettingsTab;
statusBar?: StatusBar;
Expand Down Expand Up @@ -138,8 +138,6 @@ export default class ObsidianGit extends Plugin {

pluginRef.plugin = this;

this.localStorage = new LocalStorageSettings(this);

this.localStorage.migrate();
await this.loadSettings();
await this.migrateSettings();
Expand Down Expand Up @@ -521,13 +519,14 @@ export default class ObsidianGit extends Plugin {
this.lineAuthoringFeature.conditionallyActivateBySettings();

this.app.workspace.trigger("obsidian-git:refresh");
/// Among other things, this notifies the history view that git is ready
this.app.workspace.trigger("obsidian-git:head-change");

if (!fromReload && this.settings.autoPullOnBoot) {
this.promiseQueue.addTask(() =>
this.pullChangesFromRemote()
);
}
this.automaticsManager = new AutomaticsManager(this);
await this.automaticsManager.init();
break;
default:
Expand Down
55 changes: 50 additions & 5 deletions src/ui/history/historyView.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { SimpleGit } from "src/gitManager/simpleGit";
import type ObsidianGit from "src/main";
import type { LogEntry } from "src/types";
import { onDestroy } from "svelte";
import { onDestroy, onMount } from "svelte";
import LogComponent from "./components/logComponent.svelte";
import type HistoryView from "./historyView";
Expand All @@ -20,25 +20,46 @@
let refreshRef: EventRef;
let layoutBtn: HTMLElement | undefined = $state();
$effect(() => {
if (layoutBtn) {
layoutBtn.empty();
}
});
refreshRef = view.app.workspace.on(
"obsidian-git:head-change",
() => void refresh().catch(console.error)
);
refresh().catch(console.error);
$effect(() => {
buttons.forEach((btn) => setIcon(btn, btn.getAttr("data-icon")!));
});
onDestroy(() => {
view.app.workspace.offref(refreshRef);
});
onMount(() => {
const observer = new IntersectionObserver((entries) => {
if (entries[0].isIntersecting && !loading) {
appendLogs().catch(console.error);
}
});
const sentinel = document.querySelector("#sentinel");
if (sentinel) {
observer.observe(sentinel);
}
return () => {
observer.disconnect();
};
});
refresh().catch(console.error);
function triggerRefresh() {
view.app.workspace.trigger("obsidian-git:refresh");
refresh().catch(console.error);
}
async function refresh() {
Expand All @@ -48,11 +69,32 @@
}
loading = true;
const isSimpleGit = plugin.gitManager instanceof SimpleGit;
logs = await plugin.gitManager.log(
let limit;
if ((logs?.length ?? 0) == 0) {
limit = isSimpleGit ? 50 : 10;
} else {
limit = logs!.length;
}
logs = await plugin.gitManager.log(undefined, false, limit);
loading = false;
}
async function appendLogs() {
if (!plugin.gitReady || logs === undefined) {
return;
}
loading = true;
const isSimpleGit = plugin.gitManager instanceof SimpleGit;
const limit = isSimpleGit ? 50 : 10;
const newLogs = await plugin.gitManager.log(
undefined,
false,
isSimpleGit ? 50 : 10
limit,
logs.last()?.hash
);
// Remove the first element of the new logs, as it is the same as the last element of the current logs.
// And don't use hash^ as it fails for the first commit.
logs.push(...newLogs.slice(1));
loading = false;
}
</script>
Expand Down Expand Up @@ -97,6 +139,9 @@
</div>
{/if}
</div>
<div id="sentinel"></div>
<!-- Ensure that the sentinel item is reachable with the overlaying status bar and indicate that the end of the list is reached -->
<div style="margin-bottom:40px"></div>
</main>

<style lang="scss">
Expand Down

0 comments on commit a17cb55

Please sign in to comment.