Release RuneStone 0.1.3 and expand plugin plans

This commit is contained in:
Knotty Wyvern
2026-10-03 04:23:45 -07:00
parent 43106ed9c7
commit 0268c5c9f0
44 changed files with 247 additions and 53 deletions
+4 -22
View File
@@ -2,8 +2,9 @@
import { untrack } from "svelte";
import { Compartment, EditorState } from "@codemirror/state";
import { EditorView, drawSelection, keymap } from "@codemirror/view";
import { defaultKeymap, history, historyField, historyKeymap, isolateHistory } from "@codemirror/commands";
import { defaultKeymap, history, historyField, historyKeymap, indentWithTab, isolateHistory } from "@codemirror/commands";
import { markdown, markdownLanguage } from "@codemirror/lang-markdown";
import { noteFormats } from "./noteFormats";
import { searchKeymap } from "@codemirror/search";
import { writerSearch } from "./writerSearch";
import type { OpenBuffer } from "$lib/workspace/types";
@@ -135,26 +136,7 @@
model.pushStackElement();
const extensions = [
EditorState.lineSeparator.of(model.getEOL()),
markdown({ base: markdownLanguage, extensions: [{
defineNodes: ["NoteHighlight", "NoteMath", "NoteComment", "NoteFormatMark"],
parseInline: [{ name: "NoteFormats", before: "Emphasis", parse(context, next, position) {
const marker = next === 61 ? "==" : next === 37 ? "%%" : next === 36 ? "$" : "";
if (!marker || context.slice(position, position + marker.length) !== marker) return -1;
if (marker === "$" && (context.char(position + 1) === 36 || context.char(position - 1) === 36)) return -1;
let end = position + marker.length;
for (; end < context.end; end++) {
if (context.char(end) === 92) { end++; continue; }
if (context.slice(end, end + marker.length) === marker) break;
}
if (end >= context.end || end === position + marker.length) return -1;
const kind = marker === "==" ? "NoteHighlight" : marker === "$" ? "NoteMath" : "NoteComment";
const children = marker === "==" ? context.parser.parseInline(context.slice(position + marker.length, end), position + marker.length) : [];
return context.addElement(context.elt(kind, position, end + marker.length, [
context.elt("NoteFormatMark", position, position + marker.length), ...children,
context.elt("NoteFormatMark", end, end + marker.length),
]));
} }],
}] }),
markdown({ base: markdownLanguage, extensions: [noteFormats] }),
// Rich cell serialization can change delimiters away from the caret.
// Keep a burst of typing together even when those source edits aren't adjacent.
history({ joinToEvent: (transaction, adjacent) => adjacent || transaction.isUserEvent("input.type.table") }), drawSelection(),
@@ -164,7 +146,7 @@
keymap.of([{ key: "Escape", run: view => {
if (!view.state.field(imageSource)) return false;
view.dispatch({ effects: editImageSource.of(null) }); return true;
} }, ...searchKeymap, ...historyKeymap, ...defaultKeymap]), EditorView.lineWrapping,
} }, indentWithTab, ...searchKeymap, ...historyKeymap, ...defaultKeymap]), EditorView.lineWrapping,
liveMarkdown(urls, untrack(() => tableActions), {
open: image => onOpenLink?.(image.dataset.mdKind === "wiki" ? "wiki" : "md", image.dataset.mdSrc || ""),
reveal: async image => {
+20 -5
View File
@@ -54,9 +54,17 @@ class Checkbox extends WidgetType {
}
class Preview extends WidgetType {
constructor(readonly html: string, readonly source: string, readonly actions: TableContextActions, readonly sourceVisible = false) { super(); }
constructor(readonly html: string, readonly source: string, readonly actions: TableContextActions, readonly sourceVisible = false, readonly height = { value: -1 }) { super(); }
get estimatedHeight() { return this.height.value; }
eq(other: Preview) { return other.html === this.html && other.source === this.source && other.sourceVisible === this.sourceVisible; }
updateDOM(dom: HTMLElement) { return tableEditors.get(dom)?.source === this.source; }
measure(dom: HTMLElement, view: EditorView) {
view.requestMeasure({ key: this.height, read: () => dom.offsetHeight, write: height => { this.height.value = height; } });
}
updateDOM(dom: HTMLElement, view: EditorView) {
if (tableEditors.get(dom)?.source !== this.source) return false;
this.measure(dom, view);
return true;
}
destroy(dom: HTMLElement) { tableEditors.get(dom)?.destroy(); imageCleanups.get(dom)?.(); }
toDOM(view: EditorView) {
const dom = document.createElement("div");
@@ -64,6 +72,7 @@ class Preview extends WidgetType {
dom.innerHTML = this.html;
tableControls(dom, view, this.source, this.actions);
imageCleanups.set(dom, imageControls(dom, {}, () => { if (dom.isConnected) view.requestMeasure(); }));
this.measure(dom, view);
dom.querySelectorAll("button.rs-fence-copy").forEach(button => button.remove());
dom.addEventListener("mousedown", event => {
if (event.target instanceof Element && event.target.closest(".writer-table, .table-footer")) return;
@@ -85,7 +94,7 @@ class Preview extends WidgetType {
}
export function liveMarkdown(urls: Map<string, string>, actions: TableContextActions = {}, images: ImageContext = {}) {
function build(state: EditorState): DecorationSet {
function build(state: EditorState, previous?: DecorationSet): DecorationSet {
const ranges: Range<Decoration>[] = [];
const active = state.selection.ranges.map(range => ({ from: state.doc.lineAt(range.from).from, to: state.doc.lineAt(range.to).to }));
const selected = (from: number, to: number) => active.some(range => range.from <= to && range.to >= from);
@@ -123,7 +132,13 @@ export function liveMarkdown(urls: Map<string, string>, actions: TableContextAct
continue;
}
hiddenBlocks.push({ from: section.start, to: end });
ranges.push(Decoration.replace({ widget: new Preview(section.html, source.slice(section.start, end), actions), block: true, atomic: true }).range(section.start, end));
// Keep the table height between edits and the next layout pass. A one-line
// estimate can move the scroll anchor below a tall table on a second update.
let height: { value: number } | undefined;
if (isTable) previous?.between(section.start, end, (from, to, decoration) => {
if (from === section.start && to === end && decoration.spec.widget instanceof Preview) height = decoration.spec.widget.height;
});
ranges.push(Decoration.replace({ widget: new Preview(section.html, source.slice(section.start, end), actions, false, height), block: true, atomic: true }).range(section.start, end));
}
const hidden = (from: number, to: number) => hiddenBlocks.some(block => block.from <= from && block.to >= to);
const replace = (from: number, to: number) => { if (to > from) ranges.push(Decoration.replace({ atomic: true }).range(from, to)); };
@@ -188,7 +203,7 @@ export function liveMarkdown(urls: Map<string, string>, actions: TableContextAct
return [imageSource, StateField.define<DecorationSet>({
create: build,
update(value, transaction) {
return transaction.docChanged || transaction.selection || transaction.effects.some(effect => effect.is(refreshImages) || effect.is(editImageSource)) ? build(transaction.state) : value;
return transaction.docChanged || transaction.selection || transaction.effects.some(effect => effect.is(refreshImages) || effect.is(editImageSource)) ? build(transaction.state, value.map(transaction.changes)) : value;
},
provide: field => [EditorView.decorations.from(field), EditorView.atomicRanges.of(view => view.state.field(field).update({
filter: (_from, _to, decoration) => decoration.spec.atomic === true,
+29
View File
@@ -0,0 +1,29 @@
import type { markdown } from "@codemirror/lang-markdown";
import { inlineMathEnd } from "../workspace/inlineMath.ts";
export const noteFormats: NonNullable<NonNullable<Parameters<typeof markdown>[0]>["extensions"]> = {
defineNodes: ["NoteHighlight", "NoteMath", "NoteComment", "NoteFormatMark"],
parseInline: [{ name: "NoteFormats", before: "Emphasis", parse(context, next, position) {
const marker = next === 61 ? "==" : next === 37 ? "%%" : next === 36 ? "$" : "";
if (!marker || context.slice(position, position + marker.length) !== marker) return -1;
let end = position + marker.length;
if (marker === "$") {
if (context.char(position - 1) === 36) return -1;
const offset = inlineMathEnd(context.slice(position, context.end));
if (offset < 0) return -1;
end = position + offset;
} else {
for (; end < context.end; end++) {
if (context.char(end) === 92) { end++; continue; }
if (context.slice(end, end + marker.length) === marker) break;
}
if (end >= context.end || end === position + marker.length) return -1;
}
const kind = marker === "==" ? "NoteHighlight" : marker === "$" ? "NoteMath" : "NoteComment";
const children = marker === "==" ? context.parser.parseInline(context.slice(position + marker.length, end), position + marker.length) : [];
return context.addElement(context.elt(kind, position, end + marker.length, [
context.elt("NoteFormatMark", position, position + marker.length), ...children,
context.elt("NoteFormatMark", end, end + marker.length),
]));
} }],
};