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