Pad: Simplify setText

This commit is contained in:
Richard Hansen 2021-12-13 01:21:40 -05:00
parent 10e2b09b96
commit a6bf7816ce

View file

@ -256,21 +256,11 @@ Pad.prototype.text = function () {
}; };
Pad.prototype.setText = async function (newText) { Pad.prototype.setText = async function (newText) {
// clean the new text
newText = exports.cleanText(newText); newText = exports.cleanText(newText);
if (!newText.endsWith('\n')) newText += '\n';
const oldText = this.text(); const orig = this.text();
if (newText === orig) return;
// create the changeset const changeset = Changeset.makeSplice(orig, 0, orig.length, newText);
// We want to ensure the pad still ends with a \n, but otherwise keep
// getText() and setText() consistent.
let changeset;
if (newText[newText.length - 1] === '\n') {
changeset = Changeset.makeSplice(oldText, 0, oldText.length, newText);
} else {
changeset = Changeset.makeSplice(oldText, 0, oldText.length - 1, newText);
}
await this.appendRevision(changeset); await this.appendRevision(changeset);
}; };