mindoodb-app-sdk 0.0.16 → 0.0.18

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -142,6 +142,7 @@ var PortRpcClient = class {
142
142
  this.port.addEventListener("message", this.handleMessage);
143
143
  this.port.start();
144
144
  }
145
+ port;
145
146
  pending = /* @__PURE__ */ new Map();
146
147
  listeners = /* @__PURE__ */ new Set();
147
148
  requestCounter = 0;
@@ -232,7 +233,10 @@ var PortRpcClient = class {
232
233
  var PROTOCOL2 = "mindoodb-app-bridge";
233
234
  var DEFAULT_CONNECT_TIMEOUT_MS = 1e4;
234
235
  function toTransferableArrayBuffer(chunk) {
235
- return chunk.buffer.slice(chunk.byteOffset, chunk.byteOffset + chunk.byteLength);
236
+ return chunk.buffer.slice(
237
+ chunk.byteOffset,
238
+ chunk.byteOffset + chunk.byteLength
239
+ );
236
240
  }
237
241
  function isStreamChunkFor(streamId, message) {
238
242
  return message.kind === "stream-chunk" && message.streamId === streamId;
@@ -262,7 +266,9 @@ function resolveLaunchId(options) {
262
266
  return options.launchId;
263
267
  }
264
268
  if (typeof window === "undefined") {
265
- throw new Error("A launchId must be provided when connecting outside the browser.");
269
+ throw new Error(
270
+ "A launchId must be provided when connecting outside the browser."
271
+ );
266
272
  }
267
273
  const search = new URLSearchParams(window.location.search);
268
274
  const launchId = search.get("mindoodbAppLaunchId");
@@ -271,9 +277,17 @@ function resolveLaunchId(options) {
271
277
  }
272
278
  return launchId;
273
279
  }
280
+ function shouldUseWindowedHostTab() {
281
+ if (typeof window === "undefined") {
282
+ return false;
283
+ }
284
+ return window.parent === window && Boolean(window.opener);
285
+ }
274
286
  function resolveTargetWindow() {
275
287
  if (typeof window === "undefined") {
276
- throw new Error("The MindooDB app bridge is only available in the browser.");
288
+ throw new Error(
289
+ "The MindooDB app bridge is only available in the browser."
290
+ );
277
291
  }
278
292
  if (window.parent && window.parent !== window) {
279
293
  return window.parent;
@@ -294,7 +308,10 @@ function waitForConnectedPort(options, launchId) {
294
308
  if (timeoutId !== null) {
295
309
  window.clearTimeout(timeoutId);
296
310
  }
297
- channel.port1.removeEventListener("message", handleMessage);
311
+ channel.port1.removeEventListener(
312
+ "message",
313
+ handleMessage
314
+ );
298
315
  };
299
316
  const handleMessage = (event) => {
300
317
  const payload = event.data;
@@ -316,7 +333,11 @@ function waitForConnectedPort(options, launchId) {
316
333
  timeoutId = window.setTimeout(() => {
317
334
  cleanup();
318
335
  channel.port1.close();
319
- reject(new Error(`Timed out while connecting to the MindooDB Haven bridge for launch ${launchId}.`));
336
+ reject(
337
+ new Error(
338
+ `Timed out while connecting to the MindooDB Haven bridge for launch ${launchId}.`
339
+ )
340
+ );
320
341
  }, timeoutMs);
321
342
  channel.port1.addEventListener("message", handleMessage);
322
343
  channel.port1.start();
@@ -334,6 +355,9 @@ var MindooDBAppViewNavigatorImpl = class {
334
355
  this.navigatorId = navigatorId;
335
356
  this.viewCursor = viewCursor;
336
357
  }
358
+ rpc;
359
+ navigatorId;
360
+ viewCursor;
337
361
  /** Shorthand: call an RPC method with the navigator ID automatically injected. */
338
362
  async call(method, params = {}) {
339
363
  return await this.rpc.call(method, {
@@ -348,7 +372,9 @@ var MindooDBAppViewNavigatorImpl = class {
348
372
  return this.viewCursor;
349
373
  }
350
374
  async refresh() {
351
- const result = await this.call("viewNavigators.refresh");
375
+ const result = await this.call(
376
+ "viewNavigators.refresh"
377
+ );
352
378
  this.viewCursor = result.viewCursor;
353
379
  return this.viewCursor;
354
380
  }
@@ -392,10 +418,14 @@ var MindooDBAppViewNavigatorImpl = class {
392
418
  return await this.call("viewNavigators.category.findByParts", { parts });
393
419
  }
394
420
  async entriesForward(options) {
395
- return await this.call("viewNavigators.entries.forward", { options: options ?? {} });
421
+ return await this.call("viewNavigators.entries.forward", {
422
+ options: options ?? {}
423
+ });
396
424
  }
397
425
  async entriesBackward(options) {
398
- return await this.call("viewNavigators.entries.backward", { options: options ?? {} });
426
+ return await this.call("viewNavigators.entries.backward", {
427
+ options: options ?? {}
428
+ });
399
429
  }
400
430
  async gotoNextSelected() {
401
431
  return await this.call("viewNavigators.goto.nextSelected");
@@ -420,7 +450,10 @@ var MindooDBAppViewNavigatorImpl = class {
420
450
  await this.call("viewNavigators.selection.deselectAll");
421
451
  }
422
452
  async isSelected(origin, docId) {
423
- return await this.call("viewNavigators.selection.isSelected", { origin, docId });
453
+ return await this.call("viewNavigators.selection.isSelected", {
454
+ origin,
455
+ docId
456
+ });
424
457
  }
425
458
  async getSelectionState() {
426
459
  return await this.call("viewNavigators.selection.get");
@@ -453,13 +486,22 @@ var MindooDBAppViewNavigatorImpl = class {
453
486
  await this.call("viewNavigators.expansion.set", { state });
454
487
  }
455
488
  async childEntries(entryKey, descending) {
456
- return await this.call("viewNavigators.children.entries", { entryKey, descending });
489
+ return await this.call("viewNavigators.children.entries", {
490
+ entryKey,
491
+ descending
492
+ });
457
493
  }
458
494
  async childCategories(entryKey, descending) {
459
- return await this.call("viewNavigators.children.categories", { entryKey, descending });
495
+ return await this.call("viewNavigators.children.categories", {
496
+ entryKey,
497
+ descending
498
+ });
460
499
  }
461
500
  async childDocuments(entryKey, descending) {
462
- return await this.call("viewNavigators.children.documents", { entryKey, descending });
501
+ return await this.call("viewNavigators.children.documents", {
502
+ entryKey,
503
+ descending
504
+ });
463
505
  }
464
506
  async childCategoriesByKey(entryKey, key, exact, descending) {
465
507
  return await this.call("viewNavigators.children.categoriesByKey", {
@@ -478,16 +520,28 @@ var MindooDBAppViewNavigatorImpl = class {
478
520
  });
479
521
  }
480
522
  async childCategoriesBetween(entryKey, range) {
481
- return await this.call("viewNavigators.children.categoriesBetween", { entryKey, range });
523
+ return await this.call("viewNavigators.children.categoriesBetween", {
524
+ entryKey,
525
+ range
526
+ });
482
527
  }
483
528
  async childDocumentsBetween(entryKey, range) {
484
- return await this.call("viewNavigators.children.documentsBetween", { entryKey, range });
529
+ return await this.call("viewNavigators.children.documentsBetween", {
530
+ entryKey,
531
+ range
532
+ });
485
533
  }
486
534
  async getSortedDocIds(descending) {
487
- return await this.call("viewNavigators.sortedDocIds.get", { descending });
535
+ return await this.call(
536
+ "viewNavigators.sortedDocIds.get",
537
+ { descending }
538
+ );
488
539
  }
489
540
  async getSortedDocIdsScoped(entryKey, descending) {
490
- return await this.call("viewNavigators.sortedDocIds.scoped", { entryKey, descending });
541
+ return await this.call(
542
+ "viewNavigators.sortedDocIds.scoped",
543
+ { entryKey, descending }
544
+ );
491
545
  }
492
546
  async dispose() {
493
547
  await this.call("viewNavigators.dispose");
@@ -504,7 +558,9 @@ var MindooDBAppReadableAttachmentStreamImpl = class {
504
558
  if (!pending) {
505
559
  return;
506
560
  }
507
- pending.resolve(message.done ? null : new Uint8Array(message.chunk ?? new ArrayBuffer(0)));
561
+ pending.resolve(
562
+ message.done ? null : new Uint8Array(message.chunk ?? new ArrayBuffer(0))
563
+ );
508
564
  if (message.done) {
509
565
  this.closed = true;
510
566
  this.unsubscribe();
@@ -520,6 +576,8 @@ var MindooDBAppReadableAttachmentStreamImpl = class {
520
576
  }
521
577
  });
522
578
  }
579
+ rpc;
580
+ streamId;
523
581
  unsubscribe;
524
582
  closed = false;
525
583
  pendingRead = null;
@@ -529,7 +587,9 @@ var MindooDBAppReadableAttachmentStreamImpl = class {
529
587
  return null;
530
588
  }
531
589
  if (this.pendingRead) {
532
- throw new Error("A read() call is already in progress for this attachment stream.");
590
+ throw new Error(
591
+ "A read() call is already in progress for this attachment stream."
592
+ );
533
593
  }
534
594
  return await new Promise((resolve, reject) => {
535
595
  this.pendingRead = { resolve, reject };
@@ -576,6 +636,8 @@ var MindooDBAppWritableAttachmentStreamImpl = class {
576
636
  }
577
637
  });
578
638
  }
639
+ rpc;
640
+ streamId;
579
641
  unsubscribe;
580
642
  closed = false;
581
643
  inflight = null;
@@ -601,12 +663,15 @@ var MindooDBAppWritableAttachmentStreamImpl = class {
601
663
  return await this.enqueue(async () => {
602
664
  const buffer = toTransferableArrayBuffer(chunk);
603
665
  await this.waitForAck(() => {
604
- this.rpc.postMessage({
605
- protocol: PROTOCOL2,
606
- kind: "stream-write",
607
- streamId: this.streamId,
608
- chunk: buffer
609
- }, [buffer]);
666
+ this.rpc.postMessage(
667
+ {
668
+ protocol: PROTOCOL2,
669
+ kind: "stream-write",
670
+ streamId: this.streamId,
671
+ chunk: buffer
672
+ },
673
+ [buffer]
674
+ );
610
675
  });
611
676
  });
612
677
  }
@@ -706,23 +771,71 @@ var MindooDBAppDatabaseImpl = class {
706
771
  attachmentName
707
772
  }),
708
773
  openReadStream: async (docId, attachmentName, options) => {
709
- const result = await this.rpc.call("attachments.openReadStream", {
710
- databaseId: this.databaseId,
711
- docId,
712
- attachmentName,
713
- timestamp: options?.timestamp,
714
- revisionId: options?.revisionId
715
- });
716
- return new MindooDBAppReadableAttachmentStreamImpl(this.rpc, result.streamId);
774
+ const result = await this.rpc.call(
775
+ "attachments.openReadStream",
776
+ {
777
+ databaseId: this.databaseId,
778
+ docId,
779
+ attachmentName,
780
+ timestamp: options?.timestamp,
781
+ revisionId: options?.revisionId
782
+ }
783
+ );
784
+ return new MindooDBAppReadableAttachmentStreamImpl(
785
+ this.rpc,
786
+ result.streamId
787
+ );
717
788
  },
718
789
  openWriteStream: async (docId, attachmentName, contentType) => {
719
- const result = await this.rpc.call("attachments.openWriteStream", {
720
- databaseId: this.databaseId,
721
- docId,
722
- attachmentName,
723
- contentType
724
- });
725
- return new MindooDBAppWritableAttachmentStreamImpl(this.rpc, result.streamId);
790
+ const result = await this.rpc.call(
791
+ "attachments.openWriteStream",
792
+ {
793
+ databaseId: this.databaseId,
794
+ docId,
795
+ attachmentName,
796
+ contentType
797
+ }
798
+ );
799
+ return new MindooDBAppWritableAttachmentStreamImpl(
800
+ this.rpc,
801
+ result.streamId
802
+ );
803
+ },
804
+ scan: async (docId, options) => {
805
+ const scannerTab = shouldUseWindowedHostTab() ? window.open("", "_blank") : null;
806
+ if (scannerTab) {
807
+ try {
808
+ scannerTab.document.title = "Opening document scanner...";
809
+ const preparedSession = await this.rpc.call("attachments.prepareScanSession", {
810
+ databaseId: this.databaseId,
811
+ docId,
812
+ defaultFileName: options?.defaultFileName,
813
+ preset: options?.preset,
814
+ mimeType: options?.mimeType
815
+ });
816
+ scannerTab.location.href = preparedSession.scanUrl;
817
+ return await this.rpc.call(
818
+ "attachments.awaitScanSession",
819
+ {
820
+ databaseId: this.databaseId,
821
+ sessionId: preparedSession.sessionId
822
+ }
823
+ );
824
+ } catch (error) {
825
+ scannerTab.close();
826
+ throw error;
827
+ }
828
+ }
829
+ return await this.rpc.call(
830
+ "attachments.scanAndWrite",
831
+ {
832
+ databaseId: this.databaseId,
833
+ docId,
834
+ defaultFileName: options?.defaultFileName,
835
+ preset: options?.preset,
836
+ mimeType: options?.mimeType
837
+ }
838
+ );
726
839
  },
727
840
  preparePreviewSession: async (docId, attachmentName, options) => await this.rpc.call("attachments.preparePreviewSession", {
728
841
  databaseId: this.databaseId,
@@ -731,15 +844,37 @@ var MindooDBAppDatabaseImpl = class {
731
844
  timestamp: options?.timestamp,
732
845
  revisionId: options?.revisionId
733
846
  }),
734
- openPreview: async (docId, attachmentName, options) => await this.rpc.call("attachments.openPreview", {
735
- databaseId: this.databaseId,
736
- docId,
737
- attachmentName,
738
- timestamp: options?.timestamp,
739
- revisionId: options?.revisionId
740
- })
847
+ openPreview: async (docId, attachmentName, options) => {
848
+ const previewTab = shouldUseWindowedHostTab() ? window.open("", "_blank") : null;
849
+ if (previewTab) {
850
+ try {
851
+ previewTab.document.title = "Opening attachment preview...";
852
+ const previewSession = await this.rpc.call("attachments.preparePreviewSession", {
853
+ databaseId: this.databaseId,
854
+ docId,
855
+ attachmentName,
856
+ timestamp: options?.timestamp,
857
+ revisionId: options?.revisionId
858
+ });
859
+ previewTab.location.href = previewSession.previewUrl;
860
+ return { ok: true };
861
+ } catch (error) {
862
+ previewTab.close();
863
+ throw error;
864
+ }
865
+ }
866
+ return await this.rpc.call("attachments.openPreview", {
867
+ databaseId: this.databaseId,
868
+ docId,
869
+ attachmentName,
870
+ timestamp: options?.timestamp,
871
+ revisionId: options?.revisionId
872
+ });
873
+ }
741
874
  };
742
875
  }
876
+ rpc;
877
+ databaseId;
743
878
  documents;
744
879
  attachments;
745
880
  /** Returns database metadata for the currently opened binding. */
@@ -759,6 +894,7 @@ var MindooDBAppSessionImpl = class {
759
894
  }
760
895
  };
761
896
  }
897
+ rpc;
762
898
  menus;
763
899
  /** Returns the launch context provided by the Haven host. */
764
900
  async getLaunchContext() {
@@ -774,7 +910,9 @@ var MindooDBAppSessionImpl = class {
774
910
  return new MindooDBAppDatabaseImpl(this.rpc, databaseId);
775
911
  }
776
912
  async listDocumentsSinceViewCursor(cursor) {
777
- return await this.rpc.call("session.listDocumentsSinceViewCursor", { cursor });
913
+ return await this.rpc.call("session.listDocumentsSinceViewCursor", {
914
+ cursor
915
+ });
778
916
  }
779
917
  /**
780
918
  * Create a navigator from an ad-hoc view definition and one or more database bindings.
@@ -785,7 +923,11 @@ var MindooDBAppSessionImpl = class {
785
923
  */
786
924
  async createViewNavigator(input) {
787
925
  const result = await this.rpc.call("session.createViewNavigator", input);
788
- return new MindooDBAppViewNavigatorImpl(this.rpc, result.navigatorId, result.viewCursor);
926
+ return new MindooDBAppViewNavigatorImpl(
927
+ this.rpc,
928
+ result.navigatorId,
929
+ result.viewCursor
930
+ );
789
931
  }
790
932
  /**
791
933
  * Open a navigator for a preconfigured view that the host already knows about.
@@ -795,7 +937,11 @@ var MindooDBAppSessionImpl = class {
795
937
  */
796
938
  async openViewNavigator(viewId, options) {
797
939
  const result = await this.rpc.call("session.openViewNavigator", { viewId, options: options ?? {} });
798
- return new MindooDBAppViewNavigatorImpl(this.rpc, result.navigatorId, result.viewCursor);
940
+ return new MindooDBAppViewNavigatorImpl(
941
+ this.rpc,
942
+ result.navigatorId,
943
+ result.viewCursor
944
+ );
799
945
  }
800
946
  /** Subscribe to host-pushed theme changes (dark/light mode, color tokens). */
801
947
  onThemeChange(listener) {
@@ -848,6 +994,7 @@ var MindooDBTextBuffer = class {
848
994
  this.valueInternal = readTextAtPath(options.document.data, options.path);
849
995
  this.heads = options.document.heads ? [...options.document.heads] : [];
850
996
  }
997
+ options;
851
998
  valueInternal;
852
999
  heads;
853
1000
  pendingEdits = [];
@@ -1,4 +1,4 @@
1
- import { a6 as MindooDBAppLaunchContext, X as MindooDBAppDatabaseInfo, Y as MindooDBAppDocumentApi, W as MindooDBAppCreateViewNavigatorInput, aC as MindooDBAppViewNavigator, aE as MindooDBAppViewNavigatorOpenOptions, d as MindooDBAppAttachmentApi, a as MindooDBAppBridge, aq as MindooDBAppSession, q as MindooDBAppBridgeRpcRequest, a5 as MindooDBAppHostTheme, aJ as MindooDBAppViewport, aw as MindooDBAppUiPreferences, n as MindooDBAppBridgePortMessage } from '../types-D0vtIHBf.js';
1
+ import { ac as MindooDBAppLaunchContext, X as MindooDBAppDatabaseInfo, Y as MindooDBAppDocumentApi, W as MindooDBAppCreateViewNavigatorInput, aK as MindooDBAppViewNavigator, aM as MindooDBAppViewNavigatorOpenOptions, d as MindooDBAppAttachmentApi, a as MindooDBAppBridge, ay as MindooDBAppSession, q as MindooDBAppBridgeRpcRequest, a6 as MindooDBAppHostTheme, aR as MindooDBAppViewport, aE as MindooDBAppUiPreferences, n as MindooDBAppBridgePortMessage } from '../types-jzokkBX5.js';
2
2
  import 'mindoodb-view-language';
3
3
 
4
4
  type MaybePromise<T> = T | Promise<T>;