import { defineStore } from "pinia"; import { store } from "@/store"; interface IPathDrawingState { active: boolean; template: Record | null; submit: ((payload: IPathDrawingResult) => void) | null; } export interface IPathDrawingResult { worldPoints: Array<{ x: number; y: number; z: number }>; origin: { x: number; y: number; z: number }; options: Record; } export interface IPathDrawingRequest { template?: Record | null; submit?: (payload: IPathDrawingResult) => void; } function isPathDrawingRequest(payload: Record | IPathDrawingRequest) { return Object.prototype.hasOwnProperty.call(payload, "template") || Object.prototype.hasOwnProperty.call(payload, "submit"); } export const usePathDrawingStore = defineStore({ id: "pathDrawing", state: (): IPathDrawingState => ({ active: false, template: null, submit: null, }), getters: { isActive: state => state.active, getTemplate: state => state.template, getSubmit: state => state.submit, }, actions: { start(payload: Record | IPathDrawingRequest) { const request = isPathDrawingRequest(payload) ? payload : { template: payload }; this.template = request.template ? JSON.parse(JSON.stringify(request.template)) : null; this.submit = typeof request.submit === "function" ? request.submit : null; this.active = true; }, cancel() { this.active = false; this.template = null; this.submit = null; }, finish() { this.active = false; this.template = null; this.submit = null; }, }, }); export function usePathDrawingStoreWithOut() { return usePathDrawingStore(store); }