forked from AykutSarac/jsoncrack.com
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add ability to edit through tree view (AykutSarac#376)
- Loading branch information
1 parent
bc380ba
commit effac67
Showing
7 changed files
with
293 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import React from "react"; | ||
import { Button, Divider, Group, Modal, TextInput } from "@mantine/core"; | ||
import _unset from "lodash.unset"; | ||
import _update from "lodash.update"; | ||
import { VscLock } from "react-icons/vsc"; | ||
import useFile from "src/store/useFile"; | ||
import useJson from "src/store/useJson"; | ||
import useModal from "src/store/useModal"; | ||
import useUser from "src/store/useUser"; | ||
|
||
interface EditModalProps { | ||
opened: boolean; | ||
setOpened: React.Dispatch<React.SetStateAction<boolean>>; | ||
selectedValue: string | number | null; | ||
path: (string | number)[]; | ||
value: string; | ||
setValue: React.Dispatch<React.SetStateAction<string>>; | ||
errorMessage: string | null; | ||
setErrorMessage: React.Dispatch<React.SetStateAction<string | null>>; | ||
} | ||
|
||
export const EditModal = ({ | ||
opened, | ||
setOpened, | ||
selectedValue, | ||
path, | ||
value, | ||
setValue, | ||
errorMessage, | ||
setErrorMessage, | ||
}: EditModalProps) => { | ||
const setContents = useFile(state => state.setContents); | ||
const getJson = useJson(state => state.getJson); | ||
const showPremiumModal = useModal(state => state.setVisible("premium")); | ||
const premium = useUser(state => state.premium); | ||
|
||
return ( | ||
<Modal centered title={selectedValue} opened={opened} onClose={() => setOpened(false)}> | ||
<TextInput | ||
value={value} | ||
onChange={e => { | ||
setValue(e.currentTarget.value); | ||
}} | ||
error={errorMessage} | ||
/> | ||
<Divider my="md" /> | ||
<Group justify="right"> | ||
<Button | ||
color="red" | ||
onClick={() => { | ||
try { | ||
if (!premium) return showPremiumModal(true); | ||
const updatedJson = JSON.parse(getJson()); | ||
_unset(updatedJson, path); | ||
|
||
setContents({ contents: JSON.stringify(updatedJson, null, 2) }); | ||
setErrorMessage(null); | ||
setOpened(false); | ||
} catch (error: any) { | ||
setErrorMessage(error.message); | ||
} | ||
}} | ||
rightSection={!premium && <VscLock />} | ||
> | ||
Delete | ||
</Button> | ||
<Button | ||
onClick={() => { | ||
try { | ||
if (!premium) return showPremiumModal(true); | ||
const updatedJson = _update(JSON.parse(getJson()), path, () => JSON.parse(value)); | ||
setContents({ contents: JSON.stringify(updatedJson, null, 2) }); | ||
|
||
setErrorMessage(null); | ||
setOpened(false); | ||
} catch (error: any) { | ||
setErrorMessage(error.message); | ||
} | ||
}} | ||
rightSection={!premium && <VscLock />} | ||
> | ||
Apply | ||
</Button> | ||
</Group> | ||
</Modal> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import React from "react"; | ||
import { Button, HoverCard } from "@mantine/core"; | ||
import { styled, DefaultTheme } from "styled-components"; | ||
import _get from "lodash.get"; | ||
import { VscEdit } from "react-icons/vsc"; | ||
import { KeyPath } from "react-json-tree"; | ||
import useJson from "src/store/useJson"; | ||
|
||
interface LabelProps { | ||
keyPath: KeyPath; | ||
nodeType: string; | ||
setOpened: React.Dispatch<React.SetStateAction<boolean>>; | ||
setSelectedValue: React.Dispatch<React.SetStateAction<string | number | null>>; | ||
setPath: React.Dispatch<React.SetStateAction<(string | number)[]>>; | ||
setValue: React.Dispatch<React.SetStateAction<string>>; | ||
} | ||
|
||
function getLabelColor({ $type, theme }: { $type?: string; theme: DefaultTheme }) { | ||
if ($type === "Object") return theme.NODE_COLORS.PARENT_OBJ; | ||
if ($type === "Array") return theme.NODE_COLORS.PARENT_ARR; | ||
return theme.NODE_COLORS.PARENT_OBJ; | ||
} | ||
|
||
const StyledLabel = styled.span<{ $nodeType?: string }>` | ||
color: ${({ theme, $nodeType }) => getLabelColor({ theme, $type: $nodeType })}; | ||
&:hover { | ||
filter: brightness(1.5); | ||
transition: filter 0.2s ease-in-out; | ||
} | ||
`; | ||
|
||
export const Label = ({ | ||
keyPath, | ||
nodeType, | ||
setOpened, | ||
setSelectedValue, | ||
setPath, | ||
setValue, | ||
}: LabelProps) => { | ||
const getJson = useJson(state => state.getJson); | ||
|
||
return ( | ||
<HoverCard shadow="sm" openDelay={50} closeDelay={250} withArrow position="left"> | ||
<HoverCard.Target> | ||
<StyledLabel $nodeType={nodeType}>{keyPath[0]}:</StyledLabel> | ||
</HoverCard.Target> | ||
<HoverCard.Dropdown py={4}> | ||
<Button | ||
variant="transparent" | ||
size="xs" | ||
onClick={() => { | ||
setOpened(true); | ||
setSelectedValue(keyPath[0]); | ||
|
||
const path = keyPath.toReversed(); | ||
const value = _get(JSON.parse(getJson()), path); | ||
|
||
setPath(path); | ||
setValue(JSON.stringify(value)); | ||
}} | ||
leftSection={<VscEdit size="12" />} | ||
> | ||
Click to edit | ||
</Button> | ||
</HoverCard.Dropdown> | ||
</HoverCard> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import React from "react"; | ||
import { DefaultTheme, useTheme } from "styled-components"; | ||
import { TextRenderer } from "src/containers/Views/GraphView/CustomNode/TextRenderer"; | ||
|
||
type TextColorFn = { | ||
theme: DefaultTheme; | ||
$value?: string | unknown; | ||
}; | ||
|
||
function getValueColor({ $value, theme }: TextColorFn) { | ||
if ($value && !Number.isNaN(+$value)) return theme.NODE_COLORS.INTEGER; | ||
if ($value === "true") return theme.NODE_COLORS.BOOL.TRUE; | ||
if ($value === "false") return theme.NODE_COLORS.BOOL.FALSE; | ||
if ($value === "null") return theme.NODE_COLORS.NULL; | ||
|
||
// default | ||
return theme.NODE_COLORS.NODE_VALUE; | ||
} | ||
|
||
interface ValueProps { | ||
valueAsString: unknown; | ||
value: unknown; | ||
} | ||
|
||
export const Value = (props: ValueProps) => { | ||
const theme = useTheme(); | ||
const { valueAsString, value } = props; | ||
|
||
return ( | ||
<span | ||
style={{ | ||
color: getValueColor({ | ||
theme, | ||
$value: valueAsString, | ||
}), | ||
}} | ||
> | ||
<TextRenderer>{JSON.stringify(value)}</TextRenderer> | ||
</span> | ||
); | ||
}; |
Oops, something went wrong.