forked from usebruno/bruno
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Improve Manoco editor implementation
- Better styling for singleline editor - Multiline singleline editor - Improved hover Ref: usebruno#1450
- Loading branch information
1 parent
1555fa7
commit 86f0e3f
Showing
11 changed files
with
323 additions
and
161 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
39 changes: 39 additions & 0 deletions
39
packages/bruno-app/src/components/CodeEditor/Monaco/MonacoSingleline.module.scss
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,39 @@ | ||
/** | ||
* This file is part of bruno-app. | ||
* For license information, see the file LICENSE_GPL3 at the root directory of this distribution. | ||
*/ | ||
.paper { | ||
width: 100%; | ||
height: fit-content; /* Monaco will auto set the height */ | ||
|
||
padding: var(--mantine-spacing-xs); | ||
border: 1px solid var(--mantine-color-dark-4); | ||
border-radius: var(--mantine-radius-xs); | ||
} | ||
.paperHidden { | ||
width: 100%; | ||
height: fit-content; /* Monaco will auto set the height */ | ||
|
||
background-color: transparent !important; | ||
} | ||
|
||
.editor { | ||
min-height: 22px; | ||
|
||
:global { | ||
.monaco-editor { | ||
.lines-content { | ||
padding-left: 0; | ||
} | ||
.view-line { | ||
font-family: var(--mantine-font-family-monospace); | ||
line-height: var(--mantine-line-height-sm); | ||
font-size: var(--mantine-font-size-sm); | ||
font-weight: 400; | ||
} | ||
.scroll-decoration { | ||
display: none; | ||
} | ||
} | ||
} | ||
} |
123 changes: 123 additions & 0 deletions
123
packages/bruno-app/src/components/CodeEditor/Monaco/MonacoSingleline.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,123 @@ | ||
/** | ||
* This file is part of bruno-app. | ||
* For license information, see the file LICENSE_GPL3 at the root directory of this distribution. | ||
*/ | ||
import { Editor, Monaco, useMonaco } from '@monaco-editor/react'; | ||
import { useEffect, useRef, useState } from 'react'; | ||
import { debounce } from 'lodash'; | ||
import { | ||
BrunoEditorCallbacks, | ||
addMonacoCommands, | ||
addMonacoSingleLineActions, | ||
setMonacoVariables | ||
} from 'utils/monaco/monacoUtils'; | ||
import { getAllVariables } from 'utils/collections'; | ||
import { useTheme } from 'providers/Theme'; | ||
import { editor } from 'monaco-editor'; | ||
import { Paper } from '@mantine/core'; | ||
import classes from './MonacoSingleline.module.scss'; | ||
|
||
type MonacoSinglelineProps = { | ||
collection: { | ||
collectionVariables: unknown; | ||
activeEnvironmentUid: string | undefined; | ||
}; | ||
readOnly: boolean; | ||
value: string; | ||
withVariables: boolean; | ||
allowLinebreaks: boolean; | ||
asInput: boolean; | ||
|
||
onChange: (newValue: string) => void; | ||
onRun: () => void; | ||
onSave: () => void; | ||
}; | ||
|
||
export const MonacoSingleline: React.FC<MonacoSinglelineProps> = ({ | ||
collection, | ||
onChange, | ||
onRun, | ||
onSave, | ||
readOnly = false, | ||
value, | ||
withVariables = false, | ||
allowLinebreaks = false, | ||
asInput = false | ||
}) => { | ||
const monaco = useMonaco(); | ||
const { displayedTheme } = useTheme(); | ||
const callbackRefs = useRef<BrunoEditorCallbacks>({}); | ||
const [height, setHeight] = useState(22); | ||
|
||
useEffect(() => { | ||
// Save the reference to the callback so the callbacks always update | ||
// OnMount is only executed once | ||
callbackRefs.current.onRun = onRun; | ||
callbackRefs.current.onSave = onSave; | ||
callbackRefs.current.onChange = onChange; | ||
}, [onRun, onSave, onChange]); | ||
|
||
const debounceChanges = debounce((newValue) => { | ||
onChange(newValue); | ||
}, 300); | ||
const handleEditorChange = (value: string | undefined) => { | ||
debounceChanges(value); | ||
}; | ||
|
||
const onMount = (editor: editor.IStandaloneCodeEditor, monaco: Monaco) => { | ||
addMonacoCommands(monaco, editor, callbackRefs.current); | ||
addMonacoSingleLineActions(editor, monaco, allowLinebreaks, setHeight); | ||
}; | ||
|
||
useEffect(() => { | ||
const allVariables = getAllVariables(collection); | ||
if (allVariables && withVariables && monaco) { | ||
setMonacoVariables(monaco, allVariables, 'plaintext'); | ||
} | ||
}, [collection.collectionVariables, collection.activeEnvironmentUid, withVariables, monaco]); | ||
|
||
return ( | ||
<Paper className={asInput ? classes.paper : classes.paperHidden}> | ||
<Editor | ||
options={{ | ||
readOnly: readOnly, | ||
wordWrap: 'off', | ||
wrappingIndent: 'indent', | ||
autoIndent: 'keep', | ||
formatOnType: true, | ||
formatOnPaste: true, | ||
scrollBeyondLastLine: false, | ||
automaticLayout: true, | ||
scrollbar: { | ||
vertical: allowLinebreaks ? 'auto' : 'hidden', | ||
horizontal: 'hidden' | ||
}, | ||
folding: false, | ||
renderLineHighlight: 'none', | ||
lineNumbers: 'off', | ||
lineDecorationsWidth: 0, | ||
lineNumbersMinChars: 0, | ||
glyphMargin: false, | ||
links: false, | ||
overviewRulerLanes: 0, | ||
overviewRulerBorder: false, | ||
hideCursorInOverviewRuler: true, | ||
scrollBeyondLastColumn: 0, | ||
showFoldingControls: 'never', | ||
selectionHighlight: false, | ||
occurrencesHighlight: 'off', | ||
find: { addExtraSpaceOnTop: false, autoFindInSelection: 'never', seedSearchStringFromSelection: 'never' }, | ||
minimap: { enabled: false } | ||
}} | ||
className={classes.editor} | ||
theme={displayedTheme === 'dark' ? 'bruno-dark' : 'bruno-light'} | ||
loading={null} // Loading looks weird with singeline editor | ||
language={'plaintext'} | ||
value={value} | ||
onMount={onMount} | ||
onChange={!readOnly ? handleEditorChange : () => {}} | ||
height={height} | ||
/> | ||
</Paper> | ||
); | ||
}; |
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
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
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.