| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- import react, { useMemo, useRef } from 'react';
- import { CodeMirrorEditorReadOnly, GlobalCodeMirrorEditorKey } from '@growi/editor';
- import { throttle } from 'throttle-debounce';
- import { useShouldExpandContent } from '~/client/services/layout';
- import { useSWRxCurrentPage, useIsLatestRevision } from '~/stores/page';
- import { usePreviewOptions } from '~/stores/renderer';
- import { EditorNavbar } from './EditorNavbar';
- import Preview from './Preview';
- import { useScrollSync } from './ScrollSyncHelper';
- export const PageEditorReadOnly = react.memo((): JSX.Element => {
- const previewRef = useRef<HTMLDivElement>(null);
- const { data: currentPage } = useSWRxCurrentPage();
- const { data: rendererOptions } = usePreviewOptions();
- const { data: isLatestRevision } = useIsLatestRevision();
- const shouldExpandContent = useShouldExpandContent(currentPage);
- const { scrollEditorHandler, scrollPreviewHandler } = useScrollSync(GlobalCodeMirrorEditorKey.READONLY, previewRef);
- const scrollEditorHandlerThrottle = useMemo(() => throttle(25, scrollEditorHandler), [scrollEditorHandler]);
- const scrollPreviewHandlerThrottle = useMemo(() => throttle(25, scrollPreviewHandler), [scrollPreviewHandler]);
- const revisionBody = currentPage?.revision?.body;
- if (rendererOptions == null || isLatestRevision) {
- return <></>;
- }
- return (
- <div id="page-editor" className="flex-expand-vert">
- <EditorNavbar />
- <div className="flex-expand-horiz">
- <div className="page-editor-editor-container flex-expand-vert border-end">
- <CodeMirrorEditorReadOnly
- markdown={revisionBody}
- onScroll={scrollEditorHandlerThrottle}
- />
- </div>
- <div
- ref={previewRef}
- onScroll={scrollPreviewHandlerThrottle}
- className="page-editor-preview-container flex-expand-vert overflow-y-auto d-none d-lg-flex"
- >
- <Preview
- markdown={revisionBody}
- pagePath={currentPage?.path}
- rendererOptions={rendererOptions}
- expandContentWidth={shouldExpandContent}
- />
- </div>
- </div>
- </div>
- );
- });
|