@daltonr/pathwrite-vue 0.1.4 → 0.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +3 -2
- package/src/index.ts +285 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@daltonr/pathwrite-vue",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.5",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"description": "Vue 3 adapter for @daltonr/pathwrite-core — composables with reactive refs, and optional <PathShell> default UI.",
|
|
@@ -33,6 +33,7 @@
|
|
|
33
33
|
"types": "dist/index.d.ts",
|
|
34
34
|
"files": [
|
|
35
35
|
"dist",
|
|
36
|
+
"src",
|
|
36
37
|
"README.md",
|
|
37
38
|
"LICENSE"
|
|
38
39
|
],
|
|
@@ -45,7 +46,7 @@
|
|
|
45
46
|
"vue": ">=3.3.0"
|
|
46
47
|
},
|
|
47
48
|
"dependencies": {
|
|
48
|
-
"@daltonr/pathwrite-core": "^0.1.
|
|
49
|
+
"@daltonr/pathwrite-core": "^0.1.5"
|
|
49
50
|
},
|
|
50
51
|
"devDependencies": {
|
|
51
52
|
"@vue/test-utils": "^2.4.6",
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ref,
|
|
3
|
+
shallowRef,
|
|
4
|
+
readonly,
|
|
5
|
+
onScopeDispose,
|
|
6
|
+
defineComponent,
|
|
7
|
+
h,
|
|
8
|
+
computed,
|
|
9
|
+
onMounted,
|
|
10
|
+
provide,
|
|
11
|
+
inject,
|
|
12
|
+
type Ref,
|
|
13
|
+
type DeepReadonly,
|
|
14
|
+
type PropType,
|
|
15
|
+
type InjectionKey,
|
|
16
|
+
type VNode
|
|
17
|
+
} from "vue";
|
|
18
|
+
import {
|
|
19
|
+
PathData,
|
|
20
|
+
PathDefinition,
|
|
21
|
+
PathEngine,
|
|
22
|
+
PathEvent,
|
|
23
|
+
PathSnapshot
|
|
24
|
+
} from "@daltonr/pathwrite-core";
|
|
25
|
+
|
|
26
|
+
// ---------------------------------------------------------------------------
|
|
27
|
+
// Types
|
|
28
|
+
// ---------------------------------------------------------------------------
|
|
29
|
+
|
|
30
|
+
export interface UsePathOptions {
|
|
31
|
+
/** Called for every engine event (stateChanged, completed, cancelled, resumed). */
|
|
32
|
+
onEvent?: (event: PathEvent) => void;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface UsePathReturn<TData extends PathData = PathData> {
|
|
36
|
+
/** Current path snapshot, or `null` when no path is active. Reactive — triggers Vue re-renders on change. */
|
|
37
|
+
snapshot: DeepReadonly<Ref<PathSnapshot<TData> | null>>;
|
|
38
|
+
/** Start (or restart) a path. */
|
|
39
|
+
start: (path: PathDefinition, initialData?: PathData) => Promise<void>;
|
|
40
|
+
/** Push a sub-path onto the stack. Requires an active path. */
|
|
41
|
+
startSubPath: (path: PathDefinition, initialData?: PathData) => Promise<void>;
|
|
42
|
+
/** Advance one step. Completes the path on the last step. */
|
|
43
|
+
next: () => Promise<void>;
|
|
44
|
+
/** Go back one step. Cancels the path from the first step. */
|
|
45
|
+
previous: () => Promise<void>;
|
|
46
|
+
/** Cancel the active path (or sub-path). */
|
|
47
|
+
cancel: () => Promise<void>;
|
|
48
|
+
/** Jump directly to a step by ID. Calls onLeave / onEnter but bypasses guards and shouldSkip. */
|
|
49
|
+
goToStep: (stepId: string) => Promise<void>;
|
|
50
|
+
/** Update a single data value; triggers a re-render via stateChanged. When `TData` is specified, `key` and `value` are type-checked against your data shape. */
|
|
51
|
+
setData: <K extends string & keyof TData>(key: K, value: TData[K]) => Promise<void>;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// ---------------------------------------------------------------------------
|
|
55
|
+
// usePath composable
|
|
56
|
+
// ---------------------------------------------------------------------------
|
|
57
|
+
|
|
58
|
+
export function usePath<TData extends PathData = PathData>(options?: UsePathOptions): UsePathReturn<TData> {
|
|
59
|
+
const engine = new PathEngine();
|
|
60
|
+
const _snapshot = shallowRef<PathSnapshot<TData> | null>(null);
|
|
61
|
+
|
|
62
|
+
const unsubscribe = engine.subscribe((event: PathEvent) => {
|
|
63
|
+
if (event.type === "stateChanged" || event.type === "resumed") {
|
|
64
|
+
_snapshot.value = event.snapshot as PathSnapshot<TData>;
|
|
65
|
+
} else if (event.type === "completed" || event.type === "cancelled") {
|
|
66
|
+
_snapshot.value = null;
|
|
67
|
+
}
|
|
68
|
+
options?.onEvent?.(event);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
onScopeDispose(unsubscribe);
|
|
72
|
+
|
|
73
|
+
const snapshot = readonly(_snapshot) as DeepReadonly<Ref<PathSnapshot<TData> | null>>;
|
|
74
|
+
|
|
75
|
+
const start = (path: PathDefinition, initialData: PathData = {}): Promise<void> =>
|
|
76
|
+
engine.start(path, initialData);
|
|
77
|
+
|
|
78
|
+
const startSubPath = (path: PathDefinition, initialData: PathData = {}): Promise<void> =>
|
|
79
|
+
engine.startSubPath(path, initialData);
|
|
80
|
+
|
|
81
|
+
const next = (): Promise<void> => engine.next();
|
|
82
|
+
const previous = (): Promise<void> => engine.previous();
|
|
83
|
+
const cancel = (): Promise<void> => engine.cancel();
|
|
84
|
+
|
|
85
|
+
const goToStep = (stepId: string): Promise<void> => engine.goToStep(stepId);
|
|
86
|
+
|
|
87
|
+
const setData = (<K extends string & keyof TData>(key: K, value: TData[K]): Promise<void> =>
|
|
88
|
+
engine.setData(key, value as unknown)) as UsePathReturn<TData>["setData"];
|
|
89
|
+
|
|
90
|
+
return { snapshot, start, startSubPath, next, previous, cancel, goToStep, setData };
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// ---------------------------------------------------------------------------
|
|
94
|
+
// Context — provide / inject
|
|
95
|
+
// ---------------------------------------------------------------------------
|
|
96
|
+
|
|
97
|
+
/** Injection key used by PathShell and usePathContext. */
|
|
98
|
+
const PathInjectionKey: InjectionKey<UsePathReturn> = Symbol("PathContext");
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Access the nearest `PathShell`'s path instance via Vue `inject`.
|
|
102
|
+
* Throws if used outside of a `<PathShell>`.
|
|
103
|
+
*
|
|
104
|
+
* The optional generic narrows `snapshot.data` for convenience — it is a
|
|
105
|
+
* **type-level assertion**, not a runtime guarantee.
|
|
106
|
+
*/
|
|
107
|
+
export function usePathContext<TData extends PathData = PathData>(): UsePathReturn<TData> {
|
|
108
|
+
const ctx = inject(PathInjectionKey, null);
|
|
109
|
+
if (ctx === null) {
|
|
110
|
+
throw new Error("usePathContext must be used within a <PathShell>.");
|
|
111
|
+
}
|
|
112
|
+
return ctx as UsePathReturn<TData>;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// ---------------------------------------------------------------------------
|
|
116
|
+
// Default UI — PathShell
|
|
117
|
+
// ---------------------------------------------------------------------------
|
|
118
|
+
|
|
119
|
+
export interface PathShellActions {
|
|
120
|
+
next: () => Promise<void>;
|
|
121
|
+
previous: () => Promise<void>;
|
|
122
|
+
cancel: () => Promise<void>;
|
|
123
|
+
goToStep: (stepId: string) => Promise<void>;
|
|
124
|
+
setData: (key: string, value: unknown) => Promise<void>;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* `<PathShell>` — default UI shell that renders a progress indicator,
|
|
129
|
+
* step content, and navigation buttons. Step content is provided via
|
|
130
|
+
* **named slots** matching each step's `id`.
|
|
131
|
+
*
|
|
132
|
+
* ```vue
|
|
133
|
+
* <PathShell :path="myPath" :initial-data="{ name: '' }" @complete="handleDone">
|
|
134
|
+
* <template #details><DetailsForm /></template>
|
|
135
|
+
* <template #review><ReviewPanel /></template>
|
|
136
|
+
* </PathShell>
|
|
137
|
+
* ```
|
|
138
|
+
*/
|
|
139
|
+
export const PathShell = defineComponent({
|
|
140
|
+
name: "PathShell",
|
|
141
|
+
props: {
|
|
142
|
+
path: { type: Object as PropType<PathDefinition>, required: true },
|
|
143
|
+
initialData: { type: Object as PropType<PathData>, default: () => ({}) },
|
|
144
|
+
autoStart: { type: Boolean, default: true },
|
|
145
|
+
backLabel: { type: String, default: "Back" },
|
|
146
|
+
nextLabel: { type: String, default: "Next" },
|
|
147
|
+
finishLabel: { type: String, default: "Finish" },
|
|
148
|
+
cancelLabel: { type: String, default: "Cancel" },
|
|
149
|
+
hideCancel: { type: Boolean, default: false },
|
|
150
|
+
hideProgress: { type: Boolean, default: false }
|
|
151
|
+
},
|
|
152
|
+
emits: ["complete", "cancel", "event"],
|
|
153
|
+
setup(props, { slots, emit }) {
|
|
154
|
+
const pathReturn = usePath({
|
|
155
|
+
onEvent(event) {
|
|
156
|
+
emit("event", event);
|
|
157
|
+
if (event.type === "completed") emit("complete", event.data);
|
|
158
|
+
if (event.type === "cancelled") emit("cancel", event.data);
|
|
159
|
+
}
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
const { snapshot, start, next, previous, cancel, goToStep, setData } = pathReturn;
|
|
163
|
+
|
|
164
|
+
// Provide context so child components can use usePathContext()
|
|
165
|
+
provide(PathInjectionKey, pathReturn);
|
|
166
|
+
|
|
167
|
+
const started = ref(false);
|
|
168
|
+
onMounted(() => {
|
|
169
|
+
if (props.autoStart && !started.value) {
|
|
170
|
+
started.value = true;
|
|
171
|
+
start(props.path, props.initialData);
|
|
172
|
+
}
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
const actions: PathShellActions = { next, previous, cancel, goToStep, setData };
|
|
176
|
+
|
|
177
|
+
return () => {
|
|
178
|
+
const snap = snapshot.value as PathSnapshot | null;
|
|
179
|
+
|
|
180
|
+
if (!snap) {
|
|
181
|
+
return h("div", { class: "pw-shell" },
|
|
182
|
+
h("div", { class: "pw-shell__empty" }, [
|
|
183
|
+
h("p", "No active path."),
|
|
184
|
+
!props.autoStart
|
|
185
|
+
? h("button", {
|
|
186
|
+
type: "button",
|
|
187
|
+
class: "pw-shell__start-btn",
|
|
188
|
+
onClick: () => start(props.path, props.initialData)
|
|
189
|
+
}, "Start")
|
|
190
|
+
: null
|
|
191
|
+
])
|
|
192
|
+
);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// Resolve step content from named slot matching the current step ID
|
|
196
|
+
const stepSlot = slots[snap.stepId];
|
|
197
|
+
const stepContent = stepSlot ? stepSlot({ snapshot: snap }) : null;
|
|
198
|
+
|
|
199
|
+
return h("div", { class: "pw-shell" }, [
|
|
200
|
+
// Header — progress
|
|
201
|
+
!props.hideProgress && (
|
|
202
|
+
slots.header
|
|
203
|
+
? slots.header({ snapshot: snap })
|
|
204
|
+
: renderVueHeader(snap)
|
|
205
|
+
),
|
|
206
|
+
// Body — step content
|
|
207
|
+
h("div", { class: "pw-shell__body" }, stepContent ?? []),
|
|
208
|
+
// Footer — navigation
|
|
209
|
+
slots.footer
|
|
210
|
+
? slots.footer({ snapshot: snap, actions })
|
|
211
|
+
: renderVueFooter(snap, actions, props)
|
|
212
|
+
]);
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
// ---------------------------------------------------------------------------
|
|
218
|
+
// Default header (progress indicator)
|
|
219
|
+
// ---------------------------------------------------------------------------
|
|
220
|
+
|
|
221
|
+
function renderVueHeader(snapshot: PathSnapshot): VNode {
|
|
222
|
+
return h("div", { class: "pw-shell__header" }, [
|
|
223
|
+
h("div", { class: "pw-shell__steps" },
|
|
224
|
+
snapshot.steps.map((step, i) =>
|
|
225
|
+
h("div", {
|
|
226
|
+
key: step.id,
|
|
227
|
+
class: ["pw-shell__step", `pw-shell__step--${step.status}`]
|
|
228
|
+
}, [
|
|
229
|
+
h("span", { class: "pw-shell__step-dot" },
|
|
230
|
+
step.status === "completed" ? "✓" : String(i + 1)
|
|
231
|
+
),
|
|
232
|
+
h("span", { class: "pw-shell__step-label" },
|
|
233
|
+
step.title ?? step.id
|
|
234
|
+
)
|
|
235
|
+
])
|
|
236
|
+
)
|
|
237
|
+
),
|
|
238
|
+
h("div", { class: "pw-shell__track" },
|
|
239
|
+
h("div", {
|
|
240
|
+
class: "pw-shell__track-fill",
|
|
241
|
+
style: { width: `${snapshot.progress * 100}%` }
|
|
242
|
+
})
|
|
243
|
+
)
|
|
244
|
+
]);
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
// ---------------------------------------------------------------------------
|
|
248
|
+
// Default footer (navigation buttons)
|
|
249
|
+
// ---------------------------------------------------------------------------
|
|
250
|
+
|
|
251
|
+
function renderVueFooter(
|
|
252
|
+
snapshot: PathSnapshot,
|
|
253
|
+
actions: PathShellActions,
|
|
254
|
+
props: { backLabel: string; nextLabel: string; finishLabel: string; cancelLabel: string; hideCancel: boolean }
|
|
255
|
+
): VNode {
|
|
256
|
+
return h("div", { class: "pw-shell__footer" }, [
|
|
257
|
+
h("div", { class: "pw-shell__footer-left" }, [
|
|
258
|
+
!snapshot.isFirstStep
|
|
259
|
+
? h("button", {
|
|
260
|
+
type: "button",
|
|
261
|
+
class: "pw-shell__btn pw-shell__btn--back",
|
|
262
|
+
disabled: snapshot.isNavigating || !snapshot.canMovePrevious,
|
|
263
|
+
onClick: actions.previous
|
|
264
|
+
}, props.backLabel)
|
|
265
|
+
: null
|
|
266
|
+
]),
|
|
267
|
+
h("div", { class: "pw-shell__footer-right" }, [
|
|
268
|
+
!props.hideCancel
|
|
269
|
+
? h("button", {
|
|
270
|
+
type: "button",
|
|
271
|
+
class: "pw-shell__btn pw-shell__btn--cancel",
|
|
272
|
+
disabled: snapshot.isNavigating,
|
|
273
|
+
onClick: actions.cancel
|
|
274
|
+
}, props.cancelLabel)
|
|
275
|
+
: null,
|
|
276
|
+
h("button", {
|
|
277
|
+
type: "button",
|
|
278
|
+
class: "pw-shell__btn pw-shell__btn--next",
|
|
279
|
+
disabled: snapshot.isNavigating || !snapshot.canMoveNext,
|
|
280
|
+
onClick: actions.next
|
|
281
|
+
}, snapshot.isLastStep ? props.finishLabel : props.nextLabel)
|
|
282
|
+
])
|
|
283
|
+
]);
|
|
284
|
+
}
|
|
285
|
+
|