mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2026-11-04 20:29:01 +00:00
Merge pull request #5098 from naturalcrit/new/edit/home-more-cleanup
New/Edit/Home Make "brewToSave" prep (patch/hash/pageCount) common
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import './editPage.less';
|
||||
|
||||
// Common imports
|
||||
import React, { useState, useEffect, useRef, useEffectEvent } from 'react';
|
||||
import React, { useState } from 'react';
|
||||
import request from '../../utils/request-middleware.js';
|
||||
|
||||
import _ from 'lodash';
|
||||
@@ -13,9 +13,7 @@ import useCommonEditPageFunctions from '../../utils/commonEditPageFunctions.jsx'
|
||||
import Nav from '@navbar/nav.jsx';
|
||||
|
||||
// Page specific imports
|
||||
import { md5 } from 'hash-wasm';
|
||||
import { gzipSync, strToU8 } from 'fflate';
|
||||
import { makePatches, stringifyPatches } from '@sanity/diff-match-patch';
|
||||
|
||||
import { updateHistory, versionHistoryGarbageCollection } from '../../utils/versionHistory.js';
|
||||
import googleDriveIcon from '../../googleDrive.svg';
|
||||
@@ -40,8 +38,6 @@ const EditPage = (props)=>{
|
||||
const [alertOwnershipToTransfer, setAlertOwnershipToTransfer] = useState(false);
|
||||
const [confirmGoogleTransfer, setConfirmGoogleTransfer] = useState(false);
|
||||
|
||||
const lastSavedBrew = useRef(_.cloneDeep(props.brew));
|
||||
|
||||
const handleGoogleClick = ()=>{
|
||||
if(currentBrew.authors.length > 0 && global.account?.username !== currentBrew.authors[0]) {
|
||||
setAlertOwnershipToTransfer(true);
|
||||
@@ -76,23 +72,12 @@ const EditPage = (props)=>{
|
||||
await updateHistory(brew).catch(console.error);
|
||||
await versionHistoryGarbageCollection().catch(console.error);
|
||||
|
||||
//Prepare content to send to server
|
||||
const brewToSave = {
|
||||
...brew,
|
||||
text : brew.text.normalize('NFC'),
|
||||
pageCount : ((brew.renderer === 'legacy' ? brew.text.match(/\\page/g) : brew.text.match(/^(?=\\page(?:break)?(?: *{[^\n{}]*})?$)/gm)) || []).length + 1,
|
||||
patches : stringifyPatches(makePatches(encodeURI(lastSavedBrew.current.text.normalize('NFC')), encodeURI(brew.text.normalize('NFC')))),
|
||||
hash : await md5(lastSavedBrew.current.text.normalize('NFC')),
|
||||
textBin : undefined,
|
||||
version : lastSavedBrew.current.version
|
||||
};
|
||||
|
||||
const compressedBrew = gzipSync(strToU8(JSON.stringify(brewToSave)));
|
||||
const compressedBrew = gzipSync(strToU8(JSON.stringify(brew)));
|
||||
const transfer = saveToGoogle === _.isNil(brew.googleId);
|
||||
const params = transfer ? `?${saveToGoogle ? 'saveToGoogle' : 'removeFromGoogle'}=true` : '';
|
||||
|
||||
const res = await request
|
||||
.put(`/api/update/${brewToSave.editId}${params}`)
|
||||
.put(`/api/update/${brew.editId}${params}`)
|
||||
.set('Content-Encoding', 'gzip')
|
||||
.set('Content-Type', 'application/json')
|
||||
.send(compressedBrew)
|
||||
@@ -173,7 +158,6 @@ const EditPage = (props)=>{
|
||||
useLocalStorage,
|
||||
sandbox,
|
||||
showFloatingButtons,
|
||||
lastSavedBrew,
|
||||
save,
|
||||
onSaveSuccess,
|
||||
pageName,
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import './homePage.less';
|
||||
|
||||
// Common imports
|
||||
import React, { useState, useEffect, useRef, useEffectEvent } from 'react';
|
||||
import React, { useState } from 'react';
|
||||
import request from '../../utils/request-middleware.js';
|
||||
import _ from 'lodash';
|
||||
|
||||
@@ -26,8 +26,6 @@ const HomePage =(props)=>{
|
||||
const [saveGoogle, setSaveGoogle] = useState(global.account?.googleId ? true : false);
|
||||
const [error, setError] = useState(undefined);
|
||||
|
||||
const lastSavedBrew = useRef(_.cloneDeep(props.brew));
|
||||
|
||||
const save = async (brew, saveToGoogle)=>{
|
||||
const res = await request
|
||||
.post(`/api${saveGoogle ? '?saveToGoogle=true' : ''}`)
|
||||
@@ -58,7 +56,6 @@ const HomePage =(props)=>{
|
||||
useLocalStorage,
|
||||
sandbox,
|
||||
showFloatingButtons,
|
||||
lastSavedBrew,
|
||||
save,
|
||||
onSaveSuccess,
|
||||
pageName,
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import './newPage.less';
|
||||
|
||||
// Common imports
|
||||
import React, { useState, useEffect, useRef, useEffectEvent } from 'react';
|
||||
import React, { useState } from 'react';
|
||||
import request from '../../utils/request-middleware.js';
|
||||
import _ from 'lodash';
|
||||
|
||||
@@ -26,20 +26,10 @@ const NewPage = (props)=>{
|
||||
const [saveGoogle, setSaveGoogle] = useState(global.account?.googleId ? true : false);
|
||||
const [error, setError] = useState(null);
|
||||
|
||||
const lastSavedBrew = useRef(_.cloneDeep(props.brew));
|
||||
|
||||
const save = async (brew, saveToGoogle)=>{
|
||||
//Prepare content to send to server
|
||||
const brewToSave = {
|
||||
...brew,
|
||||
text : brew.text.normalize('NFC'),
|
||||
pageCount : ((brew.renderer === 'legacy' ? brew.text.match(/\\page/g) : brew.text.match(/^(?=\\page(?:break)?(?: *{[^\n{}]*})?$)/gm)) || []).length + 1,
|
||||
textBin : undefined
|
||||
};
|
||||
|
||||
const res = await request
|
||||
.post(`/api${saveGoogle ? '?saveToGoogle=true' : ''}`)
|
||||
.send(brewToSave)
|
||||
.send(brew)
|
||||
.catch((err)=>{
|
||||
console.error('Error Updating Local Brew');
|
||||
setError(err);
|
||||
@@ -66,7 +56,6 @@ const NewPage = (props)=>{
|
||||
useLocalStorage,
|
||||
sandbox,
|
||||
showFloatingButtons,
|
||||
lastSavedBrew,
|
||||
save,
|
||||
onSaveSuccess,
|
||||
pageName,
|
||||
|
||||
@@ -2,6 +2,8 @@ import React, { useState, useEffect, useEffectEvent, useRef } from 'react';
|
||||
import { printCurrentBrew, fetchThemeBundle } from '@shared/helpers.js';
|
||||
import _ from 'lodash';
|
||||
import { hbfm } from 'marked-hbfm';
|
||||
import { md5 } from 'hash-wasm';
|
||||
import { makePatches, stringifyPatches } from '@sanity/diff-match-patch';
|
||||
|
||||
import Headtags from '@vitreum/headtags.js';
|
||||
import SplitPane from '@components/splitPane/splitPane.jsx';
|
||||
@@ -45,7 +47,6 @@ export default function useCommonEditPageFunctions(dependencies) {
|
||||
useLocalStorage,
|
||||
sandbox,
|
||||
showFloatingButtons,
|
||||
lastSavedBrew,
|
||||
save,
|
||||
onSaveSuccess,
|
||||
pageName,
|
||||
@@ -68,6 +69,7 @@ export default function useCommonEditPageFunctions(dependencies) {
|
||||
|
||||
const unsavedChangesRef = useRef(unsavedChanges); // onBeforeUnload lives outside React and needs ref to unsavedChanges
|
||||
const warnUnsavedTimeout = useRef(null); // timers live outside React and need ref to consistently track time
|
||||
const lastSavedBrew = useRef(_.cloneDeep(currentBrew));
|
||||
const saveTimeout = useRef(null);
|
||||
const editorRef = useRef(null);
|
||||
|
||||
@@ -202,12 +204,24 @@ export default function useCommonEditPageFunctions(dependencies) {
|
||||
setIsSaving(true);
|
||||
setError(null);
|
||||
setHTMLErrors(hbfm.validate(currentBrew.text));
|
||||
const brewToSave = currentBrew;
|
||||
let savedBrew = await save(brewToSave, saveToGoogle)
|
||||
|
||||
//Prepare content to send to server
|
||||
const snapshotBrewBeforeSave = currentBrew;
|
||||
const brewToSave = {
|
||||
...currentBrew,
|
||||
text : currentBrew.text.normalize('NFC'),
|
||||
pageCount : ((currentBrew.renderer === 'legacy' ? currentBrew.text.match(/\\page/g) : currentBrew.text.match(/^(?=\\page(?:break)?(?: *{[^\n{}]*})?$)/gm)) || []).length + 1,
|
||||
patches : stringifyPatches(makePatches(encodeURI(lastSavedBrew.current.text.normalize('NFC')), encodeURI(currentBrew.text.normalize('NFC')))),
|
||||
hash : await md5(lastSavedBrew.current.text.normalize('NFC')),
|
||||
textBin : undefined,
|
||||
version : lastSavedBrew.current.version
|
||||
};
|
||||
|
||||
const savedBrew = await save(brewToSave, saveToGoogle)
|
||||
.catch((err)=>{setError(err);});
|
||||
if(savedBrew) {
|
||||
lastSavedBrew.current = {
|
||||
...brewToSave,
|
||||
...snapshotBrewBeforeSave,
|
||||
...savedBrew
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user