Skip to content

Commit bf2dfee

Browse files
add preference callback & add debug mode in workspace stae
1 parent a4e48bd commit bf2dfee

6 files changed

Lines changed: 124 additions & 18 deletions

File tree

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

Lines changed: 71 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,43 @@ import type {
66
Preference,
77
TBooleanProperty,
88
TNumberProperty,
9+
TProperty,
910
TPropertyValueType,
1011
TStringProperty,
1112
} from "../core";
1213

14+
type PropertyValues = {
15+
[key: string]: TPropertyValueType;
16+
};
17+
1318
export function PreferenceItem({
1419
identifier,
1520
title,
1621
properties,
22+
values,
23+
onChange,
1724
}: Preference & {
25+
values: PropertyValues;
1826
onChange?: OnPropertyChange;
1927
}) {
2028
return (
2129
<PreferenceItemContainer data-preference-id={identifier}>
22-
<h3>{title}</h3>
30+
<span className="title">{title}</span>
2331
<div className="properties">
2432
{Object.keys(properties).map((k) => {
2533
const property = properties[k];
34+
const value = values[k];
35+
const cb = (v) => {
36+
onChange?.(k, v);
37+
};
2638
switch (property.type) {
2739
case "boolean": {
2840
return (
2941
<PreferenceCheckboxItem
3042
key={k}
3143
{...property}
32-
onChange={() => {}}
44+
onChange={cb}
45+
value={value as boolean}
3346
/>
3447
);
3548
}
@@ -38,7 +51,8 @@ export function PreferenceItem({
3851
<PreferenceTextFieldItem
3952
key={k}
4053
{...property}
41-
onChange={() => {}}
54+
onChange={cb}
55+
value={value as number | string}
4256
/>
4357
);
4458
}
@@ -48,15 +62,17 @@ export function PreferenceItem({
4862
<PreferenceSelectItem
4963
key={k}
5064
{...property}
51-
onChange={() => {}}
65+
onChange={cb}
66+
value={value as string}
5267
/>
5368
);
5469
}
5570
return (
5671
<PreferenceTextFieldItem
5772
key={k}
5873
{...property}
59-
onChange={() => {}}
74+
onChange={cb}
75+
value={value as string}
6076
/>
6177
);
6278
}
@@ -68,7 +84,11 @@ export function PreferenceItem({
6884
}
6985

7086
type TOnChange<T> = (value: T) => void;
71-
type OnPropertyChange = (identifier: string, value: TPropertyValueType) => void;
87+
type Properties = { [key: string]: TProperty };
88+
type OnPropertyChange = <K extends keyof Properties>(
89+
key: K,
90+
value: Properties[K]["default"]
91+
) => void;
7292

7393
function PropertyDescription({
7494
description,
@@ -82,26 +102,45 @@ function PropertyDescription({
82102
{markdownDescription ? (
83103
<ReactMarkdown>{markdownDescription}</ReactMarkdown>
84104
) : (
85-
<p>{description}</p>
105+
<span>{description}</span>
86106
)}
87107
</div>
88108
);
89109
}
90110

91111
function PreferenceCheckboxItem({
112+
value,
113+
onChange,
92114
...props
93-
}: TBooleanProperty & { onChange?: TOnChange<boolean> }) {
115+
}: TBooleanProperty & {
116+
onChange?: TOnChange<boolean>;
117+
value?: boolean | "indeterminate";
118+
}) {
94119
return (
95120
<div className="checkbox">
96-
<Checkbox />
121+
<Checkbox
122+
style={{
123+
color: "white",
124+
}}
125+
// defaultChecked={props.default}
126+
checked={value}
127+
onCheckedChange={(checked) => {
128+
if (typeof checked === "boolean") {
129+
onChange?.(checked);
130+
}
131+
}}
132+
/>
97133
<PropertyDescription {...props} />
98134
</div>
99135
);
100136
}
101137

102138
function PreferenceTextFieldItem({
103139
...props
104-
}: (TStringProperty | TNumberProperty) & { onChange?: TOnChange<string> }) {
140+
}: (TStringProperty | TNumberProperty) & {
141+
onChange?: TOnChange<string>;
142+
value?: string | number;
143+
}) {
105144
return (
106145
<>
107146
<PropertyDescription {...props} />
@@ -110,21 +149,40 @@ function PreferenceTextFieldItem({
110149
}
111150

112151
function PreferenceSelectItem(
113-
props: TStringProperty & { onChange?: TOnChange<string> }
152+
props: TStringProperty & {
153+
onChange?: TOnChange<string>;
154+
value?: string;
155+
}
114156
) {
115157
return <></>;
116158
}
117159

118160
const PreferenceItemContainer = styled.div`
119161
/* */
162+
cursor: default;
120163
display: flex;
164+
flex-direction: column;
165+
gap: 8px;
166+
padding: 16px;
167+
border-radius: 4px;
168+
169+
&:hover {
170+
background: rgba(255, 255, 255, 0.04);
171+
}
172+
173+
.title {
174+
color: rgba(255, 255, 255, 0.8);
175+
font-weight: 500;
176+
}
121177
122178
.properties {
123179
display: flex;
124180
flex-direction: column;
125181
}
126182
127183
.description {
184+
color: rgba(255, 255, 255, 0.7);
185+
font-size: 14px;
128186
}
129187
130188
.checkbox {
@@ -134,4 +192,6 @@ const PreferenceItemContainer = styled.div`
134192
align-items: center;
135193
justify-content: flex-start;
136194
}
195+
196+
transition: background 0.1s ease-in-out;
137197
`;

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

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
1-
import React, { useEffect } from "react";
1+
import React, { useState, useEffect } from "react";
22
import { PageContentLayout } from "../layouts";
33
import type { Preference, PreferencePageProps } from "../core";
44
import { PreferenceItem } from "../components/preference-item";
5+
import { useWorkspace, useWorkspaceState } from "editor/core/states";
6+
7+
const ppi_debug_mode = "workbench.debug-mode.enabled";
58

69
const preference_debug_mode: Preference = {
710
identifier: "editor.debug-mode",
8-
title: "Debug Mode",
11+
title: "Enable Debug Mode",
912
properties: {
10-
"editor.debug-mode.enabled": {
13+
[ppi_debug_mode]: {
1114
type: "boolean",
12-
description: "Enable debug mode",
15+
description:
16+
"Experimental: Enabling Debug Mode will provide advanced tooling for deeper inspections and monotor activities behind the scene.",
1317
default: false,
1418
},
1519
},
@@ -19,13 +23,26 @@ export default function AdvancedPreferencesPage({
1923
state,
2024
dispatch,
2125
}: PreferencePageProps) {
26+
const { debugMode } = useWorkspaceState();
27+
const { setDebugMode } = useWorkspace();
28+
2229
return (
2330
<>
2431
<PageContentLayout>
2532
<h1>Advanced</h1>
2633
<main>
2734
<section>
28-
<PreferenceItem {...preference_debug_mode} />
35+
<PreferenceItem
36+
{...preference_debug_mode}
37+
values={{
38+
[ppi_debug_mode]: debugMode,
39+
}}
40+
onChange={(k, v: boolean) => {
41+
if (k == ppi_debug_mode) {
42+
setDebugMode(v);
43+
}
44+
}}
45+
/>
2946
</section>
3047
</main>
3148
</PageContentLayout>

editor/core/actions/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export type HistoryAction =
2525
| Action;
2626

2727
export type Action =
28+
| SetDebugModeAction
2829
| SetFigmaAuthAction
2930
| SetFigmaUserAction
3031
| PageAction
@@ -211,3 +212,8 @@ export interface BackgroundTaskUpdateProgressAction {
211212
id: string;
212213
progress: number;
213214
}
215+
216+
export type SetDebugModeAction = {
217+
type: "debug-mode/enable";
218+
enabled: boolean;
219+
};

editor/core/reducers/workspace-reducer.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
BackgroundTaskPopAction,
55
BackgroundTaskPushAction,
66
BackgroundTaskUpdateProgressAction,
7+
SetDebugModeAction,
78
WorkspaceAction,
89
WorkspaceWarmupAction,
910
} from "../actions";
@@ -65,8 +66,15 @@ export function workspaceReducer(
6566
return produce(state, (draft) => {
6667
draft.taskQueue.tasks.find((i) => i.id !== id).progress = progress;
6768
});
68-
break;
6969
}
70+
71+
case "debug-mode/enable": {
72+
const { enabled } = <SetDebugModeAction>action;
73+
return produce(state, (draft) => {
74+
draft.debugMode = enabled;
75+
});
76+
}
77+
7078
// default fallback - use history reducer
7179
case "redo":
7280
case "undo":

editor/core/states/use-workspace.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,27 @@ export function useWorkspace() {
2525
[dispatch]
2626
);
2727

28+
const setDebugMode = useCallback(
29+
(enabled: boolean) => dispatch({ type: "debug-mode/enable", enabled }),
30+
[dispatch]
31+
);
32+
2833
return useMemo(
2934
() => ({
3035
highlightedLayer,
3136
highlightLayer,
3237
taskQueue,
3338
pushTask,
3439
popTask,
40+
setDebugMode,
3541
}),
36-
[highlightedLayer, highlightLayer, taskQueue, pushTask, popTask]
42+
[
43+
highlightedLayer,
44+
highlightLayer,
45+
taskQueue,
46+
pushTask,
47+
popTask,
48+
setDebugMode,
49+
]
3750
);
3851
}

editor/core/states/workspace-state.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ export interface WorkspaceState extends EssentialWorkspaceInfo {
3333
/** URL link to the user's profile image */
3434
profile: string;
3535
};
36+
37+
debugMode?: boolean;
3638
}
3739

3840
export interface TaskQueue {

0 commit comments

Comments
 (0)