From 8e16bb74f703b4b11e7284fc358056c9de58f7a3 Mon Sep 17 00:00:00 2001 From: roll Date: Tue, 14 May 2024 14:38:08 +0100 Subject: [PATCH] Improved base source panel --- .../Controllers/Base/Panels/Source.tsx | 53 +++++++++++-------- .../Controllers/Chart/Panels/Source.tsx | 5 +- .../Controllers/File/Panels/Source.tsx | 4 +- .../Controllers/Metadata/Panels/Source.tsx | 5 +- .../Controllers/Table/Panels/Source.tsx | 4 +- .../Controllers/View/Panels/Source.tsx | 11 +--- 6 files changed, 41 insertions(+), 41 deletions(-) diff --git a/client/components/Controllers/Base/Panels/Source.tsx b/client/components/Controllers/Base/Panels/Source.tsx index fc696215f..e6797fada 100644 --- a/client/components/Controllers/Base/Panels/Source.tsx +++ b/client/components/Controllers/Base/Panels/Source.tsx @@ -1,35 +1,44 @@ import { useTheme } from '@mui/material/styles' import Box from '@mui/material/Box' import TextEditor from '../../../Editors/Text' +import * as types from '../../../../types' -export type SourcePanelProps = - | { - json?: false - value?: string - onChange?: (text: string) => void - } - | { - json: true - value?: any - onChange?: (data: any) => void - } +export type SourcePanelProps = { + value?: T + language?: string + onChange?: (value: T) => void +} + +export function JsonSourcePanel(props: SourcePanelProps) { + const { value, onChange, ...others } = props + return ( + { + try { + const data = JSON.parse(text || '{)') + onChange(data) + } catch (error) {} + } + : undefined + } + {...others} + /> + ) +} -export default function SourcePanel(props: SourcePanelProps) { +export function TextSourcePanel(props: SourcePanelProps) { const theme = useTheme() if (props.value === undefined) return null return ( { - if (props.json) { - try { - text = JSON.parse(text || '{)') - } catch (error) {} - } - if (props.onChange) props.onChange(text || '') - }} - language={props.json ? 'json' : undefined} + value={props.value} + onChange={(value) => props.onChange?.(value || '')} + language={props.language || 'plaintext'} options={{ readOnly: !props.onChange }} height={theme.spacing(41)} /> diff --git a/client/components/Controllers/Chart/Panels/Source.tsx b/client/components/Controllers/Chart/Panels/Source.tsx index 02c0d7041..071dec7a1 100644 --- a/client/components/Controllers/Chart/Panels/Source.tsx +++ b/client/components/Controllers/Chart/Panels/Source.tsx @@ -1,12 +1,11 @@ -import SourcePanel from '../../Base/Panels/Source' +import { JsonSourcePanel } from '../../Base/Panels/Source' import { useStore } from '../store' export default function Source() { const modified = useStore((state) => state.modified) const updateState = useStore((state) => state.updateState) return ( - updateState({ modified: value })} /> diff --git a/client/components/Controllers/File/Panels/Source.tsx b/client/components/Controllers/File/Panels/Source.tsx index b705989a6..f13ffff20 100644 --- a/client/components/Controllers/File/Panels/Source.tsx +++ b/client/components/Controllers/File/Panels/Source.tsx @@ -1,8 +1,8 @@ -import SourcePanel from '../../Base/Panels/Source' +import { TextSourcePanel } from '../../Base/Panels/Source' import { useStore } from '../store' export default function Source() { const textSource = useStore((state) => state.textSource) if (!textSource) return null - return + return } diff --git a/client/components/Controllers/Metadata/Panels/Source.tsx b/client/components/Controllers/Metadata/Panels/Source.tsx index 02c0d7041..071dec7a1 100644 --- a/client/components/Controllers/Metadata/Panels/Source.tsx +++ b/client/components/Controllers/Metadata/Panels/Source.tsx @@ -1,12 +1,11 @@ -import SourcePanel from '../../Base/Panels/Source' +import { JsonSourcePanel } from '../../Base/Panels/Source' import { useStore } from '../store' export default function Source() { const modified = useStore((state) => state.modified) const updateState = useStore((state) => state.updateState) return ( - updateState({ modified: value })} /> diff --git a/client/components/Controllers/Table/Panels/Source.tsx b/client/components/Controllers/Table/Panels/Source.tsx index e2e1031ad..d0af690c4 100644 --- a/client/components/Controllers/Table/Panels/Source.tsx +++ b/client/components/Controllers/Table/Panels/Source.tsx @@ -1,5 +1,5 @@ import * as React from 'react' -import SourcePanel from '../../Base/Panels/Source' +import { TextSourcePanel } from '../../Base/Panels/Source' import { useStore } from '../store' export default function Source() { @@ -10,5 +10,5 @@ export default function Source() { loadSource().catch(console.error) }, [record]) if (!source) return null - return + return } diff --git a/client/components/Controllers/View/Panels/Source.tsx b/client/components/Controllers/View/Panels/Source.tsx index 02c0d7041..51b1ea97a 100644 --- a/client/components/Controllers/View/Panels/Source.tsx +++ b/client/components/Controllers/View/Panels/Source.tsx @@ -1,14 +1,7 @@ -import SourcePanel from '../../Base/Panels/Source' +import { JsonSourcePanel } from '../../Base/Panels/Source' import { useStore } from '../store' export default function Source() { const modified = useStore((state) => state.modified) - const updateState = useStore((state) => state.updateState) - return ( - updateState({ modified: value })} - /> - ) + return }