Skip to content

Commit a65498e

Browse files
committed
Changeset: Move SmartOpAssembler.appendOpWithText() to a standalone function
1 parent eae814f commit a65498e

File tree

2 files changed

+47
-21
lines changed

2 files changed

+47
-21
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@
4343
* Changes to the `src/static/js/Changeset.js` library:
4444
* `opIterator()`: The unused start index parameter has been removed, as has
4545
the unused `lastIndex()` method on the returned object.
46+
* `smartOpAssembler()`: The returned object's `appendOpWithText()` method is
47+
deprecated without a replacement available to plugins (if you need one,
48+
let us know and we can make the private `opsFromText()` function public).
4649
* Several functions that should have never been public are no longer
4750
exported: `applyZip()`, `assert()`, `clearOp()`, `cloneOp()`, `copyOp()`,
4851
`error()`, `followAttributes()`, `opString()`, `stringOp()`,

src/static/js/Changeset.js

Lines changed: 44 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
*/
2424

2525
const AttributePool = require('./AttributePool');
26+
const {padutils} = require('./pad_utils');
2627

2728
/**
2829
* A `[key, value]` pair of strings describing a text attribute.
@@ -230,6 +231,36 @@ const copyOp = (op1, op2 = exports.newOp()) => Object.assign(op2, op1);
230231
* @property {Function} toString -
231232
*/
232233

234+
/**
235+
* Generates operations from the given text and attributes.
236+
*
237+
* @param {('-'|'+'|'=')} opcode - The operator to use.
238+
* @param {string} text - The text to remove/add/keep.
239+
* @param {(string|Attribute[])} [attribs] - The attributes to apply to the operations. See
240+
* `makeAttribsString`.
241+
* @param {?AttributePool} [pool] - See `makeAttribsString`.
242+
* @yields {Op} One or two ops (depending on the presense of newlines) that cover the given text.
243+
* @returns {Generator<Op>}
244+
*/
245+
const opsFromText = function* (opcode, text, attribs = '', pool = null) {
246+
const op = exports.newOp(opcode);
247+
op.attribs = exports.makeAttribsString(opcode, attribs, pool);
248+
const lastNewlinePos = text.lastIndexOf('\n');
249+
if (lastNewlinePos < 0) {
250+
op.chars = text.length;
251+
op.lines = 0;
252+
yield op;
253+
} else {
254+
op.chars = lastNewlinePos + 1;
255+
op.lines = text.match(/\n/g).length;
256+
yield op;
257+
const op2 = copyOp(op);
258+
op2.chars = text.length - (lastNewlinePos + 1);
259+
op2.lines = 0;
260+
yield op2;
261+
}
262+
};
263+
233264
/**
234265
* Creates an object that allows you to append operations (type Op) and also compresses them if
235266
* possible. Like MergingOpAssembler, but able to produce conforming exportss from slightly looser
@@ -353,28 +384,17 @@ exports.smartOpAssembler = () => {
353384
/**
354385
* Generates operations from the given text and attributes.
355386
*
387+
* @deprecated Use `opsFromText` instead.
356388
* @param {('-'|'+'|'=')} opcode - The operator to use.
357389
* @param {string} text - The text to remove/add/keep.
358390
* @param {(string|Attribute[])} attribs - The attributes to apply to the operations. See
359391
* `makeAttribsString`.
360392
* @param {?AttributePool} pool - See `makeAttribsString`.
361393
*/
362394
const appendOpWithText = (opcode, text, attribs, pool) => {
363-
const op = exports.newOp(opcode);
364-
op.attribs = exports.makeAttribsString(opcode, attribs, pool);
365-
const lastNewlinePos = text.lastIndexOf('\n');
366-
if (lastNewlinePos < 0) {
367-
op.chars = text.length;
368-
op.lines = 0;
369-
append(op);
370-
} else {
371-
op.chars = lastNewlinePos + 1;
372-
op.lines = text.match(/\n/g).length;
373-
append(op);
374-
op.chars = text.length - (lastNewlinePos + 1);
375-
op.lines = 0;
376-
append(op);
377-
}
395+
padutils.warnWithStack('Changeset.smartOpAssembler().appendOpWithText() is deprecated; ' +
396+
'use opsFromText() instead.');
397+
for (const op of opsFromText(opcode, text, attribs, pool)) append(op);
378398
};
379399

380400
const toString = () => {
@@ -1450,9 +1470,12 @@ exports.makeSplice = (oldFullText, spliceStart, numRemoved, newText, optNewTextA
14501470
const newLen = oldLen + newText.length - oldText.length;
14511471

14521472
const assem = exports.smartOpAssembler();
1453-
assem.appendOpWithText('=', oldFullText.substring(0, spliceStart));
1454-
assem.appendOpWithText('-', oldText);
1455-
assem.appendOpWithText('+', newText, optNewTextAPairs, pool);
1473+
const ops = (function* () {
1474+
yield* opsFromText('=', oldFullText.substring(0, spliceStart));
1475+
yield* opsFromText('-', oldText);
1476+
yield* opsFromText('+', newText, optNewTextAPairs, pool);
1477+
})();
1478+
for (const op of ops) assem.append(op);
14561479
assem.endDocument();
14571480
return exports.pack(oldLen, newLen, assem.toString(), newText);
14581481
};
@@ -1580,7 +1603,7 @@ exports.moveOpsToNewPool = (cs, oldPool, newPool) => {
15801603
*/
15811604
exports.makeAttribution = (text) => {
15821605
const assem = exports.smartOpAssembler();
1583-
assem.appendOpWithText('+', text);
1606+
for (const op of opsFromText('+', text)) assem.append(op);
15841607
return assem.toString();
15851608
};
15861609

@@ -1839,7 +1862,7 @@ exports.builder = (oldLen) => {
18391862
* @returns {Builder} this
18401863
*/
18411864
keepText: (text, attribs, pool) => {
1842-
assem.appendOpWithText('=', text, attribs, pool);
1865+
for (const op of opsFromText('=', text, attribs, pool)) assem.append(op);
18431866
return self;
18441867
},
18451868

@@ -1852,7 +1875,7 @@ exports.builder = (oldLen) => {
18521875
* @returns {Builder} this
18531876
*/
18541877
insert: (text, attribs, pool) => {
1855-
assem.appendOpWithText('+', text, attribs, pool);
1878+
for (const op of opsFromText('+', text, attribs, pool)) assem.append(op);
18561879
charBank.append(text);
18571880
return self;
18581881
},

0 commit comments

Comments
 (0)