Skip to content

Commit 9615888

Browse files
Merge pull request #328 from desmosinc/mike/rm-opqaue-snapshot-squashed
use startIndex and endIndex for restoring selection instead of opaque…
2 parents c419aec + 2bfd8e2 commit 9615888

File tree

11 files changed

+369
-280
lines changed

11 files changed

+369
-280
lines changed

docs/Api_Methods.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,13 +173,11 @@ If the cursor is before the plus this method would return:
173173
{
174174
latex: 'a+b',
175175
startIndex: 1,
176-
endIndex: 1,
177-
opaqueSnapshot: {...}
176+
endIndex: 1
178177
}
179178
```
180179

181-
You can pass the result of `.selection()` back into `.selection()` to restore a cursor / selection. This works by taking a snapshot of the selection you currently have and recording
182-
enough information to restore it within `opaqueSnapshot`. You should not peek inside of `opaqueSnapshot` or permanently store it. This is valid only for this version of MathQuill. This selection is also only valid if the MQ's latex is identical. The MQ can go through changes, but when you try to restore the selection the current latex must match the latex when the selection snapshot was created.
180+
You can pass the result of `.selection()` back into `.selection()` to restore a cursor / selection.
183181

184182
```js
185183
// this would work

src/commands/math.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -282,19 +282,19 @@ class MathCommand extends MathElement {
282282
latexRecursive(ctx: LatexContext) {
283283
this.checkCursorContextOpen(ctx);
284284

285-
ctx.latex += this.ctrlSeq || '';
285+
ctx.uncleanedLatex += this.ctrlSeq || '';
286286
this.eachChild((child) => {
287-
ctx.latex += '{';
287+
ctx.uncleanedLatex += '{';
288288

289-
let beforeLength = ctx.latex.length;
289+
let beforeLength = ctx.uncleanedLatex.length;
290290
child.latexRecursive(ctx);
291-
let afterLength = ctx.latex.length;
291+
let afterLength = ctx.uncleanedLatex.length;
292292
if (beforeLength === afterLength) {
293293
// nothing was written so we write a space
294-
ctx.latex += ' ';
294+
ctx.uncleanedLatex += ' ';
295295
}
296296

297-
ctx.latex += '}';
297+
ctx.uncleanedLatex += '}';
298298
});
299299

300300
this.checkCursorContextClose(ctx);
@@ -405,7 +405,7 @@ class MQSymbol extends MathCommand {
405405

406406
latexRecursive(ctx: LatexContext) {
407407
this.checkCursorContextOpen(ctx);
408-
ctx.latex += this.ctrlSeq || '';
408+
ctx.uncleanedLatex += this.ctrlSeq || '';
409409
this.checkCursorContextClose(ctx);
410410
}
411411
text() {

src/commands/math/LatexCommandInput.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,9 @@ CharCmds['\\'] = class LatexCommandInput extends MathCommand {
9595
latexRecursive(ctx: LatexContext) {
9696
this.checkCursorContextOpen(ctx);
9797

98-
ctx.latex += '\\';
98+
ctx.uncleanedLatex += '\\';
9999
this.getEnd(L).latexRecursive(ctx);
100-
ctx.latex += ' ';
100+
ctx.uncleanedLatex += ' ';
101101

102102
this.checkCursorContextClose(ctx);
103103
}

src/commands/math/commands.ts

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -259,9 +259,9 @@ LatexCmds.textcolor = class extends MathCommand {
259259
latexRecursive(ctx: LatexContext) {
260260
this.checkCursorContextOpen(ctx);
261261
var blocks0 = this.blocks![0];
262-
ctx.latex += '\\textcolor{' + this.color + '}{';
262+
ctx.uncleanedLatex += '\\textcolor{' + this.color + '}{';
263263
blocks0.latexRecursive(ctx);
264-
ctx.latex += '}';
264+
ctx.uncleanedLatex += '}';
265265
this.checkCursorContextClose(ctx);
266266
}
267267
parser() {
@@ -314,9 +314,9 @@ var Class = (LatexCmds['class'] = class extends MathCommand {
314314
this.checkCursorContextOpen(ctx);
315315

316316
var blocks0 = this.blocks![0];
317-
ctx.latex += '\\class{' + this.cls + '}{';
317+
ctx.uncleanedLatex += '\\class{' + this.cls + '}{';
318318
blocks0.latexRecursive(ctx);
319-
ctx.latex += '}';
319+
ctx.uncleanedLatex += '}';
320320

321321
this.checkCursorContextClose(ctx);
322322
}
@@ -546,29 +546,29 @@ class SupSub extends MathCommand {
546546
this.checkCursorContextOpen(ctx);
547547

548548
if (this.sub) {
549-
ctx.latex += '_{';
550-
const beforeLength = ctx.latex.length;
549+
ctx.uncleanedLatex += '_{';
550+
const beforeLength = ctx.uncleanedLatex.length;
551551
this.sub.latexRecursive(ctx);
552-
const afterLength = ctx.latex.length;
552+
const afterLength = ctx.uncleanedLatex.length;
553553
if (beforeLength === afterLength) {
554554
// nothing was written. so we write a space
555-
ctx.latex += ' ';
555+
ctx.uncleanedLatex += ' ';
556556
}
557557

558-
ctx.latex += '}';
558+
ctx.uncleanedLatex += '}';
559559
}
560560

561561
if (this.sup) {
562-
ctx.latex += '^{';
563-
const beforeLength = ctx.latex.length;
562+
ctx.uncleanedLatex += '^{';
563+
const beforeLength = ctx.uncleanedLatex.length;
564564
this.sup.latexRecursive(ctx);
565-
const afterLength = ctx.latex.length;
565+
const afterLength = ctx.uncleanedLatex.length;
566566
if (beforeLength === afterLength) {
567567
// nothing was written. so we write a space
568-
ctx.latex += ' ';
568+
ctx.uncleanedLatex += ' ';
569569
}
570570

571-
ctx.latex += '}';
571+
ctx.uncleanedLatex += '}';
572572
}
573573

574574
this.checkCursorContextClose(ctx);
@@ -771,25 +771,25 @@ class SummationNotation extends MathCommand {
771771
latexRecursive(ctx: LatexContext) {
772772
this.checkCursorContextOpen(ctx);
773773

774-
ctx.latex += this.ctrlSeq + '_{';
775-
let beforeLength = ctx.latex.length;
774+
ctx.uncleanedLatex += this.ctrlSeq + '_{';
775+
let beforeLength = ctx.uncleanedLatex.length;
776776
this.getEnd(L).latexRecursive(ctx);
777-
let afterLength = ctx.latex.length;
777+
let afterLength = ctx.uncleanedLatex.length;
778778
if (afterLength === beforeLength) {
779779
// nothing was written so we write a space
780-
ctx.latex += ' ';
780+
ctx.uncleanedLatex += ' ';
781781
}
782782

783-
ctx.latex += '}^{';
784-
beforeLength = ctx.latex.length;
783+
ctx.uncleanedLatex += '}^{';
784+
beforeLength = ctx.uncleanedLatex.length;
785785
this.getEnd(R).latexRecursive(ctx);
786-
afterLength = ctx.latex.length;
786+
afterLength = ctx.uncleanedLatex.length;
787787
if (beforeLength === afterLength) {
788788
// nothing was written so we write a space
789-
ctx.latex += ' ';
789+
ctx.uncleanedLatex += ' ';
790790
}
791791

792-
ctx.latex += '}';
792+
ctx.uncleanedLatex += '}';
793793
this.checkCursorContextClose(ctx);
794794
}
795795
mathspeak() {
@@ -1121,7 +1121,7 @@ class Token extends MQSymbol {
11211121
latexRecursive(ctx: LatexContext): void {
11221122
this.checkCursorContextOpen(ctx);
11231123

1124-
ctx.latex += '\\token{' + this.tokenId + '}';
1124+
ctx.uncleanedLatex += '\\token{' + this.tokenId + '}';
11251125

11261126
this.checkCursorContextClose(ctx);
11271127
}
@@ -1227,11 +1227,11 @@ class NthRoot extends SquareRoot {
12271227
latexRecursive(ctx: LatexContext) {
12281228
this.checkCursorContextOpen(ctx);
12291229

1230-
ctx.latex += '\\sqrt[';
1230+
ctx.uncleanedLatex += '\\sqrt[';
12311231
this.getEnd(L).latexRecursive(ctx);
1232-
ctx.latex += ']{';
1232+
ctx.uncleanedLatex += ']{';
12331233
this.getEnd(R).latexRecursive(ctx);
1234-
ctx.latex += '}';
1234+
ctx.uncleanedLatex += '}';
12351235

12361236
this.checkCursorContextClose(ctx);
12371237
}
@@ -1380,9 +1380,9 @@ class Bracket extends DelimsNode {
13801380
latexRecursive(ctx: LatexContext) {
13811381
this.checkCursorContextOpen(ctx);
13821382

1383-
ctx.latex += '\\left' + this.sides[L].ctrlSeq;
1383+
ctx.uncleanedLatex += '\\left' + this.sides[L].ctrlSeq;
13841384
this.getEnd(L).latexRecursive(ctx);
1385-
ctx.latex += '\\right' + this.sides[R].ctrlSeq;
1385+
ctx.uncleanedLatex += '\\right' + this.sides[R].ctrlSeq;
13861386

13871387
this.checkCursorContextClose(ctx);
13881388
}
@@ -1921,7 +1921,7 @@ class EmbedNode extends MQSymbol {
19211921
latexRecursive(ctx: LatexContext): void {
19221922
this.checkCursorContextOpen(ctx);
19231923

1924-
ctx.latex += this.latex();
1924+
ctx.uncleanedLatex += this.latex();
19251925

19261926
this.checkCursorContextClose(ctx);
19271927
}

src/commands/text.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,11 @@ class TextBlock extends MQNode {
8585

8686
var contents = this.textContents();
8787
if (contents.length > 0) {
88-
ctx.latex += this.ctrlSeq + '{';
89-
ctx.latex += contents
88+
ctx.uncleanedLatex += this.ctrlSeq + '{';
89+
ctx.uncleanedLatex += contents
9090
.replace(/\\/g, '\\backslash ')
9191
.replace(/[{}]/g, '\\$&');
92-
ctx.latex += '}';
92+
ctx.uncleanedLatex += '}';
9393
}
9494

9595
this.checkCursorContextClose(ctx);
@@ -361,7 +361,7 @@ class TextPiece extends MQNode {
361361
}
362362
latexRecursive(ctx: LatexContext) {
363363
this.checkCursorContextOpen(ctx);
364-
ctx.latex += this.textStr;
364+
ctx.uncleanedLatex += this.textStr;
365365
this.checkCursorContextClose(ctx);
366366
}
367367

@@ -509,9 +509,9 @@ class RootMathCommand extends MathCommand {
509509
}
510510
latexRecursive(ctx: LatexContext) {
511511
this.checkCursorContextOpen(ctx);
512-
ctx.latex += '$';
512+
ctx.uncleanedLatex += '$';
513513
this.getEnd(L).latexRecursive(ctx);
514-
ctx.latex += '$';
514+
ctx.uncleanedLatex += '$';
515515
this.checkCursorContextClose(ctx);
516516
}
517517
}

src/mathquill.d.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,6 @@ declare namespace MathQuill {
2323
latex: string;
2424
startIndex: number;
2525
endIndex: number;
26-
opaqueSnapshot: {
27-
uncleanedLatex: string;
28-
cursorInsertPath: string;
29-
signedSelectionSize: number;
30-
};
3126
};
3227

3328
interface BaseMathQuill {

src/publicapi.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ function getInterface(v: number): MathQuill.v3.API | MathQuill.v1.API {
346346
return this;
347347
}
348348

349-
return this.__controller.exportLatexSelection();
349+
return this.__controller.exportLatexSelection().selection;
350350
}
351351
html() {
352352
return this.__controller.root

0 commit comments

Comments
 (0)