Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions docs/Api_Methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,13 +173,11 @@ If the cursor is before the plus this method would return:
{
latex: 'a+b',
startIndex: 1,
endIndex: 1,
opaqueSnapshot: {...}
endIndex: 1
}
```

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
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.
You can pass the result of `.selection()` back into `.selection()` to restore a cursor / selection.

```js
// this would work
Expand Down
14 changes: 7 additions & 7 deletions src/commands/math.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,19 +282,19 @@ class MathCommand extends MathElement {
latexRecursive(ctx: LatexContext) {
this.checkCursorContextOpen(ctx);

ctx.latex += this.ctrlSeq || '';
ctx.uncleanedLatex += this.ctrlSeq || '';
this.eachChild((child) => {
ctx.latex += '{';
ctx.uncleanedLatex += '{';

let beforeLength = ctx.latex.length;
let beforeLength = ctx.uncleanedLatex.length;
child.latexRecursive(ctx);
let afterLength = ctx.latex.length;
let afterLength = ctx.uncleanedLatex.length;
if (beforeLength === afterLength) {
// nothing was written so we write a space
ctx.latex += ' ';
ctx.uncleanedLatex += ' ';
}

ctx.latex += '}';
ctx.uncleanedLatex += '}';
});

this.checkCursorContextClose(ctx);
Expand Down Expand Up @@ -405,7 +405,7 @@ class MQSymbol extends MathCommand {

latexRecursive(ctx: LatexContext) {
this.checkCursorContextOpen(ctx);
ctx.latex += this.ctrlSeq || '';
ctx.uncleanedLatex += this.ctrlSeq || '';
this.checkCursorContextClose(ctx);
}
text() {
Expand Down
4 changes: 2 additions & 2 deletions src/commands/math/LatexCommandInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ CharCmds['\\'] = class LatexCommandInput extends MathCommand {
latexRecursive(ctx: LatexContext) {
this.checkCursorContextOpen(ctx);

ctx.latex += '\\';
ctx.uncleanedLatex += '\\';
this.getEnd(L).latexRecursive(ctx);
ctx.latex += ' ';
ctx.uncleanedLatex += ' ';

this.checkCursorContextClose(ctx);
}
Expand Down
60 changes: 30 additions & 30 deletions src/commands/math/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,9 +259,9 @@ LatexCmds.textcolor = class extends MathCommand {
latexRecursive(ctx: LatexContext) {
this.checkCursorContextOpen(ctx);
var blocks0 = this.blocks![0];
ctx.latex += '\\textcolor{' + this.color + '}{';
ctx.uncleanedLatex += '\\textcolor{' + this.color + '}{';
blocks0.latexRecursive(ctx);
ctx.latex += '}';
ctx.uncleanedLatex += '}';
this.checkCursorContextClose(ctx);
}
parser() {
Expand Down Expand Up @@ -314,9 +314,9 @@ var Class = (LatexCmds['class'] = class extends MathCommand {
this.checkCursorContextOpen(ctx);

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

this.checkCursorContextClose(ctx);
}
Expand Down Expand Up @@ -488,29 +488,29 @@ class SupSub extends MathCommand {
this.checkCursorContextOpen(ctx);

if (this.sub) {
ctx.latex += '_{';
const beforeLength = ctx.latex.length;
ctx.uncleanedLatex += '_{';
const beforeLength = ctx.uncleanedLatex.length;
this.sub.latexRecursive(ctx);
const afterLength = ctx.latex.length;
const afterLength = ctx.uncleanedLatex.length;
if (beforeLength === afterLength) {
// nothing was written. so we write a space
ctx.latex += ' ';
ctx.uncleanedLatex += ' ';
}

ctx.latex += '}';
ctx.uncleanedLatex += '}';
}

if (this.sup) {
ctx.latex += '^{';
const beforeLength = ctx.latex.length;
ctx.uncleanedLatex += '^{';
const beforeLength = ctx.uncleanedLatex.length;
this.sup.latexRecursive(ctx);
const afterLength = ctx.latex.length;
const afterLength = ctx.uncleanedLatex.length;
if (beforeLength === afterLength) {
// nothing was written. so we write a space
ctx.latex += ' ';
ctx.uncleanedLatex += ' ';
}

ctx.latex += '}';
ctx.uncleanedLatex += '}';
}

this.checkCursorContextClose(ctx);
Expand Down Expand Up @@ -727,25 +727,25 @@ class SummationNotation extends MathCommand {
latexRecursive(ctx: LatexContext) {
this.checkCursorContextOpen(ctx);

ctx.latex += this.ctrlSeq + '_{';
let beforeLength = ctx.latex.length;
ctx.uncleanedLatex += this.ctrlSeq + '_{';
let beforeLength = ctx.uncleanedLatex.length;
this.getEnd(L).latexRecursive(ctx);
let afterLength = ctx.latex.length;
let afterLength = ctx.uncleanedLatex.length;
if (afterLength === beforeLength) {
// nothing was written so we write a space
ctx.latex += ' ';
ctx.uncleanedLatex += ' ';
}

ctx.latex += '}^{';
beforeLength = ctx.latex.length;
ctx.uncleanedLatex += '}^{';
beforeLength = ctx.uncleanedLatex.length;
this.getEnd(R).latexRecursive(ctx);
afterLength = ctx.latex.length;
afterLength = ctx.uncleanedLatex.length;
if (beforeLength === afterLength) {
// nothing was written so we write a space
ctx.latex += ' ';
ctx.uncleanedLatex += ' ';
}

ctx.latex += '}';
ctx.uncleanedLatex += '}';
this.checkCursorContextClose(ctx);
}
mathspeak() {
Expand Down Expand Up @@ -1069,7 +1069,7 @@ class Token extends MQSymbol {
latexRecursive(ctx: LatexContext): void {
this.checkCursorContextOpen(ctx);

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

this.checkCursorContextClose(ctx);
}
Expand Down Expand Up @@ -1175,11 +1175,11 @@ class NthRoot extends SquareRoot {
latexRecursive(ctx: LatexContext) {
this.checkCursorContextOpen(ctx);

ctx.latex += '\\sqrt[';
ctx.uncleanedLatex += '\\sqrt[';
this.getEnd(L).latexRecursive(ctx);
ctx.latex += ']{';
ctx.uncleanedLatex += ']{';
this.getEnd(R).latexRecursive(ctx);
ctx.latex += '}';
ctx.uncleanedLatex += '}';

this.checkCursorContextClose(ctx);
}
Expand Down Expand Up @@ -1328,9 +1328,9 @@ class Bracket extends DelimsNode {
latexRecursive(ctx: LatexContext) {
this.checkCursorContextOpen(ctx);

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

this.checkCursorContextClose(ctx);
}
Expand Down Expand Up @@ -1869,7 +1869,7 @@ class EmbedNode extends MQSymbol {
latexRecursive(ctx: LatexContext): void {
this.checkCursorContextOpen(ctx);

ctx.latex += this.latex();
ctx.uncleanedLatex += this.latex();

this.checkCursorContextClose(ctx);
}
Expand Down
12 changes: 6 additions & 6 deletions src/commands/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,11 @@ class TextBlock extends MQNode {

var contents = this.textContents();
if (contents.length > 0) {
ctx.latex += this.ctrlSeq + '{';
ctx.latex += contents
ctx.uncleanedLatex += this.ctrlSeq + '{';
ctx.uncleanedLatex += contents
.replace(/\\/g, '\\backslash ')
.replace(/[{}]/g, '\\$&');
ctx.latex += '}';
ctx.uncleanedLatex += '}';
}

this.checkCursorContextClose(ctx);
Expand Down Expand Up @@ -361,7 +361,7 @@ class TextPiece extends MQNode {
}
latexRecursive(ctx: LatexContext) {
this.checkCursorContextOpen(ctx);
ctx.latex += this.textStr;
ctx.uncleanedLatex += this.textStr;
this.checkCursorContextClose(ctx);
}

Expand Down Expand Up @@ -509,9 +509,9 @@ class RootMathCommand extends MathCommand {
}
latexRecursive(ctx: LatexContext) {
this.checkCursorContextOpen(ctx);
ctx.latex += '$';
ctx.uncleanedLatex += '$';
this.getEnd(L).latexRecursive(ctx);
ctx.latex += '$';
ctx.uncleanedLatex += '$';
this.checkCursorContextClose(ctx);
}
}
Expand Down
5 changes: 0 additions & 5 deletions src/mathquill.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@ declare namespace MathQuill {
latex: string;
startIndex: number;
endIndex: number;
opaqueSnapshot: {
uncleanedLatex: string;
cursorInsertPath: string;
signedSelectionSize: number;
};
};

interface BaseMathQuill {
Expand Down
2 changes: 1 addition & 1 deletion src/publicapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ function getInterface(v: number): MathQuill.v3.API | MathQuill.v1.API {
return this;
}

return this.__controller.exportLatexSelection();
return this.__controller.exportLatexSelection().selection;
}
html() {
return this.__controller.root
Expand Down
Loading