Skip to content
Open
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
61 changes: 34 additions & 27 deletions src/commands/math.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ class DOMView {
* Descendant commands are organized into blocks.
*/
class MathCommand extends MathElement {
parent: MQNode;
replacedFragment: Fragment | undefined;
protected domView: DOMView;
protected ends: Ends<MQNode>;
Expand Down Expand Up @@ -465,7 +466,7 @@ function bindBinaryOperator(
* symbols and operators that descend (in the Math DOM tree) from
* ancestor operators.
*/
class MathBlock extends MathElement {
class MathBlockOrRootBlock extends MathElement {
controller?: Controller;

join(methodName: JoinMethod) {
Expand Down Expand Up @@ -547,32 +548,6 @@ class MathBlock extends MathElement {
return super.keystroke(key, e, ctrlr);
}

// editability methods: called by the cursor for editing, cursor movements,
// and selection of the MathQuill tree, these all take in a direction and
// the cursor
moveOutOf(dir: Direction, cursor: Cursor, updown?: 'up' | 'down') {
var updownInto: NodeRef | undefined;
if (updown === 'up') {
updownInto = this.parent.upInto;
} else if (updown === 'down') {
updownInto = this.parent.downInto;
}

if (!updownInto && this[dir]) {
const otherDir = -dir as Direction;
cursor.insAtDirEnd(otherDir, this[dir] as MQNode);
cursor.controller.aria.queueDirEndOf(otherDir).queue(cursor.parent, true);
} else {
cursor.insDirOf(dir, this.parent);
cursor.controller.aria.queueDirOf(dir).queue(this.parent);
}
}
selectOutOf(dir: Direction, cursor: Cursor) {
cursor.insDirOf(dir, this.parent);
}
deleteOutOf(_dir: Direction, cursor: Cursor) {
cursor.unwrapGramp();
}
seek(clientX: number, cursor: Cursor) {
var node = this.getEnd(R);
if (!node) return cursor.insAtRightEnd(this);
Expand Down Expand Up @@ -674,6 +649,38 @@ class MathBlock extends MathElement {
}
}

class MathBlock extends MathBlockOrRootBlock {
parent: MQNode;

// editability methods: called by the cursor for editing, cursor movements,
// and selection of the MathQuill tree, these all take in a direction and
// the cursor
moveOutOf(dir: Direction, cursor: Cursor, updown?: 'up' | 'down') {
var updownInto: NodeRef | undefined;
if (updown === 'up') {
updownInto = this.parent.upInto;
} else if (updown === 'down') {
updownInto = this.parent.downInto;
}

if (!updownInto && this[dir]) {
const otherDir = -dir as Direction;
cursor.insAtDirEnd(otherDir, this[dir] as MQNode);
cursor.controller.aria.queueDirEndOf(otherDir).queue(cursor.parent, true);
} else {
cursor.insDirOf(dir, this.parent);
cursor.controller.aria.queueDirOf(dir).queue(this.parent);
}
}
selectOutOf(dir: Direction, cursor: Cursor) {
cursor.insDirOf(dir, this.parent);
}
deleteOutOf(_dir: Direction, cursor: Cursor) {
pray('cursor is inside this block', cursor.parent === this);
cursor.unwrapGramp(this.parent);
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: these methods have separate implementations in RootBlockMixin, which is important because the Root block doesn't have a parent.

}

Options.prototype.mouseEvents = true;
API.StaticMath = function (APIClasses: APIClasses) {
return class StaticMath extends APIClasses.AbstractMathQuill {
Expand Down
10 changes: 6 additions & 4 deletions src/commands/math/LatexCommandInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,16 @@ CharCmds['\\'] = class LatexCommandInput extends MathCommand {
const endsL = this.getEnd(L);

endsL.focus = function () {
this.parent.domFrag().addClass('mq-hasCursor');
if (this.isEmpty()) this.parent.domFrag().removeClass('mq-empty');
(this.parent as MQNode).domFrag().addClass('mq-hasCursor');
if (this.isEmpty())
(this.parent as MQNode).domFrag().removeClass('mq-empty');

return this;
};
endsL.blur = function () {
this.parent.domFrag().removeClass('mq-hasCursor');
if (this.isEmpty()) this.parent.domFrag().addClass('mq-empty');
(this.parent as MQNode).domFrag().removeClass('mq-hasCursor');
if (this.isEmpty())
(this.parent as MQNode).domFrag().addClass('mq-empty');

return this;
};
Expand Down
4 changes: 3 additions & 1 deletion src/commands/math/basicSymbols.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,9 @@ class Digit extends DigitGroupingChar {
) {
new SubscriptCommand().createLeftOf(cursor);
super.createLeftOf(cursor);
cursor.insRightOf(cursor.parent.parent);
var gramp = cursor.parent.parent as MQNode;
pray('gramp exists', gramp);
cursor.insRightOf(gramp);
} else super.createLeftOf(cursor);
}
mathspeak(opts: MathspeakOptions) {
Expand Down
3 changes: 2 additions & 1 deletion src/commands/math/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -540,13 +540,14 @@ class SupSub extends MathCommand {
}
}

function insLeftOfMeUnlessAtEnd(this: MQNode, cursor: Cursor) {
function insLeftOfMeUnlessAtEnd(this: MathCommand | MathBlock, cursor: Cursor) {
// cursor.insLeftOf(cmd), unless cursor at the end of block, and every
// ancestor cmd is at the end of every ancestor block
var cmd = this.parent;
var ancestorCmd: MQNode | Anticursor | Cursor = cursor;
do {
if (ancestorCmd[R]) return cursor.insLeftOf(cmd);
if (!ancestorCmd.parent || !ancestorCmd.parent.parent) break;
ancestorCmd = ancestorCmd.parent.parent;
} while (ancestorCmd !== cmd);
cursor.insRightOf(cmd);
Expand Down
1 change: 1 addition & 0 deletions src/commands/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ function TextBlockFuseChildren(self: TextBlock) {
* Text contents must always be nonempty.
*/
class TextPiece extends MQNode {
parent: MQNode;
textStr: string;

constructor(text: string) {
Expand Down
9 changes: 5 additions & 4 deletions src/cursor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ class Cursor extends Point {
/** Place the cursor before or after `el`, according the side specified by `dir`. */
insDirOf(dir: Direction, el: MQNode) {
prayDirection(dir);
if (!el.parent) return this.hide();
this.domFrag().insDirOf(dir, el.domFrag());
this.withDirInsertAt(dir, el.parent, el[dir], el);
this.parent.domFrag().addClass('mq-hasCursor');
Expand Down Expand Up @@ -181,9 +182,9 @@ class Cursor extends Point {
right,
};
}
unwrapGramp() {
var gramp = this.parent.parent;
var greatgramp = gramp.parent;
unwrapGramp(gramp: MQNode) {
var greatgramp = gramp.parent as MQNode;
pray('greatgramp exists', greatgramp);
var rightward = gramp[R];
var cursor = this;

Expand Down Expand Up @@ -365,7 +366,7 @@ class Cursor extends Point {
return seln;
}
depth() {
var node: MQNode | Point = this;
var node: NodeRef | Point = this;
var depth = 0;
while ((node = node.parent)) {
depth += node instanceof MathBlock ? 1 : 0;
Expand Down
10 changes: 7 additions & 3 deletions src/services/keystroke.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,11 +351,11 @@ class Controller_keystroke extends Controller_focusBlur {
deleteDir(dir: Direction) {
prayDirection(dir);
var cursor = this.cursor;
var cursorEl = cursor[dir] as MQNode;
var cursorEl = cursor[dir];
var cursorElParent = cursor.parent.parent;
var ctrlr = cursor.controller;

if (cursorEl && cursorEl instanceof MQNode) {
if (cursorEl && cursorEl.parent && cursorEl instanceof MQNode) {
if (cursorEl.sides) {
ctrlr.aria.queue(
cursorEl.parent
Expand All @@ -368,7 +368,11 @@ class Controller_keystroke extends Controller_focusBlur {
} else if (!cursorEl.blocks && cursorEl.parent.ctrlSeq !== '\\text') {
ctrlr.aria.queue(cursorEl);
}
} else if (cursorElParent && cursorElParent instanceof MQNode) {
} else if (
cursorElParent &&
cursorElParent.parent &&
cursorElParent instanceof MQNode
) {
if (cursorElParent.sides) {
ctrlr.aria.queue(
cursorElParent.parent
Expand Down
9 changes: 6 additions & 3 deletions src/tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,7 @@ class NodeBase {
[L]: NodeRef = 0;
[R]: NodeRef = 0;

// TODO - can this ever actually stay 0? if so we need to add null checks
parent: MQNode = 0 as any as MQNode;
parent: NodeRef = 0;

/**
* The (doubly-linked) list of this node's children.
Expand Down Expand Up @@ -351,7 +350,11 @@ class NodeBase {
write(_cursor: Cursor, _ch: string) {}
}

function prayWellFormed(parent: MQNode, leftward: NodeRef, rightward: NodeRef) {
function prayWellFormed(
parent: NodeRef,
leftward: NodeRef,
rightward: NodeRef
): asserts parent {
pray('a parent is always present', parent);
pray(
'leftward is properly set up',
Expand Down