@vscode/web-editors 0.0.2-12 → 0.0.2-120
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 +398 -12
- package/dist/index.js +1074 -68
- package/dist/index.js.map +1 -1
- package/package.json +6 -3
package/dist/index.js
CHANGED
|
@@ -152,7 +152,7 @@ const allowsEval = /* @__PURE__*/ cached(() => {
|
|
|
152
152
|
return false;
|
|
153
153
|
}
|
|
154
154
|
});
|
|
155
|
-
function isPlainObject(o) {
|
|
155
|
+
function isPlainObject$1(o) {
|
|
156
156
|
if (isObject(o) === false) return false;
|
|
157
157
|
const ctor = o.constructor;
|
|
158
158
|
if (ctor === void 0) return true;
|
|
@@ -163,7 +163,7 @@ function isPlainObject(o) {
|
|
|
163
163
|
return true;
|
|
164
164
|
}
|
|
165
165
|
function shallowClone(o) {
|
|
166
|
-
if (isPlainObject(o)) return { ...o };
|
|
166
|
+
if (isPlainObject$1(o)) return { ...o };
|
|
167
167
|
if (Array.isArray(o)) return [...o];
|
|
168
168
|
if (o instanceof Map) return new Map(o);
|
|
169
169
|
if (o instanceof Set) return new Set(o);
|
|
@@ -246,7 +246,7 @@ function omit(schema, mask) {
|
|
|
246
246
|
}));
|
|
247
247
|
}
|
|
248
248
|
function extend(schema, shape) {
|
|
249
|
-
if (!isPlainObject(shape)) throw new Error("Invalid input to extend: expected a plain object");
|
|
249
|
+
if (!isPlainObject$1(shape)) throw new Error("Invalid input to extend: expected a plain object");
|
|
250
250
|
const checks = schema._zod.def.checks;
|
|
251
251
|
if (checks && checks.length > 0) {
|
|
252
252
|
const existingShape = schema._zod.def.shape;
|
|
@@ -262,7 +262,7 @@ function extend(schema, shape) {
|
|
|
262
262
|
} }));
|
|
263
263
|
}
|
|
264
264
|
function safeExtend(schema, shape) {
|
|
265
|
-
if (!isPlainObject(shape)) throw new Error("Invalid input to safeExtend: expected a plain object");
|
|
265
|
+
if (!isPlainObject$1(shape)) throw new Error("Invalid input to safeExtend: expected a plain object");
|
|
266
266
|
return clone(schema, mergeDefs(schema._zod.def, { get shape() {
|
|
267
267
|
const _shape = {
|
|
268
268
|
...schema._zod.def.shape,
|
|
@@ -1853,7 +1853,7 @@ function mergeValues(a, b) {
|
|
|
1853
1853
|
valid: true,
|
|
1854
1854
|
data: a
|
|
1855
1855
|
};
|
|
1856
|
-
if (isPlainObject(a) && isPlainObject(b)) {
|
|
1856
|
+
if (isPlainObject$1(a) && isPlainObject$1(b)) {
|
|
1857
1857
|
const bKeys = Object.keys(b);
|
|
1858
1858
|
const sharedKeys = Object.keys(a).filter((key) => bKeys.indexOf(key) !== -1);
|
|
1859
1859
|
const newObj = {
|
|
@@ -4118,14 +4118,33 @@ function superRefine(fn, params) {
|
|
|
4118
4118
|
//#endregion
|
|
4119
4119
|
//#region src/protocol.ts
|
|
4120
4120
|
const zJsonValue = unknown();
|
|
4121
|
+
/**
|
|
4122
|
+
* The concurrency control both sides use when they announce it: concurrent
|
|
4123
|
+
* edits are transformed against each other instead of one side's being
|
|
4124
|
+
* dropped. See `OtPeer`.
|
|
4125
|
+
*/
|
|
4126
|
+
const OT_EDIT_TRANSFORM = "ot/1";
|
|
4121
4127
|
const zEditorCapabilities = object({
|
|
4122
4128
|
supportsContentEdits: boolean().optional(),
|
|
4123
|
-
supportsOptions: boolean().optional()
|
|
4129
|
+
supportsOptions: boolean().optional(),
|
|
4130
|
+
/** The editor implements `web-editor-presentation` (see {@link webEditorPresentationInterface}). */
|
|
4131
|
+
supportsPresentation: boolean().optional(),
|
|
4132
|
+
/**
|
|
4133
|
+
* Concurrency controls the editor implements, such as {@link OT_EDIT_TRANSFORM}.
|
|
4134
|
+
* Without one the host uses the client-authority policy, where
|
|
4135
|
+
* conflicting edits are dropped.
|
|
4136
|
+
*/
|
|
4137
|
+
editTransforms: array(string()).optional()
|
|
4124
4138
|
});
|
|
4125
4139
|
const zHostCapabilities = object({
|
|
4126
4140
|
supportsContentEdits: boolean().optional(),
|
|
4127
4141
|
supportsOptions: boolean().optional(),
|
|
4128
|
-
supportsForceUpdate: boolean().optional()
|
|
4142
|
+
supportsForceUpdate: boolean().optional(),
|
|
4143
|
+
supportsHostTransport: boolean().optional(),
|
|
4144
|
+
/** The host presents the editor inline and implements `web-editor-presentation-host`. */
|
|
4145
|
+
supportsPresentation: boolean().optional(),
|
|
4146
|
+
/** The concurrency control the host chose from the editor's `editTransforms`. */
|
|
4147
|
+
editTransform: string().optional()
|
|
4129
4148
|
});
|
|
4130
4149
|
const zTextEdit = object({
|
|
4131
4150
|
offset: number().int().nonnegative(),
|
|
@@ -4147,7 +4166,34 @@ const zWebEditorUpdate = object({
|
|
|
4147
4166
|
readOnly: boolean().optional(),
|
|
4148
4167
|
serverRevision: number().int().nonnegative().optional(),
|
|
4149
4168
|
acknowledgedClientRevision: number().int().nonnegative().optional(),
|
|
4150
|
-
force: boolean().optional()
|
|
4169
|
+
force: boolean().optional(),
|
|
4170
|
+
/**
|
|
4171
|
+
* Sent with every `content` once the host chose a concurrency control, so
|
|
4172
|
+
* the editor knows it before `initialized` returns.
|
|
4173
|
+
*/
|
|
4174
|
+
editTransform: string().optional()
|
|
4175
|
+
});
|
|
4176
|
+
/** How an embedding host currently presents the editor. */
|
|
4177
|
+
const zPresentationMode = _enum([
|
|
4178
|
+
"inline",
|
|
4179
|
+
"full-width",
|
|
4180
|
+
"full-view"
|
|
4181
|
+
]);
|
|
4182
|
+
const zEditorHeight = number().positive().max(1e4);
|
|
4183
|
+
/**
|
|
4184
|
+
* Presentation controls the editor asks its embedding host for. The host's own
|
|
4185
|
+
* configuration takes precedence over these preferences.
|
|
4186
|
+
*/
|
|
4187
|
+
const zPresentationPreferences = object({
|
|
4188
|
+
/** A height grip; dragging near the automatic (content) height snaps back to it. */
|
|
4189
|
+
resizable: boolean().optional(),
|
|
4190
|
+
/** A width grip that widens the editor up to the host's viewport. */
|
|
4191
|
+
fullWidth: boolean().optional(),
|
|
4192
|
+
/** A toolbar action that fills the host's viewport. */
|
|
4193
|
+
fullView: boolean().optional(),
|
|
4194
|
+
minHeight: zEditorHeight.optional(),
|
|
4195
|
+
/** Upper bound for the automatic height; users can still resize beyond it. */
|
|
4196
|
+
maxAutomaticHeight: zEditorHeight.optional()
|
|
4151
4197
|
});
|
|
4152
4198
|
const webEditorHostInterface = defineInterface({
|
|
4153
4199
|
id: "web-editor-host",
|
|
@@ -4167,9 +4213,18 @@ const webEditorHostInterface = defineInterface({
|
|
|
4167
4213
|
basedOnServerRevision: number().int().nonnegative()
|
|
4168
4214
|
})),
|
|
4169
4215
|
/**
|
|
4170
|
-
*
|
|
4171
|
-
*
|
|
4172
|
-
*
|
|
4216
|
+
* With an `editTransform`: the editor has integrated the host's edits up
|
|
4217
|
+
* to `serverRevision`. Lets the host forget them while the editor does
|
|
4218
|
+
* not edit; an `applyContentEdit` acknowledges them as well.
|
|
4219
|
+
*/
|
|
4220
|
+
acknowledge: notificationType(object({ serverRevision: number().int().nonnegative() })),
|
|
4221
|
+
/**
|
|
4222
|
+
* The editor reports its preferred content height (px): the height it needs
|
|
4223
|
+
* to show its content without scrolling, independent of the frame's current
|
|
4224
|
+
* height. Measure the content, not the viewport or document, which follow
|
|
4225
|
+
* the frame. An embedding host that reserves block space (e.g. an iframe
|
|
4226
|
+
* with no intrinsic height) sizes the frame to it and uses it as the
|
|
4227
|
+
* automatic height that user resizing snaps back to.
|
|
4173
4228
|
*/
|
|
4174
4229
|
reportSize: notificationType(object({ height: number().nonnegative() }))
|
|
4175
4230
|
});
|
|
@@ -4185,6 +4240,31 @@ const webEditorInterface = defineInterface({
|
|
|
4185
4240
|
})),
|
|
4186
4241
|
getContentSchema: requestType(object({}), object({ schema: zJsonValue }))
|
|
4187
4242
|
});
|
|
4243
|
+
const webEditorHostTransportInterface = defineInterface({
|
|
4244
|
+
id: "web-editor-host-transport",
|
|
4245
|
+
description: "Optional messages the web editor sends to its host."
|
|
4246
|
+
}, { message: notificationType(object({ message: zJsonValue })) });
|
|
4247
|
+
const webEditorClientTransportInterface = defineInterface({
|
|
4248
|
+
id: "web-editor-client-transport",
|
|
4249
|
+
description: "Optional messages the web editor host sends to the editor."
|
|
4250
|
+
}, { message: notificationType(object({ message: zJsonValue })) });
|
|
4251
|
+
/** Methods the editor exposes to a presenting host. */
|
|
4252
|
+
const webEditorPresentationInterface = defineInterface({
|
|
4253
|
+
id: "web-editor-presentation",
|
|
4254
|
+
description: "How the embedding host currently presents the editor."
|
|
4255
|
+
}, {
|
|
4256
|
+
/** Sent once the editor initialized, and whenever the mode changes. */
|
|
4257
|
+
changed: notificationType(object({ mode: zPresentationMode })) });
|
|
4258
|
+
/** Methods a presenting host exposes to the editor. */
|
|
4259
|
+
const webEditorPresentationHostInterface = defineInterface({
|
|
4260
|
+
id: "web-editor-presentation-host",
|
|
4261
|
+
description: "Presentation preferences and actions of an embedded editor."
|
|
4262
|
+
}, {
|
|
4263
|
+
/** Replaces the editor's previous preferences. */
|
|
4264
|
+
setPreferences: notificationType(zPresentationPreferences),
|
|
4265
|
+
/** Asks the host to leave full view, for example when the user presses Escape. */
|
|
4266
|
+
exitFullView: notificationType(object({}))
|
|
4267
|
+
});
|
|
4188
4268
|
/** Per-scope registration: `registered` (toolbar button) and `isDefault` (auto-open). */
|
|
4189
4269
|
const zScopeRegistration = object({
|
|
4190
4270
|
registered: boolean(),
|
|
@@ -4260,7 +4340,21 @@ const vscodeAppHostInterface = defineInterface({
|
|
|
4260
4340
|
workspace: zScopeRegistration,
|
|
4261
4341
|
hasWorkspace: boolean(),
|
|
4262
4342
|
changed: boolean()
|
|
4263
|
-
}))
|
|
4343
|
+
})),
|
|
4344
|
+
/**
|
|
4345
|
+
* The view state of this editor tab, such as scroll positions or
|
|
4346
|
+
* selections, as last set by {@link setViewState}. `value` is omitted
|
|
4347
|
+
* when none is set. VS Code restores it when it restores the tab (for
|
|
4348
|
+
* example after a window reload), but a newly opened tab starts
|
|
4349
|
+
* without one. Only the top-level app of a tab has view state.
|
|
4350
|
+
*/
|
|
4351
|
+
getViewState: requestType(object({}), object({ value: zJsonValue.optional() })),
|
|
4352
|
+
/**
|
|
4353
|
+
* Replace this editor tab's view state with a JSON value. The host
|
|
4354
|
+
* rejects values too large to persist with the tab, and calls from
|
|
4355
|
+
* embedded apps.
|
|
4356
|
+
*/
|
|
4357
|
+
setViewState: requestType(object({ value: zJsonValue }), object({}))
|
|
4264
4358
|
});
|
|
4265
4359
|
/** Host-to-parent notifications for loaded child runtime changes. */
|
|
4266
4360
|
const vscodeAppEmbeddingEventsInterface = defineInterface({
|
|
@@ -4283,7 +4377,7 @@ const vscodeAppEmbeddingEventsInterface = defineInterface({
|
|
|
4283
4377
|
*/
|
|
4284
4378
|
function applyContentEdits(root, edits) {
|
|
4285
4379
|
let current = root;
|
|
4286
|
-
for (const edit of edits) if (edit.kind === "replace") current = setAtPath(current, edit.path, edit.newValue);
|
|
4380
|
+
for (const edit of edits) if (edit.kind === "replace") current = setAtPath(current, edit.path, cloneJson(edit.newValue));
|
|
4287
4381
|
else {
|
|
4288
4382
|
const existing = getAtPath(current, edit.path);
|
|
4289
4383
|
if (typeof existing !== "string") throw new Error(`applyContentEdits: stringEdits at path [${edit.path.join(", ")}] requires a string, got ${typeof existing}`);
|
|
@@ -4297,6 +4391,9 @@ function applyTextEdits(text, edits) {
|
|
|
4297
4391
|
for (const e of edits) result = result.slice(0, e.offset) + e.newText + result.slice(e.offset + e.length);
|
|
4298
4392
|
return result;
|
|
4299
4393
|
}
|
|
4394
|
+
function cloneJson(value) {
|
|
4395
|
+
return typeof value === "object" && value !== null ? JSON.parse(JSON.stringify(value)) : value;
|
|
4396
|
+
}
|
|
4300
4397
|
function getAtPath(root, path) {
|
|
4301
4398
|
let cur = root;
|
|
4302
4399
|
for (const key of path) {
|
|
@@ -4371,33 +4468,604 @@ var Emitter = class {
|
|
|
4371
4468
|
}
|
|
4372
4469
|
};
|
|
4373
4470
|
//#endregion
|
|
4471
|
+
//#region src/sync/otPeer.ts
|
|
4472
|
+
/**
|
|
4473
|
+
* One of the two sides of a session that keeps a document in sync between
|
|
4474
|
+
* two parties who both edit it (the Jupiter algorithm behind most operational
|
|
4475
|
+
* transformation systems).
|
|
4476
|
+
*
|
|
4477
|
+
* Each side applies its own operations immediately and sends them along with
|
|
4478
|
+
* how many of the other side's operations it had applied. When an operation
|
|
4479
|
+
* from the other side arrives, it was made without the local operations the
|
|
4480
|
+
* other side had not seen yet. Those pending operations and the incoming one
|
|
4481
|
+
* are transformed against each other: the incoming one is then applied, and
|
|
4482
|
+
* the pending ones now describe how the local state differs from the other
|
|
4483
|
+
* side's. Once both sides have received everything, they hold the same
|
|
4484
|
+
* document, and no operation was dropped.
|
|
4485
|
+
*
|
|
4486
|
+
* The leader's operations take precedence in conflicts. Exactly one side must
|
|
4487
|
+
* be the leader. Messages must arrive in the order they were sent.
|
|
4488
|
+
*
|
|
4489
|
+
* The leader can also start over from a state it sends in full ({@link reset});
|
|
4490
|
+
* the follower's operations that were made before it received that state are
|
|
4491
|
+
* then discarded on both sides.
|
|
4492
|
+
*/
|
|
4493
|
+
var OtPeer = class {
|
|
4494
|
+
_type;
|
|
4495
|
+
role;
|
|
4496
|
+
_localRevision = 0;
|
|
4497
|
+
_remoteRevision = 0;
|
|
4498
|
+
_pending = [];
|
|
4499
|
+
/** Remote operations made before the remote side saw this local revision are discarded. */
|
|
4500
|
+
_barrier = 0;
|
|
4501
|
+
constructor(_type, role) {
|
|
4502
|
+
this._type = _type;
|
|
4503
|
+
this.role = role;
|
|
4504
|
+
}
|
|
4505
|
+
/** Number of local operations, including resets. */
|
|
4506
|
+
get localRevision() {
|
|
4507
|
+
return this._localRevision;
|
|
4508
|
+
}
|
|
4509
|
+
/** Number of remote operations integrated, including discarded ones. */
|
|
4510
|
+
get remoteRevision() {
|
|
4511
|
+
return this._remoteRevision;
|
|
4512
|
+
}
|
|
4513
|
+
/** Local operations the remote side has not acknowledged yet, oldest first. */
|
|
4514
|
+
get pending() {
|
|
4515
|
+
return this._pending.map((p) => p.op);
|
|
4516
|
+
}
|
|
4517
|
+
/**
|
|
4518
|
+
* The local operations after `revision`, oldest first: how the local state
|
|
4519
|
+
* differs from the state the remote side had when it had integrated
|
|
4520
|
+
* `revision` and everything local before. `undefined` if they are no
|
|
4521
|
+
* longer known, because they were acknowledged since or a reset happened.
|
|
4522
|
+
*/
|
|
4523
|
+
operationsAfter(revision) {
|
|
4524
|
+
if (revision < this._barrier || revision > this._localRevision) return;
|
|
4525
|
+
const firstPending = this._pending[0]?.revision ?? this._localRevision + 1;
|
|
4526
|
+
return revision + 1 < firstPending ? void 0 : this._pending.slice(revision + 1 - firstPending).map((p) => p.op);
|
|
4527
|
+
}
|
|
4528
|
+
/**
|
|
4529
|
+
* Records an operation already applied to the local state. Send it with
|
|
4530
|
+
* the returned revision and {@link remoteRevision} as the acknowledgement.
|
|
4531
|
+
*/
|
|
4532
|
+
local(op) {
|
|
4533
|
+
const revision = ++this._localRevision;
|
|
4534
|
+
this._pending.push({
|
|
4535
|
+
revision,
|
|
4536
|
+
op
|
|
4537
|
+
});
|
|
4538
|
+
return revision;
|
|
4539
|
+
}
|
|
4540
|
+
/**
|
|
4541
|
+
* Integrates an operation from the remote side.
|
|
4542
|
+
*
|
|
4543
|
+
* @param revision The remote side's revision after `op`.
|
|
4544
|
+
* @param acknowledgedRevision How many local operations the remote side had integrated when it made `op`.
|
|
4545
|
+
* @returns The operation to apply to the local state, or `undefined` if
|
|
4546
|
+
* `op` was made before the remote side received the latest reset.
|
|
4547
|
+
*/
|
|
4548
|
+
remote(op, revision, acknowledgedRevision) {
|
|
4549
|
+
if (revision !== this._remoteRevision + 1) throw new Error(`OtPeer: expected remote revision ${this._remoteRevision + 1}, got ${revision}`);
|
|
4550
|
+
if (acknowledgedRevision > this._localRevision) throw new Error(`OtPeer: remote acknowledged revision ${acknowledgedRevision}, but the latest local revision is ${this._localRevision}`);
|
|
4551
|
+
this._remoteRevision = revision;
|
|
4552
|
+
if (acknowledgedRevision < this._barrier) return;
|
|
4553
|
+
this.acknowledge(acknowledgedRevision);
|
|
4554
|
+
let incoming = op;
|
|
4555
|
+
this._pending = this._pending.map(({ revision, op: pending }) => {
|
|
4556
|
+
let pendingPrime;
|
|
4557
|
+
if (this.role === "leader") [pendingPrime, incoming] = this._type.transform(pending, incoming);
|
|
4558
|
+
else [incoming, pendingPrime] = this._type.transform(incoming, pending);
|
|
4559
|
+
return {
|
|
4560
|
+
revision,
|
|
4561
|
+
op: pendingPrime
|
|
4562
|
+
};
|
|
4563
|
+
});
|
|
4564
|
+
return incoming;
|
|
4565
|
+
}
|
|
4566
|
+
/** The remote side has integrated all local operations up to `revision`. */
|
|
4567
|
+
acknowledge(revision) {
|
|
4568
|
+
if (revision > this._localRevision) throw new Error(`OtPeer: remote acknowledged revision ${revision}, but the latest local revision is ${this._localRevision}`);
|
|
4569
|
+
let i = 0;
|
|
4570
|
+
while (i < this._pending.length && this._pending[i].revision <= revision) i++;
|
|
4571
|
+
if (i > 0) this._pending = this._pending.slice(i);
|
|
4572
|
+
}
|
|
4573
|
+
/**
|
|
4574
|
+
* Leader only: the local state is about to be sent in full and replaces
|
|
4575
|
+
* whatever the follower has. Returns the revision of the reset.
|
|
4576
|
+
*
|
|
4577
|
+
* @param remoteRevision Treat remote operations up to this revision as
|
|
4578
|
+
* integrated, to recover when the remote side's revisions got out of step.
|
|
4579
|
+
*/
|
|
4580
|
+
reset(remoteRevision = this._remoteRevision) {
|
|
4581
|
+
if (this.role !== "leader") throw new Error("OtPeer.reset: only the leader can reset");
|
|
4582
|
+
this._remoteRevision = Math.max(this._remoteRevision, remoteRevision);
|
|
4583
|
+
this._pending = [];
|
|
4584
|
+
this._barrier = ++this._localRevision;
|
|
4585
|
+
return this._localRevision;
|
|
4586
|
+
}
|
|
4587
|
+
/**
|
|
4588
|
+
* Follower only: replaces the local state with the leader's full state at
|
|
4589
|
+
* `revision`. Pending local operations are discarded, as the leader
|
|
4590
|
+
* discards them too.
|
|
4591
|
+
*/
|
|
4592
|
+
acceptReset(revision) {
|
|
4593
|
+
if (this.role !== "follower") throw new Error("OtPeer.acceptReset: only the follower accepts resets");
|
|
4594
|
+
if (revision !== this._remoteRevision + 1) throw new Error(`OtPeer: expected remote revision ${this._remoteRevision + 1}, got ${revision}`);
|
|
4595
|
+
this._remoteRevision = revision;
|
|
4596
|
+
this._pending = [];
|
|
4597
|
+
}
|
|
4598
|
+
};
|
|
4599
|
+
//#endregion
|
|
4600
|
+
//#region src/sync/textOperation.ts
|
|
4601
|
+
/** {@link OtType} for {@link TextOperation}s. */
|
|
4602
|
+
const textOperationOt = { transform: (winner, loser) => TextOperation.transform(winner, loser) };
|
|
4603
|
+
/**
|
|
4604
|
+
* A change to a string, as a sequence of components that walk the original
|
|
4605
|
+
* string from start to end: keep (`retain`), add (`insert`) or remove
|
|
4606
|
+
* (`delete`) text. Everything after the last component is kept.
|
|
4607
|
+
*
|
|
4608
|
+
* Operations are normalized (see {@link TextOperationBuilder}), so equal
|
|
4609
|
+
* changes have equal components. Their size depends on the number of edited
|
|
4610
|
+
* regions, not on the length of the string.
|
|
4611
|
+
*/
|
|
4612
|
+
var TextOperation = class TextOperation {
|
|
4613
|
+
components;
|
|
4614
|
+
static get identity() {
|
|
4615
|
+
return identityOperation;
|
|
4616
|
+
}
|
|
4617
|
+
/** Converts edits that apply one after another, as on the wire, into one operation. */
|
|
4618
|
+
static fromTextEdits(edits) {
|
|
4619
|
+
let result = TextOperation.identity;
|
|
4620
|
+
for (const edit of edits) result = result.compose(new TextOperationBuilder().retain(edit.offset).insert(edit.newText).delete(edit.length).build());
|
|
4621
|
+
return result;
|
|
4622
|
+
}
|
|
4623
|
+
/** Use {@link TextOperationBuilder}, which normalizes the components. */
|
|
4624
|
+
constructor(token, components) {
|
|
4625
|
+
this.components = components;
|
|
4626
|
+
if (token !== normalizedToken) throw new Error("TextOperation: use TextOperationBuilder");
|
|
4627
|
+
}
|
|
4628
|
+
get isIdentity() {
|
|
4629
|
+
return this.components.length === 0;
|
|
4630
|
+
}
|
|
4631
|
+
/** Length of the shortest string this operation applies to. */
|
|
4632
|
+
get baseSpan() {
|
|
4633
|
+
let span = 0;
|
|
4634
|
+
for (const c of this.components) if (c.kind !== "insert") span += c.length;
|
|
4635
|
+
return span;
|
|
4636
|
+
}
|
|
4637
|
+
apply(text) {
|
|
4638
|
+
if (this.baseSpan > text.length) throw new Error(`TextOperation spans ${this.baseSpan} characters, but the text has ${text.length}`);
|
|
4639
|
+
const parts = [];
|
|
4640
|
+
let offset = 0;
|
|
4641
|
+
for (const c of this.components) if (c.kind === "retain") {
|
|
4642
|
+
parts.push(text.slice(offset, offset + c.length));
|
|
4643
|
+
offset += c.length;
|
|
4644
|
+
} else if (c.kind === "insert") parts.push(c.text);
|
|
4645
|
+
else offset += c.length;
|
|
4646
|
+
parts.push(text.slice(offset));
|
|
4647
|
+
return parts.join("");
|
|
4648
|
+
}
|
|
4649
|
+
/** Edits in ascending order that, applied one after another, have the effect of this operation. */
|
|
4650
|
+
toTextEdits() {
|
|
4651
|
+
const edits = [];
|
|
4652
|
+
let offset = 0;
|
|
4653
|
+
let pending;
|
|
4654
|
+
const flush = () => {
|
|
4655
|
+
if (pending) {
|
|
4656
|
+
edits.push(pending);
|
|
4657
|
+
offset += pending.newText.length;
|
|
4658
|
+
pending = void 0;
|
|
4659
|
+
}
|
|
4660
|
+
};
|
|
4661
|
+
for (const c of this.components) if (c.kind === "retain") {
|
|
4662
|
+
flush();
|
|
4663
|
+
offset += c.length;
|
|
4664
|
+
} else {
|
|
4665
|
+
pending ??= {
|
|
4666
|
+
offset,
|
|
4667
|
+
length: 0,
|
|
4668
|
+
newText: ""
|
|
4669
|
+
};
|
|
4670
|
+
if (c.kind === "insert") pending.newText += c.text;
|
|
4671
|
+
else pending.length += c.length;
|
|
4672
|
+
}
|
|
4673
|
+
flush();
|
|
4674
|
+
return edits;
|
|
4675
|
+
}
|
|
4676
|
+
/** The operation that has the effect of this operation followed by `next`. */
|
|
4677
|
+
compose(next) {
|
|
4678
|
+
const builder = new TextOperationBuilder();
|
|
4679
|
+
const first = new ComponentCursor(this.components);
|
|
4680
|
+
const second = new ComponentCursor(next.components);
|
|
4681
|
+
while (!first.done || !second.done) {
|
|
4682
|
+
const a = first.peek();
|
|
4683
|
+
const b = second.peek();
|
|
4684
|
+
if (a.kind === "delete") builder.delete(first.take(a.length));
|
|
4685
|
+
else if (b.kind === "insert") builder.insert(second.takeText());
|
|
4686
|
+
else {
|
|
4687
|
+
const length = Math.min(a.length, b.length);
|
|
4688
|
+
if (a.kind === "retain") if (b.kind === "retain") builder.retain(length);
|
|
4689
|
+
else builder.delete(length);
|
|
4690
|
+
else if (b.kind === "retain") builder.insert(a.text.slice(0, length));
|
|
4691
|
+
first.take(length);
|
|
4692
|
+
second.take(length);
|
|
4693
|
+
}
|
|
4694
|
+
}
|
|
4695
|
+
return builder.build();
|
|
4696
|
+
}
|
|
4697
|
+
/**
|
|
4698
|
+
* Transforms two operations on the same string so that each applies after
|
|
4699
|
+
* the other and both orders give the same result (`b'(a(s)) === a'(b(s))`).
|
|
4700
|
+
* Where both insert at the same position, `winner`'s text comes first.
|
|
4701
|
+
*
|
|
4702
|
+
* @returns `[winner', loser']`: `winner'` applies after `loser`, `loser'` after `winner`.
|
|
4703
|
+
*/
|
|
4704
|
+
static transform(winner, loser) {
|
|
4705
|
+
const winnerPrime = new TextOperationBuilder();
|
|
4706
|
+
const loserPrime = new TextOperationBuilder();
|
|
4707
|
+
const w = new ComponentCursor(winner.components);
|
|
4708
|
+
const l = new ComponentCursor(loser.components);
|
|
4709
|
+
while (!w.done || !l.done) {
|
|
4710
|
+
const a = w.peek();
|
|
4711
|
+
const b = l.peek();
|
|
4712
|
+
if (a.kind === "insert") {
|
|
4713
|
+
const text = w.takeText();
|
|
4714
|
+
winnerPrime.insert(text);
|
|
4715
|
+
loserPrime.retain(text.length);
|
|
4716
|
+
} else if (b.kind === "insert") {
|
|
4717
|
+
const text = l.takeText();
|
|
4718
|
+
winnerPrime.retain(text.length);
|
|
4719
|
+
loserPrime.insert(text);
|
|
4720
|
+
} else {
|
|
4721
|
+
const length = Math.min(a.length, b.length);
|
|
4722
|
+
if (a.kind === "retain" && b.kind === "retain") {
|
|
4723
|
+
winnerPrime.retain(length);
|
|
4724
|
+
loserPrime.retain(length);
|
|
4725
|
+
} else if (a.kind === "delete" && b.kind === "retain") winnerPrime.delete(length);
|
|
4726
|
+
else if (a.kind === "retain" && b.kind === "delete") loserPrime.delete(length);
|
|
4727
|
+
w.take(length);
|
|
4728
|
+
l.take(length);
|
|
4729
|
+
}
|
|
4730
|
+
}
|
|
4731
|
+
return [winnerPrime.build(), loserPrime.build()];
|
|
4732
|
+
}
|
|
4733
|
+
/**
|
|
4734
|
+
* Where `offset` in the original string ends up after this operation.
|
|
4735
|
+
* An offset inside deleted text maps to where the deletion happened. Text
|
|
4736
|
+
* inserted exactly at `offset` ends up before the result for
|
|
4737
|
+
* `assoc === "after"` and after it for `assoc === "before"`.
|
|
4738
|
+
*/
|
|
4739
|
+
mapOffset(offset, assoc = "after") {
|
|
4740
|
+
let original = 0;
|
|
4741
|
+
let mapped = 0;
|
|
4742
|
+
for (const c of this.components) {
|
|
4743
|
+
if (c.kind === "insert") {
|
|
4744
|
+
if (original < offset || original === offset && assoc === "after") mapped += c.text.length;
|
|
4745
|
+
continue;
|
|
4746
|
+
}
|
|
4747
|
+
if (original >= offset) break;
|
|
4748
|
+
const length = Math.min(c.length, offset - original);
|
|
4749
|
+
original += length;
|
|
4750
|
+
if (c.kind === "retain") mapped += length;
|
|
4751
|
+
if (length < c.length) break;
|
|
4752
|
+
}
|
|
4753
|
+
return mapped + offset - original;
|
|
4754
|
+
}
|
|
4755
|
+
equals(other) {
|
|
4756
|
+
return this.components.length === other.components.length && this.components.every((c, i) => componentsEqual(c, other.components[i]));
|
|
4757
|
+
}
|
|
4758
|
+
toString() {
|
|
4759
|
+
return this.components.map((c) => c.kind === "insert" ? JSON.stringify(c.text) : c.kind === "retain" ? `${c.length}` : `-${c.length}`).join(" ");
|
|
4760
|
+
}
|
|
4761
|
+
};
|
|
4762
|
+
const normalizedToken = Symbol("TextOperation.normalized");
|
|
4763
|
+
/**
|
|
4764
|
+
* Builds normalized {@link TextOperation}s: no empty components, adjacent
|
|
4765
|
+
* components of the same kind merged, an insert before a delete at the same
|
|
4766
|
+
* position, and no trailing retain.
|
|
4767
|
+
*/
|
|
4768
|
+
var TextOperationBuilder = class {
|
|
4769
|
+
_components = [];
|
|
4770
|
+
retain(length) {
|
|
4771
|
+
assertLength(length);
|
|
4772
|
+
if (length === 0) return this;
|
|
4773
|
+
const last = this._components.at(-1);
|
|
4774
|
+
if (last?.kind === "retain") this._components[this._components.length - 1] = {
|
|
4775
|
+
kind: "retain",
|
|
4776
|
+
length: last.length + length
|
|
4777
|
+
};
|
|
4778
|
+
else this._components.push({
|
|
4779
|
+
kind: "retain",
|
|
4780
|
+
length
|
|
4781
|
+
});
|
|
4782
|
+
return this;
|
|
4783
|
+
}
|
|
4784
|
+
insert(text) {
|
|
4785
|
+
if (text === "") return this;
|
|
4786
|
+
const last = this._components.at(-1);
|
|
4787
|
+
if (last?.kind === "insert") this._components[this._components.length - 1] = {
|
|
4788
|
+
kind: "insert",
|
|
4789
|
+
text: last.text + text
|
|
4790
|
+
};
|
|
4791
|
+
else if (last?.kind === "delete") {
|
|
4792
|
+
const beforeDelete = this._components.at(-2);
|
|
4793
|
+
if (beforeDelete?.kind === "insert") this._components[this._components.length - 2] = {
|
|
4794
|
+
kind: "insert",
|
|
4795
|
+
text: beforeDelete.text + text
|
|
4796
|
+
};
|
|
4797
|
+
else this._components.splice(this._components.length - 1, 0, {
|
|
4798
|
+
kind: "insert",
|
|
4799
|
+
text
|
|
4800
|
+
});
|
|
4801
|
+
} else this._components.push({
|
|
4802
|
+
kind: "insert",
|
|
4803
|
+
text
|
|
4804
|
+
});
|
|
4805
|
+
return this;
|
|
4806
|
+
}
|
|
4807
|
+
delete(length) {
|
|
4808
|
+
assertLength(length);
|
|
4809
|
+
if (length === 0) return this;
|
|
4810
|
+
const last = this._components.at(-1);
|
|
4811
|
+
if (last?.kind === "delete") this._components[this._components.length - 1] = {
|
|
4812
|
+
kind: "delete",
|
|
4813
|
+
length: last.length + length
|
|
4814
|
+
};
|
|
4815
|
+
else this._components.push({
|
|
4816
|
+
kind: "delete",
|
|
4817
|
+
length
|
|
4818
|
+
});
|
|
4819
|
+
return this;
|
|
4820
|
+
}
|
|
4821
|
+
build() {
|
|
4822
|
+
const components = this._components.at(-1)?.kind === "retain" ? this._components.slice(0, -1) : [...this._components];
|
|
4823
|
+
return new TextOperation(normalizedToken, components);
|
|
4824
|
+
}
|
|
4825
|
+
};
|
|
4826
|
+
/** Walks components, splitting them as needed. Past the end it yields an endless retain. */
|
|
4827
|
+
var ComponentCursor = class {
|
|
4828
|
+
_components;
|
|
4829
|
+
_index = 0;
|
|
4830
|
+
_consumed = 0;
|
|
4831
|
+
constructor(_components) {
|
|
4832
|
+
this._components = _components;
|
|
4833
|
+
}
|
|
4834
|
+
get done() {
|
|
4835
|
+
return this._index >= this._components.length;
|
|
4836
|
+
}
|
|
4837
|
+
/** The rest of the current component. */
|
|
4838
|
+
peek() {
|
|
4839
|
+
const c = this._components[this._index];
|
|
4840
|
+
if (c === void 0) return {
|
|
4841
|
+
kind: "retain",
|
|
4842
|
+
length: Infinity
|
|
4843
|
+
};
|
|
4844
|
+
if (c.kind === "insert") {
|
|
4845
|
+
const text = c.text.slice(this._consumed);
|
|
4846
|
+
return {
|
|
4847
|
+
kind: "insert",
|
|
4848
|
+
text,
|
|
4849
|
+
length: text.length
|
|
4850
|
+
};
|
|
4851
|
+
}
|
|
4852
|
+
return {
|
|
4853
|
+
kind: c.kind,
|
|
4854
|
+
length: c.length - this._consumed
|
|
4855
|
+
};
|
|
4856
|
+
}
|
|
4857
|
+
/** Consumes up to `length` characters of the current component and returns how many it consumed. */
|
|
4858
|
+
take(length) {
|
|
4859
|
+
const c = this._components[this._index];
|
|
4860
|
+
if (c === void 0) return length;
|
|
4861
|
+
const available = (c.kind === "insert" ? c.text.length : c.length) - this._consumed;
|
|
4862
|
+
if (length >= available) {
|
|
4863
|
+
this._index++;
|
|
4864
|
+
this._consumed = 0;
|
|
4865
|
+
return available;
|
|
4866
|
+
}
|
|
4867
|
+
this._consumed += length;
|
|
4868
|
+
return length;
|
|
4869
|
+
}
|
|
4870
|
+
/** Consumes the rest of the current insert. */
|
|
4871
|
+
takeText() {
|
|
4872
|
+
const c = this._components[this._index];
|
|
4873
|
+
if (c?.kind !== "insert") throw new Error("ComponentCursor.takeText: not at an insert");
|
|
4874
|
+
const text = c.text.slice(this._consumed);
|
|
4875
|
+
this._index++;
|
|
4876
|
+
this._consumed = 0;
|
|
4877
|
+
return text;
|
|
4878
|
+
}
|
|
4879
|
+
};
|
|
4880
|
+
function componentsEqual(a, b) {
|
|
4881
|
+
if (a.kind === "insert" || b.kind === "insert") return a.kind === b.kind && a.text === b.text;
|
|
4882
|
+
return a.kind === b.kind && a.length === b.length;
|
|
4883
|
+
}
|
|
4884
|
+
function assertLength(length) {
|
|
4885
|
+
if (!Number.isInteger(length) || length < 0) throw new Error(`TextOperation: invalid length ${length}`);
|
|
4886
|
+
}
|
|
4887
|
+
const identityOperation = new TextOperationBuilder().build();
|
|
4888
|
+
//#endregion
|
|
4889
|
+
//#region src/sync/textDiff.ts
|
|
4890
|
+
/**
|
|
4891
|
+
* An operation that turns `oldText` into `newText` by replacing everything
|
|
4892
|
+
* between their common prefix and suffix. Linear time, so it stays fast for
|
|
4893
|
+
* large texts; separate changes become one replacement of the text between them.
|
|
4894
|
+
*/
|
|
4895
|
+
function diffText(oldText, newText) {
|
|
4896
|
+
const shorter = Math.min(oldText.length, newText.length);
|
|
4897
|
+
let start = 0;
|
|
4898
|
+
while (start < shorter && oldText.charCodeAt(start) === newText.charCodeAt(start)) start++;
|
|
4899
|
+
let oldEnd = oldText.length;
|
|
4900
|
+
let newEnd = newText.length;
|
|
4901
|
+
while (oldEnd > start && newEnd > start && oldText.charCodeAt(oldEnd - 1) === newText.charCodeAt(newEnd - 1)) {
|
|
4902
|
+
oldEnd--;
|
|
4903
|
+
newEnd--;
|
|
4904
|
+
}
|
|
4905
|
+
return new TextOperationBuilder().retain(start).insert(newText.slice(start, newEnd)).delete(oldEnd - start).build();
|
|
4906
|
+
}
|
|
4907
|
+
//#endregion
|
|
4908
|
+
//#region src/sync/contentOperation.ts
|
|
4909
|
+
/** {@link OtType} for lists of {@link ContentEdit}s, as exchanged by web editors. */
|
|
4910
|
+
const contentEditsOt = { transform: (winner, loser) => transformContentEdits(winner, loser) };
|
|
4911
|
+
/**
|
|
4912
|
+
* Transforms two concurrent lists of {@link ContentEdit}s made on the same
|
|
4913
|
+
* content, so that each list applies after the other and both orders give the
|
|
4914
|
+
* same content.
|
|
4915
|
+
*
|
|
4916
|
+
* Edits at unrelated paths do not interact. String edits at the same path are
|
|
4917
|
+
* merged with {@link TextOperation.transform}. A `replace` overrides every
|
|
4918
|
+
* concurrent edit at or below its path; of two replacements at the same path,
|
|
4919
|
+
* `winner`'s is kept.
|
|
4920
|
+
*
|
|
4921
|
+
* @returns `[winner', loser']`: `winner'` applies after `loser`, `loser'` after `winner`.
|
|
4922
|
+
*/
|
|
4923
|
+
function transformContentEdits(winner, loser) {
|
|
4924
|
+
const winnerPrime = [];
|
|
4925
|
+
let loserPrime = [...loser];
|
|
4926
|
+
for (const edit of winner) {
|
|
4927
|
+
let current = edit;
|
|
4928
|
+
const nextLoser = [];
|
|
4929
|
+
for (const other of loserPrime) {
|
|
4930
|
+
if (current === void 0) {
|
|
4931
|
+
nextLoser.push(other);
|
|
4932
|
+
continue;
|
|
4933
|
+
}
|
|
4934
|
+
const [w, l] = transformContentEdit(current, other);
|
|
4935
|
+
current = w;
|
|
4936
|
+
if (l !== void 0) nextLoser.push(l);
|
|
4937
|
+
}
|
|
4938
|
+
loserPrime = nextLoser;
|
|
4939
|
+
if (current !== void 0) winnerPrime.push(current);
|
|
4940
|
+
}
|
|
4941
|
+
return [winnerPrime, loserPrime];
|
|
4942
|
+
}
|
|
4943
|
+
/** `undefined` stands for an edit that has no effect anymore. */
|
|
4944
|
+
function transformContentEdit(winner, loser) {
|
|
4945
|
+
const relation = relatePaths(winner.path, loser.path);
|
|
4946
|
+
if (relation === "unrelated") return [winner, loser];
|
|
4947
|
+
if (winner.kind === "replace" && loser.kind === "replace") return relation === "descendant" ? [void 0, loser] : [winner, void 0];
|
|
4948
|
+
if (winner.kind === "replace") return [winner, void 0];
|
|
4949
|
+
if (loser.kind === "replace") return [void 0, loser];
|
|
4950
|
+
if (relation !== "equal") return [winner, loser];
|
|
4951
|
+
const [w, l] = TextOperation.transform(TextOperation.fromTextEdits(winner.stringEdits), TextOperation.fromTextEdits(loser.stringEdits));
|
|
4952
|
+
return [toStringEdits(winner.path, w), toStringEdits(loser.path, l)];
|
|
4953
|
+
}
|
|
4954
|
+
/** How path `a` relates to path `b`: `"ancestor"` means `a` is a proper prefix of `b`. */
|
|
4955
|
+
function relatePaths(a, b) {
|
|
4956
|
+
const length = Math.min(a.length, b.length);
|
|
4957
|
+
for (let i = 0; i < length; i++) if (a[i] !== b[i]) return "unrelated";
|
|
4958
|
+
return a.length === b.length ? "equal" : a.length < b.length ? "ancestor" : "descendant";
|
|
4959
|
+
}
|
|
4960
|
+
function toStringEdits(path, operation) {
|
|
4961
|
+
return operation.isIdentity ? void 0 : {
|
|
4962
|
+
kind: "stringEdits",
|
|
4963
|
+
path,
|
|
4964
|
+
stringEdits: operation.toTextEdits()
|
|
4965
|
+
};
|
|
4966
|
+
}
|
|
4967
|
+
/**
|
|
4968
|
+
* Where `offset` in the string at `path` ends up after `edits`, or `undefined`
|
|
4969
|
+
* if the string was replaced. See {@link TextOperation.mapOffset} for `assoc`.
|
|
4970
|
+
*/
|
|
4971
|
+
function mapContentOffset(edits, path, offset, assoc = "after") {
|
|
4972
|
+
let result = offset;
|
|
4973
|
+
for (const edit of edits) {
|
|
4974
|
+
const relation = relatePaths(edit.path, path);
|
|
4975
|
+
if (relation === "unrelated" || relation === "descendant") continue;
|
|
4976
|
+
if (edit.kind === "replace") return;
|
|
4977
|
+
if (relation === "equal") result = TextOperation.fromTextEdits(edit.stringEdits).mapOffset(result, assoc);
|
|
4978
|
+
}
|
|
4979
|
+
return result;
|
|
4980
|
+
}
|
|
4981
|
+
/** Maps `offset` through each list of edits in turn; `undefined` if `operations` is `undefined` or the offset does not survive an edit. */
|
|
4982
|
+
function mapThrough(operations, path, offset, assoc) {
|
|
4983
|
+
if (!operations) return;
|
|
4984
|
+
let result = offset;
|
|
4985
|
+
for (const edits of operations) {
|
|
4986
|
+
result = mapContentOffset(edits, path, result, assoc);
|
|
4987
|
+
if (result === void 0) return;
|
|
4988
|
+
}
|
|
4989
|
+
return result;
|
|
4990
|
+
}
|
|
4991
|
+
/**
|
|
4992
|
+
* Edits that turn `oldValue` into `newValue`, as fine-grained as possible so
|
|
4993
|
+
* that they merge with concurrent edits: changed strings become string edits,
|
|
4994
|
+
* objects with the same keys and arrays with the same length are compared
|
|
4995
|
+
* member by member, and anything else is replaced.
|
|
4996
|
+
*/
|
|
4997
|
+
function diffContent(oldValue, newValue, path = []) {
|
|
4998
|
+
if (typeof oldValue === "string" && typeof newValue === "string") {
|
|
4999
|
+
const operation = diffText(oldValue, newValue);
|
|
5000
|
+
return operation.isIdentity ? [] : [{
|
|
5001
|
+
kind: "stringEdits",
|
|
5002
|
+
path,
|
|
5003
|
+
stringEdits: operation.toTextEdits()
|
|
5004
|
+
}];
|
|
5005
|
+
}
|
|
5006
|
+
if (Array.isArray(oldValue) && Array.isArray(newValue) && oldValue.length === newValue.length) return oldValue.flatMap((item, i) => diffContent(item, newValue[i], [...path, String(i)]));
|
|
5007
|
+
if (isPlainObject(oldValue) && isPlainObject(newValue) && haveSameKeys(oldValue, newValue)) return Object.keys(oldValue).flatMap((key) => diffContent(oldValue[key], newValue[key], [...path, key]));
|
|
5008
|
+
return oldValue === newValue ? [] : [{
|
|
5009
|
+
kind: "replace",
|
|
5010
|
+
path,
|
|
5011
|
+
newValue
|
|
5012
|
+
}];
|
|
5013
|
+
}
|
|
5014
|
+
function isPlainObject(value) {
|
|
5015
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
5016
|
+
}
|
|
5017
|
+
function haveSameKeys(a, b) {
|
|
5018
|
+
const keys = Object.keys(a);
|
|
5019
|
+
return keys.length === Object.keys(b).length && keys.every((key) => Object.hasOwn(b, key));
|
|
5020
|
+
}
|
|
5021
|
+
//#endregion
|
|
4374
5022
|
//#region src/host/WebEditorHost.ts
|
|
4375
5023
|
/**
|
|
4376
5024
|
* Host-side façade for the web-editor v0.12 protocol.
|
|
4377
5025
|
*
|
|
4378
5026
|
* Owns the canonical text content. Translates between text/JSON form and the
|
|
4379
|
-
* structural edits exchanged with the editor.
|
|
4380
|
-
*
|
|
5027
|
+
* structural edits exchanged with the editor.
|
|
5028
|
+
*
|
|
5029
|
+
* Concurrent edits from both sides are merged with operational transformation
|
|
5030
|
+
* when the editor supports it ({@link OT_EDIT_TRANSFORM}). Otherwise the host
|
|
5031
|
+
* maintains the revision pair required by the client-authority conflict
|
|
5032
|
+
* policy, under which conflicting edits are dropped.
|
|
4381
5033
|
*/
|
|
4382
5034
|
var WebEditorHost = class {
|
|
4383
5035
|
_connection;
|
|
4384
5036
|
_editor;
|
|
5037
|
+
_clientTransport;
|
|
5038
|
+
_presentation;
|
|
4385
5039
|
_contentType;
|
|
4386
5040
|
_jsonFormat;
|
|
4387
5041
|
_onError;
|
|
4388
5042
|
_capabilities;
|
|
5043
|
+
_hostTransportSubscription;
|
|
4389
5044
|
_text;
|
|
4390
5045
|
_readOnly;
|
|
5046
|
+
_presentationMode;
|
|
5047
|
+
/** Set while connected to an editor that merges concurrent edits. */
|
|
5048
|
+
_transform;
|
|
5049
|
+
/** Client-authority policy only. */
|
|
4391
5050
|
_serverRevision = 0;
|
|
5051
|
+
/** Client-authority policy only. */
|
|
4392
5052
|
_acknowledgedClientRevision = 0;
|
|
4393
5053
|
_editorInitialized = false;
|
|
4394
5054
|
_editorCapabilities;
|
|
5055
|
+
_pendingHostTransportMessages = [];
|
|
4395
5056
|
_onDidChangeText = new Emitter();
|
|
5057
|
+
/** The editor changed the content. `edits` apply to the previous content, in the editor's form. */
|
|
4396
5058
|
onDidChangeText = this._onDidChangeText.event;
|
|
4397
5059
|
_onDidInitialize = new Emitter();
|
|
4398
5060
|
onDidInitialize = this._onDidInitialize.event;
|
|
4399
5061
|
_onDidReportSize = new Emitter();
|
|
4400
5062
|
onDidReportSize = this._onDidReportSize.event;
|
|
5063
|
+
_onDidSetPresentationPreferences = new Emitter();
|
|
5064
|
+
/** The editor replaced its presentation preferences. Only fires with `supportsPresentation`. */
|
|
5065
|
+
onDidSetPresentationPreferences = this._onDidSetPresentationPreferences.event;
|
|
5066
|
+
_onDidRequestExitFullView = new Emitter();
|
|
5067
|
+
/** The editor asked to leave full view. Only fires with `supportsPresentation`. */
|
|
5068
|
+
onDidRequestExitFullView = this._onDidRequestExitFullView.event;
|
|
4401
5069
|
constructor(options) {
|
|
4402
5070
|
this._connection = options.connection;
|
|
4403
5071
|
this._contentType = options.contentType ?? "text";
|
|
@@ -4405,8 +5073,14 @@ var WebEditorHost = class {
|
|
|
4405
5073
|
this._readOnly = options.readOnly ?? false;
|
|
4406
5074
|
this._jsonFormat = options.jsonFormat ?? {};
|
|
4407
5075
|
this._onError = options.onError ?? ((m, d) => console.error("WebEditorHost:", m, d));
|
|
4408
|
-
this._capabilities =
|
|
5076
|
+
this._capabilities = {
|
|
5077
|
+
...options.capabilities ?? { supportsForceUpdate: true },
|
|
5078
|
+
supportsHostTransport: options.hostTransport ? true : void 0,
|
|
5079
|
+
supportsPresentation: options.supportsPresentation ? true : void 0
|
|
5080
|
+
};
|
|
4409
5081
|
this._editor = this._connection.get(webEditorInterface);
|
|
5082
|
+
this._clientTransport = this._connection.get(webEditorClientTransportInterface);
|
|
5083
|
+
this._presentation = this._connection.get(webEditorPresentationInterface);
|
|
4410
5084
|
this._connection.register(webEditorHostInterface, {
|
|
4411
5085
|
initialized: (params) => {
|
|
4412
5086
|
if (params.contentType !== this._contentType) this._onError(`Editor contentType "${params.contentType}" does not match host "${this._contentType}"`);
|
|
@@ -4414,14 +5088,24 @@ var WebEditorHost = class {
|
|
|
4414
5088
|
this._editorInitialized = true;
|
|
4415
5089
|
this._serverRevision = 0;
|
|
4416
5090
|
this._acknowledgedClientRevision = 0;
|
|
5091
|
+
this._transform = params.capabilities?.editTransforms?.includes("ot/1") ? new OtPeer(contentEditsOt, "leader") : void 0;
|
|
4417
5092
|
this._pushFullUpdate();
|
|
5093
|
+
for (const message of this._pendingHostTransportMessages.splice(0)) this._clientTransport.message({ message });
|
|
5094
|
+
this._pushPresentationMode();
|
|
4418
5095
|
this._onDidInitialize.fire({ capabilities: params.capabilities });
|
|
4419
5096
|
return {
|
|
4420
5097
|
protocolVersion: "web-editor-host/0.12",
|
|
4421
|
-
capabilities: this.
|
|
5098
|
+
capabilities: this._transform ? {
|
|
5099
|
+
...this._capabilities,
|
|
5100
|
+
editTransform: OT_EDIT_TRANSFORM
|
|
5101
|
+
} : this._capabilities
|
|
4422
5102
|
};
|
|
4423
5103
|
},
|
|
4424
5104
|
applyContentEdit: (params) => {
|
|
5105
|
+
if (this._transform) {
|
|
5106
|
+
this._applyTransformedEditorEdit(this._transform, params);
|
|
5107
|
+
return;
|
|
5108
|
+
}
|
|
4425
5109
|
if (this._readOnly) {
|
|
4426
5110
|
this._onError("Editor sent applyContentEdit while readOnly");
|
|
4427
5111
|
this._acknowledgeAndRestore(params.clientRevision);
|
|
@@ -4432,46 +5116,88 @@ var WebEditorHost = class {
|
|
|
4432
5116
|
this._acknowledgeAndRestore(params.clientRevision);
|
|
4433
5117
|
return;
|
|
4434
5118
|
}
|
|
4435
|
-
|
|
4436
|
-
|
|
4437
|
-
newValue = applyContentEdits(encodeForEditor(this._text, this._contentType), params.edits);
|
|
4438
|
-
} catch (e) {
|
|
4439
|
-
this._onError("Failed to apply content edits from editor", e);
|
|
4440
|
-
this._acknowledgeAndRestore(params.clientRevision);
|
|
4441
|
-
return;
|
|
4442
|
-
}
|
|
4443
|
-
let newText;
|
|
4444
|
-
try {
|
|
4445
|
-
newText = decodeFromEditor(newValue, this._contentType, this._jsonFormat);
|
|
4446
|
-
} catch (e) {
|
|
4447
|
-
this._onError("Failed to decode editor content", e);
|
|
5119
|
+
const newText = this._applyEditorEdits(params.edits);
|
|
5120
|
+
if (newText === void 0) {
|
|
4448
5121
|
this._acknowledgeAndRestore(params.clientRevision);
|
|
4449
5122
|
return;
|
|
4450
5123
|
}
|
|
4451
5124
|
this._acknowledgedClientRevision = Math.max(this._acknowledgedClientRevision, params.clientRevision);
|
|
4452
5125
|
if (newText !== this._text) {
|
|
4453
5126
|
this._text = newText;
|
|
4454
|
-
this._onDidChangeText.fire({
|
|
5127
|
+
this._onDidChangeText.fire({
|
|
5128
|
+
text: newText,
|
|
5129
|
+
edits: params.edits
|
|
5130
|
+
});
|
|
4455
5131
|
}
|
|
4456
5132
|
this._pushAcknowledgement();
|
|
4457
5133
|
},
|
|
5134
|
+
acknowledge: (params) => {
|
|
5135
|
+
try {
|
|
5136
|
+
this._transform?.acknowledge(params.serverRevision);
|
|
5137
|
+
} catch (e) {
|
|
5138
|
+
this._onError("Editor acknowledged edits the host did not send", e);
|
|
5139
|
+
}
|
|
5140
|
+
},
|
|
4458
5141
|
reportSize: (params) => {
|
|
4459
5142
|
this._onDidReportSize.fire({ height: params.height });
|
|
4460
5143
|
}
|
|
4461
5144
|
});
|
|
5145
|
+
if (options.supportsPresentation) this._connection.register(webEditorPresentationHostInterface, {
|
|
5146
|
+
setPreferences: (preferences) => this._onDidSetPresentationPreferences.fire(preferences),
|
|
5147
|
+
exitFullView: () => this._onDidRequestExitFullView.fire()
|
|
5148
|
+
});
|
|
5149
|
+
if (options.hostTransport) {
|
|
5150
|
+
this._connection.register(webEditorHostTransportInterface, { message: ({ message }) => options.hostTransport.sendMessage(message) });
|
|
5151
|
+
this._hostTransportSubscription = options.hostTransport.onMessage((message) => {
|
|
5152
|
+
if (this._editorInitialized) this._clientTransport.message({ message });
|
|
5153
|
+
else this._pendingHostTransportMessages.push(message);
|
|
5154
|
+
});
|
|
5155
|
+
}
|
|
4462
5156
|
}
|
|
4463
5157
|
/** The text the host believes the document currently holds. */
|
|
4464
5158
|
getText() {
|
|
4465
5159
|
return this._text;
|
|
4466
5160
|
}
|
|
5161
|
+
/** The current content's version, to send along with offsets into it. */
|
|
5162
|
+
get version() {
|
|
5163
|
+
return this._transform ? {
|
|
5164
|
+
serverRevision: this._transform.localRevision,
|
|
5165
|
+
clientRevision: this._transform.remoteRevision
|
|
5166
|
+
} : {
|
|
5167
|
+
serverRevision: this._serverRevision,
|
|
5168
|
+
clientRevision: this._acknowledgedClientRevision
|
|
5169
|
+
};
|
|
5170
|
+
}
|
|
5171
|
+
/**
|
|
5172
|
+
* Maps an offset into the string at `path`, which the editor computed on
|
|
5173
|
+
* its content at `version`, into the current content. Call it when the
|
|
5174
|
+
* editor's message arrives: its earlier edits are integrated by then.
|
|
5175
|
+
*
|
|
5176
|
+
* @returns `undefined` if the offset cannot be mapped, for example because
|
|
5177
|
+
* the string was replaced or the content was reset since.
|
|
5178
|
+
*/
|
|
5179
|
+
mapOffset(version, path, offset, assoc) {
|
|
5180
|
+
if (!this._transform) {
|
|
5181
|
+
const current = this.version;
|
|
5182
|
+
return version.serverRevision === current.serverRevision && version.clientRevision === current.clientRevision ? offset : void 0;
|
|
5183
|
+
}
|
|
5184
|
+
if (version.clientRevision !== this._transform.remoteRevision) return;
|
|
5185
|
+
return mapThrough(this._transform.operationsAfter(version.serverRevision), path, offset, assoc);
|
|
5186
|
+
}
|
|
4467
5187
|
/**
|
|
4468
|
-
*
|
|
4469
|
-
*
|
|
5188
|
+
* Change the text the editor shows.
|
|
5189
|
+
*
|
|
5190
|
+
* When the editor merges concurrent edits, the change is sent as edits
|
|
5191
|
+
* that merge with the editor's unsent edits. Otherwise, and with `force`,
|
|
5192
|
+
* the full text is sent. If `force`, the editor must adopt it even with
|
|
5193
|
+
* pending local edits, which are then discarded.
|
|
4470
5194
|
*/
|
|
4471
5195
|
setText(text, opts = {}) {
|
|
4472
5196
|
if (text === this._text && !opts.force) return;
|
|
5197
|
+
const edits = this._transform && !opts.force ? this._diff(this._text, text) : void 0;
|
|
4473
5198
|
this._text = text;
|
|
4474
|
-
this.
|
|
5199
|
+
if (edits) this._sendHostEdits(edits);
|
|
5200
|
+
else this._pushFullUpdate(opts.force);
|
|
4475
5201
|
}
|
|
4476
5202
|
/**
|
|
4477
5203
|
* Reassign this host/editor connection to another logical document.
|
|
@@ -4486,43 +5212,128 @@ var WebEditorHost = class {
|
|
|
4486
5212
|
getReadOnly() {
|
|
4487
5213
|
return this._readOnly;
|
|
4488
5214
|
}
|
|
5215
|
+
/**
|
|
5216
|
+
* Tell the editor how the host presents it. Sent to editors that support
|
|
5217
|
+
* presentation once they initialized, and again after they reconnect.
|
|
5218
|
+
*/
|
|
5219
|
+
setPresentationMode(mode) {
|
|
5220
|
+
if (this._presentationMode === mode) return;
|
|
5221
|
+
this._presentationMode = mode;
|
|
5222
|
+
this._pushPresentationMode();
|
|
5223
|
+
}
|
|
4489
5224
|
setReadOnly(readOnly) {
|
|
4490
5225
|
if (this._readOnly === readOnly) return;
|
|
4491
5226
|
this._readOnly = readOnly;
|
|
4492
5227
|
if (this._editorInitialized) this._editor.update({
|
|
4493
5228
|
readOnly,
|
|
4494
|
-
acknowledgedClientRevision: this._acknowledgedClientRevision
|
|
5229
|
+
acknowledgedClientRevision: this._transform?.remoteRevision ?? this._acknowledgedClientRevision
|
|
4495
5230
|
});
|
|
4496
5231
|
}
|
|
4497
5232
|
/**
|
|
4498
|
-
*
|
|
4499
|
-
*
|
|
5233
|
+
* Change the text by fine-grained edits, which are pushed to the editor.
|
|
5234
|
+
* An editor that initializes later receives the changed text.
|
|
4500
5235
|
*/
|
|
4501
5236
|
applyEdits(edits) {
|
|
4502
|
-
if (edits.length === 0
|
|
4503
|
-
let newValue;
|
|
5237
|
+
if (edits.length === 0) return;
|
|
4504
5238
|
try {
|
|
4505
|
-
newValue = applyContentEdits(encodeForEditor(this._text, this._contentType), edits);
|
|
5239
|
+
const newValue = applyContentEdits(encodeForEditor(this._text, this._contentType), edits);
|
|
4506
5240
|
this._text = decodeFromEditor(newValue, this._contentType, this._jsonFormat);
|
|
4507
5241
|
} catch (e) {
|
|
4508
5242
|
this._onError("Failed to apply outgoing edits to local text", e);
|
|
4509
5243
|
return;
|
|
4510
5244
|
}
|
|
4511
|
-
this.
|
|
4512
|
-
this._editor.applyContentEdits({
|
|
4513
|
-
edits,
|
|
4514
|
-
serverRevision: this._serverRevision,
|
|
4515
|
-
acknowledgedClientRevision: this._acknowledgedClientRevision
|
|
4516
|
-
});
|
|
5245
|
+
this._sendHostEdits(edits);
|
|
4517
5246
|
}
|
|
4518
5247
|
dispose() {
|
|
5248
|
+
this._hostTransportSubscription?.dispose();
|
|
4519
5249
|
this._onDidChangeText.dispose();
|
|
4520
5250
|
this._onDidInitialize.dispose();
|
|
4521
5251
|
this._onDidReportSize.dispose();
|
|
5252
|
+
this._onDidSetPresentationPreferences.dispose();
|
|
5253
|
+
this._onDidRequestExitFullView.dispose();
|
|
5254
|
+
this._pendingHostTransportMessages.length = 0;
|
|
4522
5255
|
this._connection.close();
|
|
4523
5256
|
}
|
|
5257
|
+
_applyTransformedEditorEdit(transform, params) {
|
|
5258
|
+
let edits;
|
|
5259
|
+
try {
|
|
5260
|
+
edits = transform.remote(params.edits, params.clientRevision, params.basedOnServerRevision);
|
|
5261
|
+
} catch (e) {
|
|
5262
|
+
this._onError("Editor edit does not follow the edit transform protocol", e);
|
|
5263
|
+
this._resetEditor(transform, params.clientRevision);
|
|
5264
|
+
return;
|
|
5265
|
+
}
|
|
5266
|
+
if (edits === void 0) {
|
|
5267
|
+
this._pushAcknowledgement();
|
|
5268
|
+
return;
|
|
5269
|
+
}
|
|
5270
|
+
if (this._readOnly) {
|
|
5271
|
+
this._onError("Editor sent applyContentEdit while readOnly");
|
|
5272
|
+
this._resetEditor(transform);
|
|
5273
|
+
return;
|
|
5274
|
+
}
|
|
5275
|
+
const newText = this._applyEditorEdits(edits);
|
|
5276
|
+
if (newText === void 0) {
|
|
5277
|
+
this._resetEditor(transform);
|
|
5278
|
+
return;
|
|
5279
|
+
}
|
|
5280
|
+
if (newText !== this._text) {
|
|
5281
|
+
this._text = newText;
|
|
5282
|
+
this._onDidChangeText.fire({
|
|
5283
|
+
text: newText,
|
|
5284
|
+
edits
|
|
5285
|
+
});
|
|
5286
|
+
}
|
|
5287
|
+
this._pushAcknowledgement();
|
|
5288
|
+
}
|
|
5289
|
+
/** The text after the editor's `edits`, or `undefined` after reporting why they do not apply. */
|
|
5290
|
+
_applyEditorEdits(edits) {
|
|
5291
|
+
let newValue;
|
|
5292
|
+
try {
|
|
5293
|
+
newValue = applyContentEdits(encodeForEditor(this._text, this._contentType), edits);
|
|
5294
|
+
} catch (e) {
|
|
5295
|
+
this._onError("Failed to apply content edits from editor", e);
|
|
5296
|
+
return;
|
|
5297
|
+
}
|
|
5298
|
+
try {
|
|
5299
|
+
return decodeFromEditor(newValue, this._contentType, this._jsonFormat);
|
|
5300
|
+
} catch (e) {
|
|
5301
|
+
this._onError("Failed to decode editor content", e);
|
|
5302
|
+
return;
|
|
5303
|
+
}
|
|
5304
|
+
}
|
|
5305
|
+
/** Edits from `oldText` to `newText` in the editor's form, or `undefined` if either cannot be encoded. */
|
|
5306
|
+
_diff(oldText, newText) {
|
|
5307
|
+
try {
|
|
5308
|
+
return diffContent(encodeForEditor(oldText, this._contentType), encodeForEditor(newText, this._contentType));
|
|
5309
|
+
} catch {
|
|
5310
|
+
return;
|
|
5311
|
+
}
|
|
5312
|
+
}
|
|
5313
|
+
_sendHostEdits(edits) {
|
|
5314
|
+
if (edits.length === 0 || !this._editorInitialized) return;
|
|
5315
|
+
if (this._transform) {
|
|
5316
|
+
const serverRevision = this._transform.local(edits);
|
|
5317
|
+
this._editor.applyContentEdits({
|
|
5318
|
+
edits: [...edits],
|
|
5319
|
+
serverRevision,
|
|
5320
|
+
acknowledgedClientRevision: this._transform.remoteRevision
|
|
5321
|
+
});
|
|
5322
|
+
return;
|
|
5323
|
+
}
|
|
5324
|
+
this._serverRevision++;
|
|
5325
|
+
this._editor.applyContentEdits({
|
|
5326
|
+
edits,
|
|
5327
|
+
serverRevision: this._serverRevision,
|
|
5328
|
+
acknowledgedClientRevision: this._acknowledgedClientRevision
|
|
5329
|
+
});
|
|
5330
|
+
}
|
|
4524
5331
|
_pushFullUpdate(force) {
|
|
4525
5332
|
if (!this._editorInitialized) return;
|
|
5333
|
+
if (this._transform) {
|
|
5334
|
+
this._resetEditor(this._transform);
|
|
5335
|
+
return;
|
|
5336
|
+
}
|
|
4526
5337
|
this._serverRevision++;
|
|
4527
5338
|
let content;
|
|
4528
5339
|
try {
|
|
@@ -4539,13 +5350,43 @@ var WebEditorHost = class {
|
|
|
4539
5350
|
force
|
|
4540
5351
|
});
|
|
4541
5352
|
}
|
|
5353
|
+
/**
|
|
5354
|
+
* Sends the full content, which replaces the editor's. Editor edits made
|
|
5355
|
+
* before the editor receives it are discarded on both sides.
|
|
5356
|
+
*/
|
|
5357
|
+
_resetEditor(transform, clientRevision) {
|
|
5358
|
+
let content;
|
|
5359
|
+
try {
|
|
5360
|
+
content = encodeForEditor(this._text, this._contentType);
|
|
5361
|
+
} catch (e) {
|
|
5362
|
+
this._onError("Failed to encode text for editor", e);
|
|
5363
|
+
return;
|
|
5364
|
+
}
|
|
5365
|
+
const serverRevision = transform.reset(clientRevision);
|
|
5366
|
+
this._editor.update({
|
|
5367
|
+
content,
|
|
5368
|
+
readOnly: this._readOnly,
|
|
5369
|
+
serverRevision,
|
|
5370
|
+
acknowledgedClientRevision: transform.remoteRevision,
|
|
5371
|
+
force: true,
|
|
5372
|
+
editTransform: OT_EDIT_TRANSFORM
|
|
5373
|
+
});
|
|
5374
|
+
}
|
|
4542
5375
|
_pushAcknowledgement() {
|
|
4543
5376
|
if (!this._editorInitialized) return;
|
|
5377
|
+
if (this._transform) {
|
|
5378
|
+
this._editor.update({ acknowledgedClientRevision: this._transform.remoteRevision });
|
|
5379
|
+
return;
|
|
5380
|
+
}
|
|
4544
5381
|
this._editor.update({
|
|
4545
5382
|
serverRevision: this._serverRevision,
|
|
4546
5383
|
acknowledgedClientRevision: this._acknowledgedClientRevision
|
|
4547
5384
|
});
|
|
4548
5385
|
}
|
|
5386
|
+
_pushPresentationMode() {
|
|
5387
|
+
if (!this._editorInitialized || !this._capabilities.supportsPresentation || !this._editorCapabilities?.supportsPresentation || this._presentationMode === void 0) return;
|
|
5388
|
+
this._presentation.changed({ mode: this._presentationMode });
|
|
5389
|
+
}
|
|
4549
5390
|
_acknowledgeAndRestore(clientRevision) {
|
|
4550
5391
|
this._acknowledgedClientRevision = Math.max(this._acknowledgedClientRevision, clientRevision);
|
|
4551
5392
|
this._pushFullUpdate(true);
|
|
@@ -4645,7 +5486,7 @@ var HostConnection = class HostConnection {
|
|
|
4645
5486
|
const result = await signed.connection.get(hubAccessInterface).requestAccess({
|
|
4646
5487
|
consumer: {
|
|
4647
5488
|
...req.consumer,
|
|
4648
|
-
principal: signed.principal.identity.principal
|
|
5489
|
+
principal: signed.principal.identity.publicSigningIdentity.principal
|
|
4649
5490
|
},
|
|
4650
5491
|
permissions,
|
|
4651
5492
|
duration: req.duration
|
|
@@ -4693,27 +5534,52 @@ var HostConnection = class HostConnection {
|
|
|
4693
5534
|
* Calls `host.initialized` immediately, then exposes the current content and
|
|
4694
5535
|
* read-only flag as observable events. Local edits made through {@link applyEdits}
|
|
4695
5536
|
* are sent to the host with monotonically increasing client revisions.
|
|
5537
|
+
*
|
|
5538
|
+
* When the host supports it ({@link OT_EDIT_TRANSFORM}), host edits that
|
|
5539
|
+
* arrive while local edits are unacknowledged are transformed against them,
|
|
5540
|
+
* so neither side's edits are lost. Otherwise such host changes are dropped
|
|
5541
|
+
* (client-authority policy).
|
|
4696
5542
|
*/
|
|
4697
5543
|
var WebEditorClient = class WebEditorClient {
|
|
4698
5544
|
_connection;
|
|
4699
5545
|
_host;
|
|
5546
|
+
_hostTransportProxy;
|
|
5547
|
+
_presentationHost;
|
|
4700
5548
|
_onError;
|
|
5549
|
+
_hostTransport;
|
|
4701
5550
|
_content = void 0;
|
|
4702
5551
|
_readOnly = false;
|
|
4703
5552
|
_options = void 0;
|
|
4704
5553
|
_hostCapabilities;
|
|
4705
|
-
|
|
5554
|
+
_connected = false;
|
|
5555
|
+
_presentationMode;
|
|
5556
|
+
_presentationPreferences;
|
|
5557
|
+
_pendingHostTransportMessages = [];
|
|
5558
|
+
_hostTransportActivated = false;
|
|
5559
|
+
/** Local edits and their revisions; its pending edits are those the host has not acknowledged. */
|
|
5560
|
+
_edits = new OtPeer(contentEditsOt, "follower");
|
|
5561
|
+
/** Whether the host transforms concurrent edits, as announced with its first content. */
|
|
5562
|
+
_transform = false;
|
|
5563
|
+
/** Client-authority policy only. */
|
|
4706
5564
|
_basedOnServerRevision = 0;
|
|
4707
|
-
/** Number of un-acked local revisions. While > 0 the editor has pending edits. */
|
|
4708
|
-
_pendingLocalEdits = 0;
|
|
4709
5565
|
_onDidChangeContent = new Emitter();
|
|
5566
|
+
/**
|
|
5567
|
+
* The content changed: by a local edit, by host edits (after
|
|
5568
|
+
* {@link onDidApplyHostEdits}) or replaced by the host. Listeners must
|
|
5569
|
+
* tolerate content they already show.
|
|
5570
|
+
*/
|
|
4710
5571
|
onDidChangeContent = this._onDidChangeContent.event;
|
|
4711
5572
|
_onDidApplyHostEdits = new Emitter();
|
|
5573
|
+
/** Host edits were applied; `edits` are relative to the previous content, after transformation against local edits. */
|
|
4712
5574
|
onDidApplyHostEdits = this._onDidApplyHostEdits.event;
|
|
4713
5575
|
_onDidChangeReadOnly = new Emitter();
|
|
4714
5576
|
onDidChangeReadOnly = this._onDidChangeReadOnly.event;
|
|
4715
5577
|
_onDidChangeOptions = new Emitter();
|
|
4716
5578
|
onDidChangeOptions = this._onDidChangeOptions.event;
|
|
5579
|
+
_onDidChangePresentationMode = new Emitter();
|
|
5580
|
+
/** The embedding host changed how it presents this editor. See {@link getPresentationMode}. */
|
|
5581
|
+
onDidChangePresentationMode = this._onDidChangePresentationMode.event;
|
|
5582
|
+
_onDidReceiveHostMessage = new Emitter();
|
|
4717
5583
|
/** Resolves once the host has acknowledged `initialized`. */
|
|
4718
5584
|
onDidConnect;
|
|
4719
5585
|
static async connect(options) {
|
|
@@ -4725,6 +5591,8 @@ var WebEditorClient = class WebEditorClient {
|
|
|
4725
5591
|
this._connection = resolveConnection(options.connection);
|
|
4726
5592
|
this._onError = options.onError ?? ((m, d) => console.error("WebEditorClient:", m, d));
|
|
4727
5593
|
this._host = this._connection.get(webEditorHostInterface);
|
|
5594
|
+
this._hostTransportProxy = this._connection.get(webEditorHostTransportInterface);
|
|
5595
|
+
this._presentationHost = this._connection.get(webEditorPresentationHostInterface);
|
|
4728
5596
|
this._connection.register(webEditorInterface, {
|
|
4729
5597
|
update: (params) => this._handleUpdate(params),
|
|
4730
5598
|
applyContentEdits: (params) => this._handleApplyContentEdits(params),
|
|
@@ -4732,12 +5600,40 @@ var WebEditorClient = class WebEditorClient {
|
|
|
4732
5600
|
return { schema: options.getContentSchema ? await options.getContentSchema() : null };
|
|
4733
5601
|
}
|
|
4734
5602
|
});
|
|
5603
|
+
this._connection.register(webEditorPresentationInterface, { changed: ({ mode }) => {
|
|
5604
|
+
if (mode === this._presentationMode) return;
|
|
5605
|
+
this._presentationMode = mode;
|
|
5606
|
+
this._onDidChangePresentationMode.fire({ mode });
|
|
5607
|
+
} });
|
|
5608
|
+
this._connection.register(webEditorClientTransportInterface, { message: ({ message }) => {
|
|
5609
|
+
if (this._hostTransportActivated) this._onDidReceiveHostMessage.fire(message);
|
|
5610
|
+
else this._pendingHostTransportMessages.push(message);
|
|
5611
|
+
} });
|
|
4735
5612
|
this.onDidConnect = this._host.initialized({
|
|
4736
5613
|
protocolVersion: "web-editor/0.12",
|
|
4737
5614
|
contentType: options.contentType ?? "text",
|
|
4738
|
-
capabilities:
|
|
5615
|
+
capabilities: {
|
|
5616
|
+
editTransforms: [OT_EDIT_TRANSFORM],
|
|
5617
|
+
...options.capabilities,
|
|
5618
|
+
supportsPresentation: true
|
|
5619
|
+
}
|
|
4739
5620
|
}).then((r) => {
|
|
4740
5621
|
this._hostCapabilities = r.capabilities;
|
|
5622
|
+
this._transform ||= r.capabilities?.editTransform === OT_EDIT_TRANSFORM;
|
|
5623
|
+
this._connected = true;
|
|
5624
|
+
if (this._presentationPreferences) this.setPresentationPreferences(this._presentationPreferences);
|
|
5625
|
+
if (r.capabilities?.supportsHostTransport) this._hostTransport = {
|
|
5626
|
+
onMessage: (listener) => {
|
|
5627
|
+
const disposable = this._onDidReceiveHostMessage.event(listener);
|
|
5628
|
+
if (!this._hostTransportActivated) {
|
|
5629
|
+
this._hostTransportActivated = true;
|
|
5630
|
+
for (const message of this._pendingHostTransportMessages.splice(0)) listener(message);
|
|
5631
|
+
}
|
|
5632
|
+
return disposable;
|
|
5633
|
+
},
|
|
5634
|
+
sendMessage: (message) => this._hostTransportProxy.message({ message })
|
|
5635
|
+
};
|
|
5636
|
+
else this._pendingHostTransportMessages.length = 0;
|
|
4741
5637
|
return { capabilities: r.capabilities };
|
|
4742
5638
|
});
|
|
4743
5639
|
}
|
|
@@ -4749,9 +5645,36 @@ var WebEditorClient = class WebEditorClient {
|
|
|
4749
5645
|
get connection() {
|
|
4750
5646
|
return this._connection;
|
|
4751
5647
|
}
|
|
5648
|
+
/** Optional notification transport supplied by the web editor host. */
|
|
5649
|
+
get hostTransport() {
|
|
5650
|
+
return this._hostTransport;
|
|
5651
|
+
}
|
|
4752
5652
|
getContent() {
|
|
4753
5653
|
return this._content;
|
|
4754
5654
|
}
|
|
5655
|
+
/** The current content's version, to send along with offsets into it. */
|
|
5656
|
+
get version() {
|
|
5657
|
+
return {
|
|
5658
|
+
serverRevision: this._serverRevision,
|
|
5659
|
+
clientRevision: this._edits.localRevision
|
|
5660
|
+
};
|
|
5661
|
+
}
|
|
5662
|
+
/**
|
|
5663
|
+
* Maps an offset into the string at `path`, which the host computed on its
|
|
5664
|
+
* content at `version`, into the current content. Call it when the host's
|
|
5665
|
+
* message arrives.
|
|
5666
|
+
*
|
|
5667
|
+
* @returns `undefined` if the offset cannot be mapped, for example because
|
|
5668
|
+
* host edits arrived since or the string was replaced.
|
|
5669
|
+
*/
|
|
5670
|
+
mapOffset(version, path, offset, assoc) {
|
|
5671
|
+
if (version.serverRevision !== this._serverRevision) return;
|
|
5672
|
+
return mapThrough(this._edits.operationsAfter(version.clientRevision), path, offset, assoc);
|
|
5673
|
+
}
|
|
5674
|
+
/** The host revision the content is based on. */
|
|
5675
|
+
get _serverRevision() {
|
|
5676
|
+
return this._transform ? this._edits.remoteRevision : this._basedOnServerRevision;
|
|
5677
|
+
}
|
|
4755
5678
|
getReadOnly() {
|
|
4756
5679
|
return this._readOnly;
|
|
4757
5680
|
}
|
|
@@ -4773,35 +5696,59 @@ var WebEditorClient = class WebEditorClient {
|
|
|
4773
5696
|
this._onError("Failed to apply local edits", e);
|
|
4774
5697
|
return;
|
|
4775
5698
|
}
|
|
4776
|
-
this.
|
|
4777
|
-
this._pendingLocalEdits++;
|
|
5699
|
+
const clientRevision = this._edits.local(edits);
|
|
4778
5700
|
this._onDidChangeContent.fire({
|
|
4779
5701
|
content: this._content,
|
|
4780
5702
|
force: false
|
|
4781
5703
|
});
|
|
4782
5704
|
this._host.applyContentEdit({
|
|
4783
|
-
edits,
|
|
4784
|
-
clientRevision
|
|
4785
|
-
basedOnServerRevision: this.
|
|
5705
|
+
edits: [...edits],
|
|
5706
|
+
clientRevision,
|
|
5707
|
+
basedOnServerRevision: this._serverRevision
|
|
4786
5708
|
});
|
|
4787
5709
|
}
|
|
4788
5710
|
/**
|
|
4789
|
-
* Report the editor's
|
|
4790
|
-
*
|
|
5711
|
+
* Report the editor's preferred content height (px): the height it needs to
|
|
5712
|
+
* show its content without scrolling, independent of the frame's current
|
|
5713
|
+
* height. A host embedding this editor in an iframe sizes the frame to it.
|
|
4791
5714
|
*/
|
|
4792
5715
|
reportSize(height) {
|
|
4793
5716
|
this._host.reportSize({ height });
|
|
4794
5717
|
}
|
|
5718
|
+
/**
|
|
5719
|
+
* How an embedding host currently presents this editor, or `undefined` when
|
|
5720
|
+
* the host does not present it inline (for example a standalone editor).
|
|
5721
|
+
*/
|
|
5722
|
+
getPresentationMode() {
|
|
5723
|
+
return this._presentationMode;
|
|
5724
|
+
}
|
|
5725
|
+
/**
|
|
5726
|
+
* Ask a presenting host for presentation controls (resize grips, full view).
|
|
5727
|
+
* Replaces earlier preferences; sent once connected, and ignored by hosts
|
|
5728
|
+
* that do not present the editor inline.
|
|
5729
|
+
*/
|
|
5730
|
+
setPresentationPreferences(preferences) {
|
|
5731
|
+
this._presentationPreferences = preferences;
|
|
5732
|
+
if (this._connected && this._hostCapabilities?.supportsPresentation) this._presentationHost.setPreferences(preferences);
|
|
5733
|
+
}
|
|
5734
|
+
/** Ask a presenting host to leave full view. */
|
|
5735
|
+
exitFullView() {
|
|
5736
|
+
if (this._connected && this._hostCapabilities?.supportsPresentation) this._presentationHost.exitFullView({});
|
|
5737
|
+
}
|
|
4795
5738
|
dispose() {
|
|
4796
5739
|
this._onDidChangeContent.dispose();
|
|
4797
5740
|
this._onDidApplyHostEdits.dispose();
|
|
4798
5741
|
this._onDidChangeReadOnly.dispose();
|
|
4799
5742
|
this._onDidChangeOptions.dispose();
|
|
5743
|
+
this._onDidChangePresentationMode.dispose();
|
|
5744
|
+
this._onDidReceiveHostMessage.dispose();
|
|
5745
|
+
this._pendingHostTransportMessages.length = 0;
|
|
4800
5746
|
this._connection.close();
|
|
4801
5747
|
}
|
|
4802
5748
|
_handleUpdate(params) {
|
|
5749
|
+
if (params.editTransform === "ot/1") this._transform = true;
|
|
4803
5750
|
if (params.acknowledgedClientRevision !== void 0) this._acknowledge(params.acknowledgedClientRevision);
|
|
4804
|
-
if (params.serverRevision !== void 0) this._basedOnServerRevision = params.serverRevision;
|
|
5751
|
+
if (!this._transform && params.serverRevision !== void 0) this._basedOnServerRevision = params.serverRevision;
|
|
4805
5752
|
if (params.readOnly !== void 0 && params.readOnly !== this._readOnly) {
|
|
4806
5753
|
this._readOnly = params.readOnly;
|
|
4807
5754
|
this._onDidChangeReadOnly.fire({ readOnly: params.readOnly });
|
|
@@ -4810,19 +5757,35 @@ var WebEditorClient = class WebEditorClient {
|
|
|
4810
5757
|
this._options = params.options;
|
|
4811
5758
|
this._onDidChangeOptions.fire({ options: params.options });
|
|
4812
5759
|
}
|
|
4813
|
-
if (params.content
|
|
4814
|
-
|
|
5760
|
+
if (params.content === void 0) return;
|
|
5761
|
+
if (this._transform) {
|
|
5762
|
+
try {
|
|
5763
|
+
this._edits.acceptReset(params.serverRevision ?? -1);
|
|
5764
|
+
} catch (e) {
|
|
5765
|
+
this._onError("Host content does not follow the edit transform protocol", e);
|
|
5766
|
+
}
|
|
4815
5767
|
this._content = params.content;
|
|
4816
5768
|
this._onDidChangeContent.fire({
|
|
4817
5769
|
content: params.content,
|
|
4818
|
-
force:
|
|
5770
|
+
force: true
|
|
4819
5771
|
});
|
|
5772
|
+
return;
|
|
4820
5773
|
}
|
|
5774
|
+
if (this._edits.pending.length > 0 && !params.force) return;
|
|
5775
|
+
this._content = params.content;
|
|
5776
|
+
this._onDidChangeContent.fire({
|
|
5777
|
+
content: params.content,
|
|
5778
|
+
force: params.force === true
|
|
5779
|
+
});
|
|
4821
5780
|
}
|
|
4822
5781
|
_handleApplyContentEdits(params) {
|
|
5782
|
+
if (this._transform) {
|
|
5783
|
+
this._applyTransformedHostEdits(params);
|
|
5784
|
+
return;
|
|
5785
|
+
}
|
|
4823
5786
|
this._acknowledge(params.acknowledgedClientRevision);
|
|
4824
5787
|
this._basedOnServerRevision = params.serverRevision;
|
|
4825
|
-
if (this.
|
|
5788
|
+
if (this._edits.pending.length > 0) return;
|
|
4826
5789
|
try {
|
|
4827
5790
|
this._content = applyContentEdits(this._content, params.edits);
|
|
4828
5791
|
} catch (e) {
|
|
@@ -4834,10 +5797,36 @@ var WebEditorClient = class WebEditorClient {
|
|
|
4834
5797
|
content: this._content
|
|
4835
5798
|
});
|
|
4836
5799
|
}
|
|
4837
|
-
|
|
4838
|
-
|
|
4839
|
-
|
|
4840
|
-
|
|
5800
|
+
_applyTransformedHostEdits(params) {
|
|
5801
|
+
let edits;
|
|
5802
|
+
try {
|
|
5803
|
+
edits = [...this._edits.remote(params.edits, params.serverRevision, params.acknowledgedClientRevision) ?? []];
|
|
5804
|
+
} catch (e) {
|
|
5805
|
+
this._onError("Host edits do not follow the edit transform protocol", e);
|
|
5806
|
+
return;
|
|
5807
|
+
}
|
|
5808
|
+
try {
|
|
5809
|
+
this._content = applyContentEdits(this._content, edits);
|
|
5810
|
+
} catch (e) {
|
|
5811
|
+
this._onError("Failed to apply host edits", e);
|
|
5812
|
+
return;
|
|
5813
|
+
}
|
|
5814
|
+
this._host.acknowledge({ serverRevision: params.serverRevision });
|
|
5815
|
+
this._onDidApplyHostEdits.fire({
|
|
5816
|
+
edits,
|
|
5817
|
+
content: this._content
|
|
5818
|
+
});
|
|
5819
|
+
this._onDidChangeContent.fire({
|
|
5820
|
+
content: this._content,
|
|
5821
|
+
force: false
|
|
5822
|
+
});
|
|
5823
|
+
}
|
|
5824
|
+
_acknowledge(clientRevision) {
|
|
5825
|
+
try {
|
|
5826
|
+
this._edits.acknowledge(clientRevision);
|
|
5827
|
+
} catch (e) {
|
|
5828
|
+
this._onError("Host acknowledged edits the editor did not send", e);
|
|
5829
|
+
}
|
|
4841
5830
|
}
|
|
4842
5831
|
};
|
|
4843
5832
|
//#endregion
|
|
@@ -5045,8 +6034,25 @@ var VsCodeAppHostClient = class {
|
|
|
5045
6034
|
dismissable: options?.dismissable
|
|
5046
6035
|
});
|
|
5047
6036
|
}
|
|
6037
|
+
/**
|
|
6038
|
+
* This editor tab's view state, or `undefined` when none is set. VS Code
|
|
6039
|
+
* restores it with the tab (for example after a window reload); a newly
|
|
6040
|
+
* opened tab starts without one. Validate it like any other persisted
|
|
6041
|
+
* input, since it may come from an older version of the app.
|
|
6042
|
+
*/
|
|
6043
|
+
async getViewState() {
|
|
6044
|
+
return (await this._host.getViewState({})).value;
|
|
6045
|
+
}
|
|
6046
|
+
/**
|
|
6047
|
+
* Replace this editor tab's view state with a JSON value, for example
|
|
6048
|
+
* scroll positions or selections. Use per-app storage for state shared by
|
|
6049
|
+
* every tab. Rejects for values too large to persist and in embedded apps.
|
|
6050
|
+
*/
|
|
6051
|
+
async setViewState(value) {
|
|
6052
|
+
await this._host.setViewState({ value });
|
|
6053
|
+
}
|
|
5048
6054
|
};
|
|
5049
6055
|
//#endregion
|
|
5050
|
-
export { EmbeddedAppHandle, VsCodeAppHostClient, WebEditorClient, WebEditorHost, WindowMessageTransport, applyContentEdits, applyTextEdits, createWindowParentConnection, decodeFromEditor, encodeForEditor, vscodeAppEmbeddingEventsInterface, vscodeAppHostInterface, webEditorHostInterface, webEditorInterface };
|
|
6056
|
+
export { EmbeddedAppHandle, OT_EDIT_TRANSFORM, OtPeer, TextOperation, TextOperationBuilder, VsCodeAppHostClient, WebEditorClient, WebEditorHost, WindowMessageTransport, applyContentEdits, applyTextEdits, contentEditsOt, createWindowParentConnection, decodeFromEditor, diffContent, diffText, encodeForEditor, mapContentOffset, mapThrough, textOperationOt, transformContentEdits, vscodeAppEmbeddingEventsInterface, vscodeAppHostInterface, webEditorClientTransportInterface, webEditorHostInterface, webEditorHostTransportInterface, webEditorInterface, webEditorPresentationHostInterface, webEditorPresentationInterface };
|
|
5051
6057
|
|
|
5052
6058
|
//# sourceMappingURL=index.js.map
|