Skip to content

Commit

Permalink
Update AutosizeCodeEditor (#70)
Browse files Browse the repository at this point in the history
* upd AutosizeCodeEditor

* Fix typo

---------

Co-authored-by: asimonok <[email protected]>
  • Loading branch information
vitPinchuk and asimonok authored Nov 26, 2024
1 parent 5d8c53b commit 8e0b080
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,5 @@
"typecheck": "tsc --emitDeclarationOnly false --noEmit"
},
"types": "dist/index.d.ts",
"version": "3.5.0"
"version": "3.5.1"
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,21 @@ const InTestIds = {
field: createSelector('data-testid field'),
};

const defaultModel = {
setEOL: jest.fn(),
};

const defaultMonaco = {
editor: {
EndOfLineSequence: {
'0': 'LF',
'1': 'CRLF',
LF: 0,
CRLF: 1,
},
},
} as any;

const editor = {
getPosition: () => ({
lineNumber: 12,
Expand All @@ -27,6 +42,7 @@ const editor = {
revealLineInCenter: (value: any) => value,
getSelection: () => null,
executeEdits: (source: any, edits: any) => true,
getModel: () => defaultModel,
};

/**
Expand All @@ -46,7 +62,7 @@ jest.mock('@grafana/ui', () => ({
/**
* Call the onEditorDidMount callback with the editor instance
*/
onEditorDidMount(editor, '');
onEditorDidMount(editor, defaultMonaco);

return (
<textarea
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ export const AutosizeCodeEditor: React.FC<Props> = ({
*/
const onEditorDidMountMain = useCallback(
(editor: monacoType.editor.IStandaloneCodeEditor, monaco: typeof monacoType) => {
/**
* Set end of line to \n to all OS
*/
const model = editor.getModel();
model?.setEOL(monaco.editor.EndOfLineSequence.LF);

setMonacoEditor(editor);
if (onEditorDidMount) {
onEditorDidMount(editor, monaco);
Expand All @@ -92,6 +98,12 @@ export const AutosizeCodeEditor: React.FC<Props> = ({
*/
const modalEditorDidMount = useCallback(
(editor: monacoType.editor.IStandaloneCodeEditor, monaco: typeof monacoType) => {
/**
* Set end of line to \n to all OS
*/
const model = editor.getModel();
model?.setEOL(monaco.editor.EndOfLineSequence.LF);

setMonacoEditorModal(editor);
if (monacoEditor) {
const positionsParams: monacoType.Position | null = monacoEditor?.getPosition();
Expand Down Expand Up @@ -133,13 +145,14 @@ export const AutosizeCodeEditor: React.FC<Props> = ({
setCurrentMonacoOptions={setCurrentMonacoOptions}
/>
<CodeEditor
value={value}
value={value.replaceAll('\\n', '\n')}
showMiniMap={isShowMiniMap}
height={staticHeight ?? height}
monacoOptions={currentMonacoOptions}
onEditorDidMount={onEditorDidMountMain}
onChange={(value) => {
onChange?.(value);
const currentValue = value.replaceAll('\n', '\\n');
onChange?.(currentValue);
setHeight(getHeightByValue(value, minHeight, maxHeight));
}}
{...restProps}
Expand All @@ -166,13 +179,14 @@ export const AutosizeCodeEditor: React.FC<Props> = ({
setCurrentMonacoOptions={setCurrentMonacoOptions}
/>
<CodeEditor
value={value}
value={value.replaceAll('\\n', '\n')}
showMiniMap={isShowMiniMap}
containerStyles={styles.modalEditor}
monacoOptions={currentMonacoOptions}
onEditorDidMount={modalEditorDidMount}
onChange={(value) => {
onChange?.(value);
const currentValue = value.replaceAll('\n', '\\n');
onChange?.(currentValue);
setHeight(getHeightByValue(value, minHeight, maxHeight));
}}
{...restProps}
Expand Down
2 changes: 1 addition & 1 deletion packages/components/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"compilerOptions": {
"target": "es5",
"target": "es2021",
"module": "esnext",
"jsx": "react",
"sourceMap": true,
Expand Down

0 comments on commit 8e0b080

Please sign in to comment.