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
24 changes: 12 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions src/abstractFields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,10 @@ export class EditableField extends AbstractMathQuill {
if (klass) {
const newCmd = new klass(cmd);
if (cursor.selection) newCmd.replaces(cursor.replaceSelection());
newCmd.createLeftOf(cursor.show());
this.__controller.scrollHoriz();
if (cursor.parent?.prepareCommandInsertion(cursor, newCmd)) {
newCmd.createLeftOf(cursor.show());
this.__controller.scrollHoriz();
}
} else {
// TODO: API needs better error reporting
}
Expand Down
18 changes: 11 additions & 7 deletions src/commands/mathBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { VanillaSymbol, MathElement, MathCommand, Letter, Digit, latexMathParser
export const writeMethodMixin = <TBase extends Constructor<TNode>>(Base: TBase) =>
class extends Base {
writeHandler?: (cursor: Cursor, ch: string) => boolean;
writeLatexHandler?: (cursor: Cursor, block: MathBlock) => MathBlock | boolean;

chToCmd(ch: string, options?: Options): TNode {
const cons = (CharCmds[ch] as Constructor<TNode> | undefined) || LatexCmds[ch];
Expand Down Expand Up @@ -178,16 +179,19 @@ export class MathBlock extends BlockFocusBlur(writeMethodMixin(MathElement)) {
const all = Parser.all;
const eof = Parser.eof;

const block = latexMathParser.skip(eof).or(all.result(false)).parse<MathCommand | undefined>(latex);
const block = latexMathParser.skip(eof).or(all.result(false)).parse<MathBlock | undefined>(latex);

if (block && !block.isEmpty() && block.prepareInsertionAt(cursor)) {
if (cursor.parent) block.children().adopt(cursor.parent, cursor.left, cursor.right);
const elements = block.domify();
const handlerResult = this.writeLatexHandler?.(cursor, block);
if (handlerResult === true) return;
const blockToInsert = handlerResult || block;
if (cursor.parent) blockToInsert.children().adopt(cursor.parent, cursor.left, cursor.right);
const elements = blockToInsert.domify();
cursor.element.before(...elements.contents);
cursor.left = block.ends.right;
block.finalizeInsert(cursor.options, cursor);
block.ends.right?.right?.siblingCreated?.(cursor.options, 'left');
block.ends.left?.left?.siblingCreated?.(cursor.options, 'right');
cursor.left = blockToInsert.ends.right;
blockToInsert.finalizeInsert(cursor.options, cursor);
blockToInsert.ends.right?.right?.siblingCreated?.(cursor.options, 'left');
blockToInsert.ends.left?.left?.siblingCreated?.(cursor.options, 'right');
cursor.parent?.bubble('reflow');
}
}
Expand Down
34 changes: 31 additions & 3 deletions src/commands/mathElements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ export class Digit extends VanillaSymbol {
}

export class Variable extends Symbol {
isItalic = false;
isItalic = true;
isPartOfOperator = false;

constructor(ch: string, html?: string) {
Expand Down Expand Up @@ -1431,8 +1431,6 @@ const BracketMixin = <TBase extends Constructor<MathCommand>>(Base: TBase) =>
}

replaceBracket(brackFrag: HTMLElement, side: Direction) {
if (!(this instanceof Bracket) && !(this instanceof MathFunction))
throw new Error('can only replace bracket for a Bracket or MathFunction');
const symbol = this.getSymbol(side);

brackFrag.innerHTML = symbol.html;
Expand Down Expand Up @@ -1660,6 +1658,36 @@ export class MathFunction extends BracketMixin(MathCommand) {
return false;
};

// Only allow a SubSub to be inserted into the block before the parentheses. If anything else is contained in
// the block, then move to the content block so it will be inserted there.
this.blocks[0].writeLatexHandler = (cursor: Cursor, block: MathBlock) => {
if (
block.ends.left === block.ends.right &&
block.ends.left instanceof Bracket &&
block.ends.left.ctrlSeq === '\\left('
) {
this.enterContentBlock('left', cursor);
const bracketContents = block.ends.left.blocks[0].children().disown();
block.ends.left.disown();
bracketContents.adopt(block);
return block;
}

block.eachChild((node: TNode) => {
if (node instanceof SupSub) return true;
this.enterContentBlock('left', cursor);
return false;
});

return false;
};

this.blocks[0].prepareCommandInsertion = (cursor: Cursor, cmd: TNode) => {
if (cmd instanceof SupSub) return true;
this.enterContentBlock('left', cursor);
return true;
};

return super.html();
}

Expand Down
1 change: 0 additions & 1 deletion src/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,6 @@ export class Controller extends ExportText(

selectAll() {
this.notify('move').cursor.insAtRightEnd(this.root);
while (this.cursor.left) this.selectLeft();
this.withIncrementalSelection((selectDir) => {
while (this.cursor.left) selectDir('left');
});
Expand Down
3 changes: 3 additions & 0 deletions src/tree/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,9 @@ export class TNode {
replaces(_fragment?: string | Fragment) {
/* do nothing */
}
prepareCommandInsertion(_cursor: Cursor, _cmd: TNode): boolean {
return true;
}
setOptions(_options: { text?: () => string; htmlTemplate?: string; latex?: () => string }) {
return this;
}
Expand Down
Loading