mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-04-28 10:26:33 +02:00
feat(editor): implement auto-save functionality and manual save command in SourceCodeEditor
This commit is contained in:
parent
d397fec54f
commit
3f203f8c49
3 changed files with 63 additions and 6 deletions
|
|
@ -187,7 +187,7 @@ export function EditorPanelContent({
|
||||||
setEditedMarkdown(md);
|
setEditedMarkdown(md);
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const handleSave = useCallback(async () => {
|
const handleSave = useCallback(async (options?: { silent?: boolean }) => {
|
||||||
setSaving(true);
|
setSaving(true);
|
||||||
try {
|
try {
|
||||||
if (isLocalFileMode) {
|
if (isLocalFileMode) {
|
||||||
|
|
@ -197,18 +197,18 @@ export function EditorPanelContent({
|
||||||
if (!electronAPI?.writeAgentLocalFileText) {
|
if (!electronAPI?.writeAgentLocalFileText) {
|
||||||
throw new Error("Local file editor is available only in desktop mode.");
|
throw new Error("Local file editor is available only in desktop mode.");
|
||||||
}
|
}
|
||||||
|
const contentToSave = markdownRef.current;
|
||||||
const writeResult = await electronAPI.writeAgentLocalFileText(
|
const writeResult = await electronAPI.writeAgentLocalFileText(
|
||||||
localFilePath,
|
localFilePath,
|
||||||
markdownRef.current
|
contentToSave
|
||||||
);
|
);
|
||||||
if (!writeResult.ok) {
|
if (!writeResult.ok) {
|
||||||
throw new Error(writeResult.error || "Failed to save local file");
|
throw new Error(writeResult.error || "Failed to save local file");
|
||||||
}
|
}
|
||||||
setEditorDoc((prev) =>
|
setEditorDoc((prev) =>
|
||||||
prev ? { ...prev, source_markdown: markdownRef.current } : prev
|
prev ? { ...prev, source_markdown: contentToSave } : prev
|
||||||
);
|
);
|
||||||
setEditedMarkdown(null);
|
setEditedMarkdown(markdownRef.current === contentToSave ? null : markdownRef.current);
|
||||||
toast.success("File saved");
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (!searchSpaceId || !documentId) {
|
if (!searchSpaceId || !documentId) {
|
||||||
|
|
@ -363,6 +363,8 @@ export function EditorPanelContent({
|
||||||
path={localFilePath ?? "local-file.txt"}
|
path={localFilePath ?? "local-file.txt"}
|
||||||
language={localFileLanguage}
|
language={localFileLanguage}
|
||||||
value={localFileContent}
|
value={localFileContent}
|
||||||
|
onSave={() => handleSave({ silent: true })}
|
||||||
|
saveMode="auto"
|
||||||
onChange={(next) => {
|
onChange={(next) => {
|
||||||
markdownRef.current = next;
|
markdownRef.current = next;
|
||||||
setLocalFileContent(next);
|
setLocalFileContent(next);
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
import dynamic from "next/dynamic";
|
import dynamic from "next/dynamic";
|
||||||
|
import { useEffect, useRef } from "react";
|
||||||
import { useTheme } from "next-themes";
|
import { useTheme } from "next-themes";
|
||||||
import { Spinner } from "@/components/ui/spinner";
|
import { Spinner } from "@/components/ui/spinner";
|
||||||
|
|
||||||
|
|
@ -15,6 +16,9 @@ interface SourceCodeEditorProps {
|
||||||
language?: string;
|
language?: string;
|
||||||
readOnly?: boolean;
|
readOnly?: boolean;
|
||||||
fontSize?: number;
|
fontSize?: number;
|
||||||
|
onSave?: () => Promise<void> | void;
|
||||||
|
saveMode?: "manual" | "auto" | "both";
|
||||||
|
autoSaveDelayMs?: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function SourceCodeEditor({
|
export function SourceCodeEditor({
|
||||||
|
|
@ -24,8 +28,50 @@ export function SourceCodeEditor({
|
||||||
language = "plaintext",
|
language = "plaintext",
|
||||||
readOnly = false,
|
readOnly = false,
|
||||||
fontSize = 12,
|
fontSize = 12,
|
||||||
|
onSave,
|
||||||
|
saveMode = "manual",
|
||||||
|
autoSaveDelayMs = 800,
|
||||||
}: SourceCodeEditorProps) {
|
}: SourceCodeEditorProps) {
|
||||||
const { resolvedTheme } = useTheme();
|
const { resolvedTheme } = useTheme();
|
||||||
|
const saveTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
|
||||||
|
const onSaveRef = useRef(onSave);
|
||||||
|
const skipNextAutoSaveRef = useRef(true);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
onSaveRef.current = onSave;
|
||||||
|
}, [onSave]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
skipNextAutoSaveRef.current = true;
|
||||||
|
}, [path]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (readOnly || !onSaveRef.current) return;
|
||||||
|
if (saveMode !== "auto" && saveMode !== "both") return;
|
||||||
|
|
||||||
|
if (skipNextAutoSaveRef.current) {
|
||||||
|
skipNextAutoSaveRef.current = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (saveTimerRef.current) {
|
||||||
|
clearTimeout(saveTimerRef.current);
|
||||||
|
}
|
||||||
|
|
||||||
|
saveTimerRef.current = setTimeout(() => {
|
||||||
|
void onSaveRef.current?.();
|
||||||
|
saveTimerRef.current = null;
|
||||||
|
}, autoSaveDelayMs);
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
if (saveTimerRef.current) {
|
||||||
|
clearTimeout(saveTimerRef.current);
|
||||||
|
saveTimerRef.current = null;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}, [autoSaveDelayMs, readOnly, saveMode, value]);
|
||||||
|
|
||||||
|
const isManualSaveEnabled = !!onSave && !readOnly && (saveMode === "manual" || saveMode === "both");
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="h-full w-full overflow-hidden bg-sidebar">
|
<div className="h-full w-full overflow-hidden bg-sidebar">
|
||||||
|
|
@ -40,6 +86,12 @@ export function SourceCodeEditor({
|
||||||
<Spinner size="sm" className="text-muted-foreground" />
|
<Spinner size="sm" className="text-muted-foreground" />
|
||||||
</div>
|
</div>
|
||||||
}
|
}
|
||||||
|
onMount={(editor, monaco) => {
|
||||||
|
if (!isManualSaveEnabled) return;
|
||||||
|
editor.addCommand(monaco.KeyMod.CtrlCmd | monaco.KeyCode.KeyS, () => {
|
||||||
|
void onSaveRef.current?.();
|
||||||
|
});
|
||||||
|
}}
|
||||||
options={{
|
options={{
|
||||||
automaticLayout: true,
|
automaticLayout: true,
|
||||||
minimap: { enabled: false },
|
minimap: { enabled: false },
|
||||||
|
|
@ -51,6 +103,9 @@ export function SourceCodeEditor({
|
||||||
overviewRulerLanes: 0,
|
overviewRulerLanes: 0,
|
||||||
hideCursorInOverviewRuler: true,
|
hideCursorInOverviewRuler: true,
|
||||||
scrollBeyondLastLine: false,
|
scrollBeyondLastLine: false,
|
||||||
|
renderLineHighlight: "none",
|
||||||
|
selectionHighlight: false,
|
||||||
|
occurrencesHighlight: "off",
|
||||||
wordWrap: "off",
|
wordWrap: "off",
|
||||||
scrollbar: {
|
scrollbar: {
|
||||||
vertical: "hidden",
|
vertical: "hidden",
|
||||||
|
|
|
||||||
|
|
@ -53,7 +53,7 @@ function CollapseButton({ onClick }: { onClick: () => void }) {
|
||||||
<span className="sr-only">Collapse panel</span>
|
<span className="sr-only">Collapse panel</span>
|
||||||
</Button>
|
</Button>
|
||||||
</TooltipTrigger>
|
</TooltipTrigger>
|
||||||
<TooltipContent side="left">Collapse panel</TooltipContent>
|
<TooltipContent side="bottom">Collapse panel</TooltipContent>
|
||||||
</Tooltip>
|
</Tooltip>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue