Skip to content

Commit 16b714d

Browse files
committed
Changeset: Move builder() logic to a new class
1 parent d19a34e commit 16b714d

File tree

8 files changed

+70
-72
lines changed

8 files changed

+70
-72
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
[citizenos/ep_image_upload#49](https://github.com/citizenos/ep_image_upload/pull/49)
3333
for an example fix.
3434
* Changes to the `src/static/js/Changeset.js` library:
35+
* The `builder()` function is deprecated; use the new `Builder` class
36+
instead.
3537
* The `newOp()` function is deprecated; use the new `Op` class instead.
3638
* The `opIterator()` function is deprecated; use the new `OpIter` class
3739
instead.

src/node/db/API.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ exports.restoreRevision = async (padID, rev) => {
539539
};
540540

541541
// create a new changeset with a helper builder object
542-
const builder = Changeset.builder(oldText.length);
542+
const builder = new Changeset.Builder(oldText.length);
543543

544544
// assemble each line into the builder
545545
eachAttribRun(atext.attribs, (start, end, attribs) => {

src/node/handler/PadMessageHandler.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -787,7 +787,7 @@ const _correctMarkersInPad = (atext, apool) => {
787787
// create changeset that removes these bad markers
788788
offset = 0;
789789

790-
const builder = Changeset.builder(text.length);
790+
const builder = new Changeset.Builder(text.length);
791791

792792
badMarkers.forEach((pos) => {
793793
builder.keepText(text.substring(offset, pos));

src/node/utils/ImportHtml.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ exports.setPadHTML = async (pad, html) => {
8181
};
8282

8383
// create a new changeset with a helper builder object
84-
const builder = Changeset.builder(1);
84+
const builder = new Changeset.Builder(1);
8585

8686
// assemble each line into the builder
8787
eachAttribRun(newAttribs, (start, end, attribs) => {

src/node/utils/padDiff.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ PadDiff.prototype._createClearAuthorship = async function (rev) {
7272
const atext = await this._pad.getInternalRevisionAText(rev);
7373

7474
// build clearAuthorship changeset
75-
const builder = Changeset.builder(atext.text.length);
75+
const builder = new Changeset.Builder(atext.text.length);
7676
builder.keepText(atext.text, [['author', '']], this._pad.pool);
7777
const changeset = builder.toString();
7878

@@ -265,7 +265,7 @@ PadDiff.prototype._createDeletionChangeset = function (cs, startAText, apool) {
265265
let curLineNextOp = new Changeset.Op('+');
266266

267267
const unpacked = Changeset.unpack(cs);
268-
const builder = Changeset.builder(unpacked.newLen);
268+
const builder = new Changeset.Builder(unpacked.newLen);
269269

270270
const consumeAttribRuns = (numChars, func /* (len, attribs, endsLine)*/) => {
271271
if ((!curLineOpIter) || (curLineOpIterLine !== curLine)) {

src/static/js/AttributeManager.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ AttributeManager.prototype = _(AttributeManager.prototype).extend({
123123
* @param attribs an array of attributes
124124
*/
125125
_setAttributesOnRangeByLine(row, startCol, endCol, attribs) {
126-
const builder = Changeset.builder(this.rep.lines.totalWidth());
126+
const builder = new Changeset.Builder(this.rep.lines.totalWidth());
127127
ChangesetUtils.buildKeepToStartOfRange(this.rep, builder, [row, startCol]);
128128
ChangesetUtils.buildKeepRange(
129129
this.rep, builder, [row, startCol], [row, endCol], attribs, this.rep.apool);
@@ -307,7 +307,7 @@ AttributeManager.prototype = _(AttributeManager.prototype).extend({
307307
*/
308308
setAttributeOnLine(lineNum, attributeName, attributeValue) {
309309
let loc = [0, 0];
310-
const builder = Changeset.builder(this.rep.lines.totalWidth());
310+
const builder = new Changeset.Builder(this.rep.lines.totalWidth());
311311
const hasMarker = this.lineHasMarker(lineNum);
312312

313313
ChangesetUtils.buildKeepRange(this.rep, builder, loc, (loc = [lineNum, 0]));
@@ -336,7 +336,7 @@ AttributeManager.prototype = _(AttributeManager.prototype).extend({
336336
* @param attributeValue if given only attributes with equal value will be removed
337337
*/
338338
removeAttributeOnLine(lineNum, attributeName, attributeValue) {
339-
const builder = Changeset.builder(this.rep.lines.totalWidth());
339+
const builder = new Changeset.Builder(this.rep.lines.totalWidth());
340340
const hasMarker = this.lineHasMarker(lineNum);
341341
let found = false;
342342

src/static/js/Changeset.js

Lines changed: 56 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1905,72 +1905,68 @@ exports.attribsAttributeValue = (attribs, key, pool) => {
19051905

19061906
/**
19071907
* Incrementally builds a Changeset.
1908-
*
1909-
* @typedef {object} Builder
1910-
* @property {Function} insert -
1911-
* @property {Function} keep -
1912-
* @property {Function} keepText -
1913-
* @property {Function} remove -
1914-
* @property {Function} toString -
19151908
*/
1909+
exports.Builder = class {
1910+
/**
1911+
* @param {number} oldLen - Old length
1912+
*/
1913+
constructor(oldLen) {
1914+
this._oldLen = oldLen;
1915+
this._ops = [];
1916+
this._charBank = exports.stringAssembler();
1917+
}
1918+
1919+
/**
1920+
* @param attribs - Either [[key1,value1],[key2,value2],...] or '*0*1...' (no pool needed in
1921+
* latter case).
1922+
*/
1923+
keep(N, L, attribs, pool) {
1924+
const o = new exports.Op('=');
1925+
o.attribs = (attribs && exports.makeAttribsString('=', attribs, pool)) || '';
1926+
o.chars = N;
1927+
o.lines = (L || 0);
1928+
this._ops.push(o);
1929+
return this;
1930+
}
1931+
1932+
keepText(text, attribs, pool) {
1933+
this._ops.push(...opsFromText('=', text, attribs, pool));
1934+
return this;
1935+
}
1936+
1937+
insert(text, attribs, pool) {
1938+
this._ops.push(...opsFromText('+', text, attribs, pool));
1939+
this._charBank.append(text);
1940+
return this;
1941+
}
1942+
1943+
remove(N, L) {
1944+
const o = new exports.Op('-');
1945+
o.attribs = '';
1946+
o.chars = N;
1947+
o.lines = (L || 0);
1948+
this._ops.push(o);
1949+
return this;
1950+
}
1951+
1952+
toString() {
1953+
let lengthChange;
1954+
const serializedOps = exports.serializeOps((function* () {
1955+
lengthChange = yield* exports.canonicalizeOps(this._ops, true);
1956+
}).call(this));
1957+
const newLen = this._oldLen + lengthChange;
1958+
return exports.pack(this._oldLen, newLen, serializedOps, this._charBank.toString());
1959+
}
1960+
};
19161961

19171962
/**
1963+
* @deprecated Use the `Builder` class instead.
19181964
* @param {number} oldLen - Old length
19191965
* @returns {Builder}
19201966
*/
19211967
exports.builder = (oldLen) => {
1922-
const ops = [];
1923-
let packed = null;
1924-
const charBank = exports.stringAssembler();
1925-
1926-
const self = {
1927-
/**
1928-
* @param attribs - Either [[key1,value1],[key2,value2],...] or '*0*1...' (no pool needed in
1929-
* latter case).
1930-
*/
1931-
keep: (N, L, attribs, pool) => {
1932-
const o = new exports.Op('=');
1933-
o.attribs = (attribs && exports.makeAttribsString('=', attribs, pool)) || '';
1934-
o.chars = N;
1935-
o.lines = (L || 0);
1936-
packed = null;
1937-
ops.push(o);
1938-
return self;
1939-
},
1940-
keepText: (text, attribs, pool) => {
1941-
packed = null;
1942-
ops.push(...opsFromText('=', text, attribs, pool));
1943-
return self;
1944-
},
1945-
insert: (text, attribs, pool) => {
1946-
packed = null;
1947-
ops.push(...opsFromText('+', text, attribs, pool));
1948-
charBank.append(text);
1949-
return self;
1950-
},
1951-
remove: (N, L) => {
1952-
const o = new exports.Op('-');
1953-
o.attribs = '';
1954-
o.chars = N;
1955-
o.lines = (L || 0);
1956-
packed = null;
1957-
ops.push(o);
1958-
return self;
1959-
},
1960-
toString: () => {
1961-
if (packed == null) {
1962-
let lengthChange;
1963-
const serializedOps = exports.serializeOps((function* () {
1964-
lengthChange = yield* exports.canonicalizeOps(ops, true);
1965-
})());
1966-
const newLen = oldLen + lengthChange;
1967-
packed = exports.pack(oldLen, newLen, serializedOps, charBank.toString());
1968-
}
1969-
return packed;
1970-
},
1971-
};
1972-
1973-
return self;
1968+
warnDeprecated('Changeset.builder() is deprecated; use the Changeset.Builder class instead');
1969+
return new exports.Builder(oldLen);
19741970
};
19751971

19761972
exports.makeAttribsString = (opcode, attribs, pool) => {
@@ -2062,7 +2058,7 @@ exports.inverse = (cs, lines, alines, pool) => {
20622058
let curLineNextOp = new exports.Op('+');
20632059

20642060
const unpacked = exports.unpack(cs);
2065-
const builder = exports.builder(unpacked.newLen);
2061+
const builder = new exports.Builder(unpacked.newLen);
20662062

20672063
const consumeAttribRuns = (numChars, func /* (len, attribs, endsLine)*/) => {
20682064
if ((!curLineOpIter) || (curLineOpIterLine !== curLine)) {

src/static/js/ace2_inner.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ function Ace2Inner(editorInfo, cssManagers) {
171171
// CCCCCCCCCCCCCCCCCCCC\n
172172
// CCCC\n
173173
// end[0]: <CCC end[1] CCC>-------\n
174-
const builder = Changeset.builder(rep.lines.totalWidth());
174+
const builder = new Changeset.Builder(rep.lines.totalWidth());
175175
ChangesetUtils.buildKeepToStartOfRange(rep, builder, start);
176176
ChangesetUtils.buildRemoveRange(rep, builder, start, end);
177177
builder.insert(newText, [
@@ -1299,7 +1299,7 @@ function Ace2Inner(editorInfo, cssManagers) {
12991299
if (shouldIndent && /[[(:{]\s*$/.exec(prevLineText)) {
13001300
theIndent += THE_TAB;
13011301
}
1302-
const cs = Changeset.builder(rep.lines.totalWidth()).keep(
1302+
const cs = new Changeset.Builder(rep.lines.totalWidth()).keep(
13031303
rep.lines.offsetOfIndex(lineNum), lineNum).insert(
13041304
theIndent, [
13051305
['author', thisAuthor],
@@ -1777,7 +1777,7 @@ function Ace2Inner(editorInfo, cssManagers) {
17771777
const spliceStartLineStart = rep.lines.offsetOfIndex(spliceStartLine);
17781778

17791779
const startBuilder = () => {
1780-
const builder = Changeset.builder(oldLen);
1780+
const builder = new Changeset.Builder(oldLen);
17811781
builder.keep(spliceStartLineStart, spliceStartLine);
17821782
builder.keep(spliceStart - spliceStartLineStart);
17831783
return builder;
@@ -2335,7 +2335,7 @@ function Ace2Inner(editorInfo, cssManagers) {
23352335

23362336
// 3-renumber every list item of the same level from the beginning, level 1
23372337
// IMPORTANT: never skip a level because there imbrication may be arbitrary
2338-
const builder = Changeset.builder(rep.lines.totalWidth());
2338+
const builder = new Changeset.Builder(rep.lines.totalWidth());
23392339
let loc = [0, 0];
23402340
const applyNumberList = (line, level) => {
23412341
// init

0 commit comments

Comments
 (0)