Skip to content

Commit

Permalink
Improved base source panel
Browse files Browse the repository at this point in the history
  • Loading branch information
roll committed May 14, 2024
1 parent d3ba053 commit 8e16bb7
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 41 deletions.
53 changes: 31 additions & 22 deletions client/components/Controllers/Base/Panels/Source.tsx
Original file line number Diff line number Diff line change
@@ -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<T extends string | types.IData> = {
value?: T
language?: string
onChange?: (value: T) => void
}

export function JsonSourcePanel(props: SourcePanelProps<types.IData>) {
const { value, onChange, ...others } = props
return (
<TextSourcePanel
language="json"
value={JSON.stringify(value, null, 2)}
onChange={
onChange
? (text) => {
try {
const data = JSON.parse(text || '{)')
onChange(data)
} catch (error) {}
}
: undefined
}
{...others}
/>
)
}

export default function SourcePanel(props: SourcePanelProps) {
export function TextSourcePanel(props: SourcePanelProps<string>) {
const theme = useTheme()
if (props.value === undefined) return null
return (
<Box>
<TextEditor
value={props.json ? JSON.stringify(props.value, null, 2) : props.value}
onChange={(text) => {
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)}
/>
Expand Down
5 changes: 2 additions & 3 deletions client/components/Controllers/Chart/Panels/Source.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<SourcePanel
json
<JsonSourcePanel
value={modified}
onChange={(value) => updateState({ modified: value })}
/>
Expand Down
4 changes: 2 additions & 2 deletions client/components/Controllers/File/Panels/Source.tsx
Original file line number Diff line number Diff line change
@@ -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 <SourcePanel value={textSource} />
return <TextSourcePanel value={textSource} />
}
5 changes: 2 additions & 3 deletions client/components/Controllers/Metadata/Panels/Source.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<SourcePanel
json
<JsonSourcePanel
value={modified}
onChange={(value) => updateState({ modified: value })}
/>
Expand Down
4 changes: 2 additions & 2 deletions client/components/Controllers/Table/Panels/Source.tsx
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -10,5 +10,5 @@ export default function Source() {
loadSource().catch(console.error)
}, [record])
if (!source) return null
return <SourcePanel value={source} />
return <TextSourcePanel value={source} />
}
11 changes: 2 additions & 9 deletions client/components/Controllers/View/Panels/Source.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<SourcePanel
json
value={modified}
onChange={(value) => updateState({ modified: value })}
/>
)
return <JsonSourcePanel value={modified} />
}

0 comments on commit 8e16bb7

Please sign in to comment.