Keeping last project open
This commit is contained in:
@ -21,6 +21,7 @@
|
||||
templateRegistry,
|
||||
type CsoundTemplate,
|
||||
} from "./lib/templates/template-registry";
|
||||
import { loadLastProjectId } from "./lib/project-system/persistence";
|
||||
import { createAppContext, setAppContext } from "./lib/contexts/app-context";
|
||||
import type { ProjectMode } from "./lib/project-system/types";
|
||||
import {
|
||||
@ -53,20 +54,43 @@
|
||||
onMount(async () => {
|
||||
await projectManager.init();
|
||||
|
||||
const result = await projectManager.getAllProjects();
|
||||
if (result.success && result.data.length === 0) {
|
||||
const classicTemplate = templateRegistry.getById("classic");
|
||||
if (classicTemplate) {
|
||||
await projectManager.createProject({
|
||||
title: "Welcome",
|
||||
author: "System",
|
||||
content: classicTemplate.content,
|
||||
tags: [],
|
||||
mode: classicTemplate.mode,
|
||||
});
|
||||
const lastProjectId = loadLastProjectId();
|
||||
let projectToLoad: CsoundProject | null = null;
|
||||
|
||||
if (lastProjectId) {
|
||||
const result = await projectManager.getProject(lastProjectId);
|
||||
if (result.success) {
|
||||
projectToLoad = result.data;
|
||||
}
|
||||
}
|
||||
|
||||
if (!projectToLoad) {
|
||||
const allProjectsResult = await projectManager.getAllProjects();
|
||||
if (allProjectsResult.success) {
|
||||
if (allProjectsResult.data.length > 0) {
|
||||
projectToLoad = allProjectsResult.data[0];
|
||||
} else {
|
||||
const classicTemplate = templateRegistry.getById("classic");
|
||||
if (classicTemplate) {
|
||||
const createResult = await projectManager.createProject({
|
||||
title: "Welcome",
|
||||
author: "System",
|
||||
content: classicTemplate.content,
|
||||
tags: [],
|
||||
mode: classicTemplate.mode,
|
||||
});
|
||||
if (createResult.success) {
|
||||
projectToLoad = createResult.data;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (projectToLoad) {
|
||||
projectEditor.loadProject(projectToLoad);
|
||||
}
|
||||
|
||||
logsUnsubscribe = csoundDerived.logs.subscribe((logs) => {
|
||||
interpreterLogs = logs;
|
||||
});
|
||||
@ -344,6 +368,7 @@
|
||||
onExecute={handleExecute}
|
||||
logs={interpreterLogs}
|
||||
{editorSettings}
|
||||
mode={projectEditor.currentProject?.mode || 'composition'}
|
||||
/>
|
||||
</div>
|
||||
|
||||
|
||||
@ -25,13 +25,15 @@
|
||||
onChange?: (value: string) => void;
|
||||
onExecute?: (code: string, source: 'selection' | 'block' | 'document') => void;
|
||||
editorSettings: EditorSettingsStore;
|
||||
mode: 'composition' | 'livecoding';
|
||||
}
|
||||
|
||||
let {
|
||||
value = '',
|
||||
onChange,
|
||||
onExecute,
|
||||
editorSettings
|
||||
editorSettings,
|
||||
mode = 'composition'
|
||||
}: Props = $props();
|
||||
|
||||
let editorContainer: HTMLDivElement;
|
||||
@ -44,6 +46,13 @@
|
||||
function handleExecute() {
|
||||
if (!editorView) return;
|
||||
|
||||
if (mode === 'composition') {
|
||||
const doc = getDocument(editorView.state);
|
||||
flash(editorView, doc.from, doc.to);
|
||||
onExecute?.(doc.text, 'document');
|
||||
return;
|
||||
}
|
||||
|
||||
const selection = getSelection(editorView.state);
|
||||
if (selection.text) {
|
||||
flash(editorView, selection.from, selection.to);
|
||||
|
||||
@ -10,6 +10,7 @@
|
||||
onExecute?: (code: string, source: 'selection' | 'block' | 'document') => void;
|
||||
logs?: string[];
|
||||
editorSettings: EditorSettingsStore;
|
||||
mode: 'composition' | 'livecoding';
|
||||
}
|
||||
|
||||
let {
|
||||
@ -17,7 +18,8 @@
|
||||
onChange,
|
||||
onExecute,
|
||||
logs = [],
|
||||
editorSettings
|
||||
editorSettings,
|
||||
mode = 'composition'
|
||||
}: Props = $props();
|
||||
|
||||
let logPanelRef: LogPanel;
|
||||
@ -84,6 +86,7 @@
|
||||
{onChange}
|
||||
{onExecute}
|
||||
{editorSettings}
|
||||
{mode}
|
||||
/>
|
||||
</div>
|
||||
|
||||
|
||||
17
src/lib/project-system/persistence.ts
Normal file
17
src/lib/project-system/persistence.ts
Normal file
@ -0,0 +1,17 @@
|
||||
const LAST_PROJECT_KEY = 'oldboy:lastProjectId';
|
||||
|
||||
export function saveLastProjectId(projectId: string | null): void {
|
||||
if (projectId === null) {
|
||||
localStorage.removeItem(LAST_PROJECT_KEY);
|
||||
} else {
|
||||
localStorage.setItem(LAST_PROJECT_KEY, projectId);
|
||||
}
|
||||
}
|
||||
|
||||
export function loadLastProjectId(): string | null {
|
||||
return localStorage.getItem(LAST_PROJECT_KEY);
|
||||
}
|
||||
|
||||
export function clearLastProjectId(): void {
|
||||
localStorage.removeItem(LAST_PROJECT_KEY);
|
||||
}
|
||||
@ -1,4 +1,5 @@
|
||||
import type { CsoundProject, ProjectManager } from '../project-system';
|
||||
import { saveLastProjectId } from '../project-system/persistence';
|
||||
|
||||
interface ProjectEditorState {
|
||||
currentProject: CsoundProject | null;
|
||||
@ -54,6 +55,7 @@ export class ProjectEditor {
|
||||
this.state.content = project.content;
|
||||
this.state.hasUnsavedChanges = false;
|
||||
this.state.isNewUnsavedBuffer = false;
|
||||
saveLastProjectId(project.id);
|
||||
}
|
||||
|
||||
createNew(template: string) {
|
||||
@ -61,6 +63,7 @@ export class ProjectEditor {
|
||||
this.state.content = template;
|
||||
this.state.hasUnsavedChanges = false;
|
||||
this.state.isNewUnsavedBuffer = true;
|
||||
saveLastProjectId(null);
|
||||
}
|
||||
|
||||
async save(): Promise<boolean> {
|
||||
@ -105,6 +108,7 @@ export class ProjectEditor {
|
||||
this.state.currentProject = result.data;
|
||||
this.state.hasUnsavedChanges = false;
|
||||
this.state.isNewUnsavedBuffer = false;
|
||||
saveLastProjectId(result.data.id);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user