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 | 30x | import { createAsyncThunk } from '@reduxjs/toolkit';
import { nanoid } from 'nanoid';
import type { RootState } from '@/store';
import type { HistoryEntry } from '@/shared/types';
import { layerService } from '@/entities/layer/model';
import { historyRepository } from '@/entities/history/api';
import { LAYER, LAYER_DEFAULTS, LAYER_SLICE_ACTIONS, TOOLS } from '@/shared/constants';
import { fetchLayersByProject, setActiveLayerId } from '@/entities/layer/model/slice';
/**
* Ensures that the project has at least one base layer.
* Also logs the creation of that base layer in the history.
*/
export const ensureInitialLayer = createAsyncThunk<void, string, { state: RootState }>(
`${LAYER_SLICE_ACTIONS.LAYER_INIT_BASE}`,
async (projectId, { dispatch }) => {
// Atomically create base layer if none exist
const baseLayer = await layerService.ensureBaseLayer(
projectId,
`${LAYER_DEFAULTS.INITIAL_NAME}`,
);
// Sync Redux store with DB
await dispatch(fetchLayersByProject(projectId)).unwrap();
// Set active layer to the created or first existing one
if (baseLayer) {
dispatch(setActiveLayerId(baseLayer.id));
// Create base layer
const historyEntry: HistoryEntry = {
id: nanoid(),
label: `${LAYER.CREATE_BASE_LAYER(baseLayer.name)}`,
timestamp: Date.now(),
toolType: TOOLS.BRUSH,
state: {
projectId,
layers: [
{
id: baseLayer.id,
name: baseLayer.name,
visible: baseLayer.visible,
opacity: baseLayer.opacity,
zIndex: baseLayer.zIndex,
snapshot: baseLayer.snapshot,
},
],
activeTool: null,
viewport: { scale: 1, offsetX: 0, offsetY: 0 },
},
};
await historyRepository.add(historyEntry);
}
},
);
|