@object-ui/core 3.0.2 → 3.1.0
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/.turbo/turbo-build.log +1 -1
- package/CHANGELOG.md +6 -0
- package/dist/actions/ActionEngine.d.ts +98 -0
- package/dist/actions/ActionEngine.js +222 -0
- package/dist/actions/UndoManager.d.ts +80 -0
- package/dist/actions/UndoManager.js +193 -0
- package/dist/actions/index.d.ts +2 -0
- package/dist/actions/index.js +2 -0
- package/dist/adapters/ApiDataSource.d.ts +2 -1
- package/dist/adapters/ApiDataSource.js +25 -0
- package/dist/adapters/ValueDataSource.d.ts +2 -1
- package/dist/adapters/ValueDataSource.js +99 -3
- package/dist/data-scope/ViewDataProvider.d.ts +143 -0
- package/dist/data-scope/ViewDataProvider.js +153 -0
- package/dist/data-scope/index.d.ts +1 -0
- package/dist/data-scope/index.js +1 -0
- package/dist/evaluator/ExpressionEvaluator.d.ts +7 -0
- package/dist/evaluator/ExpressionEvaluator.js +19 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +5 -0
- package/dist/protocols/DndProtocol.d.ts +84 -0
- package/dist/protocols/DndProtocol.js +113 -0
- package/dist/protocols/KeyboardProtocol.d.ts +93 -0
- package/dist/protocols/KeyboardProtocol.js +108 -0
- package/dist/protocols/NotificationProtocol.d.ts +71 -0
- package/dist/protocols/NotificationProtocol.js +99 -0
- package/dist/protocols/ResponsiveProtocol.d.ts +73 -0
- package/dist/protocols/ResponsiveProtocol.js +158 -0
- package/dist/protocols/SharingProtocol.d.ts +71 -0
- package/dist/protocols/SharingProtocol.js +124 -0
- package/dist/protocols/index.d.ts +12 -0
- package/dist/protocols/index.js +12 -0
- package/dist/utils/debug-collector.d.ts +59 -0
- package/dist/utils/debug-collector.js +73 -0
- package/dist/utils/debug.d.ts +37 -2
- package/dist/utils/debug.js +62 -3
- package/dist/utils/expand-fields.d.ts +40 -0
- package/dist/utils/expand-fields.js +68 -0
- package/dist/utils/extract-records.d.ts +16 -0
- package/dist/utils/extract-records.js +32 -0
- package/dist/utils/normalize-quick-filter.d.ts +29 -0
- package/dist/utils/normalize-quick-filter.js +66 -0
- package/package.json +3 -3
- package/src/__tests__/protocols/DndProtocol.test.ts +186 -0
- package/src/__tests__/protocols/KeyboardProtocol.test.ts +177 -0
- package/src/__tests__/protocols/NotificationProtocol.test.ts +142 -0
- package/src/__tests__/protocols/ResponsiveProtocol.test.ts +176 -0
- package/src/__tests__/protocols/SharingProtocol.test.ts +188 -0
- package/src/actions/ActionEngine.ts +268 -0
- package/src/actions/UndoManager.ts +215 -0
- package/src/actions/__tests__/ActionEngine.test.ts +206 -0
- package/src/actions/__tests__/UndoManager.test.ts +320 -0
- package/src/actions/index.ts +2 -0
- package/src/adapters/ApiDataSource.ts +27 -0
- package/src/adapters/ValueDataSource.ts +109 -3
- package/src/adapters/__tests__/ValueDataSource.test.ts +147 -0
- package/src/data-scope/ViewDataProvider.ts +282 -0
- package/src/data-scope/__tests__/ViewDataProvider.test.ts +270 -0
- package/src/data-scope/index.ts +8 -0
- package/src/evaluator/ExpressionEvaluator.ts +22 -0
- package/src/evaluator/__tests__/ExpressionEvaluator.test.ts +31 -1
- package/src/index.ts +5 -0
- package/src/protocols/DndProtocol.ts +184 -0
- package/src/protocols/KeyboardProtocol.ts +185 -0
- package/src/protocols/NotificationProtocol.ts +159 -0
- package/src/protocols/ResponsiveProtocol.ts +210 -0
- package/src/protocols/SharingProtocol.ts +185 -0
- package/src/{index.test.ts → protocols/index.ts} +5 -7
- package/src/utils/__tests__/debug-collector.test.ts +102 -0
- package/src/utils/__tests__/debug.test.ts +52 -1
- package/src/utils/__tests__/expand-fields.test.ts +120 -0
- package/src/utils/__tests__/extract-records.test.ts +50 -0
- package/src/utils/__tests__/normalize-quick-filter.test.ts +123 -0
- package/src/utils/debug-collector.ts +100 -0
- package/src/utils/debug.ts +87 -6
- package/src/utils/expand-fields.ts +76 -0
- package/src/utils/extract-records.ts +33 -0
- package/src/utils/normalize-quick-filter.ts +78 -0
- package/tsconfig.tsbuildinfo +1 -1
|
@@ -0,0 +1,184 @@
|
|
|
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 - DnD Protocol Bridge
|
|
11
|
+
*
|
|
12
|
+
* Converts spec-aligned DnD configuration schemas into runtime-usable
|
|
13
|
+
* component props and CSS constraint styles for drag-and-drop interactions.
|
|
14
|
+
*
|
|
15
|
+
* @module protocols/DndProtocol
|
|
16
|
+
* @packageDocumentation
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import type { DndConfig, DragItem, DropZone, DragConstraint } from '@object-ui/types';
|
|
20
|
+
|
|
21
|
+
// ============================================================================
|
|
22
|
+
// Resolved Types
|
|
23
|
+
// ============================================================================
|
|
24
|
+
|
|
25
|
+
/** Fully resolved DnD configuration with all defaults applied. */
|
|
26
|
+
export interface ResolvedDndConfig {
|
|
27
|
+
enabled: boolean;
|
|
28
|
+
sortable: boolean;
|
|
29
|
+
autoScroll: boolean;
|
|
30
|
+
touchDelay: number;
|
|
31
|
+
dragItem?: DragItem;
|
|
32
|
+
dropZone?: DropZone;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** Component props for a draggable element. */
|
|
36
|
+
export interface DragItemProps {
|
|
37
|
+
draggable: boolean;
|
|
38
|
+
'aria-roledescription': string;
|
|
39
|
+
'aria-label'?: string;
|
|
40
|
+
'aria-describedby'?: string;
|
|
41
|
+
role: string;
|
|
42
|
+
'data-drag-type': string;
|
|
43
|
+
'data-drag-handle': string;
|
|
44
|
+
'data-drag-disabled': string;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** Component props for a droppable area. */
|
|
48
|
+
export interface DropZoneProps {
|
|
49
|
+
'aria-dropeffect': string;
|
|
50
|
+
'aria-label'?: string;
|
|
51
|
+
'aria-describedby'?: string;
|
|
52
|
+
role: string;
|
|
53
|
+
'data-drop-accept': string;
|
|
54
|
+
'data-drop-max-items'?: number;
|
|
55
|
+
'data-drop-highlight': string;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** CSS constraint styles for drag movement. */
|
|
59
|
+
export interface DragConstraintStyles {
|
|
60
|
+
position: string;
|
|
61
|
+
touchAction: string;
|
|
62
|
+
[key: string]: string | number | undefined;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// ============================================================================
|
|
66
|
+
// DnD Config Resolution
|
|
67
|
+
// ============================================================================
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Resolve a DnD configuration by applying spec defaults to missing fields.
|
|
71
|
+
*
|
|
72
|
+
* @param config - Partial DnD configuration from the spec
|
|
73
|
+
* @returns Fully resolved DnD configuration
|
|
74
|
+
*/
|
|
75
|
+
export function resolveDndConfig(config: DndConfig): ResolvedDndConfig {
|
|
76
|
+
return {
|
|
77
|
+
enabled: config.enabled ?? false,
|
|
78
|
+
sortable: config.sortable ?? false,
|
|
79
|
+
autoScroll: config.autoScroll ?? true,
|
|
80
|
+
touchDelay: config.touchDelay ?? 200,
|
|
81
|
+
dragItem: config.dragItem,
|
|
82
|
+
dropZone: config.dropZone,
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// ============================================================================
|
|
87
|
+
// Drag Item Props
|
|
88
|
+
// ============================================================================
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Convert a spec DragItem to component props suitable for a draggable element.
|
|
92
|
+
* Produces `draggable`, ARIA attributes, and data attributes for DnD libraries.
|
|
93
|
+
*
|
|
94
|
+
* @param item - DragItem configuration from the spec
|
|
95
|
+
* @returns Component props object for a draggable element
|
|
96
|
+
*/
|
|
97
|
+
export function createDragItemProps(item: DragItem): DragItemProps {
|
|
98
|
+
const ariaLabel = typeof item.ariaLabel === 'string'
|
|
99
|
+
? item.ariaLabel
|
|
100
|
+
: item.ariaLabel?.defaultValue;
|
|
101
|
+
|
|
102
|
+
const label = typeof item.label === 'string'
|
|
103
|
+
? item.label
|
|
104
|
+
: item.label?.defaultValue;
|
|
105
|
+
|
|
106
|
+
return {
|
|
107
|
+
draggable: !(item.disabled ?? false),
|
|
108
|
+
'aria-roledescription': 'draggable',
|
|
109
|
+
'aria-label': ariaLabel ?? label,
|
|
110
|
+
'aria-describedby': item.ariaDescribedBy,
|
|
111
|
+
role: item.role ?? 'listitem',
|
|
112
|
+
'data-drag-type': item.type,
|
|
113
|
+
'data-drag-handle': item.handle ?? 'element',
|
|
114
|
+
'data-drag-disabled': String(item.disabled ?? false),
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// ============================================================================
|
|
119
|
+
// Drop Zone Props
|
|
120
|
+
// ============================================================================
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Convert a spec DropZone to component props suitable for a droppable area.
|
|
124
|
+
* Produces ARIA attributes and data attributes for DnD libraries.
|
|
125
|
+
*
|
|
126
|
+
* @param zone - DropZone configuration from the spec
|
|
127
|
+
* @returns Component props object for a droppable area
|
|
128
|
+
*/
|
|
129
|
+
export function createDropZoneProps(zone: DropZone): DropZoneProps {
|
|
130
|
+
const ariaLabel = typeof zone.ariaLabel === 'string'
|
|
131
|
+
? zone.ariaLabel
|
|
132
|
+
: zone.ariaLabel?.defaultValue;
|
|
133
|
+
|
|
134
|
+
const label = typeof zone.label === 'string'
|
|
135
|
+
? zone.label
|
|
136
|
+
: zone.label?.defaultValue;
|
|
137
|
+
|
|
138
|
+
return {
|
|
139
|
+
'aria-dropeffect': zone.dropEffect ?? 'move',
|
|
140
|
+
'aria-label': ariaLabel ?? label,
|
|
141
|
+
'aria-describedby': zone.ariaDescribedBy,
|
|
142
|
+
role: zone.role ?? 'list',
|
|
143
|
+
'data-drop-accept': zone.accept.join(','),
|
|
144
|
+
'data-drop-max-items': zone.maxItems,
|
|
145
|
+
'data-drop-highlight': String(zone.highlightOnDragOver ?? true),
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// ============================================================================
|
|
150
|
+
// Drag Constraint Styles
|
|
151
|
+
// ============================================================================
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Resolve a DragConstraint into CSS style properties that restrict
|
|
155
|
+
* drag movement along an axis or within bounds.
|
|
156
|
+
*
|
|
157
|
+
* @param constraint - DragConstraint configuration from the spec
|
|
158
|
+
* @returns CSS styles object for constraining drag movement
|
|
159
|
+
*/
|
|
160
|
+
export function resolveDragConstraints(constraint: DragConstraint): DragConstraintStyles {
|
|
161
|
+
const styles: DragConstraintStyles = {
|
|
162
|
+
position: 'relative',
|
|
163
|
+
touchAction: 'none',
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
const axis = constraint.axis ?? 'both';
|
|
167
|
+
if (axis === 'x') {
|
|
168
|
+
styles.touchAction = 'pan-y';
|
|
169
|
+
} else if (axis === 'y') {
|
|
170
|
+
styles.touchAction = 'pan-x';
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
const bounds = constraint.bounds ?? 'none';
|
|
174
|
+
if (bounds === 'parent') {
|
|
175
|
+
styles.overflow = 'hidden';
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
if (constraint.grid) {
|
|
179
|
+
styles['--drag-grid-x'] = `${constraint.grid[0]}px`;
|
|
180
|
+
styles['--drag-grid-y'] = `${constraint.grid[1]}px`;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
return styles;
|
|
184
|
+
}
|
|
@@ -0,0 +1,185 @@
|
|
|
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 - Keyboard Protocol Bridge
|
|
11
|
+
*
|
|
12
|
+
* Converts spec-aligned keyboard navigation and focus management
|
|
13
|
+
* schemas into runtime-usable configurations, shortcut parsers,
|
|
14
|
+
* and focus trap settings.
|
|
15
|
+
*
|
|
16
|
+
* @module protocols/KeyboardProtocol
|
|
17
|
+
* @packageDocumentation
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
import type {
|
|
21
|
+
KeyboardNavigationConfig,
|
|
22
|
+
KeyboardShortcut,
|
|
23
|
+
FocusManagement,
|
|
24
|
+
FocusTrapConfig,
|
|
25
|
+
} from '@object-ui/types';
|
|
26
|
+
|
|
27
|
+
// ============================================================================
|
|
28
|
+
// Resolved Types
|
|
29
|
+
// ============================================================================
|
|
30
|
+
|
|
31
|
+
/** Fully resolved keyboard navigation configuration. */
|
|
32
|
+
export interface ResolvedKeyboardConfig {
|
|
33
|
+
shortcuts: KeyboardShortcut[];
|
|
34
|
+
focusManagement: ResolvedFocusManagement;
|
|
35
|
+
rovingTabindex: boolean;
|
|
36
|
+
ariaLabel?: string;
|
|
37
|
+
ariaDescribedBy?: string;
|
|
38
|
+
role?: string;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Fully resolved focus management configuration. */
|
|
42
|
+
export interface ResolvedFocusManagement {
|
|
43
|
+
tabOrder: 'auto' | 'manual';
|
|
44
|
+
skipLinks: boolean;
|
|
45
|
+
focusVisible: boolean;
|
|
46
|
+
focusTrap?: ResolvedFocusTrapConfig;
|
|
47
|
+
arrowNavigation: boolean;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** Fully resolved focus trap configuration. */
|
|
51
|
+
export interface ResolvedFocusTrapConfig {
|
|
52
|
+
enabled: boolean;
|
|
53
|
+
initialFocus?: string;
|
|
54
|
+
returnFocus: boolean;
|
|
55
|
+
escapeDeactivates: boolean;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** Parsed keyboard shortcut descriptor. */
|
|
59
|
+
export interface ParsedShortcut {
|
|
60
|
+
key: string;
|
|
61
|
+
ctrlOrMeta: boolean;
|
|
62
|
+
shift: boolean;
|
|
63
|
+
alt: boolean;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** Minimal keyboard event shape for matching (no React dependency). */
|
|
67
|
+
export interface KeyboardEventLike {
|
|
68
|
+
key: string;
|
|
69
|
+
ctrlKey?: boolean;
|
|
70
|
+
metaKey?: boolean;
|
|
71
|
+
shiftKey?: boolean;
|
|
72
|
+
altKey?: boolean;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// ============================================================================
|
|
76
|
+
// Keyboard Config Resolution
|
|
77
|
+
// ============================================================================
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Resolve a keyboard navigation configuration by applying spec defaults.
|
|
81
|
+
*
|
|
82
|
+
* @param config - KeyboardNavigationConfig from the spec
|
|
83
|
+
* @returns Fully resolved keyboard navigation configuration
|
|
84
|
+
*/
|
|
85
|
+
export function resolveKeyboardConfig(config: KeyboardNavigationConfig): ResolvedKeyboardConfig {
|
|
86
|
+
const ariaLabel = typeof config.ariaLabel === 'string'
|
|
87
|
+
? config.ariaLabel
|
|
88
|
+
: config.ariaLabel?.defaultValue;
|
|
89
|
+
|
|
90
|
+
return {
|
|
91
|
+
shortcuts: config.shortcuts ?? [],
|
|
92
|
+
focusManagement: resolveFocusManagement(config.focusManagement),
|
|
93
|
+
rovingTabindex: config.rovingTabindex ?? false,
|
|
94
|
+
ariaLabel,
|
|
95
|
+
ariaDescribedBy: config.ariaDescribedBy,
|
|
96
|
+
role: config.role,
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Resolve focus management configuration with defaults.
|
|
102
|
+
*/
|
|
103
|
+
function resolveFocusManagement(fm?: FocusManagement): ResolvedFocusManagement {
|
|
104
|
+
return {
|
|
105
|
+
tabOrder: fm?.tabOrder ?? 'auto',
|
|
106
|
+
skipLinks: fm?.skipLinks ?? false,
|
|
107
|
+
focusVisible: fm?.focusVisible ?? true,
|
|
108
|
+
focusTrap: fm?.focusTrap ? createFocusTrapConfig(fm.focusTrap) : undefined,
|
|
109
|
+
arrowNavigation: fm?.arrowNavigation ?? false,
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// ============================================================================
|
|
114
|
+
// Shortcut Parsing
|
|
115
|
+
// ============================================================================
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Parse a shortcut string like "Ctrl+Shift+S" into a structured descriptor.
|
|
119
|
+
* Recognises Ctrl, Meta, Cmd (mapped to ctrlOrMeta), Shift, and Alt modifiers.
|
|
120
|
+
* The last segment is treated as the key.
|
|
121
|
+
*
|
|
122
|
+
* Keys are normalised to lowercase for case-insensitive matching. The Shift
|
|
123
|
+
* modifier is tracked separately, so "Shift+A" and "Shift+a" are equivalent
|
|
124
|
+
* (both represent the same physical key combination).
|
|
125
|
+
*
|
|
126
|
+
* @param shortcut - Shortcut string (e.g. "Ctrl+S", "Alt+Shift+N", "Escape")
|
|
127
|
+
* @returns Parsed shortcut descriptor
|
|
128
|
+
*/
|
|
129
|
+
export function parseShortcutKey(shortcut: string): ParsedShortcut {
|
|
130
|
+
const parts = shortcut.split('+').map(p => p.trim());
|
|
131
|
+
const modifiers = new Set(parts.slice(0, -1).map(m => m.toLowerCase()));
|
|
132
|
+
const key = (parts[parts.length - 1] ?? '').toLowerCase();
|
|
133
|
+
|
|
134
|
+
return {
|
|
135
|
+
key,
|
|
136
|
+
ctrlOrMeta: modifiers.has('ctrl') || modifiers.has('meta') || modifiers.has('cmd'),
|
|
137
|
+
shift: modifiers.has('shift'),
|
|
138
|
+
alt: modifiers.has('alt'),
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// ============================================================================
|
|
143
|
+
// Shortcut Matching
|
|
144
|
+
// ============================================================================
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Test whether a keyboard event matches a shortcut string.
|
|
148
|
+
* Comparison is case-insensitive; the Shift modifier is checked separately.
|
|
149
|
+
*
|
|
150
|
+
* @param event - The keyboard event (or a plain object with key + modifier flags)
|
|
151
|
+
* @param shortcut - Shortcut string (e.g. "Ctrl+S")
|
|
152
|
+
* @returns `true` if the event matches the shortcut
|
|
153
|
+
*/
|
|
154
|
+
export function matchesShortcut(event: KeyboardEventLike, shortcut: string): boolean {
|
|
155
|
+
const parsed = parseShortcutKey(shortcut);
|
|
156
|
+
|
|
157
|
+
if (event.key.toLowerCase() !== parsed.key) return false;
|
|
158
|
+
|
|
159
|
+
const eventCtrlOrMeta = !!(event.ctrlKey || event.metaKey);
|
|
160
|
+
if (parsed.ctrlOrMeta !== eventCtrlOrMeta) return false;
|
|
161
|
+
|
|
162
|
+
if (parsed.shift !== !!(event.shiftKey)) return false;
|
|
163
|
+
if (parsed.alt !== !!(event.altKey)) return false;
|
|
164
|
+
|
|
165
|
+
return true;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// ============================================================================
|
|
169
|
+
// Focus Trap Configuration
|
|
170
|
+
// ============================================================================
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Create a resolved focus trap configuration from a spec FocusTrapConfig.
|
|
174
|
+
*
|
|
175
|
+
* @param config - FocusTrapConfig from the spec
|
|
176
|
+
* @returns Resolved focus trap configuration with defaults applied
|
|
177
|
+
*/
|
|
178
|
+
export function createFocusTrapConfig(config: FocusTrapConfig): ResolvedFocusTrapConfig {
|
|
179
|
+
return {
|
|
180
|
+
enabled: config.enabled ?? false,
|
|
181
|
+
initialFocus: config.initialFocus,
|
|
182
|
+
returnFocus: config.returnFocus ?? true,
|
|
183
|
+
escapeDeactivates: config.escapeDeactivates ?? true,
|
|
184
|
+
};
|
|
185
|
+
}
|
|
@@ -0,0 +1,159 @@
|
|
|
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 - Notification Protocol Bridge
|
|
11
|
+
*
|
|
12
|
+
* Converts spec-aligned Notification schemas into toast-compatible
|
|
13
|
+
* objects that UI layers can render. Maps severity to variant,
|
|
14
|
+
* position names, and resolves default notification configs.
|
|
15
|
+
*
|
|
16
|
+
* @module protocols/NotificationProtocol
|
|
17
|
+
* @packageDocumentation
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
import type {
|
|
21
|
+
Notification as SpecNotification,
|
|
22
|
+
NotificationAction,
|
|
23
|
+
NotificationConfig,
|
|
24
|
+
NotificationPosition,
|
|
25
|
+
NotificationSeverity,
|
|
26
|
+
} from '@object-ui/types';
|
|
27
|
+
|
|
28
|
+
// ============================================================================
|
|
29
|
+
// Resolved Types
|
|
30
|
+
// ============================================================================
|
|
31
|
+
|
|
32
|
+
/** Fully resolved notification configuration. */
|
|
33
|
+
export interface ResolvedNotificationConfig {
|
|
34
|
+
defaultPosition: NotificationPosition;
|
|
35
|
+
defaultDuration: number;
|
|
36
|
+
maxVisible: number;
|
|
37
|
+
stackDirection: 'up' | 'down';
|
|
38
|
+
pauseOnHover: boolean;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Toast-compatible representation of a spec Notification. */
|
|
42
|
+
export interface ToastNotification {
|
|
43
|
+
title?: string;
|
|
44
|
+
description: string;
|
|
45
|
+
variant: string;
|
|
46
|
+
position: string;
|
|
47
|
+
duration: number;
|
|
48
|
+
dismissible: boolean;
|
|
49
|
+
actions: ToastAction[];
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** Toast-compatible action button. */
|
|
53
|
+
export interface ToastAction {
|
|
54
|
+
label: string;
|
|
55
|
+
action: string;
|
|
56
|
+
variant: 'primary' | 'secondary' | 'link';
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// ============================================================================
|
|
60
|
+
// Severity → Variant Mapping
|
|
61
|
+
// ============================================================================
|
|
62
|
+
|
|
63
|
+
const SEVERITY_TO_VARIANT: Record<string, string> = {
|
|
64
|
+
info: 'default',
|
|
65
|
+
success: 'success',
|
|
66
|
+
warning: 'warning',
|
|
67
|
+
error: 'destructive',
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Map a spec notification severity to a toast variant string.
|
|
72
|
+
*
|
|
73
|
+
* @param severity - Spec severity (info, success, warning, error)
|
|
74
|
+
* @returns Toast variant (default, success, warning, destructive)
|
|
75
|
+
*/
|
|
76
|
+
export function mapSeverityToVariant(severity: string): string {
|
|
77
|
+
return SEVERITY_TO_VARIANT[severity] ?? 'default';
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// ============================================================================
|
|
81
|
+
// Position Mapping
|
|
82
|
+
// ============================================================================
|
|
83
|
+
|
|
84
|
+
const POSITION_MAP: Record<string, string> = {
|
|
85
|
+
top_left: 'top-left',
|
|
86
|
+
top_center: 'top-center',
|
|
87
|
+
top_right: 'top-right',
|
|
88
|
+
bottom_left: 'bottom-left',
|
|
89
|
+
bottom_center: 'bottom-center',
|
|
90
|
+
bottom_right: 'bottom-right',
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Map a spec notification position (underscore-separated) to a
|
|
95
|
+
* toast position string (hyphen-separated).
|
|
96
|
+
*
|
|
97
|
+
* @param position - Spec position (e.g. "top_right")
|
|
98
|
+
* @returns Toast position (e.g. "top-right")
|
|
99
|
+
*/
|
|
100
|
+
export function mapPosition(position: string): string {
|
|
101
|
+
return POSITION_MAP[position] ?? 'top-right';
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// ============================================================================
|
|
105
|
+
// Notification Config Resolution
|
|
106
|
+
// ============================================================================
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Resolve a notification configuration by applying spec defaults.
|
|
110
|
+
*
|
|
111
|
+
* @param config - NotificationConfig from the spec
|
|
112
|
+
* @returns Fully resolved notification configuration
|
|
113
|
+
*/
|
|
114
|
+
export function resolveNotificationConfig(config: NotificationConfig): ResolvedNotificationConfig {
|
|
115
|
+
return {
|
|
116
|
+
defaultPosition: config.defaultPosition ?? 'top_right',
|
|
117
|
+
defaultDuration: config.defaultDuration ?? 5000,
|
|
118
|
+
maxVisible: config.maxVisible ?? 5,
|
|
119
|
+
stackDirection: config.stackDirection ?? 'down',
|
|
120
|
+
pauseOnHover: config.pauseOnHover ?? true,
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// ============================================================================
|
|
125
|
+
// Spec Notification → Toast
|
|
126
|
+
// ============================================================================
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Extract the display string from a translatable value (string or Translation object).
|
|
130
|
+
*/
|
|
131
|
+
function resolveTranslatableString(value: string | { key: string; defaultValue?: string } | undefined): string | undefined {
|
|
132
|
+
if (value === undefined) return undefined;
|
|
133
|
+
if (typeof value === 'string') return value;
|
|
134
|
+
return value.defaultValue;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Convert a spec Notification to a toast-compatible object.
|
|
139
|
+
*
|
|
140
|
+
* @param notification - Spec Notification
|
|
141
|
+
* @returns Toast-compatible notification with title, description, variant, etc.
|
|
142
|
+
*/
|
|
143
|
+
export function specNotificationToToast(notification: SpecNotification): ToastNotification {
|
|
144
|
+
const actions: ToastAction[] = (notification.actions ?? []).map((a: NotificationAction) => ({
|
|
145
|
+
label: typeof a.label === 'string' ? a.label : a.label?.defaultValue ?? '',
|
|
146
|
+
action: a.action,
|
|
147
|
+
variant: a.variant ?? 'primary',
|
|
148
|
+
}));
|
|
149
|
+
|
|
150
|
+
return {
|
|
151
|
+
title: resolveTranslatableString(notification.title),
|
|
152
|
+
description: resolveTranslatableString(notification.message) ?? '',
|
|
153
|
+
variant: mapSeverityToVariant(notification.severity ?? 'info'),
|
|
154
|
+
position: mapPosition(notification.position ?? 'top_right'),
|
|
155
|
+
duration: notification.duration ?? 5000,
|
|
156
|
+
dismissible: notification.dismissible ?? true,
|
|
157
|
+
actions,
|
|
158
|
+
};
|
|
159
|
+
}
|