All files / src/store/features/project projectActions.ts

45.33% Statements 34/75
0% Branches 0/6
0% Functions 0/14
43.84% Lines 32/73

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 33630x                               30x 30x       30x 30x   30x                     30x                       30x                                     30x                                                                                                                                             30x 30x   30x 30x       30x                             30x 30x           30x   30x                                     30x                                           30x       30x       30x       30x       30x       30x       30x     30x       30x     30x               30x                                                                                                                   30x                                               30x  
import { createAsyncThunk, createAction } from "@reduxjs/toolkit";
import {
  EntitiesState,
  ProjectEntitiesData,
  BackgroundData,
  SpriteSheetData,
  MusicData,
  FontData,
  AvatarData,
  EmoteData,
  SoundData,
  TilesetData,
} from "shared/lib/entities/entitiesTypes";
import type { RootState } from "store/configureStore";
import { SettingsState } from "store/features/settings/settingsState";
import { MetadataState } from "store/features/metadata/metadataState";
import { denormalizeEntities } from "shared/lib/entities/entitiesHelpers";
import API from "renderer/lib/api";
import { Asset, AssetType } from "shared/lib/helpers/assets";
import type { LoadProjectResult } from "lib/project/loadProjectData";
import { ProjectResources } from "shared/lib/resources/types";
import { compressProjectResources } from "shared/lib/resources/compression";
import { buildCompressedProjectResourcesPatch } from "shared/lib/resources/patch";
 
let saving = false;
 
export type ProjectData = ProjectEntitiesData & {
  name: string;
  author: string;
  notes: string;
  _version: string;
  _release: string;
  settings: SettingsState;
};
 
export const saveSteps = [
  "saving",
  "normalizing",
  "compressing",
  "checksums",
  "patching",
  "writing",
  "complete",
] as const;
 
export type SaveStep = typeof saveSteps[number];
 
export const denormalizeProject = (project: {
  entities: EntitiesState;
  settings: SettingsState;
  metadata: MetadataState;
}): ProjectResources => {
  const entitiesData = denormalizeEntities(project.entities);
  return {
    ...entitiesData,
    settings: {
      _resourceType: "settings",
      ...project.settings,
    },
    metadata: {
      _resourceType: "project",
      ...project.metadata,
    },
  };
};
 
export const trimProjectData = (data: ProjectData): ProjectData => {
  return {
    ...data,
    backgrounds: data.backgrounds.map(
      (background) =>
        ({
          ...background,
          inode: undefined,
          _v: undefined,
        } as unknown as BackgroundData)
    ),
    spriteSheets: data.spriteSheets.map(
      (spriteSheet) =>
        ({
          ...spriteSheet,
          inode: undefined,
          _v: undefined,
        } as unknown as SpriteSheetData)
    ),
    music: data.music.map(
      (track) =>
        ({
          ...track,
          inode: undefined,
          _v: undefined,
        } as unknown as MusicData)
    ),
    sounds: data.sounds.map(
      (sound) =>
        ({
          ...sound,
          inode: undefined,
          _v: undefined,
        } as unknown as SoundData)
    ),
    fonts: data.fonts.map(
      (font) =>
        ({
          ...font,
          mapping: undefined,
          inode: undefined,
          _v: undefined,
        } as unknown as FontData)
    ),
    avatars: data.avatars.map(
      (avatar) =>
        ({
          ...avatar,
          inode: undefined,
          _v: undefined,
        } as unknown as AvatarData)
    ),
    emotes: data.emotes.map(
      (emote) =>
        ({
          ...emote,
          inode: undefined,
          _v: undefined,
        } as unknown as EmoteData)
    ),
    tilesets: data.tilesets.map(
      (tileset) =>
        ({
          ...tileset,
          inode: undefined,
          _v: undefined,
        } as unknown as TilesetData)
    ),
  };
};
 
const openProject = createAction<string>("project/openProject");
const closeProject = createAction<void>("project/closeProject");
 
const setSaveStep = createAction<SaveStep>("project/setSaveStep");
const setSaveWriteProgress = createAction<{ completed: number; total: number }>(
  "project/setSaveWriteProgress"
);
 
const loadProject = createAsyncThunk<
  LoadProjectResult & { path: string },
  string
>("project/loadProject", async (path) => {
  const data = await API.project.loadProject();
  return {
    ...data,
    path,
  };
});
 
/**************************************************************************
 * UI
 */
 
const loadUI = createAction("project/loadUI");
const reloadAssets = createAction("project/reloadAssets");
 
/**************************************************************************
 * Asset Files
 */
 
const addFileToProject = createAction<string>("project/addFile");
 
const removeAsset = createAsyncThunk<
  {
    assetType: AssetType;
    asset: Asset;
  },
  {
    assetType: AssetType;
    asset: Asset;
  }
>("project/removeAsset", async ({ assetType, asset }) => {
  Iif (!(await API.project.removeAsset(assetType, asset))) {
    throw new Error("Didn't remove asset");
  }
  return {
    assetType,
    asset,
  };
});
 
const renameAsset = createAsyncThunk<
  {
    assetType: AssetType;
    asset: Asset;
    newFilename: string;
  },
  {
    assetType: AssetType;
    asset: Asset;
    newFilename: string;
  }
>("project/renameAsset", async ({ assetType, asset, newFilename }) => {
  Iif (!(await API.project.renameAsset(assetType, asset, newFilename))) {
    throw new Error("Didn't rename asset");
  }
  return {
    assetType,
    asset,
    newFilename,
  };
});
 
const renameBackgroundAsset = createAction<{
  backgroundId: string;
  newFilename: string;
}>("project/renameBackgroundAsset");
const removeBackgroundAsset = createAction<{ backgroundId: string }>(
  "project/removeBackgroundAsset"
);
 
const renameTilesetAsset = createAction<{
  tilesetId: string;
  newFilename: string;
}>("project/renameTilesetAsset");
const removeTilesetAsset = createAction<{ tilesetId: string }>(
  "project/removeTilesetAsset"
);
 
const renameSpriteAsset = createAction<{
  spriteSheetId: string;
  newFilename: string;
}>("project/renameSpriteAsset");
const removeSpriteAsset = createAction<{ spriteSheetId: string }>(
  "project/removeSpriteAsset"
);
 
const renameMusicAsset = createAction<{ musicId: string; newFilename: string }>(
  "project/renameMusicAsset"
);
const removeMusicAsset = createAction<{ musicId: string }>(
  "project/removeMusicAsset"
);
 
const renameSoundAsset = createAction<{ soundId: string; newFilename: string }>(
  "project/renameSoundAsset"
);
const removeSoundAsset = createAction<{ soundId: string }>(
  "project/removeSoundAsset"
);
 
/**************************************************************************
 * Save
 */
 
const saveProject = createAsyncThunk<void>(
  "project/saveProject",
  async (_, thunkApi) => {
    const state = thunkApi.getState() as RootState;
 
    Iif (!state.document.loaded) {
      throw new Error("Cannot save project that has not finished loading");
    }
    Iif (saving) {
      throw new Error("Cannot save project while already saving");
    }
 
    saving = true;
 
    try {
      thunkApi.dispatch(setSaveStep("normalizing"));
 
      const normalizedProject = denormalizeProject(state.project.present);
 
      thunkApi.dispatch(setSaveStep("compressing"));
 
      const data = compressProjectResources({
        ...normalizedProject,
        settings: {
          ...normalizedProject.settings,
          zoom: state.editor.zoom,
          worldScrollX: state.editor.worldScrollX,
          worldScrollY: state.editor.worldScrollY,
          navigatorSplitSizes: state.editor.navigatorSplitSizesManuallyEdited
            ? state.editor.navigatorSplitSizes
            : normalizedProject.settings.navigatorSplitSizes,
        },
      });
 
      // Save
      thunkApi.dispatch(setSaveStep("checksums"));
      const resourceChecksums = await API.project.getResourceChecksums();
 
      thunkApi.dispatch(setSaveStep("patching"));
      const patch = buildCompressedProjectResourcesPatch(
        data,
        resourceChecksums
      );
 
      thunkApi.dispatch(setSaveStep("writing"));
      await API.project.saveProject(patch);
 
      thunkApi.dispatch(setSaveStep("complete"));
    } catch (e) {
      console.error(e);
      saving = false;
      throw e;
    }
 
    saving = false;
  }
);
 
const projectActions = {
  openProject,
  closeProject,
  loadProject,
  loadUI,
  addFileToProject,
  reloadAssets,
  saveProject,
  setSaveStep,
  setSaveWriteProgress,
  renameAsset,
  renameBackgroundAsset,
  renameTilesetAsset,
  renameSpriteAsset,
  renameMusicAsset,
  renameSoundAsset,
  removeAsset,
  removeBackgroundAsset,
  removeTilesetAsset,
  removeSpriteAsset,
  removeMusicAsset,
  removeSoundAsset,
};
 
export default projectActions;