PagePathHeader.tsx 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. import {
  2. useState, useCallback, memo,
  3. } from 'react';
  4. import type { IPagePopulatedToShowRevision } from '@growi/core';
  5. import { DevidedPagePath } from '@growi/core/dist/models';
  6. import { normalizePath } from '@growi/core/dist/utils/path-utils';
  7. import { useTranslation } from 'next-i18next';
  8. import { ValidationTarget } from '~/client/util/input-validator';
  9. import LinkedPagePath from '~/models/linked-page-path';
  10. import { usePageSelectModal } from '~/stores/modal';
  11. import ClosableTextInput from '../Common/ClosableTextInput';
  12. import { PagePathHierarchicalLink } from '../Common/PagePathHierarchicalLink';
  13. import { usePagePathRenameHandler } from '../PageEditor/page-path-rename-utils';
  14. import { PageSelectModal } from '../PageSelectModal/PageSelectModal';
  15. import styles from './PagePathHeader.module.scss';
  16. const moduleClass = styles['page-path-header'];
  17. type Props = {
  18. currentPage: IPagePopulatedToShowRevision,
  19. className?: string,
  20. maxWidth?: number,
  21. onRenameTerminated?: () => void,
  22. }
  23. export const PagePathHeader = memo((props: Props): JSX.Element => {
  24. const { t } = useTranslation();
  25. const {
  26. currentPage, className, maxWidth, onRenameTerminated,
  27. } = props;
  28. const dPagePath = new DevidedPagePath(currentPage.path, true);
  29. const parentPagePath = dPagePath.former;
  30. const linkedPagePath = new LinkedPagePath(parentPagePath);
  31. const [isRenameInputShown, setRenameInputShown] = useState(false);
  32. const [isHover, setHover] = useState(false);
  33. // const [isIconHidden, setIsIconHidden] = useState(false);
  34. const { data: PageSelectModalData, open: openPageSelectModal } = usePageSelectModal();
  35. const isOpened = PageSelectModalData?.isOpened ?? false;
  36. const pagePathRenameHandler = usePagePathRenameHandler(currentPage);
  37. const rename = useCallback((inputText) => {
  38. const pathToRename = normalizePath(`${inputText}/${dPagePath.latter}`);
  39. pagePathRenameHandler(pathToRename,
  40. () => {
  41. setRenameInputShown(false);
  42. onRenameTerminated?.();
  43. },
  44. () => {
  45. setRenameInputShown(true);
  46. });
  47. }, [dPagePath.latter, pagePathRenameHandler, onRenameTerminated]);
  48. const cancel = useCallback(() => {
  49. // reset
  50. setRenameInputShown(false);
  51. }, []);
  52. const onClickEditButton = useCallback(() => {
  53. // reset
  54. setRenameInputShown(true);
  55. }, []);
  56. // TODO: https://redmine.weseek.co.jp/issues/141062
  57. // Truncate left side and don't use getElementById
  58. //
  59. // useEffect(() => {
  60. // const areaElem = document.getElementById('grw-page-path-header-container');
  61. // const linkElem = document.getElementById('grw-page-path-hierarchical-link');
  62. // const areaElemWidth = areaElem?.offsetWidth;
  63. // const linkElemWidth = linkElem?.offsetWidth;
  64. // if (areaElemWidth && linkElemWidth) {
  65. // setIsIconHidden(linkElemWidth > areaElemWidth);
  66. // }
  67. // else {
  68. // setIsIconHidden(false);
  69. // }
  70. // }, [currentPage]);
  71. //
  72. // const styles: CSSProperties | undefined = isIconHidden ? { direction: 'rtl' } : undefined;
  73. if (dPagePath.isRoot) {
  74. return <></>;
  75. }
  76. return (
  77. <div
  78. id="page-path-header"
  79. className={`d-flex ${moduleClass} ${className ?? ''} small position-relative ms-2`}
  80. style={{ maxWidth }}
  81. onMouseEnter={() => setHover(true)}
  82. onMouseLeave={() => setHover(false)}
  83. >
  84. <div
  85. className="page-path-header-input d-inline-block overflow-x-scroll"
  86. >
  87. { isRenameInputShown && (
  88. <div className="position-relative">
  89. <div className="position-absolute w-100">
  90. <ClosableTextInput
  91. value={parentPagePath}
  92. placeholder={t('Input parent page path')}
  93. inputClassName="form-control-sm"
  94. onPressEnter={rename}
  95. onPressEscape={cancel}
  96. onBlur={rename}
  97. validationTarget={ValidationTarget.PAGE}
  98. useAutosizeInput
  99. />
  100. </div>
  101. </div>
  102. ) }
  103. <div
  104. className={`${isRenameInputShown ? 'invisible' : ''} text-truncate`}
  105. // style={styles}
  106. >
  107. <PagePathHierarchicalLink
  108. linkedPagePath={linkedPagePath}
  109. // isIconHidden={isIconHidden}
  110. />
  111. </div>
  112. </div>
  113. <div
  114. className={`page-path-header-buttons d-flex align-items-center ${isHover && !isRenameInputShown ? '' : 'invisible'}`}
  115. >
  116. <button
  117. type="button"
  118. className="btn btn-outline-neutral-secondary me-2 d-flex align-items-center justify-content-center"
  119. onClick={onClickEditButton}
  120. >
  121. <span className="material-symbols-outlined fs-6">edit</span>
  122. </button>
  123. <button
  124. type="button"
  125. className="btn btn-outline-neutral-secondary d-flex align-items-center justify-content-center"
  126. onClick={openPageSelectModal}
  127. >
  128. <span className="material-symbols-outlined fs-6">account_tree</span>
  129. </button>
  130. </div>
  131. {isOpened && <PageSelectModal />}
  132. </div>
  133. );
  134. });