forked from Archives/Athou_commafeed
move RichCodeEditor to its own component
This commit is contained in:
27
commafeed-client/src/components/RichCodeEditor.tsx
Normal file
27
commafeed-client/src/components/RichCodeEditor.tsx
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
import { useMantineTheme } from "@mantine/core"
|
||||||
|
import { Editor } from "@monaco-editor/react"
|
||||||
|
|
||||||
|
interface RichCodeEditorProps {
|
||||||
|
height: number | string
|
||||||
|
language: "css" | "javascript"
|
||||||
|
value: string
|
||||||
|
onChange: (value: string | undefined) => void
|
||||||
|
}
|
||||||
|
|
||||||
|
function RichCodeEditor(props: RichCodeEditorProps) {
|
||||||
|
const theme = useMantineTheme()
|
||||||
|
const editorTheme = theme.colorScheme === "dark" ? "vs-dark" : "light"
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Editor
|
||||||
|
height={props.height}
|
||||||
|
defaultLanguage={props.language}
|
||||||
|
theme={editorTheme}
|
||||||
|
options={{ minimap: { enabled: false } }}
|
||||||
|
value={props.value}
|
||||||
|
onChange={props.onChange}
|
||||||
|
/>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
export default RichCodeEditor
|
||||||
@@ -1,94 +1,79 @@
|
|||||||
import { Trans } from "@lingui/macro"
|
import { Trans } from "@lingui/macro"
|
||||||
import { Box, Button, Group, Input, Stack, useMantineTheme } from "@mantine/core"
|
import { Box, Button, Group, Input, Stack } from "@mantine/core"
|
||||||
import { useForm } from "@mantine/form"
|
import { useForm } from "@mantine/form"
|
||||||
import { Editor } from "@monaco-editor/react"
|
import { client, errorToStrings } from "app/client"
|
||||||
import { client, errorToStrings } from "app/client"
|
import { redirectToSelectedSource } from "app/slices/redirect"
|
||||||
import { redirectToSelectedSource } from "app/slices/redirect"
|
import { useAppDispatch, useAppSelector } from "app/store"
|
||||||
import { useAppDispatch, useAppSelector } from "app/store"
|
import { Alert } from "components/Alert"
|
||||||
import { Alert } from "components/Alert"
|
import RichCodeEditor from "components/RichCodeEditor"
|
||||||
import { useEffect } from "react"
|
import { useEffect } from "react"
|
||||||
import { useAsyncCallback } from "react-async-hook"
|
import { useAsyncCallback } from "react-async-hook"
|
||||||
import { TbDeviceFloppy } from "react-icons/tb"
|
import { TbDeviceFloppy } from "react-icons/tb"
|
||||||
|
|
||||||
interface FormData {
|
interface FormData {
|
||||||
customCss: string
|
customCss: string
|
||||||
customJs: string
|
customJs: string
|
||||||
}
|
}
|
||||||
|
|
||||||
export function CustomCodeSettings() {
|
export function CustomCodeSettings() {
|
||||||
const settings = useAppSelector(state => state.user.settings)
|
const settings = useAppSelector(state => state.user.settings)
|
||||||
const theme = useMantineTheme()
|
const dispatch = useAppDispatch()
|
||||||
const dispatch = useAppDispatch()
|
|
||||||
|
const form = useForm<FormData>()
|
||||||
const editorTheme = theme.colorScheme === "dark" ? "vs-dark" : "light"
|
const { setValues } = form
|
||||||
|
|
||||||
const form = useForm<FormData>()
|
const saveCustomCode = useAsyncCallback(
|
||||||
const { setValues } = form
|
async (d: FormData) => {
|
||||||
|
if (!settings) return
|
||||||
const saveCustomCode = useAsyncCallback(
|
await client.user.saveSettings({
|
||||||
async (d: FormData) => {
|
...settings,
|
||||||
if (!settings) return
|
customCss: d.customCss,
|
||||||
await client.user.saveSettings({
|
customJs: d.customJs,
|
||||||
...settings,
|
})
|
||||||
customCss: d.customCss,
|
},
|
||||||
customJs: d.customJs,
|
{
|
||||||
})
|
onSuccess: () => {
|
||||||
},
|
window.location.reload()
|
||||||
{
|
},
|
||||||
onSuccess: () => {
|
}
|
||||||
window.location.reload()
|
)
|
||||||
},
|
|
||||||
}
|
useEffect(() => {
|
||||||
)
|
if (!settings) return
|
||||||
|
setValues({
|
||||||
useEffect(() => {
|
customCss: settings.customCss,
|
||||||
if (!settings) return
|
customJs: settings.customJs,
|
||||||
setValues({
|
})
|
||||||
customCss: settings.customCss,
|
}, [setValues, settings])
|
||||||
customJs: settings.customJs,
|
|
||||||
})
|
return (
|
||||||
}, [setValues, settings])
|
<>
|
||||||
|
{saveCustomCode.error && (
|
||||||
return (
|
<Box mb="md">
|
||||||
<>
|
<Alert messages={errorToStrings(saveCustomCode.error)} />
|
||||||
{saveCustomCode.error && (
|
</Box>
|
||||||
<Box mb="md">
|
)}
|
||||||
<Alert messages={errorToStrings(saveCustomCode.error)} />
|
|
||||||
</Box>
|
<form onSubmit={form.onSubmit(saveCustomCode.execute)}>
|
||||||
)}
|
<Stack>
|
||||||
|
<Input.Wrapper description={<Trans>Custom CSS rules that will be applied</Trans>}>
|
||||||
<form onSubmit={form.onSubmit(saveCustomCode.execute)}>
|
<RichCodeEditor height="30vh" language="css" {...form.getInputProps("customCss")} />
|
||||||
<Stack>
|
</Input.Wrapper>
|
||||||
<Input.Wrapper description={<Trans>Custom CSS rules that will be applied</Trans>}>
|
|
||||||
<Editor
|
<Input.Wrapper description={<Trans>Custom JS code that will be executed on page load</Trans>}>
|
||||||
height="30vh"
|
<RichCodeEditor height="30vh" language="javascript" {...form.getInputProps("customJs")} />
|
||||||
defaultLanguage="css"
|
</Input.Wrapper>
|
||||||
theme={editorTheme}
|
|
||||||
options={{ minimap: { enabled: false } }}
|
<Group>
|
||||||
{...form.getInputProps("customCss")}
|
<Button variant="default" onClick={() => dispatch(redirectToSelectedSource())}>
|
||||||
/>
|
<Trans>Cancel</Trans>
|
||||||
</Input.Wrapper>
|
</Button>
|
||||||
|
<Button type="submit" leftIcon={<TbDeviceFloppy size={16} />} loading={saveCustomCode.loading}>
|
||||||
<Input.Wrapper description={<Trans>Custom JS code that will be executed on page load</Trans>}>
|
<Trans>Save</Trans>
|
||||||
<Editor
|
</Button>
|
||||||
height="30vh"
|
</Group>
|
||||||
defaultLanguage="javascript"
|
</Stack>
|
||||||
theme={editorTheme}
|
</form>
|
||||||
options={{ minimap: { enabled: false } }}
|
</>
|
||||||
{...form.getInputProps("customJs")}
|
)
|
||||||
/>
|
}
|
||||||
</Input.Wrapper>
|
|
||||||
|
|
||||||
<Group>
|
|
||||||
<Button variant="default" onClick={() => dispatch(redirectToSelectedSource())}>
|
|
||||||
<Trans>Cancel</Trans>
|
|
||||||
</Button>
|
|
||||||
<Button type="submit" leftIcon={<TbDeviceFloppy size={16} />} loading={saveCustomCode.loading}>
|
|
||||||
<Trans>Save</Trans>
|
|
||||||
</Button>
|
|
||||||
</Group>
|
|
||||||
</Stack>
|
|
||||||
</form>
|
|
||||||
</>
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user