77 lines
1.5 KiB
TypeScript
77 lines
1.5 KiB
TypeScript
|
|
import { create } from 'zustand';
|
||
|
|
import type {
|
||
|
|
Transform,
|
||
|
|
TransformType,
|
||
|
|
TransformBounds,
|
||
|
|
TransformState,
|
||
|
|
TransformStore as ITransformStore,
|
||
|
|
} from '@/types/transform';
|
||
|
|
|
||
|
|
export const useTransformStore = create<ITransformStore>((set, get) => ({
|
||
|
|
activeTransform: null,
|
||
|
|
transformType: 'move',
|
||
|
|
showHandles: true,
|
||
|
|
maintainAspectRatio: false,
|
||
|
|
|
||
|
|
setTransformType: (type) =>
|
||
|
|
set({
|
||
|
|
transformType: type,
|
||
|
|
}),
|
||
|
|
|
||
|
|
setShowHandles: (show) =>
|
||
|
|
set({
|
||
|
|
showHandles: show,
|
||
|
|
}),
|
||
|
|
|
||
|
|
setMaintainAspectRatio: (maintain) =>
|
||
|
|
set({
|
||
|
|
maintainAspectRatio: maintain,
|
||
|
|
}),
|
||
|
|
|
||
|
|
startTransform: (layerId, bounds) =>
|
||
|
|
set({
|
||
|
|
activeTransform: {
|
||
|
|
layerId,
|
||
|
|
originalBounds: bounds,
|
||
|
|
currentState: {
|
||
|
|
x: 0,
|
||
|
|
y: 0,
|
||
|
|
scaleX: 1,
|
||
|
|
scaleY: 1,
|
||
|
|
rotation: 0,
|
||
|
|
skewX: 0,
|
||
|
|
skewY: 0,
|
||
|
|
},
|
||
|
|
isActive: true,
|
||
|
|
maintainAspectRatio: get().maintainAspectRatio,
|
||
|
|
},
|
||
|
|
}),
|
||
|
|
|
||
|
|
updateTransform: (stateUpdate) =>
|
||
|
|
set((state) => {
|
||
|
|
if (!state.activeTransform) return state;
|
||
|
|
|
||
|
|
return {
|
||
|
|
activeTransform: {
|
||
|
|
...state.activeTransform,
|
||
|
|
currentState: {
|
||
|
|
...state.activeTransform.currentState,
|
||
|
|
...stateUpdate,
|
||
|
|
},
|
||
|
|
},
|
||
|
|
};
|
||
|
|
}),
|
||
|
|
|
||
|
|
applyTransform: () => {
|
||
|
|
// Transform will be applied via command
|
||
|
|
set({
|
||
|
|
activeTransform: null,
|
||
|
|
});
|
||
|
|
},
|
||
|
|
|
||
|
|
cancelTransform: () =>
|
||
|
|
set({
|
||
|
|
activeTransform: null,
|
||
|
|
}),
|
||
|
|
}));
|