PageEditorReadOnly.tsx 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import react, { useMemo, useRef } from 'react';
  2. import { CodeMirrorEditorReadOnly, GlobalCodeMirrorEditorKey } from '@growi/editor';
  3. import { throttle } from 'throttle-debounce';
  4. import { useShouldExpandContent } from '~/client/services/layout';
  5. import { useSWRxCurrentPage, useIsLatestRevision } from '~/stores/page';
  6. import { usePreviewOptions } from '~/stores/renderer';
  7. import { EditorNavbar } from './EditorNavbar';
  8. import Preview from './Preview';
  9. import { useScrollSync } from './ScrollSyncHelper';
  10. export const PageEditorReadOnly = react.memo((): JSX.Element => {
  11. const previewRef = useRef<HTMLDivElement>(null);
  12. const { data: currentPage } = useSWRxCurrentPage();
  13. const { data: rendererOptions } = usePreviewOptions();
  14. const { data: isLatestRevision } = useIsLatestRevision();
  15. const shouldExpandContent = useShouldExpandContent(currentPage);
  16. const { scrollEditorHandler, scrollPreviewHandler } = useScrollSync(GlobalCodeMirrorEditorKey.READONLY, previewRef);
  17. const scrollEditorHandlerThrottle = useMemo(() => throttle(25, scrollEditorHandler), [scrollEditorHandler]);
  18. const scrollPreviewHandlerThrottle = useMemo(() => throttle(25, scrollPreviewHandler), [scrollPreviewHandler]);
  19. const revisionBody = currentPage?.revision?.body;
  20. if (rendererOptions == null || isLatestRevision) {
  21. return <></>;
  22. }
  23. return (
  24. <div id="page-editor" className="flex-expand-vert">
  25. <EditorNavbar />
  26. <div className="flex-expand-horiz">
  27. <div className="page-editor-editor-container flex-expand-vert border-end">
  28. <CodeMirrorEditorReadOnly
  29. markdown={revisionBody}
  30. onScroll={scrollEditorHandlerThrottle}
  31. />
  32. </div>
  33. <div
  34. ref={previewRef}
  35. onScroll={scrollPreviewHandlerThrottle}
  36. className="page-editor-preview-container flex-expand-vert overflow-y-auto d-none d-lg-flex"
  37. >
  38. <Preview
  39. markdown={revisionBody}
  40. pagePath={currentPage?.path}
  41. rendererOptions={rendererOptions}
  42. expandContentWidth={shouldExpandContent}
  43. />
  44. </div>
  45. </div>
  46. </div>
  47. );
  48. });