Skip to content

Commit c0a63cd

Browse files
authored
feat: support naming on pod and scope (#129)
1 parent d9c7ccb commit c0a63cd

File tree

2 files changed

+47
-5
lines changed

2 files changed

+47
-5
lines changed

ui/src/components/Canvas.tsx

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import ReactFlow, {
2424
import "reactflow/dist/style.css";
2525

2626
import Box from "@mui/material/Box";
27+
import InputBase from "@mui/material/InputBase";
2728
import CircularProgress from "@mui/material/CircularProgress";
2829
import Tooltip from "@mui/material/Tooltip";
2930
import IconButton from "@mui/material/IconButton";
@@ -71,6 +72,9 @@ const ScopeNode = memo<Props>(({ data, id, isConnectable }) => {
7172
const ref = useRef(null);
7273
const store = useContext(RepoContext);
7374
if (!store) throw new Error("Missing BearContext.Provider in the tree");
75+
const getPod = useStore(store, (state) => state.getPod);
76+
const pod = getPod(id);
77+
const setPodName = useStore(store, (state) => state.setPodName);
7478
const updatePod = useStore(store, (state) => state.updatePod);
7579
const [target, setTarget] = React.useState<any>();
7680
const nodesMap = useStore(store, (state) => state.ydoc.getMap<Node>("pods"));
@@ -130,7 +134,21 @@ const ScopeNode = memo<Props>(({ data, id, isConnectable }) => {
130134
justifyContent: "center",
131135
}}
132136
>
133-
Scope
137+
<InputBase
138+
defaultValue={pod.name || "Scope"}
139+
onBlur={(e) => {
140+
const name = e.target.value;
141+
if (name === pod.name) return;
142+
setPodName({ id, name });
143+
}}
144+
inputProps={{
145+
style: {
146+
padding: "0px",
147+
textAlign: "center",
148+
textOverflow: "ellipsis",
149+
},
150+
}}
151+
></InputBase>
134152
</Box>
135153
</Grid>
136154
<Grid item xs={4}></Grid>
@@ -339,6 +357,7 @@ const CodeNode = memo<Props>(({ data, id, isConnectable }) => {
339357
const isRightLayout = layout === "right";
340358
const { setNodes } = useReactFlow();
341359
// const selected = useStore(store, (state) => state.selected);
360+
const setPodName = useStore(store, (state) => state.setPodName);
342361
const setPodSelected = useStore(store, (state) => state.setPodSelected);
343362
const setCurrentEditor = useStore(store, (state) => state.setCurrentEditor);
344363
const getPod = useStore(store, (state) => state.getPod);
@@ -435,6 +454,28 @@ const CodeNode = memo<Props>(({ data, id, isConnectable }) => {
435454
/>
436455
{/* The header of code pods. */}
437456
<Box className="custom-drag-handle">
457+
<Box
458+
sx={{
459+
position: "absolute",
460+
top: "-24px",
461+
width: "50%",
462+
}}
463+
>
464+
<InputBase
465+
defaultValue={pod.name}
466+
onBlur={(e) => {
467+
const name = e.target.value;
468+
if (name === pod.name) return;
469+
setPodName({ id, name });
470+
}}
471+
inputProps={{
472+
style: {
473+
padding: "0px",
474+
textOverflow: "ellipsis",
475+
},
476+
}}
477+
></InputBase>
478+
</Box>
438479
<Box sx={styles["pod-index"]}>[{pod.index}]</Box>
439480
<Box
440481
sx={{
@@ -581,7 +622,7 @@ export function Canvas() {
581622
const provider = useStore(store, (state) => state.provider);
582623

583624
const getRealNodes = useCallback(
584-
(id, level) => {
625+
(id: string, level: number) => {
585626
let res: any[] = [];
586627
let children = getId2children(id) || [];
587628
const pod = getPod(id);
@@ -683,7 +724,7 @@ export function Canvas() {
683724
const userColor = useStore(store, (state) => state.user?.color);
684725

685726
const addNode = useCallback(
686-
(x, y, type) => {
727+
(x: number, y: number, type: string) => {
687728
const reactFlowBounds = reactFlowWrapper.current.getBoundingClientRect();
688729
let style;
689730

ui/src/lib/store.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ export interface RepoSlice {
160160
clearError: () => void;
161161
foldAll: () => void;
162162
unfoldAll: () => void;
163+
setPodName: ({ id, name }: { id: string; name: string }) => void;
163164
setPodContent: ({ id, content }: { id: string; content: string }) => void;
164165
addPod: (
165166
client: ApolloClient<object> | null,
@@ -460,7 +461,7 @@ const createRepoSlice: StateCreator<
460461
// @ts-ignore
461462
"toggleUtility"
462463
),
463-
setName: ({ id, name }) =>
464+
setPodName: ({ id, name }) =>
464465
set(
465466
produce((state) => {
466467
let pod = state.pods[id];
@@ -469,7 +470,7 @@ const createRepoSlice: StateCreator<
469470
}),
470471
false,
471472
// @ts-ignore
472-
"setName"
473+
"setPodName"
473474
),
474475
setPodLang: ({ id, lang }) =>
475476
set(

0 commit comments

Comments
 (0)