open-collaboration-yjs
Version:
Open Collaboration Yjs integration, part of the Open Collaboration Tools project
57 lines • 2.07 kB
JavaScript
// ******************************************************************************
// Copyright 2025 TypeFox GmbH
// This program and the accompanying materials are made available under the
// terms of the MIT License, which is available in the project root.
// ******************************************************************************
export var YTextChange;
(function (YTextChange) {
function sort(changes) {
return [...changes].sort((a, b) => a.start - b.start);
}
YTextChange.sort = sort;
})(YTextChange || (YTextChange = {}));
export var YTextChangeDelta;
(function (YTextChangeDelta) {
function isInsert(delta) {
return typeof delta.insert === 'string';
}
YTextChangeDelta.isInsert = isInsert;
function isDelete(delta) {
return typeof delta.delete === 'number';
}
YTextChangeDelta.isDelete = isDelete;
function isRetain(delta) {
return typeof delta.retain === 'number';
}
YTextChangeDelta.isRetain = isRetain;
function toChanges(delta) {
const changes = [];
let index = 0;
for (const op of delta) {
if (isRetain(op)) {
index += op.retain;
}
else if (isInsert(op)) {
changes.push({
start: index,
end: index,
text: op.insert
});
}
else if (isDelete(op)) {
changes.push({
start: index,
end: index + op.delete,
text: ''
});
// Increase the index by the number of characters deleted
// In the client, every following operation will still operate on the "old code"
// So we need to adjust the index to reflect that
index += op.delete;
}
}
return changes;
}
YTextChangeDelta.toChanges = toChanges;
})(YTextChangeDelta || (YTextChangeDelta = {}));
//# sourceMappingURL=ytext-change-tracker.js.map