2023-05-23 18:53:53 +00:00
|
|
|
import { Decoration, EditorState, syntaxTree, WidgetType } from "../deps.ts";
|
2022-11-18 15:04:37 +00:00
|
|
|
import {
|
2022-12-09 15:09:53 +00:00
|
|
|
decoratorStateField,
|
2022-11-18 15:04:37 +00:00
|
|
|
invisibleDecoration,
|
|
|
|
isCursorInRange,
|
|
|
|
} from "./util.ts";
|
|
|
|
|
|
|
|
import { renderMarkdownToHtml } from "../../plugs/markdown/markdown_render.ts";
|
|
|
|
import { ParseTree } from "$sb/lib/tree.ts";
|
2022-12-14 19:04:20 +00:00
|
|
|
import { lezerToParseTree } from "../../common/markdown_parser/parse_tree.ts";
|
2023-07-14 14:56:20 +00:00
|
|
|
import type { Client } from "../client.ts";
|
2023-07-29 21:41:37 +00:00
|
|
|
import { resolvePath } from "$sb/lib/resolve.ts";
|
2022-11-18 15:04:37 +00:00
|
|
|
|
|
|
|
class TableViewWidget extends WidgetType {
|
|
|
|
constructor(
|
|
|
|
readonly pos: number,
|
2023-07-14 14:56:20 +00:00
|
|
|
readonly editor: Client,
|
2022-11-18 15:04:37 +00:00
|
|
|
readonly t: ParseTree,
|
|
|
|
) {
|
|
|
|
super();
|
|
|
|
}
|
|
|
|
|
|
|
|
toDOM(): HTMLElement {
|
|
|
|
const dom = document.createElement("span");
|
|
|
|
dom.classList.add("sb-table-widget");
|
|
|
|
dom.addEventListener("click", (e) => {
|
|
|
|
// Pulling data-pos to put the cursor in the right place, falling back
|
|
|
|
// to the start of the table.
|
|
|
|
const dataAttributes = (e.target as any).dataset;
|
2023-07-27 09:41:44 +00:00
|
|
|
this.editor.editorView.dispatch({
|
2022-11-18 15:04:37 +00:00
|
|
|
selection: {
|
|
|
|
anchor: dataAttributes.pos ? +dataAttributes.pos : this.pos,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-05-26 12:04:32 +00:00
|
|
|
dom.innerHTML = renderMarkdownToHtml(this.t, {
|
2022-11-18 15:04:37 +00:00
|
|
|
// Annotate every element with its position so we can use it to put
|
|
|
|
// the cursor there when the user clicks on the table.
|
|
|
|
annotationPositions: true,
|
2023-07-02 09:25:32 +00:00
|
|
|
translateUrls: (url) => {
|
2023-02-23 14:33:51 +00:00
|
|
|
if (!url.includes("://")) {
|
2023-07-29 21:41:37 +00:00
|
|
|
url = resolvePath(this.editor.currentPage!, decodeURI(url), true);
|
2023-02-23 14:33:51 +00:00
|
|
|
}
|
|
|
|
return url;
|
|
|
|
},
|
2023-07-24 17:54:31 +00:00
|
|
|
preserveAttributes: true,
|
2022-11-18 15:04:37 +00:00
|
|
|
});
|
|
|
|
return dom;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-14 14:56:20 +00:00
|
|
|
export function tablePlugin(editor: Client) {
|
2022-12-09 15:09:53 +00:00
|
|
|
return decoratorStateField((state: EditorState) => {
|
2022-11-18 15:04:37 +00:00
|
|
|
const widgets: any[] = [];
|
2022-12-09 15:09:53 +00:00
|
|
|
syntaxTree(state).iterate({
|
2022-11-18 15:04:37 +00:00
|
|
|
enter: (node) => {
|
|
|
|
const { from, to, name } = node;
|
|
|
|
if (name !== "Table") return;
|
2022-12-09 15:09:53 +00:00
|
|
|
if (isCursorInRange(state, [from, to])) return;
|
2022-11-18 15:04:37 +00:00
|
|
|
|
2022-12-09 15:09:53 +00:00
|
|
|
const tableText = state.sliceDoc(from, to);
|
|
|
|
const lineStrings = tableText.split("\n");
|
|
|
|
|
|
|
|
const lines: { from: number; to: number }[] = [];
|
|
|
|
let fromIt = from;
|
|
|
|
for (const line of lineStrings) {
|
|
|
|
lines.push({
|
|
|
|
from: fromIt,
|
|
|
|
to: fromIt + line.length,
|
|
|
|
});
|
|
|
|
fromIt += line.length + 1;
|
|
|
|
}
|
2022-11-18 15:04:37 +00:00
|
|
|
|
|
|
|
const firstLine = lines[0], lastLine = lines[lines.length - 1];
|
|
|
|
|
|
|
|
// In case of doubt, back out
|
|
|
|
if (!firstLine || !lastLine) return;
|
|
|
|
|
|
|
|
widgets.push(invisibleDecoration.range(firstLine.from, firstLine.to));
|
|
|
|
widgets.push(invisibleDecoration.range(lastLine.from, lastLine.to));
|
|
|
|
|
|
|
|
lines.slice(1, lines.length - 1).forEach((line) => {
|
|
|
|
widgets.push(
|
|
|
|
Decoration.line({ class: "sb-line-table-outside" }).range(
|
|
|
|
line.from,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
});
|
2022-12-09 15:09:53 +00:00
|
|
|
const text = state.sliceDoc(0, to);
|
2022-11-18 15:04:37 +00:00
|
|
|
widgets.push(
|
|
|
|
Decoration.widget({
|
|
|
|
widget: new TableViewWidget(
|
|
|
|
from,
|
2023-02-23 14:33:51 +00:00
|
|
|
editor,
|
2022-11-18 15:04:37 +00:00
|
|
|
lezerToParseTree(text, node.node),
|
|
|
|
),
|
|
|
|
}).range(from),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
return Decoration.set(widgets, true);
|
2022-12-09 15:09:53 +00:00
|
|
|
});
|
2022-11-18 15:04:37 +00:00
|
|
|
}
|