A2: disk-conflict banner, overwrite question and Local History dialog
- Banner inside the editor pane for a dirty tab whose file changed (Reload (discard mine) / Keep mine / Compare) or was deleted (Keep as unsaved / Close); state in stores/diskConflicts.ts, the old notice is gone. - Save and save all ask Overwrite / Cancel when the engine refuses with disk_changed. - Diff tabs against the disk text (file.readDisk) and a Local History entry (history.read); "Revert to This" in the caption. - Show Local History dialog (history.show: tree context menu, palette, File menu) with Show Diff and an undoable Revert to This. - History failures warn once until a snapshot works again. - Docs: docs/local-history.md, G2 in gaps.md, ui-parity, architecture. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
dfe2f19ca5
commit
d2344bf216
29 changed files with 1999 additions and 52 deletions
142
ui/src/components/editor/DiskBanner.vue
Normal file
142
ui/src/components/editor/DiskBanner.vue
Normal file
|
|
@ -0,0 +1,142 @@
|
|||
<script setup lang="ts">
|
||||
/**
|
||||
* The banner inside an editor pane for its active tab when that tab's file
|
||||
* changed or disappeared on disk while it had unsaved changes (C10 A2).
|
||||
* The actions and the rules live in the disk-conflict store
|
||||
* (`stores/diskConflicts.ts`); a failed action shows its message in the
|
||||
* pane of the focused tab.
|
||||
*/
|
||||
import { computed } from "vue";
|
||||
import { t } from "../../i18n";
|
||||
import { useDiskConflictsStore } from "../../stores/diskConflicts";
|
||||
import { baseName, useEditorStore } from "../../stores/editor";
|
||||
|
||||
const props = defineProps<{
|
||||
/** Buffer id of the pane's active tab, null if none. */
|
||||
id: number | null;
|
||||
}>();
|
||||
|
||||
const disk = useDiskConflictsStore();
|
||||
const editor = useEditorStore();
|
||||
|
||||
/** The pane's banner, null if none. */
|
||||
const banner = computed(() => disk.bannerOf(props.id));
|
||||
|
||||
/** The banner's text. */
|
||||
const message = computed(() => {
|
||||
const current = banner.value;
|
||||
if (!current) {
|
||||
return "";
|
||||
}
|
||||
const name = baseName(current.path);
|
||||
return current.kind === "removed" ? t("editor.disk.banner.removed", { name }) : t("editor.disk.banner.changed", { name });
|
||||
});
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div
|
||||
v-if="banner"
|
||||
class="disk-banner"
|
||||
role="alert"
|
||||
data-test="disk-banner"
|
||||
:data-kind="banner.kind"
|
||||
>
|
||||
<span class="disk-banner__text">{{ message }}</span>
|
||||
<template v-if="banner.kind === 'changed'">
|
||||
<button
|
||||
type="button"
|
||||
class="disk-banner__button"
|
||||
:disabled="disk.busy"
|
||||
data-test="disk-reload"
|
||||
@click="disk.reload(banner.id)"
|
||||
>
|
||||
{{ t("editor.disk.reload") }}
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
class="disk-banner__button"
|
||||
data-test="disk-keep"
|
||||
@click="disk.keep(banner.id)"
|
||||
>
|
||||
{{ t("editor.disk.keepMine") }}
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
class="disk-banner__button"
|
||||
data-test="disk-compare"
|
||||
@click="disk.compare(banner.id)"
|
||||
>
|
||||
{{ t("editor.disk.compare") }}
|
||||
</button>
|
||||
</template>
|
||||
<template v-else>
|
||||
<button
|
||||
type="button"
|
||||
class="disk-banner__button"
|
||||
data-test="disk-keep"
|
||||
@click="disk.keep(banner.id)"
|
||||
>
|
||||
{{ t("editor.disk.keepUnsaved") }}
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
class="disk-banner__button"
|
||||
:disabled="disk.busy"
|
||||
data-test="disk-close"
|
||||
@click="disk.closeTab(banner.id)"
|
||||
>
|
||||
{{ t("common.close") }}
|
||||
</button>
|
||||
</template>
|
||||
</div>
|
||||
<div
|
||||
v-if="disk.error && id !== null && id === editor.activeId"
|
||||
class="disk-banner disk-banner--error"
|
||||
role="alert"
|
||||
data-test="disk-error"
|
||||
>
|
||||
<span class="disk-banner__text">{{ disk.error }}</span>
|
||||
<button
|
||||
type="button"
|
||||
class="disk-banner__button"
|
||||
@click="disk.error = null"
|
||||
>
|
||||
{{ t("common.dismiss") }}
|
||||
</button>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.disk-banner {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
padding: 4px 12px;
|
||||
flex-shrink: 0;
|
||||
color: var(--lt-fg);
|
||||
background: color-mix(in srgb, var(--lt-diag-warning) 18%, var(--lt-bg));
|
||||
border-bottom: 1px solid var(--lt-border);
|
||||
}
|
||||
|
||||
.disk-banner--error {
|
||||
background: color-mix(in srgb, var(--lt-error) 15%, var(--lt-bg));
|
||||
}
|
||||
|
||||
.disk-banner__text {
|
||||
flex: 1;
|
||||
min-width: 0;
|
||||
}
|
||||
|
||||
.disk-banner__button {
|
||||
padding: 0 8px;
|
||||
font: inherit;
|
||||
color: var(--lt-fg);
|
||||
background: var(--lt-bg);
|
||||
border: 1px solid var(--lt-border);
|
||||
border-radius: 3px;
|
||||
}
|
||||
|
||||
.disk-banner__button:focus-visible {
|
||||
outline: 1px solid var(--lt-accent);
|
||||
}
|
||||
</style>
|
||||
|
|
@ -11,15 +11,20 @@
|
|||
* (`assist/commands.ts`) and the tab management commands
|
||||
* (`tabs/commands.ts`). Diff tabs (M4.3, `vcs/diff/`) share the first
|
||||
* group's tab bar; while one is active the diff view replaces that group's
|
||||
* editor, and the area supplies the diff commands.
|
||||
* editor, and the area supplies the diff commands. Files changed on disk
|
||||
* (C10 A2): each pane shows its active tab's banner (`DiskBanner.vue`);
|
||||
* the area shows the overwrite question (`OverwritePrompt.vue`), both from
|
||||
* `stores/diskConflicts.ts`.
|
||||
*/
|
||||
import { onBeforeUnmount, onMounted } from "vue";
|
||||
import { useCommandsStore } from "../../commands/store";
|
||||
import { t } from "../../i18n";
|
||||
import { useEditorStore } from "../../stores/editor";
|
||||
import { useDiskConflictsStore } from "../../stores/diskConflicts";
|
||||
import ClosePrompt from "./ClosePrompt.vue";
|
||||
import EditorPane from "./EditorPane.vue";
|
||||
import { provideTabCommands } from "./tabs/commands";
|
||||
import OverwritePrompt from "./OverwritePrompt.vue";
|
||||
import { reveal, REVEAL_COMMAND } from "./reveal";
|
||||
import * as assist from "./assist/commands";
|
||||
import { provideDiffCommands } from "../vcs/diff/commands";
|
||||
|
|
@ -28,12 +33,14 @@ import { useDiffStore } from "../vcs/diff/diff";
|
|||
const editor = useEditorStore();
|
||||
const commands = useCommandsStore();
|
||||
const diff = useDiffStore();
|
||||
const disk = useDiskConflictsStore();
|
||||
|
||||
/** Withdraws the command handlers; filled while mounted. */
|
||||
let withdraw: (() => void)[] = [];
|
||||
|
||||
onMounted(() => {
|
||||
editor.start();
|
||||
disk.start();
|
||||
const hasActive = (): boolean => editor.activeTab !== null;
|
||||
withdraw = [
|
||||
commands.provide("file.save", { run: async () => { await editor.save(); }, enabled: hasActive }),
|
||||
|
|
@ -53,6 +60,7 @@ onBeforeUnmount(() => {
|
|||
fn();
|
||||
}
|
||||
withdraw = [];
|
||||
disk.stop();
|
||||
editor.stop();
|
||||
});
|
||||
</script>
|
||||
|
|
@ -96,6 +104,7 @@ onBeforeUnmount(() => {
|
|||
</button>
|
||||
</div>
|
||||
<ClosePrompt />
|
||||
<OverwritePrompt />
|
||||
</div>
|
||||
</template>
|
||||
|
||||
|
|
|
|||
|
|
@ -4,15 +4,15 @@
|
|||
* tab and its editor. The first group also hosts the diff tabs; while a
|
||||
* diff is active there, the diff view replaces that group's editor.
|
||||
*
|
||||
* Banners about the active tab (C10 A2's conflict banner) belong in this
|
||||
* pane, between the tab bar and the editor, keyed by
|
||||
* {@link activeTab}.
|
||||
* The disk-conflict banner of the active tab (C10 A2, `DiskBanner.vue`)
|
||||
* sits between the tab bar and the editor, keyed by {@link activeTab}.
|
||||
*/
|
||||
import { computed, defineAsyncComponent } from "vue";
|
||||
import { t } from "../../i18n";
|
||||
import { useEditorStore } from "../../stores/editor";
|
||||
import { useDiffStore } from "../vcs/diff/diff";
|
||||
import CodeEditor from "./CodeEditor.vue";
|
||||
import DiskBanner from "./DiskBanner.vue";
|
||||
import EditorTabs from "./EditorTabs.vue";
|
||||
|
||||
/** The diff view, loaded on first use so `@codemirror/merge` stays out of the start bundle. */
|
||||
|
|
@ -45,6 +45,10 @@ const showsDiff = computed(() => props.group === 0 && diff.visible);
|
|||
data-test="editor-pane"
|
||||
>
|
||||
<EditorTabs :group="group" />
|
||||
<DiskBanner
|
||||
v-if="!showsDiff"
|
||||
:id="activeTab?.id ?? null"
|
||||
/>
|
||||
<div
|
||||
v-if="!showsDiff && activeTab?.notice"
|
||||
class="editor-pane__notice"
|
||||
|
|
|
|||
130
ui/src/components/editor/OverwritePrompt.vue
Normal file
130
ui/src/components/editor/OverwritePrompt.vue
Normal file
|
|
@ -0,0 +1,130 @@
|
|||
<script setup lang="ts">
|
||||
/**
|
||||
* The question before a save overwrites a file that another program
|
||||
* changed (C10 A2): Overwrite or Cancel. Escape cancels; Cancel has the
|
||||
* focus, so Enter never overwrites by accident. The questions come from
|
||||
* the disk-conflict store (`stores/diskConflicts.ts`), one at a time.
|
||||
*/
|
||||
import { nextTick, ref, watch } from "vue";
|
||||
import { t } from "../../i18n";
|
||||
import { useDiskConflictsStore } from "../../stores/diskConflicts";
|
||||
|
||||
const disk = useDiskConflictsStore();
|
||||
|
||||
/** The Cancel button, focused when the question appears. */
|
||||
const cancelButton = ref<HTMLButtonElement | null>(null);
|
||||
|
||||
watch(
|
||||
() => disk.prompt,
|
||||
async (prompt) => {
|
||||
if (prompt) {
|
||||
await nextTick();
|
||||
cancelButton.value?.focus();
|
||||
}
|
||||
},
|
||||
{ immediate: true },
|
||||
);
|
||||
|
||||
/**
|
||||
* Cancels on Escape.
|
||||
*
|
||||
* @param event - the key event inside the dialog.
|
||||
*/
|
||||
function onKeydown(event: KeyboardEvent): void {
|
||||
if (event.key === "Escape") {
|
||||
event.preventDefault();
|
||||
void disk.resolveOverwrite(false);
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div
|
||||
v-if="disk.prompt"
|
||||
class="overwrite-prompt"
|
||||
data-test="overwrite-prompt"
|
||||
>
|
||||
<div
|
||||
class="overwrite-prompt__dialog"
|
||||
role="alertdialog"
|
||||
aria-modal="true"
|
||||
aria-labelledby="lt-overwrite-title"
|
||||
@keydown="onKeydown"
|
||||
>
|
||||
<p
|
||||
id="lt-overwrite-title"
|
||||
class="overwrite-prompt__text"
|
||||
>
|
||||
{{ t("editor.disk.overwrite.question", { name: disk.prompt.name }) }}
|
||||
</p>
|
||||
<div class="overwrite-prompt__buttons">
|
||||
<button
|
||||
type="button"
|
||||
class="overwrite-prompt__button overwrite-prompt__button--danger"
|
||||
data-test="overwrite-confirm"
|
||||
@click="disk.resolveOverwrite(true)"
|
||||
>
|
||||
{{ t("editor.disk.overwrite.confirm") }}
|
||||
</button>
|
||||
<button
|
||||
ref="cancelButton"
|
||||
type="button"
|
||||
class="overwrite-prompt__button"
|
||||
data-test="overwrite-cancel"
|
||||
@click="disk.resolveOverwrite(false)"
|
||||
>
|
||||
{{ t("common.cancel") }}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.overwrite-prompt {
|
||||
position: absolute;
|
||||
inset: 0;
|
||||
display: flex;
|
||||
align-items: flex-start;
|
||||
justify-content: center;
|
||||
padding-top: 15%;
|
||||
background: color-mix(in srgb, var(--lt-bg) 50%, transparent);
|
||||
z-index: 10;
|
||||
}
|
||||
|
||||
.overwrite-prompt__dialog {
|
||||
max-width: 420px;
|
||||
padding: 16px;
|
||||
background: var(--lt-bg);
|
||||
color: var(--lt-fg);
|
||||
border: 1px solid var(--lt-border);
|
||||
border-radius: 6px;
|
||||
}
|
||||
|
||||
.overwrite-prompt__text {
|
||||
margin: 0 0 16px;
|
||||
}
|
||||
|
||||
.overwrite-prompt__buttons {
|
||||
display: flex;
|
||||
justify-content: flex-end;
|
||||
gap: 8px;
|
||||
}
|
||||
|
||||
.overwrite-prompt__button {
|
||||
padding: 4px 12px;
|
||||
font: inherit;
|
||||
color: var(--lt-fg);
|
||||
background: var(--lt-editor-bg);
|
||||
border: 1px solid var(--lt-border);
|
||||
border-radius: 4px;
|
||||
}
|
||||
|
||||
.overwrite-prompt__button--danger {
|
||||
border-color: var(--lt-error);
|
||||
}
|
||||
|
||||
.overwrite-prompt__button:focus-visible {
|
||||
outline: 1px solid var(--lt-accent);
|
||||
}
|
||||
</style>
|
||||
|
|
@ -18,7 +18,8 @@ export type EditorEvent =
|
|||
| { kind: "bufferChanged"; id: number; version: number }
|
||||
| { kind: "dirtyChanged"; id: number; dirty: boolean }
|
||||
| { kind: "bufferClosed"; id: number }
|
||||
| { kind: "fileChangedOnDisk"; path: string; id: number; removed: boolean };
|
||||
| { kind: "fileChangedOnDisk"; path: string; id: number; removed: boolean }
|
||||
| { kind: "diskConflictResolved"; id: number };
|
||||
|
||||
/** Callback receiving editor events. */
|
||||
export type EditorEventHandler = (event: EditorEvent) => void;
|
||||
|
|
@ -58,6 +59,8 @@ export function toEditorEvent(payload: EngineEventPayload): EditorEvent | null {
|
|||
return id !== null && typeof raw.path === "string" && typeof raw.removed === "boolean"
|
||||
? { kind: "fileChangedOnDisk", id, path: raw.path, removed: raw.removed }
|
||||
: null;
|
||||
case "diskConflictResolved":
|
||||
return id !== null ? { kind: "diskConflictResolved", id } : null;
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
|
|
|
|||
413
ui/src/components/history/LocalHistoryDialog.vue
Normal file
413
ui/src/components/history/LocalHistoryDialog.vue
Normal file
|
|
@ -0,0 +1,413 @@
|
|||
<script setup lang="ts">
|
||||
/**
|
||||
* The "Show Local History" dialog (C10 A2), centred over the window: the
|
||||
* snapshots of one file, newest first, with time, reason and size.
|
||||
*
|
||||
* - **List:** Up/Down, Home and End move; Enter or "Show Diff" opens the
|
||||
* entry against the current text in a diff tab and closes the dialog;
|
||||
* "Revert to This" puts the entry's text into the buffer (undoable,
|
||||
* unsaved). A failure shows under the list.
|
||||
* - **Command:** the dialog supplies the UI command `history.show`,
|
||||
* enabled while `history.list` is (an open project).
|
||||
* - **Closing:** Escape, Cancel or a click outside; the focus goes back
|
||||
* where it was. The dialog is an exclusive modal (`commands/modals.ts`).
|
||||
*/
|
||||
import { History } from "@lucide/vue";
|
||||
import { nextTick, onBeforeUnmount, onMounted, ref, watch } from "vue";
|
||||
import { useModalsStore } from "../../commands/modals";
|
||||
import { useCommandsStore } from "../../commands/store";
|
||||
import { formatDateTime, t } from "../../i18n";
|
||||
import { baseName } from "../../stores/editor";
|
||||
import { SHOW_HISTORY_COMMAND, useLocalHistoryStore, type HistoryEntry, type HistoryReason } from "./localHistory";
|
||||
|
||||
const history = useLocalHistoryStore();
|
||||
const modals = useModalsStore();
|
||||
const commands = useCommandsStore();
|
||||
|
||||
/** The list. */
|
||||
const list = ref<HTMLElement | null>(null);
|
||||
/** The dialog box. */
|
||||
const dialogBox = ref<HTMLElement | null>(null);
|
||||
/** The element that had the focus before the dialog opened. */
|
||||
let returnFocus: HTMLElement | null = null;
|
||||
|
||||
/**
|
||||
* The DOM id of an entry's row.
|
||||
*
|
||||
* @param index - its index in the list.
|
||||
*/
|
||||
function rowId(index: number): string {
|
||||
return `lt-history-${String(index)}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* The size of an entry for the list.
|
||||
*
|
||||
* @param entry - the entry.
|
||||
*/
|
||||
function sizeText(entry: HistoryEntry): string {
|
||||
return entry.size < 1024
|
||||
? t("history.sizeBytes", { size: entry.size })
|
||||
: t("history.sizeKib", { size: Math.round(entry.size / 102.4) / 10 });
|
||||
}
|
||||
|
||||
/** The label of each reason. */
|
||||
const REASON_TEXT: Record<HistoryReason, () => string> = {
|
||||
save: () => t("history.reason.save"),
|
||||
beforeSave: () => t("history.reason.beforeSave"),
|
||||
externalChange: () => t("history.reason.externalChange"),
|
||||
beforeReload: () => t("history.reason.beforeReload"),
|
||||
deleted: () => t("history.reason.deleted"),
|
||||
refactoring: () => t("history.reason.refactoring"),
|
||||
git: () => t("history.reason.git"),
|
||||
beforeRevert: () => t("history.reason.beforeRevert"),
|
||||
};
|
||||
|
||||
/**
|
||||
* The text of a reason for the list.
|
||||
*
|
||||
* @param reason - why the snapshot was taken.
|
||||
*/
|
||||
function reasonText(reason: HistoryReason): string {
|
||||
return REASON_TEXT[reason]();
|
||||
}
|
||||
|
||||
/** Closes the dialog. */
|
||||
function close(): void {
|
||||
if (history.open) {
|
||||
history.hide();
|
||||
}
|
||||
}
|
||||
|
||||
watch(
|
||||
() => history.open,
|
||||
(isOpen) => {
|
||||
if (isOpen) {
|
||||
const active = document.activeElement;
|
||||
returnFocus = active instanceof HTMLElement ? active : null;
|
||||
modals.opened("localHistory", close);
|
||||
} else {
|
||||
modals.closed("localHistory");
|
||||
returnFocus?.focus();
|
||||
returnFocus = null;
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
watch(
|
||||
() => [history.focusRequest, history.entries] as const,
|
||||
async () => {
|
||||
await nextTick();
|
||||
if ((history.entries?.length ?? 0) > 0) {
|
||||
list.value?.focus();
|
||||
} else {
|
||||
dialogBox.value?.focus();
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
/** Keeps the selected row in view. */
|
||||
function scrollSelected(): void {
|
||||
void nextTick(() => {
|
||||
const row = list.value?.querySelector<HTMLElement>(`#${rowId(history.selected)}`);
|
||||
if (row && typeof row.scrollIntoView === "function") {
|
||||
row.scrollIntoView({ block: "nearest" });
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Escape closes the dialog.
|
||||
*
|
||||
* @param event - the key press.
|
||||
*/
|
||||
function onDialogKey(event: KeyboardEvent): void {
|
||||
if (event.key === "Escape") {
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
close();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Keyboard handling of the list.
|
||||
*
|
||||
* @param event - the key press.
|
||||
*/
|
||||
function onListKey(event: KeyboardEvent): void {
|
||||
const count = history.entries?.length ?? 0;
|
||||
const moves: Record<string, number> = { ArrowDown: 1, ArrowUp: -1, Home: -count, End: count };
|
||||
const delta = moves[event.key];
|
||||
const entry = history.current;
|
||||
if (delta !== undefined) {
|
||||
history.move(delta);
|
||||
scrollSelected();
|
||||
} else if (event.key === "Enter" && entry) {
|
||||
void history.showDiff(entry);
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
}
|
||||
|
||||
/** Withdraws the command handler; set while mounted. */
|
||||
let withdraw: (() => void) | null = null;
|
||||
|
||||
onMounted(() => {
|
||||
withdraw = commands.provide(SHOW_HISTORY_COMMAND, {
|
||||
run: (params) => history.showCommand(params),
|
||||
enabled: () => commands.isEnabled("history.list"),
|
||||
});
|
||||
});
|
||||
|
||||
onBeforeUnmount(() => {
|
||||
withdraw?.();
|
||||
withdraw = null;
|
||||
});
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div
|
||||
v-if="history.open"
|
||||
class="local-history"
|
||||
data-test="local-history"
|
||||
@mousedown.self.prevent="close()"
|
||||
>
|
||||
<div
|
||||
ref="dialogBox"
|
||||
class="local-history__dialog"
|
||||
tabindex="-1"
|
||||
role="dialog"
|
||||
aria-modal="true"
|
||||
aria-labelledby="lt-history-title"
|
||||
@keydown="onDialogKey"
|
||||
>
|
||||
<div class="local-history__header">
|
||||
<History
|
||||
:size="14"
|
||||
aria-hidden="true"
|
||||
/>
|
||||
<h2
|
||||
id="lt-history-title"
|
||||
class="local-history__title"
|
||||
>
|
||||
{{ t("history.title", { name: baseName(history.path) }) }}
|
||||
</h2>
|
||||
<span class="local-history__path">{{ history.path }}</span>
|
||||
</div>
|
||||
<p
|
||||
v-if="history.entries === null || history.entries.length === 0"
|
||||
class="local-history__hint"
|
||||
role="status"
|
||||
data-test="history-hint"
|
||||
>
|
||||
{{ history.entries === null ? (history.loading ? t("history.loading") : "") : t("history.empty") }}
|
||||
</p>
|
||||
<ul
|
||||
v-show="(history.entries?.length ?? 0) > 0"
|
||||
ref="list"
|
||||
class="local-history__list"
|
||||
role="listbox"
|
||||
tabindex="0"
|
||||
:aria-label="t('history.listLabel')"
|
||||
:aria-activedescendant="rowId(history.selected)"
|
||||
data-test="history-list"
|
||||
@keydown="onListKey"
|
||||
>
|
||||
<li
|
||||
v-for="(entry, i) in history.entries"
|
||||
:id="rowId(i)"
|
||||
:key="entry.id"
|
||||
class="local-history__row"
|
||||
:class="{ 'local-history__row--selected': history.selected === i }"
|
||||
role="option"
|
||||
:aria-selected="history.selected === i"
|
||||
:data-id="entry.id"
|
||||
data-test="history-row"
|
||||
@click="history.selected = i"
|
||||
@dblclick="history.showDiff(entry)"
|
||||
>
|
||||
<span class="local-history__time">{{ formatDateTime(entry.time) }}</span>
|
||||
<span class="local-history__reason">{{ reasonText(entry.reason) }}</span>
|
||||
<span class="local-history__size">{{ sizeText(entry) }}</span>
|
||||
</li>
|
||||
</ul>
|
||||
<p
|
||||
v-if="history.error !== null"
|
||||
class="local-history__error"
|
||||
role="alert"
|
||||
data-test="history-error"
|
||||
>
|
||||
{{ history.error }}
|
||||
</p>
|
||||
<div class="local-history__buttons">
|
||||
<button
|
||||
type="button"
|
||||
class="local-history__button"
|
||||
data-test="history-cancel"
|
||||
@click="close()"
|
||||
>
|
||||
{{ t("common.cancel") }}
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
class="local-history__button"
|
||||
:disabled="history.busy || history.current === null"
|
||||
data-test="history-revert"
|
||||
@click="history.current && history.revert(history.path, history.current.id)"
|
||||
>
|
||||
{{ t("history.revert") }}
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
class="local-history__button local-history__button--primary"
|
||||
:disabled="history.current === null"
|
||||
data-test="history-diff"
|
||||
@click="history.current && history.showDiff(history.current)"
|
||||
>
|
||||
{{ t("history.showDiff") }}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.local-history {
|
||||
position: fixed;
|
||||
inset: 0;
|
||||
display: flex;
|
||||
align-items: flex-start;
|
||||
justify-content: center;
|
||||
padding-top: 12vh;
|
||||
z-index: 30;
|
||||
}
|
||||
|
||||
.local-history__dialog {
|
||||
width: min(560px, 92vw);
|
||||
max-height: 70vh;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 6px;
|
||||
padding: 10px;
|
||||
background: var(--lt-bg);
|
||||
color: var(--lt-fg);
|
||||
border: 1px solid var(--lt-border);
|
||||
border-radius: 6px;
|
||||
outline: none;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.local-history__header {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 6px;
|
||||
min-width: 0;
|
||||
color: var(--lt-fg-muted);
|
||||
}
|
||||
|
||||
.local-history__title {
|
||||
margin: 0;
|
||||
font-size: 1em;
|
||||
font-weight: 600;
|
||||
color: var(--lt-fg);
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
.local-history__path {
|
||||
min-width: 0;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
.local-history__hint {
|
||||
margin: 0;
|
||||
color: var(--lt-fg-muted);
|
||||
}
|
||||
|
||||
.local-history__error {
|
||||
margin: 0;
|
||||
white-space: pre-wrap;
|
||||
color: var(--lt-error);
|
||||
}
|
||||
|
||||
.local-history__list {
|
||||
flex: 1;
|
||||
min-height: 0;
|
||||
margin: 0;
|
||||
padding: 2px 0;
|
||||
list-style: none;
|
||||
overflow-y: auto;
|
||||
border: 1px solid var(--lt-border);
|
||||
border-radius: var(--lt-radius, 4px);
|
||||
outline: none;
|
||||
}
|
||||
|
||||
.local-history__list:focus-visible {
|
||||
box-shadow: inset 0 0 0 1px var(--lt-accent);
|
||||
}
|
||||
|
||||
.local-history__row {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 12px;
|
||||
padding: 3px 8px;
|
||||
white-space: nowrap;
|
||||
cursor: default;
|
||||
}
|
||||
|
||||
.local-history__row:hover {
|
||||
background: color-mix(in srgb, var(--lt-fg) 6%, transparent);
|
||||
}
|
||||
|
||||
.local-history__row--selected,
|
||||
.local-history__row--selected:hover {
|
||||
background: var(--lt-editor-selection-bg);
|
||||
}
|
||||
|
||||
.local-history__time {
|
||||
flex: none;
|
||||
font-family: var(--lt-font-mono);
|
||||
}
|
||||
|
||||
.local-history__reason {
|
||||
flex: 1;
|
||||
min-width: 0;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
}
|
||||
|
||||
.local-history__size {
|
||||
flex: none;
|
||||
color: var(--lt-fg-muted);
|
||||
}
|
||||
|
||||
.local-history__buttons {
|
||||
display: flex;
|
||||
justify-content: flex-end;
|
||||
gap: 8px;
|
||||
}
|
||||
|
||||
.local-history__button {
|
||||
padding: 4px 12px;
|
||||
font: inherit;
|
||||
color: var(--lt-fg);
|
||||
background: var(--lt-editor-bg);
|
||||
border: 1px solid var(--lt-border);
|
||||
border-radius: 4px;
|
||||
}
|
||||
|
||||
.local-history__button--primary {
|
||||
border-color: var(--lt-accent);
|
||||
}
|
||||
|
||||
.local-history__button:focus-visible {
|
||||
outline: 1px solid var(--lt-accent);
|
||||
}
|
||||
|
||||
.local-history__button:disabled {
|
||||
opacity: 0.5;
|
||||
}
|
||||
</style>
|
||||
260
ui/src/components/history/localHistory.ts
Normal file
260
ui/src/components/history/localHistory.ts
Normal file
|
|
@ -0,0 +1,260 @@
|
|||
/**
|
||||
* Local History (C10 A2): the "Show Local History" dialog's state and the
|
||||
* revert action. The engine takes the snapshots (`crates/engine/src/
|
||||
* history.rs`); this store lists and applies them.
|
||||
*
|
||||
* - **Opening:** the UI command `history.show {path?}` (project tree and
|
||||
* tab context menus with the file's path, the palette with the active
|
||||
* tab's file). It lists `history.list {path}`, newest first, with time
|
||||
* and reason.
|
||||
* - **Show Diff:** opens a diff tab of the entry (left) against the
|
||||
* current buffer (right), see `vcs/diff/diff.ts` › `openHistory`, and
|
||||
* closes the dialog. The diff tab's caption has "Revert to This" too.
|
||||
* - **Revert to This:** `history.revert {path, id}` replaces the buffer's
|
||||
* text through the engine (an ordinary unsaved edit). For a file open in
|
||||
* a tab, the change enters the editor's undo history
|
||||
* (`BufferSync.beginUndoableRemote`), so Undo brings the text back.
|
||||
* - **Untrusted input:** list entries are checked field by field.
|
||||
*/
|
||||
import { defineStore } from "pinia";
|
||||
import { computed, ref } from "vue";
|
||||
import { t } from "../../i18n";
|
||||
import { run, type JsonValue } from "../../ipc";
|
||||
import { useEditorStore } from "../../stores/editor";
|
||||
import { useDiffStore } from "../vcs/diff/diff";
|
||||
|
||||
/** Command id that opens the dialog. */
|
||||
export const SHOW_HISTORY_COMMAND = "history.show";
|
||||
|
||||
/** Why a snapshot was taken, as the engine names it. */
|
||||
export type HistoryReason =
|
||||
| "save"
|
||||
| "beforeSave"
|
||||
| "externalChange"
|
||||
| "beforeReload"
|
||||
| "deleted"
|
||||
| "refactoring"
|
||||
| "git"
|
||||
| "beforeRevert";
|
||||
|
||||
/** Every reason, for checking untrusted input. */
|
||||
const REASONS: readonly HistoryReason[] = [
|
||||
"save",
|
||||
"beforeSave",
|
||||
"externalChange",
|
||||
"beforeReload",
|
||||
"deleted",
|
||||
"refactoring",
|
||||
"git",
|
||||
"beforeRevert",
|
||||
];
|
||||
|
||||
/** One entry of `history.list`. */
|
||||
export interface HistoryEntry {
|
||||
/** The entry's id. */
|
||||
id: number;
|
||||
/** When it was taken, ms since the Unix epoch. */
|
||||
time: number;
|
||||
/** Why it was taken. */
|
||||
reason: HistoryReason;
|
||||
/** Size of the text in bytes. */
|
||||
size: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads the entries of a `history.list` result; malformed entries are
|
||||
* skipped, a malformed result gives null.
|
||||
*
|
||||
* @param raw - untrusted.
|
||||
*/
|
||||
export function toEntries(raw: unknown): HistoryEntry[] | null {
|
||||
if (typeof raw !== "object" || raw === null || !Array.isArray((raw as { entries?: unknown }).entries)) {
|
||||
return null;
|
||||
}
|
||||
const out: HistoryEntry[] = [];
|
||||
for (const item of (raw as { entries: unknown[] }).entries) {
|
||||
if (typeof item !== "object" || item === null) {
|
||||
continue;
|
||||
}
|
||||
const e = item as Record<string, unknown>;
|
||||
const reason = REASONS.find((r) => r === e.reason);
|
||||
if (typeof e.id === "number" && typeof e.time === "number" && typeof e.size === "number" && reason) {
|
||||
out.push({ id: e.id, time: e.time, reason, size: e.size });
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads the params of `history.show`; null when malformed.
|
||||
*
|
||||
* @param params - untrusted (`{}` from the palette and the keyboard).
|
||||
* @returns the path, or `""` for the active tab's file.
|
||||
*/
|
||||
export function parseShowParams(params: unknown): string | null {
|
||||
if (params === null || params === undefined) {
|
||||
return "";
|
||||
}
|
||||
if (typeof params !== "object" || Array.isArray(params)) {
|
||||
return null;
|
||||
}
|
||||
const path = (params as Record<string, unknown>).path;
|
||||
if (path === undefined || path === null) {
|
||||
return "";
|
||||
}
|
||||
return typeof path === "string" && path !== "" ? path : null;
|
||||
}
|
||||
|
||||
/** The Local History store. */
|
||||
export const useLocalHistoryStore = defineStore("localHistory", () => {
|
||||
const editor = useEditorStore();
|
||||
|
||||
/** Whether the dialog is open. */
|
||||
const open = ref(false);
|
||||
/** The file whose history is shown. */
|
||||
const path = ref("");
|
||||
/** The entries, newest first; null while loading or after a failure. */
|
||||
const entries = ref<HistoryEntry[] | null>(null);
|
||||
/** Index of the selected entry. */
|
||||
const selected = ref(0);
|
||||
/** Whether the list is loading. */
|
||||
const loading = ref(false);
|
||||
/** Whether a revert runs. */
|
||||
const busy = ref(false);
|
||||
/** Last failure, null if none. */
|
||||
const error = ref<string | null>(null);
|
||||
/** Bumped when the dialog wants the focus. */
|
||||
const focusRequest = ref(0);
|
||||
/** Load generation, so only the newest load lands. */
|
||||
let generation = 0;
|
||||
|
||||
/** The selected entry, or null. */
|
||||
const current = computed(() => entries.value?.[selected.value] ?? null);
|
||||
|
||||
/**
|
||||
* Opens the dialog for a file and lists its entries.
|
||||
*
|
||||
* @param file - project-relative path.
|
||||
*/
|
||||
async function show(file: string): Promise<void> {
|
||||
path.value = file;
|
||||
open.value = true;
|
||||
entries.value = null;
|
||||
selected.value = 0;
|
||||
error.value = null;
|
||||
loading.value = true;
|
||||
focusRequest.value += 1;
|
||||
const mine = ++generation;
|
||||
const result = await run<unknown>("history.list", { path: file });
|
||||
if (mine !== generation) {
|
||||
return;
|
||||
}
|
||||
loading.value = false;
|
||||
if (!result.ok) {
|
||||
error.value = result.error.message;
|
||||
return;
|
||||
}
|
||||
const list = toEntries(result.value);
|
||||
if (list === null) {
|
||||
error.value = t("common.error.unexpectedResult", { command: "history.list" });
|
||||
return;
|
||||
}
|
||||
entries.value = list;
|
||||
}
|
||||
|
||||
/** Closes the dialog. */
|
||||
function hide(): void {
|
||||
open.value = false;
|
||||
generation += 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Moves the selection.
|
||||
*
|
||||
* @param delta - rows to move; clamped to the list.
|
||||
*/
|
||||
function move(delta: number): void {
|
||||
const count = entries.value?.length ?? 0;
|
||||
if (count > 0) {
|
||||
selected.value = Math.max(0, Math.min(count - 1, selected.value + delta));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Shows an entry against the current text in the diff viewer.
|
||||
*
|
||||
* @param entry - the entry.
|
||||
*/
|
||||
async function showDiff(entry: HistoryEntry): Promise<void> {
|
||||
const file = path.value;
|
||||
hide();
|
||||
await useDiffStore().openHistory(file, entry.id, entry.time);
|
||||
}
|
||||
|
||||
/**
|
||||
* Puts an entry's text into the file's buffer, undoably when the file is
|
||||
* open in a tab, and closes the dialog.
|
||||
*
|
||||
* @param file - project-relative path.
|
||||
* @param id - the entry's id.
|
||||
* @returns whether it worked ({@link error} says why not).
|
||||
*/
|
||||
async function revert(file: string, id: number): Promise<boolean> {
|
||||
busy.value = true;
|
||||
const tab = editor.tabOfPath(file);
|
||||
const sync = tab ? editor.syncOf(tab.id) : undefined;
|
||||
if (tab) {
|
||||
await editor.settleAll();
|
||||
}
|
||||
sync?.beginUndoableRemote();
|
||||
const result = await run<{ version?: unknown }>("history.revert", { path: file, id });
|
||||
busy.value = false;
|
||||
if (!result.ok) {
|
||||
sync?.endUndoableRemote(null);
|
||||
error.value = result.error.message;
|
||||
return false;
|
||||
}
|
||||
sync?.endUndoableRemote(typeof result.value.version === "number" ? result.value.version : null);
|
||||
error.value = null;
|
||||
hide();
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs `history.show`: the given file, else the active diff's or editor
|
||||
* tab's file.
|
||||
*
|
||||
* @param params - untrusted params.
|
||||
* @throws Error with a catalog message when the params are malformed or
|
||||
* there is no file.
|
||||
*/
|
||||
async function showCommand(params: JsonValue): Promise<void> {
|
||||
const parsed = parseShowParams(params);
|
||||
if (parsed === null) {
|
||||
throw new Error(t("history.invalidParams"));
|
||||
}
|
||||
const file = parsed !== "" ? parsed : (useDiffStore().activeTab?.path ?? editor.activeTab?.path ?? "");
|
||||
if (file === "") {
|
||||
throw new Error(t("history.noFile"));
|
||||
}
|
||||
await show(file);
|
||||
}
|
||||
|
||||
return {
|
||||
open,
|
||||
path,
|
||||
entries,
|
||||
selected,
|
||||
loading,
|
||||
busy,
|
||||
error,
|
||||
focusRequest,
|
||||
current,
|
||||
show,
|
||||
hide,
|
||||
move,
|
||||
showDiff,
|
||||
revert,
|
||||
showCommand,
|
||||
};
|
||||
});
|
||||
|
|
@ -62,6 +62,7 @@ import { useChangesStore } from "../vcs/changes/changes";
|
|||
import LogToolWindow from "../vcs/log/LogToolWindow.vue";
|
||||
import { SHOW_HISTORY_COMMAND, SHOW_LOG_COMMAND, useLogStore } from "../vcs/log/log";
|
||||
import StashDialog from "../vcs/stash/StashDialog.vue";
|
||||
import LocalHistoryDialog from "../history/LocalHistoryDialog.vue";
|
||||
import DeploymentLogToolWindow from "../deploy/DeploymentLogToolWindow.vue";
|
||||
import RemoteBrowserToolWindow from "../deploy/RemoteBrowserToolWindow.vue";
|
||||
import { useRemoteBrowserStore } from "../deploy/remoteBrowser";
|
||||
|
|
@ -417,6 +418,7 @@ onBeforeUnmount(() => {
|
|||
</BottomPanel>
|
||||
<StatusBar />
|
||||
<StashDialog />
|
||||
<LocalHistoryDialog />
|
||||
</div>
|
||||
</template>
|
||||
|
||||
|
|
|
|||
|
|
@ -46,6 +46,7 @@ const ITEM_ORDER = [
|
|||
"project.openDialog",
|
||||
"file.save",
|
||||
"file.saveAll",
|
||||
"history.show",
|
||||
"buffer.close",
|
||||
"project.close",
|
||||
"app.quit",
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
/**
|
||||
* The project tree's context menu (M4.3). For a file: Open, Show Diff
|
||||
* (`vcs.showDiff {path}`) and Show History (`vcs.showHistory {path}`,
|
||||
* M4.6), the Git items dimmed while their command is disabled. For a file
|
||||
* M4.6) and Show Local History (`history.show {path}`, C10 A2), the Git items dimmed while their command is disabled. For a file
|
||||
* and a folder: Upload to Server, Compare with Server (a diff for a file,
|
||||
* the sync preview for a folder, C9 D2) and Download from Server
|
||||
* (`deployment.upload` / `deployment.compare` / `deployment.download
|
||||
|
|
@ -20,6 +20,7 @@ import { t } from "../../i18n";
|
|||
import { useEditorStore } from "../../stores/editor";
|
||||
import { SHOW_DIFF_COMMAND } from "../vcs/diff/diff";
|
||||
import { SHOW_HISTORY_COMMAND } from "../vcs/log/log";
|
||||
import { SHOW_HISTORY_COMMAND as SHOW_LOCAL_HISTORY_COMMAND } from "../history/localHistory";
|
||||
|
||||
const props = defineProps<{
|
||||
/** Project-relative path of the file. */
|
||||
|
|
@ -103,6 +104,12 @@ const fileItems = computed<MenuItem[]>(() => [
|
|||
enabled: commands.isEnabled(SHOW_HISTORY_COMMAND),
|
||||
run: () => commands.execute(SHOW_HISTORY_COMMAND, { path: props.path }),
|
||||
},
|
||||
{
|
||||
id: "showLocalHistory",
|
||||
label: t("project.tree.menu.showLocalHistory"),
|
||||
enabled: commands.isEnabled(SHOW_LOCAL_HISTORY_COMMAND),
|
||||
run: () => commands.execute(SHOW_LOCAL_HISTORY_COMMAND, { path: props.path }),
|
||||
},
|
||||
]);
|
||||
|
||||
/** The items, in order. */
|
||||
|
|
|
|||
|
|
@ -22,6 +22,9 @@
|
|||
* buffer, so its Git gutter shows unstaged and staged markers; a click
|
||||
* opens the chunk popup with Stage, Unstage and Revert. A hint line says
|
||||
* so.
|
||||
* - **Local tabs (C10 A2):** the disk text or a Local History entry on the
|
||||
* left; a history tab's caption has "Revert to This"
|
||||
* (`history.revert`, an undoable buffer edit).
|
||||
* - **Navigation:** a navigator over the merge view is registered for
|
||||
* `vcs.nextChange` / `vcs.previousChange` (`navigation.ts`): the focused
|
||||
* side, else the working copy, moves to the next or previous chunk.
|
||||
|
|
@ -31,7 +34,8 @@ import { Annotation, EditorState, Transaction, type Extension } from "@codemirro
|
|||
import { EditorView } from "@codemirror/view";
|
||||
import { basicSetup } from "codemirror";
|
||||
import { computed, onBeforeUnmount, onMounted, ref, watch } from "vue";
|
||||
import { t } from "../../../i18n";
|
||||
import { formatDateTime, t } from "../../../i18n";
|
||||
import { useLocalHistoryStore } from "../../history/localHistory";
|
||||
import { onEngineEvent } from "../../../ipc";
|
||||
import { useEditorStore } from "../../../stores/editor";
|
||||
import type { BufferSync } from "../../editor/bufferSync";
|
||||
|
|
@ -44,6 +48,7 @@ import { setNavigator } from "./navigation";
|
|||
|
||||
const diff = useDiffStore();
|
||||
const editor = useEditorStore();
|
||||
const history = useLocalHistoryStore();
|
||||
|
||||
/** Element the merge view is mounted in. */
|
||||
const host = ref<HTMLElement | null>(null);
|
||||
|
|
@ -102,6 +107,12 @@ const caption = computed(() => {
|
|||
if (tab.remote) {
|
||||
return t("vcs.diff.captionRemote", { path: tab.path, server: tab.remote.server });
|
||||
}
|
||||
if (tab.local?.kind === "disk") {
|
||||
return t("history.diff.captionDisk", { path: tab.path });
|
||||
}
|
||||
if (tab.local?.kind === "history") {
|
||||
return t("history.diff.captionEntry", { path: tab.path, time: formatDateTime(tab.local.time) });
|
||||
}
|
||||
return refFor(tab.staged) === "HEAD"
|
||||
? t("vcs.diff.captionHead", { path: tab.path })
|
||||
: t("vcs.diff.captionIndex", { path: tab.path });
|
||||
|
|
@ -276,9 +287,19 @@ onBeforeUnmount(() => {
|
|||
data-test="diff-caption"
|
||||
>
|
||||
{{ caption }}
|
||||
<button
|
||||
v-if="diff.activeTab?.local?.kind === 'history'"
|
||||
type="button"
|
||||
class="diff-view__action"
|
||||
:disabled="history.busy"
|
||||
data-test="diff-history-revert"
|
||||
@click="diff.activeTab?.local?.kind === 'history' && history.revert(diff.activeTab.path, diff.activeTab.local.id)"
|
||||
>
|
||||
{{ t("history.revert") }}
|
||||
</button>
|
||||
</div>
|
||||
<div
|
||||
v-if="diff.activeTab && !diff.activeTab.commit && !diff.activeTab.remote && diff.activeContent?.phase === 'ready' && diff.activeContent.bufferId !== null"
|
||||
v-if="diff.activeTab && !diff.activeTab.commit && !diff.activeTab.remote && !diff.activeTab.local && diff.activeContent?.phase === 'ready' && diff.activeContent.bufferId !== null"
|
||||
class="diff-view__hint"
|
||||
data-test="diff-chunk-hint"
|
||||
>
|
||||
|
|
@ -318,6 +339,9 @@ onBeforeUnmount(() => {
|
|||
}
|
||||
|
||||
.diff-view__caption {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 12px;
|
||||
flex-shrink: 0;
|
||||
padding: 3px 12px;
|
||||
color: var(--lt-fg-muted);
|
||||
|
|
@ -328,6 +352,16 @@ onBeforeUnmount(() => {
|
|||
text-overflow: ellipsis;
|
||||
}
|
||||
|
||||
.diff-view__action {
|
||||
margin-left: auto;
|
||||
padding: 0 8px;
|
||||
font: inherit;
|
||||
color: var(--lt-fg);
|
||||
background: transparent;
|
||||
border: 1px solid var(--lt-border);
|
||||
border-radius: 3px;
|
||||
}
|
||||
|
||||
.diff-view__hint {
|
||||
flex-shrink: 0;
|
||||
padding: 2px 12px;
|
||||
|
|
|
|||
|
|
@ -36,12 +36,17 @@
|
|||
* which answers like `git.show`) with the working copy (right, the
|
||||
* ordinary buffer). Nothing is re-read on `gitChanged`; reopening the
|
||||
* tab reads the server again.
|
||||
* - **Local tabs (C10 A2):** {@link openDisk} compares the file on disk
|
||||
* (left, read-only, `file.readDisk {path}`) with the buffer: "Compare" in
|
||||
* the disk-conflict banner. {@link openHistory} compares a Local History
|
||||
* entry (left, `history.read {path, id}`) with the buffer. Both answer
|
||||
* like `git.show`; nothing is re-read on `gitChanged`.
|
||||
* - **Untrusted input:** `git.show` results are checked field by field.
|
||||
*/
|
||||
import { defineStore } from "pinia";
|
||||
import { computed, ref, shallowRef, watch } from "vue";
|
||||
import { t } from "../../../i18n";
|
||||
import { onEngineEvent, runWaiting, type EngineEventPayload, type JsonValue } from "../../../ipc";
|
||||
import { onEngineEvent, run, runWaiting, type EngineEventPayload, type JsonValue } from "../../../ipc";
|
||||
import { baseName, useEditorStore } from "../../../stores/editor";
|
||||
|
||||
/** Command id that opens a diff. */
|
||||
|
|
@ -90,6 +95,33 @@ export interface DiffTab {
|
|||
commit?: DiffCommit;
|
||||
/** For a server tab: the deployment server compared with. */
|
||||
remote?: DiffRemote;
|
||||
/** For a local tab: the disk text or a Local History entry. */
|
||||
local?: DiffLocal;
|
||||
}
|
||||
|
||||
/** What a local tab compares the buffer with (C10 A2). */
|
||||
export type DiffLocal =
|
||||
| {
|
||||
/** The file as it is on disk now. */
|
||||
kind: "disk";
|
||||
}
|
||||
| {
|
||||
/** A Local History entry. */
|
||||
kind: "history";
|
||||
/** The entry's id from `history.list`. */
|
||||
id: number;
|
||||
/** When it was taken, ms since the Unix epoch. */
|
||||
time: number;
|
||||
};
|
||||
|
||||
/**
|
||||
* The key of a local tab.
|
||||
*
|
||||
* @param path - project-relative path.
|
||||
* @param local - what it compares with.
|
||||
*/
|
||||
export function localDiffKey(path: string, local: DiffLocal): string {
|
||||
return local.kind === "disk" ? `disk:${path}` : `history:${String(local.id)}:${path}`;
|
||||
}
|
||||
|
||||
/** The server a server tab compares the working copy with. */
|
||||
|
|
@ -307,6 +339,15 @@ export const useDiffStore = defineStore("diff", () => {
|
|||
* @returns the checked result, or the error text.
|
||||
*/
|
||||
async function readLeft(tab: DiffTab): Promise<ShowResult | string> {
|
||||
if (tab.local) {
|
||||
const command = tab.local.kind === "disk" ? "file.readDisk" : "history.read";
|
||||
const params: Record<string, JsonValue> = tab.local.kind === "disk" ? { path: tab.path } : { path: tab.path, id: tab.local.id };
|
||||
const local = await run(command, params);
|
||||
if (!local.ok) {
|
||||
return local.error.message;
|
||||
}
|
||||
return toShowResult(local.value) ?? t("common.error.unexpectedResult", { command });
|
||||
}
|
||||
if (tab.remote) {
|
||||
const params: Record<string, JsonValue> = { path: tab.path };
|
||||
if (tab.remote.serverId !== null) {
|
||||
|
|
@ -502,6 +543,44 @@ export const useDiffStore = defineStore("diff", () => {
|
|||
await load(tab);
|
||||
}
|
||||
|
||||
/**
|
||||
* Opens (or activates and reloads) the comparison of a file's buffer
|
||||
* with the file on disk, or with a Local History entry.
|
||||
*
|
||||
* @param path - project-relative path.
|
||||
* @param local - what to compare with.
|
||||
*/
|
||||
async function openLocal(path: string, local: DiffLocal): Promise<void> {
|
||||
const key = localDiffKey(path, local);
|
||||
let tab = tabs.value.find((t) => t.key === key);
|
||||
if (!tab) {
|
||||
tab = { key, path, staged: false, name: baseName(path), local };
|
||||
tabs.value.push(tab);
|
||||
}
|
||||
activate(key);
|
||||
await load(tab);
|
||||
}
|
||||
|
||||
/**
|
||||
* Opens the comparison of a file's buffer with the file on disk.
|
||||
*
|
||||
* @param path - project-relative path.
|
||||
*/
|
||||
async function openDisk(path: string): Promise<void> {
|
||||
await openLocal(path, { kind: "disk" });
|
||||
}
|
||||
|
||||
/**
|
||||
* Opens the comparison of a file's buffer with a Local History entry.
|
||||
*
|
||||
* @param path - project-relative path.
|
||||
* @param id - the entry's id.
|
||||
* @param time - when it was taken, ms since the Unix epoch.
|
||||
*/
|
||||
async function openHistory(path: string, id: number, time: number): Promise<void> {
|
||||
await openLocal(path, { kind: "history", id, time });
|
||||
}
|
||||
|
||||
/**
|
||||
* Re-reads the left side of every loaded text tab; a changed text
|
||||
* replaces the left document (`gitChanged`).
|
||||
|
|
@ -510,7 +589,7 @@ export const useDiffStore = defineStore("diff", () => {
|
|||
await Promise.all(
|
||||
tabs.value.map(async (tab) => {
|
||||
const before = contents.value[tab.key];
|
||||
if (before?.phase !== "ready" || tab.commit || tab.remote) {
|
||||
if (before?.phase !== "ready" || tab.commit || tab.remote || tab.local) {
|
||||
return;
|
||||
}
|
||||
const generation = generations.get(tab.key);
|
||||
|
|
@ -624,6 +703,8 @@ export const useDiffStore = defineStore("diff", () => {
|
|||
open,
|
||||
openAtCommit,
|
||||
openRemote,
|
||||
openDisk,
|
||||
openHistory,
|
||||
showDiff,
|
||||
refreshLeft,
|
||||
close,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue