@lvce-editor/main-area-worker 1.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.
@@ -0,0 +1,2263 @@
1
+ const normalizeLine = line => {
2
+ if (line.startsWith('Error: ')) {
3
+ return line.slice('Error: '.length);
4
+ }
5
+ if (line.startsWith('VError: ')) {
6
+ return line.slice('VError: '.length);
7
+ }
8
+ return line;
9
+ };
10
+ const getCombinedMessage = (error, message) => {
11
+ const stringifiedError = normalizeLine(`${error}`);
12
+ if (message) {
13
+ return `${message}: ${stringifiedError}`;
14
+ }
15
+ return stringifiedError;
16
+ };
17
+ const NewLine$2 = '\n';
18
+ const getNewLineIndex$1 = (string, startIndex = undefined) => {
19
+ return string.indexOf(NewLine$2, startIndex);
20
+ };
21
+ const mergeStacks = (parent, child) => {
22
+ if (!child) {
23
+ return parent;
24
+ }
25
+ const parentNewLineIndex = getNewLineIndex$1(parent);
26
+ const childNewLineIndex = getNewLineIndex$1(child);
27
+ if (childNewLineIndex === -1) {
28
+ return parent;
29
+ }
30
+ const parentFirstLine = parent.slice(0, parentNewLineIndex);
31
+ const childRest = child.slice(childNewLineIndex);
32
+ const childFirstLine = normalizeLine(child.slice(0, childNewLineIndex));
33
+ if (parentFirstLine.includes(childFirstLine)) {
34
+ return parentFirstLine + childRest;
35
+ }
36
+ return child;
37
+ };
38
+ class VError extends Error {
39
+ constructor(error, message) {
40
+ const combinedMessage = getCombinedMessage(error, message);
41
+ super(combinedMessage);
42
+ this.name = 'VError';
43
+ if (error instanceof Error) {
44
+ this.stack = mergeStacks(this.stack, error.stack);
45
+ }
46
+ if (error.codeFrame) {
47
+ // @ts-ignore
48
+ this.codeFrame = error.codeFrame;
49
+ }
50
+ if (error.code) {
51
+ // @ts-ignore
52
+ this.code = error.code;
53
+ }
54
+ }
55
+ }
56
+
57
+ class AssertionError extends Error {
58
+ constructor(message) {
59
+ super(message);
60
+ this.name = 'AssertionError';
61
+ }
62
+ }
63
+ const Object$1 = 1;
64
+ const Number$1 = 2;
65
+ const Array$1 = 3;
66
+ const String = 4;
67
+ const Boolean = 5;
68
+ const Function = 6;
69
+ const Null = 7;
70
+ const Unknown = 8;
71
+ const getType = value => {
72
+ switch (typeof value) {
73
+ case 'number':
74
+ return Number$1;
75
+ case 'function':
76
+ return Function;
77
+ case 'string':
78
+ return String;
79
+ case 'object':
80
+ if (value === null) {
81
+ return Null;
82
+ }
83
+ if (Array.isArray(value)) {
84
+ return Array$1;
85
+ }
86
+ return Object$1;
87
+ case 'boolean':
88
+ return Boolean;
89
+ default:
90
+ return Unknown;
91
+ }
92
+ };
93
+ const number = value => {
94
+ const type = getType(value);
95
+ if (type !== Number$1) {
96
+ throw new AssertionError('expected value to be of type number');
97
+ }
98
+ };
99
+
100
+ const isMessagePort = value => {
101
+ return value && value instanceof MessagePort;
102
+ };
103
+ const isMessagePortMain = value => {
104
+ return value && value.constructor && value.constructor.name === 'MessagePortMain';
105
+ };
106
+ const isOffscreenCanvas = value => {
107
+ return typeof OffscreenCanvas !== 'undefined' && value instanceof OffscreenCanvas;
108
+ };
109
+ const isInstanceOf = (value, constructorName) => {
110
+ return value?.constructor?.name === constructorName;
111
+ };
112
+ const isSocket = value => {
113
+ return isInstanceOf(value, 'Socket');
114
+ };
115
+ const transferrables = [isMessagePort, isMessagePortMain, isOffscreenCanvas, isSocket];
116
+ const isTransferrable = value => {
117
+ for (const fn of transferrables) {
118
+ if (fn(value)) {
119
+ return true;
120
+ }
121
+ }
122
+ return false;
123
+ };
124
+ const walkValue = (value, transferrables, isTransferrable) => {
125
+ if (!value) {
126
+ return;
127
+ }
128
+ if (isTransferrable(value)) {
129
+ transferrables.push(value);
130
+ return;
131
+ }
132
+ if (Array.isArray(value)) {
133
+ for (const item of value) {
134
+ walkValue(item, transferrables, isTransferrable);
135
+ }
136
+ return;
137
+ }
138
+ if (typeof value === 'object') {
139
+ for (const property of Object.values(value)) {
140
+ walkValue(property, transferrables, isTransferrable);
141
+ }
142
+ return;
143
+ }
144
+ };
145
+ const getTransferrables = value => {
146
+ const transferrables = [];
147
+ walkValue(value, transferrables, isTransferrable);
148
+ return transferrables;
149
+ };
150
+ const attachEvents = that => {
151
+ const handleMessage = (...args) => {
152
+ const data = that.getData(...args);
153
+ that.dispatchEvent(new MessageEvent('message', {
154
+ data
155
+ }));
156
+ };
157
+ that.onMessage(handleMessage);
158
+ const handleClose = event => {
159
+ that.dispatchEvent(new Event('close'));
160
+ };
161
+ that.onClose(handleClose);
162
+ };
163
+ class Ipc extends EventTarget {
164
+ constructor(rawIpc) {
165
+ super();
166
+ this._rawIpc = rawIpc;
167
+ attachEvents(this);
168
+ }
169
+ }
170
+ const E_INCOMPATIBLE_NATIVE_MODULE = 'E_INCOMPATIBLE_NATIVE_MODULE';
171
+ const E_MODULES_NOT_SUPPORTED_IN_ELECTRON = 'E_MODULES_NOT_SUPPORTED_IN_ELECTRON';
172
+ const ERR_MODULE_NOT_FOUND = 'ERR_MODULE_NOT_FOUND';
173
+ const NewLine$1 = '\n';
174
+ const joinLines$1 = lines => {
175
+ return lines.join(NewLine$1);
176
+ };
177
+ const RE_AT = /^\s+at/;
178
+ const RE_AT_PROMISE_INDEX = /^\s*at async Promise.all \(index \d+\)$/;
179
+ const isNormalStackLine = line => {
180
+ return RE_AT.test(line) && !RE_AT_PROMISE_INDEX.test(line);
181
+ };
182
+ const getDetails = lines => {
183
+ const index = lines.findIndex(isNormalStackLine);
184
+ if (index === -1) {
185
+ return {
186
+ actualMessage: joinLines$1(lines),
187
+ rest: []
188
+ };
189
+ }
190
+ let lastIndex = index - 1;
191
+ while (++lastIndex < lines.length) {
192
+ if (!isNormalStackLine(lines[lastIndex])) {
193
+ break;
194
+ }
195
+ }
196
+ return {
197
+ actualMessage: lines[index - 1],
198
+ rest: lines.slice(index, lastIndex)
199
+ };
200
+ };
201
+ const splitLines$1 = lines => {
202
+ return lines.split(NewLine$1);
203
+ };
204
+ const RE_MESSAGE_CODE_BLOCK_START = /^Error: The module '.*'$/;
205
+ const RE_MESSAGE_CODE_BLOCK_END = /^\s* at/;
206
+ const isMessageCodeBlockStartIndex = line => {
207
+ return RE_MESSAGE_CODE_BLOCK_START.test(line);
208
+ };
209
+ const isMessageCodeBlockEndIndex = line => {
210
+ return RE_MESSAGE_CODE_BLOCK_END.test(line);
211
+ };
212
+ const getMessageCodeBlock = stderr => {
213
+ const lines = splitLines$1(stderr);
214
+ const startIndex = lines.findIndex(isMessageCodeBlockStartIndex);
215
+ const endIndex = startIndex + lines.slice(startIndex).findIndex(isMessageCodeBlockEndIndex, startIndex);
216
+ const relevantLines = lines.slice(startIndex, endIndex);
217
+ const relevantMessage = relevantLines.join(' ').slice('Error: '.length);
218
+ return relevantMessage;
219
+ };
220
+ const isModuleNotFoundMessage = line => {
221
+ return line.includes('[ERR_MODULE_NOT_FOUND]');
222
+ };
223
+ const getModuleNotFoundError = stderr => {
224
+ const lines = splitLines$1(stderr);
225
+ const messageIndex = lines.findIndex(isModuleNotFoundMessage);
226
+ const message = lines[messageIndex];
227
+ return {
228
+ code: ERR_MODULE_NOT_FOUND,
229
+ message
230
+ };
231
+ };
232
+ const isModuleNotFoundError = stderr => {
233
+ if (!stderr) {
234
+ return false;
235
+ }
236
+ return stderr.includes('ERR_MODULE_NOT_FOUND');
237
+ };
238
+ const isModulesSyntaxError = stderr => {
239
+ if (!stderr) {
240
+ return false;
241
+ }
242
+ return stderr.includes('SyntaxError: Cannot use import statement outside a module');
243
+ };
244
+ const RE_NATIVE_MODULE_ERROR = /^innerError Error: Cannot find module '.*.node'/;
245
+ const RE_NATIVE_MODULE_ERROR_2 = /was compiled against a different Node.js version/;
246
+ const isUnhelpfulNativeModuleError = stderr => {
247
+ return RE_NATIVE_MODULE_ERROR.test(stderr) && RE_NATIVE_MODULE_ERROR_2.test(stderr);
248
+ };
249
+ const getNativeModuleErrorMessage = stderr => {
250
+ const message = getMessageCodeBlock(stderr);
251
+ return {
252
+ code: E_INCOMPATIBLE_NATIVE_MODULE,
253
+ message: `Incompatible native node module: ${message}`
254
+ };
255
+ };
256
+ const getModuleSyntaxError = () => {
257
+ return {
258
+ code: E_MODULES_NOT_SUPPORTED_IN_ELECTRON,
259
+ message: `ES Modules are not supported in electron`
260
+ };
261
+ };
262
+ const getHelpfulChildProcessError = (stdout, stderr) => {
263
+ if (isUnhelpfulNativeModuleError(stderr)) {
264
+ return getNativeModuleErrorMessage(stderr);
265
+ }
266
+ if (isModulesSyntaxError(stderr)) {
267
+ return getModuleSyntaxError();
268
+ }
269
+ if (isModuleNotFoundError(stderr)) {
270
+ return getModuleNotFoundError(stderr);
271
+ }
272
+ const lines = splitLines$1(stderr);
273
+ const {
274
+ actualMessage,
275
+ rest
276
+ } = getDetails(lines);
277
+ return {
278
+ code: '',
279
+ message: actualMessage,
280
+ stack: rest
281
+ };
282
+ };
283
+ class IpcError extends VError {
284
+ // @ts-ignore
285
+ constructor(betterMessage, stdout = '', stderr = '') {
286
+ if (stdout || stderr) {
287
+ // @ts-ignore
288
+ const {
289
+ code,
290
+ message,
291
+ stack
292
+ } = getHelpfulChildProcessError(stdout, stderr);
293
+ const cause = new Error(message);
294
+ // @ts-ignore
295
+ cause.code = code;
296
+ cause.stack = stack;
297
+ super(cause, betterMessage);
298
+ } else {
299
+ super(betterMessage);
300
+ }
301
+ // @ts-ignore
302
+ this.name = 'IpcError';
303
+ // @ts-ignore
304
+ this.stdout = stdout;
305
+ // @ts-ignore
306
+ this.stderr = stderr;
307
+ }
308
+ }
309
+ const readyMessage = 'ready';
310
+ const getData$2 = event => {
311
+ return event.data;
312
+ };
313
+ const listen$7 = () => {
314
+ // @ts-ignore
315
+ if (typeof WorkerGlobalScope === 'undefined') {
316
+ throw new TypeError('module is not in web worker scope');
317
+ }
318
+ return globalThis;
319
+ };
320
+ const signal$8 = global => {
321
+ global.postMessage(readyMessage);
322
+ };
323
+ class IpcChildWithModuleWorker extends Ipc {
324
+ getData(event) {
325
+ return getData$2(event);
326
+ }
327
+ send(message) {
328
+ // @ts-ignore
329
+ this._rawIpc.postMessage(message);
330
+ }
331
+ sendAndTransfer(message) {
332
+ const transfer = getTransferrables(message);
333
+ // @ts-ignore
334
+ this._rawIpc.postMessage(message, transfer);
335
+ }
336
+ dispose() {
337
+ // ignore
338
+ }
339
+ onClose(callback) {
340
+ // ignore
341
+ }
342
+ onMessage(callback) {
343
+ this._rawIpc.addEventListener('message', callback);
344
+ }
345
+ }
346
+ const wrap$f = global => {
347
+ return new IpcChildWithModuleWorker(global);
348
+ };
349
+ const waitForFirstMessage = async port => {
350
+ const {
351
+ promise,
352
+ resolve
353
+ } = Promise.withResolvers();
354
+ port.addEventListener('message', resolve, {
355
+ once: true
356
+ });
357
+ const event = await promise;
358
+ // @ts-ignore
359
+ return event.data;
360
+ };
361
+ const listen$6 = async () => {
362
+ const parentIpcRaw = listen$7();
363
+ signal$8(parentIpcRaw);
364
+ const parentIpc = wrap$f(parentIpcRaw);
365
+ const firstMessage = await waitForFirstMessage(parentIpc);
366
+ if (firstMessage.method !== 'initialize') {
367
+ throw new IpcError('unexpected first message');
368
+ }
369
+ const type = firstMessage.params[0];
370
+ if (type === 'message-port') {
371
+ parentIpc.send({
372
+ id: firstMessage.id,
373
+ jsonrpc: '2.0',
374
+ result: null
375
+ });
376
+ parentIpc.dispose();
377
+ const port = firstMessage.params[1];
378
+ return port;
379
+ }
380
+ return globalThis;
381
+ };
382
+ class IpcChildWithModuleWorkerAndMessagePort extends Ipc {
383
+ getData(event) {
384
+ return getData$2(event);
385
+ }
386
+ send(message) {
387
+ this._rawIpc.postMessage(message);
388
+ }
389
+ sendAndTransfer(message) {
390
+ const transfer = getTransferrables(message);
391
+ this._rawIpc.postMessage(message, transfer);
392
+ }
393
+ dispose() {
394
+ if (this._rawIpc.close) {
395
+ this._rawIpc.close();
396
+ }
397
+ }
398
+ onClose(callback) {
399
+ // ignore
400
+ }
401
+ onMessage(callback) {
402
+ this._rawIpc.addEventListener('message', callback);
403
+ this._rawIpc.start();
404
+ }
405
+ }
406
+ const wrap$e = port => {
407
+ return new IpcChildWithModuleWorkerAndMessagePort(port);
408
+ };
409
+ const IpcChildWithModuleWorkerAndMessagePort$1 = {
410
+ __proto__: null,
411
+ listen: listen$6,
412
+ wrap: wrap$e
413
+ };
414
+ const addListener = (emitter, type, callback) => {
415
+ if ('addEventListener' in emitter) {
416
+ emitter.addEventListener(type, callback);
417
+ } else {
418
+ emitter.on(type, callback);
419
+ }
420
+ };
421
+ const removeListener = (emitter, type, callback) => {
422
+ if ('removeEventListener' in emitter) {
423
+ emitter.removeEventListener(type, callback);
424
+ } else {
425
+ emitter.off(type, callback);
426
+ }
427
+ };
428
+ const getFirstEvent = (eventEmitter, eventMap) => {
429
+ const {
430
+ promise,
431
+ resolve
432
+ } = Promise.withResolvers();
433
+ const listenerMap = Object.create(null);
434
+ const cleanup = value => {
435
+ for (const event of Object.keys(eventMap)) {
436
+ removeListener(eventEmitter, event, listenerMap[event]);
437
+ }
438
+ resolve(value);
439
+ };
440
+ for (const [event, type] of Object.entries(eventMap)) {
441
+ const listener = event => {
442
+ cleanup({
443
+ event,
444
+ type
445
+ });
446
+ };
447
+ addListener(eventEmitter, event, listener);
448
+ listenerMap[event] = listener;
449
+ }
450
+ return promise;
451
+ };
452
+ const Message$1 = 3;
453
+ const create$5 = async ({
454
+ isMessagePortOpen,
455
+ messagePort
456
+ }) => {
457
+ if (!isMessagePort(messagePort)) {
458
+ throw new IpcError('port must be of type MessagePort');
459
+ }
460
+ if (isMessagePortOpen) {
461
+ return messagePort;
462
+ }
463
+ const eventPromise = getFirstEvent(messagePort, {
464
+ message: Message$1
465
+ });
466
+ messagePort.start();
467
+ const {
468
+ event,
469
+ type
470
+ } = await eventPromise;
471
+ if (type !== Message$1) {
472
+ throw new IpcError('Failed to wait for ipc message');
473
+ }
474
+ if (event.data !== readyMessage) {
475
+ throw new IpcError('unexpected first message');
476
+ }
477
+ return messagePort;
478
+ };
479
+ const signal$1 = messagePort => {
480
+ messagePort.start();
481
+ };
482
+ class IpcParentWithMessagePort extends Ipc {
483
+ getData = getData$2;
484
+ send(message) {
485
+ this._rawIpc.postMessage(message);
486
+ }
487
+ sendAndTransfer(message) {
488
+ const transfer = getTransferrables(message);
489
+ this._rawIpc.postMessage(message, transfer);
490
+ }
491
+ dispose() {
492
+ this._rawIpc.close();
493
+ }
494
+ onMessage(callback) {
495
+ this._rawIpc.addEventListener('message', callback);
496
+ }
497
+ onClose(callback) {}
498
+ }
499
+ const wrap$5 = messagePort => {
500
+ return new IpcParentWithMessagePort(messagePort);
501
+ };
502
+ const IpcParentWithMessagePort$1 = {
503
+ __proto__: null,
504
+ create: create$5,
505
+ signal: signal$1,
506
+ wrap: wrap$5
507
+ };
508
+
509
+ const Two$1 = '2.0';
510
+ const callbacks = Object.create(null);
511
+ const get$3 = id => {
512
+ return callbacks[id];
513
+ };
514
+ const remove$1 = id => {
515
+ delete callbacks[id];
516
+ };
517
+ class JsonRpcError extends Error {
518
+ constructor(message) {
519
+ super(message);
520
+ this.name = 'JsonRpcError';
521
+ }
522
+ }
523
+ const NewLine = '\n';
524
+ const DomException = 'DOMException';
525
+ const ReferenceError$1 = 'ReferenceError';
526
+ const SyntaxError$1 = 'SyntaxError';
527
+ const TypeError$1 = 'TypeError';
528
+ const getErrorConstructor = (message, type) => {
529
+ if (type) {
530
+ switch (type) {
531
+ case DomException:
532
+ return DOMException;
533
+ case TypeError$1:
534
+ return TypeError;
535
+ case SyntaxError$1:
536
+ return SyntaxError;
537
+ case ReferenceError$1:
538
+ return ReferenceError;
539
+ default:
540
+ return Error;
541
+ }
542
+ }
543
+ if (message.startsWith('TypeError: ')) {
544
+ return TypeError;
545
+ }
546
+ if (message.startsWith('SyntaxError: ')) {
547
+ return SyntaxError;
548
+ }
549
+ if (message.startsWith('ReferenceError: ')) {
550
+ return ReferenceError;
551
+ }
552
+ return Error;
553
+ };
554
+ const constructError = (message, type, name) => {
555
+ const ErrorConstructor = getErrorConstructor(message, type);
556
+ if (ErrorConstructor === DOMException && name) {
557
+ return new ErrorConstructor(message, name);
558
+ }
559
+ if (ErrorConstructor === Error) {
560
+ const error = new Error(message);
561
+ if (name && name !== 'VError') {
562
+ error.name = name;
563
+ }
564
+ return error;
565
+ }
566
+ return new ErrorConstructor(message);
567
+ };
568
+ const joinLines = lines => {
569
+ return lines.join(NewLine);
570
+ };
571
+ const splitLines = lines => {
572
+ return lines.split(NewLine);
573
+ };
574
+ const getCurrentStack = () => {
575
+ const stackLinesToSkip = 3;
576
+ const currentStack = joinLines(splitLines(new Error().stack || '').slice(stackLinesToSkip));
577
+ return currentStack;
578
+ };
579
+ const getNewLineIndex = (string, startIndex = undefined) => {
580
+ return string.indexOf(NewLine, startIndex);
581
+ };
582
+ const getParentStack = error => {
583
+ let parentStack = error.stack || error.data || error.message || '';
584
+ if (parentStack.startsWith(' at')) {
585
+ parentStack = error.message + NewLine + parentStack;
586
+ }
587
+ return parentStack;
588
+ };
589
+ const MethodNotFound = -32601;
590
+ const Custom = -32001;
591
+ const restoreJsonRpcError = error => {
592
+ const currentStack = getCurrentStack();
593
+ if (error && error instanceof Error) {
594
+ if (typeof error.stack === 'string') {
595
+ error.stack = error.stack + NewLine + currentStack;
596
+ }
597
+ return error;
598
+ }
599
+ if (error && error.code && error.code === MethodNotFound) {
600
+ const restoredError = new JsonRpcError(error.message);
601
+ const parentStack = getParentStack(error);
602
+ restoredError.stack = parentStack + NewLine + currentStack;
603
+ return restoredError;
604
+ }
605
+ if (error && error.message) {
606
+ const restoredError = constructError(error.message, error.type, error.name);
607
+ if (error.data) {
608
+ if (error.data.stack && error.data.type && error.message) {
609
+ restoredError.stack = error.data.type + ': ' + error.message + NewLine + error.data.stack + NewLine + currentStack;
610
+ } else if (error.data.stack) {
611
+ restoredError.stack = error.data.stack;
612
+ }
613
+ if (error.data.codeFrame) {
614
+ // @ts-ignore
615
+ restoredError.codeFrame = error.data.codeFrame;
616
+ }
617
+ if (error.data.code) {
618
+ // @ts-ignore
619
+ restoredError.code = error.data.code;
620
+ }
621
+ if (error.data.type) {
622
+ // @ts-ignore
623
+ restoredError.name = error.data.type;
624
+ }
625
+ } else {
626
+ if (error.stack) {
627
+ const lowerStack = restoredError.stack || '';
628
+ // @ts-ignore
629
+ const indexNewLine = getNewLineIndex(lowerStack);
630
+ const parentStack = getParentStack(error);
631
+ // @ts-ignore
632
+ restoredError.stack = parentStack + lowerStack.slice(indexNewLine);
633
+ }
634
+ if (error.codeFrame) {
635
+ // @ts-ignore
636
+ restoredError.codeFrame = error.codeFrame;
637
+ }
638
+ }
639
+ return restoredError;
640
+ }
641
+ if (typeof error === 'string') {
642
+ return new Error(`JsonRpc Error: ${error}`);
643
+ }
644
+ return new Error(`JsonRpc Error: ${error}`);
645
+ };
646
+ const unwrapJsonRpcResult = responseMessage => {
647
+ if ('error' in responseMessage) {
648
+ const restoredError = restoreJsonRpcError(responseMessage.error);
649
+ throw restoredError;
650
+ }
651
+ if ('result' in responseMessage) {
652
+ return responseMessage.result;
653
+ }
654
+ throw new JsonRpcError('unexpected response message');
655
+ };
656
+ const warn = (...args) => {
657
+ console.warn(...args);
658
+ };
659
+ const resolve = (id, response) => {
660
+ const fn = get$3(id);
661
+ if (!fn) {
662
+ console.log(response);
663
+ warn(`callback ${id} may already be disposed`);
664
+ return;
665
+ }
666
+ fn(response);
667
+ remove$1(id);
668
+ };
669
+ const E_COMMAND_NOT_FOUND = 'E_COMMAND_NOT_FOUND';
670
+ const getErrorType = prettyError => {
671
+ if (prettyError && prettyError.type) {
672
+ return prettyError.type;
673
+ }
674
+ if (prettyError && prettyError.constructor && prettyError.constructor.name) {
675
+ return prettyError.constructor.name;
676
+ }
677
+ return undefined;
678
+ };
679
+ const isAlreadyStack = line => {
680
+ return line.trim().startsWith('at ');
681
+ };
682
+ const getStack = prettyError => {
683
+ const stackString = prettyError.stack || '';
684
+ const newLineIndex = stackString.indexOf('\n');
685
+ if (newLineIndex !== -1 && !isAlreadyStack(stackString.slice(0, newLineIndex))) {
686
+ return stackString.slice(newLineIndex + 1);
687
+ }
688
+ return stackString;
689
+ };
690
+ const getErrorProperty = (error, prettyError) => {
691
+ if (error && error.code === E_COMMAND_NOT_FOUND) {
692
+ return {
693
+ code: MethodNotFound,
694
+ message: error.message,
695
+ data: error.stack
696
+ };
697
+ }
698
+ return {
699
+ code: Custom,
700
+ message: prettyError.message,
701
+ data: {
702
+ stack: getStack(prettyError),
703
+ codeFrame: prettyError.codeFrame,
704
+ type: getErrorType(prettyError),
705
+ code: prettyError.code,
706
+ name: prettyError.name
707
+ }
708
+ };
709
+ };
710
+ const create$1$2 = (id, error) => {
711
+ return {
712
+ jsonrpc: Two$1,
713
+ id,
714
+ error
715
+ };
716
+ };
717
+ const getErrorResponse = (id, error, preparePrettyError, logError) => {
718
+ const prettyError = preparePrettyError(error);
719
+ logError(error, prettyError);
720
+ const errorProperty = getErrorProperty(error, prettyError);
721
+ return create$1$2(id, errorProperty);
722
+ };
723
+ const create$3 = (message, result) => {
724
+ return {
725
+ jsonrpc: Two$1,
726
+ id: message.id,
727
+ result: result ?? null
728
+ };
729
+ };
730
+ const getSuccessResponse = (message, result) => {
731
+ const resultProperty = result ?? null;
732
+ return create$3(message, resultProperty);
733
+ };
734
+ const getErrorResponseSimple = (id, error) => {
735
+ return {
736
+ jsonrpc: Two$1,
737
+ id,
738
+ error: {
739
+ code: Custom,
740
+ // @ts-ignore
741
+ message: error.message,
742
+ data: error
743
+ }
744
+ };
745
+ };
746
+ const getResponse = async (message, ipc, execute, preparePrettyError, logError, requiresSocket) => {
747
+ try {
748
+ const result = requiresSocket(message.method) ? await execute(message.method, ipc, ...message.params) : await execute(message.method, ...message.params);
749
+ return getSuccessResponse(message, result);
750
+ } catch (error) {
751
+ if (ipc.canUseSimpleErrorResponse) {
752
+ return getErrorResponseSimple(message.id, error);
753
+ }
754
+ return getErrorResponse(message.id, error, preparePrettyError, logError);
755
+ }
756
+ };
757
+ const defaultPreparePrettyError = error => {
758
+ return error;
759
+ };
760
+ const defaultLogError = () => {
761
+ // ignore
762
+ };
763
+ const defaultRequiresSocket = () => {
764
+ return false;
765
+ };
766
+ const defaultResolve = resolve;
767
+
768
+ // TODO maybe remove this in v6 or v7, only accept options object to simplify the code
769
+ const normalizeParams = args => {
770
+ if (args.length === 1) {
771
+ const options = args[0];
772
+ return {
773
+ ipc: options.ipc,
774
+ message: options.message,
775
+ execute: options.execute,
776
+ resolve: options.resolve || defaultResolve,
777
+ preparePrettyError: options.preparePrettyError || defaultPreparePrettyError,
778
+ logError: options.logError || defaultLogError,
779
+ requiresSocket: options.requiresSocket || defaultRequiresSocket
780
+ };
781
+ }
782
+ return {
783
+ ipc: args[0],
784
+ message: args[1],
785
+ execute: args[2],
786
+ resolve: args[3],
787
+ preparePrettyError: args[4],
788
+ logError: args[5],
789
+ requiresSocket: args[6]
790
+ };
791
+ };
792
+ const handleJsonRpcMessage = async (...args) => {
793
+ const options = normalizeParams(args);
794
+ const {
795
+ message,
796
+ ipc,
797
+ execute,
798
+ resolve,
799
+ preparePrettyError,
800
+ logError,
801
+ requiresSocket
802
+ } = options;
803
+ if ('id' in message) {
804
+ if ('method' in message) {
805
+ const response = await getResponse(message, ipc, execute, preparePrettyError, logError, requiresSocket);
806
+ try {
807
+ ipc.send(response);
808
+ } catch (error) {
809
+ const errorResponse = getErrorResponse(message.id, error, preparePrettyError, logError);
810
+ ipc.send(errorResponse);
811
+ }
812
+ return;
813
+ }
814
+ resolve(message.id, message);
815
+ return;
816
+ }
817
+ if ('method' in message) {
818
+ await getResponse(message, ipc, execute, preparePrettyError, logError, requiresSocket);
819
+ return;
820
+ }
821
+ throw new JsonRpcError('unexpected message');
822
+ };
823
+
824
+ class CommandNotFoundError extends Error {
825
+ constructor(command) {
826
+ super(`Command not found ${command}`);
827
+ this.name = 'CommandNotFoundError';
828
+ }
829
+ }
830
+ const commands = Object.create(null);
831
+ const register = commandMap => {
832
+ Object.assign(commands, commandMap);
833
+ };
834
+ const getCommand = key => {
835
+ return commands[key];
836
+ };
837
+ const execute = (command, ...args) => {
838
+ const fn = getCommand(command);
839
+ if (!fn) {
840
+ throw new CommandNotFoundError(command);
841
+ }
842
+ return fn(...args);
843
+ };
844
+
845
+ const Two = '2.0';
846
+ const create$s = (method, params) => {
847
+ return {
848
+ jsonrpc: Two,
849
+ method,
850
+ params
851
+ };
852
+ };
853
+ const create$r = (id, method, params) => {
854
+ const message = {
855
+ id,
856
+ jsonrpc: Two,
857
+ method,
858
+ params
859
+ };
860
+ return message;
861
+ };
862
+ let id$1 = 0;
863
+ const create$q = () => {
864
+ return ++id$1;
865
+ };
866
+
867
+ /* eslint-disable n/no-unsupported-features/es-syntax */
868
+
869
+ const registerPromise = map => {
870
+ const id = create$q();
871
+ const {
872
+ promise,
873
+ resolve
874
+ } = Promise.withResolvers();
875
+ map[id] = resolve;
876
+ return {
877
+ id,
878
+ promise
879
+ };
880
+ };
881
+
882
+ // @ts-ignore
883
+ const invokeHelper = async (callbacks, ipc, method, params, useSendAndTransfer) => {
884
+ const {
885
+ id,
886
+ promise
887
+ } = registerPromise(callbacks);
888
+ const message = create$r(id, method, params);
889
+ if (useSendAndTransfer && ipc.sendAndTransfer) {
890
+ ipc.sendAndTransfer(message);
891
+ } else {
892
+ ipc.send(message);
893
+ }
894
+ const responseMessage = await promise;
895
+ return unwrapJsonRpcResult(responseMessage);
896
+ };
897
+ const createRpc = ipc => {
898
+ const callbacks = Object.create(null);
899
+ ipc._resolve = (id, response) => {
900
+ const fn = callbacks[id];
901
+ if (!fn) {
902
+ console.warn(`callback ${id} may already be disposed`);
903
+ return;
904
+ }
905
+ fn(response);
906
+ delete callbacks[id];
907
+ };
908
+ const rpc = {
909
+ async dispose() {
910
+ await ipc?.dispose();
911
+ },
912
+ invoke(method, ...params) {
913
+ return invokeHelper(callbacks, ipc, method, params, false);
914
+ },
915
+ invokeAndTransfer(method, ...params) {
916
+ return invokeHelper(callbacks, ipc, method, params, true);
917
+ },
918
+ // @ts-ignore
919
+ ipc,
920
+ /**
921
+ * @deprecated
922
+ */
923
+ send(method, ...params) {
924
+ const message = create$s(method, params);
925
+ ipc.send(message);
926
+ }
927
+ };
928
+ return rpc;
929
+ };
930
+ const requiresSocket = () => {
931
+ return false;
932
+ };
933
+ const preparePrettyError = error => {
934
+ return error;
935
+ };
936
+ const logError = () => {
937
+ // handled by renderer worker
938
+ };
939
+ const handleMessage = event => {
940
+ const actualRequiresSocket = event?.target?.requiresSocket || requiresSocket;
941
+ const actualExecute = event?.target?.execute || execute;
942
+ return handleJsonRpcMessage(event.target, event.data, actualExecute, event.target._resolve, preparePrettyError, logError, actualRequiresSocket);
943
+ };
944
+ const handleIpc = ipc => {
945
+ if ('addEventListener' in ipc) {
946
+ ipc.addEventListener('message', handleMessage);
947
+ } else if ('on' in ipc) {
948
+ // deprecated
949
+ ipc.on('message', handleMessage);
950
+ }
951
+ };
952
+ const listen$1 = async (module, options) => {
953
+ const rawIpc = await module.listen(options);
954
+ if (module.signal) {
955
+ module.signal(rawIpc);
956
+ }
957
+ const ipc = module.wrap(rawIpc);
958
+ return ipc;
959
+ };
960
+ const create$4 = async ({
961
+ commandMap,
962
+ isMessagePortOpen = true,
963
+ messagePort
964
+ }) => {
965
+ // TODO create a commandMap per rpc instance
966
+ register(commandMap);
967
+ const rawIpc = await IpcParentWithMessagePort$1.create({
968
+ isMessagePortOpen,
969
+ messagePort
970
+ });
971
+ const ipc = IpcParentWithMessagePort$1.wrap(rawIpc);
972
+ handleIpc(ipc);
973
+ const rpc = createRpc(ipc);
974
+ messagePort.start();
975
+ return rpc;
976
+ };
977
+ const create$2$1 = async ({
978
+ commandMap,
979
+ isMessagePortOpen,
980
+ send
981
+ }) => {
982
+ const {
983
+ port1,
984
+ port2
985
+ } = new MessageChannel();
986
+ await send(port1);
987
+ return create$4({
988
+ commandMap,
989
+ isMessagePortOpen,
990
+ messagePort: port2
991
+ });
992
+ };
993
+ const TransferMessagePortRpcParent = {
994
+ __proto__: null,
995
+ create: create$2$1
996
+ };
997
+ const create$1$1 = async ({
998
+ commandMap
999
+ }) => {
1000
+ // TODO create a commandMap per rpc instance
1001
+ register(commandMap);
1002
+ const ipc = await listen$1(IpcChildWithModuleWorkerAndMessagePort$1);
1003
+ handleIpc(ipc);
1004
+ const rpc = createRpc(ipc);
1005
+ return rpc;
1006
+ };
1007
+ const WebWorkerRpcClient = {
1008
+ __proto__: null,
1009
+ create: create$1$1
1010
+ };
1011
+ const createMockRpc = ({
1012
+ commandMap
1013
+ }) => {
1014
+ const invocations = [];
1015
+ const invoke = (method, ...params) => {
1016
+ invocations.push([method, ...params]);
1017
+ const command = commandMap[method];
1018
+ if (!command) {
1019
+ throw new Error(`command ${method} not found`);
1020
+ }
1021
+ return command(...params);
1022
+ };
1023
+ const mockRpc = {
1024
+ invocations,
1025
+ invoke,
1026
+ invokeAndTransfer: invoke
1027
+ };
1028
+ return mockRpc;
1029
+ };
1030
+
1031
+ const Button$1 = 'button';
1032
+
1033
+ const Button = 1;
1034
+ const Div = 4;
1035
+ const Span = 8;
1036
+ const Text = 12;
1037
+ const Pre = 51;
1038
+
1039
+ const TargetName = 'event.target.name';
1040
+
1041
+ const ExtensionHostWorker = 44;
1042
+ const RendererWorker = 1;
1043
+
1044
+ const SetDom2 = 'Viewlet.setDom2';
1045
+
1046
+ const rpcs = Object.create(null);
1047
+ const set$3 = (id, rpc) => {
1048
+ rpcs[id] = rpc;
1049
+ };
1050
+ const get$2 = id => {
1051
+ return rpcs[id];
1052
+ };
1053
+ const remove = id => {
1054
+ delete rpcs[id];
1055
+ };
1056
+
1057
+ /* eslint-disable @typescript-eslint/explicit-function-return-type */
1058
+ const create$2 = rpcId => {
1059
+ return {
1060
+ async dispose() {
1061
+ const rpc = get$2(rpcId);
1062
+ await rpc.dispose();
1063
+ },
1064
+ // @ts-ignore
1065
+ invoke(method, ...params) {
1066
+ const rpc = get$2(rpcId);
1067
+ // @ts-ignore
1068
+ return rpc.invoke(method, ...params);
1069
+ },
1070
+ // @ts-ignore
1071
+ invokeAndTransfer(method, ...params) {
1072
+ const rpc = get$2(rpcId);
1073
+ // @ts-ignore
1074
+ return rpc.invokeAndTransfer(method, ...params);
1075
+ },
1076
+ registerMockRpc(commandMap) {
1077
+ const mockRpc = createMockRpc({
1078
+ commandMap
1079
+ });
1080
+ set$3(rpcId, mockRpc);
1081
+ // @ts-ignore
1082
+ mockRpc[Symbol.dispose] = () => {
1083
+ remove(rpcId);
1084
+ };
1085
+ // @ts-ignore
1086
+ return mockRpc;
1087
+ },
1088
+ set(rpc) {
1089
+ set$3(rpcId, rpc);
1090
+ }
1091
+ };
1092
+ };
1093
+
1094
+ const {
1095
+ invoke: invoke$1,
1096
+ set: set$2
1097
+ } = create$2(ExtensionHostWorker);
1098
+
1099
+ const {
1100
+ invoke,
1101
+ invokeAndTransfer,
1102
+ set: set$1
1103
+ } = create$2(RendererWorker);
1104
+ const sendMessagePortToExtensionHostWorker$1 = async (port, rpcId = 0) => {
1105
+ const command = 'HandleMessagePort.handleMessagePort2';
1106
+ await invokeAndTransfer('SendMessagePortToExtensionHostWorker.sendMessagePortToExtensionHostWorker', port, command, rpcId);
1107
+ };
1108
+ const activateByEvent$1 = (event, assetDir, platform) => {
1109
+ return invoke('ExtensionHostManagement.activateByEvent', event, assetDir, platform);
1110
+ };
1111
+ const getPreference = async key => {
1112
+ return await invoke('Preferences.get', key);
1113
+ };
1114
+
1115
+ const toCommandId = key => {
1116
+ const dotIndex = key.indexOf('.');
1117
+ return key.slice(dotIndex + 1);
1118
+ };
1119
+ const create$1 = () => {
1120
+ const states = Object.create(null);
1121
+ const commandMapRef = {};
1122
+ return {
1123
+ get(uid) {
1124
+ return states[uid];
1125
+ },
1126
+ set(uid, oldState, newState) {
1127
+ states[uid] = {
1128
+ oldState,
1129
+ newState
1130
+ };
1131
+ },
1132
+ dispose(uid) {
1133
+ delete states[uid];
1134
+ },
1135
+ getKeys() {
1136
+ return Object.keys(states).map(key => {
1137
+ return Number.parseInt(key);
1138
+ });
1139
+ },
1140
+ clear() {
1141
+ for (const key of Object.keys(states)) {
1142
+ delete states[key];
1143
+ }
1144
+ },
1145
+ wrapCommand(fn) {
1146
+ const wrapped = async (uid, ...args) => {
1147
+ const {
1148
+ oldState,
1149
+ newState
1150
+ } = states[uid];
1151
+ const newerState = await fn(newState, ...args);
1152
+ if (oldState === newerState || newState === newerState) {
1153
+ return;
1154
+ }
1155
+ const latest = states[uid];
1156
+ states[uid] = {
1157
+ oldState: latest.oldState,
1158
+ newState: newerState
1159
+ };
1160
+ };
1161
+ return wrapped;
1162
+ },
1163
+ wrapGetter(fn) {
1164
+ const wrapped = (uid, ...args) => {
1165
+ const {
1166
+ newState
1167
+ } = states[uid];
1168
+ return fn(newState, ...args);
1169
+ };
1170
+ return wrapped;
1171
+ },
1172
+ diff(uid, modules, numbers) {
1173
+ const {
1174
+ oldState,
1175
+ newState
1176
+ } = states[uid];
1177
+ const diffResult = [];
1178
+ for (let i = 0; i < modules.length; i++) {
1179
+ const fn = modules[i];
1180
+ if (!fn(oldState, newState)) {
1181
+ diffResult.push(numbers[i]);
1182
+ }
1183
+ }
1184
+ return diffResult;
1185
+ },
1186
+ getCommandIds() {
1187
+ const keys = Object.keys(commandMapRef);
1188
+ const ids = keys.map(toCommandId);
1189
+ return ids;
1190
+ },
1191
+ registerCommands(commandMap) {
1192
+ Object.assign(commandMapRef, commandMap);
1193
+ }
1194
+ };
1195
+ };
1196
+ const terminate = () => {
1197
+ globalThis.close();
1198
+ };
1199
+
1200
+ const {
1201
+ get: get$1,
1202
+ getCommandIds: getCommandIds$1,
1203
+ registerCommands: registerCommands$1,
1204
+ set,
1205
+ wrapCommand: wrapCommand$1} = create$1();
1206
+
1207
+ const create = (uid, uri, x, y, width, height, platform, assetDir) => {
1208
+ const state = {
1209
+ assetDir,
1210
+ platform,
1211
+ statusBarItemsLeft: [],
1212
+ statusBarItemsRight: [],
1213
+ uid
1214
+ };
1215
+ set(uid, state, state);
1216
+ };
1217
+
1218
+ const isEqual = (oldState, newState) => {
1219
+ return oldState.statusBarItemsLeft === newState.statusBarItemsLeft && oldState.statusBarItemsRight === newState.statusBarItemsRight;
1220
+ };
1221
+
1222
+ const RenderItems = 4;
1223
+
1224
+ const modules = [isEqual];
1225
+ const numbers = [RenderItems];
1226
+
1227
+ const diff = (oldState, newState) => {
1228
+ const diffResult = [];
1229
+ for (let i = 0; i < modules.length; i++) {
1230
+ const fn = modules[i];
1231
+ if (!fn(oldState, newState)) {
1232
+ diffResult.push(numbers[i]);
1233
+ }
1234
+ }
1235
+ return diffResult;
1236
+ };
1237
+
1238
+ const diff2 = uid => {
1239
+ const {
1240
+ newState,
1241
+ oldState
1242
+ } = get$1(uid);
1243
+ const result = diff(oldState, newState);
1244
+ return result;
1245
+ };
1246
+
1247
+ const getMatchingItem = (itemsLeft, itemsRight, name) => {
1248
+ for (const item of itemsLeft) {
1249
+ if (item.name === name) {
1250
+ return item;
1251
+ }
1252
+ }
1253
+ for (const item of itemsRight) {
1254
+ if (item.name === name) {
1255
+ return item;
1256
+ }
1257
+ }
1258
+ return undefined;
1259
+ };
1260
+
1261
+ const handleClickExtensionStatusBarItem = async name => {
1262
+ // @ts-ignore
1263
+ await invoke$1(`ExtensionHostStatusBar.executeCommand`, name);
1264
+ };
1265
+
1266
+ const handleClickNotification = async () => {
1267
+ // TODO toggle notifications
1268
+ };
1269
+
1270
+ const handleClickProblems = async () => {
1271
+ // @ts-ignore
1272
+ await invoke('Layout.showPanel');
1273
+ // @ts-ignore
1274
+ await invoke('Panel.toggleView', 'Problems');
1275
+ };
1276
+
1277
+ const Notifications = 'Notifications';
1278
+ const Problems = 'Problems';
1279
+
1280
+ const handleClick = async (state, name) => {
1281
+ if (!name) {
1282
+ return state;
1283
+ }
1284
+ const {
1285
+ statusBarItemsLeft,
1286
+ statusBarItemsRight
1287
+ } = state;
1288
+ const item = getMatchingItem(statusBarItemsLeft, statusBarItemsRight, name);
1289
+ if (!item) {
1290
+ return state;
1291
+ }
1292
+ if (item.name === Notifications) {
1293
+ await handleClickNotification();
1294
+ } else if (item.name === Problems) {
1295
+ await handleClickProblems();
1296
+ } else {
1297
+ await handleClickExtensionStatusBarItem(name);
1298
+ }
1299
+ // TODO
1300
+ // sendExtensionWorker([/* statusBarItemHandleClick */ 7657, /* name */ name])
1301
+ return state;
1302
+ };
1303
+
1304
+ const id = 7201;
1305
+ const sendMessagePortToExtensionHostWorker = async port => {
1306
+ await sendMessagePortToExtensionHostWorker$1(port, id);
1307
+ };
1308
+
1309
+ const createExtensionHostRpc = async () => {
1310
+ try {
1311
+ const rpc = await TransferMessagePortRpcParent.create({
1312
+ commandMap: {},
1313
+ send: sendMessagePortToExtensionHostWorker
1314
+ });
1315
+ return rpc;
1316
+ } catch (error) {
1317
+ throw new VError(error, `Failed to create extension host rpc`);
1318
+ }
1319
+ };
1320
+
1321
+ const initialize = async () => {
1322
+ const rpc = await createExtensionHostRpc();
1323
+ set$2(rpc);
1324
+ };
1325
+
1326
+ const getIndex = (items, item) => {
1327
+ for (let i = 0; i < items.length; i++) {
1328
+ if (items[i].name === item.name) {
1329
+ return i;
1330
+ }
1331
+ }
1332
+ return -1;
1333
+ };
1334
+
1335
+ const updateArray = (items, newItem) => {
1336
+ const index = getIndex(items, newItem);
1337
+ const before = items.slice(0, index);
1338
+ const after = items.slice(index + 1);
1339
+ return [...before, newItem, ...after];
1340
+ };
1341
+
1342
+ const itemLeftUpdate = (state, newItem) => {
1343
+ return {
1344
+ ...state,
1345
+ statusBarItemsLeft: updateArray([...state.statusBarItemsLeft], newItem)
1346
+ };
1347
+ };
1348
+
1349
+ const itemRightCreate = (state, newItem) => {
1350
+ const {
1351
+ statusBarItemsRight
1352
+ } = state;
1353
+ const newStatusBarItemsRight = [...statusBarItemsRight, newItem];
1354
+ return {
1355
+ ...state,
1356
+ statusBarItemsRight: newStatusBarItemsRight
1357
+ };
1358
+ };
1359
+
1360
+ const itemRightUpdate = (state, newItem) => {
1361
+ const {
1362
+ statusBarItemsRight
1363
+ } = state;
1364
+ const newStatusBarItemsRight = updateArray([...statusBarItemsRight], newItem);
1365
+ return {
1366
+ ...state,
1367
+ statusBarItemsRight: newStatusBarItemsRight
1368
+ };
1369
+ };
1370
+
1371
+ const ProblemsErrorIcon = 'ProblemsErrorIcon';
1372
+ const ProblemsWarningIcon = 'ProblemsWarningIcon';
1373
+ const StatusBarItem = 'StatusBarItem';
1374
+ const StatusBarItemsLeft = 'StatusBarItemsLeft';
1375
+ const StatusBarItemsRight = 'StatusBarItemsRight';
1376
+
1377
+ const OnStatusBarItem = 'onStatusBarItem';
1378
+
1379
+ const GetStatusBarItems = 'ExtensionHost.getStatusBarItems2';
1380
+
1381
+ const activateByEvent = (event, assetDir, platform) => {
1382
+ // @ts-ignore
1383
+ return activateByEvent$1(event, assetDir, platform);
1384
+ };
1385
+
1386
+ const executeProviders = async ({
1387
+ assetDir,
1388
+ combineResults,
1389
+ event,
1390
+ method,
1391
+ noProviderFoundMessage = 'No provider found',
1392
+ noProviderFoundResult,
1393
+ params,
1394
+ platform
1395
+ }) => {
1396
+ await activateByEvent(event, assetDir, platform);
1397
+ // @ts-ignore
1398
+ const result = await invoke$1(method, ...params);
1399
+ return result;
1400
+ };
1401
+
1402
+ const combineResults = results => {
1403
+ return results.flat();
1404
+ };
1405
+ const getStatusBarItems$1 = (assetDir, platform) => {
1406
+ return executeProviders({
1407
+ assetDir,
1408
+ combineResults,
1409
+ event: OnStatusBarItem,
1410
+ method: GetStatusBarItems,
1411
+ noProviderFoundMessage: 'No status bar item provider found',
1412
+ noProviderFoundResult: [],
1413
+ params: [],
1414
+ platform
1415
+ });
1416
+ };
1417
+
1418
+ const toStatusBarItem = uiStatusBarItem => {
1419
+ const elements = [];
1420
+ if (uiStatusBarItem.icon) {
1421
+ elements.push({
1422
+ type: 'icon',
1423
+ value: uiStatusBarItem.icon
1424
+ });
1425
+ }
1426
+ if (uiStatusBarItem.text) {
1427
+ elements.push({
1428
+ type: 'text',
1429
+ value: uiStatusBarItem.text
1430
+ });
1431
+ }
1432
+ if (elements.length === 0) {
1433
+ elements.push({
1434
+ type: 'text',
1435
+ value: ''
1436
+ });
1437
+ }
1438
+ return {
1439
+ command: uiStatusBarItem.command || undefined,
1440
+ elements,
1441
+ name: uiStatusBarItem.name,
1442
+ tooltip: uiStatusBarItem.tooltip
1443
+ };
1444
+ };
1445
+
1446
+ const toUiStatusBarItem = extensionHostStatusBarItem => {
1447
+ return {
1448
+ command: extensionHostStatusBarItem.command || '',
1449
+ icon: extensionHostStatusBarItem.icon || '',
1450
+ name: extensionHostStatusBarItem.id || extensionHostStatusBarItem.name || '',
1451
+ text: extensionHostStatusBarItem.text || '',
1452
+ tooltip: extensionHostStatusBarItem.tooltip || ''
1453
+ };
1454
+ };
1455
+
1456
+ const toUiStatusBarItems = statusBarItems => {
1457
+ if (!statusBarItems) {
1458
+ return [];
1459
+ }
1460
+ return statusBarItems.map(toUiStatusBarItem);
1461
+ };
1462
+
1463
+ const getStatusBarItems = async (showItems, assetDir, platform) => {
1464
+ if (!showItems) {
1465
+ return [];
1466
+ }
1467
+ await activateByEvent('onSourceControl', assetDir, platform);
1468
+ const extensionStatusBarItems = await getStatusBarItems$1(assetDir, platform);
1469
+ const uiStatusBarItems = toUiStatusBarItems(extensionStatusBarItems);
1470
+ const extraItems = [{
1471
+ command: undefined,
1472
+ elements: [{
1473
+ type: 'text',
1474
+ value: 'Notifications'
1475
+ }],
1476
+ name: Notifications,
1477
+ tooltip: ''
1478
+ }, {
1479
+ command: undefined,
1480
+ elements: [{
1481
+ type: 'icon',
1482
+ value: ProblemsErrorIcon
1483
+ }, {
1484
+ type: 'text',
1485
+ value: '0'
1486
+ }, {
1487
+ type: 'icon',
1488
+ value: ProblemsWarningIcon
1489
+ }, {
1490
+ type: 'text',
1491
+ value: '0'
1492
+ }],
1493
+ name: Problems,
1494
+ tooltip: ''
1495
+ }];
1496
+ return [...uiStatusBarItems.map(toStatusBarItem), ...extraItems];
1497
+ };
1498
+
1499
+ const get = async key => {
1500
+ return getPreference(key);
1501
+ };
1502
+
1503
+ const itemsVisible = async () => {
1504
+ const statusBarItemsPreference = (await get('statusBar.itemsVisible')) ?? true;
1505
+ return statusBarItemsPreference;
1506
+ };
1507
+
1508
+ const loadContent = async state => {
1509
+ const {
1510
+ assetDir,
1511
+ platform
1512
+ } = state;
1513
+ const statusBarItemsPreference = await itemsVisible();
1514
+ const statusBarItems = await getStatusBarItems(statusBarItemsPreference, assetDir, platform);
1515
+ return {
1516
+ ...state,
1517
+ statusBarItemsLeft: [...statusBarItems],
1518
+ statusBarItemsRight: []
1519
+ };
1520
+ };
1521
+
1522
+ const text = data => {
1523
+ return {
1524
+ childCount: 0,
1525
+ text: data,
1526
+ type: Text
1527
+ };
1528
+ };
1529
+
1530
+ const HandleClick = 11;
1531
+
1532
+ const getElementVirtualDom = element => {
1533
+ if (element.type === 'text') {
1534
+ return [text(element.value)];
1535
+ }
1536
+ if (element.type === 'icon') {
1537
+ return [{
1538
+ childCount: 0,
1539
+ className: element.value,
1540
+ type: Div
1541
+ }];
1542
+ }
1543
+ return [];
1544
+ };
1545
+ const getStatusBarItemVirtualDom = statusBarItem => {
1546
+ const {
1547
+ elements,
1548
+ name,
1549
+ tooltip
1550
+ } = statusBarItem;
1551
+ const elementNodes = elements.flatMap(getElementVirtualDom);
1552
+ return [{
1553
+ childCount: elementNodes.length,
1554
+ className: StatusBarItem,
1555
+ name,
1556
+ role: Button$1,
1557
+ tabIndex: -1,
1558
+ title: tooltip,
1559
+ type: Button
1560
+ }, ...elementNodes];
1561
+ };
1562
+
1563
+ const getStatusBarItemsVirtualDom = (items, className) => {
1564
+ if (items.length === 0) {
1565
+ return [];
1566
+ }
1567
+ return [{
1568
+ childCount: items.length,
1569
+ className,
1570
+ type: Div
1571
+ }, ...items.flatMap(getStatusBarItemVirtualDom)];
1572
+ };
1573
+
1574
+ const getChildCount = (leftCount, rightCount) => {
1575
+ let count = 0;
1576
+ if (leftCount > 0) {
1577
+ count++;
1578
+ }
1579
+ if (rightCount > 0) {
1580
+ count++;
1581
+ }
1582
+ return count;
1583
+ };
1584
+ const getStatusBarVirtualDom = (statusBarItemsLeft, statusBarItemsRight) => {
1585
+ const dom = [{
1586
+ childCount: getChildCount(statusBarItemsLeft.length, statusBarItemsRight.length),
1587
+ className: 'StatusBar',
1588
+ onClick: HandleClick,
1589
+ type: Div
1590
+ }, ...getStatusBarItemsVirtualDom(statusBarItemsLeft, StatusBarItemsLeft), ...getStatusBarItemsVirtualDom(statusBarItemsRight, StatusBarItemsRight)];
1591
+ return dom;
1592
+ };
1593
+
1594
+ const renderItems = (oldState, newState) => {
1595
+ const {
1596
+ statusBarItemsLeft,
1597
+ statusBarItemsRight,
1598
+ uid
1599
+ } = newState;
1600
+ const dom = getStatusBarVirtualDom(statusBarItemsLeft, statusBarItemsRight);
1601
+ return [SetDom2, uid, dom];
1602
+ };
1603
+
1604
+ const getRenderer = diffType => {
1605
+ switch (diffType) {
1606
+ case RenderItems:
1607
+ return renderItems;
1608
+ default:
1609
+ throw new Error('unknown renderer');
1610
+ }
1611
+ };
1612
+
1613
+ const applyRender = (oldState, newState, diffResult) => {
1614
+ const commands = [];
1615
+ for (const item of diffResult) {
1616
+ const fn = getRenderer(item);
1617
+ const result = fn(oldState, newState);
1618
+ if (result.length > 0) {
1619
+ commands.push(result);
1620
+ }
1621
+ }
1622
+ return commands;
1623
+ };
1624
+
1625
+ const render2 = (uid, diffResult) => {
1626
+ const {
1627
+ newState,
1628
+ oldState
1629
+ } = get$1(uid);
1630
+ set(uid, newState, newState);
1631
+ const commands = applyRender(oldState, newState, diffResult);
1632
+ return commands;
1633
+ };
1634
+
1635
+ const renderEventListeners = () => {
1636
+ return [{
1637
+ name: HandleClick,
1638
+ params: ['handleClick', TargetName]
1639
+ }];
1640
+ };
1641
+
1642
+ const resize = (state, dimensions) => {
1643
+ return {
1644
+ ...state,
1645
+ ...dimensions
1646
+ };
1647
+ };
1648
+
1649
+ const saveState = uid => {
1650
+ number(uid);
1651
+ const value = get$1(uid);
1652
+ const {
1653
+ newState
1654
+ } = value;
1655
+ const {
1656
+ statusBarItemsLeft,
1657
+ statusBarItemsRight
1658
+ } = newState;
1659
+ return {
1660
+ itemsLeft: statusBarItemsLeft,
1661
+ itemsRight: statusBarItemsRight
1662
+ };
1663
+ };
1664
+
1665
+ const commandMap = {
1666
+ 'StatusBar.create': create,
1667
+ 'StatusBar.diff2': diff2,
1668
+ 'StatusBar.getCommandIds': getCommandIds$1,
1669
+ 'StatusBar.handleClick': wrapCommand$1(handleClick),
1670
+ 'StatusBar.initialize': initialize,
1671
+ 'StatusBar.itemLeftUpdate': wrapCommand$1(itemLeftUpdate),
1672
+ 'StatusBar.itemRightCreate': wrapCommand$1(itemRightCreate),
1673
+ 'StatusBar.itemRightUpdate': wrapCommand$1(itemRightUpdate),
1674
+ 'StatusBar.loadContent': wrapCommand$1(loadContent),
1675
+ 'StatusBar.render2': render2,
1676
+ 'StatusBar.renderEventListeners': renderEventListeners,
1677
+ 'StatusBar.resize': wrapCommand$1(resize),
1678
+ 'StatusBar.saveState': saveState,
1679
+ 'StatusBar.terminate': terminate
1680
+ };
1681
+
1682
+ const listen = async () => {
1683
+ registerCommands$1(commandMap);
1684
+ const rpc = await WebWorkerRpcClient.create({
1685
+ commandMap: commandMap
1686
+ });
1687
+ set$1(rpc);
1688
+ };
1689
+
1690
+ const main$2 = async () => {
1691
+ await listen();
1692
+ };
1693
+
1694
+ const closeEditorGroup = (state, groupId) => {
1695
+ const groupIndex = state.layout.groups.findIndex(group => group.id === groupId);
1696
+ if (groupIndex === -1 || state.layout.groups.length <= 1) {
1697
+ return state;
1698
+ }
1699
+ const remainingGroups = state.layout.groups.filter(group => group.id !== groupId);
1700
+ const redistributedGroups = remainingGroups.map((group, index) => ({
1701
+ ...group,
1702
+ size: Math.round(100 / remainingGroups.length)
1703
+ }));
1704
+ const newActiveGroupId = state.layout.activeGroupId === groupId ? remainingGroups[0].id : state.layout.activeGroupId;
1705
+ return {
1706
+ ...state,
1707
+ layout: {
1708
+ ...state.layout,
1709
+ activeGroupId: newActiveGroupId,
1710
+ groups: redistributedGroups
1711
+ }
1712
+ };
1713
+ };
1714
+
1715
+ const closeTab = (state, groupId, tabId) => {
1716
+ const groups = state.layout.groups.map(group => {
1717
+ if (group.id === groupId) {
1718
+ const newTabs = group.tabs.filter(tab => tab.id !== tabId);
1719
+ let newActiveTabId = group.activeTabId;
1720
+ if (group.activeTabId === tabId) {
1721
+ const tabIndex = group.tabs.findIndex(tab => tab.id === tabId);
1722
+ if (newTabs.length > 0) {
1723
+ newActiveTabId = newTabs[Math.min(tabIndex, newTabs.length - 1)].id;
1724
+ } else {
1725
+ newActiveTabId = undefined;
1726
+ }
1727
+ }
1728
+ return {
1729
+ ...group,
1730
+ activeTabId: newActiveTabId,
1731
+ tabs: newTabs
1732
+ };
1733
+ }
1734
+ return group;
1735
+ });
1736
+ return {
1737
+ ...state,
1738
+ layout: {
1739
+ ...state.layout,
1740
+ groups
1741
+ }
1742
+ };
1743
+ };
1744
+
1745
+ const focusEditorGroup = (state, groupId) => {
1746
+ const groups = state.layout.groups.map(group => ({
1747
+ ...group,
1748
+ focused: group.id === groupId
1749
+ }));
1750
+ return {
1751
+ ...state,
1752
+ layout: {
1753
+ ...state.layout,
1754
+ activeGroupId: groupId,
1755
+ groups
1756
+ }
1757
+ };
1758
+ };
1759
+
1760
+ const CSS_CLASSES = {
1761
+ CUSTOM_EDITOR: 'custom-editor',
1762
+ EDITOR_CONTAINER: 'editor-container',
1763
+ EDITOR_CONTENT: 'editor-content',
1764
+ EDITOR_GROUP: 'editor-group',
1765
+ EDITOR_GROUP_FOCUSED: 'focused',
1766
+ EDITOR_GROUPS_CONTAINER: 'editor-groups-container',
1767
+ EMPTY_EDITOR: 'empty-editor',
1768
+ MAIN_AREA: 'main-area',
1769
+ TAB: 'tab',
1770
+ TAB_ACTIVE: 'active',
1771
+ TAB_BAR: 'tab-bar',
1772
+ TAB_CLOSE: 'tab-close',
1773
+ TAB_TITLE: 'tab-title',
1774
+ TEXT_EDITOR: 'text-editor'
1775
+ };
1776
+ const CSS_ATTRIBUTES = {
1777
+ DATA_ACTION: 'data-action',
1778
+ DATA_CUSTOM_EDITOR_ID: 'data-custom-editor-id',
1779
+ DATA_DIRECTION: 'data-direction',
1780
+ DATA_GROUP_ID: 'data-group-id',
1781
+ DATA_LANGUAGE: 'data-language',
1782
+ DATA_TAB_ID: 'data-tab-id'
1783
+ };
1784
+ const CSS_STYLES = {
1785
+ CUSTOM_EDITOR_STYLE: 'flex: 1; overflow: auto;',
1786
+ EDITOR_GROUP_BASE: 'display: flex; flex-direction: column; border-right: 1px solid var(--border-color);',
1787
+ EDITOR_GROUP_FOCUSED_STYLE: 'box-shadow: 0 0 0 1px var(--focus-border-color);',
1788
+ EMPTY_EDITOR_STYLE: 'flex: 1; display: flex; align-items: center; justify-content: center; color: var(--dimmed-color);',
1789
+ FLEX_COLUMN: 'display: flex; flex-direction: column; height: 100%;',
1790
+ FLEX_ROW: 'display: flex; flex-direction: row; height: 100%;',
1791
+ TAB_ACTIVE_STYLE: 'background: var(--tab-active-background); color: var(--tab-active-color);',
1792
+ TAB_BAR_BASE: 'display: flex; align-items: center; background: var(--tab-bar-background); border-bottom: 1px solid var(--border-color);',
1793
+ TAB_BASE: 'padding: 4px 8px; cursor: pointer; border-right: 1px solid var(--border-color); display: flex; align-items: center; gap: 4px;',
1794
+ TAB_CLOSE_STYLE: 'background: none; border: none; cursor: pointer; padding: 2px; border-radius: 2px; opacity: 0.7;',
1795
+ TEXT_EDITOR_STYLE: 'flex: 1; overflow: auto; font-family: var(--editor-font-family); font-size: var(--editor-font-size);'
1796
+ };
1797
+ const THEMES = {
1798
+ DARK: {
1799
+ '--border-color': '#3e3e42',
1800
+ '--dimmed-color': '#858585',
1801
+ '--editor-font-family': 'Consolas, Monaco, "Courier New", monospace',
1802
+ '--editor-font-size': '14px',
1803
+ '--focus-border-color': '#0078d4',
1804
+ '--tab-active-background': '#1e1e1e',
1805
+ '--tab-active-color': '#ffffff',
1806
+ '--tab-bar-background': '#252526',
1807
+ '--tab-close-hover-background': '#3e3e42'
1808
+ },
1809
+ LIGHT: {
1810
+ '--border-color': '#e1e1e1',
1811
+ '--dimmed-color': '#999999',
1812
+ '--editor-font-family': 'Consolas, Monaco, "Courier New", monospace',
1813
+ '--editor-font-size': '14px',
1814
+ '--focus-border-color': '#0078d4',
1815
+ '--tab-active-background': '#ffffff',
1816
+ '--tab-active-color': '#333333',
1817
+ '--tab-bar-background': '#f3f3f3',
1818
+ '--tab-close-hover-background': '#e1e1e1'
1819
+ }
1820
+ };
1821
+ const getThemeStyles = (theme = 'DARK') => {
1822
+ const themeVars = THEMES[theme];
1823
+ return Object.entries(themeVars).map(([key, value]) => `${key}: ${value};`).join(' ');
1824
+ };
1825
+
1826
+ const renderTab = (tab, isActive) => {
1827
+ return {
1828
+ attributes: {
1829
+ [CSS_ATTRIBUTES.DATA_TAB_ID]: tab.id,
1830
+ style: `${CSS_STYLES.TAB_BASE} ${isActive ? CSS_STYLES.TAB_ACTIVE_STYLE : ''}`
1831
+ },
1832
+ childCount: 2,
1833
+ children: [{
1834
+ childCount: 1,
1835
+ children: [tab.isDirty ? `*${tab.title}` : tab.title],
1836
+ className: CSS_CLASSES.TAB_TITLE,
1837
+ type: Span
1838
+ }, {
1839
+ attributes: {
1840
+ [CSS_ATTRIBUTES.DATA_ACTION]: 'close-tab',
1841
+ [CSS_ATTRIBUTES.DATA_TAB_ID]: tab.id,
1842
+ style: CSS_STYLES.TAB_CLOSE_STYLE
1843
+ },
1844
+ childCount: 1,
1845
+ children: ['×'],
1846
+ className: CSS_CLASSES.TAB_CLOSE,
1847
+ type: Button
1848
+ }],
1849
+ className: `${CSS_CLASSES.TAB} ${isActive ? CSS_CLASSES.TAB_ACTIVE : ''}`,
1850
+ type: Div
1851
+ };
1852
+ };
1853
+ const renderTabBar = group => {
1854
+ return {
1855
+ attributes: {
1856
+ [CSS_ATTRIBUTES.DATA_GROUP_ID]: group.id,
1857
+ style: CSS_STYLES.TAB_BAR_BASE
1858
+ },
1859
+ childCount: group.tabs.length,
1860
+ children: group.tabs.map(tab => renderTab(tab, tab.id === group.activeTabId)),
1861
+ className: CSS_CLASSES.TAB_BAR,
1862
+ type: Div
1863
+ };
1864
+ };
1865
+ const renderEditor = tab => {
1866
+ if (tab.editorType === 'custom') {
1867
+ return {
1868
+ attributes: {
1869
+ [CSS_ATTRIBUTES.DATA_CUSTOM_EDITOR_ID]: tab.customEditorId,
1870
+ [CSS_ATTRIBUTES.DATA_TAB_ID]: tab.id,
1871
+ style: CSS_STYLES.CUSTOM_EDITOR_STYLE
1872
+ },
1873
+ childCount: 1,
1874
+ children: [`Custom Editor: ${tab.customEditorId}`],
1875
+ className: CSS_CLASSES.CUSTOM_EDITOR,
1876
+ type: Div
1877
+ };
1878
+ }
1879
+ return {
1880
+ attributes: {
1881
+ [CSS_ATTRIBUTES.DATA_LANGUAGE]: tab.language || 'plaintext',
1882
+ [CSS_ATTRIBUTES.DATA_TAB_ID]: tab.id,
1883
+ style: CSS_STYLES.TEXT_EDITOR_STYLE
1884
+ },
1885
+ childCount: 1,
1886
+ children: [{
1887
+ childCount: 1,
1888
+ children: [tab.content || ''],
1889
+ className: CSS_CLASSES.EDITOR_CONTENT,
1890
+ type: Pre
1891
+ }],
1892
+ className: CSS_CLASSES.TEXT_EDITOR,
1893
+ type: Div
1894
+ };
1895
+ };
1896
+ const renderEditorGroup = group => {
1897
+ const activeTab = group.tabs.find(tab => tab.id === group.activeTabId);
1898
+ return {
1899
+ attributes: {
1900
+ [CSS_ATTRIBUTES.DATA_GROUP_ID]: group.id,
1901
+ style: `${CSS_STYLES.EDITOR_GROUP_BASE} flex: ${group.size}; ${group.focused ? CSS_STYLES.EDITOR_GROUP_FOCUSED_STYLE : ''}`
1902
+ },
1903
+ childCount: 2,
1904
+ children: [renderTabBar(group), {
1905
+ childCount: activeTab ? 1 : 1,
1906
+ children: activeTab ? [renderEditor(activeTab)] : [{
1907
+ attributes: {
1908
+ style: CSS_STYLES.EMPTY_EDITOR_STYLE
1909
+ },
1910
+ childCount: 1,
1911
+ children: ['No open tabs'],
1912
+ className: CSS_CLASSES.EMPTY_EDITOR,
1913
+ type: Div
1914
+ }],
1915
+ className: CSS_CLASSES.EDITOR_CONTAINER,
1916
+ type: Div
1917
+ }],
1918
+ className: `${CSS_CLASSES.EDITOR_GROUP} ${group.focused ? CSS_CLASSES.EDITOR_GROUP_FOCUSED : ''}`,
1919
+ type: Div
1920
+ };
1921
+ };
1922
+ const getMainAreaVirtualDom = state => {
1923
+ return [{
1924
+ attributes: {
1925
+ [CSS_ATTRIBUTES.DATA_DIRECTION]: state.layout.direction,
1926
+ style: getThemeStyles('DARK')
1927
+ },
1928
+ childCount: 1,
1929
+ children: [{
1930
+ attributes: {
1931
+ style: state.layout.direction === 'horizontal' ? CSS_STYLES.FLEX_ROW : CSS_STYLES.FLEX_COLUMN
1932
+ },
1933
+ childCount: state.layout.groups.length,
1934
+ children: state.layout.groups.map(renderEditorGroup),
1935
+ className: CSS_CLASSES.EDITOR_GROUPS_CONTAINER,
1936
+ type: Div
1937
+ }],
1938
+ className: CSS_CLASSES.MAIN_AREA,
1939
+ type: Div
1940
+ }];
1941
+ };
1942
+
1943
+ const splitEditorGroup = (state, groupId, direction) => {
1944
+ const sourceGroup = state.layout.groups.find(group => group.id === groupId);
1945
+ if (!sourceGroup) {
1946
+ return state;
1947
+ }
1948
+ const newGroupId = `group-${Date.now()}-${Math.random().toString(36).slice(2, 11)}`;
1949
+ const isHorizontalSplit = direction === 'left' || direction === 'right';
1950
+ const newLayoutDirection = isHorizontalSplit ? 'horizontal' : 'vertical';
1951
+ const updatedGroups = state.layout.groups.map(group => {
1952
+ if (group.id === groupId) {
1953
+ return {
1954
+ ...group,
1955
+ focused: false,
1956
+ size: 50
1957
+ };
1958
+ }
1959
+ return group;
1960
+ });
1961
+ const newGroup = {
1962
+ activeTabId: undefined,
1963
+ focused: true,
1964
+ id: newGroupId,
1965
+ size: 50,
1966
+ tabs: []
1967
+ };
1968
+ let reorderedGroups;
1969
+ if (direction === 'right' || direction === 'down') {
1970
+ reorderedGroups = [...updatedGroups, newGroup];
1971
+ } else {
1972
+ const sourceIndex = updatedGroups.findIndex(group => group.id === groupId);
1973
+ reorderedGroups = [...updatedGroups.slice(0, sourceIndex), newGroup, ...updatedGroups.slice(sourceIndex)];
1974
+ }
1975
+ return {
1976
+ ...state,
1977
+ layout: {
1978
+ activeGroupId: newGroupId,
1979
+ direction: newLayoutDirection,
1980
+ groups: reorderedGroups
1981
+ }
1982
+ };
1983
+ };
1984
+
1985
+ const switchTab = (state, groupId, tabId) => {
1986
+ const groups = state.layout.groups.map(group => {
1987
+ if (group.id === groupId) {
1988
+ const tabExists = group.tabs.some(tab => tab.id === tabId);
1989
+ if (tabExists) {
1990
+ return {
1991
+ ...group,
1992
+ activeTabId: tabId
1993
+ };
1994
+ }
1995
+ }
1996
+ return group;
1997
+ });
1998
+ return {
1999
+ ...state,
2000
+ layout: {
2001
+ ...state.layout,
2002
+ groups
2003
+ }
2004
+ };
2005
+ };
2006
+
2007
+ const handleMainAreaClick = (state, event) => {
2008
+ const {
2009
+ target
2010
+ } = event;
2011
+ if (!target.dataset) {
2012
+ return state;
2013
+ }
2014
+ const {
2015
+ dataset
2016
+ } = target;
2017
+
2018
+ // Handle tab click
2019
+ if (dataset.tabId) {
2020
+ const {
2021
+ groupId
2022
+ } = dataset;
2023
+ if (groupId) {
2024
+ return switchTab(state, groupId, dataset.tabId);
2025
+ }
2026
+ }
2027
+
2028
+ // Handle tab close button
2029
+ if (dataset.action === 'close-tab' && dataset.tabId) {
2030
+ const {
2031
+ groupId
2032
+ } = dataset;
2033
+ if (groupId) {
2034
+ return closeTab(state, groupId, dataset.tabId);
2035
+ }
2036
+ }
2037
+
2038
+ // Handle editor group focus
2039
+ if (dataset.groupId && !dataset.tabId) {
2040
+ return focusEditorGroup(state, dataset.groupId);
2041
+ }
2042
+
2043
+ // Handle split actions
2044
+ if (dataset.action?.startsWith('split-')) {
2045
+ const {
2046
+ groupId
2047
+ } = dataset;
2048
+ if (groupId) {
2049
+ const direction = dataset.action.replace('split-', '');
2050
+ return splitEditorGroup(state, groupId, direction);
2051
+ }
2052
+ }
2053
+ return state;
2054
+ };
2055
+
2056
+ const handleMainAreaKeyboard = (state, event) => {
2057
+ const {
2058
+ ctrlKey = false,
2059
+ key,
2060
+ metaKey = false,
2061
+ shiftKey = false
2062
+ } = event;
2063
+ const isCtrl = ctrlKey || metaKey;
2064
+ const activeGroup = state.layout.groups.find(group => group.focused);
2065
+ if (!activeGroup) {
2066
+ return state;
2067
+ }
2068
+
2069
+ // Tab navigation
2070
+ if (key === 'Tab' && isCtrl) {
2071
+ const groupIndex = state.layout.groups.findIndex(group => group.id === activeGroup.id);
2072
+ const nextGroupIndex = shiftKey ? (groupIndex - 1 + state.layout.groups.length) % state.layout.groups.length : (groupIndex + 1) % state.layout.groups.length;
2073
+ const nextGroup = state.layout.groups[nextGroupIndex];
2074
+ return focusEditorGroup(state, nextGroup.id);
2075
+ }
2076
+
2077
+ // Switch between tabs within group
2078
+ if (key === 'ArrowLeft' && isCtrl && !shiftKey) {
2079
+ const activeTabIndex = activeGroup.tabs.findIndex(tab => tab.id === activeGroup.activeTabId);
2080
+ if (activeTabIndex > 0) {
2081
+ const prevTab = activeGroup.tabs[activeTabIndex - 1];
2082
+ return switchTab(state, activeGroup.id, prevTab.id);
2083
+ }
2084
+ }
2085
+ if (key === 'ArrowRight' && isCtrl && !shiftKey) {
2086
+ const activeTabIndex = activeGroup.tabs.findIndex(tab => tab.id === activeGroup.activeTabId);
2087
+ if (activeTabIndex < activeGroup.tabs.length - 1) {
2088
+ const nextTab = activeGroup.tabs[activeTabIndex + 1];
2089
+ return switchTab(state, activeGroup.id, nextTab.id);
2090
+ }
2091
+ }
2092
+
2093
+ // Close current tab
2094
+ if (key === 'w' && isCtrl && activeGroup.activeTabId) {
2095
+ return closeTab(state, activeGroup.id, activeGroup.activeTabId);
2096
+ }
2097
+
2098
+ // Split editor
2099
+ if (key === '\\' && isCtrl) {
2100
+ const direction = shiftKey ? 'down' : 'right';
2101
+ return splitEditorGroup(state, activeGroup.id, direction);
2102
+ }
2103
+ return state;
2104
+ };
2105
+
2106
+ const moveTabToGroup = (state, sourceGroupId, targetGroupId, tabId, targetIndex) => {
2107
+ const sourceGroup = state.layout.groups.find(group => group.id === sourceGroupId);
2108
+ const targetGroup = state.layout.groups.find(group => group.id === targetGroupId);
2109
+ if (!sourceGroup || !targetGroup || sourceGroupId === targetGroupId) {
2110
+ return state;
2111
+ }
2112
+ const tabToMove = sourceGroup.tabs.find(tab => tab.id === tabId);
2113
+ if (!tabToMove) {
2114
+ return state;
2115
+ }
2116
+ const updatedGroups = state.layout.groups.map(group => {
2117
+ if (group.id === sourceGroupId) {
2118
+ const newTabs = group.tabs.filter(tab => tab.id !== tabId);
2119
+ let newActiveTabId = group.activeTabId;
2120
+ if (group.activeTabId === tabId) {
2121
+ if (newTabs.length > 0) {
2122
+ const removedIndex = group.tabs.findIndex(tab => tab.id === tabId);
2123
+ newActiveTabId = newTabs[Math.min(removedIndex, newTabs.length - 1)].id;
2124
+ } else {
2125
+ newActiveTabId = undefined;
2126
+ }
2127
+ }
2128
+ return {
2129
+ ...group,
2130
+ activeTabId: newActiveTabId,
2131
+ tabs: newTabs
2132
+ };
2133
+ }
2134
+ if (group.id === targetGroupId) {
2135
+ const insertIndex = targetIndex === undefined ? group.tabs.length : targetIndex;
2136
+ const newTabs = [...group.tabs];
2137
+ newTabs.splice(insertIndex, 0, tabToMove);
2138
+ return {
2139
+ ...group,
2140
+ activeTabId: tabId,
2141
+ tabs: newTabs
2142
+ };
2143
+ }
2144
+ return group;
2145
+ });
2146
+ return {
2147
+ ...state,
2148
+ layout: {
2149
+ ...state.layout,
2150
+ activeGroupId: targetGroupId,
2151
+ groups: updatedGroups
2152
+ }
2153
+ };
2154
+ };
2155
+
2156
+ const startTabDrag = (state, tabId, groupId) => {
2157
+ return {
2158
+ dragState: {
2159
+ draggedTabId: tabId,
2160
+ sourceGroupId: groupId
2161
+ },
2162
+ state
2163
+ };
2164
+ };
2165
+ const updateTabDrag = (state, dragState, targetGroupId, targetIndex) => {
2166
+ return {
2167
+ ...dragState,
2168
+ targetGroupId,
2169
+ targetIndex
2170
+ };
2171
+ };
2172
+ const endTabDrag = (state, dragState) => {
2173
+ if (dragState.targetGroupId && dragState.targetGroupId !== dragState.sourceGroupId) {
2174
+ return moveTabToGroup(state, dragState.sourceGroupId, dragState.targetGroupId, dragState.draggedTabId, dragState.targetIndex);
2175
+ }
2176
+ return state;
2177
+ };
2178
+
2179
+ const {
2180
+ getCommandIds,
2181
+ registerCommands,
2182
+ wrapCommand} = create$1();
2183
+
2184
+ const openTab = (state, groupId, tab) => {
2185
+ const newTab = {
2186
+ ...tab,
2187
+ id: `tab-${Date.now()}-${Math.random().toString(36).slice(2, 11)}`
2188
+ };
2189
+ const groups = state.layout.groups.map(group => {
2190
+ if (group.id === groupId) {
2191
+ return {
2192
+ ...group,
2193
+ activeTabId: newTab.id,
2194
+ tabs: [...group.tabs, newTab]
2195
+ };
2196
+ }
2197
+ return group;
2198
+ });
2199
+ return {
2200
+ ...state,
2201
+ layout: {
2202
+ ...state.layout,
2203
+ groups
2204
+ }
2205
+ };
2206
+ };
2207
+
2208
+ const restoreMainAreaState = (savedState, currentState) => {
2209
+ try {
2210
+ const parsed = JSON.parse(savedState);
2211
+ return {
2212
+ ...currentState,
2213
+ layout: parsed.layout
2214
+ };
2215
+ } catch (error) {
2216
+ console.error('Failed to restore main area state:', error);
2217
+ return currentState;
2218
+ }
2219
+ };
2220
+
2221
+ const saveMainAreaState = state => {
2222
+ return JSON.stringify({
2223
+ layout: state.layout,
2224
+ version: '1.0.0'
2225
+ });
2226
+ };
2227
+
2228
+ const mainAreaCommandMap = {
2229
+ 'MainArea.closeEditorGroup': wrapCommand((state, groupId) => closeEditorGroup(state, groupId)),
2230
+ 'MainArea.closeTab': wrapCommand((state, groupId, tabId) => closeTab(state, groupId, tabId)),
2231
+ 'MainArea.create': () => {},
2232
+ 'MainArea.endTabDrag': wrapCommand((state, dragState) => endTabDrag(state, dragState)),
2233
+ 'MainArea.focusEditorGroup': wrapCommand((state, groupId) => focusEditorGroup(state, groupId)),
2234
+ 'MainArea.getCommandIds': getCommandIds,
2235
+ 'MainArea.getVirtualDom': state => getMainAreaVirtualDom(state),
2236
+ 'MainArea.handleClick': wrapCommand((state, event) => handleMainAreaClick(state, event)),
2237
+ 'MainArea.handleKeyboard': wrapCommand((state, event) => handleMainAreaKeyboard(state, event)),
2238
+ 'MainArea.moveTabToGroup': wrapCommand((state, sourceGroupId, targetGroupId, tabId, targetIndex) => moveTabToGroup(state, sourceGroupId, targetGroupId, tabId, targetIndex)),
2239
+ 'MainArea.openTab': wrapCommand((state, groupId, tab) => openTab(state, groupId, tab)),
2240
+ 'MainArea.restoreState': wrapCommand((state, savedState) => restoreMainAreaState(savedState, state)),
2241
+ 'MainArea.saveState': state => saveMainAreaState(state),
2242
+ 'MainArea.splitEditorGroup': wrapCommand((state, groupId, direction) => splitEditorGroup(state, groupId, direction)),
2243
+ 'MainArea.startTabDrag': (state, tabId, groupId) => startTabDrag(state, tabId, groupId),
2244
+ 'MainArea.switchTab': wrapCommand((state, groupId, tabId) => switchTab(state, groupId, tabId)),
2245
+ 'MainArea.terminate': terminate,
2246
+ 'MainArea.updateTabDrag': (state, dragState, targetGroupId, targetIndex) => updateTabDrag(state, dragState, targetGroupId, targetIndex)
2247
+ };
2248
+
2249
+ const main$1 = async () => {
2250
+ registerCommands(mainAreaCommandMap);
2251
+ const rpc = await WebWorkerRpcClient.create({
2252
+ commandMap: mainAreaCommandMap
2253
+ });
2254
+ set$1(rpc);
2255
+ };
2256
+
2257
+ const main = async () => {
2258
+ await main$2();
2259
+ await main$1();
2260
+ };
2261
+ main$2();
2262
+
2263
+ export { main };