| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- import { useEffect } from 'react';
- import type { Extension } from '@codemirror/state';
- import { keymap, scrollPastEnd } from '@codemirror/view';
- import { GlobalCodeMirrorEditorKey, AcceptedUploadFileType } from '../consts';
- import { useCodeMirrorEditorIsolated } from '../stores';
- import { CodeMirrorEditor } from '.';
- const additionalExtensions: Extension[] = [
- scrollPastEnd(),
- ];
- type Props = {
- onChange?: (value: string) => void,
- onSave?: () => void,
- onUpload?: (files: File[]) => void,
- acceptedFileType?: AcceptedUploadFileType,
- indentSize?: number,
- }
- export const CodeMirrorEditorMain = (props: Props): JSX.Element => {
- const {
- onSave, onChange, onUpload, acceptedFileType, indentSize,
- } = props;
- const { data: codeMirrorEditor } = useCodeMirrorEditorIsolated(GlobalCodeMirrorEditorKey.MAIN);
- const acceptedFileTypeNoOpt = acceptedFileType ?? AcceptedUploadFileType.NONE;
- // setup additional extensions
- useEffect(() => {
- return codeMirrorEditor?.appendExtensions?.(additionalExtensions);
- }, [codeMirrorEditor]);
- // set handler to save with shortcut key
- useEffect(() => {
- if (onSave == null) {
- return;
- }
- const extension = keymap.of([
- {
- key: 'Mod-s',
- preventDefault: true,
- run: () => {
- const doc = codeMirrorEditor?.getDoc();
- if (doc != null) {
- onSave();
- }
- return true;
- },
- },
- ]);
- const cleanupFunction = codeMirrorEditor?.appendExtensions?.(extension);
- return cleanupFunction;
- }, [codeMirrorEditor, onSave]);
- return (
- <CodeMirrorEditor
- editorKey={GlobalCodeMirrorEditorKey.MAIN}
- onChange={onChange}
- onUpload={onUpload}
- acceptedFileType={acceptedFileTypeNoOpt}
- indentSize={indentSize}
- />
- );
- };
|