Files
alkane-os/third-party/flare-ui/dash-editor/editor/dashboard-json.js
T
x ad31cfbafa Vendor flare-ui v0.2.9 and prove the schema-driven UI in an image
Spike, not the final UI: it answers whether flare-ui can drive Alkane OS's
on-box pages, and what it costs.

What works:

- schema/alkane-bb.json authors the sidebar as data (3 sections, 14 tabs);
- src/bin/ui-spike.rs renders it through flare_ui::nav + flare_ui::shell, so
  the unified sidebar, ordering and surface filtering come from the schema
  rather than from hand-written HTML;
- the render uses the shared bf- design tokens, the same ones the rest of the
  suite uses.

Size, measured rather than assumed (armv7, release, stripped):

  ui-baseline   323 KiB   (maud + serde_json, no flare-ui)
  ui-spike      771 KiB   (flare-ui + wschema + sha2)
  marginal      448 KiB

Vendoring: third-party/flare-ui is the full v0.2.9 tree pinned in
flare-ui/flare-ui.lock (a partial archive does not compile, since the crate
include_str!s assets from dash-editor/). vendor/ is re-generated as a cargo
directory source with the new registry dependencies (maud, sha2, typenum,
digest, generic-array, block-buffer, crypto-common, cfg-if, etc.) so the build
still works offline. third-party/README.md records how to re-pin.
2026-11-04 14:06:25 -06:00

159 lines
6.9 KiB
JavaScript

// Colour for the raw dashboard JSON pane.
//
// The pane is a <textarea>, and a textarea draws one colour: it holds text, not
// markup, which is exactly why it is the control that posts and the control an
// operator with no scripting types into. So the colour goes behind it. This
// file wraps the textarea, puts a <pre> under it carrying the same text as
// spans, and makes the textarea's own glyphs transparent with a visible caret.
// The two layers share their font, size, line height, padding and wrapping
// through studio.css, because any difference between them is colour that drifts
// away from the letters it belongs to.
//
// What this does not do is change what posts. The textarea is still the one
// control, still named `document`, still holding exactly the text the server
// rendered or the operator typed. The <pre> is aria-hidden and has no name: to
// a screen reader this pane is the labelled textarea it was before, and with no
// script it is that textarea unchanged.
//
// The indentation is the server's. dashboard::editing_text renders the document
// through serde_json::to_string_pretty and dashboard-editor.js writes it back
// with the same two-space indent, so this file never reformats anything; it
// only colours what is there. A document this build cannot parse is coloured as
// far as it tokenises and left alone after that, because that text is the work.
(function () {
var root = document.querySelector("[data-dashboard-editor]");
var area = root && root.querySelector("[data-dashboard-raw]");
if (!area || area.dataset.jsonInk) return;
// Past this, colour is not worth the nodes. A document at the byte cap is
// tens of thousands of tokens, which is tens of thousands of spans rebuilt on
// every keystroke; the pane stays a plain textarea instead, which is what it
// was. The cap is well above any document the palette can build: 64 blocks of
// every field at its maximum indents to a fraction of this.
var INK_MAX = 40 * 1024;
var wrap = document.createElement("div");
wrap.className = "bf-code-edit";
var ink = document.createElement("pre");
ink.className = "bf-code-edit__ink";
// Decoration standing in for text that is already in the accessibility tree,
// one layer up. Without this the pane reads out twice.
ink.setAttribute("aria-hidden", "true");
var code = document.createElement("code");
ink.appendChild(code);
// Moving a node in the DOM takes the focus off it, and this script runs after
// dashboard-editor.js has already put the caret in this pane for a document
// the build cannot read. That is the one case where the text is the work, so
// losing the caret here loses the only thing pointing at what to repair.
// Carried across by hand, selection included, because a bare focus() would
// put the caret back at the start of the text rather than where it was.
var hadFocus = document.activeElement === area;
var start = area.selectionStart;
var end = area.selectionEnd;
area.parentNode.insertBefore(wrap, area);
wrap.appendChild(ink);
wrap.appendChild(area);
area.classList.add("bf-code-edit__area");
if (hadFocus) {
area.focus({ preventScroll: true });
// Guarded: a selection range is meaningless on a control that never had
// one, and assigning to it on a detached or empty textarea throws in some
// browsers rather than being ignored.
if (typeof start === "number" && typeof end === "number") {
try {
area.setSelectionRange(start, end);
} catch (ignored) {
// A browser that refuses the range still has the focus, which is the
// part that matters.
}
}
}
// So a second copy of this script, or a second pass of it, leaves the pane
// alone rather than nesting another wrapper around it.
area.dataset.jsonInk = "on";
// Strings first, so a brace inside one is never punctuation and a key is the
// string that has a colon after it. Numbers are matched without a leading
// word boundary because JSON allows a minus sign, which is not a word
// character and would put the boundary in the wrong place.
var TOKEN = new RegExp(
"(\"(?:\\\\u[0-9a-fA-F]{4}|\\\\[^u]|[^\\\\\"])*\")(\\s*:)?" +
"|\\b(?:true|false)\\b" +
"|\\bnull\\b" +
"|-?\\d+(?:\\.\\d+)?(?:[eE][+-]?\\d+)?",
"g"
);
function classOf(match, string, colon) {
if (string) return colon ? "key" : "str";
if (match === "true" || match === "false") return "bool";
if (match === "null") return "null";
return "num";
}
/// Rebuild the coloured layer from the text in the textarea.
///
/// Built as nodes with textContent rather than as a string of HTML. The
/// document is operator-supplied text, and a title containing "<" is a title,
/// not a tag: assembling markup here would be the one place in this editor
/// where it could stop being one.
function paint() {
var text = area.value;
code.textContent = "";
if (text.length > INK_MAX) {
wrap.classList.add("bf-code-edit--plain");
return;
}
wrap.classList.remove("bf-code-edit--plain");
var frag = document.createDocumentFragment();
var last = 0;
var match;
TOKEN.lastIndex = 0;
while ((match = TOKEN.exec(text)) !== null) {
if (match.index > last) {
frag.appendChild(document.createTextNode(text.slice(last, match.index)));
}
var span = document.createElement("span");
span.className = "bf-json__" + classOf(match[0], match[1], match[2]);
span.textContent = match[1] || match[0];
frag.appendChild(span);
// The colon is punctuation, not part of the key, and it is inside the
// match so that a string is only a key when one follows it.
if (match[1] && match[2]) frag.appendChild(document.createTextNode(match[2]));
last = match.index + match[0].length;
// A zero-length match cannot happen with this pattern, but a regex loop
// that trusts that is one edit away from spinning forever.
if (match[0].length === 0) TOKEN.lastIndex += 1;
}
if (last < text.length) {
frag.appendChild(document.createTextNode(text.slice(last)));
}
// A trailing newline, so the last line can scroll to the same place the
// textarea puts it. Without it the two layers disagree by one line at the
// bottom of a long document.
frag.appendChild(document.createTextNode("\n"));
code.appendChild(frag);
}
function follow() {
ink.scrollTop = area.scrollTop;
ink.scrollLeft = area.scrollLeft;
}
area.addEventListener("input", function () {
paint();
follow();
});
area.addEventListener("scroll", follow);
// The canvas writes this pane on every render, and assigning to `value` fires
// no input event. dashboard-editor.js syncs the pane before it calls its draw
// listeners, so by here the text is the one the document just became.
var editor = window.FlareDashboardEditor;
if (editor) editor.onDraw(function () { paint(); follow(); });
else paint();
}());