Просмотр исходного кода

declear createPage, updatePage, saveAndReload on page-operation-block

kaori 3 лет назад
Родитель
Сommit
f16850ddc5

+ 3 - 3
packages/app/src/client/services/PageContainer.js

@@ -7,9 +7,6 @@ import { Container } from 'unstated';
 import { EditorMode } from '~/stores/ui';
 import { EditorMode } from '~/stores/ui';
 import loggerFactory from '~/utils/logger';
 import loggerFactory from '~/utils/logger';
 
 
-import { toastError } from '../util/apiNotification';
-import { apiPost } from '../util/apiv1-client';
-import { apiv3Post } from '../util/apiv3-client';
 import {
 import {
   DetachCodeBlockInterceptor,
   DetachCodeBlockInterceptor,
   RestoreCodeBlockInterceptor,
   RestoreCodeBlockInterceptor,
@@ -17,6 +14,9 @@ import {
 import {
 import {
   DrawioInterceptor,
   DrawioInterceptor,
 } from '../../services/renderer/interceptor/drawio-interceptor';
 } from '../../services/renderer/interceptor/drawio-interceptor';
+import { toastError } from '../util/apiNotification';
+import { apiPost } from '../util/apiv1-client';
+import { apiv3Post } from '../util/apiv3-client';
 
 
 const { isTrashPage } = pagePathUtils;
 const { isTrashPage } = pagePathUtils;
 
 

+ 84 - 0
packages/app/src/client/services/page-operation.ts

@@ -2,8 +2,12 @@ import { SubscriptionStatusType } from '@growi/core';
 import urljoin from 'url-join';
 import urljoin from 'url-join';
 
 
 import { toastError } from '../util/apiNotification';
 import { toastError } from '../util/apiNotification';
+import { apiPost } from '../util/apiv1-client';
 import { apiv3Post, apiv3Put } from '../util/apiv3-client';
 import { apiv3Post, apiv3Put } from '../util/apiv3-client';
+import { EditorMode } from '~/stores/ui';
 
 
+import loggerFactory from '~/utils/logger';
+const logger = loggerFactory('growi:services:page-operation');
 
 
 export const toggleSubscribe = async(pageId: string, currentStatus: SubscriptionStatusType | undefined): Promise<void> => {
 export const toggleSubscribe = async(pageId: string, currentStatus: SubscriptionStatusType | undefined): Promise<void> => {
   try {
   try {
@@ -90,3 +94,83 @@ export const exportAsMarkdown = (pageId: string, revisionId: string, format: str
 export const resumeRenameOperation = async(pageId: string): Promise<void> => {
 export const resumeRenameOperation = async(pageId: string): Promise<void> => {
   await apiv3Post('/pages/resume-rename', { pageId });
   await apiv3Post('/pages/resume-rename', { pageId });
 };
 };
+
+
+
+const createPage = async(pagePath, markdown, tmpParams) => {
+  // clone
+  const params = Object.assign(tmpParams, {
+    path: pagePath,
+    body: markdown,
+  });
+
+  const res = await apiv3Post('/pages/', params);
+  const { page, tags, revision } = res.data;
+
+  return { page, tags, revision };
+}
+
+const updatePage = async(pageId, revisionId, markdown, tmpParams) => {
+  // clone
+  const params = Object.assign(tmpParams, {
+    page_id: pageId,
+    revision_id: revisionId,
+    body: markdown,
+  });
+
+  const res: any = await apiPost('/pages.update', params);
+  if (!res.ok) {
+    throw new Error(res.error);
+  }
+  return res;
+}
+
+
+
+
+export const saveAndReload = async(optionsToSave, editorMode, pageInfo) => {
+  if (optionsToSave == null) {
+    const msg = '\'saveAndReload\' requires the \'optionsToSave\' param';
+    throw new Error(msg);
+  }
+
+  if (editorMode == null) {
+    logger.warn('\'saveAndReload\' requires the \'editorMode\' param');
+    return;
+  }
+
+  const { pageId, path, revisionId } = pageInfo;
+  // const { pageId, path } = this.state;
+  // let { revisionId } = this.state;
+
+  const options = Object.assign({}, optionsToSave);
+
+  let markdown;
+  if (editorMode === EditorMode.HackMD) {
+    // const pageEditorByHackmd = this.appContainer.getComponentInstance('PageEditorByHackmd');
+    // markdown = await pageEditorByHackmd.getMarkdown();
+    // // set option to sync
+    // options.isSyncRevisionToHackmd = true;
+    // revisionId = this.state.revisionIdHackmdSynced;
+  }
+  else {
+    // const pageEditor = this.appContainer.getComponentInstance('PageEditor');
+    // markdown = pageEditor.getMarkdown();
+  }
+
+  let res;
+  if (pageId == null) {
+    res = await createPage(path, markdown, options);
+  }
+  else {
+    res = await updatePage(pageId, revisionId, markdown, options);
+  }
+
+  // const editorContainer = this.appContainer.getContainer('EditorContainer');
+  // editorContainer.clearDraft(path);
+  window.location.href = path;
+
+  return res;
+}
+
+

+ 8 - 8
packages/app/src/components/PageEditor.tsx

@@ -317,14 +317,14 @@ const PageEditor = (props: Props): JSX.Element => {
 
 
 
 
   // register dummy instance to get markdown
   // register dummy instance to get markdown
-  // useEffect(() => {
-  //   const pageEditorInstance = {
-  //     getMarkdown: () => {
-  //       return markdown;
-  //     },
-  //   };
-  //   appContainer.registerComponentInstance('PageEditor', pageEditorInstance);
-  // }, [appContainer, markdown]);
+  useEffect(() => {
+    const pageEditorInstance = {
+      getMarkdown: () => {
+        return markdown;
+      },
+    };
+    // appContainer.registerComponentInstance('PageEditor', pageEditorInstance);
+  }, [markdown]);
 
 
   // initial caret line
   // initial caret line
   useEffect(() => {
   useEffect(() => {

+ 0 - 90
packages/app/src/stores/page.tsx

@@ -140,93 +140,3 @@ export const useSWRxApplicableGrant = (
     (endpoint, pageId) => apiv3Get(endpoint, { pageId }).then(response => response.data),
     (endpoint, pageId) => apiv3Get(endpoint, { pageId }).then(response => response.data),
   );
   );
 };
 };
-
-export type IPageOperation = {
-  createPage: (pagePath, markdown, tmpParams) => Promise<any>
-  updatePage: (pageId, revisionId, markdown, tmpParams) => Promise<any>
-  saveAndReload: (optionsToSave, editorMode) => Promise<any>
-}
-
-export const useSWRxPageOperation = (pageId: string, path: string, revisionId: string): IPageOperation => {
-
-  const createPage = async(pagePath, markdown, tmpParams) => {
-    // const socketIoContainer = this.appContainer.getContainer('SocketIoContainer');
-
-    // clone
-    const params = Object.assign(tmpParams, {
-      path: pagePath,
-      body: markdown,
-    });
-
-    const res = await apiv3Post('/pages/', params);
-    const { page, tags, revision } = res.data;
-
-    return { page, tags, revision };
-  };
-
-  const updatePage = async(pageId, revisionId, markdown, tmpParams): Promise<any> => {
-    // const socketIoContainer = this.appContainer.getContainer('SocketIoContainer');
-
-    // clone
-    const params = Object.assign(tmpParams, {
-      page_id: pageId,
-      revision_id: revisionId,
-      body: markdown,
-    });
-
-    const res: any = await apiPost('/pages.update', params);
-    if (!res.ok) {
-      throw new Error(res.error);
-    }
-    return res;
-  };
-
-
-  const saveAndReload = async(optionsToSave, editorMode): Promise<void> => {
-    if (optionsToSave == null) {
-      const msg = '\'saveAndReload\' requires the \'optionsToSave\' param';
-      throw new Error(msg);
-    }
-
-    if (editorMode == null) {
-      logger.warn('\'saveAndReload\' requires the \'editorMode\' param');
-      return;
-    }
-
-    const options = Object.assign({}, optionsToSave);
-
-    let markdown;
-    // if (editorMode === EditorMode.HackMD) {
-    //   const pageEditorByHackmd = this.appContainer.getComponentInstance('PageEditorByHackmd');
-    //   markdown = await pageEditorByHackmd.getMarkdown();
-    //   // set option to sync
-    //   options.isSyncRevisionToHackmd = true;
-    //   revisionId = this.state.revisionIdHackmdSynced;
-    // }
-    // else {
-    // const pageEditor = this.appContainer.getComponentInstance('PageEditor');
-    // markdown = pageEditor.getMarkdown();
-    // }
-
-    let res;
-    if (pageId == null) {
-      res = await createPage(path, markdown, options);
-    }
-    else {
-      res = await updatePage(pageId, revisionId, markdown, options);
-    }
-
-    // const editorContainer = this.appContainer.getContainer('EditorContainer');
-    // editorContainer.clearDraft(path);
-    window.location.href = path;
-
-    return res;
-  };
-
-
-  return {
-    createPage,
-    updatePage,
-    saveAndReload,
-  };
-};