@object-ui/core 3.1.5 → 3.3.1
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/CHANGELOG.md +18 -0
- package/README.md +20 -1
- package/dist/actions/ActionRunner.d.ts +9 -0
- package/dist/actions/ActionRunner.js +41 -4
- package/dist/adapters/ValueDataSource.d.ts +5 -1
- package/dist/adapters/ValueDataSource.js +30 -1
- package/dist/errors/index.js +2 -3
- package/dist/evaluator/ExpressionCache.d.ts +9 -10
- package/dist/evaluator/ExpressionCache.js +29 -8
- package/dist/evaluator/SafeExpressionParser.d.ts +131 -0
- package/dist/evaluator/SafeExpressionParser.js +851 -0
- package/dist/evaluator/index.d.ts +1 -0
- package/dist/evaluator/index.js +1 -0
- package/dist/protocols/DndProtocol.js +2 -14
- package/dist/protocols/KeyboardProtocol.js +1 -4
- package/dist/protocols/NotificationProtocol.js +3 -13
- package/dist/utils/debug.js +2 -1
- package/dist/utils/filter-converter.js +25 -5
- package/package.json +33 -9
- package/.turbo/turbo-build.log +0 -4
- package/src/__benchmarks__/core.bench.ts +0 -64
- package/src/__tests__/protocols/DndProtocol.test.ts +0 -186
- package/src/__tests__/protocols/KeyboardProtocol.test.ts +0 -177
- package/src/__tests__/protocols/NotificationProtocol.test.ts +0 -142
- package/src/__tests__/protocols/ResponsiveProtocol.test.ts +0 -176
- package/src/__tests__/protocols/SharingProtocol.test.ts +0 -188
- package/src/actions/ActionEngine.ts +0 -268
- package/src/actions/ActionRunner.ts +0 -717
- package/src/actions/TransactionManager.ts +0 -521
- package/src/actions/UndoManager.ts +0 -215
- package/src/actions/__tests__/ActionEngine.test.ts +0 -206
- package/src/actions/__tests__/ActionRunner.params.test.ts +0 -134
- package/src/actions/__tests__/ActionRunner.test.ts +0 -711
- package/src/actions/__tests__/TransactionManager.test.ts +0 -447
- package/src/actions/__tests__/UndoManager.test.ts +0 -320
- package/src/actions/index.ts +0 -12
- package/src/adapters/ApiDataSource.ts +0 -376
- package/src/adapters/README.md +0 -180
- package/src/adapters/ValueDataSource.ts +0 -438
- package/src/adapters/__tests__/ApiDataSource.test.ts +0 -418
- package/src/adapters/__tests__/ValueDataSource.test.ts +0 -472
- package/src/adapters/__tests__/resolveDataSource.test.ts +0 -144
- package/src/adapters/index.ts +0 -15
- package/src/adapters/resolveDataSource.ts +0 -79
- package/src/builder/__tests__/schema-builder.test.ts +0 -235
- package/src/builder/schema-builder.ts +0 -584
- package/src/data-scope/DataScopeManager.ts +0 -269
- package/src/data-scope/ViewDataProvider.ts +0 -282
- package/src/data-scope/__tests__/DataScopeManager.test.ts +0 -211
- package/src/data-scope/__tests__/ViewDataProvider.test.ts +0 -270
- package/src/data-scope/index.ts +0 -24
- package/src/errors/__tests__/errors.test.ts +0 -292
- package/src/errors/index.ts +0 -270
- package/src/evaluator/ExpressionCache.ts +0 -192
- package/src/evaluator/ExpressionContext.ts +0 -118
- package/src/evaluator/ExpressionEvaluator.ts +0 -315
- package/src/evaluator/FormulaFunctions.ts +0 -398
- package/src/evaluator/__tests__/ExpressionCache.test.ts +0 -135
- package/src/evaluator/__tests__/ExpressionContext.test.ts +0 -110
- package/src/evaluator/__tests__/ExpressionEvaluator.test.ts +0 -131
- package/src/evaluator/__tests__/FormulaFunctions.test.ts +0 -447
- package/src/evaluator/index.ts +0 -12
- package/src/index.ts +0 -38
- package/src/protocols/DndProtocol.ts +0 -184
- package/src/protocols/KeyboardProtocol.ts +0 -185
- package/src/protocols/NotificationProtocol.ts +0 -159
- package/src/protocols/ResponsiveProtocol.ts +0 -210
- package/src/protocols/SharingProtocol.ts +0 -185
- package/src/protocols/index.ts +0 -13
- package/src/query/__tests__/query-ast.test.ts +0 -211
- package/src/query/__tests__/window-functions.test.ts +0 -275
- package/src/query/index.ts +0 -7
- package/src/query/query-ast.ts +0 -341
- package/src/registry/PluginScopeImpl.ts +0 -259
- package/src/registry/PluginSystem.ts +0 -206
- package/src/registry/Registry.ts +0 -219
- package/src/registry/WidgetRegistry.ts +0 -316
- package/src/registry/__tests__/PluginSystem.test.ts +0 -309
- package/src/registry/__tests__/Registry.test.ts +0 -293
- package/src/registry/__tests__/WidgetRegistry.test.ts +0 -321
- package/src/registry/__tests__/plugin-scope-integration.test.ts +0 -283
- package/src/theme/ThemeEngine.ts +0 -530
- package/src/theme/__tests__/ThemeEngine.test.ts +0 -668
- package/src/theme/index.ts +0 -24
- package/src/types/index.ts +0 -21
- package/src/utils/__tests__/debug-collector.test.ts +0 -102
- package/src/utils/__tests__/debug.test.ts +0 -134
- package/src/utils/__tests__/expand-fields.test.ts +0 -120
- package/src/utils/__tests__/extract-records.test.ts +0 -50
- package/src/utils/__tests__/filter-converter.test.ts +0 -118
- package/src/utils/__tests__/merge-views-into-objects.test.ts +0 -110
- package/src/utils/__tests__/normalize-quick-filter.test.ts +0 -123
- package/src/utils/debug-collector.ts +0 -100
- package/src/utils/debug.ts +0 -147
- package/src/utils/expand-fields.ts +0 -76
- package/src/utils/extract-records.ts +0 -33
- package/src/utils/filter-converter.ts +0 -133
- package/src/utils/merge-views-into-objects.ts +0 -36
- package/src/utils/normalize-quick-filter.ts +0 -78
- package/src/validation/__tests__/object-validation-engine.test.ts +0 -567
- package/src/validation/__tests__/schema-validator.test.ts +0 -118
- package/src/validation/__tests__/validation-engine.test.ts +0 -102
- package/src/validation/index.ts +0 -10
- package/src/validation/schema-validator.ts +0 -344
- package/src/validation/validation-engine.ts +0 -528
- package/src/validation/validators/index.ts +0 -25
- package/src/validation/validators/object-validation-engine.ts +0 -722
- package/tsconfig.json +0 -15
- package/tsconfig.tsbuildinfo +0 -1
- package/vitest.config.ts +0 -2
|
@@ -1,215 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* ObjectUI
|
|
3
|
-
* Copyright (c) 2024-present ObjectStack Inc.
|
|
4
|
-
*
|
|
5
|
-
* This source code is licensed under the MIT license found in the
|
|
6
|
-
* LICENSE file in the root directory of this source tree.
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* @object-ui/core - Undo Manager
|
|
11
|
-
*
|
|
12
|
-
* Global undo/redo system for CRUD operations. Class-based singleton
|
|
13
|
-
* pattern for use from ActionRunner, React hooks, and standalone contexts.
|
|
14
|
-
* Maintains bounded history stacks with subscriber notifications.
|
|
15
|
-
*/
|
|
16
|
-
|
|
17
|
-
/** Represents a single undoable CRUD operation. */
|
|
18
|
-
export interface UndoableOperation {
|
|
19
|
-
id: string;
|
|
20
|
-
type: 'create' | 'update' | 'delete';
|
|
21
|
-
objectName: string;
|
|
22
|
-
recordId: string;
|
|
23
|
-
timestamp: number;
|
|
24
|
-
description: string;
|
|
25
|
-
/** Data needed to undo: for create=recordId, for update=previousData, for delete=fullRecord */
|
|
26
|
-
undoData: Record<string, unknown>;
|
|
27
|
-
/** Data needed to redo: for create=newData, for update=newData, for delete=recordId */
|
|
28
|
-
redoData: Record<string, unknown>;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export interface UndoManagerOptions {
|
|
32
|
-
/** Maximum number of operations to retain in history. @default 50 */
|
|
33
|
-
maxHistory?: number;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/** Type guard validating the required shape of a persisted UndoableOperation. */
|
|
37
|
-
function isValidOperation(op: unknown): op is UndoableOperation {
|
|
38
|
-
if (typeof op !== 'object' || op === null) return false;
|
|
39
|
-
const o = op as Record<string, unknown>;
|
|
40
|
-
return (
|
|
41
|
-
typeof o.id === 'string' &&
|
|
42
|
-
typeof o.type === 'string' &&
|
|
43
|
-
typeof o.objectName === 'string' &&
|
|
44
|
-
typeof o.recordId === 'string' &&
|
|
45
|
-
typeof o.timestamp === 'number'
|
|
46
|
-
);
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* Manages undo/redo stacks for CRUD operations.
|
|
51
|
-
*
|
|
52
|
-
* Designed as a framework-agnostic class so it can be used from
|
|
53
|
-
* ActionRunner, React hooks, or any other consumer.
|
|
54
|
-
*/
|
|
55
|
-
export class UndoManager {
|
|
56
|
-
private undoStack: UndoableOperation[] = [];
|
|
57
|
-
private redoStack: UndoableOperation[] = [];
|
|
58
|
-
private readonly maxHistory: number;
|
|
59
|
-
private listeners: Set<() => void> = new Set();
|
|
60
|
-
|
|
61
|
-
constructor(options?: UndoManagerOptions) {
|
|
62
|
-
this.maxHistory = options?.maxHistory ?? 50;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
/** Record an undoable operation. Clears the redo stack. */
|
|
66
|
-
push(operation: UndoableOperation): void {
|
|
67
|
-
this.undoStack.push(operation);
|
|
68
|
-
if (this.undoStack.length > this.maxHistory) {
|
|
69
|
-
this.undoStack.shift();
|
|
70
|
-
}
|
|
71
|
-
this.redoStack = [];
|
|
72
|
-
this.notify();
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
/** Peek at the next undoable operation without removing it. */
|
|
76
|
-
peekUndo(): UndoableOperation | undefined {
|
|
77
|
-
return this.undoStack[this.undoStack.length - 1];
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
/** Peek at the next redoable operation without removing it. */
|
|
81
|
-
peekRedo(): UndoableOperation | undefined {
|
|
82
|
-
return this.redoStack[this.redoStack.length - 1];
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
/** Pop the last operation from the undo stack and move it to redo. */
|
|
86
|
-
popUndo(): UndoableOperation | undefined {
|
|
87
|
-
const op = this.undoStack.pop();
|
|
88
|
-
if (op) {
|
|
89
|
-
this.redoStack.push(op);
|
|
90
|
-
this.notify();
|
|
91
|
-
}
|
|
92
|
-
return op;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
/** Pop the last operation from the redo stack and move it to undo. */
|
|
96
|
-
popRedo(): UndoableOperation | undefined {
|
|
97
|
-
const op = this.redoStack.pop();
|
|
98
|
-
if (op) {
|
|
99
|
-
this.undoStack.push(op);
|
|
100
|
-
this.notify();
|
|
101
|
-
}
|
|
102
|
-
return op;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
/** Clear all undo and redo history. */
|
|
106
|
-
clear(): void {
|
|
107
|
-
this.undoStack = [];
|
|
108
|
-
this.redoStack = [];
|
|
109
|
-
this.notify();
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
get canUndo(): boolean { return this.undoStack.length > 0; }
|
|
113
|
-
get canRedo(): boolean { return this.redoStack.length > 0; }
|
|
114
|
-
get undoCount(): number { return this.undoStack.length; }
|
|
115
|
-
get redoCount(): number { return this.redoStack.length; }
|
|
116
|
-
|
|
117
|
-
/** Subscribe to state changes. Returns an unsubscribe function. */
|
|
118
|
-
subscribe(listener: () => void): () => void {
|
|
119
|
-
this.listeners.add(listener);
|
|
120
|
-
return () => this.listeners.delete(listener);
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
/** Get a shallow copy of the undo history (for developer tools). */
|
|
124
|
-
getHistory(): UndoableOperation[] { return [...this.undoStack]; }
|
|
125
|
-
|
|
126
|
-
/** Get a shallow copy of the redo history (for developer tools). */
|
|
127
|
-
getRedoHistory(): UndoableOperation[] { return [...this.redoStack]; }
|
|
128
|
-
|
|
129
|
-
// ---------------------------------------------------------------------------
|
|
130
|
-
// Batch operations
|
|
131
|
-
// ---------------------------------------------------------------------------
|
|
132
|
-
|
|
133
|
-
/** Push multiple operations as one atomic unit. Clears the redo stack. */
|
|
134
|
-
pushBatch(operations: UndoableOperation[]): void {
|
|
135
|
-
if (operations.length === 0) return;
|
|
136
|
-
this.undoStack.push(...operations);
|
|
137
|
-
// Trim from the front if we exceed maxHistory
|
|
138
|
-
if (this.undoStack.length > this.maxHistory) {
|
|
139
|
-
this.undoStack.splice(0, this.undoStack.length - this.maxHistory);
|
|
140
|
-
}
|
|
141
|
-
this.redoStack = [];
|
|
142
|
-
this.notify();
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
/** Pop `count` operations from the undo stack and move them to redo (LIFO order). */
|
|
146
|
-
popUndoBatch(count: number): UndoableOperation[] {
|
|
147
|
-
const actual = Math.min(count, this.undoStack.length);
|
|
148
|
-
if (actual === 0) return [];
|
|
149
|
-
const ops = this.undoStack.splice(this.undoStack.length - actual, actual);
|
|
150
|
-
// Preserve LIFO order on the redo stack (last undone goes on top)
|
|
151
|
-
this.redoStack.push(...ops);
|
|
152
|
-
this.notify();
|
|
153
|
-
return ops;
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
/** Pop `count` operations from the redo stack and move them to undo (LIFO order). */
|
|
157
|
-
popRedoBatch(count: number): UndoableOperation[] {
|
|
158
|
-
const actual = Math.min(count, this.redoStack.length);
|
|
159
|
-
if (actual === 0) return [];
|
|
160
|
-
const ops = this.redoStack.splice(this.redoStack.length - actual, actual);
|
|
161
|
-
this.undoStack.push(...ops);
|
|
162
|
-
this.notify();
|
|
163
|
-
return ops;
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
// ---------------------------------------------------------------------------
|
|
167
|
-
// Persistence (localStorage)
|
|
168
|
-
// ---------------------------------------------------------------------------
|
|
169
|
-
|
|
170
|
-
private static readonly STORAGE_KEY = 'objectui:undo-history';
|
|
171
|
-
|
|
172
|
-
/** Persist the current undo/redo stacks to localStorage. */
|
|
173
|
-
saveToStorage(): void {
|
|
174
|
-
try {
|
|
175
|
-
const payload = JSON.stringify({
|
|
176
|
-
undoStack: this.undoStack,
|
|
177
|
-
redoStack: this.redoStack,
|
|
178
|
-
});
|
|
179
|
-
localStorage.setItem(UndoManager.STORAGE_KEY, payload);
|
|
180
|
-
} catch {
|
|
181
|
-
// localStorage may be unavailable (SSR, quota exceeded, etc.)
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
/** Restore undo/redo stacks from localStorage (no-op when unavailable). */
|
|
186
|
-
loadFromStorage(): void {
|
|
187
|
-
try {
|
|
188
|
-
if (typeof localStorage === 'undefined') return;
|
|
189
|
-
const raw = localStorage.getItem(UndoManager.STORAGE_KEY);
|
|
190
|
-
if (!raw) return;
|
|
191
|
-
const parsed = JSON.parse(raw) as {
|
|
192
|
-
undoStack?: UndoableOperation[];
|
|
193
|
-
redoStack?: UndoableOperation[];
|
|
194
|
-
};
|
|
195
|
-
if (Array.isArray(parsed.undoStack)) {
|
|
196
|
-
this.undoStack = parsed.undoStack.filter(isValidOperation);
|
|
197
|
-
}
|
|
198
|
-
if (Array.isArray(parsed.redoStack)) {
|
|
199
|
-
this.redoStack = parsed.redoStack.filter(isValidOperation);
|
|
200
|
-
}
|
|
201
|
-
// Enforce maxHistory in case persisted state used a different limit
|
|
202
|
-
if (this.undoStack.length > this.maxHistory) {
|
|
203
|
-
this.undoStack.splice(0, this.undoStack.length - this.maxHistory);
|
|
204
|
-
}
|
|
205
|
-
this.notify();
|
|
206
|
-
} catch {
|
|
207
|
-
// Silently ignore parse errors or missing storage
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
private notify(): void { this.listeners.forEach((fn) => fn()); }
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
/** Global singleton instance. */
|
|
215
|
-
export const globalUndoManager = new UndoManager();
|
|
@@ -1,206 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* ObjectUI
|
|
3
|
-
* Copyright (c) 2024-present ObjectStack Inc.
|
|
4
|
-
*
|
|
5
|
-
* This source code is licensed under the MIT license found in the
|
|
6
|
-
* LICENSE file in the root directory of this source tree.
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
import { describe, it, expect, beforeEach } from 'vitest';
|
|
10
|
-
import { ActionEngine } from '../ActionEngine';
|
|
11
|
-
import type { ActionDef } from '../ActionRunner';
|
|
12
|
-
|
|
13
|
-
describe('ActionEngine', () => {
|
|
14
|
-
let engine: ActionEngine;
|
|
15
|
-
|
|
16
|
-
beforeEach(() => {
|
|
17
|
-
engine = new ActionEngine({ data: { status: 'active' } });
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
describe('registerAction', () => {
|
|
21
|
-
it('registers an action by name', () => {
|
|
22
|
-
engine.registerAction({ name: 'save', type: 'api', endpoint: '/save' });
|
|
23
|
-
expect(engine.getAction('save')).toBeDefined();
|
|
24
|
-
expect(engine.getAction('save')?.type).toBe('api');
|
|
25
|
-
});
|
|
26
|
-
|
|
27
|
-
it('throws if action has no name or type', () => {
|
|
28
|
-
expect(() => engine.registerAction({} as ActionDef)).toThrow('Action must have a name or type');
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
it('registers with locations', () => {
|
|
32
|
-
engine.registerAction(
|
|
33
|
-
{ name: 'delete', type: 'api' },
|
|
34
|
-
{ locations: ['record_header', 'list_item'] }
|
|
35
|
-
);
|
|
36
|
-
expect(engine.getActionsForLocation('record_header')).toHaveLength(1);
|
|
37
|
-
expect(engine.getActionsForLocation('list_toolbar')).toHaveLength(0);
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
it('auto-registers keyboard shortcuts', () => {
|
|
41
|
-
engine.registerAction(
|
|
42
|
-
{ name: 'save', type: 'api' },
|
|
43
|
-
{ shortcut: 'ctrl+s' }
|
|
44
|
-
);
|
|
45
|
-
expect(engine.getShortcuts()).toHaveLength(1);
|
|
46
|
-
expect(engine.getShortcuts()[0].keys).toBe('ctrl+s');
|
|
47
|
-
});
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
describe('registerActions', () => {
|
|
51
|
-
it('registers multiple actions from array', () => {
|
|
52
|
-
engine.registerActions([
|
|
53
|
-
{ name: 'save', type: 'api', endpoint: '/save' },
|
|
54
|
-
{ name: 'delete', type: 'api', endpoint: '/delete' },
|
|
55
|
-
]);
|
|
56
|
-
expect(engine.getAction('save')).toBeDefined();
|
|
57
|
-
expect(engine.getAction('delete')).toBeDefined();
|
|
58
|
-
});
|
|
59
|
-
});
|
|
60
|
-
|
|
61
|
-
describe('unregisterAction', () => {
|
|
62
|
-
it('removes action and its shortcuts/mappings', () => {
|
|
63
|
-
engine.registerAction({ name: 'save', type: 'api' }, { shortcut: 'ctrl+s' });
|
|
64
|
-
engine.addMapping({ event: 'toolbar:save', actionName: 'save' });
|
|
65
|
-
|
|
66
|
-
engine.unregisterAction('save');
|
|
67
|
-
|
|
68
|
-
expect(engine.getAction('save')).toBeUndefined();
|
|
69
|
-
expect(engine.getShortcuts()).toHaveLength(0);
|
|
70
|
-
});
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
describe('getActionsForLocation', () => {
|
|
74
|
-
it('returns actions sorted by priority', () => {
|
|
75
|
-
engine.registerAction({ name: 'delete', type: 'api' }, { locations: ['list_toolbar'], priority: 200 });
|
|
76
|
-
engine.registerAction({ name: 'create', type: 'modal' }, { locations: ['list_toolbar'], priority: 10 });
|
|
77
|
-
engine.registerAction({ name: 'export', type: 'api' }, { locations: ['list_toolbar'], priority: 100 });
|
|
78
|
-
|
|
79
|
-
const actions = engine.getActionsForLocation('list_toolbar');
|
|
80
|
-
expect(actions).toHaveLength(3);
|
|
81
|
-
expect(actions[0].name).toBe('create');
|
|
82
|
-
expect(actions[1].name).toBe('export');
|
|
83
|
-
expect(actions[2].name).toBe('delete');
|
|
84
|
-
});
|
|
85
|
-
});
|
|
86
|
-
|
|
87
|
-
describe('getBulkActions', () => {
|
|
88
|
-
it('returns only bulk-enabled actions', () => {
|
|
89
|
-
engine.registerAction({ name: 'delete', type: 'api' }, { bulkEnabled: true });
|
|
90
|
-
engine.registerAction({ name: 'edit', type: 'modal' }, { bulkEnabled: false });
|
|
91
|
-
|
|
92
|
-
expect(engine.getBulkActions()).toHaveLength(1);
|
|
93
|
-
expect(engine.getBulkActions()[0].name).toBe('delete');
|
|
94
|
-
});
|
|
95
|
-
});
|
|
96
|
-
|
|
97
|
-
describe('dispatch', () => {
|
|
98
|
-
it('executes mapped actions for an event', async () => {
|
|
99
|
-
engine.registerAction({ name: 'log', type: 'script', execute: '"logged"' });
|
|
100
|
-
engine.addMapping({ event: 'row:click', actionName: 'log' });
|
|
101
|
-
|
|
102
|
-
const results = await engine.dispatch('row:click');
|
|
103
|
-
expect(results).toHaveLength(1);
|
|
104
|
-
expect(results[0].success).toBe(true);
|
|
105
|
-
});
|
|
106
|
-
|
|
107
|
-
it('returns empty array for unmapped events', async () => {
|
|
108
|
-
const results = await engine.dispatch('unknown:event');
|
|
109
|
-
expect(results).toEqual([]);
|
|
110
|
-
});
|
|
111
|
-
|
|
112
|
-
it('skips actions when mapping condition is false', async () => {
|
|
113
|
-
engine = new ActionEngine({ data: { status: 'locked' } });
|
|
114
|
-
engine.registerAction({ name: 'edit', type: 'script', execute: '"edited"' });
|
|
115
|
-
engine.addMapping({
|
|
116
|
-
event: 'row:click',
|
|
117
|
-
actionName: 'edit',
|
|
118
|
-
condition: '${data.status === "active"}'
|
|
119
|
-
});
|
|
120
|
-
|
|
121
|
-
const results = await engine.dispatch('row:click');
|
|
122
|
-
expect(results).toHaveLength(0);
|
|
123
|
-
});
|
|
124
|
-
});
|
|
125
|
-
|
|
126
|
-
describe('handleShortcut', () => {
|
|
127
|
-
it('executes action for matching shortcut', async () => {
|
|
128
|
-
engine.registerAction(
|
|
129
|
-
{ name: 'save', type: 'script', execute: '"saved"' },
|
|
130
|
-
{ shortcut: 'ctrl+s' }
|
|
131
|
-
);
|
|
132
|
-
|
|
133
|
-
const result = await engine.handleShortcut('ctrl+s');
|
|
134
|
-
expect(result).not.toBeNull();
|
|
135
|
-
expect(result?.success).toBe(true);
|
|
136
|
-
});
|
|
137
|
-
|
|
138
|
-
it('normalizes shortcut key order', async () => {
|
|
139
|
-
engine.registerAction(
|
|
140
|
-
{ name: 'save', type: 'script', execute: '"saved"' },
|
|
141
|
-
{ shortcut: 'ctrl+shift+s' }
|
|
142
|
-
);
|
|
143
|
-
|
|
144
|
-
// Different order should still match
|
|
145
|
-
const result = await engine.handleShortcut('shift+ctrl+s');
|
|
146
|
-
expect(result).not.toBeNull();
|
|
147
|
-
expect(result?.success).toBe(true);
|
|
148
|
-
});
|
|
149
|
-
|
|
150
|
-
it('returns null for unregistered shortcut', async () => {
|
|
151
|
-
const result = await engine.handleShortcut('ctrl+z');
|
|
152
|
-
expect(result).toBeNull();
|
|
153
|
-
});
|
|
154
|
-
});
|
|
155
|
-
|
|
156
|
-
describe('executeBulk', () => {
|
|
157
|
-
it('executes action on multiple records sequentially', async () => {
|
|
158
|
-
engine.registerAction(
|
|
159
|
-
{ name: 'approve', type: 'script', execute: '"approved"' },
|
|
160
|
-
{ bulkEnabled: true }
|
|
161
|
-
);
|
|
162
|
-
|
|
163
|
-
const result = await engine.executeBulk('approve', [
|
|
164
|
-
{ id: '1', name: 'Record 1' },
|
|
165
|
-
{ id: '2', name: 'Record 2' },
|
|
166
|
-
]);
|
|
167
|
-
|
|
168
|
-
expect(result.total).toBe(2);
|
|
169
|
-
expect(result.succeeded).toBe(2);
|
|
170
|
-
expect(result.failed).toBe(0);
|
|
171
|
-
});
|
|
172
|
-
|
|
173
|
-
it('fails if action is not bulk-enabled', async () => {
|
|
174
|
-
engine.registerAction({ name: 'edit', type: 'modal' }, { bulkEnabled: false });
|
|
175
|
-
|
|
176
|
-
const result = await engine.executeBulk('edit', [{ id: '1' }]);
|
|
177
|
-
expect(result.results[0].success).toBe(false);
|
|
178
|
-
expect(result.results[0].error).toContain('does not support bulk');
|
|
179
|
-
});
|
|
180
|
-
|
|
181
|
-
it('fails if action is not found', async () => {
|
|
182
|
-
const result = await engine.executeBulk('nonexistent', [{ id: '1' }]);
|
|
183
|
-
expect(result.results[0].success).toBe(false);
|
|
184
|
-
});
|
|
185
|
-
});
|
|
186
|
-
|
|
187
|
-
describe('executeAction', () => {
|
|
188
|
-
it('returns error for unknown action', async () => {
|
|
189
|
-
const result = await engine.executeAction('nonexistent');
|
|
190
|
-
expect(result.success).toBe(false);
|
|
191
|
-
expect(result.error).toContain('Action not found');
|
|
192
|
-
});
|
|
193
|
-
});
|
|
194
|
-
|
|
195
|
-
describe('clear', () => {
|
|
196
|
-
it('removes all actions, mappings, and shortcuts', () => {
|
|
197
|
-
engine.registerAction({ name: 'save', type: 'api' }, { shortcut: 'ctrl+s' });
|
|
198
|
-
engine.addMapping({ event: 'test', actionName: 'save' });
|
|
199
|
-
|
|
200
|
-
engine.clear();
|
|
201
|
-
|
|
202
|
-
expect(engine.getAction('save')).toBeUndefined();
|
|
203
|
-
expect(engine.getShortcuts()).toHaveLength(0);
|
|
204
|
-
});
|
|
205
|
-
});
|
|
206
|
-
});
|
|
@@ -1,134 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* ObjectUI
|
|
3
|
-
* Copyright (c) 2024-present ObjectStack Inc.
|
|
4
|
-
*
|
|
5
|
-
* This source code is licensed under the MIT license found in the
|
|
6
|
-
* LICENSE file in the root directory of this source tree.
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
10
|
-
import { ActionRunner, type ActionContext } from '../ActionRunner';
|
|
11
|
-
|
|
12
|
-
describe('ActionRunner — ParamCollectionHandler', () => {
|
|
13
|
-
let runner: ActionRunner;
|
|
14
|
-
let context: ActionContext;
|
|
15
|
-
|
|
16
|
-
beforeEach(() => {
|
|
17
|
-
context = {
|
|
18
|
-
data: { id: 1, name: 'Test' },
|
|
19
|
-
record: { id: 1, status: 'active' },
|
|
20
|
-
user: { id: 'u1', role: 'admin' },
|
|
21
|
-
};
|
|
22
|
-
runner = new ActionRunner(context);
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
// ==========================================================================
|
|
26
|
-
// Param collection before execution
|
|
27
|
-
// ==========================================================================
|
|
28
|
-
|
|
29
|
-
describe('param collection', () => {
|
|
30
|
-
it('should call paramCollectionHandler when actionParams are defined', async () => {
|
|
31
|
-
const paramHandler = vi.fn().mockResolvedValue({ owner_email: 'alice@example.com' });
|
|
32
|
-
runner.setParamCollectionHandler(paramHandler);
|
|
33
|
-
|
|
34
|
-
const onClick = vi.fn();
|
|
35
|
-
await runner.execute({
|
|
36
|
-
onClick,
|
|
37
|
-
actionParams: [
|
|
38
|
-
{ name: 'owner_email', label: 'Owner Email', type: 'text', required: true },
|
|
39
|
-
],
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
expect(paramHandler).toHaveBeenCalledOnce();
|
|
43
|
-
expect(paramHandler).toHaveBeenCalledWith([
|
|
44
|
-
{ name: 'owner_email', label: 'Owner Email', type: 'text', required: true },
|
|
45
|
-
]);
|
|
46
|
-
expect(onClick).toHaveBeenCalledOnce();
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
it('should cancel action when paramCollectionHandler returns null', async () => {
|
|
50
|
-
const paramHandler = vi.fn().mockResolvedValue(null);
|
|
51
|
-
runner.setParamCollectionHandler(paramHandler);
|
|
52
|
-
|
|
53
|
-
const onClick = vi.fn();
|
|
54
|
-
const result = await runner.execute({
|
|
55
|
-
onClick,
|
|
56
|
-
actionParams: [
|
|
57
|
-
{ name: 'owner_email', label: 'Owner Email', type: 'text', required: true },
|
|
58
|
-
],
|
|
59
|
-
});
|
|
60
|
-
|
|
61
|
-
expect(paramHandler).toHaveBeenCalledOnce();
|
|
62
|
-
expect(result.success).toBe(false);
|
|
63
|
-
expect(result.error).toBe('Action cancelled by user (params)');
|
|
64
|
-
expect(onClick).not.toHaveBeenCalled();
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
it('should merge collected params into action.params', async () => {
|
|
68
|
-
const paramHandler = vi.fn().mockResolvedValue({
|
|
69
|
-
owner_email: 'alice@example.com',
|
|
70
|
-
notify: true,
|
|
71
|
-
});
|
|
72
|
-
runner.setParamCollectionHandler(paramHandler);
|
|
73
|
-
|
|
74
|
-
const handler = vi.fn().mockResolvedValue({ success: true });
|
|
75
|
-
runner.registerHandler('custom_action', handler);
|
|
76
|
-
|
|
77
|
-
await runner.execute({
|
|
78
|
-
type: 'custom_action',
|
|
79
|
-
params: { existing: 'value' },
|
|
80
|
-
actionParams: [
|
|
81
|
-
{ name: 'owner_email', label: 'Owner Email', type: 'text' },
|
|
82
|
-
{ name: 'notify', label: 'Notify', type: 'boolean' },
|
|
83
|
-
],
|
|
84
|
-
});
|
|
85
|
-
|
|
86
|
-
expect(handler).toHaveBeenCalledOnce();
|
|
87
|
-
const calledAction = handler.mock.calls[0][0];
|
|
88
|
-
expect(calledAction.params).toEqual({
|
|
89
|
-
existing: 'value',
|
|
90
|
-
owner_email: 'alice@example.com',
|
|
91
|
-
notify: true,
|
|
92
|
-
});
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
it('should skip param collection when no paramCollectionHandler is set', async () => {
|
|
96
|
-
const onClick = vi.fn();
|
|
97
|
-
const result = await runner.execute({
|
|
98
|
-
onClick,
|
|
99
|
-
actionParams: [
|
|
100
|
-
{ name: 'test', label: 'Test', type: 'text' },
|
|
101
|
-
],
|
|
102
|
-
});
|
|
103
|
-
|
|
104
|
-
// Should proceed normally without param collection
|
|
105
|
-
expect(result.success).toBe(true);
|
|
106
|
-
expect(onClick).toHaveBeenCalledOnce();
|
|
107
|
-
});
|
|
108
|
-
|
|
109
|
-
it('should skip param collection when actionParams is empty', async () => {
|
|
110
|
-
const paramHandler = vi.fn();
|
|
111
|
-
runner.setParamCollectionHandler(paramHandler);
|
|
112
|
-
|
|
113
|
-
const onClick = vi.fn();
|
|
114
|
-
await runner.execute({
|
|
115
|
-
onClick,
|
|
116
|
-
actionParams: [],
|
|
117
|
-
});
|
|
118
|
-
|
|
119
|
-
expect(paramHandler).not.toHaveBeenCalled();
|
|
120
|
-
expect(onClick).toHaveBeenCalledOnce();
|
|
121
|
-
});
|
|
122
|
-
|
|
123
|
-
it('should skip param collection when actionParams is not provided', async () => {
|
|
124
|
-
const paramHandler = vi.fn();
|
|
125
|
-
runner.setParamCollectionHandler(paramHandler);
|
|
126
|
-
|
|
127
|
-
const onClick = vi.fn();
|
|
128
|
-
await runner.execute({ onClick });
|
|
129
|
-
|
|
130
|
-
expect(paramHandler).not.toHaveBeenCalled();
|
|
131
|
-
expect(onClick).toHaveBeenCalledOnce();
|
|
132
|
-
});
|
|
133
|
-
});
|
|
134
|
-
});
|