forked from hpi-sam/digital-fuesim-manv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviewport.ts
121 lines (109 loc) · 4.07 KB
/
viewport.ts
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
import { Type } from 'class-transformer';
import { IsString, IsUUID, ValidateNested } from 'class-validator';
import { Viewport } from '../../models';
import { MapCoordinates, MapPosition, Size } from '../../models/utils';
import {
changePosition,
changePositionWithId,
} from '../../models/utils/position/position-helpers-mutable';
import { cloneDeepMutable, UUID, uuidValidationOptions } from '../../utils';
import { IsValue } from '../../utils/validators';
import type { Action, ActionReducer } from '../action-reducer';
import { getElement } from './utils/get-element';
export class AddViewportAction implements Action {
@IsValue('[Viewport] Add viewport' as const)
readonly type = '[Viewport] Add viewport';
@ValidateNested()
@Type(() => Viewport)
public viewport!: Viewport;
}
export class RemoveViewportAction implements Action {
@IsValue('[Viewport] Remove viewport' as const)
public readonly type = '[Viewport] Remove viewport';
@IsUUID(4, uuidValidationOptions)
public readonly viewportId!: UUID;
}
export class MoveViewportAction implements Action {
@IsValue('[Viewport] Move viewport' as const)
public readonly type = '[Viewport] Move viewport';
@IsUUID(4, uuidValidationOptions)
public readonly viewportId!: UUID;
@ValidateNested()
@Type(() => MapCoordinates)
public readonly targetPosition!: MapCoordinates;
}
export class ResizeViewportAction implements Action {
@IsValue('[Viewport] Resize viewport' as const)
public readonly type = '[Viewport] Resize viewport';
@IsUUID(4, uuidValidationOptions)
public readonly viewportId!: UUID;
@ValidateNested()
@Type(() => MapCoordinates)
public readonly targetPosition!: MapCoordinates;
@ValidateNested()
@Type(() => Size)
public readonly newSize!: Size;
}
export class RenameViewportAction implements Action {
@IsValue('[Viewport] Rename viewport' as const)
public readonly type = '[Viewport] Rename viewport';
@IsUUID(4, uuidValidationOptions)
public readonly viewportId!: UUID;
@IsString()
public readonly newName!: string;
}
export namespace ViewportActionReducers {
export const addViewport: ActionReducer<AddViewportAction> = {
action: AddViewportAction,
reducer: (draftState, { viewport }) => {
draftState.viewports[viewport.id] = cloneDeepMutable(viewport);
return draftState;
},
rights: 'trainer',
};
export const removeViewport: ActionReducer<RemoveViewportAction> = {
action: RemoveViewportAction,
reducer: (draftState, { viewportId }) => {
getElement(draftState, 'viewport', viewportId);
delete draftState.viewports[viewportId];
return draftState;
},
rights: 'trainer',
};
export const moveViewport: ActionReducer<MoveViewportAction> = {
action: MoveViewportAction,
reducer: (draftState, { viewportId, targetPosition }) => {
changePositionWithId(
viewportId,
MapPosition.create(targetPosition),
'viewport',
draftState
);
return draftState;
},
rights: 'trainer',
};
export const resizeViewport: ActionReducer<ResizeViewportAction> = {
action: ResizeViewportAction,
reducer: (draftState, { viewportId, targetPosition, newSize }) => {
const viewport = getElement(draftState, 'viewport', viewportId);
changePosition(
viewport,
MapPosition.create(targetPosition),
draftState
);
viewport.size = cloneDeepMutable(newSize);
return draftState;
},
rights: 'trainer',
};
export const renameViewport: ActionReducer<RenameViewportAction> = {
action: RenameViewportAction,
reducer: (draftState, { viewportId, newName }) => {
const viewport = getElement(draftState, 'viewport', viewportId);
viewport.name = newName;
return draftState;
},
rights: 'trainer',
};
}