From 38e762d58914f1e00f5e47382204ed67011cae53 Mon Sep 17 00:00:00 2001 From: Andreas Hahn Date: Mon, 9 Sep 2024 00:30:14 +0200 Subject: [PATCH 1/2] Added option to convert stored frames to new format --- .../app/components/Frames/index.tsx | 13 +++++ .../app/components/Frames/useFrames.ts | 50 ++++++++++++++++++- .../tools/saveLocalStorageItems/index.ts | 24 +++++---- 3 files changed, 77 insertions(+), 10 deletions(-) diff --git a/src/javascript/app/components/Frames/index.tsx b/src/javascript/app/components/Frames/index.tsx index 612973a0..a1816f65 100644 --- a/src/javascript/app/components/Frames/index.tsx +++ b/src/javascript/app/components/Frames/index.tsx @@ -17,6 +17,8 @@ function Frames() { exportJson, setActiveFrameGroupName, activeFrameGroup, + convertFormat, + enableDebug, } = useFrames(); return ( @@ -84,6 +86,17 @@ function Frames() { > {`Export current framegroup (${selectedFrameGroup})`} + { + enableDebug ? ( + + ) : null + } ); diff --git a/src/javascript/app/components/Frames/useFrames.ts b/src/javascript/app/components/Frames/useFrames.ts index 3dec11b1..3ef917a3 100644 --- a/src/javascript/app/components/Frames/useFrames.ts +++ b/src/javascript/app/components/Frames/useFrames.ts @@ -8,6 +8,9 @@ import type { State } from '../../store/State'; import type { Frame } from '../../../../types/Frame'; import type { ExportJSONAction } from '../../../../types/actions/StorageActions'; import type { ExportTypes } from '../../../consts/exportTypes'; +import type { GlobalUpdateAction } from '../../../../types/GlobalUpdateAction'; +import { compressAndHashFrame, loadFrameData, saveFrameData } from '../../../tools/applyFrame/frameData'; +import { padFrameData } from '../../../tools/saveLocalStorageItems'; const getValidFrameGroupId = (groups: FrameGroup[], byId: string): string => { const group = groups.find(({ id }) => id === byId); @@ -27,16 +30,25 @@ interface UseFrames { palette: string[], setActiveFrameGroupName: (name: string) => void, activeFrameGroup: FrameGroup, + convertFormat: () => void, + enableDebug: boolean, } const useFrames = (): UseFrames => { const dispatch = useDispatch(); - const { savFrameTypes, frames, frameGroupNames, palette } = useSelector((state: State) => ({ + const { + savFrameTypes, + frames, + frameGroupNames, + palette, + enableDebug, + } = useSelector((state: State) => ({ savFrameTypes: state.savFrameTypes, frames: state.frames, frameGroupNames: state.frameGroupNames, palette: state.palettes.find(({ shortName }) => shortName === state.activePalette) || state.palettes[0], + enableDebug: state.enableDebug, })); const frameGroups = getFrameGroups(frames, frameGroupNames); @@ -75,6 +87,40 @@ const useFrames = (): UseFrames => { }); }; + const convertFormat = async () => { + const updatedFrames = await Promise.all(frames.map(async (frame): Promise => { + const stateData = await loadFrameData(frame.hash); + + if (!stateData) { + return frame; + } + + const imageStartLine = stateData.upper.length / 20; + const tileData = padFrameData(stateData); + + const { dataHash: newHash } = await compressAndHashFrame(tileData, imageStartLine); + + + if (frame.hash === newHash) { + return frame; + } + + const saveHash = await saveFrameData(tileData, imageStartLine); + + return { + ...frame, + hash: saveHash, + }; + })); + + dispatch({ + type: Actions.GLOBAL_UPDATE, + payload: { + frames: updatedFrames, + }, + }); + }; + return { selectedFrameGroup, groupFrames, @@ -84,6 +130,8 @@ const useFrames = (): UseFrames => { palette: palette?.palette, setActiveFrameGroupName, activeFrameGroup, + convertFormat, + enableDebug, }; }; diff --git a/src/javascript/tools/saveLocalStorageItems/index.ts b/src/javascript/tools/saveLocalStorageItems/index.ts index 0f8841f8..84755c10 100644 --- a/src/javascript/tools/saveLocalStorageItems/index.ts +++ b/src/javascript/tools/saveLocalStorageItems/index.ts @@ -12,29 +12,35 @@ export const saveImageFileContent = async (fileContent: string): Promise return save(lines); }; -export const saveFrameFileContent = async (fileContent: string): Promise => { - const tiles = JSON.parse(fileContent) as FrameData; - const imageStartLine = tiles.upper.length / 20; - +export const padFrameData = (frameData: FrameData): string[] => { const black = Array(32) .fill('f') .join(''); const pad = Array(16) .fill(black); - // tiles need to be padded with some lines that get stripped again when saving frame data const paddedFrameData = [ - ...tiles.upper, + ...frameData.upper, ...Array(14) .fill('') .map((_, index) => ([ - ...tiles.left[index], + ...frameData.left[index], ...pad, - ...tiles.right[index], + ...frameData.right[index], ])) .flat(), - ...tiles.lower, + ...frameData.lower, ]; + + return paddedFrameData; +}; + +export const saveFrameFileContent = async (fileContent: string): Promise => { + const tiles = JSON.parse(fileContent) as FrameData; + const imageStartLine = tiles.upper.length / 20; + + // tiles need to be padded with some lines that get stripped again when saving frame data + const paddedFrameData = padFrameData(tiles); return saveFrameData(paddedFrameData, imageStartLine); }; From 967bf56b8953f802e93e2e6729d92528813928d6 Mon Sep 17 00:00:00 2001 From: Andreas Hahn Date: Mon, 9 Sep 2024 00:30:35 +0200 Subject: [PATCH 2/2] Version 1.42.7 --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index b3835496..96fce284 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "gb-printer-web", - "version": "1.42.6", + "version": "1.42.7", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "gb-printer-web", - "version": "1.42.6", + "version": "1.42.7", "license": "MIT", "devDependencies": { "@babel/core": "^7.14.3", diff --git a/package.json b/package.json index 0d445432..9a3f40b1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "gb-printer-web", - "version": "1.42.6", + "version": "1.42.7", "description": "gb-printer-web", "scripts": { "start": "webpack serve --config ./scripts/webpack.dev.js",