Skip to content

Commit e015a44

Browse files
add workspace seed option
1 parent f280477 commit e015a44

4 files changed

Lines changed: 31 additions & 12 deletions

File tree

editor/core/states/workspace-initial-state.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { EditorSnapshot } from "./editor-state";
2-
import { WorkspaceState } from "./workspace-state";
2+
import { WorkspaceState, EssentialWorkspaceInfo } from "./workspace-state";
33
import {
44
createInitialHistoryState,
55
createPendingHistoryState,
@@ -23,7 +23,7 @@ export function merge_initial_workspace_state_with_editor_snapshot(
2323
};
2424
}
2525

26-
export function create_initial_pending_workspace_state(): WorkspaceState {
26+
export function create_initial_pending_workspace_state({}: EssentialWorkspaceInfo): WorkspaceState {
2727
return {
2828
taskQueue: {
2929
isBusy: false,

editor/core/states/workspace-state.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import { config } from "@grida/builder-config";
22
import { HistoryState } from "core/states/history-state";
33

4-
export interface WorkspaceState {
4+
export interface EssentialWorkspaceInfo {
5+
// Add workspace seed data here, which cannot be automatically filled on initial state.
6+
}
7+
8+
export interface WorkspaceState extends EssentialWorkspaceInfo {
59
history: HistoryState;
610
/**
711
* hovered layer; single or none.

editor/scaffolds/workspace/warmup.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,29 @@ import {
33
EditorSnapshot,
44
WorkspaceState,
55
} from "core/states";
6-
import { merge_initial_workspace_state_with_editor_snapshot } from "core/states";
6+
import {
7+
merge_initial_workspace_state_with_editor_snapshot,
8+
EssentialWorkspaceInfo,
9+
} from "core/states";
710
import { workspaceWarmupReducer, workspaceReducer } from "core/reducers";
811
import { PendingState, PendingState_Pending } from "core/utility-types";
912
import { WorkspaceAction, WorkspaceWarmupAction } from "core/actions";
1013

11-
const initial_pending_workspace_state =
12-
create_initial_pending_workspace_state();
1314
//
1415
export type InitializationAction =
1516
| { type: "warmup"; value: WorkspaceWarmupAction }
1617
| { type: "setup-with-editor-snapshot"; value: EditorSnapshot }
1718
| { type: "update"; value: WorkspaceAction };
1819

20+
type TState = PendingState<WorkspaceState> & EssentialWorkspaceInfo;
21+
1922
export function initialReducer(
20-
state: PendingState<WorkspaceState>,
23+
state: TState,
2124
action: InitializationAction
2225
): PendingState<WorkspaceState> {
26+
const initial_pending_workspace_state =
27+
create_initial_pending_workspace_state({});
28+
2329
switch (action.type) {
2430
case "setup-with-editor-snapshot":
2531
return {
@@ -63,13 +69,16 @@ export function initialReducer(
6369
}
6470
}
6571

66-
export function safestate(initialState) {
72+
export function safestate(initialState: TState): WorkspaceState {
73+
const initial_pending_workspace_state =
74+
create_initial_pending_workspace_state({});
75+
6776
switch (initialState.type) {
6877
case "success":
6978
return initialState.value;
7079
case "pending": {
7180
if (initialState.value) {
72-
return initialState.value;
81+
return initialState.value as WorkspaceState;
7382
} else {
7483
return initial_pending_workspace_state;
7584
}

editor/scaffolds/workspace/workspace.tsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import React, {
55
createContext,
66
} from "react";
77
import { useRouter } from "next/router";
8-
import { StateProvider } from "core/states";
8+
import { EssentialWorkspaceInfo, StateProvider } from "core/states";
99
import { SetupWorkspace } from "./setup";
1010
import { WorkspaceDefaultProviders } from "./_providers";
1111
import * as warmup from "./warmup";
@@ -21,7 +21,10 @@ export function useWorkspaceInitializerContext() {
2121
return useContext(WorkspaceInitializerContext);
2222
}
2323

24-
export function Workspace({ children }: React.PropsWithChildren<{}>) {
24+
export function Workspace({
25+
children,
26+
...seed
27+
}: React.PropsWithChildren<EssentialWorkspaceInfo>) {
2528
const router = useRouter();
2629

2730
const handleDispatch = useCallback((action: WorkspaceAction) => {
@@ -46,7 +49,10 @@ export function Workspace({ children }: React.PropsWithChildren<{}>) {
4649
type: "pending",
4750
});
4851

49-
const safe_value = warmup.safestate(initialState);
52+
const safe_value = warmup.safestate({
53+
...initialState,
54+
...seed,
55+
});
5056

5157
return (
5258
<>

0 commit comments

Comments
 (0)