-
Notifications
You must be signed in to change notification settings - Fork 1
/
puterjs.pas
592 lines (517 loc) · 17.6 KB
/
puterjs.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
unit puterjs;
{$mode objfpc}{$H+}
{$modeswitch externalclass}
interface
uses
Classes, SysUtils, JS, Web;
type
TPuterFSItem = Class external name 'FSItem' (TJSObject)
public
name, path, content: string;
size: Integer;
created: TJSDate;
isDirectory: Boolean;
function read: TJSPromise external name 'read';
end;
TPuterDirList = array of TPuterFSItem;
TPuterUserInfo = class(TJSObject)
public
uuid, username: string;
email_confirmed: boolean;
end;
TPuterErrorMsg = class(TJSObject)
public
message, code: string;
end;
TPuterWindowOptions = Class external name 'Object' (TJSObject)
public
center, disable_parent_window, has_head, is_resizable, show_in_taskbar: boolean;
content, title, uri: string;
height, width: Integer;
end;
TPuterWebsite = class(TJSObject)
public
id, subdomain: string;
created_on: TJSDate;
end;
TPuterHostList = Array of TPuterWebsite;
TLaunchCallback = reference to procedure(AItems: TJSArray);
TPuterFS = class(TJSObject)
public
function write(AFile, AData: string): TJSPromise external name 'write';
function read(AFile: string): TJSPromise external name 'read';
function mkdir(APath: string): TJSPromise external name 'mkdir';
function readdir(APath: string): TJSPromise external name 'readdir';
function rename(APath, ANewName: string): TJSPromise external name 'rename';
function copy(ASource, ADest: string): TJSPromise external name 'copy';
function move(ASource, ADest: string): TJSPromise external name 'move';
function stat(AFilePath: string): TJSPromise external name 'stat';
function delete(APath: string): TJSPromise external name 'delete';
function upload(AFileList: TJSHTMLFileList): TJSPromise external name 'upload';
end;
TPuterKeyValue = class(TJSObject)
public
function _set(AKey, AValue: string): TJSPromise external name 'set';
function _set(AKey: string; AValue: integer): TJSPromise external name 'set';
function get(AKey: string): TJSPromise external name 'get';
function incr(AKey: string): TJSPromise external name 'incr';
function incr(AKey: string; AAmount: integer): TJSPromise external name 'incr';
function decr(AKey: string): TJSPromise external name 'decr';
function decr(AKey: string; AAmount: integer): TJSPromise external name 'decr';
function del(AKey: string): TJSPromise external name 'del';
function list: TJSPromise external name 'list';
function list(APattern: string): TJSPromise external name 'list';
function flush: TJSPromise external name 'flush';
end;
TPuterHosting = class(TJSObject)
public
function create(ASubDomain, ADirPath: string): TJSPromise external name 'create';
function list: TJSPromise external name 'list';
function delete(ASubDomain: string): TJSPromise external name 'delete';
function update(ASubDomain, ADirPath: string): TJSPromise external name 'update';
function get(ASubDomain: string): TJSPromise external name 'get';
end;
TPuterAuth = class(TJSObject)
public
function signIn: TJSPromise external name 'signIn';
function signOut: TJSPromise external name 'signOut';
function isSignedIn: Boolean external name 'isSignedIn';
function getUser: TJSPromise external name 'getUser';
end;
{ TPuterUI }
TPuterUI = class(TJSObject)
public
env: string; external name 'env';
function authenticateWithPuter: TJSPromise external name 'authenticateWithPuter';
function alert(AMessage: string): TJSPromise external name 'alert';
function alert(AMessage: string; AButtons: TJSArray): TJSPromise external name 'alert';
function prompt(AMessage: string): TJSPromise external name 'prompt';
function prompt(AMessage, APlaceHolder: string): TJSPromise external name 'prompt';
procedure createWindow(options: TPuterWindowOptions) external name 'createWindow';
function launchApp: TJSPromise external name 'launchApp';
function launchApp(appName: string): TJSPromise external name 'launchApp';
function launchApp(appName: string; args: TJSObject): TJSPromise external name 'launchApp';
function launchApp(args: TJSObject): TJSPromise external name 'launchApp';
procedure onLaunchWithItems(ACallback: TLaunchCallback) external name 'onLaunchWithItems';
function showOpenFilePicker: TJSPromise external name 'showOpenFilePicker';
function showSaveFilePicker(data: string): TJSPromise external name 'showSaveFilePicker';
function showDirectoryPicker: TJSPromise external name 'showDirectoryPicker';
function showColorPicker: TJSPromise external name 'showColorPicker';
function showColorPicker(DefaultColor: string): TJSPromise external name 'showColorPicker';
function showFontPicker: TJSPromise external name 'showFontPicker';
function showFontPicker(DefaultFont: string): TJSPromise external name 'showFontPicker';
function setWindowTitle(ATitle: string): TJSPromise external name 'setWindowTitle';
procedure setWindowHeight(AHeight: integer) external name 'setWindowHeight';
procedure setWindowWidth(AWidth: integer) external name 'setWindowWidth';
procedure setWindowSize(AWidth, AHeight: integer) external name 'setWindowSize';
end;
TPuterJS = class(TJSObject)
public
fs: TPuterFS;
kv: TPuterKeyValue;
hosting: TPuterHosting;
auth: TPuterAuth;
ui: TPuterUI;
args: TJSObject;
function randName: string external name 'randName';
procedure exit external name 'exit';
end;
TPuterErrorEvent = reference to procedure(AError: TPuterErrorMsg);
TPuterFileEvent = reference to procedure(AFile: TPuterFSItem);
TReadFileEvent = reference to procedure(AContent: string);
TPuterDirectoryEvent = reference to procedure(ADir: TPuterFSItem);
TDirListEvent = reference to procedure(ADirList: TPuterDirList);
TUploadEvent = reference to procedure(AFileList: TPuterFSItem);
TPuterAuthEvent = reference to procedure;
TPuterAlertEvent = reference to procedure(ALabel: string);
TPuterAppLaunchEvent = reference to procedure;
TColorEvent = reference to procedure(AColor: string);
TFontEvent = reference to procedure(AFont: string);
{ TPuter }
TPuter = class(TComponent)
private
FOnLaunchWithItems: TLaunchCallback;
FOnPuterError: TPuterErrorEvent;
FOnWriteSuccess: TPuterFileEvent;
FOnReadSuccess: TReadFileEvent;
FOnDirSuccess: TPuterDirectoryEvent;
FOnDirListSuccess: TDirListEvent;
FOnRenameSuccess: TPuterFileEvent;
FOnCopySuccess: TPuterFileEvent;
FOnMoveSuccess: TPuterFileEvent;
FOnStatSuccess: TPuterFileEvent;
FOnUploadSuccess: TUploadEvent;
FOnOpenFileSuccess: TPuterFileEvent;
FOnAuthSuccess: TPuterAuthEvent;
FOnAlertButton: TPuterAlertEvent;
FOnAppLaunch: TPuterAppLaunchEvent;
FOnColorSuccess: TColorEvent;
FOnFontSuccess: TFontEvent;
FWindowHeight: integer;
FWindowTitle: string;
FWindowWidth: integer;
procedure SetOnLaunchWithItems(AValue: TLaunchCallback);
procedure SetWindowHeight(AValue: integer);
procedure SetWindowTitle(AValue: string);
procedure SetWindowWidth(AValue: integer);
function WriteSuccess(AValue: JSValue): JSValue;
function PuterError(AValue: JSValue): JSValue;
function ReadSuccess(AValue: JSValue): JSValue;
function MkdirSuccess(AValue: JSValue): JSValue;
function ReadDirSuccess(AValue: JSValue): JSValue;
function RenameSuccess(AValue: JSValue): JSValue;
function CopySuccess(AValue: JSValue): JSValue;
function MoveSuccess(AValue: JSValue): JSValue;
function StatSuccess(AValue: JSValue): JSValue;
function UploadSuccess(AValue: JSValue): JSValue;
procedure ReadFile(f: TPuterFSItem); async;
function OpenFile(AValue: JSValue): JSValue;
function SaveFile(AValue: JSValue): JSValue;
function AuthSuccess(AValue: JSValue): JSValue;
function AlertSuccess(AValue: JSValue): JSValue;
function AppLaunchSuccess(AValue: JSValue): JSValue;
function ColorSuccess(AValue: JSValue): JSValue;
function FontSuccess(AValue: JSValue): JSValue;
public
property OnPuterError: TPuterErrorEvent read FOnPuterError write FOnPuterError;
property OnWriteSuccess: TPuterFileEvent read FOnWriteSuccess write FOnWriteSuccess;
property OnReadSuccess: TReadFileEvent read FOnReadSuccess write FOnReadSuccess;
property OnDirSuccess: TPuterDirectoryEvent read FOnDirSuccess write FOnDirSuccess;
property OnDirListSuccess: TDirListEvent read FOnDirListSuccess write FOnDirListSuccess;
property OnRenameSuccess: TPuterFileEvent read FOnRenameSuccess write FOnRenameSuccess;
property OnCopySuccess: TPuterFileEvent read FOnCopySuccess write FOnCopySuccess;
property OnMoveSuccess: TPuterFileEvent read FOnMoveSuccess write FOnWriteSuccess;
property OnStatSuccess: TPuterFileEvent read FOnStatSuccess write FOnStatSuccess;
property OnUploadSuccess: TUploadEvent read FOnUploadSuccess write FOnUploadSuccess;
property OnOpenFileSuccess: TPuterFileEvent read FOnOpenFileSuccess write FOnOpenFileSuccess;
property OnAuthSuccess: TPuterAuthEvent read FOnAuthSuccess write FOnAuthSuccess;
property OnAlertButton: TPuterAlertEvent read FOnAlertButton write FOnAlertButton;
property OnAppLaunch: TPuterAppLaunchEvent read FOnAppLaunch write FOnAppLaunch;
property OnLaunchWithItems: TLaunchCallback read FOnLaunchWithItems write SetOnLaunchWithItems;
property OnColorSuccess: TColorEvent read FOnColorSuccess write FOnColorSuccess;
property OnFontSuccess: TFontEvent read FOnFontSuccess write FOnFontSuccess;
property WindowTitle: string read FWindowTitle write SetWindowTitle;
property WindowHeight: integer read FWindowHeight write SetWindowHeight;
property WindowWidth: integer read FWindowWidth write SetWindowWidth;
procedure WriteFile(AFile, AData: string);
procedure ReadFile(AFile: string); async;
procedure MakeDirectory(ADir: string);
procedure GetDirectory(ADir: string);
procedure RenameFile(AFile, ANewName: string);
procedure CopyFile(AFile, ADestDir: string);
procedure MoveFile(AFile, ADestDir: string);
procedure StatFile(AFile: string);
procedure DeleteFile(AFile: string); async;
procedure UploadFile(AFileList: TJSHTMLFileList);
procedure OpenFileDialog;
procedure SaveFileDialog(AData: string);
procedure AuthenticateWithPuter;
procedure Alert(AMessage: string);
procedure Alert(AMessage: string; AButtons: TJSArray);
procedure Prompt(AMessage: string);
procedure Prompt(AMessage, APlaceHolder: string);
procedure CreateWindow(AOptions: TPuterWindowOptions);
procedure Exit;
procedure LaunchApp;
procedure LaunchApp(AName: string);
procedure ColorPicker;
procedure ColorPicker(DefaultColor: string);
procedure FontPicker;
procedure FontPicker(DefaultFont: string);
end;
var
PuterAPI: TPuterJS; external name 'window.puter';
Puter: TPuter;
implementation
{ TPuterUI }
{ TPuter }
function TPuter.WriteSuccess(AValue: JSValue): JSValue;
begin
if Assigned(FOnWriteSuccess) then
FOnWriteSuccess(TPuterFSItem(AValue));
end;
procedure TPuter.SetOnLaunchWithItems(AValue: TLaunchCallback);
begin
if FOnLaunchWithItems=AValue then Exit;
PuterAPI.ui.onLaunchWithItems(AValue);
FOnLaunchWithItems:=AValue;
end;
procedure TPuter.SetWindowHeight(AValue: integer);
begin
if FWindowHeight=AValue then Exit;
PuterAPI.ui.setWindowHeight(AValue);
FWindowHeight:=AValue;
end;
procedure TPuter.SetWindowTitle(AValue: string);
begin
if FWindowTitle=AValue then Exit;
PuterAPI.ui.setWindowTitle(AValue);
FWindowTitle:=AValue;
end;
procedure TPuter.SetWindowWidth(AValue: integer);
begin
if FWindowWidth=AValue then Exit;
PuterAPI.ui.setWindowWidth(AValue);
FWindowWidth:=AValue;
end;
function TPuter.PuterError(AValue: JSValue): JSValue;
begin
if Assigned(FOnPuterError) then
FOnPuterError(TPuterErrorMsg(AValue));
end;
function TPuter.ReadSuccess(AValue: JSValue): JSValue;
begin
if Assigned(FOnReadSuccess) then
FOnReadSuccess(String(AValue));
end;
function TPuter.MkdirSuccess(AValue: JSValue): JSValue;
begin
if Assigned(FOnDirSuccess) then
FOnDirSuccess(TPuterFSItem(AValue));
end;
function TPuter.ReadDirSuccess(AValue: JSValue): JSValue;
begin
if Assigned(FOnDirListSuccess) then
FOnDirListSuccess(TPuterDirList(AValue));
end;
function TPuter.RenameSuccess(AValue: JSValue): JSValue;
begin
if Assigned(FOnRenameSuccess) then
FOnRenameSuccess(TPuterFSItem(AValue));
end;
function TPuter.CopySuccess(AValue: JSValue): JSValue;
begin
if Assigned(FOnCopySuccess) then
FOnCopySuccess(TPuterFSItem(AValue));
end;
function TPuter.MoveSuccess(AValue: JSValue): JSValue;
begin
if Assigned(FOnMoveSuccess) then
FOnMoveSuccess(TPuterFSItem(AValue));
end;
function TPuter.StatSuccess(AValue: JSValue): JSValue;
begin
if Assigned(FOnStatSuccess) then
FOnStatSuccess(TPuterFSItem(AValue));
end;
function TPuter.UploadSuccess(AValue: JSValue): JSValue;
begin
if Assigned(FOnUploadSuccess) then
FOnUploadSuccess(TPuterFSItem(AValue));
end;
procedure TPuter.ReadFile(f: TPuterFSItem);
var
b: TJSBlob;
begin
if Assigned(FOnOpenFileSuccess) then
begin
b:=AWait(TJSBlob, f.read);
f.content:=AWait(String, b.text);
FOnOpenFileSuccess(f);
end;
end;
function TPuter.OpenFile(AValue: JSValue): JSValue;
begin
ReadFile(TPuterFSItem(AValue));
end;
function TPuter.SaveFile(AValue: JSValue): JSValue;
begin
if Assigned(FOnWriteSuccess) then
FOnWriteSuccess(TPuterFSItem(AValue));
end;
function TPuter.AuthSuccess(AValue: JSValue): JSValue;
begin
if Assigned(FOnAuthSuccess) then
FOnAuthSuccess;
end;
function TPuter.AlertSuccess(AValue: JSValue): JSValue;
begin
if Assigned(FOnAlertButton) then
FOnAlertButton(String(AValue));
end;
function TPuter.AppLaunchSuccess(AValue: JSValue): JSValue;
begin
if Assigned(FOnAppLaunch) then
FOnAppLaunch;
end;
function TPuter.ColorSuccess(AValue: JSValue): JSValue;
begin
if Assigned(FOnColorSuccess) then
FOnColorSuccess(String(AValue));
end;
function TPuter.FontSuccess(AValue: JSValue): JSValue;
begin
if Assigned(FOnFontSuccess) then
FOnFontSuccess(String(AValue));
end;
procedure TPuter.WriteFile(AFile, AData: string);
var
p: TJSPromise;
begin
p:=PuterAPI.fs.write(AFile, AData);
p._then(@WriteSuccess, @PuterError);
end;
procedure TPuter.ReadFile(AFile: string);
var
s: TJSBlob;
p: TJSPromise;
begin
s:=AWait(TJSBlob, PuterAPI.fs.read(AFile));
p:=s.text;
p._then(@ReadSuccess, @PuterError);
end;
procedure TPuter.MakeDirectory(ADir: string);
var
p: TJSPromise;
begin
p:=PuterAPI.fs.mkdir(ADir);
p._then(@MkdirSuccess, @PuterError);
end;
procedure TPuter.GetDirectory(ADir: string);
var
p: TJSPromise;
begin
p:=PuterAPI.fs.readdir(ADir);
p._then(@ReadDirSuccess, @PuterError);
end;
procedure TPuter.RenameFile(AFile, ANewName: string);
var
p: TJSPromise;
begin
p:=PuterAPI.fs.rename(AFile, ANewName);
p._then(@RenameSuccess, @PuterError);
end;
procedure TPuter.CopyFile(AFile, ADestDir: string);
var
p: TJSPromise;
begin
p:=PuterAPI.fs.copy(AFile, ADestDir);
p._then(@CopySuccess, @PuterError);
end;
procedure TPuter.MoveFile(AFile, ADestDir: string);
var
p: TJSPromise;
begin
p:=PuterAPI.fs.move(AFile, ADestDir);
p._then(@MoveSuccess, @PuterError);
end;
procedure TPuter.StatFile(AFile: string);
var
p: TJSPromise;
begin
p:=PuterAPI.fs.stat(AFile);
p._then(@StatSuccess, @PuterError);
end;
procedure TPuter.DeleteFile(AFile: string);
begin
AWait(JSValue, PuterAPI.fs.delete(AFile));
end;
procedure TPuter.UploadFile(AFileList: TJSHTMLFileList);
var
p: TJSPromise;
begin
p:=PuterAPI.fs.upload(AFileList);
p._then(@UploadSuccess, @PuterError);
end;
procedure TPuter.OpenFileDialog;
var
p: TJSPromise;
begin
p:=PuterAPI.ui.showOpenFilePicker;
p._then(@OpenFile);
end;
procedure TPuter.SaveFileDialog(AData: string);
var
p: TJSPromise;
begin
p:=PuterAPI.ui.showSaveFilePicker(AData);
p._then(@SaveFile);
end;
procedure TPuter.AuthenticateWithPuter;
var
p: TJSPromise;
begin
p:=PuterAPI.ui.authenticateWithPuter;
p._then(@AuthSuccess, @PuterError);
end;
procedure TPuter.Alert(AMessage: string);
var
p: TJSPromise;
begin
p:=PuterAPI.ui.alert(AMessage);
p._then(@AlertSuccess, @PuterError);
end;
procedure TPuter.Alert(AMessage: string; AButtons: TJSArray);
var
p: TJSPromise;
begin
p:=PuterAPI.ui.alert(AMessage, AButtons);
p._then(@AlertSuccess, @PuterError);
end;
procedure TPuter.Prompt(AMessage: string);
var
p: TJSPromise;
begin
p:=PuterAPI.ui.prompt(AMessage);
p._then(@AlertSuccess, @PuterError);
end;
procedure TPuter.Prompt(AMessage, APlaceHolder: string);
var
p: TJSPromise;
begin
p:=PuterAPI.ui.prompt(AMessage, APlaceHolder);
p._then(@AlertSuccess, @PuterError);
end;
procedure TPuter.CreateWindow(AOptions: TPuterWindowOptions);
begin
PuterAPI.ui.createWindow(AOptions);
end;
procedure TPuter.Exit;
begin
PuterAPI.exit;
end;
procedure TPuter.LaunchApp;
var
p: TJSPromise;
begin
p:=PuterAPI.ui.launchApp;
p._then(@AppLaunchSuccess, @PuterError);
end;
procedure TPuter.LaunchApp(AName: string);
var
p: TJSPromise;
begin
p:=PuterAPI.ui.launchApp(AName);
p._then(@AppLaunchSuccess, @PuterError);
end;
procedure TPuter.ColorPicker;
var
p: TJSPromise;
begin
p:=PuterAPI.ui.showColorPicker;
p._then(@ColorSuccess, @PuterError);
end;
procedure TPuter.ColorPicker(DefaultColor: string);
var
p: TJSPromise;
begin
p:=PuterAPI.ui.showColorPicker(DefaultColor);
p._then(@ColorSuccess, @PuterError);
end;
procedure TPuter.FontPicker;
var
p: TJSPromise;
begin
p:=PuterAPI.ui.showFontPicker;
p._then(@FontSuccess, @PuterError);
end;
procedure TPuter.FontPicker(DefaultFont: string);
var
p: TJSPromise;
begin
p:=PuterAPI.ui.showFontPicker(DefaultFont);
p._then(@FontSuccess, @PuterError);
end;
initialization
Puter:=TPuter.Create(Nil);
end.