Initial RuneStone project
This commit is contained in:
@@ -0,0 +1,99 @@
|
||||
import { Decoration, EditorView, WidgetType } from "@codemirror/view";
|
||||
import { actionIcon } from "../ui/actionIcons";
|
||||
|
||||
type RenameTitle = (title: string) => Promise<void>;
|
||||
|
||||
class NoteTitle extends WidgetType {
|
||||
constructor(readonly title: string, readonly rename?: RenameTitle) { super(); }
|
||||
eq(other: NoteTitle) { return this.title === other.title && this.rename === other.rename; }
|
||||
toDOM(view: EditorView) {
|
||||
// Contain heading margins so CodeMirror measures the full block height.
|
||||
const block = document.createElement("div");
|
||||
block.style.display = "flow-root";
|
||||
const heading = document.createElement("h1");
|
||||
heading.className = "document-title";
|
||||
heading.textContent = this.title;
|
||||
block.append(heading);
|
||||
if (!this.rename) return block;
|
||||
|
||||
const button = document.createElement("button");
|
||||
button.type = "button";
|
||||
button.className = "note-title-button";
|
||||
button.setAttribute("aria-label", "Rename note");
|
||||
button.append(document.createTextNode(this.title), actionIcon("rename"));
|
||||
const help = document.createElement("span");
|
||||
help.className = "note-title-help";
|
||||
help.textContent = "Rename note";
|
||||
help.setAttribute("role", "tooltip");
|
||||
button.append(help);
|
||||
heading.replaceChildren(button);
|
||||
|
||||
const input = document.createElement("input");
|
||||
input.className = "note-title-input";
|
||||
input.setAttribute("aria-label", "Note filename");
|
||||
input.autocomplete = "off";
|
||||
input.spellcheck = false;
|
||||
input.hidden = true;
|
||||
heading.append(input);
|
||||
const message = document.createElement("p");
|
||||
message.className = "note-title-error";
|
||||
message.setAttribute("role", "alert");
|
||||
message.hidden = true;
|
||||
block.append(message);
|
||||
let editing = false, pending = false;
|
||||
const finish = () => {
|
||||
editing = false;
|
||||
input.hidden = true;
|
||||
button.hidden = false;
|
||||
message.hidden = true;
|
||||
input.removeAttribute("aria-invalid");
|
||||
view.requestMeasure();
|
||||
};
|
||||
const commit = async (focusEditor: boolean) => {
|
||||
if (!editing || pending) return;
|
||||
pending = true;
|
||||
input.readOnly = true;
|
||||
input.setAttribute("aria-busy", "true");
|
||||
try {
|
||||
await this.rename!(input.value);
|
||||
finish();
|
||||
if (focusEditor && view.dom.isConnected && (document.activeElement === input || document.activeElement === document.body)) view.focus();
|
||||
} catch (reason) {
|
||||
if (!block.isConnected) return;
|
||||
input.setAttribute("aria-invalid", "true");
|
||||
message.textContent = reason instanceof Error ? reason.message : String(reason);
|
||||
message.hidden = false;
|
||||
view.requestMeasure();
|
||||
} finally {
|
||||
pending = false;
|
||||
input.readOnly = false;
|
||||
input.removeAttribute("aria-busy");
|
||||
}
|
||||
};
|
||||
button.onclick = () => {
|
||||
editing = true;
|
||||
input.value = this.title;
|
||||
button.hidden = true;
|
||||
input.hidden = false;
|
||||
input.focus();
|
||||
input.select();
|
||||
view.requestMeasure();
|
||||
};
|
||||
input.onblur = () => { void commit(false); };
|
||||
input.onkeydown = event => {
|
||||
event.stopPropagation();
|
||||
if (event.isComposing) return;
|
||||
if (event.key === "Enter") { event.preventDefault(); void commit(true); }
|
||||
if (event.key === "Escape" && !pending) { event.preventDefault(); finish(); button.focus(); }
|
||||
};
|
||||
return block;
|
||||
}
|
||||
ignoreEvent() { return true; }
|
||||
}
|
||||
|
||||
// Filename editing stays outside the Markdown and its undo history.
|
||||
export function noteTitle(title: string, rename?: RenameTitle) {
|
||||
return EditorView.decorations.of(Decoration.set([
|
||||
Decoration.widget({ widget: new NoteTitle(title, rename), block: true, side: -1 }).range(0),
|
||||
]));
|
||||
}
|
||||
Reference in New Issue
Block a user