Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
ota-meshi committed Sep 24, 2023
1 parent 6f8c970 commit 16dcb65
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
12 changes: 6 additions & 6 deletions docs/.vitepress/theme/components/state/deserialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ export function deserializeState(serializedString) {
}

try {
// For backward compatibility, it can address non-compressed data.
const compressed = !serializedString.startsWith("eyJj");
const decodedText = window.atob(serializedString);
const jsonText = compressed
? pako.inflate(decodedText, { to: "string" })
: decodedText;
const compressedString = window.atob(serializedString);
const uint8Arr = pako.inflate(
Uint8Array.from(compressedString, (c) => c.charCodeAt(0)),
);

const jsonText = new TextDecoder().decode(uint8Arr);
const json = JSON.parse(jsonText);

if (typeof json === "object" && json != null) {
Expand Down
4 changes: 3 additions & 1 deletion docs/.vitepress/theme/components/state/serialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ export function serializeState(state) {
rules: state.rules ? getEnabledRules(state.rules) : undefined,
};
const jsonString = JSON.stringify(saveData);
const compressedString = pako.deflate(jsonString, { to: "string" });

const uint8Arr = new TextEncoder().encode(jsonString);
const compressedString = String.fromCharCode(...pako.deflate(uint8Arr));
const base64 =
(typeof window !== "undefined" && window.btoa(compressedString)) ||
compressedString;
Expand Down

0 comments on commit 16dcb65

Please sign in to comment.