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

Update AutosizeCodeEditor #70

Merged
merged 2 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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
Loading