Skip to content

Commit 71b401c

Browse files
extract some preference pages with register method to be managed externally
1 parent c05d572 commit 71b401c

17 files changed

Lines changed: 243 additions & 46 deletions

File tree

editor-packages/editor-preferences/core/action.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1+
import React from "react";
12
import type { PreferenceState } from "./state";
2-
import type { Subset } from "./types";
3+
import type { PreferencePageProps, Subset } from "./types";
34
type Preference = PreferenceState["config"];
45

56
export type Action =
67
| PreferenceSetRouteAction
78
| OpenPreferenceAction
89
| ClosePreferenceAction
9-
| ConfigurationAction;
10+
| ConfigurationAction
11+
| RegisterPreferenceAction;
1012

1113
export type ActionType = PreferenceSetRouteAction["type"];
1214

@@ -28,3 +30,11 @@ export type ConfigurationAction = {
2830
type: "configure";
2931
update: Subset<Preference>;
3032
};
33+
34+
export type RegisterPreferenceAction = {
35+
type: "register";
36+
route: string;
37+
name: string;
38+
icon?: string;
39+
renderer: React.FC<PreferencePageProps>;
40+
};

editor-packages/editor-preferences/core/state.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
import type { PreferenceRouteInfo } from "./types";
1+
import type { PreferencePageProps, PreferenceRouteInfo } from "./types";
22
import type { FrameworkConfig } from "@grida/builder-config";
33
export interface PreferenceState {
44
open: boolean;
55
route: string;
66
routes: PreferenceRouteInfo[];
7+
renderers: {
8+
[key: string]: React.FC<PreferencePageProps>;
9+
};
710
config: {
811
/**
912
* @beta

editor-packages/editor-preferences/core/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export type Dispatcher = (action: Action) => void;
77
export type PreferenceRouteInfo = {
88
route: string;
99
name: string;
10-
hidden?: boolean;
10+
icon?: string;
1111
};
1212

1313
export interface PreferencePageProps {

editor-packages/editor-preferences/editor-preference.tsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React, { useReducer, useMemo, useCallback } from "react";
22
import styled from "@emotion/styled";
33
import { EditorPreferenceTree } from "./editor-preference-tree";
4-
import routes from "./k/routes";
4+
import initial_routes from "./k/routes";
55
import type { Action, Dispatcher, PreferenceState } from "./core";
66
import { reducer } from "./reducers";
77
import { Router } from "./router";
@@ -55,8 +55,9 @@ export function EditorPreference({ children }: React.PropsWithChildren<{}>) {
5555

5656
const [state, dispatch] = useReducer(reducer, {
5757
open: false,
58-
route: "general",
59-
routes: routes,
58+
route: "/editor",
59+
routes: initial_routes,
60+
renderers: {},
6061
config: pref,
6162
});
6263

@@ -78,7 +79,7 @@ export function EditorPreference({ children }: React.PropsWithChildren<{}>) {
7879

7980
function Preferences() {
8081
const state = usePreferences();
81-
const { route } = state;
82+
const { route, renderers } = state;
8283

8384
const dispatch = useDispatch();
8485

@@ -103,7 +104,7 @@ function Preferences() {
103104
textTransform={"capitalize"}
104105
/>
105106
</ContentHeader>
106-
<Router />
107+
<Router customRenderers={renderers} />
107108
</Content>
108109
</Page>
109110
);

editor-packages/editor-preferences/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,6 @@ export {
55
useOpenPreferences,
66
useClosePreferences,
77
} from "./editor-preference";
8+
export { TextField, HelpPanel } from "./components";
9+
export * from "./layouts";
10+
export type { PreferencePageProps } from "./core/types";
Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { PreferenceRouteInfo } from "../core";
22

3-
const routes: PreferenceRouteInfo[] = [
3+
const initial_routes: PreferenceRouteInfo[] = [
44
// {
55
// route: "/general",
66
// name: "General",
@@ -17,14 +17,6 @@ const routes: PreferenceRouteInfo[] = [
1717
route: "/framework",
1818
name: "Framework Profile",
1919
},
20-
{
21-
route: "/figma",
22-
name: "Figma",
23-
},
24-
{
25-
route: "/figma/personal-access-token",
26-
name: "Personal Access Tokens",
27-
},
2820
];
2921

30-
export default routes;
22+
export default initial_routes;

editor-packages/editor-preferences/pages/editor.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React from "react";
22
import styled from "@emotion/styled";
33
import { PageContentLayout } from "../layouts";
44
import { CanvasModeSelectItem } from "../components";
5-
import { PreferencePageProps } from "core";
5+
import { PreferencePageProps } from "../core";
66

77
export function EditorPreferencePage({ dispatch, state }: PreferencePageProps) {
88
const { renderer } = state.config.canvas;

editor-packages/editor-preferences/pages/figma/index.tsx

Lines changed: 0 additions & 11 deletions
This file was deleted.
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# `@code-editor/preferences`
2+
3+
Preferences management for the editor.
4+
5+
## Usage
6+
7+
Setup Provider - Place the `EditorPreferences` on the top of the preference consumer tree.
8+
9+
```tsx
10+
import React from "react";
11+
import { EditorPreferences } from "@code-editor/preferences";
12+
13+
export MyApp(){
14+
return(
15+
<EditorPreferences>
16+
<Editor/>
17+
</EditorPreferences>
18+
);
19+
}
20+
```
21+
22+
Registering a preference page (that does not actually contribute to the preferences)
23+
24+
```tsx
25+
import React from "react";
26+
import Preferences, { PreferencesPageProps } from "@code-editor/preferences";
27+
import { useProfile } from "some-other-provider";
28+
29+
const router = Preferences.router("/user");
30+
31+
router.route(
32+
"/my-profile",
33+
(p) => {
34+
return <MyProfilePageOnPreferences {...p} />;
35+
},
36+
{
37+
title: "My Profile",
38+
icon: "user",
39+
}
40+
);
41+
42+
function MyProfilePageOnPreferences(p: PreferencesPageProps) {
43+
const profile = useProfile();
44+
return <div>{profile.name}</div>;
45+
}
46+
```
47+
48+
Registering a preference
49+
50+
```ts
51+
import { addPreference } from "@code-editor/preferences";
52+
import schema from "custom-preference-schema.json";
53+
54+
addPreference(schema);
55+
```
56+
57+
```json
58+
// todo
59+
```

editor-packages/editor-preferences/reducers/index.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type {
55
PreferenceState,
66
ConfigurationAction,
77
Subset,
8+
RegisterPreferenceAction,
89
} from "../core";
910
import { PreferencesStore } from "../store";
1011

@@ -52,6 +53,22 @@ export function reducer(
5253
config,
5354
};
5455
}
56+
case "register": {
57+
const {
58+
route,
59+
name,
60+
renderer: pageRenderer,
61+
icon,
62+
} = <RegisterPreferenceAction>action;
63+
const { routes } = state;
64+
const new_routes = [...routes, { route, name, icon }];
65+
const new_renderers = { ...state.renderers, [route]: pageRenderer };
66+
return {
67+
...state,
68+
routes: new_routes,
69+
renderers: new_renderers,
70+
};
71+
}
5572
}
5673
return state;
5774
}

0 commit comments

Comments
 (0)