2022-11-18 15:04:37 +00:00
|
|
|
import {
|
|
|
|
Decoration,
|
2022-12-09 15:09:53 +00:00
|
|
|
EditorState,
|
2022-11-18 15:04:37 +00:00
|
|
|
EditorView,
|
2022-12-09 15:09:53 +00:00
|
|
|
syntaxTree,
|
2022-11-18 15:04:37 +00:00
|
|
|
WidgetType,
|
|
|
|
} from "../deps.ts";
|
|
|
|
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";
|
2022-12-09 15:09:53 +00:00
|
|
|
import type { Editor } from "../editor.tsx";
|
2023-02-23 14:33:51 +00:00
|
|
|
import { urlToPathname } from "../../plugos/util.ts";
|
2022-11-18 15:04:37 +00:00
|
|
|
|
|
|
|
class TableViewWidget extends WidgetType {
|
|
|
|
constructor(
|
|
|
|
readonly pos: number,
|
2023-02-23 14:33:51 +00:00
|
|
|
readonly editor: Editor,
|
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-02-23 14:33:51 +00:00
|
|
|
this.editor.editorView!.dispatch({
|
2022-11-18 15:04:37 +00:00
|
|
|
selection: {
|
|
|
|
anchor: dataAttributes.pos ? +dataAttributes.pos : this.pos,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-02-23 14:33:51 +00:00
|
|
|
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-02-23 14:33:51 +00:00
|
|
|
inlineAttachments: async (url): Promise<string> => {
|
|
|
|
if (!url.includes("://")) {
|
|
|
|
try {
|
|
|
|
const d = await this.editor.space.readAttachment(url, "dataurl");
|
|
|
|
return d.data as string;
|
|
|
|
} catch (e: any) {
|
|
|
|
console.error(e);
|
|
|
|
return url;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return url;
|
|
|
|
},
|
|
|
|
}).then((html) => {
|
|
|
|
dom.innerHTML = html;
|
2022-11-18 15:04:37 +00:00
|
|
|
});
|
|
|
|
return dom;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-09 15:09:53 +00:00
|
|
|
export function tablePlugin(editor: Editor) {
|
|
|
|
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
|
|
|
}
|