kosei-n 2 years ago
parent
commit
ceb6b92954

+ 15 - 15
apps/app/src/components/PageHeader/PageHeader.tsx

@@ -13,22 +13,22 @@ import { PageTitleHeader } from './PageTitleHeader';
 
 
 export const PageHeader: FC = () => {
 export const PageHeader: FC = () => {
   const { data: currentPage } = useSWRxCurrentPage();
   const { data: currentPage } = useSWRxCurrentPage();
-  const currentPagePath = currentPage?.path;
+  // const currentPagePath = currentPage?.path;
 
 
-  const [editedPagePath, setEditedPagePath] = useState(currentPagePath ?? '');
+  // const [editedPagePath, setEditedPagePath] = useState(currentPagePath ?? '');
 
 
-  const editedPageTitle = nodePath.basename(editedPagePath);
+  // const editedPageTitle = nodePath.basename(editedPagePath);
 
 
-  const onInputChangeForPagePath = useCallback((inputText: string) => {
-    const parentPath = pathUtils.addTrailingSlash(nodePath.dirname(currentPage?.path ?? ''));
-    const newPagePath = nodePath.resolve(parentPath, inputText);
+  // const onInputChangeForPagePath = useCallback((inputText: string) => {
+  //   const parentPath = pathUtils.addTrailingSlash(nodePath.dirname(currentPage?.path ?? ''));
+  //   const newPagePath = nodePath.resolve(parentPath, inputText);
 
 
-    setEditedPagePath(newPagePath);
-  }, [currentPage?.path, setEditedPagePath]);
+  //   setEditedPagePath(newPagePath);
+  // }, [currentPage?.path, setEditedPagePath]);
 
 
-  const onInputChangeForPageTitle = (inputText: string) => {
-    setEditedPagePath(inputText);
-  };
+  // const onInputChangeForPageTitle = (inputText: string) => {
+  //   setEditedPagePath(inputText);
+  // };
 
 
   if (currentPage == null) {
   if (currentPage == null) {
     return <></>;
     return <></>;
@@ -38,13 +38,13 @@ export const PageHeader: FC = () => {
     <>
     <>
       <PagePathHeader
       <PagePathHeader
         currentPage={currentPage}
         currentPage={currentPage}
-        inputValue={editedPagePath}
-        onInputChange={onInputChangeForPagePath}
+        // inputValue={editedPagePath}
+        // onInputChange={onInputChangeForPagePath}
       />
       />
       <PageTitleHeader
       <PageTitleHeader
         currentPage={currentPage}
         currentPage={currentPage}
-        inputValue={editedPageTitle}
-        onInputChange={onInputChangeForPageTitle}
+        // inputValue={editedPageTitle}
+        // onInputChange={onInputChangeForPageTitle}
       />
       />
     </>
     </>
   );
   );

+ 21 - 7
apps/app/src/components/PageHeader/PagePathHeader.tsx

@@ -17,8 +17,8 @@ import { usePagePathRenameHandler } from './page-header-utils';
 
 
 export type Props = {
 export type Props = {
   currentPage: IPagePopulatedToShowRevision
   currentPage: IPagePopulatedToShowRevision
-  inputValue: string
-  onInputChange?: (inputText: string) => void
+  // inputValue: string
+  // onInputChange?: (inputText: string) => void
 }
 }
 
 
 export const PagePathHeader: FC<Props> = (props) => {
 export const PagePathHeader: FC<Props> = (props) => {
@@ -28,10 +28,12 @@ export const PagePathHeader: FC<Props> = (props) => {
 
 
   const [isRenameInputShown, setRenameInputShown] = useState(false);
   const [isRenameInputShown, setRenameInputShown] = useState(false);
   const [isButtonsShown, setButtonShown] = useState(false);
   const [isButtonsShown, setButtonShown] = useState(false);
+  const [editedPagePath, setEditedPagePath] = useState(currentPagePath);
 
 
   const { data: editorMode } = useEditorMode();
   const { data: editorMode } = useEditorMode();
   const { data: PageSelectModalData, open: openPageSelectModal } = usePageSelectModal();
   const { data: PageSelectModalData, open: openPageSelectModal } = usePageSelectModal();
 
 
+
   const onRenameFinish = () => {
   const onRenameFinish = () => {
     setRenameInputShown(false);
     setRenameInputShown(false);
   };
   };
@@ -40,7 +42,16 @@ export const PagePathHeader: FC<Props> = (props) => {
     setRenameInputShown(true);
     setRenameInputShown(true);
   };
   };
 
 
-  const pagePathRenameHandler = usePagePathRenameHandler(currentPage, onRenameFinish, onRenameFailure);
+  const onInputChange = (inputText: string) => {
+    setEditedPagePath(inputText);
+  };
+
+  const onPressEscape = () => {
+    setEditedPagePath(currentPagePath);
+    setRenameInputShown(false);
+  };
+
+  const pagePathRenameHandler = usePagePathRenameHandler(currentPage);
 
 
   const stateHandler = { isRenameInputShown, setRenameInputShown };
   const stateHandler = { isRenameInputShown, setRenameInputShown };
 
 
@@ -59,12 +70,14 @@ export const PagePathHeader: FC<Props> = (props) => {
 
 
   const onClickEditButton = useCallback(() => {
   const onClickEditButton = useCallback(() => {
     if (isRenameInputShown) {
     if (isRenameInputShown) {
-      pagePathRenameHandler(props.inputValue);
+      pagePathRenameHandler(editedPagePath, onRenameFinish, onRenameFailure);
     }
     }
     else {
     else {
+      console.log(currentPagePath);
+      setEditedPagePath(currentPagePath);
       setRenameInputShown(true);
       setRenameInputShown(true);
     }
     }
-  }, [isRenameInputShown, pagePathRenameHandler, props.inputValue]);
+  }, [currentPagePath, editedPagePath, isRenameInputShown, pagePathRenameHandler]);
 
 
   const buttonStyle = isButtonsShown ? '' : 'd-none';
   const buttonStyle = isButtonsShown ? '' : 'd-none';
 
 
@@ -97,9 +110,10 @@ export const PagePathHeader: FC<Props> = (props) => {
           <TextInputForPageTitleAndPath
           <TextInputForPageTitleAndPath
             currentPage={currentPage}
             currentPage={currentPage}
             stateHandler={stateHandler}
             stateHandler={stateHandler}
-            inputValue={props.inputValue}
+            inputValue={editedPagePath}
             CustomComponent={PagePath}
             CustomComponent={PagePath}
-            onInputChange={props.onInputChange}
+            onInputChange={onInputChange}
+            onPressEscape={onPressEscape}
           />
           />
         </div>
         </div>
         <div className={`${buttonStyle} col-4 row`}>
         <div className={`${buttonStyle} col-4 row`}>

+ 27 - 5
apps/app/src/components/PageHeader/PageTitleHeader.tsx

@@ -18,6 +18,9 @@ export const PageTitleHeader: FC<Props> = (props) => {
   const pageTitle = nodePath.basename(currentPagePath) || '/';
   const pageTitle = nodePath.basename(currentPagePath) || '/';
 
 
   const [isRenameInputShown, setRenameInputShown] = useState(false);
   const [isRenameInputShown, setRenameInputShown] = useState(false);
+  const [editedPagePath, setEditedPagePath] = useState(currentPagePath);
+
+  const editedPageTitle = nodePath.basename(editedPagePath);
 
 
   const onRenameFinish = () => {
   const onRenameFinish = () => {
     setRenameInputShown(false);
     setRenameInputShown(false);
@@ -27,16 +30,34 @@ export const PageTitleHeader: FC<Props> = (props) => {
     setRenameInputShown(true);
     setRenameInputShown(true);
   };
   };
 
 
-  const pagePathRenameHandler = usePagePathRenameHandler(currentPage, onRenameFinish, onRenameFailure);
+  const onInputChange = useCallback((inputText: string) => {
+    const parentPath = pathUtils.addTrailingSlash(nodePath.dirname(currentPage?.path ?? ''));
+    const newPagePath = nodePath.resolve(parentPath, inputText);
+
+    setEditedPagePath(newPagePath);
+  }, [currentPage?.path, setEditedPagePath]);
+
+  const onPressEscape = () => {
+    setEditedPagePath(currentPagePath);
+    setRenameInputShown(false);
+  };
+
+  const onClickPageTitle = () => {
+    console.log(currentPagePath);
+    setEditedPagePath(currentPagePath);
+    setRenameInputShown(true);
+  };
+
+  const pagePathRenameHandler = usePagePathRenameHandler(currentPage);
 
 
   const stateHandler = { isRenameInputShown, setRenameInputShown };
   const stateHandler = { isRenameInputShown, setRenameInputShown };
 
 
-  const PageTitle = useMemo(() => (<div onClick={() => setRenameInputShown(true)}>{pageTitle}</div>), [pageTitle]);
+  const PageTitle = useMemo(() => (<div onClick={onClickPageTitle}>{pageTitle}</div>), [pageTitle]);
 
 
   const buttonStyle = isRenameInputShown ? '' : 'd-none';
   const buttonStyle = isRenameInputShown ? '' : 'd-none';
 
 
   const onClickButton = () => {
   const onClickButton = () => {
-    pagePathRenameHandler(props.inputValue);
+    pagePathRenameHandler(editedPagePath, onRenameFinish, onRenameFailure);
   };
   };
 
 
   return (
   return (
@@ -45,9 +66,10 @@ export const PageTitleHeader: FC<Props> = (props) => {
         <TextInputForPageTitleAndPath
         <TextInputForPageTitleAndPath
           currentPage={currentPage}
           currentPage={currentPage}
           stateHandler={stateHandler}
           stateHandler={stateHandler}
-          inputValue={props.inputValue}
+          inputValue={editedPageTitle}
           CustomComponent={PageTitle}
           CustomComponent={PageTitle}
-          onInputChange={props.onInputChange}
+          onInputChange={onInputChange}
+          onPressEscape={onPressEscape}
         />
         />
       </div>
       </div>
       <div className={`col-4 ${buttonStyle}`}>
       <div className={`col-4 ${buttonStyle}`}>

+ 3 - 0
apps/app/src/components/PageHeader/page-header-utils.ts

@@ -35,6 +35,9 @@ export const usePagePathRenameHandler = (
       return;
       return;
     }
     }
 
 
+    console.log(newPagePath);
+    console.log(currentPagePath);
+
     try {
     try {
       await apiv3Put('/pages/rename', {
       await apiv3Put('/pages/rename', {
         pageId: currentPage._id,
         pageId: currentPage._id,