@xopcai/extension-ui-sdk 0.0.520 → 0.0.522
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 +1 -1
- package/src/client.test.ts +34 -0
- package/src/client.ts +45 -0
- package/src/index.ts +1 -0
- package/src/mobile-ui-kit.css +90 -0
- package/src/transport.ts +55 -5
- package/src/types.ts +50 -2
package/package.json
CHANGED
package/src/client.test.ts
CHANGED
|
@@ -68,4 +68,38 @@ describe('extension client product navigation', () => {
|
|
|
68
68
|
await expect(client.ui.openProduct({ kind: 'file', id: '/tmp/result.md' }))
|
|
69
69
|
.rejects.toThrow('cannot be opened');
|
|
70
70
|
});
|
|
71
|
+
|
|
72
|
+
it('forwards structured records, connectivity, haptics, image picker, and share methods', async () => {
|
|
73
|
+
const request = vi.fn(async () => ({ records: [], cursorMs: 0, state: 'online' }));
|
|
74
|
+
const handlers = new Map<string, (data: unknown) => void>();
|
|
75
|
+
const on = vi.fn((event: string, handler: (data: unknown) => void) => {
|
|
76
|
+
handlers.set(event, handler);
|
|
77
|
+
return () => handlers.delete(event);
|
|
78
|
+
});
|
|
79
|
+
const client = createExtensionClient({ transport: { request, on } as unknown as Transport });
|
|
80
|
+
|
|
81
|
+
await client.records.list('logs', { sinceMs: 100 });
|
|
82
|
+
await client.records.upsert('logs', { recordId: 'r1', data: { ml: 250 } });
|
|
83
|
+
await client.records.delete('logs', 'r1', { expectedVersion: 1 });
|
|
84
|
+
await client.connectivity.getState();
|
|
85
|
+
await client.device.haptics('selection');
|
|
86
|
+
await client.media.pickImage({ source: 'library' });
|
|
87
|
+
await client.ui.share({ title: 'Tracker', text: 'Done' });
|
|
88
|
+
|
|
89
|
+
const onDelta = vi.fn();
|
|
90
|
+
const unsub = client.records.onChange('logs', onDelta);
|
|
91
|
+
handlers.get('records.delta.logs')?.([{ recordId: 'r1' }]);
|
|
92
|
+
expect(onDelta).toHaveBeenCalledWith([{ recordId: 'r1' }]);
|
|
93
|
+
unsub();
|
|
94
|
+
|
|
95
|
+
expect(request.mock.calls).toEqual([
|
|
96
|
+
['records.list', { collection: 'logs', sinceMs: 100 }],
|
|
97
|
+
['records.upsert', { collection: 'logs', recordId: 'r1', data: { ml: 250 } }],
|
|
98
|
+
['records.delete', { collection: 'logs', recordId: 'r1', expectedVersion: 1 }],
|
|
99
|
+
['connectivity.getState'],
|
|
100
|
+
['device.haptics', { style: 'selection' }],
|
|
101
|
+
['media.pickImage', { source: 'library' }],
|
|
102
|
+
['ui.share', { title: 'Tracker', text: 'Done' }],
|
|
103
|
+
]);
|
|
104
|
+
});
|
|
71
105
|
});
|
package/src/client.ts
CHANGED
|
@@ -105,6 +105,48 @@ export function createExtensionClient(options?: CreateExtensionClientOptions): E
|
|
|
105
105
|
},
|
|
106
106
|
},
|
|
107
107
|
|
|
108
|
+
records: {
|
|
109
|
+
async list(collection, options) {
|
|
110
|
+
return transport.request('records.list', { collection, ...options });
|
|
111
|
+
},
|
|
112
|
+
async upsert(collection, input) {
|
|
113
|
+
return transport.request('records.upsert', { collection, ...input });
|
|
114
|
+
},
|
|
115
|
+
async delete(collection, recordId, options) {
|
|
116
|
+
return transport.request('records.delete', { collection, recordId, ...options });
|
|
117
|
+
},
|
|
118
|
+
onChange(collection, handler) {
|
|
119
|
+
return transport.on(`records.delta.${collection}`, (data) => {
|
|
120
|
+
if (Array.isArray(data)) handler(data as never);
|
|
121
|
+
});
|
|
122
|
+
},
|
|
123
|
+
},
|
|
124
|
+
|
|
125
|
+
connectivity: {
|
|
126
|
+
async getState() {
|
|
127
|
+
return transport.request('connectivity.getState');
|
|
128
|
+
},
|
|
129
|
+
onChange(handler) {
|
|
130
|
+
return transport.on('connectivity.changed', (data) => {
|
|
131
|
+
if (data && typeof data === 'object' && 'state' in data) {
|
|
132
|
+
handler(data as { state: 'online' | 'offline'; pendingMutations?: number });
|
|
133
|
+
}
|
|
134
|
+
});
|
|
135
|
+
},
|
|
136
|
+
},
|
|
137
|
+
|
|
138
|
+
device: {
|
|
139
|
+
async haptics(style = 'light') {
|
|
140
|
+
await transport.request('device.haptics', { style });
|
|
141
|
+
},
|
|
142
|
+
},
|
|
143
|
+
|
|
144
|
+
media: {
|
|
145
|
+
async pickImage(options) {
|
|
146
|
+
return transport.request('media.pickImage', options ?? {});
|
|
147
|
+
},
|
|
148
|
+
},
|
|
149
|
+
|
|
108
150
|
ui: {
|
|
109
151
|
async reviewPermissions() { await transport.request('ui.reviewPermissions'); },
|
|
110
152
|
resize(height: number) {
|
|
@@ -113,6 +155,9 @@ export function createExtensionClient(options?: CreateExtensionClientOptions): E
|
|
|
113
155
|
async showNotification(options) {
|
|
114
156
|
await transport.request('ui.notification', options);
|
|
115
157
|
},
|
|
158
|
+
async share(payload) {
|
|
159
|
+
await transport.request('ui.share', payload);
|
|
160
|
+
},
|
|
116
161
|
async navigate(path: string) {
|
|
117
162
|
await transport.request('ui.navigate', { path });
|
|
118
163
|
},
|
package/src/index.ts
CHANGED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/* Space Mobile UI Kit — mobile-first, theme-aware baseline for xopc Spaces */
|
|
2
|
+
:root {
|
|
3
|
+
--xopc-safe-top: 0px;
|
|
4
|
+
--xopc-safe-bottom: 0px;
|
|
5
|
+
--xopc-tabbar-inset: 0px;
|
|
6
|
+
--xopc-space-bg: var(--canvas, #f7f7f5);
|
|
7
|
+
--xopc-space-card: var(--panel, #ffffff);
|
|
8
|
+
--xopc-space-fg: var(--text, #18181b);
|
|
9
|
+
--xopc-space-muted: var(--muted, #71717a);
|
|
10
|
+
--xopc-space-border: var(--border, rgba(24, 24, 27, 0.1));
|
|
11
|
+
--xopc-space-accent: var(--accent, #4b63d9);
|
|
12
|
+
--xopc-space-on-accent: var(--on-accent, #ffffff);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
*,
|
|
16
|
+
*::before,
|
|
17
|
+
*::after {
|
|
18
|
+
box-sizing: border-box;
|
|
19
|
+
-webkit-tap-highlight-color: transparent;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
html,
|
|
23
|
+
body {
|
|
24
|
+
margin: 0;
|
|
25
|
+
padding: 0;
|
|
26
|
+
max-width: 100%;
|
|
27
|
+
overflow-x: hidden;
|
|
28
|
+
background: var(--xopc-space-bg);
|
|
29
|
+
color: var(--xopc-space-fg);
|
|
30
|
+
font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
|
|
31
|
+
line-height: 1.5;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
.xopc-page {
|
|
35
|
+
min-height: 100dvh;
|
|
36
|
+
padding: calc(16px + var(--xopc-safe-top)) 16px calc(24px + var(--xopc-safe-bottom) + var(--xopc-tabbar-inset));
|
|
37
|
+
max-width: 820px;
|
|
38
|
+
margin: 0 auto;
|
|
39
|
+
display: flex;
|
|
40
|
+
flex-direction: column;
|
|
41
|
+
gap: 14px;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
.xopc-card {
|
|
45
|
+
background: var(--xopc-space-card);
|
|
46
|
+
border: 1px solid var(--xopc-space-border);
|
|
47
|
+
border-radius: 16px;
|
|
48
|
+
padding: 16px;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
.xopc-btn {
|
|
52
|
+
display: inline-flex;
|
|
53
|
+
align-items: center;
|
|
54
|
+
justify-content: center;
|
|
55
|
+
gap: 8px;
|
|
56
|
+
min-height: 44px;
|
|
57
|
+
min-width: 44px;
|
|
58
|
+
padding: 10px 16px;
|
|
59
|
+
border-radius: 12px;
|
|
60
|
+
border: 1px solid transparent;
|
|
61
|
+
background: var(--xopc-space-accent);
|
|
62
|
+
color: var(--xopc-space-on-accent);
|
|
63
|
+
font: inherit;
|
|
64
|
+
font-weight: 600;
|
|
65
|
+
cursor: pointer;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
.xopc-btn-secondary {
|
|
69
|
+
background: transparent;
|
|
70
|
+
color: var(--xopc-space-fg);
|
|
71
|
+
border-color: var(--xopc-space-border);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
.xopc-row {
|
|
75
|
+
display: flex;
|
|
76
|
+
align-items: center;
|
|
77
|
+
justify-content: space-between;
|
|
78
|
+
gap: 12px;
|
|
79
|
+
min-height: 44px;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
.xopc-muted {
|
|
83
|
+
color: var(--xopc-space-muted);
|
|
84
|
+
font-size: 0.875rem;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
html[data-xopc-connectivity="offline"] [data-requires-online] {
|
|
88
|
+
opacity: 0.45;
|
|
89
|
+
pointer-events: none;
|
|
90
|
+
}
|
package/src/transport.ts
CHANGED
|
@@ -1,11 +1,34 @@
|
|
|
1
1
|
import type {
|
|
2
2
|
ExtensionEventMessage,
|
|
3
3
|
ExtensionRequest,
|
|
4
|
+
ExtensionToHostMessage,
|
|
5
|
+
HostEnvironmentInfo,
|
|
4
6
|
HostInit,
|
|
5
7
|
HostResponse,
|
|
6
8
|
HostToExtensionMessage,
|
|
7
9
|
} from './types.js';
|
|
8
10
|
|
|
11
|
+
interface NativeBridgeHandle {
|
|
12
|
+
postMessage(payload: string): void;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function getNativeBridge(): NativeBridgeHandle | undefined {
|
|
16
|
+
if (typeof window === 'undefined') return undefined;
|
|
17
|
+
const candidate = (window as unknown as { __XOPC_NATIVE_BRIDGE__?: NativeBridgeHandle }).__XOPC_NATIVE_BRIDGE__;
|
|
18
|
+
return candidate && typeof candidate.postMessage === 'function' ? candidate : undefined;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function applyEnvironmentToDocument(env?: HostEnvironmentInfo): void {
|
|
22
|
+
if (!env || typeof document === 'undefined' || !document.documentElement) return;
|
|
23
|
+
const root = document.documentElement;
|
|
24
|
+
root.dataset.xopcPlatform = env.platform;
|
|
25
|
+
root.dataset.xopcSurface = env.surface;
|
|
26
|
+
root.dataset.xopcConnectivity = env.connectivity;
|
|
27
|
+
root.style.setProperty('--xopc-safe-top', `${Math.max(0, env.safeArea.top)}px`);
|
|
28
|
+
root.style.setProperty('--xopc-safe-bottom', `${Math.max(0, env.safeArea.bottom)}px`);
|
|
29
|
+
root.style.setProperty('--xopc-tabbar-inset', `${Math.max(0, env.tabBarInset)}px`);
|
|
30
|
+
}
|
|
31
|
+
|
|
9
32
|
type PendingRequest = {
|
|
10
33
|
resolve: (v: unknown) => void;
|
|
11
34
|
reject: (e: Error) => void;
|
|
@@ -36,8 +59,18 @@ export class Transport {
|
|
|
36
59
|
this.initResolve = resolve;
|
|
37
60
|
});
|
|
38
61
|
window.addEventListener('message', this.handleMessage);
|
|
62
|
+
window.addEventListener('xopc-native-host-message', this.handleNativeEvent as EventListener);
|
|
39
63
|
// A module can await whenReady before window.load. Request init instead of waiting for load.
|
|
40
|
-
|
|
64
|
+
this.postToHost({ source: 'xopc-extension', extensionId: '', type: 'event', event: 'ui.connect' });
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
private postToHost(message: ExtensionToHostMessage): void {
|
|
68
|
+
const native = getNativeBridge();
|
|
69
|
+
if (native) {
|
|
70
|
+
native.postMessage(JSON.stringify(message));
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
window.parent.postMessage(message, '*');
|
|
41
74
|
}
|
|
42
75
|
|
|
43
76
|
get ready(): Promise<HostInit> {
|
|
@@ -52,6 +85,7 @@ export class Transport {
|
|
|
52
85
|
if (this.disposed) return;
|
|
53
86
|
this.disposed = true;
|
|
54
87
|
window.removeEventListener('message', this.handleMessage);
|
|
88
|
+
window.removeEventListener('xopc-native-host-message', this.handleNativeEvent as EventListener);
|
|
55
89
|
for (const [, p] of this.pending) {
|
|
56
90
|
clearTimeout(p.timer);
|
|
57
91
|
p.reject(new Error('Transport disposed'));
|
|
@@ -87,7 +121,7 @@ export class Transport {
|
|
|
87
121
|
reject,
|
|
88
122
|
timer,
|
|
89
123
|
});
|
|
90
|
-
|
|
124
|
+
this.postToHost(message);
|
|
91
125
|
});
|
|
92
126
|
}
|
|
93
127
|
|
|
@@ -100,7 +134,7 @@ export class Transport {
|
|
|
100
134
|
event,
|
|
101
135
|
data,
|
|
102
136
|
};
|
|
103
|
-
|
|
137
|
+
this.postToHost(message);
|
|
104
138
|
}
|
|
105
139
|
|
|
106
140
|
on(event: string, handler: (data: unknown) => void): () => void {
|
|
@@ -123,14 +157,22 @@ export class Transport {
|
|
|
123
157
|
return `req_${Date.now()}_${this.requestCounter}`;
|
|
124
158
|
}
|
|
125
159
|
|
|
160
|
+
private handleNativeEvent = (event: CustomEvent<HostToExtensionMessage>): void => {
|
|
161
|
+
this.dispatchHostMessage(event.detail);
|
|
162
|
+
};
|
|
163
|
+
|
|
126
164
|
private handleMessage = (event: MessageEvent): void => {
|
|
165
|
+
this.dispatchHostMessage(event.data as HostToExtensionMessage | undefined);
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
private dispatchHostMessage(msg: HostToExtensionMessage | undefined): void {
|
|
127
169
|
if (this.disposed) return;
|
|
128
|
-
const msg = event.data as HostToExtensionMessage | undefined;
|
|
129
170
|
if (!msg || msg.source !== 'xopc-host') return;
|
|
130
171
|
|
|
131
172
|
switch (msg.type) {
|
|
132
173
|
case 'init': {
|
|
133
174
|
this.extensionId = msg.extensionId;
|
|
175
|
+
applyEnvironmentToDocument(msg.environment);
|
|
134
176
|
this.initResolve?.(msg);
|
|
135
177
|
this.initResolve = null;
|
|
136
178
|
return;
|
|
@@ -150,6 +192,14 @@ export class Transport {
|
|
|
150
192
|
}
|
|
151
193
|
case 'event': {
|
|
152
194
|
const ev = msg as { event: string; data?: unknown };
|
|
195
|
+
if (ev.event === 'environment.changed' && ev.data && typeof ev.data === 'object') {
|
|
196
|
+
applyEnvironmentToDocument(ev.data as HostEnvironmentInfo);
|
|
197
|
+
} else if (ev.event === 'connectivity.changed' && ev.data && typeof ev.data === 'object' && 'state' in ev.data) {
|
|
198
|
+
const state = (ev.data as { state?: string }).state;
|
|
199
|
+
if ((state === 'online' || state === 'offline') && typeof document !== 'undefined' && document.documentElement) {
|
|
200
|
+
document.documentElement.dataset.xopcConnectivity = state;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
153
203
|
const handlers = this.eventHandlers.get(ev.event);
|
|
154
204
|
if (handlers) {
|
|
155
205
|
for (const h of handlers) {
|
|
@@ -165,5 +215,5 @@ export class Transport {
|
|
|
165
215
|
default:
|
|
166
216
|
return;
|
|
167
217
|
}
|
|
168
|
-
}
|
|
218
|
+
}
|
|
169
219
|
}
|
package/src/types.ts
CHANGED
|
@@ -1,4 +1,11 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type {
|
|
2
|
+
CapabilityCall,
|
|
3
|
+
CapabilityDescriptor,
|
|
4
|
+
ProductReferenceLocator,
|
|
5
|
+
SpaceRecord,
|
|
6
|
+
SpaceRecordsListResponse,
|
|
7
|
+
SpaceRecordUpsertInput,
|
|
8
|
+
} from '@xopcai/gateway-contract';
|
|
2
9
|
|
|
3
10
|
export enum ExtensionErrorCode {
|
|
4
11
|
PermissionDenied = 4001,
|
|
@@ -34,6 +41,14 @@ export interface ExtensionEventMessage {
|
|
|
34
41
|
data?: unknown;
|
|
35
42
|
}
|
|
36
43
|
|
|
44
|
+
export interface HostEnvironmentInfo {
|
|
45
|
+
platform: 'web' | 'ios' | 'android' | 'harmony';
|
|
46
|
+
surface: 'tab' | 'fullscreen' | 'preview';
|
|
47
|
+
safeArea: { top: number; bottom: number };
|
|
48
|
+
tabBarInset: number;
|
|
49
|
+
connectivity: 'online' | 'offline';
|
|
50
|
+
}
|
|
51
|
+
|
|
37
52
|
export interface HostInit {
|
|
38
53
|
source: 'xopc-host';
|
|
39
54
|
type: 'init';
|
|
@@ -41,6 +56,7 @@ export interface HostInit {
|
|
|
41
56
|
permissions: string[];
|
|
42
57
|
theme: ThemeInfo;
|
|
43
58
|
locale: string;
|
|
59
|
+
environment?: HostEnvironmentInfo;
|
|
44
60
|
}
|
|
45
61
|
|
|
46
62
|
export interface HostResponse {
|
|
@@ -103,6 +119,38 @@ export interface ExtensionClient {
|
|
|
103
119
|
remove(key: string): Promise<void>;
|
|
104
120
|
keys(): Promise<string[]>;
|
|
105
121
|
};
|
|
122
|
+
records: {
|
|
123
|
+
list(
|
|
124
|
+
collection: string,
|
|
125
|
+
options?: { sinceMs?: number; includeDeleted?: boolean; limit?: number },
|
|
126
|
+
): Promise<SpaceRecordsListResponse & { offline?: boolean }>;
|
|
127
|
+
upsert(
|
|
128
|
+
collection: string,
|
|
129
|
+
input: SpaceRecordUpsertInput,
|
|
130
|
+
): Promise<{ record: SpaceRecord; deduplicated: boolean; queuedOffline?: boolean }>;
|
|
131
|
+
delete(
|
|
132
|
+
collection: string,
|
|
133
|
+
recordId: string,
|
|
134
|
+
options?: { expectedVersion?: number; clientMutationId?: string },
|
|
135
|
+
): Promise<{ record: SpaceRecord | null; deduplicated: boolean; queuedOffline?: boolean }>;
|
|
136
|
+
onChange(collection: string, handler: (records: SpaceRecord[]) => void): () => void;
|
|
137
|
+
};
|
|
138
|
+
connectivity: {
|
|
139
|
+
getState(): Promise<{ state: 'online' | 'offline'; pendingMutations?: number }>;
|
|
140
|
+
onChange(handler: (info: { state: 'online' | 'offline'; pendingMutations?: number }) => void): () => void;
|
|
141
|
+
};
|
|
142
|
+
device: {
|
|
143
|
+
haptics(style?: 'light' | 'medium' | 'selection' | 'success'): Promise<void>;
|
|
144
|
+
};
|
|
145
|
+
media: {
|
|
146
|
+
pickImage(options?: { source?: 'library' | 'camera' }): Promise<{
|
|
147
|
+
name: string;
|
|
148
|
+
mimeType: string;
|
|
149
|
+
size: number;
|
|
150
|
+
encoding: 'base64';
|
|
151
|
+
content: string;
|
|
152
|
+
} | null>;
|
|
153
|
+
};
|
|
106
154
|
ui: {
|
|
107
155
|
/** Open the host's permission review. This never grants access automatically. */
|
|
108
156
|
reviewPermissions(): Promise<void>;
|
|
@@ -112,6 +160,7 @@ export interface ExtensionClient {
|
|
|
112
160
|
title: string;
|
|
113
161
|
message?: string;
|
|
114
162
|
}): Promise<void>;
|
|
163
|
+
share(payload: { title?: string; text?: string; url?: string }): Promise<void>;
|
|
115
164
|
navigate(path: string): Promise<void>;
|
|
116
165
|
openProduct(reference: ProductReferenceLocator): Promise<void>;
|
|
117
166
|
/** Chat/tool widget iframe: host sends the tool result via `widget.data` after load. */
|
|
@@ -123,4 +172,3 @@ export interface ExtensionClient {
|
|
|
123
172
|
};
|
|
124
173
|
onDispose(handler: () => void): () => void;
|
|
125
174
|
}
|
|
126
|
-
import type { ProductReferenceLocator } from '@xopcai/gateway-contract';
|