PageTitleHeader.tsx 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import type { ChangeEvent } 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 type { InputValidationResult } from '~/client/util/use-input-validator';
  10. import { ValidationTarget, useInputValidator } from '~/client/util/use-input-validator';
  11. import { EditorMode, useEditorMode, useIsUntitledPage } from '~/stores/ui';
  12. import { CopyDropdown } from '../Common/CopyDropdown';
  13. import { AutosizeSubmittableInput, getAdjustedMaxWidthForAutosizeInput } from '../Common/SubmittableInput';
  14. import { usePagePathRenameHandler } from '../PageEditor/page-path-rename-utils';
  15. import styles from './PageTitleHeader.module.scss';
  16. const moduleClass = styles['page-title-header'] ?? '';
  17. const borderColorClass = styles['page-title-header-border-color'] ?? '';
  18. type Props = {
  19. currentPage: IPagePopulatedToShowRevision,
  20. className?: string,
  21. maxWidth?: number,
  22. onMoveTerminated?: () => void,
  23. };
  24. export const PageTitleHeader = (props: Props): JSX.Element => {
  25. const { t } = useTranslation();
  26. const { currentPage, maxWidth, onMoveTerminated } = props;
  27. const currentPagePath = currentPage.path;
  28. const isMovable = isMovablePage(currentPagePath);
  29. const dPagePath = new DevidedPagePath(currentPage.path, true);
  30. const pageTitle = dPagePath.latter;
  31. const [isRenameInputShown, setRenameInputShown] = useState(false);
  32. const [editedPagePath, setEditedPagePath] = useState(currentPagePath);
  33. const [validationResult, setValidationResult] = useState<InputValidationResult>();
  34. const pagePathRenameHandler = usePagePathRenameHandler(currentPage);
  35. const inputValidator = useInputValidator(ValidationTarget.PAGE);
  36. const editedPageTitle = nodePath.basename(editedPagePath);
  37. const { data: editorMode } = useEditorMode();
  38. const { data: isUntitledPage } = useIsUntitledPage();
  39. const changeHandler = useCallback(async(e: ChangeEvent<HTMLInputElement>) => {
  40. const newPageTitle = pathUtils.removeHeadingSlash(e.target.value);
  41. const parentPagePath = pathUtils.addTrailingSlash(nodePath.dirname(currentPage.path));
  42. const newPagePath = nodePath.resolve(parentPagePath, newPageTitle);
  43. setEditedPagePath(newPagePath);
  44. // validation
  45. const validationResult = inputValidator(e.target.value);
  46. setValidationResult(validationResult ?? undefined);
  47. }, [currentPage.path, inputValidator]);
  48. const rename = useCallback(() => {
  49. pagePathRenameHandler(editedPagePath,
  50. () => {
  51. setRenameInputShown(false);
  52. setValidationResult(undefined);
  53. onMoveTerminated?.();
  54. },
  55. () => {
  56. setRenameInputShown(true);
  57. });
  58. }, [editedPagePath, onMoveTerminated, pagePathRenameHandler]);
  59. const cancel = useCallback(() => {
  60. setEditedPagePath(currentPagePath);
  61. setValidationResult(undefined);
  62. setRenameInputShown(false);
  63. }, [currentPagePath]);
  64. const onClickPageTitle = useCallback(() => {
  65. if (!isMovable) {
  66. return;
  67. }
  68. setEditedPagePath(currentPagePath);
  69. setRenameInputShown(true);
  70. }, [currentPagePath, isMovable]);
  71. useEffect(() => {
  72. setEditedPagePath(currentPagePath);
  73. if (isUntitledPage && editorMode === EditorMode.Editor) {
  74. setRenameInputShown(true);
  75. }
  76. }, [currentPage._id, currentPagePath, editorMode, isUntitledPage]);
  77. const isInvalid = validationResult != null;
  78. const inputMaxWidth = maxWidth != null
  79. ? getAdjustedMaxWidthForAutosizeInput(maxWidth, 'md', validationResult != null ? false : undefined) - 16
  80. : undefined;
  81. return (
  82. <div className={`d-flex ${moduleClass} ${props.className ?? ''} position-relative`}>
  83. <div className="page-title-header-input me-1 d-inline-block">
  84. { isRenameInputShown && (
  85. <div className="position-relative">
  86. <div className="position-absolute w-100">
  87. <AutosizeSubmittableInput
  88. value={isUntitledPage ? '' : editedPageTitle}
  89. inputClassName={`form-control fs-4 ${isInvalid ? 'is-invalid' : ''}`}
  90. inputStyle={{ maxWidth: inputMaxWidth }}
  91. placeholder={t('Input page name')}
  92. onChange={changeHandler}
  93. onSubmit={rename}
  94. onCancel={cancel}
  95. autoFocus
  96. />
  97. </div>
  98. </div>
  99. ) }
  100. <h1
  101. className={`mb-0 mb-sm-1 px-2 fs-4
  102. ${isRenameInputShown ? 'invisible' : ''} text-truncate
  103. ${isMovable ? 'border border-2 rounded-2' : ''} ${borderColorClass}
  104. `}
  105. onClick={onClickPageTitle}
  106. >
  107. {pageTitle}
  108. </h1>
  109. </div>
  110. <div className={`${isRenameInputShown ? 'invisible' : ''} d-flex align-items-center`}>
  111. { currentPage.wip && (
  112. <span className="badge rounded-pill text-bg-secondary ms-2">WIP</span>
  113. )}
  114. <CopyDropdown
  115. pageId={currentPage._id}
  116. pagePath={currentPage.path}
  117. dropdownToggleId={`copydropdown-${currentPage._id}`}
  118. dropdownToggleClassName="p-1"
  119. >
  120. <span className="material-symbols-outlined fs-6">content_paste</span>
  121. </CopyDropdown>
  122. </div>
  123. </div>
  124. );
  125. };