@bobfrankston/msgapidefs 0.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/README.md +132 -0
- package/msgapidefs.d.ts +322 -0
- package/msgapidefs.js +110 -0
- package/msgapidefs.ts +427 -0
- package/package.json +39 -0
- package/samples.html +431 -0
package/msgapidefs.ts
ADDED
|
@@ -0,0 +1,427 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* TypeScript definitions for the msgapi JavaScript API
|
|
3
|
+
*
|
|
4
|
+
* This module provides type-safe access to msgapi's window control,
|
|
5
|
+
* data persistence, and file system features (used by msgview and msger).
|
|
6
|
+
*
|
|
7
|
+
* Usage:
|
|
8
|
+
* ```typescript
|
|
9
|
+
* import type { MsgAPI } from '@bobfrankston/msgapidefs';
|
|
10
|
+
*
|
|
11
|
+
* declare global {
|
|
12
|
+
* interface Window {
|
|
13
|
+
* msgapi?: MsgAPI;
|
|
14
|
+
* }
|
|
15
|
+
* }
|
|
16
|
+
*
|
|
17
|
+
* // Check if running in msgview/msger
|
|
18
|
+
* if (window.msgapi) {
|
|
19
|
+
* window.msgapi.setTitle('My App');
|
|
20
|
+
* }
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Result object returned when the window closes
|
|
26
|
+
*/
|
|
27
|
+
export interface MsgResult {
|
|
28
|
+
/** Button that was clicked */
|
|
29
|
+
button: string;
|
|
30
|
+
/** Optional input field value */
|
|
31
|
+
value?: string;
|
|
32
|
+
/** Optional form data */
|
|
33
|
+
form?: Record<string, any>;
|
|
34
|
+
/** True if window was closed via close button */
|
|
35
|
+
closed?: boolean;
|
|
36
|
+
/** True if window was dismissed via Escape key */
|
|
37
|
+
dismissed?: boolean;
|
|
38
|
+
/** True if window was closed due to timeout */
|
|
39
|
+
timeout?: boolean;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* File information returned by file system operations
|
|
44
|
+
*/
|
|
45
|
+
export interface FileInfo {
|
|
46
|
+
/** File name */
|
|
47
|
+
name: string;
|
|
48
|
+
/** Full file path */
|
|
49
|
+
path: string;
|
|
50
|
+
/** True if this is a directory */
|
|
51
|
+
isDir: boolean;
|
|
52
|
+
/** File size in bytes (0 for directories) */
|
|
53
|
+
size: number;
|
|
54
|
+
/** Last modified timestamp (ISO 8601 string) */
|
|
55
|
+
modified?: string;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Options for file selection dialogs
|
|
60
|
+
*/
|
|
61
|
+
export interface FileDialogOptions {
|
|
62
|
+
/** Dialog title */
|
|
63
|
+
title?: string;
|
|
64
|
+
/** Default filename for save dialogs */
|
|
65
|
+
defaultFilename?: string;
|
|
66
|
+
/** File filters (e.g., [{name: "Text", extensions: ["txt", "md"]}]) */
|
|
67
|
+
filters?: Array<{name: string; extensions: string[]}>;
|
|
68
|
+
/** Default directory to open */
|
|
69
|
+
defaultPath?: string;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Selected file with content
|
|
74
|
+
*/
|
|
75
|
+
export interface SelectedFile {
|
|
76
|
+
/** File name */
|
|
77
|
+
name: string;
|
|
78
|
+
/** Full file path */
|
|
79
|
+
path: string;
|
|
80
|
+
/** File content as string */
|
|
81
|
+
content: string;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* msgapi JavaScript API
|
|
86
|
+
*
|
|
87
|
+
* Available when running inside msgview/msger via window.msgapi
|
|
88
|
+
*/
|
|
89
|
+
export interface MsgAPI {
|
|
90
|
+
// ========================================
|
|
91
|
+
// Window Control
|
|
92
|
+
// ========================================
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Toggle fullscreen mode on/off
|
|
96
|
+
* @example
|
|
97
|
+
* msgapi.toggleFullscreen();
|
|
98
|
+
*/
|
|
99
|
+
toggleFullscreen(): void;
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Set fullscreen mode
|
|
103
|
+
* @param enabled - true to enter fullscreen, false to exit
|
|
104
|
+
* @example
|
|
105
|
+
* msgapi.setFullscreen(true);
|
|
106
|
+
*/
|
|
107
|
+
setFullscreen(enabled: boolean): void;
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Minimize the window
|
|
111
|
+
* @example
|
|
112
|
+
* msgapi.minimize();
|
|
113
|
+
*/
|
|
114
|
+
minimize(): void;
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Toggle maximize/restore window
|
|
118
|
+
* @example
|
|
119
|
+
* msgapi.maximize();
|
|
120
|
+
*/
|
|
121
|
+
maximize(): void;
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Set window size
|
|
125
|
+
* @param width - Window width in pixels
|
|
126
|
+
* @param height - Window height in pixels
|
|
127
|
+
* @example
|
|
128
|
+
* msgapi.setSize(800, 600);
|
|
129
|
+
*/
|
|
130
|
+
setSize(width: number, height: number): void;
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Set window position
|
|
134
|
+
* @param x - X coordinate in pixels
|
|
135
|
+
* @param y - Y coordinate in pixels
|
|
136
|
+
* @example
|
|
137
|
+
* msgapi.setPosition(100, 100);
|
|
138
|
+
*/
|
|
139
|
+
setPosition(x: number, y: number): void;
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Set always-on-top behavior
|
|
143
|
+
* @param enabled - true to keep window on top
|
|
144
|
+
* @example
|
|
145
|
+
* msgapi.setAlwaysOnTop(true);
|
|
146
|
+
*/
|
|
147
|
+
setAlwaysOnTop(enabled: boolean): void;
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Set window title
|
|
151
|
+
* @param title - New window title
|
|
152
|
+
* @example
|
|
153
|
+
* msgapi.setTitle("My Application");
|
|
154
|
+
*/
|
|
155
|
+
setTitle(title: string): void;
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Close the window
|
|
159
|
+
* @param result - Optional result to return to parent process
|
|
160
|
+
* @example
|
|
161
|
+
* // Close with default dismissed result
|
|
162
|
+
* msgapi.close();
|
|
163
|
+
*
|
|
164
|
+
* // Close with custom result
|
|
165
|
+
* msgapi.close({button: 'OK', value: 'user data'});
|
|
166
|
+
*/
|
|
167
|
+
close(result?: Partial<MsgResult>): void;
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Send result and close window (legacy compatibility)
|
|
171
|
+
* @param result - Result to return to parent process
|
|
172
|
+
* @example
|
|
173
|
+
* msgapi.sendResult({button: 'Submit', value: 'form data'});
|
|
174
|
+
*/
|
|
175
|
+
sendResult(result: Partial<MsgResult>): void;
|
|
176
|
+
|
|
177
|
+
// ========================================
|
|
178
|
+
// Data Persistence (localStorage wrapper)
|
|
179
|
+
// ========================================
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Save data to persistent storage
|
|
183
|
+
* Data is stored with 'msgapi_' prefix in localStorage
|
|
184
|
+
* @param key - Storage key
|
|
185
|
+
* @param value - Value to store (will be JSON.stringify'd)
|
|
186
|
+
* @returns true on success, false on failure
|
|
187
|
+
* @example
|
|
188
|
+
* msgapi.saveData('userPrefs', {theme: 'dark', fontSize: 14});
|
|
189
|
+
*/
|
|
190
|
+
saveData(key: string, value: any): boolean;
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Load data from persistent storage
|
|
194
|
+
* @param key - Storage key
|
|
195
|
+
* @param defaultValue - Default value if key not found
|
|
196
|
+
* @returns Stored value or defaultValue
|
|
197
|
+
* @example
|
|
198
|
+
* const prefs = msgapi.loadData('userPrefs', {theme: 'light'});
|
|
199
|
+
*/
|
|
200
|
+
loadData<T = any>(key: string, defaultValue?: T): T;
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Remove data from persistent storage
|
|
204
|
+
* @param key - Storage key to remove
|
|
205
|
+
* @returns true on success, false on failure
|
|
206
|
+
* @example
|
|
207
|
+
* msgapi.removeData('tempData');
|
|
208
|
+
*/
|
|
209
|
+
removeData(key: string): boolean;
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* Clear all msgapi data from persistent storage
|
|
213
|
+
* Only removes items with 'msgapi_' prefix
|
|
214
|
+
* @returns true on success, false on failure
|
|
215
|
+
* @example
|
|
216
|
+
* msgapi.clearData();
|
|
217
|
+
*/
|
|
218
|
+
clearData(): boolean;
|
|
219
|
+
|
|
220
|
+
// ========================================
|
|
221
|
+
// File System Access (to be implemented)
|
|
222
|
+
// ========================================
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* File system operations namespace
|
|
226
|
+
*/
|
|
227
|
+
fs?: {
|
|
228
|
+
/**
|
|
229
|
+
* Select a file using native file picker
|
|
230
|
+
* @param options - File dialog options
|
|
231
|
+
* @returns Selected file with content, or null if cancelled
|
|
232
|
+
* @example
|
|
233
|
+
* const file = await msgapi.fs.selectFile({
|
|
234
|
+
* title: 'Open File',
|
|
235
|
+
* filters: [{name: 'Text', extensions: ['txt', 'md']}]
|
|
236
|
+
* });
|
|
237
|
+
* if (file) {
|
|
238
|
+
* console.log(file.content);
|
|
239
|
+
* }
|
|
240
|
+
*/
|
|
241
|
+
selectFile(options?: FileDialogOptions): Promise<SelectedFile | null>;
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* Select multiple files using native file picker
|
|
245
|
+
* @param options - File dialog options
|
|
246
|
+
* @returns Array of selected files with content
|
|
247
|
+
* @example
|
|
248
|
+
* const files = await msgapi.fs.selectFiles({
|
|
249
|
+
* title: 'Open Files'
|
|
250
|
+
* });
|
|
251
|
+
*/
|
|
252
|
+
selectFiles(options?: FileDialogOptions): Promise<SelectedFile[]>;
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* Save file using native save dialog
|
|
256
|
+
* @param content - File content to save
|
|
257
|
+
* @param defaultFilename - Default filename
|
|
258
|
+
* @param options - Additional dialog options
|
|
259
|
+
* @returns Path where file was saved, or null if cancelled
|
|
260
|
+
* @example
|
|
261
|
+
* const path = await msgapi.fs.saveFileAs('Hello World', 'greeting.txt');
|
|
262
|
+
*/
|
|
263
|
+
saveFileAs(
|
|
264
|
+
content: string,
|
|
265
|
+
defaultFilename?: string,
|
|
266
|
+
options?: FileDialogOptions
|
|
267
|
+
): Promise<string | null>;
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* Select a directory using native folder picker
|
|
271
|
+
* @param options - Dialog options
|
|
272
|
+
* @returns Selected directory path, or null if cancelled
|
|
273
|
+
* @example
|
|
274
|
+
* const dir = await msgapi.fs.selectFolder({
|
|
275
|
+
* title: 'Select Output Folder'
|
|
276
|
+
* });
|
|
277
|
+
*/
|
|
278
|
+
selectFolder(options?: FileDialogOptions): Promise<string | null>;
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* Read file contents
|
|
282
|
+
* Note: Requires allowFs: true flag when launching msgview/msger
|
|
283
|
+
* @param path - File path to read
|
|
284
|
+
* @returns File content as string
|
|
285
|
+
* @example
|
|
286
|
+
* const content = await msgapi.fs.read('/path/to/file.txt');
|
|
287
|
+
*/
|
|
288
|
+
read(path: string): Promise<string>;
|
|
289
|
+
|
|
290
|
+
/**
|
|
291
|
+
* Write file contents
|
|
292
|
+
* Note: Requires allowFs: true flag when launching msgview/msger
|
|
293
|
+
* @param path - File path to write
|
|
294
|
+
* @param content - Content to write
|
|
295
|
+
* @example
|
|
296
|
+
* await msgapi.fs.write('/path/to/file.txt', 'Hello World');
|
|
297
|
+
*/
|
|
298
|
+
write(path: string, content: string): Promise<void>;
|
|
299
|
+
|
|
300
|
+
/**
|
|
301
|
+
* List directory contents
|
|
302
|
+
* Note: Requires allowFs: true flag when launching msgview/msger
|
|
303
|
+
* @param path - Directory path to list
|
|
304
|
+
* @returns Array of file information
|
|
305
|
+
* @example
|
|
306
|
+
* const files = await msgapi.fs.list('/path/to/dir');
|
|
307
|
+
*/
|
|
308
|
+
list(path: string): Promise<FileInfo[]>;
|
|
309
|
+
|
|
310
|
+
/**
|
|
311
|
+
* Check if file or directory exists
|
|
312
|
+
* Note: Requires allowFs: true flag when launching msgview/msger
|
|
313
|
+
* @param path - Path to check
|
|
314
|
+
* @returns true if exists
|
|
315
|
+
* @example
|
|
316
|
+
* const exists = await msgapi.fs.exists('/path/to/file.txt');
|
|
317
|
+
*/
|
|
318
|
+
exists(path: string): Promise<boolean>;
|
|
319
|
+
|
|
320
|
+
/**
|
|
321
|
+
* Delete file or directory
|
|
322
|
+
* Note: Requires allowFs: true flag when launching msgview/msger
|
|
323
|
+
* @param path - Path to delete
|
|
324
|
+
* @example
|
|
325
|
+
* await msgapi.fs.delete('/path/to/file.txt');
|
|
326
|
+
*/
|
|
327
|
+
delete(path: string): Promise<void>;
|
|
328
|
+
};
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
/**
|
|
332
|
+
* Helper to check if code is running in msgview/msger
|
|
333
|
+
* @returns true if running in msgapi environment
|
|
334
|
+
* @example
|
|
335
|
+
* if (isMsgAPI()) {
|
|
336
|
+
* msgapi.setTitle('Running in msgview/msger!');
|
|
337
|
+
* } else {
|
|
338
|
+
* console.log('Running in browser');
|
|
339
|
+
* }
|
|
340
|
+
*/
|
|
341
|
+
export function isMsgAPI(): boolean {
|
|
342
|
+
return typeof window !== 'undefined' && typeof window.msgapi !== 'undefined';
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
/**
|
|
346
|
+
* Get msgapi API with fallback for browser compatibility
|
|
347
|
+
* @returns msgapi API if available, or stub implementation for browser
|
|
348
|
+
* @example
|
|
349
|
+
* const api = getMsgAPI();
|
|
350
|
+
* api.saveData('key', 'value'); // Works in both msgview/msger and browser
|
|
351
|
+
*/
|
|
352
|
+
export function getMsgAPI(): MsgAPI {
|
|
353
|
+
if (isMsgAPI()) {
|
|
354
|
+
return window.msgapi!;
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
// Return browser-compatible stub
|
|
358
|
+
return {
|
|
359
|
+
// Window control - no-op in browser
|
|
360
|
+
toggleFullscreen: () => {},
|
|
361
|
+
setFullscreen: () => {},
|
|
362
|
+
minimize: () => {},
|
|
363
|
+
maximize: () => {},
|
|
364
|
+
setSize: () => {},
|
|
365
|
+
setPosition: () => {},
|
|
366
|
+
setAlwaysOnTop: () => {},
|
|
367
|
+
setTitle: (title: string) => { document.title = title; },
|
|
368
|
+
close: () => { window.close(); },
|
|
369
|
+
sendResult: () => { window.close(); },
|
|
370
|
+
|
|
371
|
+
// Data persistence - use localStorage in browser
|
|
372
|
+
saveData: (key: string, value: any): boolean => {
|
|
373
|
+
try {
|
|
374
|
+
localStorage.setItem('msgapi_' + key, JSON.stringify(value));
|
|
375
|
+
return true;
|
|
376
|
+
} catch {
|
|
377
|
+
return false;
|
|
378
|
+
}
|
|
379
|
+
},
|
|
380
|
+
loadData: <T = any>(key: string, defaultValue?: T): T => {
|
|
381
|
+
try {
|
|
382
|
+
const item = localStorage.getItem('msgapi_' + key);
|
|
383
|
+
return item ? JSON.parse(item) : (defaultValue as T);
|
|
384
|
+
} catch {
|
|
385
|
+
return defaultValue as T;
|
|
386
|
+
}
|
|
387
|
+
},
|
|
388
|
+
removeData: (key: string): boolean => {
|
|
389
|
+
try {
|
|
390
|
+
localStorage.removeItem('msgapi_' + key);
|
|
391
|
+
return true;
|
|
392
|
+
} catch {
|
|
393
|
+
return false;
|
|
394
|
+
}
|
|
395
|
+
},
|
|
396
|
+
clearData: (): boolean => {
|
|
397
|
+
try {
|
|
398
|
+
const keys = Object.keys(localStorage);
|
|
399
|
+
for (const key of keys) {
|
|
400
|
+
if (key.startsWith('msgapi_')) {
|
|
401
|
+
localStorage.removeItem(key);
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
return true;
|
|
405
|
+
} catch {
|
|
406
|
+
return false;
|
|
407
|
+
}
|
|
408
|
+
},
|
|
409
|
+
|
|
410
|
+
// File system - not available in browser
|
|
411
|
+
fs: undefined
|
|
412
|
+
};
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
/**
|
|
416
|
+
* Declare global window.msgapi
|
|
417
|
+
*/
|
|
418
|
+
declare global {
|
|
419
|
+
interface Window {
|
|
420
|
+
msgapi?: MsgAPI;
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
export default {
|
|
425
|
+
isMsgAPI,
|
|
426
|
+
getMsgAPI
|
|
427
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@bobfrankston/msgapidefs",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "TypeScript definitions for msgapi JavaScript API (msgview/msger)",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./msgapidefs.js",
|
|
7
|
+
"types": "./msgapidefs.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"import": "./msgapidefs.js",
|
|
11
|
+
"types": "./msgapidefs.d.ts"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"msgapidefs.js",
|
|
16
|
+
"msgapidefs.d.ts",
|
|
17
|
+
"msgapidefs.ts",
|
|
18
|
+
"README.md",
|
|
19
|
+
"samples.html"
|
|
20
|
+
],
|
|
21
|
+
"keywords": [
|
|
22
|
+
"msgapi",
|
|
23
|
+
"msgview",
|
|
24
|
+
"msger",
|
|
25
|
+
"typescript",
|
|
26
|
+
"definitions",
|
|
27
|
+
"types",
|
|
28
|
+
"webview"
|
|
29
|
+
],
|
|
30
|
+
"author": "Bob Frankston",
|
|
31
|
+
"license": "ISC",
|
|
32
|
+
"repository": {
|
|
33
|
+
"type": "git",
|
|
34
|
+
"url": "git+https://github.com/BobFrankston/msgapidefs.git"
|
|
35
|
+
},
|
|
36
|
+
"publishConfig": {
|
|
37
|
+
"access": "public"
|
|
38
|
+
}
|
|
39
|
+
}
|