mirror of
https://github.com/jparkerweb/plan2code.git
synced 2026-11-18 21:01:19 -08:00
Harden the shared console install, launcher, Loop staging, spec state and prompts
AI Assisted
This commit is contained in:
1 parent
120c259d9c
commit
3ee96834bf
130 files changed
+5988
-1434
No files matched your search
@@ -18,27 +18,44 @@
|
||||
// editable surface, everything else on the host. field.el is the host div to
|
||||
// append; host.field is the facade itself, so a page test or a helper can
|
||||
// drive it.
|
||||
|
||||
import {
|
||||
Compartment,
|
||||
EditorState,
|
||||
EditorView,
|
||||
defaultKeymap,
|
||||
history,
|
||||
historyKeymap,
|
||||
keymap,
|
||||
markdown,
|
||||
markdownLanguage,
|
||||
placeholder,
|
||||
redo,
|
||||
} from "./vendor/codemirror.esm.js";
|
||||
import { livePreview } from "./notepad-editor.js";
|
||||
//
|
||||
// CodeMirror (half a megabyte) is not part of the page's start: it is
|
||||
// imported the first time a field is made, and every field after that gets
|
||||
// its editor at once. A field made before the import lands keeps what is set
|
||||
// on it (value, caret, disabled, aria-*, a focus asked for) and hands it to
|
||||
// its editor when it arrives. destroy() ends the editor; renderMain() calls
|
||||
// it, through destroyFields(), on every field it takes off the page.
|
||||
|
||||
// These listeners run as the editor's own DOM handlers, before its keymaps:
|
||||
// a preventDefault there (the mention popover's keys, a pure-image paste)
|
||||
// still beats CodeMirror's own handling.
|
||||
const HANDLED = ["keydown", "keyup", "paste", "blur", "focus"];
|
||||
|
||||
// CodeMirror's exports plus the live preview, once loaded; the import itself
|
||||
// while it is on its way. A failed import is forgotten, so the next field
|
||||
// asks again.
|
||||
let cm = null;
|
||||
let loading = null;
|
||||
|
||||
function loadEditor() {
|
||||
loading ??= Promise.all([import("./vendor/codemirror.esm.js"), import("./notepad-editor.js")]).then(
|
||||
([lib, editor]) => (cm = { ...lib, livePreview: editor.livePreview }),
|
||||
(err) => {
|
||||
loading = null;
|
||||
throw err;
|
||||
}
|
||||
);
|
||||
return loading;
|
||||
}
|
||||
|
||||
// Ends the editor of every Markdown field inside `root` (root included), for
|
||||
// a caller about to drop that part of the page.
|
||||
export function destroyFields(root) {
|
||||
if (!root || typeof root.querySelectorAll !== "function") return;
|
||||
if (root.field && typeof root.field.destroy === "function") root.field.destroy();
|
||||
for (const host of root.querySelectorAll(".md-host")) if (host.field) host.field.destroy();
|
||||
}
|
||||
|
||||
export function makeMdField({ id, className, value = "", placeholder: hint, label, disabled = false, maxLength = 0 } = {}) {
|
||||
const host = document.createElement("div");
|
||||
host.className = "md-host" + (className ? " " + className : "");
|
||||
@@ -61,76 +78,110 @@ export function makeMdField({ id, className, value = "", placeholder: hint, labe
|
||||
};
|
||||
|
||||
let off = Boolean(disabled);
|
||||
const access = new Compartment();
|
||||
// editable, not only readOnly: a disabled field cannot take the focus,
|
||||
// which is the difference a textarea's own disabled attribute shows.
|
||||
let view = null;
|
||||
let gone = false;
|
||||
// What the field holds until its editor arrives.
|
||||
let text = String(value ?? "");
|
||||
let sel = null;
|
||||
let wantFocus = false;
|
||||
const aria = new Map();
|
||||
|
||||
let access = null;
|
||||
let stateOf = null;
|
||||
const accessOf = (on) =>
|
||||
on
|
||||
? [
|
||||
EditorState.readOnly.of(true),
|
||||
EditorView.editable.of(false),
|
||||
EditorView.contentAttributes.of({ "aria-disabled": "true" }),
|
||||
cm.EditorState.readOnly.of(true),
|
||||
cm.EditorView.editable.of(false),
|
||||
cm.EditorView.contentAttributes.of({ "aria-disabled": "true" }),
|
||||
]
|
||||
: [];
|
||||
|
||||
const extensions = [
|
||||
history(),
|
||||
keymap.of([{ key: "Mod-Shift-z", run: redo, preventDefault: true }, ...defaultKeymap, ...historyKeymap]),
|
||||
markdown({ base: markdownLanguage }),
|
||||
livePreview,
|
||||
EditorView.lineWrapping,
|
||||
EditorView.contentAttributes.of({ "aria-label": label || "Markdown text", "aria-multiline": "true", spellcheck: "true" }),
|
||||
EditorView.domEventHandlers(Object.fromEntries(HANDLED.map((type) => [type, (event) => fire(type, event)]))),
|
||||
EditorView.updateListener.of((update) => {
|
||||
if (update.docChanged) fire("input");
|
||||
}),
|
||||
...(hint ? [placeholder(hint)] : []),
|
||||
...(maxLength
|
||||
? [
|
||||
EditorState.transactionFilter.of((tr) => {
|
||||
if (!tr.docChanged || tr.newDoc.length <= maxLength) return tr;
|
||||
return {
|
||||
changes: { from: 0, to: tr.startState.doc.length, insert: tr.newDoc.sliceString(0, maxLength) },
|
||||
selection: { anchor: Math.min(tr.newSelection.main.head, maxLength) },
|
||||
};
|
||||
}),
|
||||
]
|
||||
: []),
|
||||
];
|
||||
const stateOf = (text) => EditorState.create({ doc: text, extensions: [...extensions, access.of(accessOf(off))] });
|
||||
const view = new EditorView({ state: stateOf(String(value ?? "")), parent: mount, root });
|
||||
// The stylesheets arrive after the first measure: measure again once they
|
||||
// have, or the lines would keep their unstyled height until the next edit.
|
||||
for (const sheet of sheets) sheet.addEventListener("load", () => view.requestMeasure());
|
||||
const build = () => {
|
||||
const { Compartment, EditorState, EditorView, defaultKeymap, history, historyKeymap, keymap, markdown, markdownLanguage, placeholder, redo, livePreview } = cm;
|
||||
access = new Compartment();
|
||||
// editable, not only readOnly: a disabled field cannot take the focus,
|
||||
// which is the difference a textarea's own disabled attribute shows.
|
||||
const extensions = [
|
||||
history(),
|
||||
keymap.of([{ key: "Mod-Shift-z", run: redo, preventDefault: true }, ...defaultKeymap, ...historyKeymap]),
|
||||
markdown({ base: markdownLanguage }),
|
||||
livePreview,
|
||||
EditorView.lineWrapping,
|
||||
EditorView.contentAttributes.of({ "aria-label": label || "Markdown text", "aria-multiline": "true", spellcheck: "true" }),
|
||||
EditorView.domEventHandlers(Object.fromEntries(HANDLED.map((type) => [type, (event) => fire(type, event)]))),
|
||||
EditorView.updateListener.of((update) => {
|
||||
if (update.docChanged) fire("input");
|
||||
}),
|
||||
...(hint ? [placeholder(hint)] : []),
|
||||
...(maxLength
|
||||
? [
|
||||
EditorState.transactionFilter.of((tr) => {
|
||||
if (!tr.docChanged || tr.newDoc.length <= maxLength) return tr;
|
||||
return {
|
||||
changes: { from: 0, to: tr.startState.doc.length, insert: tr.newDoc.sliceString(0, maxLength) },
|
||||
selection: { anchor: Math.min(tr.newSelection.main.head, maxLength) },
|
||||
};
|
||||
}),
|
||||
]
|
||||
: []),
|
||||
];
|
||||
stateOf = (t) => EditorState.create({ doc: t, extensions: [...extensions, access.of(accessOf(off))] });
|
||||
view = new EditorView({ state: stateOf(text), parent: mount, root });
|
||||
// The stylesheets arrive after the first measure: measure again once they
|
||||
// have, or the lines would keep their unstyled height until the next edit.
|
||||
for (const sheet of sheets) sheet.addEventListener("load", () => view.requestMeasure());
|
||||
if (sel) {
|
||||
const end = view.state.doc.length;
|
||||
view.dispatch({ selection: { anchor: Math.min(sel[0], end), head: Math.min(sel[1], end) } });
|
||||
}
|
||||
for (const [name, val] of aria) {
|
||||
if (val == null) view.contentDOM.removeAttribute(name);
|
||||
else view.contentDOM.setAttribute(name, val);
|
||||
}
|
||||
aria.clear();
|
||||
if (wantFocus && host.isConnected) view.focus();
|
||||
};
|
||||
|
||||
const field = {
|
||||
el: host,
|
||||
view,
|
||||
get value() {
|
||||
return view.state.doc.toString();
|
||||
get view() {
|
||||
return view;
|
||||
},
|
||||
set value(text) {
|
||||
get value() {
|
||||
return view ? view.state.doc.toString() : text;
|
||||
},
|
||||
set value(t) {
|
||||
// setState neither reports a change nor keeps the undo steps, exactly
|
||||
// the swap a textarea's value assignment makes. A fresh state parks its
|
||||
// caret at 0; a textarea's collapses to the end, so callers that fill
|
||||
// and focus (Autofill, quick answers, Reset to default) append as they
|
||||
// used to.
|
||||
view.setState(stateOf(String(text ?? "")));
|
||||
if (!view) {
|
||||
text = String(t ?? "");
|
||||
sel = [text.length, text.length];
|
||||
return;
|
||||
}
|
||||
view.setState(stateOf(String(t ?? "")));
|
||||
view.dispatch({ selection: { anchor: view.state.doc.length } });
|
||||
},
|
||||
get selectionStart() {
|
||||
if (!view) return sel ? Math.min(sel[0], sel[1]) : 0;
|
||||
const m = view.state.selection.main;
|
||||
return Math.min(m.anchor, m.head);
|
||||
},
|
||||
get selectionEnd() {
|
||||
if (!view) return sel ? Math.max(sel[0], sel[1]) : 0;
|
||||
const m = view.state.selection.main;
|
||||
return Math.max(m.anchor, m.head);
|
||||
},
|
||||
setSelectionRange(a, b) {
|
||||
view.dispatch({ selection: { anchor: a, head: b } });
|
||||
if (!view) sel = [a, b ?? a];
|
||||
else view.dispatch({ selection: { anchor: a, head: b } });
|
||||
},
|
||||
focus() {
|
||||
view.focus();
|
||||
if (view) view.focus();
|
||||
else wantFocus = true;
|
||||
},
|
||||
get disabled() {
|
||||
return off;
|
||||
@@ -138,7 +189,7 @@ export function makeMdField({ id, className, value = "", placeholder: hint, labe
|
||||
set disabled(on) {
|
||||
off = Boolean(on);
|
||||
host.classList.toggle("is-disabled", off);
|
||||
view.dispatch({ effects: access.reconfigure(accessOf(off)) });
|
||||
if (view) view.dispatch({ effects: access.reconfigure(accessOf(off)) });
|
||||
},
|
||||
get classList() {
|
||||
return host.classList;
|
||||
@@ -150,10 +201,14 @@ export function makeMdField({ id, className, value = "", placeholder: hint, labe
|
||||
return host.getBoundingClientRect();
|
||||
},
|
||||
setAttribute(name, val) {
|
||||
(name.startsWith("aria-") ? view.contentDOM : host).setAttribute(name, val);
|
||||
if (!name.startsWith("aria-")) host.setAttribute(name, val);
|
||||
else if (view) view.contentDOM.setAttribute(name, val);
|
||||
else aria.set(name, String(val));
|
||||
},
|
||||
removeAttribute(name) {
|
||||
(name.startsWith("aria-") ? view.contentDOM : host).removeAttribute(name);
|
||||
if (!name.startsWith("aria-")) host.removeAttribute(name);
|
||||
else if (view) view.contentDOM.removeAttribute(name);
|
||||
else aria.set(name, null);
|
||||
},
|
||||
addEventListener(type, fn) {
|
||||
if (!listeners.has(type)) listeners.set(type, new Set());
|
||||
@@ -172,11 +227,21 @@ export function makeMdField({ id, className, value = "", placeholder: hint, labe
|
||||
fire("input");
|
||||
},
|
||||
destroy() {
|
||||
if (gone) return;
|
||||
gone = true;
|
||||
listeners.clear();
|
||||
view.destroy();
|
||||
if (view) view.destroy();
|
||||
},
|
||||
};
|
||||
if (off) host.classList.add("is-disabled");
|
||||
host.field = field;
|
||||
if (cm) build();
|
||||
else
|
||||
loadEditor().then(
|
||||
() => {
|
||||
if (!gone) build();
|
||||
},
|
||||
() => {}
|
||||
);
|
||||
return field;
|
||||
}
|
||||
Reference in new issue
Block a user