PageTitleHeader.tsx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import type { FC } from 'react';
  2. import { useState, useCallback, useEffect } from 'react';
  3. import nodePath from 'path';
  4. import type { IPagePopulatedToShowRevision } from '@growi/core';
  5. import { DevidedPagePath } from '@growi/core/dist/models';
  6. import { pathUtils } from '@growi/core/dist/utils';
  7. import { isMovablePage } from '@growi/core/dist/utils/page-path-utils';
  8. import { useTranslation } from 'next-i18next';
  9. import { ValidationTarget } from '~/client/util/input-validator';
  10. import ClosableTextInput from '../Common/ClosableTextInput';
  11. import { CopyDropdown } from '../Common/CopyDropdown';
  12. import { usePagePathRenameHandler } from '../PageEditor/page-path-rename-utils';
  13. import styles from './PageTitleHeader.module.scss';
  14. const moduleClass = styles['page-title-header'] ?? '';
  15. const borderColorClass = styles['page-title-header-border-color'] ?? '';
  16. type Props = {
  17. currentPage: IPagePopulatedToShowRevision,
  18. className?: string,
  19. };
  20. export const PageTitleHeader: FC<Props> = (props) => {
  21. const { t } = useTranslation();
  22. const { currentPage } = props;
  23. const currentPagePath = currentPage.path;
  24. const isMovable = isMovablePage(currentPagePath);
  25. const dPagePath = new DevidedPagePath(currentPage.path, true);
  26. const pageTitle = dPagePath.latter;
  27. const [isRenameInputShown, setRenameInputShown] = useState(false);
  28. const [editedPagePath, setEditedPagePath] = useState(currentPagePath);
  29. const pagePathRenameHandler = usePagePathRenameHandler(currentPage);
  30. const editedPageTitle = nodePath.basename(editedPagePath);
  31. // TODO: https://redmine.weseek.co.jp/issues/142729
  32. // https://regex101.com/r/Wg2Hh6/1
  33. const untitledPageRegex = /^Untitled-\d+$/;
  34. const isNewlyCreatedPage = (currentPage.wip && currentPage.latestRevision == null && untitledPageRegex.test(editedPageTitle)) ?? false;
  35. const onRenameFinish = useCallback(() => {
  36. setRenameInputShown(false);
  37. }, []);
  38. const onRenameFailure = useCallback(() => {
  39. setRenameInputShown(true);
  40. }, []);
  41. const onInputChange = useCallback((inputText: string) => {
  42. const newPageTitle = pathUtils.removeHeadingSlash(inputText);
  43. const parentPagePath = pathUtils.addTrailingSlash(nodePath.dirname(currentPage.path));
  44. const newPagePath = nodePath.resolve(parentPagePath, newPageTitle);
  45. setEditedPagePath(newPagePath);
  46. }, [currentPage?.path, setEditedPagePath]);
  47. const onPressEnter = useCallback(() => {
  48. pagePathRenameHandler(editedPagePath, onRenameFinish, onRenameFailure);
  49. }, [editedPagePath, onRenameFailure, onRenameFinish, pagePathRenameHandler]);
  50. const onPressEscape = useCallback(() => {
  51. setEditedPagePath(currentPagePath);
  52. setRenameInputShown(false);
  53. }, [currentPagePath]);
  54. const onClickPageTitle = useCallback(() => {
  55. if (!isMovable) {
  56. return;
  57. }
  58. setEditedPagePath(currentPagePath);
  59. setRenameInputShown(true);
  60. }, [currentPagePath, isMovable]);
  61. useEffect(() => {
  62. if (isNewlyCreatedPage) {
  63. setRenameInputShown(true);
  64. }
  65. }, [currentPage._id, isNewlyCreatedPage]);
  66. return (
  67. <div className={`d-flex ${moduleClass} ${props.className ?? ''} position-relative`}>
  68. <div className="me-1 d-inline-block overflow-hidden">
  69. { isRenameInputShown && (
  70. <div className="position-absolute w-100">
  71. <ClosableTextInput
  72. value={isNewlyCreatedPage ? '' : editedPageTitle}
  73. placeholder={t('Input page name')}
  74. inputClassName="fs-4"
  75. onPressEnter={onPressEnter}
  76. onPressEscape={onPressEscape}
  77. onChange={onInputChange}
  78. onClickOutside={() => { setRenameInputShown(false) }}
  79. validationTarget={ValidationTarget.PAGE}
  80. />
  81. </div>
  82. ) }
  83. <h1
  84. className={`mb-0 mb-sm-1 px-2 fs-4
  85. ${isRenameInputShown ? 'invisible' : ''} text-truncate
  86. ${isMovable ? 'border border-2 rounded-2' : ''} ${borderColorClass}
  87. `}
  88. onClick={onClickPageTitle}
  89. >
  90. {pageTitle}
  91. </h1>
  92. </div>
  93. <div className={`${isRenameInputShown ? 'invisible' : ''} d-flex align-items-center`}>
  94. { currentPage.wip && (
  95. <span className="badge rounded-pill text-bg-secondary ms-2">WIP</span>
  96. )}
  97. <CopyDropdown
  98. pageId={currentPage._id}
  99. pagePath={currentPage.path}
  100. dropdownToggleId={`copydropdown-${currentPage._id}`}
  101. dropdownToggleClassName="p-1"
  102. >
  103. <span className="material-symbols-outlined fs-6">content_paste</span>
  104. </CopyDropdown>
  105. </div>
  106. </div>
  107. );
  108. };