Use less sessionStorage for editor state

This commit is contained in:
Gregory Schier
2025-01-20 14:56:25 -08:00
parent 2a2fe700b4
commit c58bfeb109
4 changed files with 35 additions and 11 deletions

9
package-lock.json generated
View File

@@ -8475,6 +8475,12 @@
"integrity": "sha512-HvdH2LzI/EAZcUwA8+0nKNtWHqS+ZmijLA30RwZA0bo7ToCckjK5MkGhjED9KoRcXO6BaGI3I9UIzSA1FKFPOQ==", "integrity": "sha512-HvdH2LzI/EAZcUwA8+0nKNtWHqS+ZmijLA30RwZA0bo7ToCckjK5MkGhjED9KoRcXO6BaGI3I9UIzSA1FKFPOQ==",
"license": "MIT" "license": "MIT"
}, },
"node_modules/js-md5": {
"version": "0.8.3",
"resolved": "https://registry.npmjs.org/js-md5/-/js-md5-0.8.3.tgz",
"integrity": "sha512-qR0HB5uP6wCuRMrWPTrkMaev7MJZwJuuw4fnwAzRgP4J4/F8RwtodOKpGp4XpqsLBFzzgqIO42efFAyz2Et6KQ==",
"license": "MIT"
},
"node_modules/js-tokens": { "node_modules/js-tokens": {
"version": "4.0.0", "version": "4.0.0",
"resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-4.0.0.tgz", "resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-4.0.0.tgz",
@@ -15413,7 +15419,7 @@
}, },
"packages/plugin-runtime-types": { "packages/plugin-runtime-types": {
"name": "@yaakapp/api", "name": "@yaakapp/api",
"version": "0.3.3", "version": "0.3.4",
"dependencies": { "dependencies": {
"@types/node": "^22.5.4" "@types/node": "^22.5.4"
}, },
@@ -15573,6 +15579,7 @@
"fuzzbunny": "^1.0.1", "fuzzbunny": "^1.0.1",
"history": "^5.3.0", "history": "^5.3.0",
"jotai": "^2.9.3", "jotai": "^2.9.3",
"js-md5": "^0.8.3",
"lucide-react": "^0.439.0", "lucide-react": "^0.439.0",
"mime": "^4.0.4", "mime": "^4.0.4",
"nanoid": "^5.0.9", "nanoid": "^5.0.9",

View File

@@ -10,6 +10,7 @@ import type { EditorKeymap, EnvironmentVariable } from '@yaakapp-internal/models
import type { EditorLanguage, TemplateFunction } from '@yaakapp-internal/plugins'; import type { EditorLanguage, TemplateFunction } from '@yaakapp-internal/plugins';
import classNames from 'classnames'; import classNames from 'classnames';
import { EditorView } from 'codemirror'; import { EditorView } from 'codemirror';
import { md5 } from 'js-md5';
import type { MutableRefObject, ReactNode } from 'react'; import type { MutableRefObject, ReactNode } from 'react';
import { import {
Children, Children,
@@ -583,7 +584,7 @@ const placeholderElFromText = (text: string, type: EditorProps['type']) => {
const el = document.createElement('div'); const el = document.createElement('div');
if (type === 'password') { if (type === 'password') {
// Will be obscured (dots) so just needs to be something to take up space // Will be obscured (dots) so just needs to be something to take up space
el.innerHTML = 'aaaaaaaaaa'; el.innerHTML = 'something-cool';
el.setAttribute('aria-hidden', 'true'); el.setAttribute('aria-hidden', 'true');
} else { } else {
el.innerHTML = text ? text.replaceAll('\n', '<br/>') : ' '; el.innerHTML = text ? text.replaceAll('\n', '<br/>') : ' ';
@@ -593,22 +594,38 @@ const placeholderElFromText = (text: string, type: EditorProps['type']) => {
function saveCachedEditorState(stateKey: string | null, state: EditorState | null) { function saveCachedEditorState(stateKey: string | null, state: EditorState | null) {
if (!stateKey || state == null) return; if (!stateKey || state == null) return;
sessionStorage.setItem(computeFullStateKey(stateKey), JSON.stringify(state.toJSON(stateFields))); const stateObj = state.toJSON(stateFields);
// Save state in sessionStorage by removing doc and saving the hash of it instead
// This will be checked on restore and put back in if it matches
stateObj.docHash = md5(stateObj.doc);
delete stateObj.doc;
try {
sessionStorage.setItem(computeFullStateKey(stateKey), JSON.stringify(stateObj));
} catch (err) {
console.log('Failed to save to editor state', stateKey, err);
}
} }
function getCachedEditorState(doc: string, stateKey: string | null) { function getCachedEditorState(doc: string, stateKey: string | null) {
if (stateKey == null) return; if (stateKey == null) return;
const stateStr = sessionStorage.getItem(computeFullStateKey(stateKey));
if (stateStr == null) return null;
try { try {
const state = JSON.parse(stateStr); const stateStr = sessionStorage.getItem(computeFullStateKey(stateKey));
if (state.doc !== doc) return null; if (stateStr == null) return null;
const { docHash, ...state } = JSON.parse(stateStr);
// Ensure the doc matches the one that was used to save the state
if (docHash !== md5(doc)) {
return null;
}
state.doc = doc;
return state; return state;
} catch { } catch (err) {
// Nothing console.log('Failed to restore editor storage', stateKey, err);
} }
return null; return null;

View File

@@ -23,7 +23,6 @@ export function HTMLOrTextViewer({ response, pretty, textViewerClassName }: Prop
return null; return null;
} }
console.log("HELLO", rawTextBody.data, response);
// Wasn't able to decode as text, so it must be binary // Wasn't able to decode as text, so it must be binary
if (rawTextBody.data == null) { if (rawTextBody.data == null) {
return <BinaryViewer response={response} />; return <BinaryViewer response={response} />;

View File

@@ -47,6 +47,7 @@
"fuzzbunny": "^1.0.1", "fuzzbunny": "^1.0.1",
"history": "^5.3.0", "history": "^5.3.0",
"jotai": "^2.9.3", "jotai": "^2.9.3",
"js-md5": "^0.8.3",
"lucide-react": "^0.439.0", "lucide-react": "^0.439.0",
"mime": "^4.0.4", "mime": "^4.0.4",
"nanoid": "^5.0.9", "nanoid": "^5.0.9",