2023-11-21 15:14:08 -05:00
|
|
|
import Row from './row.ts';
|
2023-11-14 15:53:45 -05:00
|
|
|
import { getRuntime } from './runtime.ts';
|
2023-11-22 11:07:33 -05:00
|
|
|
import { Position } from './mod.ts';
|
2023-11-13 14:46:04 -05:00
|
|
|
|
|
|
|
export class Document {
|
2023-11-21 15:14:08 -05:00
|
|
|
#filename: string | null;
|
2023-11-13 14:46:04 -05:00
|
|
|
#rows: Row[];
|
2023-11-21 15:14:08 -05:00
|
|
|
dirty: boolean;
|
2023-11-13 14:46:04 -05:00
|
|
|
|
|
|
|
private constructor() {
|
|
|
|
this.#rows = [];
|
2023-11-21 15:14:08 -05:00
|
|
|
this.#filename = null;
|
|
|
|
this.dirty = false;
|
2023-11-13 14:46:04 -05:00
|
|
|
}
|
|
|
|
|
2023-11-16 11:10:33 -05:00
|
|
|
get numRows(): number {
|
2023-11-13 14:46:04 -05:00
|
|
|
return this.#rows.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static empty(): Document {
|
|
|
|
return new Document();
|
|
|
|
}
|
|
|
|
|
2023-11-14 15:53:45 -05:00
|
|
|
public isEmpty(): boolean {
|
|
|
|
return this.#rows.length === 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public async open(filename: string): Promise<Document> {
|
|
|
|
const { file } = await getRuntime();
|
|
|
|
|
|
|
|
// Clear any existing rows
|
|
|
|
if (!this.isEmpty()) {
|
|
|
|
this.#rows = [];
|
|
|
|
}
|
2023-11-13 14:46:04 -05:00
|
|
|
|
2023-11-14 15:53:45 -05:00
|
|
|
const rawFile = await file.openFile(filename);
|
|
|
|
rawFile.split(/\r?\n/)
|
|
|
|
.forEach((row) => this.appendRow(row));
|
2023-11-13 14:46:04 -05:00
|
|
|
|
2023-11-21 15:14:08 -05:00
|
|
|
this.#filename = filename;
|
|
|
|
this.dirty = false;
|
|
|
|
|
2023-11-14 15:53:45 -05:00
|
|
|
return this;
|
2023-11-13 14:46:04 -05:00
|
|
|
}
|
|
|
|
|
2023-11-21 15:14:08 -05:00
|
|
|
public async save() {
|
|
|
|
if (this.#filename === null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const { file } = await getRuntime();
|
|
|
|
|
|
|
|
await file.saveFile(this.#filename, this.rowsToString());
|
|
|
|
|
|
|
|
this.dirty = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public insert(at: Position, c: string): void {
|
|
|
|
if (at.y === this.numRows) {
|
|
|
|
this.appendRow(c);
|
|
|
|
} else {
|
|
|
|
this.#rows[at.y].insertChar(at.x, c);
|
2023-11-22 11:07:33 -05:00
|
|
|
this.#rows[at.y].updateRender();
|
2023-11-21 15:14:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
this.dirty = true;
|
|
|
|
}
|
|
|
|
|
2023-11-22 11:07:33 -05:00
|
|
|
public delete(at: Position): void {
|
2023-11-22 15:11:32 -05:00
|
|
|
const len = this.numRows;
|
|
|
|
if (at.y >= len) {
|
2023-11-22 11:07:33 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-11-22 15:11:32 -05:00
|
|
|
const row = this.row(at.y)!;
|
|
|
|
const mergeNextRow = at.x === row.size - 1 && at.y < len - 1;
|
|
|
|
const mergeIntoPrevRow = at.x === 0 && at.y > 0;
|
|
|
|
|
|
|
|
// If we are at the end of a line, and press delete,
|
|
|
|
// add the contents of the next row, and delete
|
|
|
|
// the merged row object
|
|
|
|
if (mergeNextRow) {
|
|
|
|
// At the end of a line, pressing delete will merge
|
|
|
|
// the next line into the current on
|
|
|
|
const rowToAppend = this.#rows.at(at.y + 1)!.toString();
|
|
|
|
row.append(rowToAppend);
|
|
|
|
this.deleteRow(at.y + 1);
|
|
|
|
} else if (mergeIntoPrevRow) {
|
|
|
|
// At the beginning of a line, merge the current line
|
|
|
|
// into the previous Row
|
|
|
|
const rowToAppend = row.toString();
|
|
|
|
this.#rows[at.y - 1].append(rowToAppend);
|
|
|
|
this.deleteRow(at.y);
|
|
|
|
} else {
|
|
|
|
row.delete(at.x);
|
|
|
|
}
|
|
|
|
|
2023-11-22 11:07:33 -05:00
|
|
|
row.updateRender();
|
2023-11-22 11:27:46 -05:00
|
|
|
|
|
|
|
this.dirty = true;
|
2023-11-22 11:07:33 -05:00
|
|
|
}
|
|
|
|
|
2023-11-14 15:53:45 -05:00
|
|
|
public row(i: number): Row | null {
|
2023-11-20 14:21:42 -05:00
|
|
|
return this.#rows[i] ?? null;
|
2023-11-13 14:46:04 -05:00
|
|
|
}
|
2023-11-14 15:53:45 -05:00
|
|
|
|
2023-11-16 20:57:21 -05:00
|
|
|
public appendRow(s: string): void {
|
2023-11-20 15:14:36 -05:00
|
|
|
const at = this.numRows;
|
|
|
|
this.#rows[at] = new Row(s);
|
2023-11-22 11:07:33 -05:00
|
|
|
this.#rows[at].updateRender();
|
2023-11-21 15:14:08 -05:00
|
|
|
|
|
|
|
this.dirty = true;
|
2023-11-20 15:14:36 -05:00
|
|
|
}
|
|
|
|
|
2023-11-22 15:11:32 -05:00
|
|
|
/**
|
|
|
|
* Delete the specified row
|
|
|
|
* @param at - the index of the row to delete
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
private deleteRow(at: number): void {
|
|
|
|
this.#rows.splice(at, 1);
|
|
|
|
}
|
|
|
|
|
2023-11-21 15:14:08 -05:00
|
|
|
private rowsToString(): string {
|
|
|
|
return this.#rows.map((r) => r.toString()).join('\n');
|
|
|
|
}
|
2023-11-13 14:46:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
export default Document;
|