-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
371 additions
and
31 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
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
56 changes: 43 additions & 13 deletions
56
web/src/ui/pages/launcher/formFields/YamlCodeBlock.stories.tsx
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 |
---|---|---|
@@ -1,25 +1,55 @@ | ||
import type { Meta, StoryObj } from "@storybook/react"; | ||
import { YamlCodeBlockFormField } from "./YamlCodeBlockFormField"; | ||
import { action } from "@storybook/addon-actions"; | ||
import { css } from "ui/theme"; | ||
import { useState } from "react"; | ||
import type { Stringifyable } from "core/tools/Stringifyable"; | ||
|
||
const meta = { | ||
title: "pages/Launcher/formFields/YamlCodeBlock", | ||
component: YamlCodeBlockFormField | ||
component: StoryWrapper | ||
} satisfies Meta<typeof YamlCodeBlockFormField>; | ||
|
||
export default meta; | ||
|
||
type Story = StoryObj<typeof meta>; | ||
|
||
export const Default: Story = { | ||
args: { | ||
"title": "This is the title", | ||
"description": "This is the description", | ||
"expectedDataType": "object", | ||
"value": { | ||
"key1": "value1", | ||
"key2": "value2" | ||
}, | ||
"onChange": action("Value changed") | ||
} | ||
}; | ||
const onChangeAction = action("onChange"); | ||
|
||
function StoryWrapper() { | ||
const [value, setValue] = useState<Stringifyable[] | Record<string, Stringifyable>>({ | ||
"key1": "value1", | ||
"key2": 42, | ||
"arr": ["a", "b", "c"], | ||
"obj": { | ||
"isSomething": true, | ||
"name": "John", | ||
"nested": { | ||
"a": 1, | ||
"b": 2 | ||
} | ||
} | ||
}); | ||
|
||
return ( | ||
<> | ||
<YamlCodeBlockFormField | ||
className={css({ "width": 400, "maxHeight": 400 })} | ||
title="This is the title" | ||
description="This is the description" | ||
expectedDataType="object" | ||
value={value} | ||
onChange={newValue => { | ||
onChangeAction(newValue); | ||
setValue(newValue); | ||
}} | ||
/> | ||
<br /> | ||
<br /> | ||
<p>Value: </p> | ||
<pre>{JSON.stringify(value, null, 4)}</pre> | ||
</> | ||
); | ||
} | ||
|
||
export const Default: Story = {}; |
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
83 changes: 83 additions & 0 deletions
83
web/src/ui/pages/launcher/formFields/YamlCodeBlockFormField/YamlCodeEditor.tsx
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,83 @@ | ||
import CodeMirror from "@uiw/react-codemirror"; | ||
import { yaml } from "@codemirror/lang-yaml"; | ||
import { createTheme } from "@uiw/codemirror-themes"; | ||
import { tags } from "@lezer/highlight"; | ||
import { useMemo } from "react"; | ||
import { alpha } from "@mui/system"; | ||
import { tss } from "tss"; | ||
|
||
type Props = { | ||
className?: string; | ||
yamlCode: string; | ||
onYamlCodeChange: (newCode: string) => void; | ||
defaultHeight: number; | ||
}; | ||
|
||
export default function YamlCodeEditor(props: Props) { | ||
const { className, yamlCode, defaultHeight, onYamlCodeChange } = props; | ||
|
||
const { cx, classes, theme } = useStyles(); | ||
|
||
const codeMirrorTheme = useMemo( | ||
() => | ||
createTheme({ | ||
theme: theme.isDarkModeEnabled ? "dark" : "light", | ||
settings: { | ||
background: theme.colors.useCases.surfaces.surface1, | ||
foreground: theme.colors.useCases.typography.textPrimary, | ||
caret: theme.colors.useCases.typography.textFocus, | ||
selection: alpha(theme.colors.useCases.typography.textFocus, 0.6), | ||
selectionMatch: alpha( | ||
theme.colors.useCases.typography.textFocus, | ||
0.3 | ||
), | ||
lineHighlight: alpha( | ||
theme.colors.useCases.typography.textFocus, | ||
0.05 | ||
), | ||
gutterBackground: theme.colors.useCases.surfaces.surface2, | ||
gutterForeground: theme.colors.useCases.typography.textTertiary | ||
}, | ||
styles: [ | ||
{ | ||
tag: [tags.comment], | ||
color: theme.colors.useCases.typography.textDisabled, | ||
fontStyle: "italic" | ||
}, | ||
{ | ||
tag: [ | ||
tags.name, | ||
tags.deleted, | ||
tags.character, | ||
tags.propertyName, | ||
tags.macroName | ||
], | ||
color: theme.colors.useCases.typography.textSecondary | ||
}, | ||
{ | ||
tag: [tags.definition(tags.name), tags.separator], | ||
color: theme.colors.useCases.typography.textTertiary | ||
} | ||
] | ||
}), | ||
[theme.isDarkModeEnabled] | ||
); | ||
|
||
return ( | ||
<CodeMirror | ||
className={cx(classes.root, className)} | ||
value={yamlCode} | ||
theme={codeMirrorTheme} | ||
height={`${defaultHeight}px`} | ||
extensions={[yaml()]} | ||
onChange={onYamlCodeChange} | ||
/> | ||
); | ||
} | ||
|
||
const useStyles = tss.withName({ YamlCodeEditor }).create(({ theme }) => ({ | ||
"root": { | ||
"borderRadius": theme.spacing(1), | ||
"overflow": "hidden" | ||
} | ||
})); |
1 change: 1 addition & 0 deletions
1
web/src/ui/pages/launcher/formFields/YamlCodeBlockFormField/index.ts
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 @@ | ||
export * from "./YamlCodeBlockFormField"; |
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
Oops, something went wrong.