Two intertwined refactors that share too many files to split cleanly.
Both are described separately below.
PART 1 — in-dir convention for table+form spec files
Old layout had the spec at the parent and rows in a child:
archive/<party>/
mdl.table.yaml spec
mdl.form.yaml row-edit form
mdl/ rows-dir
row-001.yaml ...
URLs were /<dir>/mdl.table.html and /<dir>/mdl.form.html. Copying
mdl/ elsewhere lost the spec and form because they lived next door.
New layout collapses everything into the rows-dir:
archive/<party>/mdl/ self-contained
table.yaml spec
form.yaml row-edit form
row-001.yaml ... rows
URLs become /<dir>/mdl/table.html and /<dir>/mdl/form.html. The
"copying-the-folder-takes-everything" property the user asked for
falls out by construction; the row-edit URL /<dir>/<id>.yaml.html
keeps the same shape (spec is now in the same dir, not the
grandparent).
Server changes:
- internal/handler/tablehandler.go RecognizeTableRequest fires on
/<dir>/table.html when <dir>/table.yaml exists. The .zddc.tables
alias map is gone — pure presence-based discovery now matches
the form system's existing convention. Default-MDL fallback at
archive/<party>/mdl/ stays for the virgin-archive case (the
rows-dir need not exist on disk; the URL renders fully virtually).
- internal/handler/formhandler.go RecognizeFormRequest fires on
/<dir>/form.html and /<dir>/<id>.yaml.html with spec at
<dir>/form.yaml. specEligible accepts on-disk files OR the
default-MDL virtual path so an empty mdl/ dir still surfaces the
add-row form.
- internal/handler/tablehandler.go IsDefaultMdlSpec moves to
serving archive/<party>/mdl/{table,form}.yaml (5 segments after
ZDDC_ROOT). New isAtArchivePartyMdlLevel predicate; new
isAtArchivePartyMdlDir for directory-based recognition. New
IsDefaultMdlSpecAbs accessor for callers that hold an abs path
rather than a URL (formhandler).
- internal/handler/formhandler.go loadFormSpec(fsRoot, path) falls
back to embedded default-MDL bytes when os.ReadFile returns
NotExist AND the path matches the archive-party-mdl shape. Three
call sites updated to pass cfg.Root.
- internal/handler/formhandler.go serveFormCreate writes
submissions to filepath.Dir(req.SpecPath) — the spec, the form,
and rows all live in one directory. The submissionsDir creation
is idempotent (MkdirAll); cascade falls back one level for ACL
evaluation when the dir hasn't been materialized yet.
- internal/handler/tablehandler.go tableRowsRedirect now points at
/<dir>/table.html (was /<dir>.table.html) when the directory
request maps to a recognized table.
- cmd/zddc-server/main.go dispatch synth flips from
urlPath + ".table.html" to urlPath + "/table.html" for the
no-trailing-slash → tables-app routing.
- internal/apps/availability.go DefaultAppAt comment clarified
that the dir at archive/<party>/mdl/ IS the table (not a child).
Client changes:
- tables/js/context.js walkServer fetches <currentdir>/table.yaml
directly — no .zddc walk for table declarations. Rows are every
*.yaml in current dir EXCLUDING table.yaml and form.yaml. The
.zddc fetch-for-aliases is gated on file:// (online mode 404s
on .zddc reads via the dispatcher's reserve guard, so skipping
the request avoids browser console noise).
- tables/js/main.js add-row button links to relative form.html
(same dir).
- tables/js/render.js + filters.js: every column's autofilter is
uniformly a text-contains input, even enum columns — keeps the
filter row visually consistent and doesn't constrain users to
the enum vocabulary.
PART 2 — unified table+form HTML bundle
The form-render and table-render code paths share field schemas,
the cell editor for excel-mode IS a form widget, and the form
system's POST-back / validation already exists. Combining the two
HTMLs eliminates duplicating jsyaml/jsonschema/theme/source-
detection/.zddc-parsing across two single-file tools.
- tables/template.html grows two top-level mode containers:
#table-mode (toolbar + sortable table) and #form-mode (form +
submit button). Both hidden at parse time; the dispatcher
unhides one. The shared #form-context placeholder was added
here so the server's existing injectFormContext target
resolves.
- tables/js/mode.js (new) sets window.zddcMode synchronously
based on URL pattern: /form.html or /<id>.yaml.html → form,
/table.html → table, else inline-context fallback for
file:// (whichever context blob is non-empty wins). Unhides
the matching container at DOMContentLoaded.
- tables/js/main.js init() and form/js/main.js boot() each guard
early when mode isn't theirs. Both apps live on different
globals (window.tablesApp vs window.formApp) so module
registration doesn't collide.
- form/js/main.js title write falls back from #form-title to
#table-title (the unified bundle's shared header element)
when the dedicated id isn't present.
- tables/build.sh concatenates form modules (widgets, render,
object, array, errors, post, serialize, util) and form CSS.
No new external deps. Bundle grows from ~95KB to ~120KB.
- internal/handler/formhandler.go drops the //go:embed form.html
directive; serveFormRender now writes embeddedTablesHTML via
a small formRenderHTML() accessor (var declared in
tablehandler.go, same package). The embedded form.html file
is removed.
- build script: cp form/dist/form.html → internal/handler/form.html
step is gone (file no longer exists in the source tree). cp
tables/dist/tables.html → internal/handler/tables.html now
runs unconditionally rather than only on beta/stable cuts —
the renderer is a fixed binary component and dev iteration
needs the embedded copy refreshed every build. Channel-cascaded
apps (internal/apps/embedded/) stay channel-gated as before.
- form/dist/form.html still builds for standalone offline-only
use (downloadable from /releases/), but no longer goes into
the binary.
Tests:
- internal/handler/tablehandler_test.go and formhandler_test.go
rewritten for the in-dir layout. New test
TestRecognizeFormRequest_DefaultMdlAtArchiveParty covers
empty-form, create POST, re-edit row, and the negative cases
(Working/, non-mdl name) where the fallback must NOT fire.
- internal/handler/directory_test.go updated for the new
/<dir>/table.html redirect target.
- cmd/zddc-server/main_test.go TestDispatchSlashRouting Location
expectation updated.
- tests/form-safety.spec.js loads tables/dist/tables.html
(named form.html in the temp dir to trigger form-mode in the
dispatcher) so it tests the same bytes the server returns.
Title-element selector switches to #table-title.
- tests/tables.spec.js updates the status-filter test for the
uniform text-input filter.
Docs:
- AGENTS.md form-data system rewrites the URL conventions and
storage layout for in-dir; gains a Tables system section
parallel to forms describing the self-contained-directory
property; subfolder rules ("one table per folder by
construction; subfolders allowed and silently ignored as rows
— legitimate uses: nested sub-tables, per-row attachments,
drafts, future history sidecars") so we don't re-derive this.
Not included (deferred):
- ACL gating on cell-level writes — not relevant until Phase 3.
- Editable cells UI — separate commit (Phase 1).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
183 lines
6.6 KiB
JavaScript
183 lines
6.6 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[]');
|
|
}
|
|
|
|
// 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,
|
|
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);
|