All files / src/store/features/clipboard clipboardActions.ts

40.28% Statements 29/72
0% Branches 0/20
0% Functions 0/6
40.85% Lines 29/71

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 1921x 1x 1x             1x 1x 1x           1x 1x 1x     1x     1x     1x     1x       1x     1x     1x     1x           1x             1x     1x 1x       1x         1x         1x           1x                                                   1x                                   1x                                                                               1x                                                 1x  
import { createAction, UnknownAction, ThunkDispatch } from "@reduxjs/toolkit";
import { actions } from "./clipboardState";
import {
  sceneSelectors,
  actorSelectors,
  triggerSelectors,
} from "store/features/entities/entitiesState";
import { ScriptEventParentType } from "shared/lib/entities/entitiesTypes";
import { RootState } from "store/configureStore";
import editorActions from "store/features/editor/editorActions";
import { pasteAny } from "./clipboardHelpers";
import {
  ClipboardTypeActors,
  ClipboardTypeScenes,
  ClipboardTypeTriggers,
} from "./clipboardTypes";
 
const fetchClipboard = createAction("clipboard/fetch");
const copyText = createAction<string>("clipboard/copyText");
const copyScriptEvents = createAction<{
  scriptEventIds: string[];
}>("clipboard/copyScriptEvents");
const copyTriggers = createAction<{
  triggerIds: string[];
}>("clipboard/copyTriggers");
const copyActors = createAction<{
  actorIds: string[];
}>("clipboard/copyActors");
const copyScenes = createAction<{
  sceneIds: string[];
}>("clipboard/copyScenes");
const copyMetasprites = createAction<{
  metaspriteIds: string[];
  spriteAnimationId: string;
}>("clipboard/copyMetasprites");
const copyMetaspriteTiles = createAction<{
  metaspriteTileIds: string[];
}>("clipboard/copyMetaspriteTiles");
const copySpriteState = createAction<{
  spriteStateId: string;
}>("clipboard/copySpriteState");
const copyPaletteIds = createAction<{
  paletteIds: string[];
}>("clipboard/copyPaletteIds");
const pasteSprite = createAction<{
  spriteSheetId: string;
  metaspriteId: string;
  spriteAnimationId: string;
  spriteStateId: string;
}>("clipboard/pasteSprite");
const pasteScriptEvents = createAction<{
  entityId: string;
  type: ScriptEventParentType;
  key: string;
  insertId?: string;
  before?: boolean;
}>("clipboard/pasteScriptEvents");
const pasteScriptEventValues = createAction<{
  scriptEventId: string;
}>("clipboard/pasteScriptEventValues");
const pasteCustomEvents = createAction<void>("clipboard/pasteCustomEvents");
const pastePaletteIds = createAction<{
  sceneId: string;
  type: "background" | "sprite";
}>("clipboard/pastePaletteIds");
const pasteTriggerAt = createAction<{
  sceneId: string;
  x: number;
  y: number;
}>("clipboard/pasteTriggerAt");
const pasteActorAt = createAction<{
  sceneId: string;
  x: number;
  y: number;
}>("clipboard/pasteActorAt");
const pasteSceneAt = createAction<{
  x: number;
  y: number;
}>("clipboard/pasteSceneAt");
 
const copySelectedEntity =
  () =>
  (
    dispatch: ThunkDispatch<RootState, unknown, UnknownAction>,
    getState: () => RootState
  ) => {
    const state = getState();
    const { scene: sceneId, entityId, type: editorType } = state.editor;
    if (editorType === "scene") {
      const scene = sceneSelectors.selectById(state, sceneId);
      Iif (scene) {
        dispatch(copyScenes({ sceneIds: [scene.id] }));
      }
    } else if (editorType === "actor") {
      const actor = actorSelectors.selectById(state, entityId);
      Iif (actor) {
        dispatch(copyActors({ actorIds: [actor.id] }));
      }
    } else Iif (editorType === "trigger") {
      const trigger = triggerSelectors.selectById(state, entityId);
      Iif (trigger) {
        dispatch(copyTriggers({ triggerIds: [trigger.id] }));
      }
    }
  };
 
const pasteClipboardEntity =
  () => async (dispatch: ThunkDispatch<RootState, unknown, UnknownAction>) => {
    const clipboard = await pasteAny();
    Iif (!clipboard) {
      return;
    }
    if (clipboard.format === ClipboardTypeTriggers) {
      dispatch(editorActions.setTool({ tool: "triggers" }));
      dispatch(editorActions.setPasteMode(true));
    } else if (clipboard.format === ClipboardTypeActors) {
      dispatch(editorActions.setTool({ tool: "actors" }));
      dispatch(editorActions.setPasteMode(true));
    } else Iif (clipboard.format === ClipboardTypeScenes) {
      dispatch(editorActions.setTool({ tool: "scene" }));
      dispatch(editorActions.setPasteMode(true));
    }
  };
 
const pasteClipboardEntityInPlace =
  () =>
  async (
    dispatch: ThunkDispatch<RootState, unknown, UnknownAction>,
    getState: () => RootState
  ) => {
    const clipboard = await pasteAny();
    Iif (!clipboard) {
      return;
    }
    const state = getState();
    const { scene: sceneId } = state.editor;
    if (clipboard.format === ClipboardTypeTriggers) {
      const trigger = clipboard.data.triggers[0];
      dispatch(
        pasteTriggerAt({
          sceneId,
          x: trigger.x,
          y: trigger.y,
        })
      );
    } else if (clipboard.format === ClipboardTypeActors) {
      const actor = clipboard.data.actors[0];
      dispatch(
        pasteActorAt({
          sceneId,
          x: actor.x,
          y: actor.y,
        })
      );
    } else Iif (clipboard.format === ClipboardTypeScenes) {
      const scene = clipboard.data.scenes[0];
      dispatch(
        pasteSceneAt({
          x: scene.x,
          y: scene.y,
        })
      );
    }
  };
 
const clipboardActions = {
  ...actions,
  fetchClipboard,
  copyText,
  copyScriptEvents,
  copyTriggers,
  copyActors,
  copyScenes,
  copyMetasprites,
  copyMetaspriteTiles,
  copySpriteState,
  copySelectedEntity,
  copyPaletteIds,
  pasteClipboardEntity,
  pasteClipboardEntityInPlace,
  pasteCustomEvents,
  pasteSprite,
  pastePaletteIds,
  pasteScriptEvents,
  pasteScriptEventValues,
  pasteTriggerAt,
  pasteActorAt,
  pasteSceneAt,
};
 
export default clipboardActions;