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.
1361 lines
57 KiB
JavaScript
1361 lines
57 KiB
JavaScript
(function () {
|
|
"use strict";
|
|
|
|
var root = document.querySelector("[data-dashboard-editor]");
|
|
if (!root) return;
|
|
|
|
var canvas = root.querySelector("[data-dashboard-canvas]");
|
|
var empty = root.querySelector("[data-dashboard-empty]");
|
|
var properties = root.querySelector("[data-dashboard-properties]");
|
|
// The document lives in one control, the textarea in the raw pane: the
|
|
// server renders the stored text into it, the form posts it, and this
|
|
// script rewrites it from the canvas. There was a second hidden copy of it
|
|
// here, which is what made the page lie with scripting off: the hidden one
|
|
// was what posted and this one was filled by sync() alone, so the alert
|
|
// pointed an operator at an empty box.
|
|
var raw = root.querySelector("[data-dashboard-raw]");
|
|
var rawError = root.querySelector("[data-dashboard-raw-error]");
|
|
// The disclosure the raw text lives in, so a document that cannot be
|
|
// repaired anywhere else can be repaired here. A browser draws no
|
|
// validation bubble for a control it is not rendering, so leaving this
|
|
// closed is what made Save refuse in silence.
|
|
var rawPane = raw.closest("details");
|
|
var addButtons = root.querySelectorAll("[data-add-widget]");
|
|
var paletteNote = root.querySelector("[data-dashboard-palette-note]");
|
|
// The shell's elements, all server-rendered: this script moves and fills
|
|
// them rather than building them, which is most of why it fits the page
|
|
// budget. tests/browser/editors.html carries the same hooks.
|
|
var studio = root.querySelector("[data-dashboard-studio]");
|
|
var toolbar = root.querySelector("[data-block-toolbar]");
|
|
var toolbarLabel = root.querySelector("[data-block-toolbar-label]");
|
|
var toolbarButtons = Array.from(toolbar.querySelectorAll("[data-block-action]"));
|
|
var insertButton = toolbar.querySelector("[data-block-action=\"insert\"]");
|
|
var duplicateButton = toolbar.querySelector("[data-block-action=\"duplicate\"]");
|
|
var groupRowButton = toolbar.querySelector("[data-block-action=\"group-row\"]");
|
|
var groupSectionButton = toolbar.querySelector("[data-block-action=\"group-section\"]");
|
|
var ungroupButton = toolbar.querySelector("[data-block-action=\"ungroup\"]");
|
|
var inserter = root.querySelector("[data-block-inserter]");
|
|
var insertButtons = Array.from(inserter.querySelectorAll("[data-insert-block]"));
|
|
var undoButton = root.querySelector("[data-dashboard-undo]");
|
|
var redoButton = root.querySelector("[data-dashboard-redo]");
|
|
var shortcuts = root.querySelector("[data-dashboard-shortcuts]");
|
|
var announcer = root.querySelector("[data-dashboard-status]");
|
|
var listView = root.querySelector("[data-dashboard-list-view]");
|
|
var listTree = root.querySelector("[data-dashboard-list]");
|
|
var listCount = root.querySelector("[data-list-view-count]");
|
|
var patterns = root.querySelector("[data-dashboard-patterns]");
|
|
var patternList = root.querySelector("[data-dashboard-pattern-list]");
|
|
var variationButtons = Array.from(root.querySelectorAll("[data-row-variation]"));
|
|
// The document model, loaded before this script. Everything that knows the
|
|
// shape of a v3 document lives there and is named here, so every call site
|
|
// below reads as it did when the two were one file.
|
|
//
|
|
// Guarded, because this is a second file that can fail to arrive on its own:
|
|
// a blocked request, or a stale copy cached from before the split. Without
|
|
// the guard the next line throws, this script stops before it exports
|
|
// anything, and the page is left with a palette that looks live and answers
|
|
// nothing. Returning instead leaves exactly the page a browser with
|
|
// scripting off gets, which is a page that still works: the raw pane holds
|
|
// the document and posts it.
|
|
var model = window.FlareDashboardDoc;
|
|
if (!model || !model.PATTERNS || model.VERSION === undefined) return;
|
|
var BLOCK_CAP = model.BLOCK_CAP;
|
|
var NODE_CAP = model.NODE_CAP;
|
|
var DEPTH_CAP = model.DEPTH_CAP;
|
|
var ROW_CAP = model.ROW_CAP;
|
|
var VERSION = model.VERSION;
|
|
var KINDS = model.KINDS;
|
|
var SPACE_CLASS = model.SPACE_CLASS;
|
|
var kindOf = model.kindOf;
|
|
var childrenOf = model.childrenOf;
|
|
var walk = model.walk;
|
|
var inRow = model.inRow;
|
|
var attach = model.attach;
|
|
// Which cap is binding decides what the note says. A document can reach the
|
|
// node cap with room left under the block cap (a tree of mostly containers),
|
|
// and a note naming the wrong number reads as a bug in the count.
|
|
function capNote() {
|
|
return countNodes() >= NODE_CAP
|
|
? "This dashboard holds " + NODE_CAP + " blocks, containers included, " +
|
|
"which is the most one document can carry. Remove one to add another."
|
|
: "This dashboard holds " + BLOCK_CAP + " blocks, which is the most " +
|
|
"one document can carry. Remove one to add another.";
|
|
}
|
|
// How far back Undo goes: stated rather than unbounded, and stated on the
|
|
// page, because the shortcut list's [data-undo-depth] is filled in from
|
|
// here. The stack is this page's and does not survive a reload.
|
|
var UNDO_DEPTH = 50;
|
|
// Edits to one field of one block inside this window are one undo step.
|
|
// Per-keystroke undo across a structural editor is worse than none: it
|
|
// removes a letter where the operator meant to remove the change.
|
|
var COALESCE_MS = 1000;
|
|
// Between the toolbar and the block it belongs to.
|
|
var TOOLBAR_GAP = 6;
|
|
var doc;
|
|
// Scripts that draw from this document and are told when it changes. The
|
|
// list view is one; keeping it out of this file is what keeps this file
|
|
// under the per-file budget, and a listener list is a smaller seam than
|
|
// exporting the render function itself.
|
|
var drawn = [];
|
|
// Reading the pane, and explaining a document the canvas cannot draw, are
|
|
// dashboard-raw.js's. Guarded like the model above: without the pane this
|
|
// script cannot tell a broken document from a good one, so it leaves the
|
|
// page as a browser with scripting off gets it.
|
|
var rawModel = window.FlareDashboardRaw;
|
|
if (!rawModel) return;
|
|
var pane = rawModel.attach({
|
|
field: raw,
|
|
error: rawError,
|
|
disclosure: rawPane,
|
|
model: model,
|
|
refresh: function () { renderHistory(); }
|
|
});
|
|
|
|
doc = pane.read(raw.value);
|
|
if (!doc) doc = { version: VERSION, blocks: [] };
|
|
|
|
var selected = null;
|
|
// "nav" while the canvas owns the arrow keys, "edit" while the properties
|
|
// panel has the caret. Two explicit modes rather than a guess about what a
|
|
// click meant, and the reason the arrows are free to navigate at all.
|
|
var mode = "nav";
|
|
// Serialized documents, oldest first, and the same the other way. Snapshots
|
|
// rather than inverse operations: a document is small, bounded by the 128 KB
|
|
// the server takes, and an inverse log is where undo quietly diverges.
|
|
var past = [];
|
|
var future = [];
|
|
var coalesceKey = "";
|
|
var coalesceAt = 0;
|
|
|
|
// The gate every canvas control passes, and the reveal that goes with it.
|
|
//
|
|
// It used to ask whether Save was refusing, and that is not the same
|
|
// question: the version state deliberately leaves Save enabled, so a pasted
|
|
// version 1 document was live on screen and gone on the next click. A canvas
|
|
// that is not drawing the pane's text holds a different document from the
|
|
// one that would be posted, so nothing here may change the document while
|
|
// that is true.
|
|
function canvasReady() {
|
|
if (pane.draws()) return true;
|
|
if (rawPane) rawPane.open = true;
|
|
// Only a state that blocks Save has a bubble to draw.
|
|
if (raw.getAttribute("aria-invalid") === "true") raw.reportValidity();
|
|
// And said, because opening a pane that is already open is not visible.
|
|
announce("The canvas is not editing this document. Correct it in the raw " +
|
|
"pane below.");
|
|
return false;
|
|
}
|
|
|
|
// Interactive validation fires this on the control Save is refusing, which
|
|
// is the one thing that happens whether the refusal came from here or from
|
|
// the browser. An operator who collapses the pane over text it has already
|
|
// refused would otherwise be back where this started: a Save that does
|
|
// nothing and a bubble with nowhere to be drawn.
|
|
raw.addEventListener("invalid", function () {
|
|
if (rawPane) rawPane.open = true;
|
|
});
|
|
|
|
function selectedEntry() {
|
|
// Searched through the whole tree, not just the root list. While this
|
|
// looked only at `doc.blocks`, every edit to a block inside a row silently
|
|
// did nothing: the panel drew the block's values, the handler found no
|
|
// block to write them to, and the document kept the old ones.
|
|
return selected ? findEntry(selected) : null;
|
|
}
|
|
|
|
function selectedBlock() {
|
|
var entry = selectedEntry();
|
|
return entry && entry.block;
|
|
}
|
|
|
|
// --- the tree ---------------------------------------------------------
|
|
//
|
|
// The document is a tree, not a list, so every operation that used to be an
|
|
// array index is a walk. dashboard-doc.js owns the walk; these bind it to
|
|
// the one document this page is editing, so the rest of the file asks about
|
|
// a block rather than passing `doc` down every call.
|
|
|
|
//
|
|
// render() walks the tree once and these answer from that walk until it
|
|
// has drawn, which changes nothing in the document (flare#55). The palette
|
|
// asks per button, so a keystroke in a field used to be about 48 walks.
|
|
var walked = null;
|
|
|
|
function flat() {
|
|
return walked || model.flat(doc);
|
|
}
|
|
|
|
function findEntry(id) {
|
|
return flat().find(function (e) { return e.block.id === id; }) || null;
|
|
}
|
|
|
|
function countLeaves() {
|
|
return flat().filter(function (e) { return !childrenOf(e.block); }).length;
|
|
}
|
|
|
|
function countNodes() {
|
|
return flat().length;
|
|
}
|
|
|
|
/// Room for this many more nodes, this many of them leaves.
|
|
function room(nodes, leaves) {
|
|
return countNodes() + nodes <= NODE_CAP && countLeaves() + leaves <= BLOCK_CAP;
|
|
}
|
|
|
|
/// Remove a block from wherever it is, and say where it was.
|
|
function detach(id) {
|
|
var entry = findEntry(id);
|
|
if (!entry) return null;
|
|
entry.siblings.splice(entry.index, 1);
|
|
return entry;
|
|
}
|
|
|
|
// The canvas writes the document back into the control that posts it, and
|
|
// only while it is drawing what that control holds. Otherwise the text in it
|
|
// is the stored or typed document the canvas could not read, and overwriting
|
|
// that would discard the only copy of what has to be repaired.
|
|
// Indented to match what the server renders into this pane, so the text does
|
|
// not reflow into one line the first time the canvas touches it. Two spaces,
|
|
// which is serde_json::to_string_pretty's own indent: the pane is read and
|
|
// edited by hand in both directions, and a document that changes shape
|
|
// depending on which side last wrote it reads as a spurious diff.
|
|
//
|
|
// Storage is unaffected: `save` re-serialises compactly and the database cap
|
|
// is measured there. This text is posted back as it stands, which `read`
|
|
// bounds by MAX_BYTES; see dashboard::editing_text for that headroom.
|
|
function sync() {
|
|
if (!pane.draws()) return;
|
|
raw.value = JSON.stringify(doc, null, 2);
|
|
}
|
|
|
|
/// One block as a canvas node.
|
|
///
|
|
/// A leaf is a button. A group is a container holding its children's nodes,
|
|
/// with its own button first so the group itself can be selected, moved and
|
|
/// ungrouped. The canvas draws the same shape the live page does, which is
|
|
/// the whole point of the flow model: there is no separate idea of where a
|
|
/// block sits, only the order it is written in.
|
|
function node(entry) {
|
|
var block = entry.block;
|
|
var kids = childrenOf(block);
|
|
var button = document.createElement("button");
|
|
button.type = "button";
|
|
button.className = "bf-studio-node" + (kids ? " bf-studio-node--group" : "");
|
|
// The frame properties that change a block's shape on the page. The canvas
|
|
// claims to draw the shape the page will, so it has to carry them: a
|
|
// minimum height and a deliberate gap above were both invisible here and
|
|
// visible on the dashboard, which is a preview that misleads. They go on
|
|
// whichever element is the block's outer box, which for a group is the
|
|
// container rather than the button that selects it.
|
|
var frame = "";
|
|
if (inRow(entry)) frame += " bf-w" + (block.width || 1);
|
|
if (block.min_rows) frame += " bf-minh" + block.min_rows;
|
|
if (block.space_before && SPACE_CLASS[block.space_before]) {
|
|
frame += " bf-space-" + SPACE_CLASS[block.space_before];
|
|
}
|
|
if (!kids) button.className += frame;
|
|
button.dataset.widgetId = block.id;
|
|
button.setAttribute("aria-pressed", String(block.id === selected));
|
|
// Roving tabindex: the canvas is one Tab stop rather than one per block,
|
|
// and the arrows move inside it.
|
|
button.tabIndex = block.id === selected ? 0 : -1;
|
|
|
|
var kind = document.createElement("span");
|
|
kind.className = "bf-studio-node__kind";
|
|
kind.textContent = kindOf(block) === "group"
|
|
? (block.body.flow === "row" ? "row" : "section")
|
|
: kindOf(block);
|
|
var title = document.createElement("span");
|
|
title.className = "bf-studio-node__title";
|
|
title.textContent = block.title;
|
|
button.appendChild(kind);
|
|
button.appendChild(title);
|
|
if (!kids) return button;
|
|
|
|
var box = document.createElement("div");
|
|
box.className = "bf-studio-group" +
|
|
(block.body.flow === "row" ? " bf-studio-group--row" : "") + frame;
|
|
box.dataset.groupId = block.id;
|
|
box.appendChild(button);
|
|
var inner = document.createElement("div");
|
|
inner.className = "bf-studio-group__children";
|
|
kids.forEach(function (child, index) {
|
|
inner.appendChild(node({ block: child, parent: block, siblings: kids, index: index }));
|
|
});
|
|
// A container is never a dead end. An empty group offers a way in rather
|
|
// than being a box nothing can be put into.
|
|
if (!kids.length) {
|
|
var hint = document.createElement("p");
|
|
hint.className = "bf-studio-group__empty";
|
|
hint.textContent = "Empty. Select this group and insert a block.";
|
|
inner.appendChild(hint);
|
|
}
|
|
box.appendChild(inner);
|
|
return box;
|
|
}
|
|
|
|
function nodeFor(id) {
|
|
return Array.from(canvas.querySelectorAll("[data-widget-id]")).find(function (item) {
|
|
return item.dataset.widgetId === id;
|
|
}) || null;
|
|
}
|
|
|
|
function focusSelected() {
|
|
var node = selected ? nodeFor(selected) : null;
|
|
if (node) node.focus({ preventScroll: true });
|
|
}
|
|
|
|
// What changed, for someone who cannot see it change. A visible outline is
|
|
// not an announcement, so selection, mode and every operation that
|
|
// rearranges the document say so here.
|
|
function announce(message) {
|
|
if (announcer.textContent !== message) announcer.textContent = message;
|
|
}
|
|
|
|
function announceSelection() {
|
|
var block = selectedBlock();
|
|
if (!block) {
|
|
announce("No block selected.");
|
|
return;
|
|
}
|
|
// Counted against the whole tree, which is what the arrows walk. Counting
|
|
// against doc.blocks announced a nested block as "5 of 2", because the
|
|
// root list is not the list the position came from.
|
|
var list = ordered();
|
|
announce(kindOf(block) + " block, " + (block.title.trim() || "an untitled block") +
|
|
", " + (list.indexOf(block) + 1) + " of " + list.length + ".");
|
|
}
|
|
|
|
// Document order, which is now the same thing as the order on screen: the
|
|
// tree is walked depth first and that is exactly what the page draws. The
|
|
// grid needed a sort here because a move rewrote coordinates and left the
|
|
// array alone; a flow document has no such gap between the two.
|
|
function ordered() {
|
|
return flat().map(function (entry) { return entry.block; });
|
|
}
|
|
|
|
function setMode(next) {
|
|
if (mode === next) return;
|
|
mode = next;
|
|
studio.setAttribute("data-mode", mode);
|
|
announce(mode === "edit"
|
|
? "Editing this block's settings. Escape returns to the canvas."
|
|
: "Navigating blocks.");
|
|
}
|
|
|
|
function select(id) {
|
|
// Above the early return: clicking the block already being edited is the
|
|
// keyboard coming back to the canvas, and the mode said otherwise.
|
|
setMode("nav");
|
|
if (selected === id) return;
|
|
selected = id;
|
|
render();
|
|
announceSelection();
|
|
}
|
|
|
|
// Nothing selected, which a pointer needs a way to reach as much as a key
|
|
// does: Escape was the only one, and a touch device has no Escape.
|
|
function clearSelection() {
|
|
selected = null;
|
|
setMode("nav");
|
|
render();
|
|
announce("Selection cleared.");
|
|
}
|
|
|
|
// Move the selection along the document rather than the block. The arrows
|
|
// used to nudge a block, which left nothing to navigate with; the block
|
|
// moves on Alt with the same arrow, and on the toolbar with no chord.
|
|
function step(delta) {
|
|
var list = ordered();
|
|
var at = list.findIndex(function (block) { return block.id === selected; });
|
|
var to = at < 0 ? 0 : Math.max(0, Math.min(list.length - 1, at + delta));
|
|
if (!list[to] || list[to].id === selected) return;
|
|
select(list[to].id);
|
|
focusSelected();
|
|
}
|
|
|
|
// The palette says why it stopped. A full document disables the tools, and
|
|
// the note is what makes a dead palette readable rather than broken.
|
|
function renderPalette() {
|
|
// Per kind, not per document: a container is not a leaf, so a document at
|
|
// the block cap still has room for a row to reorganise those blocks into.
|
|
// One flag for every button refused that row, and reorganising is exactly
|
|
// what an operator at the cap is trying to do.
|
|
addButtons.forEach(function (button) {
|
|
button.disabled = full(button.dataset.addWidget);
|
|
});
|
|
// Only on a change: it is a live region, and rewriting the same sentence
|
|
// on every keystroke in a property field would announce it every time.
|
|
var note = "";
|
|
if (full("metric")) {
|
|
note = capNote();
|
|
// A container is not a block this cap counts, and arranging what is
|
|
// already here is exactly what an operator at the cap wants to do. The
|
|
// note says so rather than leaving two live buttons under a sentence
|
|
// that reads as though nothing can be added.
|
|
if (!full("row")) {
|
|
note += " Rows and sections can still be added, to arrange the " +
|
|
"blocks already here.";
|
|
}
|
|
}
|
|
if (paletteNote.textContent !== note) paletteNote.textContent = note;
|
|
// Everything that adds a block stops at the same number, not just the
|
|
// palette: the inserter and Duplicate build one too, and a control that
|
|
// stays live past the cap is the refused save this note exists to prevent.
|
|
insertButtons.forEach(function (button) {
|
|
button.disabled = full(button.dataset.insertBlock);
|
|
});
|
|
var entry = selectedEntry();
|
|
var block = entry ? entry.block : null;
|
|
duplicateButton.disabled = !block || full(childrenOf(block) ? "group" : "metric");
|
|
// The row layouts add blocks too, so they stop where the palette stops.
|
|
// They did not, and the refusal was announced into a region a sighted
|
|
// operator never sees, so the control looked live and did nothing.
|
|
variationButtons.forEach(function (button) {
|
|
var shares = button.dataset.rowVariation.split(",").length;
|
|
button.disabled = !room(shares, shares);
|
|
});
|
|
// Group and Ungroup each answer only where they mean something. A lit
|
|
// Ungroup on a block that is not a container is a click that did nothing
|
|
// and said nothing, and a lit Group at the depth cap is the same refusal
|
|
// one step later.
|
|
groupRowButton.disabled = !canGroup(entry);
|
|
groupSectionButton.disabled = !canGroup(entry);
|
|
ungroupButton.disabled = !(block && childrenOf(block));
|
|
// And the control that opens that list, which used to stay live and open
|
|
// a menu whose every item was dark.
|
|
insertButton.disabled = insertButtons.every(function (button) {
|
|
return button.disabled;
|
|
});
|
|
}
|
|
|
|
// --- history -------------------------------------------------------------
|
|
|
|
// Record the document as it stands, before whatever is about to change it.
|
|
//
|
|
// `key` is what coalesces: a burst of edits to one field of one block inside
|
|
// COALESCE_MS is one step, so undo takes back a title rather than a letter.
|
|
// A structural operation passes no key and is always its own step, because
|
|
// inserting, removing, duplicating and moving are exactly what a person
|
|
// undoes. Redo is dropped on any new change, which is the only honest thing
|
|
// to do with a future that no longer follows from the present.
|
|
function remember(key) {
|
|
var now = Date.now();
|
|
if (key && key === coalesceKey && now - coalesceAt < COALESCE_MS) {
|
|
coalesceAt = now;
|
|
return;
|
|
}
|
|
past.push(JSON.stringify(doc));
|
|
if (past.length > UNDO_DEPTH) past.shift();
|
|
future.length = 0;
|
|
coalesceKey = key || "";
|
|
coalesceAt = now;
|
|
}
|
|
|
|
// Both directions are one move in opposite tubs, and both go through
|
|
// render(), which calls sync(): what comes out of the stack is written into
|
|
// the textarea that posts, so undo cannot diverge from the pane.
|
|
function restore(from, to, said) {
|
|
if (!canvasReady() || from.length === 0) return;
|
|
to.push(JSON.stringify(doc));
|
|
doc = JSON.parse(from.pop());
|
|
coalesceKey = "";
|
|
if (!doc.blocks.some(function (block) { return block.id === selected; })) selected = null;
|
|
setMode("nav");
|
|
render();
|
|
// Not focusSelected(): an undo that takes the selected block back has
|
|
// nothing to focus, render() has already removed the node that held the
|
|
// focus, and on <body> none of the keys reach a handler at all.
|
|
var landing = (selected ? nodeFor(selected) : null) ||
|
|
canvas.querySelector("[data-widget-id]") ||
|
|
root.querySelector("[data-add-widget]");
|
|
if (landing) landing.focus({ preventScroll: true });
|
|
// The stack this move drew from, and what is left in it. `past.length` was
|
|
// reported both ways, so a redo counted the other stack and said "back".
|
|
var left = from.length;
|
|
announce(said + " " + left + (left === 1 ? " step" : " steps") +
|
|
" left to " + (from === past ? "undo" : "redo") + ".");
|
|
}
|
|
|
|
function renderHistory() {
|
|
// restore() refuses while the canvas is not drawing the pane, and these
|
|
// two were the only canvas controls that stayed lit over that refusal.
|
|
var frozen = !pane.draws();
|
|
undoButton.disabled = frozen || past.length === 0;
|
|
redoButton.disabled = frozen || future.length === 0;
|
|
}
|
|
|
|
// --- the block toolbar ---------------------------------------------------
|
|
|
|
// One toolbar, moved onto the selected block and always above it: studio.css
|
|
// keeps a lane for it over the first row, and a row gap wide enough for it
|
|
// where the canvas is one column. It used to drop below when there was no
|
|
// room above, and on a phone that covered the next block end to end and
|
|
// swallowed every tap on it. Absolute inside the canvas and never fixed,
|
|
// which is measured against the viewport and slides off on any scroll.
|
|
function placeToolbar() {
|
|
var block = selectedBlock();
|
|
var node = block ? nodeFor(block.id) : null;
|
|
if (!node) {
|
|
toolbar.hidden = true;
|
|
closeInserter();
|
|
return;
|
|
}
|
|
toolbar.hidden = false;
|
|
toolbarLabel.textContent = kindOf(block) + ": " +
|
|
(block.title.trim() || "untitled block");
|
|
armToolbar();
|
|
// Parked in the canvas's own lane rather than floated over the selected
|
|
// block. The grid gave every block a 64px row to sit in, so a bar above
|
|
// one landed in the gap; a flow canvas stacks blocks tightly, and the
|
|
// same bar covered the whole block above it. Measured: a 96px toolbar
|
|
// over a 63px block hid it completely.
|
|
//
|
|
// A fixed lane also drops the measuring: there is no clamp to get wrong
|
|
// and no case where the bar and the block it acts on fight for the same
|
|
// pixels.
|
|
toolbar.style.top = "";
|
|
toolbar.style.left = "";
|
|
placeInserter();
|
|
}
|
|
|
|
|
|
|
|
// Exactly one button in the toolbar is tabbable, so the row is one Tab stop
|
|
// and the arrows move along it (the ARIA toolbar pattern). A disabled button
|
|
// is not a stop: Duplicate goes dark at the block cap, and leaving the stop
|
|
// on it would make the arrows land on nothing.
|
|
function toolbarStops() {
|
|
return toolbarButtons.filter(function (button) { return !button.disabled; });
|
|
}
|
|
|
|
function giveStop(button) {
|
|
toolbarButtons.forEach(function (each) { each.tabIndex = -1; });
|
|
if (button) button.tabIndex = 0;
|
|
}
|
|
|
|
function armToolbar() {
|
|
var stops = toolbarStops();
|
|
giveStop(stops.filter(function (button) { return button.tabIndex === 0; })[0] ||
|
|
stops[0] || toolbarButtons[0]);
|
|
}
|
|
|
|
function rove(to) {
|
|
var stops = toolbarStops();
|
|
if (stops.length === 0) return;
|
|
var at = ((to % stops.length) + stops.length) % stops.length;
|
|
giveStop(stops[at]);
|
|
stops[at].focus();
|
|
}
|
|
|
|
// --- the inserter --------------------------------------------------------
|
|
|
|
function setInserter(open) {
|
|
if (open && !selectedBlock()) return;
|
|
inserter.hidden = !open;
|
|
insertButton.setAttribute("aria-expanded", String(Boolean(open)));
|
|
if (!open) return;
|
|
placeInserter();
|
|
var first = insertButtons.find(function (button) { return !button.disabled; });
|
|
if (first) first.focus();
|
|
}
|
|
|
|
function closeInserter() {
|
|
setInserter(false);
|
|
}
|
|
|
|
function placeInserter() {
|
|
if (inserter.hidden) return;
|
|
inserter.style.top =
|
|
(parseFloat(toolbar.style.top || "0") + toolbar.offsetHeight + TOOLBAR_GAP) + "px";
|
|
inserter.style.left = toolbar.style.left || "0px";
|
|
}
|
|
|
|
function render() {
|
|
var focusedWidget = document.activeElement && document.activeElement.dataset.widgetId;
|
|
canvas.querySelectorAll("[data-widget-id],[data-group-id]").forEach(function (item) {
|
|
if (!item.parentElement || !item.parentElement.closest("[data-group-id]")) item.remove();
|
|
});
|
|
empty.hidden = doc.blocks.length > 0;
|
|
// Before the toolbar rather than at the end, so the document order a Tab
|
|
// press follows is the blocks and then the toolbar that acts on them.
|
|
doc.blocks.forEach(function (block, index) {
|
|
canvas.insertBefore(node({ block: block, parent: null, siblings: doc.blocks, index: index }), toolbar);
|
|
});
|
|
// With nothing selected the canvas still has to be reachable, so the first
|
|
// block holds the stop until something is.
|
|
var first = canvas.querySelector("[data-widget-id]");
|
|
if (first && !selected) first.tabIndex = 0;
|
|
walked = model.flat(doc);
|
|
try {
|
|
renderPalette();
|
|
renderPatterns();
|
|
renderHistory();
|
|
// Before the listeners below, not after. One of them draws this pane's
|
|
// text (dashboard-json.js), and a listener that ran first would colour
|
|
// the document as it stood one edit ago.
|
|
sync();
|
|
// Whatever else draws from this document: the list view and the
|
|
// properties panel are separate scripts, so they are told rather than
|
|
// called. Inside the one walk, because neither changes the document.
|
|
drawn.forEach(function (listener) { listener(); });
|
|
} finally {
|
|
walked = null;
|
|
}
|
|
if (focusedWidget) {
|
|
var focused = Array.from(canvas.querySelectorAll("[data-widget-id]")).find(function (item) {
|
|
return item.dataset.widgetId === focusedWidget;
|
|
});
|
|
if (focused) focused.focus({ preventScroll: true });
|
|
}
|
|
// Last, because it measures: the nodes have to be in the document and laid
|
|
// out before the toolbar can be put above one of them.
|
|
placeToolbar();
|
|
}
|
|
|
|
// --- patterns ------------------------------------------------------------
|
|
|
|
/// Draw the pattern list, with a pattern the document has no room for dark.
|
|
///
|
|
/// Room is the pattern's whole subtree, not one block. A pattern that fits
|
|
/// by the count of its top block and not by its contents would be half a
|
|
/// pattern and a refused save.
|
|
function renderPatterns() {
|
|
if (!patternList) return;
|
|
patternList.replaceChildren();
|
|
model.PATTERNS.forEach(function (pattern) {
|
|
var size = model.patternSize(pattern.spec);
|
|
var button = document.createElement("button");
|
|
button.type = "button";
|
|
button.className = "bf-studio-tool";
|
|
button.dataset.pattern = pattern.id;
|
|
button.disabled = !room(size.total, size.leaves);
|
|
var label = document.createElement("strong");
|
|
label.textContent = pattern.label;
|
|
var hint = document.createElement("span");
|
|
hint.textContent = pattern.hint;
|
|
button.appendChild(label);
|
|
button.appendChild(hint);
|
|
patternList.appendChild(button);
|
|
});
|
|
patterns.hidden = false;
|
|
}
|
|
|
|
/// Insert a pattern at the end of the document, as one undo step.
|
|
function addPattern(id) {
|
|
var pattern = model.PATTERNS.find(function (each) { return each.id === id; });
|
|
if (!pattern) return;
|
|
var size = model.patternSize(pattern.spec);
|
|
if (!room(size.total, size.leaves)) {
|
|
announce(capNote());
|
|
renderPatterns();
|
|
return;
|
|
}
|
|
remember("");
|
|
var block = model.buildPattern(pattern.spec, model.ids(doc.blocks));
|
|
doc.blocks.push(block);
|
|
selected = block.id;
|
|
setMode("nav");
|
|
render();
|
|
focusSelected();
|
|
announce("Added the " + pattern.label + " pattern. Every block in it is " +
|
|
"an ordinary block: change or remove any of them.");
|
|
}
|
|
|
|
if (patternList) {
|
|
patternList.addEventListener("click", function (event) {
|
|
var button = event.target.closest("[data-pattern]");
|
|
if (!button || !canvasReady()) return;
|
|
addPattern(button.dataset.pattern);
|
|
});
|
|
}
|
|
|
|
/// Fill an empty row with one block per share.
|
|
///
|
|
/// One undo step for the whole row, because what the operator did was
|
|
/// choose a layout, not add three blocks.
|
|
function fillRow(shares) {
|
|
var entry = selected ? findEntry(selected) : null;
|
|
var kids = entry && childrenOf(entry.block);
|
|
if (!kids || kids.length) return;
|
|
if (shares.length > ROW_CAP) return;
|
|
if (!room(shares.length, shares.length)) {
|
|
// Announced only. Writing the palette's visible note here looked like
|
|
// the right answer and was not: renderPalette() below recomputes that
|
|
// note from whether the document is already at the cap, which is a
|
|
// coarser question than whether this particular fill would pass it, and
|
|
// cleared it again in the same turn. What a sighted operator sees is
|
|
// the button, which renderPalette() disables on exactly the condition
|
|
// this branch tests, so a real click never reaches here.
|
|
announce(capNote());
|
|
renderPalette();
|
|
return;
|
|
}
|
|
remember("");
|
|
shares.forEach(function (share) {
|
|
var block = blockOf("metric");
|
|
block.width = share;
|
|
kids.push(block);
|
|
});
|
|
selected = kids[0].id;
|
|
setMode("nav");
|
|
render();
|
|
focusSelected();
|
|
announce("Filled " + entry.block.title + " with " + shares.length +
|
|
" blocks. Change what each one shows in the panel.");
|
|
}
|
|
|
|
properties.addEventListener("click", function (event) {
|
|
var button = event.target.closest("[data-row-variation]");
|
|
if (!button || !canvasReady()) return;
|
|
fillRow(button.dataset.rowVariation.split(",").map(Number));
|
|
});
|
|
|
|
// Against every id in the document, not just the root list: ids are unique
|
|
// document wide, so a stem counted against doc.blocks alone hands the same
|
|
// id to a new block and one already sitting inside a group. The canvas
|
|
// addresses a block by id, so that pair is drawn once and removed together.
|
|
function nextId(kind) {
|
|
return model.nextId(kind, model.ids(doc.blocks));
|
|
}
|
|
|
|
function blockOf(kind) {
|
|
return model.blockOf(kind, model.ids(doc.blocks));
|
|
}
|
|
|
|
/// True when the document has no room for one more block of this shape.
|
|
function full(kind) {
|
|
var isGroup = kind === "row" || kind === "section" || kind === "group";
|
|
return !room(1, isGroup ? 0 : 1);
|
|
}
|
|
|
|
/// Add a block at the end of wherever makes sense.
|
|
///
|
|
/// Inside the selected group when a group is selected, which is what makes
|
|
/// an empty container reachable; otherwise at the end of the document. The
|
|
/// palette appends, and the inserter between blocks is what places one at a
|
|
/// position.
|
|
function add(kind) {
|
|
if (full(kind)) {
|
|
renderPalette();
|
|
return;
|
|
}
|
|
var entry = selected ? findEntry(selected) : null;
|
|
var into = entry && childrenOf(entry.block);
|
|
if (into && into.length >= ROW_CAP && entry.block.body.flow === "row") into = null;
|
|
var depth = entry ? entry.depth : 0;
|
|
// Refuse to nest past what the server accepts, rather than letting the
|
|
// save be the thing that says no.
|
|
if (into && depth + 1 > DEPTH_CAP) into = null;
|
|
remember("");
|
|
var block = blockOf(kind);
|
|
if (into) {
|
|
into.push(block);
|
|
} else {
|
|
doc.blocks.push(block);
|
|
}
|
|
selected = block.id;
|
|
setMode("nav");
|
|
render();
|
|
focusSelected();
|
|
announce("Added a " + kind + " block" + (into ? " inside the selected group." : " at the end."));
|
|
}
|
|
|
|
|
|
|
|
/// Insert directly after the selected block, in its own sibling list.
|
|
///
|
|
/// Adding a block at a position rather than at the end is the difference
|
|
/// between an inserter and a palette. The whole "make room by pushing rows
|
|
/// down" problem is gone with the grid: a flow document has no rows to run
|
|
/// out of, and inserting after a block is a splice into a list. That is the
|
|
/// single clearest gain of the model.
|
|
function insertUnder(kind) {
|
|
var entry = selected ? findEntry(selected) : null;
|
|
if (!entry) return;
|
|
if (full(kind)) {
|
|
// Said, not merely refused. The palette greys itself out at the cap and
|
|
// explains why; the inserter is a menu that opens on demand, so the only
|
|
// place its refusal can be put is the live region.
|
|
announce(capNote());
|
|
renderPalette();
|
|
return;
|
|
}
|
|
if (entry.parent && entry.parent.body.flow === "row" && entry.siblings.length >= ROW_CAP) {
|
|
announce("This row already holds " + ROW_CAP + " blocks, which is the most it takes.");
|
|
return;
|
|
}
|
|
remember("");
|
|
var block = blockOf(kind);
|
|
attach(entry.siblings, entry.index + 1, block);
|
|
if (!inRow(findEntry(block.id))) delete block.width;
|
|
selected = block.id;
|
|
setMode("nav");
|
|
render();
|
|
focusSelected();
|
|
announce("Inserted a " + kind + " block after " + entry.block.title + ".");
|
|
}
|
|
|
|
/// Insert a new block at a position rather than after the selection.
|
|
///
|
|
/// The palette's click appends and the inserter above inserts after the
|
|
/// selected block. A drag from the palette lands somewhere neither of those
|
|
/// can name, so this takes the same `(targetId, where)` a move does and
|
|
/// answers the same way: a sentence to say, or "" when it inserted. Every
|
|
/// cap is the one insertUnder() and moveBlock() already apply, checked here
|
|
/// rather than after the fact because a refused insert must leave the
|
|
/// document alone.
|
|
function insertAt(kind, targetId, where) {
|
|
var target = findEntry(targetId);
|
|
if (!target) return "That block is no longer on the canvas.";
|
|
if (full(kind)) return capNote();
|
|
var into = where === "inside" ? childrenOf(target.block) : null;
|
|
if (where === "inside" && !into) return target.block.title + " holds no blocks.";
|
|
var siblings = into || target.siblings;
|
|
var parent = into ? target.block : target.parent;
|
|
var index = into ? siblings.length : target.index + (where === "after" ? 1 : 0);
|
|
if (parent && parent.body.flow === "row" && siblings.length >= ROW_CAP) {
|
|
return "This row already holds " + ROW_CAP + " blocks, which is the most it takes.";
|
|
}
|
|
var block = blockOf(kind);
|
|
if ((into ? target.depth + 1 : target.depth) + subtreeHeight(block) > DEPTH_CAP) {
|
|
return "Blocks cannot nest deeper than " + DEPTH_CAP + " groups.";
|
|
}
|
|
remember("");
|
|
attach(siblings, index, block);
|
|
if (!inRow(findEntry(block.id))) delete block.width;
|
|
selected = block.id;
|
|
setMode("nav");
|
|
render();
|
|
focusSelected();
|
|
announce("Inserted a " + kind + " block.");
|
|
return "";
|
|
}
|
|
|
|
/// Wrap the selected block in a group, or lift a group's children out.
|
|
///
|
|
/// This is what makes an imperfect conversion survivable: an operator who
|
|
/// disagrees with how the upgrade grouped their dashboard fixes it in two
|
|
/// keystrokes rather than editing JSON.
|
|
/// How far the deepest block under this one sits below it.
|
|
///
|
|
/// Wrapping pushes a whole subtree down a level, not just the block being
|
|
/// wrapped, so this is what the depth cap has to be checked against.
|
|
/// Checking the block alone let a three-deep group be wrapped into a
|
|
/// four-deep one, which the canvas drew and the save refused.
|
|
function subtreeHeight(block) {
|
|
return walk([block], null, 0, []).reduce(function (deepest, each) {
|
|
return Math.max(deepest, each.depth);
|
|
}, 0);
|
|
}
|
|
|
|
/// True when the selected block can be wrapped in one more container.
|
|
function canGroup(entry) {
|
|
return !!entry && room(1, 0) &&
|
|
entry.depth + 1 + subtreeHeight(entry.block) <= DEPTH_CAP;
|
|
}
|
|
|
|
function groupSelected(flow) {
|
|
var entry = selected ? findEntry(selected) : null;
|
|
if (!entry) return;
|
|
if (!room(1, 0)) {
|
|
announce(capNote());
|
|
return;
|
|
}
|
|
if (!canGroup(entry)) {
|
|
announce("Blocks cannot nest deeper than " + DEPTH_CAP + " groups, and " +
|
|
entry.block.title + " already holds blocks that far down.");
|
|
return;
|
|
}
|
|
remember("");
|
|
var wrapper = blockOf(flow === "row" ? "row" : "section");
|
|
var taken = entry.siblings.splice(entry.index, 1)[0];
|
|
wrapper.body.children = [taken];
|
|
if (flow === "row") taken.width = 12;
|
|
else delete taken.width;
|
|
attach(entry.siblings, entry.index, wrapper);
|
|
selected = wrapper.id;
|
|
render();
|
|
focusSelected();
|
|
announce(taken.title + " is now inside a " + (flow === "row" ? "row" : "section") + ".");
|
|
}
|
|
|
|
function ungroupSelected() {
|
|
var entry = selected ? findEntry(selected) : null;
|
|
var kids = entry && childrenOf(entry.block);
|
|
if (!kids) return;
|
|
remember("");
|
|
entry.siblings.splice(entry.index, 1);
|
|
kids.slice().reverse().forEach(function (child) {
|
|
// A block leaving a row stops being a share of one.
|
|
if (!entry.parent || entry.parent.body.flow !== "row") delete child.width;
|
|
attach(entry.siblings, entry.index, child);
|
|
});
|
|
selected = kids.length ? kids[0].id : null;
|
|
render();
|
|
focusSelected();
|
|
announce("Lifted " + kids.length + " blocks out of " + entry.block.title + ".");
|
|
}
|
|
|
|
function duplicate() {
|
|
var block = selectedBlock();
|
|
if (!block) return;
|
|
// A group is duplicated with everything in it, so the room needed is the
|
|
// size of the subtree, not one block. Counting one is what let a group of
|
|
// six be copied into a document with two blocks of room left, and the
|
|
// save was the thing that said no.
|
|
var subtree = walk([block], null, 0, []);
|
|
var leaves = subtree.filter(function (e) { return !childrenOf(e.block); }).length;
|
|
if (!room(subtree.length, leaves)) {
|
|
announce(capNote());
|
|
renderPalette();
|
|
return;
|
|
}
|
|
remember("");
|
|
// Through JSON, so a kind this build does not draw is copied whole: its
|
|
// attributes belong to the build that wrote them and this one may not
|
|
// drop the ones it has no field for.
|
|
var copy = JSON.parse(JSON.stringify(block));
|
|
var entry = findEntry(block.id);
|
|
attach(entry.siblings, entry.index + 1, copy);
|
|
// Every id in a copied subtree has to be new, not just the top one: ids
|
|
// are unique across the whole tree and a duplicated group would otherwise
|
|
// carry a second copy of each of its children's ids.
|
|
//
|
|
// The taken list is built once and added to as the walk goes, which is how
|
|
// buildPattern() does it. Asking nextId() to rescan the document per node
|
|
// instead was a full walk of the document for every block in the copy, and
|
|
// it had to be, because the copy's own new ids were not written down
|
|
// anywhere else.
|
|
var taken = model.ids(doc.blocks);
|
|
(function rename(target) {
|
|
target.id = model.nextId(kindOf(target) || "block", taken);
|
|
taken.push(target.id);
|
|
var kids = childrenOf(target);
|
|
if (kids) kids.forEach(rename);
|
|
})(copy);
|
|
selected = copy.id;
|
|
// Beside the other three structural operations, rather than the one that
|
|
// left a dashed outline saying the canvas had stopped answering arrows.
|
|
setMode("nav");
|
|
render();
|
|
focusSelected();
|
|
announce("Duplicated as " + copy.id + ".");
|
|
}
|
|
|
|
// The movers, and the block-moving half of the keymap. One undo step per
|
|
// burst rather than per press, and none at all for a press that cannot move.
|
|
function shift(key) {
|
|
var entry = selected ? findEntry(selected) : null;
|
|
if (!entry) return;
|
|
var delta = (key === "ArrowUp" || key === "ArrowLeft") ? -1 : 1;
|
|
var to = entry.index + delta;
|
|
// A move past either end of a sibling list lifts the block out to its
|
|
// parent's list instead of refusing, which is how a block gets out of a
|
|
// group it was put in by mistake. At the root there is nowhere further to
|
|
// go and the move is refused.
|
|
if (to < 0 || to >= entry.siblings.length) {
|
|
if (!entry.parent) {
|
|
announce(entry.block.title + " is already at that end of the document.");
|
|
return;
|
|
}
|
|
remember("move:" + entry.block.id);
|
|
var outer = findEntry(entry.parent.id);
|
|
entry.siblings.splice(entry.index, 1);
|
|
attach(outer.siblings, outer.index + (delta > 0 ? 1 : 0), entry.block);
|
|
// A block that leaves a row stops being a share of one.
|
|
if (!inRow(findEntry(entry.block.id))) delete entry.block.width;
|
|
render();
|
|
announce(entry.block.title + " moved out of " + entry.parent.title + ".");
|
|
return;
|
|
}
|
|
remember("move:" + entry.block.id);
|
|
var moved = entry.siblings.splice(entry.index, 1)[0];
|
|
entry.siblings.splice(to, 0, moved);
|
|
render();
|
|
announce(entry.block.title + " moved to position " + (to + 1) + ".");
|
|
}
|
|
|
|
/// Move a block to a place named by another block.
|
|
///
|
|
/// `where` is "before", "after" or "inside". This is what a drop in the
|
|
/// list view commits, and it is deliberately the only thing that script can
|
|
/// do to the document: one write path, one undo step per gesture, and every
|
|
/// rule the movers already enforce enforced once.
|
|
///
|
|
/// Returns a sentence when it refuses, and nothing when it moves.
|
|
function moveBlock(id, targetId, where) {
|
|
var entry = findEntry(id);
|
|
var target = findEntry(targetId);
|
|
if (!entry || !target || id === targetId) return "That block cannot go there.";
|
|
// Into its own subtree is the move that loses the document: the block and
|
|
// everything under it would be spliced out and then attached to a list
|
|
// that no longer exists anywhere.
|
|
if (walk([entry.block], null, 0, []).some(function (each) {
|
|
return each.block.id === targetId;
|
|
})) {
|
|
return entry.block.title + " cannot go inside itself.";
|
|
}
|
|
var into = where === "inside" ? childrenOf(target.block) : null;
|
|
if (where === "inside" && !into) return target.block.title + " holds no blocks.";
|
|
var siblings = into || target.siblings;
|
|
var parent = into ? target.block : target.parent;
|
|
var depth = into ? target.depth + 1 : target.depth;
|
|
if (depth + subtreeHeight(entry.block) > DEPTH_CAP) {
|
|
return "Blocks cannot nest deeper than " + DEPTH_CAP + " groups.";
|
|
}
|
|
var isRow = !!(parent && parent.body.flow === "row");
|
|
// The row cap counts the block arriving, unless it is already in this row
|
|
// and only changing places.
|
|
if (isRow && siblings.indexOf(entry.block) < 0 && siblings.length >= ROW_CAP) {
|
|
return "This row already holds " + ROW_CAP + " blocks, which is the most it takes.";
|
|
}
|
|
remember("");
|
|
var moved = entry.siblings.splice(entry.index, 1)[0];
|
|
// Recomputed after the splice: removing the block can shift the target's
|
|
// index when the two share a list, and inserting at the stale one puts the
|
|
// block on the wrong side of it.
|
|
var at = into ? siblings.length : siblings.indexOf(target.block) + (where === "after" ? 1 : 0);
|
|
attach(siblings, at, moved);
|
|
// A block arriving in a row becomes a share of one; a block leaving a row
|
|
// stops being one. The same rule the movers apply.
|
|
if (isRow) {
|
|
if (!moved.width) moved.width = 12;
|
|
} else {
|
|
delete moved.width;
|
|
}
|
|
selected = moved.id;
|
|
setMode("nav");
|
|
render();
|
|
focusSelected();
|
|
announce(moved.title + " moved " + where + " " + target.block.title + ".");
|
|
return "";
|
|
}
|
|
|
|
/// Move the selected block into the group above it, or out of the group it
|
|
/// is in. The outliner convention, Alt with a left or right arrow.
|
|
///
|
|
/// This is the keyboard's answer to dropping a block *inside* a container,
|
|
/// which a pointer could do in the list view and no key could do anywhere.
|
|
/// The movers lift a block out of a group and reorder it among its
|
|
/// siblings; neither of them could ever put one in. A gesture only a
|
|
/// pointer can perform is one a keyboard operator cannot undo the effect of
|
|
/// either, because they cannot reach the state to begin with.
|
|
function indent(into) {
|
|
var entry = selected ? findEntry(selected) : null;
|
|
if (!entry) return;
|
|
if (into) {
|
|
var before = entry.siblings[entry.index - 1];
|
|
if (!before || !childrenOf(before)) {
|
|
announce("There is no group above " + entry.block.title +
|
|
" to move it into.");
|
|
return;
|
|
}
|
|
var refused = moveBlock(entry.block.id, before.id, "inside");
|
|
if (refused) announce(refused);
|
|
return;
|
|
}
|
|
if (!entry.parent) {
|
|
announce(entry.block.title + " is not inside a group.");
|
|
return;
|
|
}
|
|
var lifted = moveBlock(entry.block.id, entry.parent.id, "after");
|
|
if (lifted) announce(lifted);
|
|
}
|
|
|
|
function removeSelected() {
|
|
var entry = selected ? findEntry(selected) : null;
|
|
if (!entry) return;
|
|
remember("");
|
|
var gone = entry.block;
|
|
// Removed from its own sibling list, wherever that is. A group goes with
|
|
// everything inside it, which is what Undo is for and what the
|
|
// announcement says.
|
|
entry.siblings.splice(entry.index, 1);
|
|
var neighbour = entry.siblings[Math.min(entry.index, entry.siblings.length - 1)];
|
|
selected = neighbour ? neighbour.id : (entry.parent ? entry.parent.id : null);
|
|
setMode("nav");
|
|
render();
|
|
announce("Removed the " + kindOf(gone) + " block " +
|
|
(gone.title || gone.id) + ". Undo brings it back.");
|
|
var focusTarget = selected ? nodeFor(selected) : root.querySelector("[data-add-widget]");
|
|
if (focusTarget) focusTarget.focus({ preventScroll: true });
|
|
}
|
|
|
|
addButtons.forEach(function (button) {
|
|
button.addEventListener("click", function () {
|
|
if (canvasReady()) add(button.dataset.addWidget);
|
|
});
|
|
});
|
|
|
|
// --- the canvas ----------------------------------------------------------
|
|
|
|
// One click handler for every block. Only a block: the toolbar and the
|
|
// inserter are inside the canvas too and carry no widget id.
|
|
canvas.addEventListener("click", function (event) {
|
|
var node = event.target.closest("[data-widget-id]");
|
|
if (!canvasReady()) return;
|
|
if (node) {
|
|
select(node.dataset.widgetId);
|
|
return;
|
|
}
|
|
// The canvas itself, or the sentence drawn on it while it is empty: the
|
|
// background is the pointer's way out of a selection, and Escape was the
|
|
// only path to it.
|
|
if (event.target === canvas || event.target === empty) clearSelection();
|
|
});
|
|
|
|
// The mode follows the focus rather than the last operation that set it: a
|
|
// block node or a toolbar button holding it is exactly "the keyboard is on
|
|
// the canvas", which is what the dashed outline denies.
|
|
canvas.addEventListener("focusin", function () { setMode("nav"); });
|
|
|
|
// And one keydown handler, behind the gate every other canvas control
|
|
// passes: while the pane holds a document the canvas is not drawing,
|
|
// nothing here may touch it.
|
|
// The keys the canvas answers. Anything else falls through untouched, Tab
|
|
// most of all: a canvas that selected a block on the way past would
|
|
// re-render and take the focus back, which is a trap rather than a keymap.
|
|
var CANVAS_KEYS = ["ArrowUp", "ArrowDown", "ArrowLeft", "ArrowRight",
|
|
"Enter", "Escape", "Delete", "Backspace", "F10"];
|
|
|
|
canvas.addEventListener("keydown", function (event) {
|
|
var node = event.target.closest("[data-widget-id]");
|
|
var key = event.key;
|
|
if (!node || CANVAS_KEYS.indexOf(key) < 0) return;
|
|
if (key === "F10" && !event.altKey) return;
|
|
if (!canvasReady()) return;
|
|
if (node.dataset.widgetId !== selected) select(node.dataset.widgetId);
|
|
if (key === "F10") {
|
|
rove(0);
|
|
} else if (event.altKey && (key === "ArrowLeft" || key === "ArrowRight")) {
|
|
indent(key === "ArrowRight");
|
|
} else if (event.altKey && (key === "ArrowUp" || key === "ArrowDown")) {
|
|
shift(key);
|
|
} else if (key === "ArrowUp" || key === "ArrowLeft") {
|
|
step(-1);
|
|
} else if (key === "ArrowDown" || key === "ArrowRight") {
|
|
step(1);
|
|
} else if (key === "Enter") {
|
|
enterEdit();
|
|
} else if (key === "Escape") {
|
|
clearSelection();
|
|
} else if (key === "Delete" || key === "Backspace") {
|
|
removeSelected();
|
|
} else {
|
|
return;
|
|
}
|
|
event.preventDefault();
|
|
});
|
|
|
|
// Enter goes into the block. A canvas node holds no text to put a caret in,
|
|
// so a block's content here is its settings: Enter moves to the first field
|
|
// the panel offers for this kind and Escape comes back, with the outline
|
|
// going dashed meanwhile so the block the panel edits stays obvious.
|
|
function enterEdit() {
|
|
if (!selectedBlock()) return;
|
|
var field = Array.from(properties.querySelectorAll("[data-widget-field]"))
|
|
.find(function (input) { return input.offsetParent !== null; });
|
|
if (!field) return;
|
|
setMode("edit");
|
|
field.focus();
|
|
}
|
|
|
|
properties.addEventListener("focusin", function (event) {
|
|
if (event.target.hasAttribute("data-widget-field")) setMode("edit");
|
|
});
|
|
|
|
properties.addEventListener("keydown", function (event) {
|
|
if (event.key !== "Escape" || mode !== "edit") return;
|
|
setMode("nav");
|
|
focusSelected();
|
|
event.preventDefault();
|
|
});
|
|
|
|
// --- the toolbar and the inserter ----------------------------------------
|
|
|
|
toolbar.addEventListener("click", function (event) {
|
|
var button = event.target.closest("[data-block-action]");
|
|
if (!button || !canvasReady()) return;
|
|
var action = button.dataset.blockAction;
|
|
if (action === "insert") {
|
|
setInserter(inserter.hidden);
|
|
return;
|
|
}
|
|
closeInserter();
|
|
if (action === "up" || action === "down") {
|
|
shift(action === "up" ? "ArrowUp" : "ArrowDown");
|
|
} else if (action === "duplicate") {
|
|
duplicate();
|
|
} else if (action === "group-row") {
|
|
groupSelected("row");
|
|
} else if (action === "group-section") {
|
|
groupSelected("section");
|
|
} else if (action === "ungroup") {
|
|
ungroupSelected();
|
|
} else {
|
|
removeSelected();
|
|
}
|
|
});
|
|
|
|
toolbar.addEventListener("keydown", function (event) {
|
|
var stops = toolbarButtons.filter(function (button) { return !button.disabled; });
|
|
var at = stops.indexOf(document.activeElement);
|
|
if (event.key === "ArrowRight" || event.key === "ArrowDown") {
|
|
rove(at + 1);
|
|
} else if (event.key === "ArrowLeft" || event.key === "ArrowUp") {
|
|
rove(at - 1);
|
|
} else if (event.key === "Escape") {
|
|
closeInserter();
|
|
focusSelected();
|
|
} else {
|
|
return;
|
|
}
|
|
event.preventDefault();
|
|
});
|
|
|
|
inserter.addEventListener("click", function (event) {
|
|
var button = event.target.closest("[data-insert-block]");
|
|
if (!button || !canvasReady()) return;
|
|
closeInserter();
|
|
insertUnder(button.dataset.insertBlock);
|
|
});
|
|
|
|
inserter.addEventListener("keydown", function (event) {
|
|
if (event.key !== "Escape") return;
|
|
closeInserter();
|
|
insertButton.focus();
|
|
event.preventDefault();
|
|
});
|
|
|
|
// The toolbar is measured against its block, so a viewport that changes
|
|
// shape moves it; without this a rotation leaves it over another block.
|
|
window.addEventListener("resize", placeToolbar);
|
|
|
|
// --- history and the shortcut list ---------------------------------------
|
|
|
|
undoButton.addEventListener("click", function () { restore(past, future, "Undone."); });
|
|
redoButton.addEventListener("click", function () { restore(future, past, "Redone."); });
|
|
|
|
// A control being typed in owns its own undo, and the browser's is better
|
|
// at text than anything here could be. So Ctrl+Z is taken over outside a
|
|
// field and nowhere else.
|
|
function inAField(target) {
|
|
var tag = target ? target.tagName : "";
|
|
return tag === "INPUT" || tag === "TEXTAREA" || tag === "SELECT";
|
|
}
|
|
|
|
// On the document rather than on the editor: this script only runs on a page
|
|
// that has one, and a keymap bound inside it answers nothing once the focus
|
|
// is on <body>, which is where a removed node used to leave it.
|
|
document.addEventListener("keydown", function (event) {
|
|
if (event.altKey && event.shiftKey && (event.key === "H" || event.key === "h")) {
|
|
shortcuts.open = true;
|
|
shortcuts.querySelector("summary").focus();
|
|
event.preventDefault();
|
|
return;
|
|
}
|
|
if (!(event.ctrlKey || event.metaKey) || event.altKey) return;
|
|
// Group and ungroup, the two structural operations an operator reaches
|
|
// for most after a conversion they disagree with. Outside a field for the
|
|
// same reason Ctrl+Z is, and a section rather than a row because a
|
|
// section is the one that never changes how the blocks in it are sized.
|
|
if (event.key === "g" || event.key === "G") {
|
|
if (inAField(event.target) || !canvasReady()) return;
|
|
if (event.shiftKey) ungroupSelected();
|
|
else groupSelected("section");
|
|
event.preventDefault();
|
|
return;
|
|
}
|
|
if (event.key !== "z" && event.key !== "Z") return;
|
|
if (inAField(event.target)) return;
|
|
if (event.shiftKey) {
|
|
restore(future, past, "Redone.");
|
|
} else {
|
|
restore(past, future, "Undone.");
|
|
}
|
|
event.preventDefault();
|
|
});
|
|
|
|
raw.addEventListener("input", function () {
|
|
var parsed = pane.read(raw.value);
|
|
if (!parsed) {
|
|
pane.explain("");
|
|
return;
|
|
}
|
|
pane.refuse("");
|
|
// A document change like any other, coalesced so typing into the pane is
|
|
// one step rather than one per keystroke.
|
|
remember("raw");
|
|
doc = parsed;
|
|
selected = null;
|
|
setMode("nav");
|
|
render();
|
|
});
|
|
|
|
// One number, in this file, written into the sentence that states it.
|
|
var depthNote = root.querySelector("[data-undo-depth]");
|
|
if (depthNote) depthNote.textContent = String(UNDO_DEPTH);
|
|
// Two claims only this script makes true: every chord in that list is
|
|
// answered here, and the depth in it is written above, so with scripting off
|
|
// the page drew seven instructions that did nothing and a sentence with a
|
|
// hole in it. The template renders the list hidden and the canvas pointing
|
|
// at the raw pane, which is what a browser with no scripting has.
|
|
shortcuts.hidden = false;
|
|
empty.textContent = "Add a block from the palette.";
|
|
|
|
render();
|
|
if (!pane.draws()) {
|
|
// Nothing on the canvas can edit this document, so the operator is put
|
|
// where the only thing that can: the pane is open, the message is beside
|
|
// the text, and the text has the caret.
|
|
pane.explain("This is not a dashboard document this editor can draw, so " +
|
|
"the canvas and Save are both refusing it. Correct the JSON here.");
|
|
raw.focus();
|
|
}
|
|
|
|
// --- what another script on this page may do -----------------------------
|
|
//
|
|
// The list view lives in dashboard-list.js, because this file is near its
|
|
// per-file budget and the tree is a self-contained panel. It needs to read
|
|
// the document, follow the selection, and move a block; it does none of
|
|
// those itself. Every one of them goes through the same function the canvas
|
|
// uses, so there is one write path into the document, one undo step per
|
|
// gesture, and no second idea of what is selected.
|
|
window.FlareDashboardEditor = {
|
|
/// The live document. Read only by anything outside this file: every
|
|
/// change goes through the functions below, which remember and render.
|
|
document: function () { return doc; },
|
|
selected: function () { return selected; },
|
|
select: select,
|
|
/// The selected block with its place in the tree, or null.
|
|
entry: selectedEntry,
|
|
/// Change the selected block: `edit(block)` runs after the document is
|
|
/// remembered under `key` (one undo step per field per burst) and before
|
|
/// it is drawn again. Refused while the canvas is not drawing the pane.
|
|
change: function (key, edit) {
|
|
var block = canvasReady() && selectedBlock();
|
|
if (!block) return;
|
|
remember(key + ":" + block.id);
|
|
edit(block);
|
|
render();
|
|
},
|
|
enterEdit: enterEdit,
|
|
moveBlock: moveBlock,
|
|
insertAt: insertAt,
|
|
indent: indent,
|
|
/// True while the canvas is drawing what the pane holds. Everything
|
|
/// refuses in the other state, and so must anything built on this.
|
|
ready: canvasReady,
|
|
/// Say what just happened, into the one live region this page has. A
|
|
/// second script writing its own would be a second thing to keep in step
|
|
/// with what the first one says.
|
|
announce: announce,
|
|
/// Call `listener` after every render, and once now.
|
|
onDraw: function (listener) {
|
|
drawn.push(listener);
|
|
listener();
|
|
}
|
|
};
|
|
}());
|