mirror of
https://github.com/AykutSarac/jsoncrack.com.git
synced 2025-01-20 05:12:54 +08:00
add editor dynamic width support
This commit is contained in:
parent
9a51b7ae93
commit
79defb1c17
@ -77,6 +77,8 @@ const JsonEditor: React.FC<{
|
|||||||
message: "",
|
message: "",
|
||||||
isExpanded: true,
|
isExpanded: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const [editorWidth, setEditorWidth] = React.useState("auto");
|
||||||
const [config] = useLocalStorage<StorageConfig>("config", defaultConfig);
|
const [config] = useLocalStorage<StorageConfig>("config", defaultConfig);
|
||||||
const [value, setValue] = React.useState(
|
const [value, setValue] = React.useState(
|
||||||
JSON.stringify(JSON.parse(json), null, 2)
|
JSON.stringify(JSON.parse(json), null, 2)
|
||||||
@ -107,6 +109,20 @@ const JsonEditor: React.FC<{
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
React.useEffect(() => {
|
||||||
|
const resizeObserver = new ResizeObserver((observed) => {
|
||||||
|
const width = observed[0].contentRect.width;
|
||||||
|
setEditorWidth(width.toString());
|
||||||
|
});
|
||||||
|
|
||||||
|
const dom = document.querySelector(".ace_scroller");
|
||||||
|
if (dom) resizeObserver.observe(dom);
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
resizeObserver.disconnect();
|
||||||
|
};
|
||||||
|
}, [json]);
|
||||||
|
|
||||||
React.useEffect(() => {
|
React.useEffect(() => {
|
||||||
if (config.autoformat) {
|
if (config.autoformat) {
|
||||||
return setValue(JSON.stringify(JSON.parse(json), null, 2));
|
return setValue(JSON.stringify(JSON.parse(json), null, 2));
|
||||||
@ -152,14 +168,16 @@ const JsonEditor: React.FC<{
|
|||||||
{error.isExpanded && <StyledError>{error.message}</StyledError>}
|
{error.isExpanded && <StyledError>{error.message}</StyledError>}
|
||||||
</StyledErrorWrapper>
|
</StyledErrorWrapper>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
<AceEditor
|
<AceEditor
|
||||||
value={value}
|
value={value}
|
||||||
onChange={setValue}
|
onChange={setValue}
|
||||||
mode="json"
|
mode="json"
|
||||||
theme="tomorrow_night"
|
theme="tomorrow_night"
|
||||||
width="auto"
|
width={editorWidth}
|
||||||
height="100%"
|
height="100%"
|
||||||
fontSize={14}
|
fontSize={14}
|
||||||
|
wrapEnabled
|
||||||
/>
|
/>
|
||||||
</StyledEditorWrapper>
|
</StyledEditorWrapper>
|
||||||
);
|
);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user