Replaces the always-text-input cell editor with a per-property
widget factory keyed off the row's JSON Schema (form.yaml). The
table view now picks the right editor for each cell automatically:
strings get text inputs, enums get dropdowns, integers get number
inputs with min/max, dates get date pickers, booleans get
checkboxes, multi-select arrays get a multi-select. Cells whose
schema is a complex type (nested object, generic array, oneOf /
anyOf / allOf) can't be inline-edited and punt to the row's
form-mode editor on Enter / double-click.
Schema discovery:
context.js walkServer fetches <currentdir>/form.yaml as a
companion to <currentdir>/table.yaml — same file the form-mode
renderer already loads, just from the table view's perspective.
Best-effort: a directory with table.yaml but no form.yaml still
renders as a sortable/filterable table; cells just fall back to
plain text inputs without per-property hints. The schema is
exposed as ctx.rowSchema and consumed by the editor's
propertySchemaFor() helper, which walks dot-separated field
names through schema.properties to locate each column's
property schema.
Editor factory (editor.js):
- propertySchemaFor(col) — schema lookup keyed by col.field.
- isComplexSchema(s) — true for nested object, generic array,
oneOf/anyOf/allOf. Multi-select-friendly arrays
(string-enum + uniqueItems) are NOT complex; they get an
inline multi-select widget.
- makeWidget(propSchema, col, initialValue) — dispatches to one
of the widget builders below based on schema type / format /
enum + column-spec hints (col.format / col.enum) for tables
without a form.yaml.
Widget builders, each returning {element, getValue, focus}:
- widgetText — plain <input type=text>, default fallback.
- widgetTextarea — for string with maxLength > 200 (long
narrative fields).
- widgetTyped(type) — typed inputs the browser can help validate;
used for date / date-time / email.
- widgetNumber — <input type=number> with min/max/step
derived from schema.minimum/maximum/
multipleOf. Integer schemas force step=1.
getValue returns Number, not string, so
the draft buffer holds the right type for
JSON serialization later.
- widgetCheckbox — <input type=checkbox>; getValue returns
bool. initial value coerces from "true"/
true string-or-bool.
- widgetSelect — <select> with empty placeholder + one
option per enum choice; getValue returns
the chosen string or null.
- widgetMultiSelect — <select multiple> with size = min(6, N);
getValue returns the array of selected
values (preserves order in the option list).
Complex-type cells:
isComplexSchema(propSchema) → enterEdit calls navigateToRowForm,
which routes to row.url (already the <id>.yaml.html re-edit URL
the row tracker holds). Phase 5 may swap this for an inline
side-panel mount of form-mode in the same bundle, but the
current navigate-out path delivers the same eventual UX without
needing the side-panel scaffolding.
Type-aware draft equality:
The pre-Phase-2 commit treated every value as a string and
compared via String() equality, which would mark any number-
column edit dirty even when the user re-typed the same number.
The new sameValue() helper handles bool/object via JSON-string
equality and falls back to loose string compare so 42 == "42"
isn't a false dirty. Drafts hold typed values (number, bool,
array) instead of all strings, so when Phase 3 wires the row PUT
the body shape matches the JSON Schema the server validates
against without an additional coercion pass.
Tests (tests/tables.spec.js — 7 new specs, total 22 in the
table view, all 27 in the file):
- enum column edits via select dropdown — verifies the empty
placeholder + 3 enum options render and the chosen value
displays back in the cell.
- integer column gives a number input with min/max — verifies
the type/min/max/step attributes derive from the schema, AND
the draft buffer holds typeof === 'number'.
- boolean column gives a checkbox — verifies type=checkbox and
the draft holds true after Space-toggle. (Toggle via Space,
not Playwright's .check() helper, to dodge the click+blur
race a focused-checkbox-inside-grid-cell hits.)
- format:date column gives a date input — verifies type=date
and the existing value pre-populates as YYYY-MM-DD.
- multi-select enum-array column gives a multi-select.
- complex (object) column navigates to the row form on edit —
verifies no inline editor mounts AND the navigate seam
receives the row's URL.
- no rowSchema → falls back to plain text editor — verifies the
best-effort behavior for directories with only table.yaml.
Bundle size: 124 KB → 127 KB (+3 KB for the factory + widget
builders).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
200 lines
7.3 KiB
JavaScript
200 lines
7.3 KiB
JavaScript
(function (app) {
|
|
'use strict';
|
|
|
|
// load() resolves to the table context the rest of the app renders:
|
|
// { title?, description?, columns, rows, defaults? }
|
|
//
|
|
// Two paths:
|
|
//
|
|
// 1. Inline JSON (test seam, and also any host that wants to
|
|
// pre-render a context server-side): if #table-context parses
|
|
// to a non-empty object, return it as-is.
|
|
//
|
|
// 2. File-backed walk (the real-world path served by zddc-server):
|
|
// page is at /<dir>/table.html — fetch <dir>/table.yaml,
|
|
// list every other *.yaml in <dir> as a row file (filtering
|
|
// out table.yaml and form.yaml so they don't appear as rows),
|
|
// parse each, and assemble the same shape. The whole table
|
|
// lives in one directory.
|
|
//
|
|
// file:// mode without a directory handle is unsupported in v1 — the
|
|
// walk only runs against http(s). file:// users must either inject an
|
|
// inline context (tests) or open the page through zddc-server.
|
|
async function load() {
|
|
const inline = readInlineContext();
|
|
if (inline && Object.keys(inline).length > 0) {
|
|
return inline;
|
|
}
|
|
if (typeof location !== 'undefined' &&
|
|
(location.protocol === 'http:' || location.protocol === 'https:')) {
|
|
try {
|
|
const walked = await walkServer();
|
|
if (walked) {
|
|
return walked;
|
|
}
|
|
} catch (err) {
|
|
console.error('[tables] failed to load table from server', err);
|
|
showStatus('Could not load table: ' + (err && err.message ? err.message : err));
|
|
}
|
|
}
|
|
return {};
|
|
}
|
|
|
|
function readInlineContext() {
|
|
const el = document.getElementById('table-context');
|
|
if (!el) {
|
|
return null;
|
|
}
|
|
try {
|
|
return JSON.parse(el.textContent || '{}');
|
|
} catch (err) {
|
|
console.error('[tables] failed to parse #table-context', err);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
function showStatus(msg) {
|
|
const el = document.getElementById('table-status');
|
|
if (!el) return;
|
|
el.textContent = msg;
|
|
el.hidden = false;
|
|
}
|
|
|
|
async function walkServer() {
|
|
const source = window.zddc && window.zddc.source;
|
|
if (!source) {
|
|
throw new Error('zddc.source not available');
|
|
}
|
|
const tableName = tableNameFromUrl(location.pathname);
|
|
if (!tableName) {
|
|
throw new Error('Unrecognized table URL: ' + location.pathname);
|
|
}
|
|
const probe = await source.detectServerRoot();
|
|
if (!probe.handle) {
|
|
throw new Error(probe.status === 403
|
|
? 'No permission to list this directory'
|
|
: 'Server unreachable');
|
|
}
|
|
const dir = probe.handle;
|
|
|
|
// Spec lives at <currentdir>/table.yaml — the page URL is
|
|
// <currentdir>/table.html, so the spec is right next door.
|
|
const spec = await readYaml(dir, 'table.yaml');
|
|
if (!spec || !Array.isArray(spec.columns)) {
|
|
throw new Error('Spec table.yaml missing columns[]');
|
|
}
|
|
|
|
// Optional row schema from <dir>/form.yaml — same JSON Schema
|
|
// the form-mode renderer uses. Phase 2 derives per-cell editor
|
|
// widgets from it (text/number/date/select/checkbox).
|
|
// Best-effort: a directory with only table.yaml still renders
|
|
// as a sortable/filterable table; cells fall back to plain
|
|
// text inputs without per-property hints.
|
|
let rowSchema = null;
|
|
try {
|
|
const formSpec = await readYaml(dir, 'form.yaml');
|
|
if (formSpec && formSpec.schema) {
|
|
rowSchema = formSpec.schema;
|
|
}
|
|
} catch (_) {
|
|
// form.yaml missing or unreadable; carry on without it.
|
|
}
|
|
|
|
// Rows are every *.yaml in <currentdir> EXCEPT the spec
|
|
// (table.yaml) and the row-edit form (form.yaml). They live
|
|
// in the same directory by design — copying the directory
|
|
// copies the whole table.
|
|
const rows = await readRows(dir, '', tableName);
|
|
|
|
return {
|
|
title: spec.title,
|
|
description: spec.description,
|
|
columns: spec.columns,
|
|
defaults: spec.defaults,
|
|
rowSchema: rowSchema,
|
|
rows: rows
|
|
};
|
|
}
|
|
|
|
function tableNameFromUrl(pathname) {
|
|
// /<dir>/.../<rowsdir>/table.html → name is the rows-dir's
|
|
// basename.
|
|
const m = String(pathname || '').match(/\/([^\/]+)\/table\.html$/);
|
|
return m ? m[1] : null;
|
|
}
|
|
|
|
function stripDotSlash(p) {
|
|
let out = String(p || '');
|
|
if (out.startsWith('./')) out = out.slice(2);
|
|
if (out.startsWith('/')) out = out.slice(1);
|
|
if (out.endsWith('/')) out = out.slice(0, -1);
|
|
return out;
|
|
}
|
|
|
|
async function readYaml(dir, relPath) {
|
|
const fileHandle = await resolveFile(dir, relPath);
|
|
const file = await fileHandle.getFile();
|
|
const text = await file.text();
|
|
if (!window.jsyaml) {
|
|
throw new Error('js-yaml not loaded');
|
|
}
|
|
return window.jsyaml.load(text);
|
|
}
|
|
|
|
// Walk a "/"-separated relative path under dir, returning the
|
|
// FileSystemFileHandle (or HttpFileHandle) at the leaf.
|
|
async function resolveFile(dir, relPath) {
|
|
const parts = relPath.split('/').filter(Boolean);
|
|
if (parts.length === 0) {
|
|
throw new Error('Empty file path');
|
|
}
|
|
const fileName = parts.pop();
|
|
let cur = dir;
|
|
for (let i = 0; i < parts.length; i++) {
|
|
cur = await cur.getDirectoryHandle(parts[i]);
|
|
}
|
|
return cur.getFileHandle(fileName);
|
|
}
|
|
|
|
async function resolveDirectory(dir, relPath) {
|
|
const parts = relPath.split('/').filter(Boolean);
|
|
let cur = dir;
|
|
for (let i = 0; i < parts.length; i++) {
|
|
cur = await cur.getDirectoryHandle(parts[i]);
|
|
}
|
|
return cur;
|
|
}
|
|
|
|
async function readRows(rowsDir, _rowsRel, _tableName) {
|
|
const rows = [];
|
|
for await (const entry of rowsDir.values()) {
|
|
if (entry.kind !== 'file') continue;
|
|
if (!entry.name.endsWith('.yaml')) continue;
|
|
// Skip the spec and the row-edit form — they live alongside
|
|
// the rows but aren't rows themselves.
|
|
if (entry.name === 'table.yaml' || entry.name === 'form.yaml') continue;
|
|
try {
|
|
const file = await (await rowsDir.getFileHandle(entry.name)).getFile();
|
|
const data = window.jsyaml.load(await file.text());
|
|
rows.push({
|
|
url: rowEditUrl(entry.name),
|
|
data: data || {},
|
|
editable: true
|
|
});
|
|
} catch (err) {
|
|
console.warn('[tables] skipping unparseable row', entry.name, err);
|
|
}
|
|
}
|
|
return rows;
|
|
}
|
|
|
|
// Re-edit URL for one row. Page is at /<dir>/table.html; row file
|
|
// lives at /<dir>/<basename>.yaml; form re-edit URL is
|
|
// /<dir>/<basename>.yaml.html — same directory.
|
|
function rowEditUrl(rowFileName) {
|
|
const pageDir = location.pathname.replace(/\/table\.html$/, '/');
|
|
return pageDir + rowFileName + '.html';
|
|
}
|
|
|
|
app.modules.context = { load: load };
|
|
})(window.tablesApp);
|