PageEditorReadOnly.tsx 2.2 KB

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