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