Skip to content

Commit

Permalink
feat: allow pasting of multiple columns in environments
Browse files Browse the repository at this point in the history
  • Loading branch information
manstie committed Oct 16, 2022
1 parent 7803e3e commit cb6968e
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 4 deletions.
28 changes: 25 additions & 3 deletions src/core-atoms/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -620,9 +620,27 @@ export class ArrayAtom extends Atom {
return this.array[row][col];
}

setCell(_row: number, _column: number, _value: Atom[]): void {
// @todo array
console.assert(this.type === 'array' && Array.isArray(this.array));
setCell(row: number, column: number, value: Atom[]): void {
console.assert(
this.type === 'array' &&
Array.isArray(this.array) &&
this.array[row][column] !== undefined
);
for (const atom of this.array[row][column]!) {
atom.parent = undefined;
atom.treeBranch = undefined;
}

let atoms = value;
if (value.length === 0 || value[0].type !== 'first') {
atoms = [new Atom('first', this.context, { mode: this.mode }), ...value];
}

this.array[row][column] = atoms;
for (const atom of atoms) {
atom.parent = this;
atom.treeBranch = [row, column];
}
this.isDirty = true;
}

Expand Down Expand Up @@ -714,6 +732,10 @@ export class ArrayAtom extends Atom {
this.isDirty = true;
}

addColumn(): void {
this.addColumnAfter(this.colCount - 1);
}

removeColumn(col: number): void {
console.assert(
this.type === 'array' && Array.isArray(this.array) && this.colCount > col
Expand Down
40 changes: 39 additions & 1 deletion src/editor-mathfield/mode-editor-math.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,45 @@ export class MathModeEditor extends ModeEditor {
if (!model.selectionIsCollapsed)
model.deleteAtoms(range(model.selection));
const cursor = model.at(model.position);
cursor.parent!.addChildrenAfter(atoms, cursor);

if (cursor.parent instanceof ArrayAtom) {
console.assert(cursor.treeBranch !== undefined);
// use 'first' atoms as environment column delimiter
const columns: Atom[][] = [];
let buffer: Atom[] = [];
// trim 'first' from array of atoms
if (atoms[0].type === 'first') atoms.shift();
if (atoms[atoms.length - 1].type === 'first') atoms.pop();
for (const atom of atoms) {
if (atom.type === 'first' && buffer.length > 0) {
columns.push(buffer);
buffer = [atom];
} else {
buffer.push(atom);
}
}
if (buffer.length > 0) {
columns.push(buffer);
}

// expand environment columns to paste size
const currentRow = Number(cursor.treeBranch![0]);
const currentColumn = Number(cursor.treeBranch![1]);
while (cursor.parent.colCount - currentColumn < columns.length)
cursor.parent.addColumn();

// add content to the first cell
cursor.parent.addChildrenAfter(columns[0], cursor);
// replace the rest of the columns
for (let i = 1; i < columns.length; i++)
cursor.parent.setCell(currentRow, currentColumn + i, columns[i]);
} else {
cursor.parent!.addChildrenAfter(
atoms.filter((a) => a.type !== 'first'),
cursor
);
}

model.position = model.offsetOf(atoms[atoms.length - 1]);

contentDidChange(model, { inputType: 'insertFromPaste' });
Expand Down

0 comments on commit cb6968e

Please sign in to comment.