-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathMarkupEditorComponent.tsx
More file actions
62 lines (53 loc) · 1.96 KB
/
Copy pathMarkupEditorComponent.tsx
File metadata and controls
62 lines (53 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import {useEffect, useLayoutEffect, useRef} from 'react';
import type {QAProps} from '@gravity-ui/uikit';
import type {EditorInt} from './Editor';
export type MarkupEditorComponentProps = QAProps & {
editor: EditorInt;
autofocus?: boolean;
className?: string;
children?: React.ReactNode;
};
export const MarkupEditorComponent: React.FC<MarkupEditorComponentProps> =
function MarkupEditorComponent({editor, autofocus, className, children, qa}) {
const ref = useRef<HTMLDivElement>(null);
// insert editor to dom
useLayoutEffect(() => {
const domElem = editor.markupEditor.cm.dom;
if (ref.current) {
ref.current.appendChild(domElem);
}
return () => {
domElem.remove();
};
}, [editor.markupEditor]);
// update editor after connecting to dom
useEffect(() => {
editor.markupEditor.cm.requestMeasure();
if (autofocus) {
editor.markupEditor.focus();
}
}, [editor.markupEditor]);
// scroll to line on mount
useEffect(() => {
const scrollToLine = editor.initialScrollToLine;
if (editor.markupConfig.lineNumbers?.enabled && scrollToLine !== undefined) {
editor.moveCursor({line: scrollToLine});
}
}, [editor]);
return (
// eslint-disable-next-line jsx-a11y/click-events-have-key-events, jsx-a11y/no-static-element-interactions
<div
data-qa={qa}
ref={ref}
className={className}
onClick={(event) => {
const target = event.target;
if (target instanceof Element && target.classList.contains('cm-editor')) {
editor.markupEditor.focus();
}
}}
>
{children}
</div>
);
};