Explorar el Código

set isEnabledShowUnsavedWarning

kaori hace 3 años
padre
commit
35e71a418f

+ 6 - 1
packages/app/src/client/util/editor.ts

@@ -25,7 +25,12 @@ export const getOptionsToSave = (
   };
 };
 
+export const isEnabledShowUnsavedWarning = (bool = false) => {
+  return bool;
+}
 
 export const showAlertDialog = (msg): void => {
-  return window.alert(msg);
+  if(isEnabledShowUnsavedWarning()){
+    return window.alert(msg);
+  }
 };

+ 3 - 3
packages/app/src/components/Page.jsx

@@ -2,7 +2,7 @@ import React, { useEffect, useRef } from 'react';
 
 import PropTypes from 'prop-types';
 
-
+import { isEnabledShowUnsavedWarning } from '~/client/util/editor';
 import MarkdownTable from '~/client/models/MarkdownTable';
 import AppContainer from '~/client/services/AppContainer';
 import EditorContainer from '~/client/services/EditorContainer';
@@ -89,7 +89,7 @@ class Page extends React.Component {
 
     try {
       // disable unsaved warning
-      editorContainer.disableUnsavedWarning();
+      isEnabledShowUnsavedWarning(false)
 
       // eslint-disable-next-line no-unused-vars
       const { page, tags } = await pageContainer.save(newMarkdown, this.props.editorMode, optionsToSave);
@@ -121,7 +121,7 @@ class Page extends React.Component {
 
     try {
       // disable unsaved warning
-      editorContainer.disableUnsavedWarning();
+      isEnabledShowUnsavedWarning(false);
 
       // eslint-disable-next-line no-unused-vars
       const { page, tags } = await pageContainer.save(newMarkdown, this.props.editorMode, optionsToSave);

+ 5 - 2
packages/app/src/components/PageEditor.tsx

@@ -2,6 +2,8 @@ import React, {
   useCallback, useEffect, useMemo, useRef, useState,
 } from 'react';
 
+import { isEnabledShowUnsavedWarning } from '~/client/util/editor';
+
 import EventEmitter from 'events';
 
 import { envUtils } from '@growi/core';
@@ -129,7 +131,7 @@ const PageEditor = (props: Props): JSX.Element => {
 
     try {
       // disable unsaved warning
-      editorContainer.disableUnsavedWarning();
+      isEnabledShowUnsavedWarning(false)
 
       // eslint-disable-next-line no-unused-vars
       const { tags } = await pageContainer.save(markdown, editorMode, optionsToSave);
@@ -355,7 +357,8 @@ const PageEditor = (props: Props): JSX.Element => {
   useEffect(() => {
     // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
     if (pageContainer.state.markdown! !== markdown) {
-      editorContainer.enableUnsavedWarning();
+      // editorContainer.enableUnsavedWarning();
+      isEnabledShowUnsavedWarning(true);
     }
   }, [editorContainer, markdown, pageContainer.state.markdown]);
 

+ 4 - 3
packages/app/src/components/PageEditorByHackmd.jsx

@@ -3,7 +3,7 @@ import React from 'react';
 import PropTypes from 'prop-types';
 import { useTranslation } from 'react-i18next';
 
-
+import { isEnabledShowUnsavedWarning } from '~/client/util/editor';
 import AppContainer from '~/client/services/AppContainer';
 import EditorContainer from '~/client/services/EditorContainer';
 import PageContainer from '~/client/services/PageContainer';
@@ -178,7 +178,7 @@ class PageEditorByHackmd extends React.Component {
 
     try {
       // disable unsaved warning
-      editorContainer.disableUnsavedWarning();
+      isEnabledShowUnsavedWarning(false)
 
       // eslint-disable-next-line no-unused-vars
       const { page, tags } = await pageContainer.save(markdown, this.props.editorMode, optionsToSave);
@@ -213,7 +213,8 @@ class PageEditorByHackmd extends React.Component {
     }
 
     // enable unsaved warning
-    editorContainer.enableUnsavedWarning();
+    // editorContainer.enableUnsavedWarning();
+    isEnabledShowUnsavedWarning(true);
 
     const params = {
       pageId: pageContainer.state.pageId,

+ 3 - 2
packages/app/src/components/SavePageControls.jsx

@@ -12,6 +12,7 @@ import AppContainer from '~/client/services/AppContainer';
 import EditorContainer from '~/client/services/EditorContainer';
 import PageContainer from '~/client/services/PageContainer';
 import { getOptionsToSave } from '~/client/util/editor';
+import { isEnabledShowUnsavedWarning } from '~/client/util/editor';
 
 // TODO: remove this when omitting unstated is completed
 import { useIsEditable, useCurrentPageId } from '~/stores/context';
@@ -54,7 +55,7 @@ class SavePageControls extends React.Component {
       isSlackEnabled, slackChannels, grant, grantGroupId, grantGroupName, pageContainer, editorContainer, pageTags,
     } = this.props;
     // disable unsaved warning
-    editorContainer.disableUnsavedWarning();
+    isEnabledShowUnsavedWarning(false)
 
     try {
       // save
@@ -80,7 +81,7 @@ class SavePageControls extends React.Component {
       isSlackEnabled, slackChannels, grant, grantGroupId, grantGroupName, pageContainer, editorContainer, pageTags,
     } = this.props;
     // disable unsaved warning
-    editorContainer.disableUnsavedWarning();
+    isEnabledShowUnsavedWarning(false)
     // save
     const currentOptionsToSave = getOptionsToSave(isSlackEnabled, slackChannels, grant, grantGroupId, grantGroupName, pageTags);
     const optionsToSave = Object.assign(currentOptionsToSave, {

+ 3 - 3
packages/app/src/pages/[[...path]].page.tsx

@@ -1,7 +1,7 @@
 import React, { useEffect, useCallback } from 'react';
 
 import { pagePathUtils } from '@growi/core';
-import { showAlertDialog } from '~/client/util/editor';
+import { isEnabledShowUnsavedWarning, showAlertDialog } from '~/client/util/editor';
 import { isValidObjectId } from 'mongoose';
 import {
   NextPage, GetServerSideProps, GetServerSidePropsContext,
@@ -168,7 +168,7 @@ const GrowiPage: NextPage<Props> = (props: Props) => {
     showAlertDialog(unsavedAlertMsg);
     e.returnValue = '';
     return;
-  },[]);
+  },[isEnabledShowUnsavedWarning]);
 
   /*
   *  Route changes by Browser
@@ -179,7 +179,7 @@ const GrowiPage: NextPage<Props> = (props: Props) => {
     return () => {
       window.removeEventListener('beforeunload', showAlertDialogForRouteChangesByBrowser);
     };
-  }, []);
+  }, [isEnabledShowUnsavedWarning]);
 
 
   /*

+ 4 - 0
packages/app/src/stores/editor.tsx

@@ -108,3 +108,7 @@ export const usePageTagsForEditors = (pageId: Nullable<string>): SWRResponse<str
     },
   };
 };
+
+export const useIsEnabledShowUnsavedWarning = (bool): SWRResponse<boolean, Error> => {
+  return useStaticSWR<boolean, Error>('isEnabledShowUnsavedWarning', undefined, {fallbackData: false});
+};