Pad: Simplify `setText`

pull/5330/head
Richard Hansen 2021-12-13 01:21:40 -05:00
parent 10e2b09b96
commit a6bf7816ce
1 changed files with 4 additions and 14 deletions

View File

@ -256,21 +256,11 @@ Pad.prototype.text = function () {
};
Pad.prototype.setText = async function (newText) {
// clean the new text
newText = exports.cleanText(newText);
const oldText = this.text();
// create the changeset
// 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);
}
if (!newText.endsWith('\n')) newText += '\n';
const orig = this.text();
if (newText === orig) return;
const changeset = Changeset.makeSplice(orig, 0, orig.length, newText);
await this.appendRevision(changeset);
};