All files / src/packlets/editor-core user-actions.ts

70.9% Statements 39/55
40% Branches 4/10
76.92% Functions 10/13
75% Lines 39/52

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                                    3x           3x 3x 3x       4x 4x 6x   4x       2x 2x 3x   2x 2x 2x   2x 2x 3x 3x 3x   2x         3x           3x 3x 3x       3x 3x       1x         4x                           4x 4x 4x 4x 4x       4x 4x 4x             1x 1x 1x                                                                                          
/**
 * @packageDocumentation
 *
 * User action classes for undoable editor operations.
 */
 
import type { WritableAtom } from "nanostores";
import { uuidv7 } from "uuidv7";
import type { Entity } from "../entity-manager";
import { LEVEL_REF } from "./components";
import type { EditorContext } from "./editor-context";
import { ProjectSlice } from "./slices/project-slice";
import { SelectionSlice } from "./slices/selection-slice";
import { LevelSlice } from "./slices/level-slice";
import { ChartSlice } from "./slices/chart-slice";
import type { UserAction } from "./types";
 
export class DeleteUserAction implements UserAction {
  title = "Delete selection";
  private ctx: EditorContext;
  private entityIds: string[];
  private entities: Entity[];
 
  constructor(ctx: EditorContext, entityIds: string[], entities: Entity[]) {
    this.ctx = ctx;
    this.entityIds = entityIds;
    this.entities = entities;
  }
 
  do(): void {
    const em = this.ctx.get(ProjectSlice).entityManager;
    for (const id of this.entityIds) {
      em.delete(id);
    }
    this.ctx.get(SelectionSlice).$selection.set(new Set());
  }
 
  undo(): void {
    const em = this.ctx.get(ProjectSlice).entityManager;
    for (const entity of this.entities) {
      em.restore(entity);
    }
    const chartId = this.ctx.get(ChartSlice).$selectedChartId.get();
    const visibleLevels = new Set(
      (chartId ? this.ctx.get(LevelSlice).getVisibleLevels(chartId) : []).map((l) => l.id),
    );
    const selection = new Set<string>();
    for (const entity of this.entities) {
      const levelRef = em.getComponent(entity, LEVEL_REF);
      Iif (levelRef && !visibleLevels.has(levelRef.levelId)) continue;
      selection.add(entity.id);
    }
    this.ctx.get(SelectionSlice).$selection.set(selection);
  }
}
 
export class EraseUserAction implements UserAction {
  title = "Erase entity";
  private ctx: EditorContext;
  private entityId: string;
  private entitySnapshot: Entity;
 
  constructor(ctx: EditorContext, entityId: string, entitySnapshot: Entity) {
    this.ctx = ctx;
    this.entityId = entityId;
    this.entitySnapshot = entitySnapshot;
  }
 
  do(): void {
    this.ctx.get(ProjectSlice).entityManager.delete(this.entityId);
    this.ctx.get(SelectionSlice).$selection.set(new Set());
  }
 
  undo(): void {
    this.ctx.get(ProjectSlice).entityManager.restore(this.entitySnapshot);
  }
}
 
export class PlaceEntityUserAction implements UserAction {
  title = "Place entity";
  private ctx: EditorContext;
  private entity: Entity;
  private columnId: string;
  private previousSelection: Set<string>;
  private lastPlacedAtom: WritableAtom<{ entityId: string; columnId: string } | null>;
 
  constructor(
    ctx: EditorContext,
    entity: Entity,
    columnId: string,
    previousSelection: Set<string>,
    lastPlacedAtom: WritableAtom<{ entityId: string; columnId: string } | null>,
  ) {
    this.ctx = ctx;
    this.entity = entity;
    this.columnId = columnId;
    this.previousSelection = previousSelection;
    this.lastPlacedAtom = lastPlacedAtom;
  }
 
  do(): void {
    this.ctx.get(ProjectSlice).entityManager.insert(this.entity);
    this.ctx.get(SelectionSlice).$selection.set(new Set([this.entity.id]));
    this.lastPlacedAtom.set({
      entityId: this.entity.id,
      columnId: this.columnId,
    });
  }
 
  undo(): void {
    this.ctx.get(ProjectSlice).entityManager.delete(this.entity.id);
    this.ctx.get(SelectionSlice).$selection.set(new Set(this.previousSelection));
    this.lastPlacedAtom.set(null);
  }
}
 
export class EditEntityUserAction implements UserAction {
  title = "Edit entity";
  private ctx: EditorContext;
  private entityId: string;
  private oldComponents: Record<string, unknown>;
  private newComponents: Record<string, unknown>;
 
  constructor(
    ctx: EditorContext,
    entityId: string,
    oldComponents: Record<string, unknown>,
    newComponents: Record<string, unknown>,
  ) {
    this.ctx = ctx;
    this.entityId = entityId;
    this.oldComponents = oldComponents;
    this.newComponents = newComponents;
  }
 
  do(): void {
    const em = this.ctx.get(ProjectSlice).entityManager;
    const entity = em.get(this.entityId);
    if (!entity) return;
    em.insert({
      ...entity,
      components: structuredClone(this.newComponents),
      version: uuidv7(),
    });
  }
 
  undo(): void {
    const em = this.ctx.get(ProjectSlice).entityManager;
    const entity = em.get(this.entityId);
    if (!entity) return;
    em.insert({
      ...entity,
      components: structuredClone(this.oldComponents),
      version: uuidv7(),
    });
  }
}