2026-03-31 01:29:35 +02:00
|
|
|
import { CommandHistory } from "../commands/command-history";
|
|
|
|
|
import type { CommandContext, EditorCommand } from "../commands/command";
|
|
|
|
|
import type { EditorSelection } from "../core/selection";
|
|
|
|
|
import type { ToolMode } from "../core/tool-mode";
|
|
|
|
|
import { createEmptySceneDocument, type SceneDocument } from "../document/scene-document";
|
|
|
|
|
import {
|
|
|
|
|
DEFAULT_SCENE_DRAFT_STORAGE_KEY,
|
2026-03-31 01:50:29 +02:00
|
|
|
type LoadSceneDocumentDraftResult,
|
2026-03-31 01:29:35 +02:00
|
|
|
loadSceneDocumentDraft,
|
|
|
|
|
type KeyValueStorage,
|
2026-03-31 01:50:29 +02:00
|
|
|
type SaveSceneDocumentDraftResult,
|
2026-03-31 01:29:35 +02:00
|
|
|
saveSceneDocumentDraft
|
|
|
|
|
} from "../serialization/local-draft-storage";
|
|
|
|
|
import { parseSceneDocumentJson, serializeSceneDocument } from "../serialization/scene-document-json";
|
2026-04-02 22:10:29 +02:00
|
|
|
import type { ViewportViewMode } from "../viewport-three/viewport-view-modes";
|
2026-04-02 23:01:21 +02:00
|
|
|
import {
|
|
|
|
|
areViewportToolPreviewsEqual,
|
|
|
|
|
cloneViewportToolPreview,
|
|
|
|
|
createDefaultViewportTransientState,
|
2026-04-02 23:50:42 +02:00
|
|
|
isViewportToolPreviewCompatible,
|
2026-04-02 23:01:21 +02:00
|
|
|
type ViewportToolPreview,
|
|
|
|
|
type ViewportTransientState
|
|
|
|
|
} from "../viewport-three/viewport-transient-state";
|
2026-04-02 22:32:40 +02:00
|
|
|
import {
|
|
|
|
|
createDefaultViewportLayoutState,
|
|
|
|
|
type ViewportDisplayMode,
|
|
|
|
|
type ViewportLayoutMode,
|
|
|
|
|
type ViewportPanelId,
|
|
|
|
|
type ViewportPanelState
|
|
|
|
|
} from "../viewport-three/viewport-layout";
|
2026-03-31 01:29:35 +02:00
|
|
|
|
|
|
|
|
export interface EditorStoreState {
|
|
|
|
|
document: SceneDocument;
|
|
|
|
|
selection: EditorSelection;
|
|
|
|
|
toolMode: ToolMode;
|
2026-04-02 22:32:40 +02:00
|
|
|
viewportLayoutMode: ViewportLayoutMode;
|
|
|
|
|
activeViewportPanelId: ViewportPanelId;
|
|
|
|
|
viewportPanels: Record<ViewportPanelId, ViewportPanelState>;
|
2026-04-02 23:01:21 +02:00
|
|
|
viewportTransientState: ViewportTransientState;
|
2026-03-31 01:29:35 +02:00
|
|
|
canUndo: boolean;
|
|
|
|
|
canRedo: boolean;
|
|
|
|
|
lastCommandLabel: string | null;
|
2026-03-31 03:42:16 +02:00
|
|
|
storageAvailable: boolean;
|
2026-03-31 01:29:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface EditorStoreOptions {
|
|
|
|
|
initialDocument?: SceneDocument;
|
|
|
|
|
storage?: KeyValueStorage | null;
|
|
|
|
|
storageKey?: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type EditorStoreListener = () => void;
|
|
|
|
|
|
2026-03-31 01:50:29 +02:00
|
|
|
export type EditorDraftSaveResult = SaveSceneDocumentDraftResult;
|
|
|
|
|
export type EditorDraftLoadResult = LoadSceneDocumentDraftResult;
|
|
|
|
|
|
2026-03-31 01:29:35 +02:00
|
|
|
export class EditorStore {
|
|
|
|
|
private document: SceneDocument;
|
|
|
|
|
private selection: EditorSelection = { kind: "none" };
|
|
|
|
|
private toolMode: ToolMode = "select";
|
2026-04-02 22:32:40 +02:00
|
|
|
private viewportLayoutMode: ViewportLayoutMode = "single";
|
|
|
|
|
private activeViewportPanelId: ViewportPanelId = "topLeft";
|
|
|
|
|
private viewportPanels = createDefaultViewportLayoutState().panels;
|
2026-04-02 23:01:21 +02:00
|
|
|
private viewportTransientState = createDefaultViewportTransientState();
|
2026-03-31 03:06:48 +02:00
|
|
|
private previousEditingToolMode: Exclude<ToolMode, "play"> = "select";
|
2026-03-31 01:29:35 +02:00
|
|
|
private readonly history = new CommandHistory();
|
|
|
|
|
private readonly listeners = new Set<EditorStoreListener>();
|
|
|
|
|
private readonly storage: KeyValueStorage | null;
|
|
|
|
|
private readonly storageKey: string;
|
|
|
|
|
private lastCommandLabel: string | null = null;
|
2026-03-31 01:36:05 +02:00
|
|
|
private snapshot: EditorStoreState;
|
2026-03-31 01:29:35 +02:00
|
|
|
|
|
|
|
|
private readonly commandContext: CommandContext = {
|
|
|
|
|
getDocument: () => this.document,
|
|
|
|
|
setDocument: (document) => {
|
|
|
|
|
this.document = document;
|
|
|
|
|
},
|
|
|
|
|
getSelection: () => this.selection,
|
|
|
|
|
setSelection: (selection) => {
|
|
|
|
|
this.selection = selection;
|
|
|
|
|
},
|
|
|
|
|
getToolMode: () => this.toolMode,
|
|
|
|
|
setToolMode: (toolMode) => {
|
|
|
|
|
this.toolMode = toolMode;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
constructor(options: EditorStoreOptions = {}) {
|
|
|
|
|
this.document = options.initialDocument ?? createEmptySceneDocument();
|
|
|
|
|
this.storage = options.storage ?? null;
|
|
|
|
|
this.storageKey = options.storageKey ?? DEFAULT_SCENE_DRAFT_STORAGE_KEY;
|
2026-03-31 01:36:05 +02:00
|
|
|
this.snapshot = this.createSnapshot();
|
2026-03-31 01:29:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
subscribe = (listener: EditorStoreListener) => {
|
|
|
|
|
this.listeners.add(listener);
|
|
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
|
this.listeners.delete(listener);
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2026-03-31 01:36:05 +02:00
|
|
|
getState = (): EditorStoreState => this.snapshot;
|
2026-03-31 01:29:35 +02:00
|
|
|
|
|
|
|
|
setToolMode(toolMode: ToolMode) {
|
|
|
|
|
if (this.toolMode === toolMode) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-31 03:06:48 +02:00
|
|
|
if (toolMode !== "play") {
|
|
|
|
|
this.previousEditingToolMode = toolMode;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-31 01:29:35 +02:00
|
|
|
this.toolMode = toolMode;
|
2026-04-02 23:01:21 +02:00
|
|
|
|
2026-04-02 23:50:42 +02:00
|
|
|
if (!isViewportToolPreviewCompatible(toolMode, this.viewportTransientState.toolPreview)) {
|
2026-04-02 23:01:21 +02:00
|
|
|
this.viewportTransientState = createDefaultViewportTransientState();
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-31 01:29:35 +02:00
|
|
|
this.emit();
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-02 22:32:40 +02:00
|
|
|
setViewportLayoutMode(viewportLayoutMode: ViewportLayoutMode) {
|
|
|
|
|
if (this.viewportLayoutMode === viewportLayoutMode) {
|
2026-04-02 22:10:29 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-02 22:32:40 +02:00
|
|
|
this.viewportLayoutMode = viewportLayoutMode;
|
2026-04-02 22:10:29 +02:00
|
|
|
this.emit();
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-02 22:32:40 +02:00
|
|
|
setActiveViewportPanel(panelId: ViewportPanelId) {
|
|
|
|
|
if (this.activeViewportPanelId === panelId) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.activeViewportPanelId = panelId;
|
|
|
|
|
this.emit();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setViewportPanelViewMode(panelId: ViewportPanelId, viewMode: ViewportViewMode) {
|
|
|
|
|
if (this.viewportPanels[panelId].viewMode === viewMode) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.viewportPanels = {
|
|
|
|
|
...this.viewportPanels,
|
|
|
|
|
[panelId]: {
|
|
|
|
|
...this.viewportPanels[panelId],
|
|
|
|
|
viewMode
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
this.emit();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setViewportPanelDisplayMode(panelId: ViewportPanelId, displayMode: ViewportDisplayMode) {
|
|
|
|
|
if (this.viewportPanels[panelId].displayMode === displayMode) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.viewportPanels = {
|
|
|
|
|
...this.viewportPanels,
|
|
|
|
|
[panelId]: {
|
|
|
|
|
...this.viewportPanels[panelId],
|
|
|
|
|
displayMode
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
this.emit();
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-02 23:01:21 +02:00
|
|
|
setViewportToolPreview(toolPreview: ViewportToolPreview) {
|
|
|
|
|
const nextToolPreview = cloneViewportToolPreview(toolPreview);
|
|
|
|
|
|
|
|
|
|
if (areViewportToolPreviewsEqual(this.viewportTransientState.toolPreview, nextToolPreview)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.viewportTransientState = {
|
|
|
|
|
...this.viewportTransientState,
|
|
|
|
|
toolPreview: nextToolPreview
|
|
|
|
|
};
|
|
|
|
|
this.emit();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
clearViewportToolPreview(sourcePanelId?: ViewportPanelId) {
|
|
|
|
|
const currentToolPreview = this.viewportTransientState.toolPreview;
|
|
|
|
|
|
|
|
|
|
if (currentToolPreview.kind === "none") {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (sourcePanelId !== undefined && currentToolPreview.sourcePanelId !== sourcePanelId) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.viewportTransientState = createDefaultViewportTransientState();
|
|
|
|
|
this.emit();
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-02 22:32:40 +02:00
|
|
|
setViewportViewMode(viewportViewMode: ViewportViewMode) {
|
|
|
|
|
this.setViewportPanelViewMode(this.activeViewportPanelId, viewportViewMode);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-31 03:06:48 +02:00
|
|
|
enterPlayMode() {
|
|
|
|
|
if (this.toolMode === "play") {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-31 03:10:48 +02:00
|
|
|
this.previousEditingToolMode = this.toolMode;
|
2026-03-31 03:06:48 +02:00
|
|
|
this.toolMode = "play";
|
2026-04-02 23:01:21 +02:00
|
|
|
|
|
|
|
|
if (this.viewportTransientState.toolPreview.kind !== "none") {
|
|
|
|
|
this.viewportTransientState = createDefaultViewportTransientState();
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-31 03:06:48 +02:00
|
|
|
this.emit();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
exitPlayMode() {
|
|
|
|
|
if (this.toolMode !== "play") {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.toolMode = this.previousEditingToolMode;
|
|
|
|
|
this.emit();
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-31 01:29:35 +02:00
|
|
|
setSelection(selection: EditorSelection) {
|
|
|
|
|
this.selection = selection;
|
|
|
|
|
this.emit();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
executeCommand(command: EditorCommand) {
|
|
|
|
|
this.history.execute(command, this.commandContext);
|
|
|
|
|
this.lastCommandLabel = command.label;
|
|
|
|
|
this.emit();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
undo(): boolean {
|
|
|
|
|
const command = this.history.undo(this.commandContext);
|
|
|
|
|
|
|
|
|
|
if (command === null) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.lastCommandLabel = `Undid ${command.label}`;
|
|
|
|
|
this.emit();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
redo(): boolean {
|
|
|
|
|
const command = this.history.redo(this.commandContext);
|
|
|
|
|
|
|
|
|
|
if (command === null) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.lastCommandLabel = `Redid ${command.label}`;
|
|
|
|
|
this.emit();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
replaceDocument(document: SceneDocument, resetHistory = true) {
|
|
|
|
|
this.document = document;
|
|
|
|
|
this.selection = { kind: "none" };
|
2026-03-31 01:30:33 +02:00
|
|
|
this.toolMode = "select";
|
2026-03-31 03:06:48 +02:00
|
|
|
this.previousEditingToolMode = "select";
|
2026-04-02 23:01:21 +02:00
|
|
|
this.viewportTransientState = createDefaultViewportTransientState();
|
2026-03-31 01:29:35 +02:00
|
|
|
|
|
|
|
|
if (resetHistory) {
|
|
|
|
|
this.history.clear();
|
|
|
|
|
this.lastCommandLabel = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.emit();
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-31 01:50:29 +02:00
|
|
|
saveDraft(): EditorDraftSaveResult {
|
2026-03-31 01:29:35 +02:00
|
|
|
if (this.storage === null) {
|
2026-03-31 01:50:29 +02:00
|
|
|
return {
|
|
|
|
|
status: "error",
|
|
|
|
|
message: "Browser local storage is unavailable."
|
|
|
|
|
};
|
2026-03-31 01:29:35 +02:00
|
|
|
}
|
|
|
|
|
|
2026-03-31 01:50:29 +02:00
|
|
|
return saveSceneDocumentDraft(this.storage, this.document, this.storageKey);
|
2026-03-31 01:29:35 +02:00
|
|
|
}
|
|
|
|
|
|
2026-03-31 01:50:29 +02:00
|
|
|
loadDraft(): EditorDraftLoadResult {
|
2026-03-31 01:29:35 +02:00
|
|
|
if (this.storage === null) {
|
2026-03-31 01:50:29 +02:00
|
|
|
return {
|
|
|
|
|
status: "error",
|
|
|
|
|
message: "Browser local storage is unavailable."
|
|
|
|
|
};
|
2026-03-31 01:29:35 +02:00
|
|
|
}
|
|
|
|
|
|
2026-03-31 01:50:29 +02:00
|
|
|
const draftResult = loadSceneDocumentDraft(this.storage, this.storageKey);
|
2026-03-31 01:29:35 +02:00
|
|
|
|
2026-03-31 01:50:29 +02:00
|
|
|
if (draftResult.status !== "loaded") {
|
|
|
|
|
return draftResult;
|
2026-03-31 01:29:35 +02:00
|
|
|
}
|
|
|
|
|
|
2026-03-31 01:50:29 +02:00
|
|
|
this.replaceDocument(draftResult.document);
|
|
|
|
|
return draftResult;
|
2026-03-31 01:29:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
exportDocumentJson(): string {
|
|
|
|
|
return serializeSceneDocument(this.document);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
importDocumentJson(source: string): SceneDocument {
|
|
|
|
|
const document = parseSceneDocumentJson(source);
|
|
|
|
|
this.replaceDocument(document);
|
|
|
|
|
return document;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private emit() {
|
2026-03-31 01:36:05 +02:00
|
|
|
this.snapshot = this.createSnapshot();
|
|
|
|
|
|
2026-03-31 01:29:35 +02:00
|
|
|
for (const listener of this.listeners) {
|
|
|
|
|
listener();
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-03-31 01:36:05 +02:00
|
|
|
|
|
|
|
|
private createSnapshot(): EditorStoreState {
|
|
|
|
|
return {
|
|
|
|
|
document: this.document,
|
|
|
|
|
selection: this.selection,
|
|
|
|
|
toolMode: this.toolMode,
|
2026-04-02 22:32:40 +02:00
|
|
|
viewportLayoutMode: this.viewportLayoutMode,
|
|
|
|
|
activeViewportPanelId: this.activeViewportPanelId,
|
|
|
|
|
viewportPanels: this.viewportPanels,
|
2026-04-02 23:01:21 +02:00
|
|
|
viewportTransientState: this.viewportTransientState,
|
2026-03-31 01:36:05 +02:00
|
|
|
canUndo: this.history.canUndo(),
|
|
|
|
|
canRedo: this.history.canRedo(),
|
2026-03-31 03:42:16 +02:00
|
|
|
lastCommandLabel: this.lastCommandLabel,
|
|
|
|
|
storageAvailable: this.storage !== null
|
2026-03-31 01:36:05 +02:00
|
|
|
};
|
|
|
|
|
}
|
2026-03-31 01:29:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function createEditorStore(options?: EditorStoreOptions): EditorStore {
|
|
|
|
|
return new EditorStore(options);
|
|
|
|
|
}
|