Initial RuneStone project
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
import { parseMarkdownTable } from "./markdownTable.ts";
|
||||
import { deleteTableCells } from "./tableSelection.ts";
|
||||
import { plainInlineMarkdown } from "../workspace/noteHtml.ts";
|
||||
|
||||
export type TableAxis = "row" | "column";
|
||||
export type TableOperation = "before" | "after" | "previous" | "next" | "duplicate" | "delete";
|
||||
|
||||
// Indices refer to rendered rows (header = 0), or columns. `to` is the
|
||||
// final position, after removing the source item.
|
||||
export function moveTableAxis(source: string, axis: TableAxis, from: number, to: number): string {
|
||||
const table = parseMarkdownTable(source);
|
||||
if (!table) return source;
|
||||
const size = axis === "row" ? table.rows.length - 1 : table.columns;
|
||||
const first = axis === "row" ? 1 : 0;
|
||||
if (![from, to].every(index => Number.isInteger(index) && index >= first && index < size) || from === to) return source;
|
||||
function move<T>(items: T[], start: number, end: number) {
|
||||
items.splice(end, 0, items.splice(start, 1)[0]);
|
||||
return items;
|
||||
}
|
||||
const rows = axis === "row"
|
||||
? move(table.rows.map(row => source.slice(row.from, row.to)), from + 1, to + 1)
|
||||
: table.rows.map(row => {
|
||||
const cells = Array.from({ length: Math.max(table.columns, row.cells.length) }, (_, i) => row.cells[i]?.text || "");
|
||||
return `${row.indent}| ${move(cells, from, to).join(" | ")} |`;
|
||||
});
|
||||
return rows.join(table.eol) + source.slice(table.end);
|
||||
}
|
||||
|
||||
export function changeTableStructure(source: string, axis: TableAxis, index: number, operation: TableOperation): string {
|
||||
const table = parseMarkdownTable(source);
|
||||
if (!table) return source;
|
||||
const size = axis === "row" ? table.rows.length - 1 : table.columns;
|
||||
if (index < 0 || index >= size) return source;
|
||||
if (operation === "delete") return deleteTableCells(source, axis === "row"
|
||||
? { anchor: { row: index, column: 0 }, head: { row: index, column: table.columns - 1 } }
|
||||
: { anchor: { row: 0, column: index }, head: { row: table.rows.length - 2, column: index } });
|
||||
const moving = operation === "previous" || operation === "next";
|
||||
const other = index + (operation === "previous" ? -1 : 1);
|
||||
// The Markdown header stays a header; row movement and sorting affect the body.
|
||||
if (moving && (other < (axis === "row" ? 1 : 0) || other >= size || (axis === "row" && index === 0))) return source;
|
||||
if (axis === "row") {
|
||||
if (index === 0 && operation === "before") return source;
|
||||
const rows = table.rows.map(row => source.slice(row.from, row.to));
|
||||
const at = index === 0 ? 0 : index + 1;
|
||||
if (moving) [rows[at], rows[other + 1]] = [rows[other + 1], rows[at]];
|
||||
else {
|
||||
const value = operation === "duplicate" ? rows[at] : `${table.rows[0].indent}| ${Array(table.columns).fill("").join(" | ")} |`;
|
||||
rows.splice(index === 0 ? 2 : at + (operation === "before" ? 0 : 1), 0, value);
|
||||
}
|
||||
return rows.join(table.eol) + source.slice(table.end);
|
||||
}
|
||||
const rows = table.rows.map((row, r) => {
|
||||
const cells = Array.from({ length: Math.max(table.columns, row.cells.length) }, (_, c) => row.cells[c]?.text || "");
|
||||
if (moving) [cells[index], cells[other]] = [cells[other], cells[index]];
|
||||
else cells.splice(index + (operation === "before" ? 0 : 1), 0, operation === "duplicate" ? cells[index] : r === 1 ? "---" : "");
|
||||
return `${row.indent}| ${cells.join(" | ")} |`;
|
||||
});
|
||||
return rows.join(table.eol) + source.slice(table.end);
|
||||
}
|
||||
|
||||
export function sortTableColumn(source: string, column: number, descending = false): string {
|
||||
const table = parseMarkdownTable(source);
|
||||
if (!table || column < 0 || column >= table.columns) return source;
|
||||
const collator = new Intl.Collator(undefined, { numeric: true, sensitivity: "base" });
|
||||
const key = (text: string) => plainInlineMarkdown(text).replace(/<br>/g, " ").replace(/\\([\\`*_[\]~<>!|=$%])/g, "$1").trim();
|
||||
const rows = table.rows.slice(2).map(row => ({ row, key: key(row.cells[column]?.text || "") }));
|
||||
rows.sort((a, b) => !a.key ? (b.key ? 1 : 0) : !b.key ? -1 : collator.compare(a.key, b.key) * (descending ? -1 : 1));
|
||||
return [...table.rows.slice(0, 2), ...rows.map(item => item.row)].map(row => source.slice(row.from, row.to)).join(table.eol) + source.slice(table.end);
|
||||
}
|
||||
Reference in New Issue
Block a user