Skip to content

Commit b2c789c

Browse files
committed
add repoConfig; add doubleClickToEdit to config
1 parent 1052a1c commit b2c789c

File tree

5 files changed

+120
-1
lines changed

5 files changed

+120
-1
lines changed

cpkernel/src/resolver_local.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,18 @@ export function getResolvers(appDir) {
146146
activeSessions: async (_, __, { userId }) => {
147147
return [];
148148
},
149+
repoConfig: async (_, { name }) => {
150+
if (!fs.existsSync(path.join(appDir, name))) {
151+
return false;
152+
}
153+
// codepod config
154+
let config_file = path.join(appDir, name, "codepod.json");
155+
let config = "{}";
156+
if (fs.existsSync(config_file)) {
157+
config = await fs.promises.readFile(config_file);
158+
}
159+
return config.toString();
160+
},
149161
repo: async (_, { name, username }) => {
150162
if (!fs.existsSync(path.join(appDir, name))) {
151163
return false;
@@ -197,6 +209,21 @@ export function getResolvers(appDir) {
197209
},
198210
},
199211
Mutation: {
212+
updateRepoConfig: async (_, { name, config }) => {
213+
if (!fs.existsSync(path.join(appDir, name))) {
214+
return false;
215+
}
216+
let config_file = path.join(appDir, name, "codepod.json");
217+
let newconfig = JSON.parse(config);
218+
219+
let obj = {};
220+
if (fs.existsSync(config_file)) {
221+
obj = JSON.parse(await fs.promises.readFile(config_file));
222+
}
223+
Object.assign(obj, newconfig);
224+
await fs.promises.writeFile(config_file, JSON.stringify(obj, null, 2));
225+
return true;
226+
},
200227
signup: async (_, { username, email, password, invitation }) => {
201228
return false;
202229
},

cpkernel/src/typedefs.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ export const typeDefs = gql`
6666
me: User
6767
repos: [Repo]
6868
repo(name: String!, username: String!): Repo
69+
repoConfig(name: String!): String
6970
pods(username: String, reponame: String): [Pod]
7071
pod(id: ID!): Pod
7172
myRepos: [Repo]
@@ -86,6 +87,7 @@ export const typeDefs = gql`
8687
updateUser(username: String, email: String, name: String): Boolean
8788
createRepo(name: String): Boolean
8889
deleteRepo(name: String): Boolean
90+
updateRepoConfig(name: String, config: String): Boolean
8991
addPod(
9092
reponame: String
9193
username: String

ui/src/components/repo/pod.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -942,6 +942,9 @@ function ThePod({ id }) {
942942
// console.log("rendinering thepod", id);
943943
const pod = useSelector((state) => state.repo.pods[id]);
944944
const dispatch = useDispatch();
945+
const doubleClickToEdit = useSelector(
946+
(state) => state.repo.repoConfig?.doubleClickToEdit
947+
);
945948
if (pod.type === "WYSIWYG") {
946949
return <WysiwygPod pod={pod} />;
947950
} else if (pod.type === "MD") {
@@ -959,7 +962,7 @@ function ThePod({ id }) {
959962
);
960963
} else if (pod.type === "CODE") {
961964
// return <Text>CODE</Text>;
962-
if (pod.render) {
965+
if (!doubleClickToEdit || pod.render) {
963966
return <CodePod id={id} />;
964967
} else {
965968
return (

ui/src/components/repo/sidebar.js

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -872,6 +872,89 @@ function SidebarTest() {
872872
);
873873
}
874874

875+
function ConfigButton() {
876+
let reponame = useSelector((state) => state.repo.reponame);
877+
const dispatch = useDispatch();
878+
// let username = useSelector((state) => state.repo.username);
879+
const { data, loading, error } = useQuery(gql`
880+
query RepoConfig {
881+
repoConfig(name: "${reponame}")
882+
}
883+
`);
884+
const [updateRepoConfig, {}] = useMutation(
885+
gql`
886+
mutation UpdateRepoConfig($reponame: String, $config: String) {
887+
updateRepoConfig(name: $reponame, config: $config)
888+
}
889+
`,
890+
{ refetchQueries: ["RepoConfig"] }
891+
);
892+
useEffect(() => {
893+
console.log(data);
894+
if (data) {
895+
dispatch(repoSlice.actions.setRepoConfig(JSON.parse(data.repoConfig)));
896+
}
897+
}, [data]);
898+
const repoConfig = useSelector((state) => state.repo.repoConfig);
899+
// console.log("repoConfig", repoConfig);
900+
// console.log("ConfigButton", data);
901+
// const [open, setOpen] = React.useState(false);
902+
// const handleOpen = () => setOpen(true);
903+
// const handleClose = () => setOpen(false);
904+
const { isOpen, onOpen, onClose } = useDisclosure();
905+
return (
906+
<Box>
907+
<Button
908+
onClick={() => {
909+
// 1. show a new interface
910+
// setOpen(true);
911+
onOpen();
912+
// 2. do the config
913+
// 3. update the config
914+
}}
915+
>
916+
Config
917+
</Button>
918+
<Modal isOpen={isOpen} onClose={onClose} size="6xl">
919+
<ModalOverlay />
920+
921+
<ModalContent>
922+
<ModalHeader>Config</ModalHeader>
923+
<ModalCloseButton />
924+
<ModalBody h={50} overflow="scroll">
925+
<Box>
926+
<Text>Double Click to Edit</Text>
927+
<Switch
928+
defaultChecked={repoConfig && repoConfig.doubleClickToEdit}
929+
onChange={(e) => {
930+
// dispatch(repoSlice.actions.setDevMode(e.target.checked));
931+
updateRepoConfig({
932+
variables: {
933+
reponame,
934+
config: JSON.stringify({
935+
doubleClickToEdit: e.target.checked,
936+
}),
937+
},
938+
});
939+
}}
940+
></Switch>
941+
</Box>
942+
</ModalBody>
943+
944+
<ModalFooter>
945+
{/* <Button colorScheme="blue" mr={3} onClick={onClose}>
946+
Save
947+
</Button> */}
948+
<Button colorScheme="blue" mr={3} onClick={onClose}>
949+
Close
950+
</Button>
951+
</ModalFooter>
952+
</ModalContent>
953+
</Modal>
954+
</Box>
955+
);
956+
}
957+
875958
export function Sidebar() {
876959
return (
877960
<Box mx={2} px="1rem" boxShadow="xl" rounded="md" bg="gray.200">
@@ -898,6 +981,7 @@ export function Sidebar() {
898981
<ApplyAll />
899982
</Box>
900983
<Divider my={2} />
984+
<ConfigButton />
901985
</Box>
902986
);
903987
}

ui/src/lib/store.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,9 @@ export const repoSlice = createSlice({
181181
state.reponame = reponame;
182182
state.username = username;
183183
},
184+
setRepoConfig: (state, action) => {
185+
state.repoConfig = action.payload;
186+
},
184187
markClip: (state, action) => {
185188
let { id } = action.payload;
186189
let pod = state.pods[id];

0 commit comments

Comments
 (0)