diff --git a/client/homebrew/pages/editPage/editPage.jsx b/client/homebrew/pages/editPage/editPage.jsx index f8c99f531..088f13765 100644 --- a/client/homebrew/pages/editPage/editPage.jsx +++ b/client/homebrew/pages/editPage/editPage.jsx @@ -8,7 +8,6 @@ import { hbfm } from 'hbmarkedwrapper'; import _ from 'lodash'; import { DEFAULT_BREW_LOAD } from '../../../../server/brewDefaults.js'; -import { printCurrentBrew, fetchThemeBundle } from '@shared/helpers.js'; import useCommonEditPageFunctions from '../../utils/commonEditPageFunctions.js' @@ -43,7 +42,6 @@ const SAVE_TIMEOUT = 10000; const UNSAVED_WARNING_TIMEOUT = 900000; //Warn user afer 15 minutes of unsaved changes const UNSAVED_WARNING_POPUP_TIMEOUT = 4000; //Show the warning for 4 seconds -const AUTOSAVE_KEY = 'HB_editor_autoSaveOn'; const BREWKEY = 'HB_newPage_content'; const STYLEKEY = 'HB_newPage_style'; const SNIPKEY = 'HB_newPage_snippets'; @@ -90,14 +88,21 @@ const EditPage = (props)=>{ setThemeBundle, HTMLErrors, setHTMLErrors, + currentBrew, setCurrentBrew, useLocalStorage, BREWKEY, STYLEKEY, SNIPKEY, METAKEY, - fetchThemeBundle, - hbfm + hbfm, + autoSaveEnabled, + setAutoSaveEnabled, + setWarnUnsavedChanges, + trySaveRef, + unsavedChangesRef, + sandbox, + saveGoogle }); useEffect(()=>{ diff --git a/client/homebrew/pages/homePage/homePage.jsx b/client/homebrew/pages/homePage/homePage.jsx index b15787d48..29e5b70e2 100644 --- a/client/homebrew/pages/homePage/homePage.jsx +++ b/client/homebrew/pages/homePage/homePage.jsx @@ -8,7 +8,6 @@ import { hbfm } from 'hbmarkedwrapper'; import _ from 'lodash'; import { DEFAULT_BREW } from '../../../../server/brewDefaults.js'; -import { printCurrentBrew, fetchThemeBundle } from '@shared/helpers.js'; import useCommonEditPageFunctions from '../../utils/commonEditPageFunctions.js' @@ -36,7 +35,6 @@ const SAVE_TIMEOUT = 10000; const UNSAVED_WARNING_TIMEOUT = 900000; //Warn user afer 15 minutes of unsaved changes const UNSAVED_WARNING_POPUP_TIMEOUT = 4000; //Show the warning for 4 seconds -const AUTOSAVE_KEY = 'HB_editor_autoSaveOn'; const BREWKEY = 'HB_newPage_content'; const STYLEKEY = 'HB_newPage_style'; const SNIPKEY = 'HB_newPage_snippets'; @@ -67,6 +65,7 @@ const HomePage =(props)=>{ const editorRef = useRef(null); const lastSavedBrew = useRef(_.cloneDeep(props.brew)); const warnUnsavedTimeout = useRef(null); + const trySaveRef = useRef(null); // CTRL+S listener lives outside React and needs ref to use trySave with latest copy of brew const unsavedChangesRef = useRef(unsavedChanges); const { @@ -76,14 +75,20 @@ const HomePage =(props)=>{ setThemeBundle, HTMLErrors, setHTMLErrors, + currentBrew, setCurrentBrew, useLocalStorage, BREWKEY, STYLEKEY, SNIPKEY, METAKEY, - fetchThemeBundle, - hbfm + hbfm, + autoSaveEnabled, + setAutoSaveEnabled, + setWarnUnsavedChanges, + trySaveRef, + unsavedChangesRef, + sandbox }); useEffect(()=>{ diff --git a/client/homebrew/pages/newPage/newPage.jsx b/client/homebrew/pages/newPage/newPage.jsx index e4f30e7d5..3208af496 100644 --- a/client/homebrew/pages/newPage/newPage.jsx +++ b/client/homebrew/pages/newPage/newPage.jsx @@ -32,7 +32,6 @@ const SAVE_TIMEOUT = 10000; const UNSAVED_WARNING_TIMEOUT = 900000; //Warn user afer 15 minutes of unsaved changes const UNSAVED_WARNING_POPUP_TIMEOUT = 4000; //Show the warning for 4 seconds -const AUTOSAVE_KEY = 'HB_editor_autoSaveOn'; const BREWKEY = 'HB_newPage_content'; const STYLEKEY = 'HB_newPage_style'; const SNIPKEY = 'HB_newPage_snippets'; @@ -81,14 +80,20 @@ const NewPage = (props)=>{ setThemeBundle, HTMLErrors, setHTMLErrors, + currentBrew, setCurrentBrew, useLocalStorage, BREWKEY, STYLEKEY, SNIPKEY, METAKEY, - fetchThemeBundle, - hbfm + hbfm, + autoSaveEnabled, + setAutoSaveEnabled, + setWarnUnsavedChanges, + trySaveRef, + unsavedChangesRef, + sandbox }); const loadBrew = ()=>{ diff --git a/client/homebrew/utils/commonEditPageFunctions.js b/client/homebrew/utils/commonEditPageFunctions.js index 4536cc1c5..10327bf2a 100644 --- a/client/homebrew/utils/commonEditPageFunctions.js +++ b/client/homebrew/utils/commonEditPageFunctions.js @@ -1,23 +1,36 @@ +import React, { useState, useEffect, useRef } from 'react'; +import { printCurrentBrew, fetchThemeBundle } from '@shared/helpers.js'; + +const AUTOSAVE_KEY = 'HB_editor_autoSaveOn'; + export default function useCommonEditPageFunctions(dependencies) { const { setError, setThemeBundle, HTMLErrors, setHTMLErrors, + currentBrew, setCurrentBrew, useLocalStorage, BREWKEY, STYLEKEY, SNIPKEY, METAKEY, - fetchThemeBundle, - hbfm + hbfm, + autoSaveEnabled, + setAutoSaveEnabled, + setWarnUnsavedChanges, + trySaveRef, + sandbox, + saveGoogle = false, + unsavedChangesRef } = dependencies; //==--------- Page setup ----------==// useEffect(()=>{ - const autoSavePref = JSON.parse(localStorage.getItem(AUTOSAVE_KEY) ?? true); + const autoSavePref = !sandbox && JSON.parse(localStorage.getItem(AUTOSAVE_KEY) ?? true); setAutoSaveEnabled(autoSavePref); + console.log(autoSavePref) setWarnUnsavedChanges(!autoSavePref); setHTMLErrors(hbfm.validate(currentBrew.text)); fetchThemeBundle(setError, setThemeBundle, currentBrew.renderer, currentBrew.theme);