Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: word-wrap editor #355

Merged
merged 15 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/components/Monaco/CodeEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export function CodeEditor({

return (
<ReactMonacoEditor
showToolbar
defaultLanguage="javascript"
options={{ readOnly }}
defaultValue={value}
Expand Down
51 changes: 51 additions & 0 deletions src/components/Monaco/EditorToolbar.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { Flex, Switch, Text } from '@radix-ui/themes'
import { useEffect } from 'react'
import { k6StudioLightBackground } from './themes/k6StudioLight'
import { Label } from '../Label'
import { useTheme } from '@/hooks/useTheme'
import { useLocalStorage } from 'react-use'

export type ToolbarState = {
wordWrap: 'on' | 'off'
}

type EditorToolbarProps = {
getState: (values: ToolbarState) => void
}

export const EditorToolbar = ({ getState }: EditorToolbarProps) => {
const [state, setState] = useLocalStorage<ToolbarState>(
'editorToolbarState',
{
wordWrap: 'off',
}
)
const theme = useTheme()

useEffect(() => {
if (state) {
getState(state)
}
}, [state, getState])

return (
<Flex
p="2"
style={{
backgroundColor: theme === 'dark' ? '#1e1e1e' : k6StudioLightBackground,
borderBottom: `1px solid ${theme === 'dark' ? '#4d4b4b' : '#e5e5e5'}`,
}}
>
<Label flexGrow="1">
<Text size="2">Word wrap</Text>
<Switch
size="1"
checked={state?.wordWrap === 'on'}
onCheckedChange={(checked) =>
setState({ wordWrap: checked ? 'on' : 'off' })
}
/>
</Label>
</Flex>
)
}
34 changes: 28 additions & 6 deletions src/components/Monaco/ReactMonacoEditor.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { useTheme } from '@/hooks/useTheme'
import { Editor, EditorProps, loader } from '@monaco-editor/react'
import * as monaco from 'monaco-editor'
import { EditorToolbar, ToolbarState } from './EditorToolbar'
import { useState } from 'react'
import { Flex } from '@radix-ui/themes'

loader.config({ monaco })

Expand All @@ -24,14 +27,33 @@ const defaultOptions: monaco.editor.IStandaloneEditorConstructionOptions = {
},
}

export function ReactMonacoEditor(props: EditorProps) {
interface ReactMonacoEditorProps extends EditorProps {
showToolbar?: boolean
}

export function ReactMonacoEditor({
showToolbar,
...props
}: ReactMonacoEditorProps) {
const theme = useTheme()
const [toolbarState, setToolbarState] = useState<ToolbarState>({
wordWrap: 'off',
})

return (
<Editor
{...props}
options={{ ...defaultOptions, ...props.options }}
theme={theme === 'dark' ? 'vs-dark' : 'k6-studio-light'}
/>
<Flex height="100%" width="100%" direction="column">
{showToolbar && (
<EditorToolbar getState={(state) => setToolbarState(state)} />
)}
<Editor
{...props}
options={{
...defaultOptions,
...props.options,
wordWrap: toolbarState.wordWrap,
}}
theme={theme === 'dark' ? 'vs-dark' : 'k6-studio-light'}
/>
</Flex>
)
}
4 changes: 3 additions & 1 deletion src/components/Monaco/ReadOnlyEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,7 @@ const options: editor.IStandaloneEditorConstructionOptions = {
}

export function ReadOnlyEditor(props: ComponentProps<typeof Editor>) {
return <ReactMonacoEditor height="100%" options={options} {...props} />
return (
<ReactMonacoEditor height="100%" options={options} {...props} showToolbar />
)
}
6 changes: 4 additions & 2 deletions src/components/WebLogView/ResponseDetails/Content.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { getContentType } from '@/utils/headers'
import { Preview } from './Preview'
import { Raw } from './Raw'
import { parseContent, toFormat } from './ResponseDetails.utils'
import { OriginalContent } from './OriginalContent'
import { ReadOnlyEditor } from '@/components/Monaco/ReadOnlyEditor'

export function Content({ data }: { data: ProxyData }) {
const [selectedTab, setSelectedTab] = useState('preview')
Expand Down Expand Up @@ -58,7 +58,9 @@ export function Content({ data }: { data: ProxyData }) {
{selectedTab === 'raw' && (
<Raw content={rawContent ?? ''} format={format} />
)}
{selectedTab === 'content' && <OriginalContent {...contentProps} />}
{selectedTab === 'content' && (
<ReadOnlyEditor language={format} value={content} />
)}
</Box>
</ScrollArea>
</Flex>
Expand Down
15 changes: 0 additions & 15 deletions src/components/WebLogView/ResponseDetails/OriginalContent.tsx

This file was deleted.

6 changes: 1 addition & 5 deletions src/components/WebLogView/ResponseDetails/Raw.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@ interface RawProps {
export function Raw({ content, format }: RawProps) {
return (
<Text size="1" wrap="pretty" css={style}>
<ReadOnlyEditor
language={format}
value={content}
options={{ wordWrap: 'on' }}
/>
<ReadOnlyEditor language={format} value={content} />
</Text>
)
}
Expand Down
2 changes: 1 addition & 1 deletion src/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ const log = {
} as const

const settings = {
getSettings: () => {
getSettings: (): Promise<AppSettings> => {
return ipcRenderer.invoke('settings:get')
},
saveSettings: (settings: AppSettings): Promise<AppSettings> => {
Expand Down
Loading