@vscode/web-editors 0.0.2-141 → 0.0.2-143

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.d.ts CHANGED
@@ -2212,7 +2212,11 @@ type HostCapabilities = output<typeof zHostCapabilities>;
2212
2212
  type WebEditorUpdate = output<typeof zWebEditorUpdate>;
2213
2213
  type PresentationMode = output<typeof zPresentationMode>;
2214
2214
  type PresentationPreferences = output<typeof zPresentationPreferences>;
2215
- type ContentType = "text" | "json";
2215
+ declare const zContentType: ZodEnum<{
2216
+ text: "text";
2217
+ json: "json";
2218
+ }>;
2219
+ type ContentType = output<typeof zContentType>;
2216
2220
  /**
2217
2221
  * Identifies a state of the content that the host and the editor share: the
2218
2222
  * host's and the editor's edits integrated into it. Send it along with
@@ -2344,11 +2348,16 @@ interface WebEditorHostOptions {
2344
2348
  connection: HubRpcConnection;
2345
2349
  /**
2346
2350
  * What the host stores. `text` is plain text; `json` is round-tripped through
2347
- * `JSON.parse` / `JSON.stringify`. Default `text`.
2348
- *
2349
- * The editor announces its own `contentType` in `initialized`. They must match.
2351
+ * `JSON.parse` / `JSON.stringify`. Default `text`. Shorthand for a single
2352
+ * entry in {@link contentTypes}.
2350
2353
  */
2351
2354
  contentType?: ContentType;
2355
+ /**
2356
+ * The content types the host can serve the text as, its preference first.
2357
+ * Overrides {@link contentType}. The editor picks one in `initialized`:
2358
+ * the first of its accepted types that is in this list.
2359
+ */
2360
+ contentTypes?: readonly ContentType[];
2352
2361
  /** Initial text. Default `""`. */
2353
2362
  initialText?: string;
2354
2363
  /** Initial read-only state. Default `false`. */
@@ -2384,7 +2393,9 @@ declare class WebEditorHost implements IDisposable {
2384
2393
  private readonly _editor;
2385
2394
  private readonly _clientTransport;
2386
2395
  private readonly _presentation;
2387
- private readonly _contentType;
2396
+ private readonly _contentTypes;
2397
+ /** The type agreed with the editor; the host's preference until then. */
2398
+ private _contentType;
2388
2399
  private readonly _jsonFormat;
2389
2400
  private readonly _onError;
2390
2401
  private readonly _capabilities;
@@ -2424,6 +2435,11 @@ declare class WebEditorHost implements IDisposable {
2424
2435
  constructor(options: WebEditorHostOptions);
2425
2436
  /** The text the host believes the document currently holds. */
2426
2437
  getText(): string;
2438
+ /**
2439
+ * The content type agreed with the editor, in which edits are exchanged.
2440
+ * `undefined` until the editor initialized.
2441
+ */
2442
+ get contentType(): ContentType | undefined;
2427
2443
  /** The current content's version, to send along with offsets into it. */
2428
2444
  get version(): ContentVersion;
2429
2445
  /**
@@ -2556,8 +2572,13 @@ interface WebEditorClientOptions {
2556
2572
  * closes it on dispose.
2557
2573
  */
2558
2574
  connection: ConnectionInput;
2559
- /** Editor's content type. Must match what the host expects. Default `text`. */
2575
+ /** The content type the editor accepts. Default `text`. Shorthand for a single entry in {@link contentTypes}. */
2560
2576
  contentType?: ContentType;
2577
+ /**
2578
+ * The content types the editor accepts, most preferred first. Overrides
2579
+ * {@link contentType}. The host picks one; see {@link WebEditorClient.contentType}.
2580
+ */
2581
+ contentTypes?: readonly ContentType[];
2561
2582
  /** Editor capabilities announced to the host. */
2562
2583
  capabilities?: EditorCapabilities;
2563
2584
  /** Optional handler for `getContentSchema` requests from the host. */
@@ -2592,7 +2613,10 @@ declare class WebEditorClient implements IDisposable {
2592
2613
  private _readOnly;
2593
2614
  private _options;
2594
2615
  private _hostCapabilities;
2616
+ private _contentType;
2595
2617
  private _connected;
2618
+ /** Host messages that arrived before the `initialized` response, which tells how to read them. */
2619
+ private _messagesBeforeConnect;
2596
2620
  private _presentationMode;
2597
2621
  private _presentationPreferences;
2598
2622
  private readonly _pendingHostTransportMessages;
@@ -2636,9 +2660,16 @@ declare class WebEditorClient implements IDisposable {
2636
2660
  /** Resolves once the host has acknowledged `initialized`. */
2637
2661
  readonly onDidConnect: Promise<{
2638
2662
  capabilities: HostCapabilities | undefined;
2663
+ contentType: ContentType;
2639
2664
  }>;
2640
2665
  static connect(options: WebEditorClientOptions): Promise<WebEditorClient>;
2641
2666
  constructor(options: WebEditorClientOptions);
2667
+ /**
2668
+ * The content type the host serves, one of the accepted ones; `undefined`
2669
+ * until the host answered `initialized`. Content events fire only after it is set.
2670
+ */
2671
+ get contentType(): ContentType | undefined;
2672
+ private _afterConnect;
2642
2673
  /**
2643
2674
  * The underlying connection. Reuse it to talk to other host interfaces
2644
2675
  * (e.g. `vscode-app-host`) over the same transport instead of opening a
@@ -3081,7 +3112,6 @@ declare class TextDocumentSync implements IDisposable {
3081
3112
  /** Editor changes after {@link _writing} that are not written yet. */
3082
3113
  private _unwritten;
3083
3114
  private _host;
3084
- private _contentType;
3085
3115
  private readonly _tracked;
3086
3116
  private readonly _onDidChangeText;
3087
3117
  /** Fires after every change of {@link text}. */
package/dist/index.js CHANGED
@@ -4196,16 +4196,27 @@ const zPresentationPreferences = object({
4196
4196
  /** Upper bound for the automatic height; users can still resize beyond it. */
4197
4197
  maxAutomaticHeight: zEditorHeight.optional()
4198
4198
  });
4199
+ const zContentType = _enum(["text", "json"]);
4199
4200
  const webEditorHostInterface = defineInterface({
4200
4201
  id: "web-editor-host",
4201
4202
  description: "Methods the host exposes to the editor (web-editor/0.12)."
4202
4203
  }, {
4204
+ /**
4205
+ * The editor's first message. The host picks the content type from
4206
+ * `contentTypes` (or `contentType`, from editors that do not send it)
4207
+ * and returns it; it fails the request if it can serve none of them.
4208
+ */
4203
4209
  initialized: requestType(object({
4204
4210
  protocolVersion: literal("web-editor/0.12"),
4205
- contentType: _enum(["text", "json"]),
4211
+ /** The editor's preferred content type. Hosts that predate `contentTypes` serve only this one. */
4212
+ contentType: zContentType,
4213
+ /** The content types the editor accepts, most preferred first. */
4214
+ contentTypes: array(zContentType).optional(),
4206
4215
  capabilities: zEditorCapabilities.optional()
4207
4216
  }), object({
4208
4217
  protocolVersion: literal("web-editor-host/0.12"),
4218
+ /** The content type the host serves. Hosts that predate it serve the request's `contentType`. */
4219
+ contentType: zContentType.optional(),
4209
4220
  capabilities: zHostCapabilities.optional()
4210
4221
  })),
4211
4222
  applyContentEdit: notificationType(object({
@@ -4283,7 +4294,12 @@ const vscodeAppHostInterface = defineInterface({
4283
4294
  description: "Methods an app `.vscode-app.html` exposes to the host (vscode-app-host/0.1)."
4284
4295
  }, {
4285
4296
  getContext: requestType(object({}), object({
4286
- dataDocument: object({ contentType: _enum(["text", "json"]) }).nullable(),
4297
+ /**
4298
+ * The document the app edits, or `null` when the app was opened
4299
+ * directly. `contentType` is the host's preference; the type the
4300
+ * host serves is agreed in `web-editor-host::initialized`.
4301
+ */
4302
+ dataDocument: object({ contentType: zContentType }).nullable(),
4287
4303
  embedding: zEmbeddingContext.nullable()
4288
4304
  })),
4289
4305
  /**
@@ -5028,6 +5044,8 @@ var WebEditorHost = class {
5028
5044
  _editor;
5029
5045
  _clientTransport;
5030
5046
  _presentation;
5047
+ _contentTypes;
5048
+ /** The type agreed with the editor; the host's preference until then. */
5031
5049
  _contentType;
5032
5050
  _jsonFormat;
5033
5051
  _onError;
@@ -5060,7 +5078,9 @@ var WebEditorHost = class {
5060
5078
  onDidRequestExitFullView = this._onDidRequestExitFullView.event;
5061
5079
  constructor(options) {
5062
5080
  this._connection = options.connection;
5063
- this._contentType = options.contentType ?? "text";
5081
+ this._contentTypes = options.contentTypes ?? [options.contentType ?? "text"];
5082
+ if (this._contentTypes.length === 0) throw new Error("WebEditorHost: contentTypes must not be empty");
5083
+ this._contentType = this._contentTypes[0];
5064
5084
  this._text = options.initialText ?? "";
5065
5085
  this._readOnly = options.readOnly ?? false;
5066
5086
  this._jsonFormat = options.jsonFormat ?? {};
@@ -5075,7 +5095,15 @@ var WebEditorHost = class {
5075
5095
  this._presentation = this._connection.get(webEditorPresentationInterface);
5076
5096
  this._connection.register(webEditorHostInterface, {
5077
5097
  initialized: (params) => {
5078
- if (params.contentType !== this._contentType) this._onError(`Editor contentType "${params.contentType}" does not match host "${this._contentType}"`);
5098
+ const accepted = params.contentTypes ?? [params.contentType];
5099
+ const contentType = accepted.find((type) => this._contentTypes.includes(type));
5100
+ if (contentType === void 0) {
5101
+ this._editorInitialized = false;
5102
+ const message = `The editor accepts ${accepted.join(", ")} content, but the host can only serve this document as ${this._contentTypes.join(", ")}.`;
5103
+ this._onError(message);
5104
+ throw new Error(message);
5105
+ }
5106
+ this._contentType = contentType;
5079
5107
  this._editorCapabilities = params.capabilities;
5080
5108
  this._editorInitialized = true;
5081
5109
  this._serverRevision = 0;
@@ -5087,6 +5115,7 @@ var WebEditorHost = class {
5087
5115
  this._onDidInitialize.fire({ capabilities: params.capabilities });
5088
5116
  return {
5089
5117
  protocolVersion: "web-editor-host/0.12",
5118
+ contentType,
5090
5119
  capabilities: this._transform ? {
5091
5120
  ...this._capabilities,
5092
5121
  editTransform: OT_EDIT_TRANSFORM
@@ -5150,6 +5179,13 @@ var WebEditorHost = class {
5150
5179
  getText() {
5151
5180
  return this._text;
5152
5181
  }
5182
+ /**
5183
+ * The content type agreed with the editor, in which edits are exchanged.
5184
+ * `undefined` until the editor initialized.
5185
+ */
5186
+ get contentType() {
5187
+ return this._editorInitialized ? this._contentType : void 0;
5188
+ }
5153
5189
  /** The current content's version, to send along with offsets into it. */
5154
5190
  get version() {
5155
5191
  return this._transform ? {
@@ -5543,7 +5579,10 @@ var WebEditorClient = class WebEditorClient {
5543
5579
  _readOnly = false;
5544
5580
  _options = void 0;
5545
5581
  _hostCapabilities;
5582
+ _contentType;
5546
5583
  _connected = false;
5584
+ /** Host messages that arrived before the `initialized` response, which tells how to read them. */
5585
+ _messagesBeforeConnect = [];
5547
5586
  _presentationMode;
5548
5587
  _presentationPreferences;
5549
5588
  _pendingHostTransportMessages = [];
@@ -5586,8 +5625,8 @@ var WebEditorClient = class WebEditorClient {
5586
5625
  this._hostTransportProxy = this._connection.get(webEditorHostTransportInterface);
5587
5626
  this._presentationHost = this._connection.get(webEditorPresentationHostInterface);
5588
5627
  this._connection.register(webEditorInterface, {
5589
- update: (params) => this._handleUpdate(params),
5590
- applyContentEdits: (params) => this._handleApplyContentEdits(params),
5628
+ update: (params) => this._afterConnect(() => this._handleUpdate(params)),
5629
+ applyContentEdits: (params) => this._afterConnect(() => this._handleApplyContentEdits(params)),
5591
5630
  getContentSchema: async () => {
5592
5631
  return { schema: options.getContentSchema ? await options.getContentSchema() : null };
5593
5632
  }
@@ -5601,18 +5640,25 @@ var WebEditorClient = class WebEditorClient {
5601
5640
  if (this._hostTransportActivated) this._onDidReceiveHostMessage.fire(message);
5602
5641
  else this._pendingHostTransportMessages.push(message);
5603
5642
  } });
5643
+ const contentTypes = options.contentTypes ?? [options.contentType ?? "text"];
5644
+ if (contentTypes.length === 0) throw new Error("WebEditorClient: contentTypes must not be empty");
5604
5645
  this.onDidConnect = this._host.initialized({
5605
5646
  protocolVersion: "web-editor/0.12",
5606
- contentType: options.contentType ?? "text",
5647
+ contentType: contentTypes[0],
5648
+ contentTypes: [...contentTypes],
5607
5649
  capabilities: {
5608
5650
  editTransforms: [OT_EDIT_TRANSFORM],
5609
5651
  ...options.capabilities,
5610
5652
  supportsPresentation: true
5611
5653
  }
5612
5654
  }).then((r) => {
5655
+ const contentType = r.contentType ?? contentTypes[0];
5656
+ this._contentType = contentType;
5613
5657
  this._hostCapabilities = r.capabilities;
5614
5658
  this._transform ||= r.capabilities?.editTransform === OT_EDIT_TRANSFORM;
5615
5659
  this._connected = true;
5660
+ for (const handle of this._messagesBeforeConnect?.splice(0) ?? []) handle();
5661
+ this._messagesBeforeConnect = void 0;
5616
5662
  if (this._presentationPreferences) this.setPresentationPreferences(this._presentationPreferences);
5617
5663
  if (r.capabilities?.supportsHostTransport) this._hostTransport = {
5618
5664
  onMessage: (listener) => {
@@ -5626,10 +5672,24 @@ var WebEditorClient = class WebEditorClient {
5626
5672
  sendMessage: (message) => this._hostTransportProxy.message({ message })
5627
5673
  };
5628
5674
  else this._pendingHostTransportMessages.length = 0;
5629
- return { capabilities: r.capabilities };
5675
+ return {
5676
+ capabilities: r.capabilities,
5677
+ contentType
5678
+ };
5630
5679
  });
5631
5680
  }
5632
5681
  /**
5682
+ * The content type the host serves, one of the accepted ones; `undefined`
5683
+ * until the host answered `initialized`. Content events fire only after it is set.
5684
+ */
5685
+ get contentType() {
5686
+ return this._contentType;
5687
+ }
5688
+ _afterConnect(handle) {
5689
+ if (this._messagesBeforeConnect) this._messagesBeforeConnect.push(handle);
5690
+ else handle();
5691
+ }
5692
+ /**
5633
5693
  * The underlying connection. Reuse it to talk to other host interfaces
5634
5694
  * (e.g. `vscode-app-host`) over the same transport instead of opening a
5635
5695
  * second one.
@@ -6081,7 +6141,6 @@ var TextDocumentSync = class {
6081
6141
  /** Editor changes after {@link _writing} that are not written yet. */
6082
6142
  _unwritten = TextOperation.identity;
6083
6143
  _host;
6084
- _contentType = "text";
6085
6144
  _tracked = /* @__PURE__ */ new Set();
6086
6145
  _onDidChangeText = new Emitter();
6087
6146
  /** Fires after every change of {@link text}. */
@@ -6121,7 +6180,6 @@ var TextDocumentSync = class {
6121
6180
  onError: this._onError
6122
6181
  });
6123
6182
  this._host = host;
6124
- this._contentType = options.contentType ?? "text";
6125
6183
  host.onDidChangeText(({ text, edits }) => {
6126
6184
  if (this._host === host) this._acceptEditorChange(text, edits);
6127
6185
  });
@@ -6170,7 +6228,7 @@ var TextDocumentSync = class {
6170
6228
  this._onDidChangeText.dispose();
6171
6229
  }
6172
6230
  _acceptEditorChange(text, edits) {
6173
- const operation = this._contentType === "text" && edits.length === 1 && edits[0].kind === "stringEdits" && edits[0].path.length === 0 ? TextOperation.fromTextEdits(edits[0].stringEdits) : diffText(this._text, text);
6231
+ const operation = this._host?.contentType === "text" && edits.length === 1 && edits[0].kind === "stringEdits" && edits[0].path.length === 0 ? TextOperation.fromTextEdits(edits[0].stringEdits) : diffText(this._text, text);
6174
6232
  this._applyToText(operation);
6175
6233
  this._unwritten = this._unwritten.compose(operation);
6176
6234
  this._write();
@@ -6238,7 +6296,7 @@ var TextDocumentSync = class {
6238
6296
  _applyFromDocument(operation) {
6239
6297
  if (operation.isIdentity) return;
6240
6298
  this._applyToText(operation);
6241
- if (this._contentType === "text") this._host?.applyEdits([{
6299
+ if (this._host?.contentType === "text") this._host?.applyEdits([{
6242
6300
  kind: "stringEdits",
6243
6301
  path: [],
6244
6302
  stringEdits: operation.toTextEdits()