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

Path onMount prop to Monaco Editor #1099

Merged
merged 1 commit into from
Jun 18, 2024
Merged
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
16 changes: 12 additions & 4 deletions src/harmony/FormEditor/FormEditor.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useCallback, useContext, useEffect, useMemo, useRef, useState } from 'react';
import React, { useCallback, useContext, useEffect, useMemo, useRef, useState, ComponentProps } from 'react';
import { useDropzone } from 'react-dropzone';
import { IconAttachOutline } from '@taskany/icons';
import cn from 'classnames';
Expand Down Expand Up @@ -27,6 +27,7 @@ interface File {
type: string;
filePath: string;
}
type OnMountCallback = ComponentProps<typeof Editor>['onMount'];

interface FormEditorProps extends Omit<React.HTMLAttributes<HTMLDivElement>, 'onChange' | 'onBlur' | 'onFocus'> {
id?: string;
Expand Down Expand Up @@ -55,6 +56,8 @@ interface FormEditorProps extends Omit<React.HTMLAttributes<HTMLDivElement>, 'on
onUploadSuccess?: () => void;
onUploadFail?: (message?: string) => void;
attachFormatter?: (files: File[]) => string;
options: React.ComponentProps<typeof Editor>['options'];
onMount?: OnMountCallback;
}

const defaultAttachmentsButtonMessage = 'Attach files';
Expand Down Expand Up @@ -116,7 +119,9 @@ export const FormEditor = React.forwardRef<HTMLDivElement, FormEditorProps>(
onUploadFail,
attachFormatter = defaultAttachFormatter,
className,
onMount,
view,
options,
...attrs
},
ref,
Expand All @@ -134,8 +139,7 @@ export const FormEditor = React.forwardRef<HTMLDivElement, FormEditorProps>(
const formCtx = useContext(formContext);
const disabled = formCtx.disabled || internalDisabled;

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const handleEditorDidMount = (editor: any /* IStandaloneEditor */) => {
const handleEditorDidMount: OnMountCallback = (editor, monaco) => {
monacoEditorRef.current = editor;

if (autoFocus) {
Expand All @@ -152,6 +156,8 @@ export const FormEditor = React.forwardRef<HTMLDivElement, FormEditorProps>(
});

setViewValue(value);

onMount?.(editor, monaco);
};

useEffect(() => {
Expand Down Expand Up @@ -267,6 +273,8 @@ export const FormEditor = React.forwardRef<HTMLDivElement, FormEditorProps>(
};
}, [height, contentHeight, maxEditorHeight, outline]);

const editorOptions = useMemo(() => ({ ...defaultOptions, ...options }), []);

return (
<div
tabIndex={0}
Expand Down Expand Up @@ -315,7 +323,7 @@ export const FormEditor = React.forwardRef<HTMLDivElement, FormEditorProps>(
theme="vs-dark"
defaultLanguage="markdown"
value={viewValue}
options={defaultOptions}
options={editorOptions}
onChange={onChange}
onMount={handleEditorDidMount}
className={cn(s.MonacoEditor)}
Expand Down
Loading