PageEditorReadOnly.tsx 2.3 KB

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