PagePathHeader.tsx 5.3 KB

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