@lvce-editor/explorer-view 1.0.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.
@@ -0,0 +1,2043 @@
1
+ const Two = '2.0';
2
+ const create$4 = (method, params) => {
3
+ return {
4
+ jsonrpc: Two,
5
+ method,
6
+ params
7
+ };
8
+ };
9
+ const state = {
10
+ callbacks: Object.create(null)
11
+ };
12
+ const set = (id, fn) => {
13
+ state.callbacks[id] = fn;
14
+ };
15
+ const get = id => {
16
+ return state.callbacks[id];
17
+ };
18
+ const remove$1 = id => {
19
+ delete state.callbacks[id];
20
+ };
21
+ let id = 0;
22
+ const create$3 = () => {
23
+ return ++id;
24
+ };
25
+ const warn = (...args) => {
26
+ console.warn(...args);
27
+ };
28
+ const registerPromise = () => {
29
+ const id = create$3();
30
+ const {
31
+ resolve,
32
+ promise
33
+ } = Promise.withResolvers();
34
+ set(id, resolve);
35
+ return {
36
+ id,
37
+ promise
38
+ };
39
+ };
40
+ const resolve = (id, response) => {
41
+ const fn = get(id);
42
+ if (!fn) {
43
+ console.log(response);
44
+ warn(`callback ${id} may already be disposed`);
45
+ return;
46
+ }
47
+ fn(response);
48
+ remove$1(id);
49
+ };
50
+ const create$2 = (method, params) => {
51
+ const {
52
+ id,
53
+ promise
54
+ } = registerPromise();
55
+ const message = {
56
+ jsonrpc: Two,
57
+ method,
58
+ params,
59
+ id
60
+ };
61
+ return {
62
+ message,
63
+ promise
64
+ };
65
+ };
66
+ class JsonRpcError extends Error {
67
+ constructor(message) {
68
+ super(message);
69
+ this.name = 'JsonRpcError';
70
+ }
71
+ }
72
+ const NewLine$2 = '\n';
73
+ const DomException = 'DOMException';
74
+ const ReferenceError$1 = 'ReferenceError';
75
+ const SyntaxError$1 = 'SyntaxError';
76
+ const TypeError$1 = 'TypeError';
77
+ const getErrorConstructor = (message, type) => {
78
+ if (type) {
79
+ switch (type) {
80
+ case DomException:
81
+ return DOMException;
82
+ case TypeError$1:
83
+ return TypeError;
84
+ case SyntaxError$1:
85
+ return SyntaxError;
86
+ case ReferenceError$1:
87
+ return ReferenceError;
88
+ default:
89
+ return Error;
90
+ }
91
+ }
92
+ if (message.startsWith('TypeError: ')) {
93
+ return TypeError;
94
+ }
95
+ if (message.startsWith('SyntaxError: ')) {
96
+ return SyntaxError;
97
+ }
98
+ if (message.startsWith('ReferenceError: ')) {
99
+ return ReferenceError;
100
+ }
101
+ return Error;
102
+ };
103
+ const constructError = (message, type, name) => {
104
+ const ErrorConstructor = getErrorConstructor(message, type);
105
+ if (ErrorConstructor === DOMException && name) {
106
+ return new ErrorConstructor(message, name);
107
+ }
108
+ if (ErrorConstructor === Error) {
109
+ const error = new Error(message);
110
+ if (name && name !== 'VError') {
111
+ error.name = name;
112
+ }
113
+ return error;
114
+ }
115
+ return new ErrorConstructor(message);
116
+ };
117
+ const getNewLineIndex$1 = (string, startIndex = undefined) => {
118
+ return string.indexOf(NewLine$2, startIndex);
119
+ };
120
+ const getParentStack = error => {
121
+ let parentStack = error.stack || error.data || error.message || '';
122
+ if (parentStack.startsWith(' at')) {
123
+ parentStack = error.message + NewLine$2 + parentStack;
124
+ }
125
+ return parentStack;
126
+ };
127
+ const joinLines$1 = lines => {
128
+ return lines.join(NewLine$2);
129
+ };
130
+ const MethodNotFound = -32601;
131
+ const Custom = -32001;
132
+ const splitLines$1 = lines => {
133
+ return lines.split(NewLine$2);
134
+ };
135
+ const restoreJsonRpcError = error => {
136
+ if (error && error instanceof Error) {
137
+ return error;
138
+ }
139
+ const currentStack = joinLines$1(splitLines$1(new Error().stack || '').slice(1));
140
+ if (error && error.code && error.code === MethodNotFound) {
141
+ const restoredError = new JsonRpcError(error.message);
142
+ const parentStack = getParentStack(error);
143
+ restoredError.stack = parentStack + NewLine$2 + currentStack;
144
+ return restoredError;
145
+ }
146
+ if (error && error.message) {
147
+ const restoredError = constructError(error.message, error.type, error.name);
148
+ if (error.data) {
149
+ if (error.data.stack && error.data.type && error.message) {
150
+ restoredError.stack = error.data.type + ': ' + error.message + NewLine$2 + error.data.stack + NewLine$2 + currentStack;
151
+ } else if (error.data.stack) {
152
+ restoredError.stack = error.data.stack;
153
+ }
154
+ if (error.data.codeFrame) {
155
+ // @ts-ignore
156
+ restoredError.codeFrame = error.data.codeFrame;
157
+ }
158
+ if (error.data.code) {
159
+ // @ts-ignore
160
+ restoredError.code = error.data.code;
161
+ }
162
+ if (error.data.type) {
163
+ // @ts-ignore
164
+ restoredError.name = error.data.type;
165
+ }
166
+ } else {
167
+ if (error.stack) {
168
+ const lowerStack = restoredError.stack || '';
169
+ // @ts-ignore
170
+ const indexNewLine = getNewLineIndex$1(lowerStack);
171
+ const parentStack = getParentStack(error);
172
+ // @ts-ignore
173
+ restoredError.stack = parentStack + lowerStack.slice(indexNewLine);
174
+ }
175
+ if (error.codeFrame) {
176
+ // @ts-ignore
177
+ restoredError.codeFrame = error.codeFrame;
178
+ }
179
+ }
180
+ return restoredError;
181
+ }
182
+ if (typeof error === 'string') {
183
+ return new Error(`JsonRpc Error: ${error}`);
184
+ }
185
+ return new Error(`JsonRpc Error: ${error}`);
186
+ };
187
+ const unwrapJsonRpcResult = responseMessage => {
188
+ if ('error' in responseMessage) {
189
+ const restoredError = restoreJsonRpcError(responseMessage.error);
190
+ throw restoredError;
191
+ }
192
+ if ('result' in responseMessage) {
193
+ return responseMessage.result;
194
+ }
195
+ throw new JsonRpcError('unexpected response message');
196
+ };
197
+ const E_COMMAND_NOT_FOUND = 'E_COMMAND_NOT_FOUND';
198
+ const getErrorType = prettyError => {
199
+ if (prettyError && prettyError.type) {
200
+ return prettyError.type;
201
+ }
202
+ if (prettyError && prettyError.constructor && prettyError.constructor.name) {
203
+ return prettyError.constructor.name;
204
+ }
205
+ return undefined;
206
+ };
207
+ const getErrorProperty = (error, prettyError) => {
208
+ if (error && error.code === E_COMMAND_NOT_FOUND) {
209
+ return {
210
+ code: MethodNotFound,
211
+ message: error.message,
212
+ data: error.stack
213
+ };
214
+ }
215
+ return {
216
+ code: Custom,
217
+ message: prettyError.message,
218
+ data: {
219
+ stack: prettyError.stack,
220
+ codeFrame: prettyError.codeFrame,
221
+ type: getErrorType(prettyError),
222
+ code: prettyError.code,
223
+ name: prettyError.name
224
+ }
225
+ };
226
+ };
227
+ const create$1$1 = (message, error) => {
228
+ return {
229
+ jsonrpc: Two,
230
+ id: message.id,
231
+ error
232
+ };
233
+ };
234
+ const getErrorResponse = (message, error, preparePrettyError, logError) => {
235
+ const prettyError = preparePrettyError(error);
236
+ logError(error, prettyError);
237
+ const errorProperty = getErrorProperty(error, prettyError);
238
+ return create$1$1(message, errorProperty);
239
+ };
240
+ const create = (message, result) => {
241
+ return {
242
+ jsonrpc: Two,
243
+ id: message.id,
244
+ result: result ?? null
245
+ };
246
+ };
247
+ const getSuccessResponse = (message, result) => {
248
+ const resultProperty = result ?? null;
249
+ return create(message, resultProperty);
250
+ };
251
+ const getResponse = async (message, ipc, execute, preparePrettyError, logError, requiresSocket) => {
252
+ try {
253
+ const result = requiresSocket(message.method) ? await execute(message.method, ipc, ...message.params) : await execute(message.method, ...message.params);
254
+ return getSuccessResponse(message, result);
255
+ } catch (error) {
256
+ return getErrorResponse(message, error, preparePrettyError, logError);
257
+ }
258
+ };
259
+ const defaultPreparePrettyError = error => {
260
+ return error;
261
+ };
262
+ const defaultLogError = () => {
263
+ // ignore
264
+ };
265
+ const defaultRequiresSocket = () => {
266
+ return false;
267
+ };
268
+ const defaultResolve = resolve;
269
+
270
+ // TODO maybe remove this in v6 or v7, only accept options object to simplify the code
271
+ const normalizeParams = args => {
272
+ if (args.length === 1) {
273
+ const options = args[0];
274
+ return {
275
+ ipc: options.ipc,
276
+ message: options.message,
277
+ execute: options.execute,
278
+ resolve: options.resolve || defaultResolve,
279
+ preparePrettyError: options.preparePrettyError || defaultPreparePrettyError,
280
+ logError: options.logError || defaultLogError,
281
+ requiresSocket: options.requiresSocket || defaultRequiresSocket
282
+ };
283
+ }
284
+ return {
285
+ ipc: args[0],
286
+ message: args[1],
287
+ execute: args[2],
288
+ resolve: args[3],
289
+ preparePrettyError: args[4],
290
+ logError: args[5],
291
+ requiresSocket: args[6]
292
+ };
293
+ };
294
+ const handleJsonRpcMessage = async (...args) => {
295
+ const options = normalizeParams(args);
296
+ const {
297
+ message,
298
+ ipc,
299
+ execute,
300
+ resolve,
301
+ preparePrettyError,
302
+ logError,
303
+ requiresSocket
304
+ } = options;
305
+ if ('id' in message) {
306
+ if ('method' in message) {
307
+ const response = await getResponse(message, ipc, execute, preparePrettyError, logError, requiresSocket);
308
+ try {
309
+ ipc.send(response);
310
+ } catch (error) {
311
+ const errorResponse = getErrorResponse(message, error, preparePrettyError, logError);
312
+ ipc.send(errorResponse);
313
+ }
314
+ return;
315
+ }
316
+ resolve(message.id, message);
317
+ return;
318
+ }
319
+ if ('method' in message) {
320
+ await getResponse(message, ipc, execute, preparePrettyError, logError, requiresSocket);
321
+ return;
322
+ }
323
+ throw new JsonRpcError('unexpected message');
324
+ };
325
+ const invokeHelper = async (ipc, method, params, useSendAndTransfer) => {
326
+ const {
327
+ message,
328
+ promise
329
+ } = create$2(method, params);
330
+ if (useSendAndTransfer && ipc.sendAndTransfer) {
331
+ ipc.sendAndTransfer(message);
332
+ } else {
333
+ ipc.send(message);
334
+ }
335
+ const responseMessage = await promise;
336
+ return unwrapJsonRpcResult(responseMessage);
337
+ };
338
+ const send = (transport, method, ...params) => {
339
+ const message = create$4(method, params);
340
+ transport.send(message);
341
+ };
342
+ const invoke = (ipc, method, ...params) => {
343
+ return invokeHelper(ipc, method, params, false);
344
+ };
345
+ const invokeAndTransfer = (ipc, method, ...params) => {
346
+ return invokeHelper(ipc, method, params, true);
347
+ };
348
+
349
+ const commands = Object.create(null);
350
+ const register = commandMap => {
351
+ Object.assign(commands, commandMap);
352
+ };
353
+ const getCommand = key => {
354
+ return commands[key];
355
+ };
356
+ const execute = (command, ...args) => {
357
+ const fn = getCommand(command);
358
+ if (!fn) {
359
+ throw new Error(`command not found ${command}`);
360
+ }
361
+ return fn(...args);
362
+ };
363
+
364
+ const getData$1 = event => {
365
+ return event.data;
366
+ };
367
+ const attachEvents = that => {
368
+ const handleMessage = (...args) => {
369
+ const data = that.getData(...args);
370
+ that.dispatchEvent(new MessageEvent('message', {
371
+ data
372
+ }));
373
+ };
374
+ that.onMessage(handleMessage);
375
+ const handleClose = event => {
376
+ that.dispatchEvent(new Event('close'));
377
+ };
378
+ that.onClose(handleClose);
379
+ };
380
+ class Ipc extends EventTarget {
381
+ constructor(rawIpc) {
382
+ super();
383
+ this._rawIpc = rawIpc;
384
+ attachEvents(this);
385
+ }
386
+ }
387
+ const readyMessage = 'ready';
388
+ const walkValue = (value, transferrables, isTransferrable) => {
389
+ if (!value) {
390
+ return;
391
+ }
392
+ if (isTransferrable(value)) {
393
+ transferrables.push(value);
394
+ return;
395
+ }
396
+ if (Array.isArray(value)) {
397
+ for (const item of value) {
398
+ walkValue(item, transferrables, isTransferrable);
399
+ }
400
+ return;
401
+ }
402
+ if (typeof value === 'object') {
403
+ for (const property of Object.values(value)) {
404
+ walkValue(property, transferrables, isTransferrable);
405
+ }
406
+ return;
407
+ }
408
+ };
409
+ const isMessagePort = value => {
410
+ return value && value instanceof MessagePort;
411
+ };
412
+ const isMessagePortMain = value => {
413
+ return value && value.constructor && value.constructor.name === 'MessagePortMain';
414
+ };
415
+ const isOffscreenCanvas = value => {
416
+ return typeof OffscreenCanvas !== 'undefined' && value instanceof OffscreenCanvas;
417
+ };
418
+ const isInstanceOf = (value, constructorName) => {
419
+ return value?.constructor?.name === constructorName;
420
+ };
421
+ const isSocket = value => {
422
+ return isInstanceOf(value, 'Socket');
423
+ };
424
+ const transferrables = [isMessagePort, isMessagePortMain, isOffscreenCanvas, isSocket];
425
+ const isTransferrable = value => {
426
+ for (const fn of transferrables) {
427
+ if (fn(value)) {
428
+ return true;
429
+ }
430
+ }
431
+ return false;
432
+ };
433
+ const getTransferrables = value => {
434
+ const transferrables = [];
435
+ walkValue(value, transferrables, isTransferrable);
436
+ return transferrables;
437
+ };
438
+ const listen$2 = () => {
439
+ // @ts-ignore
440
+ if (typeof WorkerGlobalScope === 'undefined') {
441
+ throw new TypeError('module is not in web worker scope');
442
+ }
443
+ return globalThis;
444
+ };
445
+ const signal$2 = global => {
446
+ global.postMessage(readyMessage);
447
+ };
448
+ class IpcChildWithModuleWorker extends Ipc {
449
+ getData(event) {
450
+ return getData$1(event);
451
+ }
452
+ send(message) {
453
+ // @ts-ignore
454
+ this._rawIpc.postMessage(message);
455
+ }
456
+ sendAndTransfer(message) {
457
+ const transfer = getTransferrables(message);
458
+ // @ts-ignore
459
+ this._rawIpc.postMessage(message, transfer);
460
+ }
461
+ dispose() {
462
+ // ignore
463
+ }
464
+ onClose(callback) {
465
+ // ignore
466
+ }
467
+ onMessage(callback) {
468
+ this._rawIpc.addEventListener('message', callback);
469
+ }
470
+ }
471
+ const wrap$5 = global => {
472
+ return new IpcChildWithModuleWorker(global);
473
+ };
474
+ const E_INCOMPATIBLE_NATIVE_MODULE = 'E_INCOMPATIBLE_NATIVE_MODULE';
475
+ const E_MODULES_NOT_SUPPORTED_IN_ELECTRON = 'E_MODULES_NOT_SUPPORTED_IN_ELECTRON';
476
+ const ERR_MODULE_NOT_FOUND = 'ERR_MODULE_NOT_FOUND';
477
+ const NewLine$1 = '\n';
478
+ const joinLines = lines => {
479
+ return lines.join(NewLine$1);
480
+ };
481
+ const splitLines = lines => {
482
+ return lines.split(NewLine$1);
483
+ };
484
+ const isModuleNotFoundMessage = line => {
485
+ return line.includes('[ERR_MODULE_NOT_FOUND]');
486
+ };
487
+ const getModuleNotFoundError = stderr => {
488
+ const lines = splitLines(stderr);
489
+ const messageIndex = lines.findIndex(isModuleNotFoundMessage);
490
+ const message = lines[messageIndex];
491
+ return {
492
+ message,
493
+ code: ERR_MODULE_NOT_FOUND
494
+ };
495
+ };
496
+ const RE_NATIVE_MODULE_ERROR = /^innerError Error: Cannot find module '.*.node'/;
497
+ const RE_NATIVE_MODULE_ERROR_2 = /was compiled against a different Node.js version/;
498
+ const RE_MESSAGE_CODE_BLOCK_START = /^Error: The module '.*'$/;
499
+ const RE_MESSAGE_CODE_BLOCK_END = /^\s* at/;
500
+ const RE_AT = /^\s+at/;
501
+ const RE_AT_PROMISE_INDEX = /^\s*at async Promise.all \(index \d+\)$/;
502
+ const isUnhelpfulNativeModuleError = stderr => {
503
+ return RE_NATIVE_MODULE_ERROR.test(stderr) && RE_NATIVE_MODULE_ERROR_2.test(stderr);
504
+ };
505
+ const isMessageCodeBlockStartIndex = line => {
506
+ return RE_MESSAGE_CODE_BLOCK_START.test(line);
507
+ };
508
+ const isMessageCodeBlockEndIndex = line => {
509
+ return RE_MESSAGE_CODE_BLOCK_END.test(line);
510
+ };
511
+ const getMessageCodeBlock = stderr => {
512
+ const lines = splitLines(stderr);
513
+ const startIndex = lines.findIndex(isMessageCodeBlockStartIndex);
514
+ const endIndex = startIndex + lines.slice(startIndex).findIndex(isMessageCodeBlockEndIndex, startIndex);
515
+ const relevantLines = lines.slice(startIndex, endIndex);
516
+ const relevantMessage = relevantLines.join(' ').slice('Error: '.length);
517
+ return relevantMessage;
518
+ };
519
+ const getNativeModuleErrorMessage = stderr => {
520
+ const message = getMessageCodeBlock(stderr);
521
+ return {
522
+ message: `Incompatible native node module: ${message}`,
523
+ code: E_INCOMPATIBLE_NATIVE_MODULE
524
+ };
525
+ };
526
+ const isModulesSyntaxError = stderr => {
527
+ if (!stderr) {
528
+ return false;
529
+ }
530
+ return stderr.includes('SyntaxError: Cannot use import statement outside a module');
531
+ };
532
+ const getModuleSyntaxError = () => {
533
+ return {
534
+ message: `ES Modules are not supported in electron`,
535
+ code: E_MODULES_NOT_SUPPORTED_IN_ELECTRON
536
+ };
537
+ };
538
+ const isModuleNotFoundError = stderr => {
539
+ if (!stderr) {
540
+ return false;
541
+ }
542
+ return stderr.includes('ERR_MODULE_NOT_FOUND');
543
+ };
544
+ const isNormalStackLine = line => {
545
+ return RE_AT.test(line) && !RE_AT_PROMISE_INDEX.test(line);
546
+ };
547
+ const getDetails = lines => {
548
+ const index = lines.findIndex(isNormalStackLine);
549
+ if (index === -1) {
550
+ return {
551
+ actualMessage: joinLines(lines),
552
+ rest: []
553
+ };
554
+ }
555
+ let lastIndex = index - 1;
556
+ while (++lastIndex < lines.length) {
557
+ if (!isNormalStackLine(lines[lastIndex])) {
558
+ break;
559
+ }
560
+ }
561
+ return {
562
+ actualMessage: lines[index - 1],
563
+ rest: lines.slice(index, lastIndex)
564
+ };
565
+ };
566
+ const getHelpfulChildProcessError = (stdout, stderr) => {
567
+ if (isUnhelpfulNativeModuleError(stderr)) {
568
+ return getNativeModuleErrorMessage(stderr);
569
+ }
570
+ if (isModulesSyntaxError(stderr)) {
571
+ return getModuleSyntaxError();
572
+ }
573
+ if (isModuleNotFoundError(stderr)) {
574
+ return getModuleNotFoundError(stderr);
575
+ }
576
+ const lines = splitLines(stderr);
577
+ const {
578
+ actualMessage,
579
+ rest
580
+ } = getDetails(lines);
581
+ return {
582
+ message: `${actualMessage}`,
583
+ code: '',
584
+ stack: rest
585
+ };
586
+ };
587
+ const normalizeLine = line => {
588
+ if (line.startsWith('Error: ')) {
589
+ return line.slice('Error: '.length);
590
+ }
591
+ if (line.startsWith('VError: ')) {
592
+ return line.slice('VError: '.length);
593
+ }
594
+ return line;
595
+ };
596
+ const getCombinedMessage = (error, message) => {
597
+ const stringifiedError = normalizeLine(`${error}`);
598
+ if (message) {
599
+ return `${message}: ${stringifiedError}`;
600
+ }
601
+ return stringifiedError;
602
+ };
603
+ const NewLine = '\n';
604
+ const getNewLineIndex = (string, startIndex = undefined) => {
605
+ return string.indexOf(NewLine, startIndex);
606
+ };
607
+ const mergeStacks = (parent, child) => {
608
+ if (!child) {
609
+ return parent;
610
+ }
611
+ const parentNewLineIndex = getNewLineIndex(parent);
612
+ const childNewLineIndex = getNewLineIndex(child);
613
+ if (childNewLineIndex === -1) {
614
+ return parent;
615
+ }
616
+ const parentFirstLine = parent.slice(0, parentNewLineIndex);
617
+ const childRest = child.slice(childNewLineIndex);
618
+ const childFirstLine = normalizeLine(child.slice(0, childNewLineIndex));
619
+ if (parentFirstLine.includes(childFirstLine)) {
620
+ return parentFirstLine + childRest;
621
+ }
622
+ return child;
623
+ };
624
+ class VError extends Error {
625
+ constructor(error, message) {
626
+ const combinedMessage = getCombinedMessage(error, message);
627
+ super(combinedMessage);
628
+ this.name = 'VError';
629
+ if (error instanceof Error) {
630
+ this.stack = mergeStacks(this.stack, error.stack);
631
+ }
632
+ if (error.codeFrame) {
633
+ // @ts-ignore
634
+ this.codeFrame = error.codeFrame;
635
+ }
636
+ if (error.code) {
637
+ // @ts-ignore
638
+ this.code = error.code;
639
+ }
640
+ }
641
+ }
642
+ class IpcError extends VError {
643
+ // @ts-ignore
644
+ constructor(betterMessage, stdout = '', stderr = '') {
645
+ if (stdout || stderr) {
646
+ // @ts-ignore
647
+ const {
648
+ message,
649
+ code,
650
+ stack
651
+ } = getHelpfulChildProcessError(stdout, stderr);
652
+ const cause = new Error(message);
653
+ // @ts-ignore
654
+ cause.code = code;
655
+ cause.stack = stack;
656
+ super(cause, betterMessage);
657
+ } else {
658
+ super(betterMessage);
659
+ }
660
+ // @ts-ignore
661
+ this.name = 'IpcError';
662
+ // @ts-ignore
663
+ this.stdout = stdout;
664
+ // @ts-ignore
665
+ this.stderr = stderr;
666
+ }
667
+ }
668
+ const withResolvers = () => {
669
+ let _resolve;
670
+ const promise = new Promise(resolve => {
671
+ _resolve = resolve;
672
+ });
673
+ return {
674
+ resolve: _resolve,
675
+ promise
676
+ };
677
+ };
678
+ const waitForFirstMessage = async port => {
679
+ const {
680
+ resolve,
681
+ promise
682
+ } = withResolvers();
683
+ port.addEventListener('message', resolve, {
684
+ once: true
685
+ });
686
+ const event = await promise;
687
+ // @ts-ignore
688
+ return event.data;
689
+ };
690
+ const listen$1$1 = async () => {
691
+ const parentIpcRaw = listen$2();
692
+ signal$2(parentIpcRaw);
693
+ const parentIpc = wrap$5(parentIpcRaw);
694
+ const firstMessage = await waitForFirstMessage(parentIpc);
695
+ if (firstMessage.method !== 'initialize') {
696
+ throw new IpcError('unexpected first message');
697
+ }
698
+ const type = firstMessage.params[0];
699
+ if (type === 'message-port') {
700
+ parentIpc.send({
701
+ jsonrpc: '2.0',
702
+ id: firstMessage.id,
703
+ result: null
704
+ });
705
+ parentIpc.dispose();
706
+ const port = firstMessage.params[1];
707
+ return port;
708
+ }
709
+ return globalThis;
710
+ };
711
+ class IpcChildWithModuleWorkerAndMessagePort extends Ipc {
712
+ constructor(port) {
713
+ super(port);
714
+ }
715
+ getData(event) {
716
+ return getData$1(event);
717
+ }
718
+ send(message) {
719
+ this._rawIpc.postMessage(message);
720
+ }
721
+ sendAndTransfer(message) {
722
+ const transfer = getTransferrables(message);
723
+ this._rawIpc.postMessage(message, transfer);
724
+ }
725
+ dispose() {
726
+ if (this._rawIpc.close) {
727
+ this._rawIpc.close();
728
+ }
729
+ }
730
+ onClose(callback) {
731
+ // ignore
732
+ }
733
+ onMessage(callback) {
734
+ this._rawIpc.addEventListener('message', callback);
735
+ this._rawIpc.start();
736
+ }
737
+ }
738
+ const wrap$4 = port => {
739
+ return new IpcChildWithModuleWorkerAndMessagePort(port);
740
+ };
741
+ const IpcChildWithModuleWorkerAndMessagePort$1 = {
742
+ __proto__: null,
743
+ listen: listen$1$1,
744
+ wrap: wrap$4
745
+ };
746
+
747
+ const createRpc = ipc => {
748
+ const rpc = {
749
+ /**
750
+ * @deprecated
751
+ */
752
+ send(method, ...params) {
753
+ send(ipc, method, ...params);
754
+ },
755
+ invoke(method, ...params) {
756
+ return invoke(ipc, method, ...params);
757
+ },
758
+ invokeAndTransfer(method, ...params) {
759
+ return invokeAndTransfer(ipc, method, ...params);
760
+ }
761
+ };
762
+ return rpc;
763
+ };
764
+ const requiresSocket = () => {
765
+ return false;
766
+ };
767
+ const preparePrettyError = error => {
768
+ return error;
769
+ };
770
+ const logError = () => {
771
+ // handled by renderer worker
772
+ };
773
+ const handleMessage = event => {
774
+ return handleJsonRpcMessage(event.target, event.data, execute, resolve, preparePrettyError, logError, requiresSocket);
775
+ };
776
+ const handleIpc = ipc => {
777
+ ipc.addEventListener('message', handleMessage);
778
+ };
779
+
780
+ // @ts-ignore
781
+ const listen$1 = async () => {
782
+ const module = IpcChildWithModuleWorkerAndMessagePort$1;
783
+ const rawIpc = await module.listen();
784
+ const ipc = module.wrap(rawIpc);
785
+ return ipc;
786
+ };
787
+ const create$1 = async ({
788
+ commandMap
789
+ }) => {
790
+ // TODO create a commandMap per rpc instance
791
+ register(commandMap);
792
+ const ipc = await listen$1();
793
+ handleIpc(ipc);
794
+ const rpc = createRpc(ipc);
795
+ return rpc;
796
+ };
797
+ const WebWorkerRpcClient = {
798
+ __proto__: null,
799
+ create: create$1
800
+ };
801
+
802
+ const BlockDevice = 1;
803
+ const CharacterDevice = 2;
804
+ const Directory = 3;
805
+ const DirectoryExpanded = 4;
806
+ const DirectoryExpanding = 5;
807
+ const File = 7;
808
+ const Socket = 8;
809
+ const Symlink = 9;
810
+ const SymLinkFile = 10;
811
+ const SymLinkFolder = 11;
812
+ const Unknown = 12;
813
+
814
+ class AssertionError extends Error {
815
+ constructor(message) {
816
+ super(message);
817
+ this.name = 'AssertionError';
818
+ }
819
+ }
820
+ const getType = value => {
821
+ switch (typeof value) {
822
+ case 'number':
823
+ return 'number';
824
+ case 'function':
825
+ return 'function';
826
+ case 'string':
827
+ return 'string';
828
+ case 'object':
829
+ if (value === null) {
830
+ return 'null';
831
+ }
832
+ if (Array.isArray(value)) {
833
+ return 'array';
834
+ }
835
+ return 'object';
836
+ case 'boolean':
837
+ return 'boolean';
838
+ default:
839
+ return 'unknown';
840
+ }
841
+ };
842
+ const object = value => {
843
+ const type = getType(value);
844
+ if (type !== 'object') {
845
+ throw new AssertionError('expected value to be of type object');
846
+ }
847
+ };
848
+ const array = value => {
849
+ const type = getType(value);
850
+ if (type !== 'array') {
851
+ throw new AssertionError('expected value to be of type array');
852
+ }
853
+ };
854
+ const string = value => {
855
+ const type = getType(value);
856
+ if (type !== 'string') {
857
+ throw new AssertionError('expected value to be of type string');
858
+ }
859
+ };
860
+
861
+ const ENOENT = 'ENOENT';
862
+
863
+ const remove = async diren => {};
864
+ const readDirWithFileTypes = async uri => {};
865
+ const getPathSeparator$1 = async root => {};
866
+ const getRealPath = async path => {};
867
+ const stat = async dirent => {};
868
+
869
+ const getFileIcon = ({
870
+ name
871
+ }) => {
872
+ return '';
873
+ };
874
+ const getIcon = dirent => {
875
+ return '';
876
+ };
877
+
878
+ const RE_CHARACTERS = /^[a-zA-Z.-]+$/;
879
+ const compareStringNumeric = (a, b) => {
880
+ if (RE_CHARACTERS.test(a) && RE_CHARACTERS.test(b)) {
881
+ return a < b ? -1 : 1;
882
+ }
883
+ return a.localeCompare(b, 'en', {
884
+ numeric: true
885
+ });
886
+ };
887
+
888
+ const priorityMapFoldersFirst = {
889
+ [Directory]: 1,
890
+ [SymLinkFolder]: 1,
891
+ [File]: 0,
892
+ [SymLinkFile]: 0,
893
+ [Unknown]: 0,
894
+ [Socket]: 0
895
+ };
896
+ const compareDirentType = (direntA, direntB) => {
897
+ return priorityMapFoldersFirst[direntB.type] - priorityMapFoldersFirst[direntA.type];
898
+ };
899
+ const compareDirentName = (direntA, direntB) => {
900
+ return compareStringNumeric(direntA.name, direntB.name);
901
+ };
902
+ const compareDirent = (direntA, direntB) => {
903
+ return compareDirentType(direntA, direntB) || compareDirentName(direntA, direntB);
904
+ };
905
+ const sortExplorerItems = rawDirents => {
906
+ rawDirents.sort(compareDirent);
907
+ };
908
+
909
+ const toDisplayDirents = (pathSeparator, rawDirents, parentDirent, excluded) => {
910
+ sortExplorerItems(rawDirents);
911
+ // TODO figure out whether this uses too much memory (name,path -> redundant, depth could be computed on demand)
912
+ const toDisplayDirent = (rawDirent, index) => {
913
+ const path = [parentDirent.path, rawDirent.name].join(pathSeparator);
914
+ return {
915
+ name: rawDirent.name,
916
+ posInSet: index + 1,
917
+ setSize: rawDirents.length,
918
+ depth: parentDirent.depth + 1,
919
+ type: rawDirent.type,
920
+ path,
921
+ // TODO storing absolute path might be too costly, could also store relative path here
922
+ icon: getIcon()
923
+ };
924
+ };
925
+ const result = [];
926
+ let i = 0;
927
+ for (const rawDirent of rawDirents) {
928
+ if (excluded.includes(rawDirent.name)) {
929
+ continue;
930
+ }
931
+ result.push(toDisplayDirent(rawDirent, i));
932
+ i++;
933
+ }
934
+ return result;
935
+ };
936
+
937
+ const getIndexFromPosition = (state, eventX, eventY) => {
938
+ const {
939
+ y,
940
+ itemHeight,
941
+ items
942
+ } = state;
943
+ const index = Math.floor((eventY - y) / itemHeight);
944
+ if (index < 0) {
945
+ return 0;
946
+ }
947
+ if (index >= items.length) {
948
+ return -1;
949
+ }
950
+ return index;
951
+ };
952
+ const getParentStartIndex = (dirents, index) => {
953
+ const dirent = dirents[index];
954
+ let startIndex = index - 1;
955
+ while (startIndex >= 0 && dirents[startIndex].depth >= dirent.depth) {
956
+ startIndex--;
957
+ }
958
+ return startIndex;
959
+ };
960
+ const getParentEndIndex = (dirents, index) => {
961
+ const dirent = dirents[index];
962
+ let endIndex = index + 1;
963
+ while (endIndex < dirents.length && dirents[endIndex].depth > dirent.depth) {
964
+ endIndex++;
965
+ }
966
+ return endIndex;
967
+ };
968
+ const isSymbolicLink = dirent => {
969
+ return dirent.type === Symlink;
970
+ };
971
+ const hasSymbolicLinks = rawDirents => {
972
+ return rawDirents.some(isSymbolicLink);
973
+ };
974
+ const getSymlinkType = type => {
975
+ switch (type) {
976
+ case File:
977
+ return SymLinkFile;
978
+ case Directory:
979
+ return SymLinkFolder;
980
+ default:
981
+ return Symlink;
982
+ }
983
+ };
984
+ // TODO maybe resolving of symbolic links should happen in shared process?
985
+ // so that there is less code and less work in the frontend
986
+ const resolveSymbolicLink = async (uri, rawDirent) => {
987
+ try {
988
+ // TODO support windows paths
989
+ const absolutePath = uri + '/' + rawDirent.name;
990
+ const type = await stat(absolutePath);
991
+ const symLinkType = getSymlinkType(type);
992
+ return {
993
+ name: rawDirent.name,
994
+ type: symLinkType
995
+ };
996
+ } catch (error) {
997
+ // @ts-ignore
998
+ if (error && error.code === ENOENT) {
999
+ return {
1000
+ name: rawDirent.name,
1001
+ type: SymLinkFile
1002
+ };
1003
+ }
1004
+ console.error(`Failed to resolve symbolic link for ${rawDirent.name}: ${error}`);
1005
+ return rawDirent;
1006
+ }
1007
+ };
1008
+ const resolveSymbolicLinks = async (uri, rawDirents) => {
1009
+ const promises = [];
1010
+ for (const rawDirent of rawDirents) {
1011
+ if (isSymbolicLink(rawDirent)) {
1012
+ const resolvedDirent = resolveSymbolicLink(uri, rawDirent);
1013
+ promises.push(resolvedDirent);
1014
+ } else {
1015
+ promises.push(rawDirent);
1016
+ }
1017
+ }
1018
+ const resolvedDirents = await Promise.all(promises);
1019
+ return resolvedDirents;
1020
+ };
1021
+ const getChildDirentsRaw = async uri => {
1022
+ const rawDirents = await readDirWithFileTypes();
1023
+ array(rawDirents);
1024
+ if (hasSymbolicLinks(rawDirents)) {
1025
+ return resolveSymbolicLinks(uri, rawDirents);
1026
+ }
1027
+ return rawDirents;
1028
+ };
1029
+ const getChildDirents = async (pathSeparator, parentDirent, excluded = []) => {
1030
+ string(pathSeparator);
1031
+ object(parentDirent);
1032
+ // TODO use event/actor based code instead, this is impossible to cancel right now
1033
+ // also cancel updating when opening new folder
1034
+ // const dispose = state => state.pendingRequests.forEach(cancelRequest)
1035
+ // TODO should use FileSystem directly in this case because it is globally available anyway
1036
+ // and more typesafe than Command.execute
1037
+ // and more performant
1038
+ const uri = parentDirent.path;
1039
+ const rawDirents = await getChildDirentsRaw(uri);
1040
+ const displayDirents = toDisplayDirents(pathSeparator, rawDirents, parentDirent, excluded);
1041
+ return displayDirents;
1042
+ };
1043
+
1044
+ // TODO optimize this function to return the minimum number
1045
+ // of visible items needed, e.g. when not scrolled 5 items with
1046
+ // 20px fill 100px but when scrolled 6 items are needed
1047
+ const getNumberOfVisibleItems = (listHeight, itemHeight) => {
1048
+ return Math.ceil(listHeight / itemHeight) + 1;
1049
+ };
1050
+
1051
+ const getExplorerMaxLineY = (minLineY, height, itemHeight, direntsLength) => {
1052
+ const maxLineY = minLineY + Math.min(getNumberOfVisibleItems(height, itemHeight), direntsLength);
1053
+ return maxLineY;
1054
+ };
1055
+
1056
+ const expandAll = async state => {
1057
+ const {
1058
+ items,
1059
+ focusedIndex,
1060
+ pathSeparator,
1061
+ minLineY,
1062
+ height,
1063
+ itemHeight
1064
+ } = state;
1065
+ if (focusedIndex === -1) {
1066
+ return state;
1067
+ }
1068
+ const dirent = items[focusedIndex];
1069
+ const depth = dirent.depth;
1070
+ const newDirents = [...items];
1071
+ // TODO fetch child dirents in parallel
1072
+ for (const dirent of newDirents) {
1073
+ if (dirent.depth === depth && dirent.type === Directory) {
1074
+ // TODO expand
1075
+ // TODO avoid mutating state here
1076
+ dirent.type = DirectoryExpanding;
1077
+ // TODO handle error
1078
+ // TODO race condition
1079
+ const childDirents = await getChildDirents(pathSeparator, dirent);
1080
+ const newIndex = newDirents.indexOf(dirent);
1081
+ if (newIndex === -1) {
1082
+ continue;
1083
+ }
1084
+ newDirents.splice(newIndex + 1, 0, ...childDirents);
1085
+ // TODO avoid mutating state here
1086
+ dirent.type = DirectoryExpanded;
1087
+ // await expand(state, dirent.index)
1088
+ }
1089
+ }
1090
+ const maxLineY = getExplorerMaxLineY(minLineY, height, itemHeight, newDirents.length);
1091
+ return {
1092
+ ...state,
1093
+ items: newDirents,
1094
+ maxLineY
1095
+ };
1096
+ };
1097
+
1098
+ const None$1 = 'none';
1099
+ const Tree = 'tree';
1100
+ const TreeItem$1 = 'treeitem';
1101
+
1102
+ const Button$1 = 'Button';
1103
+ const ButtonPrimary = 'ButtonPrimary';
1104
+ const Explorer = 'Explorer';
1105
+ const FileIcon = 'FileIcon';
1106
+ const InputBox = 'InputBox';
1107
+ const Label = 'Label';
1108
+ const TreeItem = 'TreeItem';
1109
+ const TreeItemActive = 'TreeItemActive';
1110
+ const Viewlet = 'Viewlet';
1111
+ const Welcome = 'Welcome';
1112
+ const WelcomeMessage = 'WelcomeMessage';
1113
+
1114
+ const HandleBlur = 'handleBlur';
1115
+ const HandleClick = 'handleClick';
1116
+ const handleClickOpenFolder$1 = 'handleClickOpenFolder';
1117
+ const HandleContextMenu = 'handleContextMenu';
1118
+ const HandleFocus = 'handleFocus';
1119
+ const HandlePointerDown = 'handlePointerDown';
1120
+ const HandleWheel = 'handleWheel';
1121
+
1122
+ const emptyObject = {};
1123
+ const RE_PLACEHOLDER = /\{(PH\d+)\}/g;
1124
+ const i18nString = (key, placeholders = emptyObject) => {
1125
+ if (placeholders === emptyObject) {
1126
+ return key;
1127
+ }
1128
+ const replacer = (match, rest) => {
1129
+ return placeholders[rest];
1130
+ };
1131
+ return key.replaceAll(RE_PLACEHOLDER, replacer);
1132
+ };
1133
+
1134
+ /**
1135
+ * @enum {string}
1136
+ */
1137
+ const UiStrings = {
1138
+ NewFile: 'New File...',
1139
+ NewFolder: 'New Folder...',
1140
+ OpenContainingFolder: 'Open Containing Folder',
1141
+ OpenInIntegratedTerminal: 'Open in integrated Terminal',
1142
+ Cut: 'Cut',
1143
+ Copy: 'Copy',
1144
+ Paste: 'Paste',
1145
+ CopyPath: 'Copy Path',
1146
+ CopyRelativePath: 'Copy Relative Path',
1147
+ Rename: 'Rename',
1148
+ Delete: 'Delete',
1149
+ RefreshExplorer: 'Refresh Explorer',
1150
+ CollapseAllFoldersInExplorer: 'Collapse All Folders in Explorer',
1151
+ Explorer: 'Explorer',
1152
+ FilesExplorer: 'Files Explorer',
1153
+ YouHaveNotYetOpenedAFolder: 'You have not yet opened a folder',
1154
+ OpenFolder: 'Open folder',
1155
+ NoFolderOpen: 'No Folder Open'
1156
+ };
1157
+ const filesExplorer = () => {
1158
+ return i18nString(UiStrings.FilesExplorer);
1159
+ };
1160
+ const youHaveNotYetOpenedAFolder = () => {
1161
+ return i18nString(UiStrings.YouHaveNotYetOpenedAFolder);
1162
+ };
1163
+ const openFolder$1 = () => {
1164
+ return i18nString(UiStrings.OpenFolder);
1165
+ };
1166
+
1167
+ const Button = 1;
1168
+ const Div = 4;
1169
+ const Input = 6;
1170
+ const Text = 12;
1171
+ const Img = 17;
1172
+ const P = 50;
1173
+
1174
+ const getFileIconVirtualDom = icon => {
1175
+ return {
1176
+ type: Img,
1177
+ className: FileIcon,
1178
+ src: icon,
1179
+ role: None$1,
1180
+ childCount: 0
1181
+ };
1182
+ };
1183
+
1184
+ const defaultIndent = 1;
1185
+ const getTreeItemIndent = depth => {
1186
+ return `${depth * defaultIndent}rem`;
1187
+ };
1188
+
1189
+ const ExplorerInput = 'ExploreerInput';
1190
+
1191
+ const text = data => {
1192
+ return {
1193
+ type: Text,
1194
+ text: data,
1195
+ childCount: 0
1196
+ };
1197
+ };
1198
+
1199
+ const getItemVirtualDomFile = item => {
1200
+ const {
1201
+ posInSet,
1202
+ setSize,
1203
+ icon,
1204
+ name,
1205
+ path,
1206
+ depth,
1207
+ isFocused,
1208
+ isEditing
1209
+ } = item;
1210
+ const dom = [];
1211
+ dom.push({
1212
+ type: Div,
1213
+ role: TreeItem$1,
1214
+ className: TreeItem,
1215
+ draggable: true,
1216
+ title: path,
1217
+ ariaPosInSet: posInSet,
1218
+ ariaSetSize: setSize,
1219
+ ariaLevel: depth,
1220
+ childCount: 2,
1221
+ paddingLeft: getTreeItemIndent(depth),
1222
+ ariaLabel: name,
1223
+ ariaDescription: ''
1224
+ }, getFileIconVirtualDom(icon));
1225
+ if (isEditing) {
1226
+ dom.push({
1227
+ type: Input,
1228
+ className: InputBox,
1229
+ id: 'ExplorerInput',
1230
+ onInput: 'handleEditingInput',
1231
+ childCount: 0,
1232
+ name: ExplorerInput
1233
+ });
1234
+ } else {
1235
+ dom.push({
1236
+ type: Div,
1237
+ className: Label,
1238
+ childCount: 1
1239
+ }, text(name));
1240
+ }
1241
+ if (isFocused) {
1242
+ // @ts-ignore
1243
+ dom[0].id = 'TreeItemActive';
1244
+ // @ts-ignore
1245
+ dom[0].className += ' ' + TreeItemActive;
1246
+ }
1247
+ return dom;
1248
+ };
1249
+ const getItemVirtualDomFolder = item => {
1250
+ const {
1251
+ posInSet,
1252
+ setSize,
1253
+ icon,
1254
+ name,
1255
+ path,
1256
+ depth,
1257
+ type,
1258
+ isFocused
1259
+ } = item;
1260
+ let ariaExpanded = '';
1261
+ switch (type) {
1262
+ // TODO decide on directory vs folder
1263
+ case Directory:
1264
+ ariaExpanded = 'false';
1265
+ break;
1266
+ case DirectoryExpanding:
1267
+ ariaExpanded = 'true'; // TODO tree should be aria-busy then
1268
+ break;
1269
+ case DirectoryExpanded:
1270
+ ariaExpanded = 'true';
1271
+ break;
1272
+ }
1273
+ const dom = [];
1274
+ dom.push({
1275
+ type: Div,
1276
+ role: TreeItem$1,
1277
+ className: TreeItem,
1278
+ draggable: true,
1279
+ title: path,
1280
+ ariaPosInSet: posInSet,
1281
+ ariaSetSize: setSize,
1282
+ ariaLevel: depth,
1283
+ childCount: 2,
1284
+ paddingLeft: getTreeItemIndent(depth),
1285
+ ariaLabel: name,
1286
+ ariaExpanded,
1287
+ ariaDescription: ''
1288
+ });
1289
+ dom.push(getFileIconVirtualDom(icon), {
1290
+ type: Div,
1291
+ className: Label,
1292
+ childCount: 1
1293
+ }, text(name));
1294
+ if (isFocused) {
1295
+ // @ts-ignore
1296
+ dom[0].id = 'TreeItemActive';
1297
+ }
1298
+ return dom;
1299
+ };
1300
+ const getExplorerItemVirtualDom = item => {
1301
+ const {
1302
+ type
1303
+ } = item;
1304
+ switch (type) {
1305
+ case Directory:
1306
+ case DirectoryExpanding:
1307
+ case DirectoryExpanded:
1308
+ return getItemVirtualDomFolder(item);
1309
+ default:
1310
+ return getItemVirtualDomFile(item);
1311
+ }
1312
+ };
1313
+
1314
+ const mergeClassNames = (...classNames) => {
1315
+ return classNames.filter(Boolean).join(' ');
1316
+ };
1317
+
1318
+ const getExplorerWelcomeVirtualDom = isWide => {
1319
+ return [{
1320
+ type: Div,
1321
+ className: mergeClassNames(Viewlet, Explorer),
1322
+ tabIndex: 0,
1323
+ childCount: 1
1324
+ }, {
1325
+ type: Div,
1326
+ className: Welcome,
1327
+ childCount: 2
1328
+ }, {
1329
+ type: P,
1330
+ className: WelcomeMessage,
1331
+ childCount: 1
1332
+ }, text(youHaveNotYetOpenedAFolder()), {
1333
+ type: Button,
1334
+ className: mergeClassNames(Button$1, ButtonPrimary, isWide ? 'ButtonWide' : 'ButtonNarrow'),
1335
+ childCount: 1,
1336
+ onClick: handleClickOpenFolder$1
1337
+ }, text(openFolder$1())];
1338
+ };
1339
+
1340
+ const getExplorerVirtualDom = (visibleItems, focusedIndex, root, isWide) => {
1341
+ if (!root) {
1342
+ return getExplorerWelcomeVirtualDom(isWide);
1343
+ }
1344
+ const dom = [];
1345
+ dom.push({
1346
+ type: Div,
1347
+ className: mergeClassNames(Viewlet, Explorer),
1348
+ tabIndex: 0,
1349
+ role: Tree,
1350
+ ariaLabel: filesExplorer(),
1351
+ childCount: visibleItems.length,
1352
+ ariaActiveDescendant: focusedIndex >= 0 ? 'TreeItemActive' : undefined,
1353
+ onFocus: HandleFocus,
1354
+ onBlur: HandleBlur,
1355
+ onContextMenu: HandleContextMenu,
1356
+ onPointerDown: HandlePointerDown,
1357
+ onWheel: HandleWheel,
1358
+ onClick: HandleClick
1359
+ });
1360
+ dom.push(...visibleItems.flatMap(getExplorerItemVirtualDom));
1361
+ return dom;
1362
+ };
1363
+
1364
+ const Enter = 3;
1365
+ const Escape = 8;
1366
+ const Space = 9;
1367
+ const End = 255;
1368
+ const Home = 12;
1369
+ const LeftArrow = 13;
1370
+ const UpArrow = 14;
1371
+ const RightArrow = 15;
1372
+ const DownArrow = 16;
1373
+ const Delete = 18;
1374
+ const KeyC = 31;
1375
+ const KeyV = 50;
1376
+ const F2 = 58;
1377
+ const Star = 131;
1378
+
1379
+ const CtrlCmd = 1 << 11 >>> 0;
1380
+ const Alt = 1 << 9 >>> 0;
1381
+
1382
+ const FocusExplorer = 13;
1383
+ const FocusExplorerEditBox = 14;
1384
+
1385
+ const getKeyBindings = () => {
1386
+ return [{
1387
+ key: RightArrow,
1388
+ command: 'Explorer.handleArrowRight',
1389
+ when: FocusExplorer
1390
+ }, {
1391
+ key: LeftArrow,
1392
+ command: 'Explorer.handleArrowLeft',
1393
+ when: FocusExplorer
1394
+ }, {
1395
+ key: Home,
1396
+ command: 'Explorer.focusFirst',
1397
+ when: FocusExplorer
1398
+ }, {
1399
+ key: End,
1400
+ command: 'Explorer.focusLast',
1401
+ when: FocusExplorer
1402
+ }, {
1403
+ key: UpArrow,
1404
+ command: 'Explorer.focusPrevious',
1405
+ when: FocusExplorer
1406
+ }, {
1407
+ key: DownArrow,
1408
+ command: 'Explorer.focusNext',
1409
+ when: FocusExplorer
1410
+ }, {
1411
+ key: CtrlCmd | Star,
1412
+ command: 'Explorer.expandAll',
1413
+ when: FocusExplorer
1414
+ }, {
1415
+ key: Alt | RightArrow,
1416
+ command: 'Explorer.expandRecursively',
1417
+ when: FocusExplorer
1418
+ }, {
1419
+ key: CtrlCmd | LeftArrow,
1420
+ command: 'Explorer.collapseAll',
1421
+ when: FocusExplorer
1422
+ }, {
1423
+ key: CtrlCmd | KeyV,
1424
+ command: 'Explorer.handlePaste',
1425
+ when: FocusExplorer
1426
+ }, {
1427
+ key: CtrlCmd | KeyC,
1428
+ command: 'Explorer.handleCopy',
1429
+ when: FocusExplorer
1430
+ }, {
1431
+ key: F2,
1432
+ command: 'Explorer.rename',
1433
+ when: FocusExplorer
1434
+ }, {
1435
+ key: Escape,
1436
+ command: 'Explorer.cancelEdit',
1437
+ when: FocusExplorerEditBox
1438
+ }, {
1439
+ key: Enter,
1440
+ command: 'Explorer.acceptEdit',
1441
+ when: FocusExplorerEditBox
1442
+ }, {
1443
+ key: Delete,
1444
+ command: 'Explorer.removeDirent',
1445
+ when: FocusExplorer
1446
+ }, {
1447
+ key: Escape,
1448
+ command: 'Explorer.focusNone',
1449
+ when: FocusExplorer
1450
+ }, {
1451
+ key: Space,
1452
+ command: 'Explorer.handleClickCurrentButKeepFocus',
1453
+ when: FocusExplorer
1454
+ }, {
1455
+ key: Enter,
1456
+ command: 'Explorer.handleClickCurrent',
1457
+ when: FocusExplorer
1458
+ }];
1459
+ };
1460
+
1461
+ const None = 0;
1462
+ const Rename = 3;
1463
+
1464
+ const getVisibleExplorerItems = (items, minLineY, maxLineY, focusedIndex, editingIndex, editingType, editingValue) => {
1465
+ const visible = [];
1466
+ for (let i = minLineY; i < Math.min(maxLineY, items.length); i++) {
1467
+ const item = items[i];
1468
+ if (i === editingIndex) {
1469
+ visible.push({
1470
+ ...item,
1471
+ isFocused: i === focusedIndex,
1472
+ isEditing: true,
1473
+ icon: getFileIcon({
1474
+ name: editingValue
1475
+ })
1476
+ });
1477
+ } else {
1478
+ visible.push({
1479
+ ...item,
1480
+ isFocused: i === focusedIndex
1481
+ });
1482
+ }
1483
+ }
1484
+ if (editingType !== None && editingIndex === -1) {
1485
+ visible.push({
1486
+ depth: 3,
1487
+ posInSet: 1,
1488
+ setSize: 1,
1489
+ icon: '',
1490
+ isFocused: false,
1491
+ name: 'new',
1492
+ path: '/test/new',
1493
+ type: 2,
1494
+ isEditing: true
1495
+ });
1496
+ }
1497
+ return visible;
1498
+ };
1499
+
1500
+ const focusIndex = (state, index) => {
1501
+ const {
1502
+ minLineY,
1503
+ maxLineY
1504
+ } = state;
1505
+ if (index < minLineY) {
1506
+ if (index < 0) {
1507
+ return {
1508
+ ...state,
1509
+ focusedIndex: index,
1510
+ focused: true
1511
+ };
1512
+ }
1513
+ const diff = maxLineY - minLineY;
1514
+ return {
1515
+ ...state,
1516
+ focusedIndex: index,
1517
+ focused: true,
1518
+ minLineY: index,
1519
+ maxLineY: index + diff
1520
+ };
1521
+ }
1522
+ if (index >= maxLineY) {
1523
+ const diff = maxLineY - minLineY;
1524
+ return {
1525
+ ...state,
1526
+ focusedIndex: index,
1527
+ focused: true,
1528
+ minLineY: index + 1 - diff,
1529
+ maxLineY: index + 1
1530
+ };
1531
+ }
1532
+ return {
1533
+ ...state,
1534
+ focusedIndex: index,
1535
+ focused: true
1536
+ };
1537
+ };
1538
+
1539
+ const LeftClick = 0;
1540
+
1541
+ const openFolder = async () => {
1542
+ // TODO
1543
+ };
1544
+
1545
+ // TODO viewlet should only have create and refresh functions
1546
+ // every thing else can be in a separate module <viewlet>.lazy.js
1547
+ // and <viewlet>.ipc.js
1548
+
1549
+ // viewlet: creating | refreshing | done | disposed
1550
+ // TODO recycle viewlets (maybe)
1551
+
1552
+ // TODO instead of root string, there should be a root dirent
1553
+
1554
+ const updateIcon = dirent => {
1555
+ return {
1556
+ ...dirent,
1557
+ icon: getIcon()
1558
+ };
1559
+ };
1560
+ const updateIcons = state => {
1561
+ const newDirents = state.items.map(updateIcon);
1562
+ return {
1563
+ ...state,
1564
+ items: newDirents
1565
+ };
1566
+ };
1567
+ const handleIconThemeChange = state => {
1568
+ return updateIcons(state);
1569
+ };
1570
+
1571
+ // TODO rename dirents to items, then can use virtual list component directly
1572
+ const setDeltaY = (state, deltaY) => {
1573
+ const {
1574
+ itemHeight,
1575
+ height,
1576
+ items
1577
+ } = state;
1578
+ if (deltaY < 0) {
1579
+ deltaY = 0;
1580
+ } else if (deltaY > items.length * itemHeight - height) {
1581
+ deltaY = Math.max(items.length * itemHeight - height, 0);
1582
+ }
1583
+ if (state.deltaY === deltaY) {
1584
+ return state;
1585
+ }
1586
+ const minLineY = Math.round(deltaY / itemHeight);
1587
+ const maxLineY = minLineY + Math.round(height / itemHeight);
1588
+ return {
1589
+ ...state,
1590
+ deltaY,
1591
+ minLineY,
1592
+ maxLineY
1593
+ };
1594
+ };
1595
+ const handleWheel = (state, deltaMode, deltaY) => {
1596
+ return setDeltaY(state, state.deltaY + deltaY);
1597
+ };
1598
+ const getFocusedDirent = state => {
1599
+ const {
1600
+ focusedIndex,
1601
+ minLineY,
1602
+ items
1603
+ } = state;
1604
+ const dirent = items[focusedIndex + minLineY];
1605
+ return dirent;
1606
+ };
1607
+
1608
+ // TODO support multiselection and removing multiple dirents
1609
+ const removeDirent = async state => {
1610
+ if (state.focusedIndex < 0) {
1611
+ return state;
1612
+ }
1613
+ const dirent = getFocusedDirent(state);
1614
+ const absolutePath = dirent.path;
1615
+ try {
1616
+ // TODO handle error
1617
+ await remove(absolutePath);
1618
+ } catch (error) {
1619
+ // TODO vscode shows error as alert (no stacktrace) and retry button
1620
+ // maybe should show alert as well, but where to put stacktrace?
1621
+ // on web should probably show notification (dialog)
1622
+ // ErrorHandling.handleError(error)
1623
+ // await ErrorHandling.showErrorDialog(error)
1624
+ return;
1625
+ }
1626
+ // TODO avoid state mutation
1627
+ const newVersion = ++state.version;
1628
+ // TODO race condition
1629
+ // const newState = await loadContent(state:any)
1630
+ if (state.version !== newVersion || state.disposed) {
1631
+ return state;
1632
+ }
1633
+ // TODO is it possible to make this more functional instead of mutating state?
1634
+ // maybe every function returns a new state?
1635
+ const index = state.items.indexOf(dirent);
1636
+ let deleteEnd = index + 1;
1637
+ for (; deleteEnd < state.items.length; deleteEnd++) {
1638
+ if (state.items[deleteEnd].depth <= dirent.depth) {
1639
+ break;
1640
+ }
1641
+ }
1642
+ const deleteCount = deleteEnd - index;
1643
+ const newDirents = [...state.items];
1644
+ newDirents.splice(index, deleteCount);
1645
+ let indexToFocus = -1;
1646
+ if (newDirents.length === 0) {
1647
+ indexToFocus = -1;
1648
+ } else if (index < state.focusedIndex) {
1649
+ indexToFocus = state.focusedIndex - 1;
1650
+ } else if (index === state.focusedIndex) {
1651
+ indexToFocus = Math.max(state.focusedIndex - 1, 0);
1652
+ } else {
1653
+ indexToFocus = Math.max(state.focusedIndex - 1, 0);
1654
+ }
1655
+ return {
1656
+ ...state,
1657
+ items: newDirents,
1658
+ focusedIndex: indexToFocus
1659
+ };
1660
+ };
1661
+ const renameDirent = state => {
1662
+ const {
1663
+ focusedIndex,
1664
+ items
1665
+ } = state;
1666
+ const item = items[focusedIndex];
1667
+ // Focus.setFocus(FocusKey.ExplorerEditBox)
1668
+ return {
1669
+ ...state,
1670
+ editingIndex: focusedIndex,
1671
+ editingType: Rename,
1672
+ editingValue: item.name
1673
+ };
1674
+ };
1675
+
1676
+ // TODO use posInSet and setSize properties to compute more effectively
1677
+
1678
+ const cancelEdit = state => {
1679
+ const {
1680
+ editingIndex
1681
+ } = state;
1682
+ return {
1683
+ ...state,
1684
+ focusedIndex: editingIndex,
1685
+ focused: true,
1686
+ editingIndex: -1,
1687
+ editingValue: '',
1688
+ editingType: None
1689
+ };
1690
+ };
1691
+ const copyRelativePath = async state => {
1692
+ const dirent = getFocusedDirent(state);
1693
+ // @ts-ignore
1694
+ dirent.path.slice(1);
1695
+ // TODO handle error
1696
+
1697
+ // await Command.execute(RendererWorkerCommandType.ClipBoardWriteText, /* text */ relativePath)
1698
+ return state;
1699
+ };
1700
+ const copyPath = async state => {
1701
+ // await Command.execute(RendererWorkerCommandType.ClipBoardWriteText, /* text */ path)
1702
+ return state;
1703
+ };
1704
+
1705
+ // TODO much shared logic with newFolder
1706
+
1707
+ const handleClickFile = async (state, dirent, index, keepFocus = false) => {
1708
+ // await Command.execute(/* Main.openAbsolutePath */ 'Main.openUri', /* absolutePath */ dirent.path, /* focus */ !keepFocus)
1709
+ return {
1710
+ ...state,
1711
+ focusedIndex: index,
1712
+ focused: keepFocus
1713
+ };
1714
+ };
1715
+ const handleClickDirectory = async (state, dirent, index, keepFocus) => {
1716
+ dirent.type = DirectoryExpanding;
1717
+ // TODO handle error
1718
+ const dirents = await getChildDirents(state.pathSeparator, dirent);
1719
+ const state2 = {};
1720
+ if (!state2) {
1721
+ return state;
1722
+ }
1723
+ // TODO use Viewlet.getState here and check if it exists
1724
+ const newIndex = state2.items.indexOf(dirent);
1725
+ // TODO if viewlet is disposed or root has changed, return
1726
+ if (newIndex === -1) {
1727
+ return state;
1728
+ }
1729
+ const newDirents = [...state2.items];
1730
+ newDirents.splice(newIndex + 1, 0, ...dirents);
1731
+ dirent.type = DirectoryExpanded;
1732
+ dirent.icon = getIcon();
1733
+ const {
1734
+ height,
1735
+ itemHeight,
1736
+ minLineY
1737
+ } = state2;
1738
+ // TODO when focused index has changed while expanding, don't update it
1739
+ const maxLineY = getExplorerMaxLineY(minLineY, height, itemHeight, newDirents.length);
1740
+ return {
1741
+ ...state,
1742
+ items: newDirents,
1743
+ focusedIndex: newIndex,
1744
+ focused: keepFocus,
1745
+ maxLineY
1746
+ };
1747
+ };
1748
+ const handleClickDirectoryExpanding = (state, dirent, index, keepFocus) => {
1749
+ dirent.type = Directory;
1750
+ dirent.icon = getIcon();
1751
+ return {
1752
+ ...state,
1753
+ focusedIndex: index,
1754
+ focused: keepFocus
1755
+ };
1756
+ };
1757
+ const handleClickDirectoryExpanded = (state, dirent, index, keepFocus) => {
1758
+ const {
1759
+ minLineY,
1760
+ maxLineY,
1761
+ itemHeight
1762
+ } = state;
1763
+ dirent.type = Directory;
1764
+ dirent.icon = getIcon();
1765
+ const endIndex = getParentEndIndex(state.items, index);
1766
+ const removeCount = endIndex - index - 1;
1767
+ // TODO race conditions and side effects are everywhere
1768
+ const newDirents = [...state.items];
1769
+ newDirents.splice(index + 1, removeCount);
1770
+ const newTotal = newDirents.length;
1771
+ if (newTotal < maxLineY) {
1772
+ const visibleItems = Math.min(maxLineY - minLineY, newTotal);
1773
+ const newMaxLineY = Math.min(maxLineY, newTotal);
1774
+ const newMinLineY = newMaxLineY - visibleItems;
1775
+ const deltaY = newMinLineY * itemHeight;
1776
+ return {
1777
+ ...state,
1778
+ items: newDirents,
1779
+ focusedIndex: index,
1780
+ focused: keepFocus,
1781
+ minLineY: newMinLineY,
1782
+ maxLineY: newMaxLineY,
1783
+ deltaY
1784
+ };
1785
+ }
1786
+ return {
1787
+ ...state,
1788
+ items: newDirents,
1789
+ focusedIndex: index,
1790
+ focused: keepFocus
1791
+ };
1792
+ };
1793
+ const getClickFn = direntType => {
1794
+ switch (direntType) {
1795
+ case File:
1796
+ case SymLinkFile:
1797
+ return handleClickFile;
1798
+ case Directory:
1799
+ case SymLinkFolder:
1800
+ return handleClickDirectory;
1801
+ case DirectoryExpanding:
1802
+ return handleClickDirectoryExpanding;
1803
+ case DirectoryExpanded:
1804
+ return handleClickDirectoryExpanded;
1805
+ case Symlink:
1806
+ return handleClickSymLink;
1807
+ case CharacterDevice:
1808
+ throw new Error('Cannot open character device files');
1809
+ case BlockDevice:
1810
+ throw new Error('Cannot open block device files');
1811
+ case Socket:
1812
+ throw new Error('Cannot open socket files');
1813
+ default:
1814
+ throw new Error(`unsupported dirent type ${direntType}`);
1815
+ }
1816
+ };
1817
+ const handleClick = (state, index, keepFocus = false) => {
1818
+ const {
1819
+ items,
1820
+ minLineY
1821
+ } = state;
1822
+ if (index === -1) {
1823
+ return focusIndex(state, -1);
1824
+ }
1825
+ const actualIndex = index + minLineY;
1826
+ const dirent = items[actualIndex];
1827
+ if (!dirent) {
1828
+ console.warn(`[explorer] dirent at index ${actualIndex} not found`, state);
1829
+ return state;
1830
+ }
1831
+ const clickFn = getClickFn(dirent.type);
1832
+ return clickFn(state, dirent, actualIndex, keepFocus);
1833
+ };
1834
+ const handleClickAt = (state, button, x, y) => {
1835
+ if (button !== LeftClick) {
1836
+ return state;
1837
+ }
1838
+ const index = getIndexFromPosition(state, x, y);
1839
+ return handleClick(state, index);
1840
+ };
1841
+ const handleClickCurrentButKeepFocus = state => {
1842
+ return handleClick(state, state.focusedIndex - state.minLineY, /* keepFocus */true);
1843
+ };
1844
+
1845
+ // export const handleBlur=()=>{}
1846
+
1847
+ const handleClickSymLink = async (state, dirent, index) => {
1848
+ await getRealPath();
1849
+ const type = await stat();
1850
+ switch (type) {
1851
+ case File:
1852
+ return handleClickFile(state, dirent, index);
1853
+ default:
1854
+ throw new Error(`unsupported file type ${type}`);
1855
+ }
1856
+ };
1857
+ const handleArrowRightDirectoryExpanded = (state, dirent) => {
1858
+ const {
1859
+ items,
1860
+ focusedIndex
1861
+ } = state;
1862
+ if (focusedIndex === items.length - 1) {
1863
+ return state;
1864
+ }
1865
+ const nextDirent = items[focusedIndex + 1];
1866
+ if (nextDirent.depth === dirent.depth + 1) {
1867
+ return focusIndex(state, focusedIndex + 1);
1868
+ }
1869
+ };
1870
+ const handleArrowRight = async state => {
1871
+ const {
1872
+ items,
1873
+ focusedIndex
1874
+ } = state;
1875
+ if (focusedIndex === -1) {
1876
+ return state;
1877
+ }
1878
+ const dirent = items[focusedIndex];
1879
+ switch (dirent.type) {
1880
+ case File:
1881
+ case SymLinkFile:
1882
+ return state;
1883
+ case Directory:
1884
+ case SymLinkFolder:
1885
+ // @ts-ignore
1886
+ return handleClickDirectory(state, dirent);
1887
+ case DirectoryExpanded:
1888
+ return handleArrowRightDirectoryExpanded(state, dirent);
1889
+ case Symlink:
1890
+ return handleClickSymLink(state, dirent, focusedIndex);
1891
+ default:
1892
+ throw new Error(`unsupported file type ${dirent.type}`);
1893
+ }
1894
+ };
1895
+ const focusParentFolder = state => {
1896
+ const parentStartIndex = getParentStartIndex(state.items, state.focusedIndex);
1897
+ if (parentStartIndex === -1) {
1898
+ return state;
1899
+ }
1900
+ return focusIndex(state, parentStartIndex);
1901
+ };
1902
+ const handleArrowLeft = state => {
1903
+ const {
1904
+ items,
1905
+ focusedIndex
1906
+ } = state;
1907
+ if (focusedIndex === -1) {
1908
+ return state;
1909
+ }
1910
+ const dirent = items[focusedIndex];
1911
+ switch (dirent.type) {
1912
+ case Directory:
1913
+ case File:
1914
+ case SymLinkFile:
1915
+ return focusParentFolder(state);
1916
+ case DirectoryExpanded:
1917
+ // @ts-ignore
1918
+ return handleClickDirectoryExpanded(state, dirent, focusedIndex);
1919
+ default:
1920
+ // TODO handle expanding directory and cancel file system call to read child dirents
1921
+ return state;
1922
+ }
1923
+ };
1924
+
1925
+ // TODO what happens when mouse leave and anther mouse enter event occur?
1926
+ // should update preview instead of closing and reopening
1927
+
1928
+ const handleBlur = state => {
1929
+ // TODO when blur event occurs because of context menu, focused index should stay the same
1930
+ // but focus outline should be removed
1931
+ const {
1932
+ editingType
1933
+ } = state;
1934
+ if (editingType !== None) {
1935
+ return state;
1936
+ }
1937
+ return {
1938
+ ...state,
1939
+ focused: false
1940
+ };
1941
+ };
1942
+
1943
+ // TODO maybe just insert items into explorer and refresh whole explorer
1944
+
1945
+ const handleClickOpenFolder = async state => {
1946
+ await openFolder();
1947
+ return state;
1948
+ };
1949
+
1950
+ // TODO viewlet should only have create and refresh functions
1951
+ // every thing else can be in a separate module <viewlet>.lazy.js
1952
+ // and <viewlet>.ipc.js
1953
+
1954
+ // viewlet: creating | refreshing | done | disposed
1955
+ // TODO recycle viewlets (maybe)
1956
+
1957
+ // TODO instead of root string, there should be a root dirent
1958
+
1959
+ const getPathSeparator = root => {
1960
+ return getPathSeparator$1();
1961
+ };
1962
+ const restoreExpandedState = async (savedState, root, pathSeparator, excluded) => {
1963
+ {
1964
+ return [];
1965
+ }
1966
+ };
1967
+ const getExcluded = () => {
1968
+ const excludedObject = {};
1969
+ const excluded = [];
1970
+ for (const [key, value] of Object.entries(excludedObject)) {
1971
+ if (value) {
1972
+ excluded.push(key);
1973
+ }
1974
+ }
1975
+ return excluded;
1976
+ };
1977
+ const getSavedRoot = (savedState, workspacePath) => {
1978
+ return workspacePath;
1979
+ };
1980
+ const loadContent = async (state, savedState) => {
1981
+ const root = getSavedRoot(savedState, '');
1982
+ // TODO path separator could be restored from saved state
1983
+ const pathSeparator = await getPathSeparator(); // TODO only load path separator once
1984
+ const excluded = getExcluded();
1985
+ const restoredDirents = await restoreExpandedState();
1986
+ const {
1987
+ itemHeight,
1988
+ height
1989
+ } = state;
1990
+ let minLineY = 0;
1991
+ if (savedState && typeof savedState.minLineY === 'number') {
1992
+ minLineY = savedState.minLineY;
1993
+ }
1994
+ let deltaY = 0;
1995
+ if (savedState && typeof savedState.deltaY === 'number') {
1996
+ deltaY = savedState.deltaY;
1997
+ }
1998
+ const maxLineY = getExplorerMaxLineY(minLineY, height, itemHeight, restoredDirents.length);
1999
+ return {
2000
+ ...state,
2001
+ root,
2002
+ items: restoredDirents,
2003
+ minLineY,
2004
+ deltaY,
2005
+ maxLineY,
2006
+ pathSeparator,
2007
+ excluded
2008
+ };
2009
+ };
2010
+
2011
+ const commandMap = {
2012
+ 'Explorer.cancelEdit': cancelEdit,
2013
+ 'Explorer.copyPath': copyPath,
2014
+ 'Explorer.copyRelativePath': copyRelativePath,
2015
+ 'Explorer.expandAll': expandAll,
2016
+ 'Explorer.getKeyBindings': getKeyBindings,
2017
+ 'Explorer.getVirtualDom': getExplorerVirtualDom,
2018
+ 'Explorer.getVisibleItems': getVisibleExplorerItems,
2019
+ 'Explorer.handleArrowLeft': handleArrowLeft,
2020
+ 'Explorer.handleArrowRight': handleArrowRight,
2021
+ 'Explorer.handleBlur': handleBlur,
2022
+ 'Explorer.handleClick': handleClick,
2023
+ 'Explorer.handleClickAt': handleClickAt,
2024
+ 'Explorer.handleClickCurrentButKeepFocus': handleClickCurrentButKeepFocus,
2025
+ 'Explorer.handleClickOpenFolder': handleClickOpenFolder,
2026
+ 'Explorer.handleIconThemeChange': handleIconThemeChange,
2027
+ 'Explorer.handleWheel': handleWheel,
2028
+ 'Explorer.loadContent': loadContent,
2029
+ 'Explorer.removeDirent': removeDirent,
2030
+ 'Explorer.renameDirent': renameDirent
2031
+ };
2032
+
2033
+ const listen = async () => {
2034
+ await WebWorkerRpcClient.create({
2035
+ commandMap: commandMap
2036
+ });
2037
+ };
2038
+
2039
+ const main = async () => {
2040
+ await listen();
2041
+ };
2042
+
2043
+ main();