prototype(#105): outline editor variants A/B/C — outliner interaction
CI / ci (push) Canceled after 0s
CI / ci (push) Canceled after 0s
Three-variant prototype for the #105 wayfinder ticket (map #97): how the keyboard-first outliner should feel. Verdict recorded on the ticket: dedicated block view (variant B) with LogSeq semantics — block-granular selection, text-only undo, fold as view state, one promoted NSTextView over the focused row. Variant A kept as the fold-as-text-edit anti-example; variant C as the painted-chrome runner-up that lost on visual fidelity. Also merges prototype/wf104-library-ui (fast-forward) so both prototypes ride main. Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
1 parent
135cc0a78e
commit
074f9af080
1 file changed
+808
@@ -0,0 +1,808 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<!--
|
||||||
|
PROTOTYPE — throwaway. Wayfinder ticket #105 (map #97): Outline editor outliner interaction.
|
||||||
|
Question: keyboard-first outliner over the SAME plain-text substrate (ADR-0010 mode B) or a dedicated outline view?
|
||||||
|
Three variants of the same document, switchable via ?variant=A|B|C, the floating bar, or plain ←/→ when not editing:
|
||||||
|
A "One text buffer" — the buffer IS the nested-bullet markdown; keys rewrite line ranges; fold = text edit (data-loss trap, kept as the anti-example).
|
||||||
|
B "Node rows" — tree model; bullets/indent guides are chrome; fold = view state; one editable per node (loses native undo/selection/caret).
|
||||||
|
C "Buffer + chrome" — A's single buffer (storage = text, native undo/selection/IME), LogSeq key semantics: fold = VIEW STATE
|
||||||
|
(lines in the buffer, hidden from render only), Enter splits at the caret, ↑/↓ hop visible blocks.
|
||||||
|
Storage ruling per #101: nested-bullet markdown, indentation = nesting, inline markdown in node text, [[wiki-links]] inert.
|
||||||
|
Stated assumptions: 2-space indent units in storage. Key set per #102: Tab/⇧Tab, ⌥↑/⌥↓ move-with-subtree, ⌘↑/⌘↓ fold, Enter sibling.
|
||||||
|
C prototype ceilings (ponytail: named, with upgrade path): fold identity = parent line text (duplicate lines collapse; app keys by node id);
|
||||||
|
structural ops repaint, so the BROWSER undo stack breaks across them (the real NSTextView routes programmatic edits through
|
||||||
|
shouldChangeText → undo stays whole); ←/→ at a fold boundary can step into hidden text (app filters navigation); Enter on non-item
|
||||||
|
lines falls native. Text sync reads textContent so display:none folded lines are never dropped.
|
||||||
|
NOTE: all innerHTML writes receive esc()-escaped text first — no untrusted HTML enters the DOM.
|
||||||
|
-->
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<title>wf105 — outline editor prototype</title>
|
||||||
|
<style>
|
||||||
|
:root {
|
||||||
|
--chrome: #FAF9F3; --paper: #FFFEFB; --ink: #26251F; --muted: #8A867A;
|
||||||
|
--hairline: rgba(38,37,31,.12); --rust: #C96442;
|
||||||
|
--serif: "Valkyrie OT", "Iowan Old Style", Georgia, serif;
|
||||||
|
--mono: "Ubuntu Mono", "SF Mono", Menlo, monospace;
|
||||||
|
}
|
||||||
|
* { box-sizing: border-box; }
|
||||||
|
html, body { margin: 0; height: 100%; }
|
||||||
|
body { background: var(--chrome); color: var(--ink); font-family: var(--serif); font-size: 17px; line-height: 1.5; }
|
||||||
|
|
||||||
|
.layout { display: grid; grid-template-columns: 1fr 340px; height: 100vh; }
|
||||||
|
main { display: flex; flex-direction: column; min-width: 0; }
|
||||||
|
.docwrap { flex: 1; display: flex; justify-content: center; overflow: auto; padding: 28px 24px 120px; }
|
||||||
|
.doc { width: 100%; max-width: 640px; background: var(--paper); border: 1px solid var(--hairline);
|
||||||
|
border-radius: 10px; padding: 36px 40px 60px; box-shadow: 0 1px 2px rgba(38,37,31,.04); }
|
||||||
|
|
||||||
|
h1 { font-size: 22px; font-weight: 600; margin: 0 0 2px; }
|
||||||
|
.meta { font-family: var(--mono); font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: .06em; margin-bottom: 22px; }
|
||||||
|
|
||||||
|
/* ---------- Variant A: one text buffer ---------- */
|
||||||
|
#bufferA {
|
||||||
|
font-family: var(--serif); font-size: 17px; line-height: 1.8; white-space: pre-wrap;
|
||||||
|
tab-size: 2; outline: none; min-height: 200px; caret-color: var(--rust);
|
||||||
|
}
|
||||||
|
#bufferA .mk { color: var(--muted); } /* dimmed markers: indent + "- " */
|
||||||
|
#bufferA .syn { color: var(--muted); } /* dimmed inline syntax: ** ` [[ ]] */
|
||||||
|
#bufferA .foldchip { font-family: var(--mono); font-size: 12px; color: var(--rust); }
|
||||||
|
|
||||||
|
/* ---------- Variant B: node rows ---------- */
|
||||||
|
.rows { user-select: text; }
|
||||||
|
.row { display: flex; align-items: flex-start; padding: 0 0 1px; position: relative; }
|
||||||
|
.row .pad { flex: none; }
|
||||||
|
.bullet { flex: none; width: 22px; height: 27px; display: flex; align-items: center; justify-content: center;
|
||||||
|
color: var(--muted); cursor: default; border-radius: 4px; }
|
||||||
|
.bullet:hover { color: var(--ink); background: rgba(196,175,138,.15); }
|
||||||
|
.bullet.foldable::before { content: "●"; font-size: 7px; }
|
||||||
|
.bullet.leaf::before { content: "◦"; font-size: 10px; }
|
||||||
|
.bullet.folded { color: var(--rust); }
|
||||||
|
.row .txt { flex: 1; min-width: 0; outline: none; caret-color: var(--rust); }
|
||||||
|
.row .txt:empty::before { content: "\00a0"; }
|
||||||
|
.b-code { font-family: var(--mono); font-size: 14px; background: rgba(38,37,31,.05); border-radius: 3px; padding: 0 3px; }
|
||||||
|
.row .txt strong { font-weight: 640; }
|
||||||
|
.wikichip { font-family: var(--mono); font-size: 13px; color: var(--muted); border-bottom: 1px dotted var(--muted); }
|
||||||
|
.foldcount { font-family: var(--mono); font-size: 11px; color: var(--rust); margin-left: 6px; }
|
||||||
|
.guide { position: absolute; top: 0; bottom: 0; width: 1px; background: var(--hairline); }
|
||||||
|
|
||||||
|
/* ---------- Variant C: buffer + chrome ---------- */
|
||||||
|
.cwrap { position: relative; }
|
||||||
|
#bufferC {
|
||||||
|
font-family: var(--serif); font-size: 17px; line-height: 1.8; white-space: pre-wrap;
|
||||||
|
tab-size: 2; outline: none; min-height: 200px; caret-color: var(--rust);
|
||||||
|
}
|
||||||
|
#bufferC .mk { color: var(--muted); }
|
||||||
|
#bufferC .syn { color: var(--muted); }
|
||||||
|
#bufferC .hiddenset { display: none; } /* folded lines: in the buffer, not rendered */
|
||||||
|
#bufferC .dash { color: transparent; } /* literal "- ": layout + caret kept, glyph unseen — the chrome dot replaces it */
|
||||||
|
#bufferC .ln { display: block; padding-left: calc(var(--d) * 18px + 13px); text-indent: -13px; }
|
||||||
|
#bufferC .ln.plain { padding-left: 0; text-indent: 0; }
|
||||||
|
.gutterC { position: absolute; left: 0; top: 0; width: 0; pointer-events: none; } /* anchor at host origin */
|
||||||
|
.gutterC .dot { position: absolute; left: 0; top: 0; transform: translate(-50%, 0); width: 22px; height: 27px; display: flex; align-items: center; justify-content: center;
|
||||||
|
color: var(--muted); pointer-events: auto; cursor: default; border-radius: 4px; }
|
||||||
|
.gutterC .dot:hover { color: var(--ink); background: rgba(196,175,138,.15); }
|
||||||
|
.gutterC .dot.folded { color: var(--rust); }
|
||||||
|
.gutterC .dot.foldable::before { content: "●"; font-size: 7px; }
|
||||||
|
.gutterC .dot.leaf::before { content: "◦"; font-size: 10px; }
|
||||||
|
.gutterC .fcount { position: absolute; left: 0; top: 0; transform: translate(8px, 0); font-family: var(--mono); font-size: 11px; color: var(--rust); }
|
||||||
|
.gutterC .vrule { position: absolute; width: 1px; background: var(--hairline); }
|
||||||
|
|
||||||
|
/* ---------- rail, notes ---------- */
|
||||||
|
aside { border-left: 1px solid var(--hairline); background: var(--chrome); padding: 20px 18px; overflow: auto; }
|
||||||
|
aside h2 { font-family: var(--mono); font-size: 11px; text-transform: uppercase; letter-spacing: .06em;
|
||||||
|
color: var(--muted); font-weight: 400; margin: 0 0 10px; }
|
||||||
|
#rail { font-family: var(--mono); font-size: 11.5px; line-height: 1.7; white-space: pre-wrap; color: var(--ink);
|
||||||
|
background: var(--paper); border: 1px solid var(--hairline); border-radius: 8px; padding: 12px; }
|
||||||
|
#railnote { font-family: var(--mono); font-size: 11px; color: var(--rust); min-height: 16px; margin-top: 8px; }
|
||||||
|
.keys { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 14px; }
|
||||||
|
.key { font-family: var(--mono); font-size: 11px; color: var(--muted); border: 1px solid var(--hairline);
|
||||||
|
border-radius: 99px; padding: 2px 9px; background: var(--paper); }
|
||||||
|
.verdictq { margin-top: 18px; font-size: 13px; line-height: 1.55; color: var(--ink); }
|
||||||
|
.verdictq em { color: var(--rust); font-style: normal; }
|
||||||
|
.notes { font-size: 12.5px; color: var(--muted); margin-top: 12px; line-height: 1.55; }
|
||||||
|
.diffhead { font-family: var(--mono); font-size: 10.5px; text-transform: uppercase; letter-spacing: .05em; color: var(--rust); margin: 16px 0 4px; }
|
||||||
|
button.reset { font-family: var(--mono); font-size: 11px; margin-top: 16px; padding: 4px 10px; border: 1px solid var(--hairline);
|
||||||
|
border-radius: 6px; background: var(--paper); color: var(--muted); cursor: pointer; }
|
||||||
|
|
||||||
|
/* ---------- floating variant switcher ---------- */
|
||||||
|
#switcher { position: fixed; left: 50%; bottom: 18px; transform: translateX(-50%); display: flex; align-items: center; gap: 10px;
|
||||||
|
background: #1F1E1A; color: #F5F2E8; border-radius: 99px; padding: 8px 14px; font-family: var(--mono); font-size: 12px;
|
||||||
|
box-shadow: 0 6px 18px rgba(0,0,0,.25); z-index: 10; }
|
||||||
|
#switcher button { background: none; border: none; color: #F5F2E8; font-size: 14px; cursor: pointer; padding: 0 4px; }
|
||||||
|
#switcher .vlabel b { color: #E8A587; }
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div class="layout">
|
||||||
|
<main>
|
||||||
|
<div class="docwrap"><div class="doc">
|
||||||
|
<h1>The Long Desk — quarterly essay</h1>
|
||||||
|
<div class="meta">type: outline · prototype wf105 · <span id="vname"></span></div>
|
||||||
|
<div id="stage"></div>
|
||||||
|
</div></div>
|
||||||
|
</main>
|
||||||
|
<aside>
|
||||||
|
<h2>Live markdown (storage)</h2>
|
||||||
|
<div id="rail"></div>
|
||||||
|
<div id="railnote"></div>
|
||||||
|
<div class="keys" id="keyrow"></div>
|
||||||
|
<div class="diffhead" id="diffhead"></div>
|
||||||
|
<div class="notes" id="diffnotes"></div>
|
||||||
|
<div class="verdictq" id="verdictq"></div>
|
||||||
|
<button class="reset" id="reset">reset document</button>
|
||||||
|
</aside>
|
||||||
|
</div>
|
||||||
|
<div id="switcher">
|
||||||
|
<button id="prev" title="previous variant">←</button>
|
||||||
|
<span class="vlabel">variant <b id="vkey">C</b> — <span id="vtitle"></span></span>
|
||||||
|
<button id="next" title="next variant">→</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
"use strict";
|
||||||
|
|
||||||
|
// ---------- shared: the document (nested-bullet markdown, #101 ruling) ----------
|
||||||
|
const SAMPLE = [
|
||||||
|
"- thesis",
|
||||||
|
" - the desk is not furniture, it is a **promise about time**",
|
||||||
|
" - long desks outlast the projects they were built for",
|
||||||
|
" - evidence",
|
||||||
|
" - the monastery scriptorium: standing desks, chained books, silence as infrastructure",
|
||||||
|
" - Darwin's study at Down House — same desk for forty years, the Beagle voyage compressed into a room (this line is deliberately long so you can feel subtree moves and folds over wrapped text in both variants)",
|
||||||
|
" - [[drafts/sand-walk]] as the walking counterpart",
|
||||||
|
" - counterargument",
|
||||||
|
" - standing desks as fashion, not discipline",
|
||||||
|
" - reply: fashion fades, the `posture` persists",
|
||||||
|
"- structure",
|
||||||
|
" - open on the physical object, close on the habit",
|
||||||
|
" - three movements, one per century",
|
||||||
|
"- cuts from the first draft",
|
||||||
|
" - the `typewriter` paragraph — keep for the epigraph file",
|
||||||
|
" - everything about [[reading-rooms]]",
|
||||||
|
].join("\n");
|
||||||
|
|
||||||
|
const UNITS = 2; // spaces per indent level (stated storage assumption)
|
||||||
|
const esc = s => s.replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">");
|
||||||
|
|
||||||
|
// minimal inline-markdown render for node text (bold, code, wiki-chip) — inert [[links]] per #101.
|
||||||
|
// esc() runs BEFORE any HTML is built, so no raw text enters innerHTML.
|
||||||
|
function inlineMD(s){
|
||||||
|
return esc(s)
|
||||||
|
.replace(/\*\*([^*]+)\*\*/g, "<strong>$1</strong>")
|
||||||
|
.replace(/`([^`]+)`/g, '<span class="b-code">$1</span>')
|
||||||
|
.replace(/\[\[([^\]]+)\]\]/g, '<span class="wikichip">[[$1]]</span>');
|
||||||
|
}
|
||||||
|
|
||||||
|
// shared line grammar
|
||||||
|
const itemRe = /^(\s*)[-*] /;
|
||||||
|
const isItem = l => itemRe.test(l);
|
||||||
|
const depthOf = l => { const m = l.match(/^(\s*)[-*] /); return m ? Math.floor(m[1].length / UNITS) : -1; };
|
||||||
|
function spanOf(ls, i){
|
||||||
|
const d = depthOf(ls[i]); if (d < 0) return null;
|
||||||
|
let e = i + 1;
|
||||||
|
while (e < ls.length && (depthOf(ls[e]) > d || ls[e].trim() === "")) e++;
|
||||||
|
return { s: i, e, d };
|
||||||
|
}
|
||||||
|
function prevSib(ls, i){
|
||||||
|
const d = depthOf(ls[i]);
|
||||||
|
let k = i - 1;
|
||||||
|
while (k >= 0 && ls[k].trim() === "") k--;
|
||||||
|
if (k < 0 || depthOf(ls[k]) !== d) return null;
|
||||||
|
return k;
|
||||||
|
}
|
||||||
|
function nextSib(ls, i){
|
||||||
|
const d = depthOf(ls[i]);
|
||||||
|
let k = i + 1;
|
||||||
|
while (k < ls.length && (depthOf(ls[k]) > d || ls[k].trim() === "")) k++;
|
||||||
|
if (k >= ls.length || depthOf(ls[k]) !== d) return null;
|
||||||
|
return k;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------- Variant A: one text buffer, fold = text edit (the anti-example) ----------
|
||||||
|
const A = (() => {
|
||||||
|
let text = SAMPLE, folds = new Map(), foldSeq = 0, host = null;
|
||||||
|
const lines = () => text.split("\n");
|
||||||
|
const span = i => spanOf(lines(), i);
|
||||||
|
|
||||||
|
function caretLC(){
|
||||||
|
const sel = window.getSelection();
|
||||||
|
if (!sel.rangeCount || !host.contains(sel.anchorNode)) return null;
|
||||||
|
const r = document.createRange(); r.setStart(host, 0); r.setEnd(sel.anchorNode, sel.anchorOffset);
|
||||||
|
const upto = r.toString();
|
||||||
|
return { line: upto.split("\n").length - 1, col: upto.length - (upto.lastIndexOf("\n") + 1) };
|
||||||
|
}
|
||||||
|
function caretTo(line, col){
|
||||||
|
const ls = lines();
|
||||||
|
let off = 0;
|
||||||
|
for (let k = 0; k < line && k < ls.length; k++) off += ls[k].length + 1;
|
||||||
|
off += Math.min(col, (ls[line] || "").length);
|
||||||
|
const walker = document.createTreeWalker(host, NodeFilter.SHOW_TEXT);
|
||||||
|
let acc = 0, n;
|
||||||
|
while ((n = walker.nextNode())){
|
||||||
|
if (acc + n.length >= off){
|
||||||
|
const r = document.createRange();
|
||||||
|
r.setStart(n, Math.max(0, off - acc)); r.collapse(true);
|
||||||
|
const s = window.getSelection(); s.removeAllRanges(); s.addRange(r);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
acc += n.length;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const caretLineEnd = i => caretTo(i, Infinity);
|
||||||
|
|
||||||
|
function paint(){
|
||||||
|
host.innerHTML = lines().map(l => {
|
||||||
|
const chip = l.match(/^(\s*)(- ⋯ \d+ folded)(⟦f\d+⟧)?$/);
|
||||||
|
if (chip) return `<span class="mk">${esc(chip[1] + "- ")}</span><span class="foldchip">${esc(chip[2])}</span><span class="foldchip">${esc(chip[3] || "")}</span>`;
|
||||||
|
if (!isItem(l)) return esc(l);
|
||||||
|
const m = l.match(/^(\s*)([-*] )(.*)$/);
|
||||||
|
return `<span class="mk">${esc(m[1] + m[2])}</span>` +
|
||||||
|
esc(m[3]).replace(/\*\*([^*]*)\*\*/g, '<span class="syn">**</span>$1<span class="syn">**</span>')
|
||||||
|
.replace(/`([^`]*)`/g, '<span class="syn">`</span>$1<span class="syn">`</span>')
|
||||||
|
.replace(/(\[\[[^\]]*\]\])/g, '<span class="syn">$1</span>');
|
||||||
|
}).join("\n");
|
||||||
|
}
|
||||||
|
function commit(newText, lc){
|
||||||
|
text = newText; paint();
|
||||||
|
if (lc) caretTo(lc.line, lc.col);
|
||||||
|
rail();
|
||||||
|
}
|
||||||
|
function indent(out){
|
||||||
|
const lc = caretLC(); if (!lc) return;
|
||||||
|
const ls = lines(); const sp = span(lc.line); if (!sp) return;
|
||||||
|
if (!out){
|
||||||
|
if (prevSib(ls, lc.line) == null) return;
|
||||||
|
const block = ls.splice(sp.s, sp.e - sp.s).map(l => " ".repeat(UNITS) + l);
|
||||||
|
ls.splice(sp.s, 0, ...block);
|
||||||
|
commit(ls.join("\n"), { line: lc.line, col: lc.col + UNITS });
|
||||||
|
} else {
|
||||||
|
if (sp.d === 0) return;
|
||||||
|
const block = ls.splice(sp.s, sp.e - sp.s).map(l => l.replace(/^ {1,2}/, ""));
|
||||||
|
ls.splice(sp.s, 0, ...block);
|
||||||
|
commit(ls.join("\n"), { line: lc.line, col: Math.max(0, lc.col - UNITS) });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
function move(dir){
|
||||||
|
const lc = caretLC(); if (!lc) return;
|
||||||
|
const ls = lines(); const me = span(lc.line); if (!me) return;
|
||||||
|
let a, b;
|
||||||
|
if (dir < 0){
|
||||||
|
const p = prevSib(ls, lc.line); if (p == null) return;
|
||||||
|
a = span(p); b = me;
|
||||||
|
} else {
|
||||||
|
const n = nextSib(ls, lc.line); if (n == null) return;
|
||||||
|
a = me; b = span(n);
|
||||||
|
}
|
||||||
|
const blockA = ls.slice(a.s, a.e), blockB = ls.slice(b.s, b.e);
|
||||||
|
const head = ls.slice(0, a.s), mid = ls.slice(a.e, b.s), tail = ls.slice(b.e);
|
||||||
|
const newLs = [...head, ...blockB, ...mid, ...blockA, ...tail];
|
||||||
|
const myNewLine = dir < 0 ? a.s : a.s + blockB.length + mid.length;
|
||||||
|
commit(newLs.join("\n"), { line: myNewLine, col: Math.min(lc.col, newLs[myNewLine].length) });
|
||||||
|
}
|
||||||
|
function fold(){
|
||||||
|
const lc = caretLC(); if (!lc) return;
|
||||||
|
const ls = lines(); const sp = span(lc.line);
|
||||||
|
if (!sp || sp.e === sp.s + 1) return;
|
||||||
|
if (ls[sp.s + 1].includes("⟦f")) return;
|
||||||
|
const children = ls.splice(sp.s + 1, sp.e - sp.s - 1);
|
||||||
|
const id = foldSeq++;
|
||||||
|
ls.splice(sp.s + 1, 0, " ".repeat((sp.d + 1) * UNITS) + `- ⋯ ${children.length} folded⟦f${id}⟧`);
|
||||||
|
folds.set(id, children);
|
||||||
|
commit(ls.join("\n"), null); caretLineEnd(sp.s);
|
||||||
|
note(`fold: ${children.length} lines left the buffer as ONE marker line — a text edit, not view state (data-loss trap)`);
|
||||||
|
}
|
||||||
|
function unfold(){
|
||||||
|
const lc = caretLC(); if (!lc) return;
|
||||||
|
const ls = lines();
|
||||||
|
for (const k of [lc.line, lc.line + 1]){
|
||||||
|
const m = (ls[k] || "").match(/⟦f(\d+)⟧/);
|
||||||
|
if (m && folds.has(+m[1])){
|
||||||
|
const children = folds.get(+m[1]);
|
||||||
|
ls.splice(k, 1, ...children);
|
||||||
|
folds.delete(+m[1]);
|
||||||
|
commit(ls.join("\n"), null); caretLineEnd(k);
|
||||||
|
note("unfold: stashed lines returned into the buffer");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
note("unfold: caret is not on/near a fold marker");
|
||||||
|
}
|
||||||
|
function enterKey(shift){
|
||||||
|
const lc = caretLC(); if (!lc) return true;
|
||||||
|
const ls = lines();
|
||||||
|
if (!isItem(ls[lc.line])) return true;
|
||||||
|
if (shift) return true;
|
||||||
|
const d = depthOf(ls[lc.line]);
|
||||||
|
if (ls[lc.line].replace(itemRe, "").trim() === ""){
|
||||||
|
if (d > 0) indent(true);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
const sel = window.getSelection();
|
||||||
|
const r = document.createRange(); r.setStart(host, 0); r.setEnd(sel.anchorNode, sel.anchorOffset);
|
||||||
|
const off = r.toString().length;
|
||||||
|
const newText = text.slice(0, off) + "\n" + " ".repeat(d * UNITS) + "- " + text.slice(off);
|
||||||
|
commit(newText, { line: lc.line + 1, col: d * UNITS + 2 });
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
function mount(stage){
|
||||||
|
stage.innerHTML = `<div id="bufferA" contenteditable="true" spellcheck="false"></div>`;
|
||||||
|
host = stage.querySelector("#bufferA");
|
||||||
|
text = SAMPLE; folds = new Map(); foldSeq = 0;
|
||||||
|
paint(); caretTo(0, 0); rail();
|
||||||
|
host.addEventListener("input", () => { text = host.innerText.replace(/\n$/, ""); rail(); });
|
||||||
|
host.addEventListener("keydown", e => {
|
||||||
|
if (e.key === "Tab"){ e.preventDefault(); indent(e.shiftKey); }
|
||||||
|
else if (e.altKey && (e.key === "ArrowUp" || e.key === "ArrowDown")){ e.preventDefault(); move(e.key === "ArrowUp" ? -1 : 1); }
|
||||||
|
else if (e.metaKey && e.key === "ArrowUp"){ e.preventDefault(); fold(); }
|
||||||
|
else if (e.metaKey && e.key === "ArrowDown"){ e.preventDefault(); unfold(); }
|
||||||
|
else if (e.key === "Enter"){ if (!enterKey(e.shiftKey)) e.preventDefault(); }
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return { mount, storage: () => text, label: "One text buffer" };
|
||||||
|
})();
|
||||||
|
|
||||||
|
// ---------- Variant B: node rows (dedicated view) ----------
|
||||||
|
const B = (() => {
|
||||||
|
let root = null, uid = 0, host = null, focusId = null, caretOff = null;
|
||||||
|
|
||||||
|
function parse(){
|
||||||
|
root = { id: uid++, text: "", children: [], folded: false };
|
||||||
|
const stack = [{ d: -1, n: root }];
|
||||||
|
for (const l of SAMPLE.split("\n")){
|
||||||
|
const m = l.match(/^(\s*)[-*] (.*)$/); if (!m) continue;
|
||||||
|
const d = Math.floor(m[1].length / UNITS);
|
||||||
|
const node = { id: uid++, text: m[2], children: [], folded: false };
|
||||||
|
while (stack[stack.length - 1].d >= d) stack.pop();
|
||||||
|
stack[stack.length - 1].n.children.push(node);
|
||||||
|
stack.push({ d, n: node });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
function serialize(){
|
||||||
|
const out = [];
|
||||||
|
(function walk(nodes, d){
|
||||||
|
for (const n of nodes){
|
||||||
|
out.push(" ".repeat(d * UNITS) + "- " + n.text);
|
||||||
|
walk(n.children, d + 1);
|
||||||
|
}
|
||||||
|
})(root.children, 0);
|
||||||
|
return out.join("\n");
|
||||||
|
}
|
||||||
|
function findPath(id){
|
||||||
|
const path = [];
|
||||||
|
(function walk(nodes){
|
||||||
|
for (let k = 0; k < nodes.length; k++){
|
||||||
|
path.push(k);
|
||||||
|
if (nodes[k].id === id) return true;
|
||||||
|
if (walk(nodes[k].children)) return true;
|
||||||
|
path.pop();
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
})(root.children);
|
||||||
|
return path;
|
||||||
|
}
|
||||||
|
const sibsOf = path => { let arr = root.children; for (let i = 0; i < path.length - 1; i++) arr = arr[path[i]].children; return arr; };
|
||||||
|
const countAll = n => n.children.reduce((a, c) => a + 1 + countAll(c), 0);
|
||||||
|
|
||||||
|
function render(){
|
||||||
|
host.innerHTML = "";
|
||||||
|
(function walk(nodes, d){
|
||||||
|
for (const n of nodes){
|
||||||
|
const row = document.createElement("div"); row.className = "row"; row.dataset.id = n.id;
|
||||||
|
for (let g = 0; g < d; g++){
|
||||||
|
const guide = document.createElement("div"); guide.className = "guide";
|
||||||
|
guide.style.left = (g * UNITS * 9 + 11) + "px"; row.appendChild(guide);
|
||||||
|
}
|
||||||
|
const pad = document.createElement("div"); pad.className = "pad"; pad.style.width = (d * UNITS * 9) + "px"; row.appendChild(pad);
|
||||||
|
const bullet = document.createElement("div");
|
||||||
|
bullet.className = "bullet " + (n.children.length ? "foldable" : "leaf") + (n.folded ? " folded" : "");
|
||||||
|
bullet.addEventListener("mousedown", e => { e.preventDefault(); n.folded = !n.folded; focusId = n.id; caretOff = null; render(); });
|
||||||
|
row.appendChild(bullet);
|
||||||
|
const txt = document.createElement("div"); txt.className = "txt"; txt.contentEditable = "true"; txt.spellcheck = false;
|
||||||
|
txt.innerHTML = inlineMD(n.text); // esc() inside inlineMD — safe
|
||||||
|
txt.addEventListener("input", () => { n.text = txt.innerText.replace(/\n/g, " "); rail(); });
|
||||||
|
txt.addEventListener("focus", () => { focusId = n.id; });
|
||||||
|
txt.addEventListener("blur", () => { saveCaret(txt); });
|
||||||
|
txt.addEventListener("keyup", () => { saveCaret(txt); });
|
||||||
|
txt.addEventListener("keydown", e => keydown(e, n));
|
||||||
|
row.appendChild(txt);
|
||||||
|
if (n.folded && n.children.length){
|
||||||
|
const c = document.createElement("span"); c.className = "foldcount"; c.textContent = "⋯ " + countAll(n);
|
||||||
|
row.appendChild(c);
|
||||||
|
}
|
||||||
|
host.appendChild(row);
|
||||||
|
if (!n.folded) walk(n.children, d + 1);
|
||||||
|
}
|
||||||
|
})(root.children, 0);
|
||||||
|
if (focusId != null){
|
||||||
|
const el = host.querySelector(`.row[data-id="${focusId}"] .txt`);
|
||||||
|
if (el){
|
||||||
|
el.focus();
|
||||||
|
const sel = window.getSelection(); const r = document.createRange();
|
||||||
|
if (caretOff == null){ r.selectNodeContents(el); r.collapse(false); }
|
||||||
|
else {
|
||||||
|
r.selectNodeContents(el); r.collapse(true);
|
||||||
|
const walker = document.createTreeWalker(el, NodeFilter.SHOW_TEXT);
|
||||||
|
let acc = 0, n;
|
||||||
|
while ((n = walker.nextNode())){
|
||||||
|
if (acc + n.length >= caretOff){ r.setStart(n, Math.max(0, caretOff - acc)); r.collapse(true); break; }
|
||||||
|
acc += n.length;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sel.removeAllRanges(); sel.addRange(r);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
rail();
|
||||||
|
}
|
||||||
|
function saveCaret(el){
|
||||||
|
const sel = window.getSelection();
|
||||||
|
if (!sel.rangeCount){ caretOff = null; return; }
|
||||||
|
const r = document.createRange(); r.setStart(el, 0); r.setEnd(sel.anchorNode, sel.anchorOffset);
|
||||||
|
caretOff = r.toString().length;
|
||||||
|
}
|
||||||
|
function keydown(e, n){
|
||||||
|
const el = e.currentTarget;
|
||||||
|
if (e.key === "Tab"){
|
||||||
|
e.preventDefault(); saveCaret(el);
|
||||||
|
const path = findPath(n.id); const sibs = sibsOf(path); const k = path[path.length - 1];
|
||||||
|
if (!e.shiftKey){
|
||||||
|
if (k === 0) return;
|
||||||
|
const prev = sibs[k - 1];
|
||||||
|
sibs.splice(k, 1); prev.children.push(n);
|
||||||
|
} else {
|
||||||
|
if (path.length < 2) return;
|
||||||
|
const parentPath = path.slice(0, -1); const parentSibs = sibsOf(parentPath);
|
||||||
|
const pk = parentPath[parentPath.length - 1];
|
||||||
|
sibs.splice(k, 1); parentSibs.splice(pk + 1, 0, n);
|
||||||
|
}
|
||||||
|
render();
|
||||||
|
}
|
||||||
|
else if (e.altKey && (e.key === "ArrowUp" || e.key === "ArrowDown")){
|
||||||
|
e.preventDefault(); saveCaret(el);
|
||||||
|
const path = findPath(n.id); const sibs = sibsOf(path); const k = path[path.length - 1];
|
||||||
|
const to = e.key === "ArrowUp" ? k - 1 : k + 1;
|
||||||
|
if (to < 0 || to >= sibs.length) return;
|
||||||
|
sibs.splice(k, 1); sibs.splice(to, 0, n);
|
||||||
|
render();
|
||||||
|
}
|
||||||
|
else if (e.metaKey && e.key === "ArrowUp"){ e.preventDefault(); if (n.children.length){ n.folded = true; render(); } }
|
||||||
|
else if (e.metaKey && e.key === "ArrowDown"){ e.preventDefault(); if (n.children.length){ n.folded = false; render(); } }
|
||||||
|
else if (e.key === "Enter"){
|
||||||
|
e.preventDefault(); saveCaret(el);
|
||||||
|
const path = findPath(n.id); const sibs = sibsOf(path); const k = path[path.length - 1];
|
||||||
|
if (n.text.trim() === "" && path.length >= 2){
|
||||||
|
const parentPath = path.slice(0, -1); const parentSibs = sibsOf(parentPath);
|
||||||
|
const pk = parentPath[parentPath.length - 1];
|
||||||
|
sibs.splice(k, 1); parentSibs.splice(pk + 1, 0, n);
|
||||||
|
render(); return;
|
||||||
|
}
|
||||||
|
const node = { id: uid++, text: "", children: [], folded: false }; // fresh sibling (no caret-split — noted ceiling)
|
||||||
|
sibs.splice(k + 1, 0, node);
|
||||||
|
focusId = node.id; caretOff = 0;
|
||||||
|
render();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
function mount(stage){
|
||||||
|
stage.innerHTML = `<div class="rows" id="rowsB"></div>`;
|
||||||
|
host = stage.querySelector("#rowsB");
|
||||||
|
parse(); focusId = null; caretOff = null;
|
||||||
|
render();
|
||||||
|
}
|
||||||
|
return { mount, storage: serialize, label: "Node rows" };
|
||||||
|
})();
|
||||||
|
|
||||||
|
// ---------- Variant C: buffer + chrome — LogSeq semantics on the ADR-0010 substrate ----------
|
||||||
|
const C = (() => {
|
||||||
|
let text = SAMPLE, host = null, gutter = null;
|
||||||
|
const folds = new Set(); // folded parent line indices (view state; identity = parent text, dupes collapse — ceiling)
|
||||||
|
|
||||||
|
const lines = () => text.split("\n");
|
||||||
|
const span = i => spanOf(lines(), i);
|
||||||
|
const isHidden = i => { for (const f of folds){ const sp = span(f); if (i > f && i < sp.e) return true; } return false; };
|
||||||
|
|
||||||
|
function caretLC(){
|
||||||
|
const sel = window.getSelection();
|
||||||
|
if (!sel.rangeCount || !host.contains(sel.anchorNode)) return null;
|
||||||
|
const r = document.createRange(); r.setStart(host, 0); r.setEnd(sel.anchorNode, sel.anchorOffset);
|
||||||
|
const upto = r.toString();
|
||||||
|
return { line: upto.split("\n").length - 1, col: upto.length - (upto.lastIndexOf("\n") + 1) };
|
||||||
|
}
|
||||||
|
function caretTo(line, col){
|
||||||
|
const ls = lines();
|
||||||
|
let off = 0;
|
||||||
|
for (let k = 0; k < line && k < ls.length; k++) off += ls[k].length + 1;
|
||||||
|
off += Math.min(col, (ls[line] || "").length);
|
||||||
|
const walker = document.createTreeWalker(host, NodeFilter.SHOW_TEXT);
|
||||||
|
let acc = 0, n;
|
||||||
|
while ((n = walker.nextNode())){
|
||||||
|
if (acc + n.length >= off){
|
||||||
|
const r = document.createRange();
|
||||||
|
r.setStart(n, Math.max(0, off - acc)); r.collapse(true);
|
||||||
|
const s = window.getSelection(); s.removeAllRanges(); s.addRange(r);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
acc += n.length;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const caretLineEnd = i => caretTo(i, Infinity);
|
||||||
|
|
||||||
|
function paintedLine(l){
|
||||||
|
if (!isItem(l)) return `<span class="ln plain">${esc(l)}</span>`;
|
||||||
|
const m = l.match(/^(\s*)([-*] )(.*)$/);
|
||||||
|
const d = depthOf(l);
|
||||||
|
// per-line block: padding-left does the indent, text-indent hangs wraps under node text.
|
||||||
|
// indent whitespace stays in the text (storage truth); dash transparent (layout kept).
|
||||||
|
return `<span class="ln" style="--d:${d}"><span class="mk">${esc(m[1])}</span><span class="dash">${esc(m[2])}</span>` +
|
||||||
|
esc(m[3]).replace(/\*\*([^*]*)\*\*/g, '<span class="syn">**</span>$1<span class="syn">**</span>')
|
||||||
|
.replace(/`([^`]*)`/g, '<span class="syn">`</span>$1<span class="syn">`</span>')
|
||||||
|
.replace(/(\[\[[^\]]*\]\])/g, '<span class="syn">$1</span>') + `</span>`;
|
||||||
|
}
|
||||||
|
|
||||||
|
function render(){
|
||||||
|
const ls = lines();
|
||||||
|
const parts = [];
|
||||||
|
for (let i = 0; i < ls.length; i++){
|
||||||
|
if (folds.has(i)){
|
||||||
|
const sp = span(i);
|
||||||
|
parts.push(paintedLine(ls[i]));
|
||||||
|
// folded children: IN the buffer, hidden from render only — storage text unchanged
|
||||||
|
parts.push(`<span class="hiddenset">${ls.slice(i + 1, sp.e).map(paintedLine).join("\n")}</span>`);
|
||||||
|
i = sp.e - 1;
|
||||||
|
} else {
|
||||||
|
parts.push(paintedLine(ls[i]));
|
||||||
|
}
|
||||||
|
if (i < ls.length - 1) parts.push("\n");
|
||||||
|
}
|
||||||
|
host.innerHTML = parts.join("");
|
||||||
|
drawGutter();
|
||||||
|
rail();
|
||||||
|
}
|
||||||
|
|
||||||
|
// gutter dots positioned by measuring each visible line's rect via range probes (wrap-safe)
|
||||||
|
function drawGutter(){
|
||||||
|
const ls = lines();
|
||||||
|
gutter.innerHTML = "";
|
||||||
|
const hostRect = host.getBoundingClientRect();
|
||||||
|
const hostTop = hostRect.top, hostLeft = hostRect.left;
|
||||||
|
let off = 0;
|
||||||
|
for (let i = 0; i < ls.length; i++){
|
||||||
|
const lineLen = ls[i].length;
|
||||||
|
if (isItem(ls[i]) && !isHidden(i)){
|
||||||
|
const hasKids = span(i).e > i + 1;
|
||||||
|
const y = lineTopAt(off, hostTop);
|
||||||
|
const x = markerLeftAt(off + indentWidth(ls[i]), hostLeft);
|
||||||
|
if (y != null && x != null){
|
||||||
|
const dot = document.createElement("div");
|
||||||
|
dot.className = "dot " + (hasKids ? "foldable" : "leaf") + (folds.has(i) ? " folded" : "");
|
||||||
|
dot.style.top = (y - 1) + "px";
|
||||||
|
dot.style.left = x + "px"; // x = the transparent marker's own column — nesting inherits
|
||||||
|
dot.title = hasKids ? "fold/unfold (⌘↑ / ⌘↓)" : "";
|
||||||
|
if (hasKids) dot.addEventListener("mousedown", e => { e.preventDefault(); toggleFold(i); });
|
||||||
|
gutter.appendChild(dot);
|
||||||
|
if (folds.has(i)){
|
||||||
|
const hidden = span(i).e - i - 1;
|
||||||
|
const c = document.createElement("div"); c.className = "fcount"; c.textContent = "⋯" + hidden;
|
||||||
|
c.style.top = (y + 5) + "px";
|
||||||
|
c.style.left = (x + 24) + "px";
|
||||||
|
gutter.appendChild(c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
off += lineLen + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// x of the marker: probe the rect just AFTER the indent (where "- " begins) — transparent but laid out
|
||||||
|
function markerLeftAt(charOff, hostLeft){
|
||||||
|
const walker = document.createTreeWalker(host, NodeFilter.SHOW_TEXT);
|
||||||
|
let acc = 0, n;
|
||||||
|
while ((n = walker.nextNode())){
|
||||||
|
if (acc + n.length >= charOff){
|
||||||
|
const r = document.createRange();
|
||||||
|
r.setStart(n, Math.max(0, charOff - acc)); r.collapse(true);
|
||||||
|
const rects = r.getClientRects();
|
||||||
|
return rects.length ? rects[0].left - hostLeft : null;
|
||||||
|
}
|
||||||
|
acc += n.length;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
// storage indent width in characters (line start → marker start)
|
||||||
|
const indentWidth = l => (l.match(/^(\s*)[-*] /) || ["", ""])[1].length;
|
||||||
|
function lineTopAt(charOff, hostTop){
|
||||||
|
const walker = document.createTreeWalker(host, NodeFilter.SHOW_TEXT);
|
||||||
|
let acc = 0, n;
|
||||||
|
while ((n = walker.nextNode())){
|
||||||
|
if (acc + n.length >= charOff){
|
||||||
|
const r = document.createRange();
|
||||||
|
r.setStart(n, Math.max(0, charOff - acc)); r.collapse(true);
|
||||||
|
const rects = r.getClientRects();
|
||||||
|
if (!rects.length) return null; // inside a hidden span — no rect
|
||||||
|
return rects[0].top - hostTop;
|
||||||
|
}
|
||||||
|
acc += n.length;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
function toggleFold(i){
|
||||||
|
const sp = span(i);
|
||||||
|
if (!sp || sp.e === i + 1) return;
|
||||||
|
if (folds.has(i)) folds.delete(i); else folds.add(i);
|
||||||
|
render();
|
||||||
|
caretLineEnd(i);
|
||||||
|
note(folds.has(i)
|
||||||
|
? `fold: ${sp.e - i - 1} lines hidden from RENDER only — the storage rail did not move; quit loses nothing`
|
||||||
|
: `unfold: hidden lines re-rendered from the buffer`);
|
||||||
|
}
|
||||||
|
|
||||||
|
// fold identity survives structural edits by matching parent text (dupes collapse — stated ceiling)
|
||||||
|
function remapFolds(oldLs){
|
||||||
|
const keys = [...folds].map(i => oldLs[i]);
|
||||||
|
folds.clear();
|
||||||
|
const ls = lines();
|
||||||
|
for (const t of keys){
|
||||||
|
const i = ls.indexOf(t);
|
||||||
|
if (i >= 0 && isItem(ls[i]) && span(i).e > i + 1) folds.add(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function commit(newText, lc){
|
||||||
|
text = newText;
|
||||||
|
render();
|
||||||
|
if (lc) caretTo(lc.line, lc.col);
|
||||||
|
}
|
||||||
|
|
||||||
|
function indent(out){
|
||||||
|
const lc = caretLC(); if (!lc) return;
|
||||||
|
const ls = lines(); const sp = span(lc.line); if (!sp) return;
|
||||||
|
const oldLs = [...ls];
|
||||||
|
if (!out){
|
||||||
|
if (prevSib(ls, lc.line) == null) return;
|
||||||
|
const block = ls.splice(sp.s, sp.e - sp.s).map(l => " ".repeat(UNITS) + l);
|
||||||
|
ls.splice(sp.s, 0, ...block);
|
||||||
|
remapFolds(oldLs);
|
||||||
|
commit(ls.join("\n"), { line: lc.line, col: lc.col + UNITS });
|
||||||
|
} else {
|
||||||
|
if (sp.d === 0) return;
|
||||||
|
const block = ls.splice(sp.s, sp.e - sp.s).map(l => l.replace(/^ {1,2}/, ""));
|
||||||
|
ls.splice(sp.s, 0, ...block);
|
||||||
|
remapFolds(oldLs);
|
||||||
|
commit(ls.join("\n"), { line: lc.line, col: Math.max(0, lc.col - UNITS) });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function move(dir){
|
||||||
|
const lc = caretLC(); if (!lc) return;
|
||||||
|
const ls = lines(); const me = span(lc.line); if (!me) return;
|
||||||
|
let a, b;
|
||||||
|
if (dir < 0){
|
||||||
|
const p = prevSib(ls, lc.line); if (p == null) return;
|
||||||
|
a = span(p); b = me;
|
||||||
|
} else {
|
||||||
|
const n = nextSib(ls, lc.line); if (n == null) return;
|
||||||
|
a = me; b = span(n);
|
||||||
|
}
|
||||||
|
const blockA = ls.slice(a.s, a.e), blockB = ls.slice(b.s, b.e);
|
||||||
|
const head = ls.slice(0, a.s), mid = ls.slice(a.e, b.s), tail = ls.slice(b.e);
|
||||||
|
const newLs = [...head, ...blockB, ...mid, ...blockA, ...tail];
|
||||||
|
const myNewLine = dir < 0 ? a.s : a.s + blockB.length + mid.length;
|
||||||
|
remapFolds(ls);
|
||||||
|
commit(newLs.join("\n"), { line: myNewLine, col: Math.min(lc.col, newLs[myNewLine].length) });
|
||||||
|
note("move: subtree traveled as a line-range swap — folds followed their parents");
|
||||||
|
}
|
||||||
|
|
||||||
|
// ↑/↓ hop VISIBLE blocks (LogSeq feel); plain ←/→ stay native
|
||||||
|
function blockHop(dir){
|
||||||
|
const lc = caretLC(); if (!lc) return;
|
||||||
|
const ls = lines();
|
||||||
|
let i = lc.line + dir;
|
||||||
|
while (i >= 0 && i < ls.length){
|
||||||
|
if (isItem(ls[i]) && !isHidden(i)){ caretLineEnd(i); return; }
|
||||||
|
i += dir;
|
||||||
|
}
|
||||||
|
if (i < 0) caretLineEnd(0);
|
||||||
|
else if (i >= ls.length) caretLineEnd(ls.length - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
function enterKey(shift){
|
||||||
|
const lc = caretLC(); if (!lc) return true;
|
||||||
|
const ls = lines();
|
||||||
|
if (!isItem(ls[lc.line])) return true; // non-item line: native Enter
|
||||||
|
if (shift) return true; // ⇧Enter: soft newline inside node text
|
||||||
|
const line = ls[lc.line];
|
||||||
|
const d = depthOf(line);
|
||||||
|
if (line.replace(itemRe, "").trim() === ""){
|
||||||
|
if (d > 0) indent(true); // empty node: Enter outdents
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
// split at caret: tail becomes the new sibling below (LogSeq behavior)
|
||||||
|
const oldLs = [...ls];
|
||||||
|
const markerLen = line.match(itemRe)[0].length;
|
||||||
|
const splitCol = Math.max(lc.col, markerLen);
|
||||||
|
const before = line.slice(0, splitCol);
|
||||||
|
const after = line.slice(splitCol);
|
||||||
|
ls[lc.line] = before;
|
||||||
|
ls.splice(lc.line + 1, 0, " ".repeat(d * UNITS) + "- " + after);
|
||||||
|
remapFolds(oldLs);
|
||||||
|
commit(ls.join("\n"), { line: lc.line + 1, col: d * UNITS + 2 });
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
function mount(stage){
|
||||||
|
stage.innerHTML = `<div class="cwrap"><div class="gutterC"></div><div id="bufferC" contenteditable="true" spellcheck="false"></div></div>`;
|
||||||
|
host = stage.querySelector("#bufferC");
|
||||||
|
gutter = stage.querySelector(".gutterC");
|
||||||
|
text = SAMPLE; folds.clear();
|
||||||
|
render(); caretTo(0, 0);
|
||||||
|
// text sync via textContent: display:none folded lines are INCLUDED — never dropped
|
||||||
|
host.addEventListener("input", () => { text = host.textContent.replace(/\n$/, ""); rail(); });
|
||||||
|
host.addEventListener("keydown", e => {
|
||||||
|
if (e.key === "Tab"){ e.preventDefault(); indent(e.shiftKey); }
|
||||||
|
else if (e.altKey && (e.key === "ArrowUp" || e.key === "ArrowDown")){ e.preventDefault(); move(e.key === "ArrowUp" ? -1 : 1); }
|
||||||
|
else if (e.metaKey && e.key === "ArrowUp"){ e.preventDefault(); const lc = caretLC(); if (lc) toggleFold(lc.line); }
|
||||||
|
else if (e.metaKey && e.key === "ArrowDown"){ e.preventDefault(); const lc = caretLC(); if (lc) toggleFold(lc.line); }
|
||||||
|
else if (!e.altKey && !e.metaKey && (e.key === "ArrowUp" || e.key === "ArrowDown")){ e.preventDefault(); blockHop(e.key === "ArrowUp" ? -1 : 1); }
|
||||||
|
else if (e.key === "Enter"){ if (!enterKey(e.shiftKey)) e.preventDefault(); }
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return { mount, storage: () => text, label: "Buffer + chrome" };
|
||||||
|
})();
|
||||||
|
|
||||||
|
// ---------- variant plumbing ----------
|
||||||
|
const VARIANTS = { A, B, C };
|
||||||
|
const ORDER = ["A", "B", "C"];
|
||||||
|
const META = {
|
||||||
|
A: {
|
||||||
|
title: "One text buffer",
|
||||||
|
keys: `Tab/⇧Tab indent · ⌥↑⌥↓ move · ⌘↑⌘↓ fold · Enter sibling`,
|
||||||
|
diff: `The buffer IS the markdown (#101 storage = editor state, ADR-0010 mode B). Markers live in the text, dimmed not hidden. Folding <em>replaces</em> child lines with one "⋯ N folded" marker line — a text edit; watch the rail move. Typing undo (⌘Z) is native and free. The marker line is the cost: folds become buffer state — and quitting with folds stashed LOSES the hidden lines. The trap you spotted: kept as the anti-example.`,
|
||||||
|
q: `Feel how selection/undo/caret are free — then remember the fold writes into your file. Disqualified on that alone.`
|
||||||
|
},
|
||||||
|
B: {
|
||||||
|
title: "Node rows",
|
||||||
|
keys: `Tab/⇧Tab indent · ⌥↑⌥↓ move · ⌘↑⌘↓ fold · Enter sibling`,
|
||||||
|
diff: `A tree of rows; bullets and indent guides are chrome, node text is the only content. Folding is pure view state — rows simply don't render, the rail never changes. Costs to feel: ⌘Z does nothing after structural ops, no cross-row selection, caret is restored by hand after every op, Enter does not split at the caret here. LogSeq has these working — behind a hand-rolled selection/caret/undo layer built over its database, the most expensive corner of that codebase.`,
|
||||||
|
q: `Is per-row editing + instant view-state fold worth losing free-text selection and undo?`
|
||||||
|
},
|
||||||
|
C: {
|
||||||
|
title: "Buffer + chrome (the synthesis)",
|
||||||
|
keys: `Tab/⇧Tab indent · ⌥↑⌥↓ move · ⌘↑⌘↓ fold · ↑/↓ hop blocks · Enter splits at caret`,
|
||||||
|
diff: `One buffer (ADR-0010 substrate): storage = text, native selection/IME, ⌘Z whole for typing. Fold is <em>view state</em> — hidden lines stay in the buffer (rail untouched; nothing to lose on quit). Enter splits the node at the caret; ↑/↓ hop visible blocks (LogSeq feel); gutter dots fold on click. Ceilings: structural ops reset the browser undo stack (the app routes programmatic edits through shouldChangeText — undo stays whole there); fold identity is parent-text (dupes collapse); ←/→ can step into hidden text at a fold edge.`,
|
||||||
|
q: `Is this the one? B's face, A's substrate, LogSeq's keys — fold safe, split free, arrows hop. Poke the fold-then-⌥↓ case and the Enter split mid-word.`
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let current = new URLSearchParams(location.search).get("variant") || "C";
|
||||||
|
if (!VARIANTS[current]) current = "C";
|
||||||
|
|
||||||
|
function rail(){ document.getElementById("rail").textContent = VARIANTS[current].storage(); }
|
||||||
|
function note(msg){ document.getElementById("railnote").textContent = msg; }
|
||||||
|
|
||||||
|
function show(v){
|
||||||
|
current = v;
|
||||||
|
const url = new URL(location.href); url.searchParams.set("variant", v); history.replaceState(null, "", url);
|
||||||
|
document.getElementById("vkey").textContent = v;
|
||||||
|
document.getElementById("vtitle").textContent = META[v].title;
|
||||||
|
document.getElementById("vname").textContent = `variant ${v} — ${META[v].title}`;
|
||||||
|
document.getElementById("diffhead").textContent = `variant ${v} substrate`;
|
||||||
|
document.getElementById("diffnotes").innerHTML = META[v].diff; // static literal — safe
|
||||||
|
document.getElementById("verdictq").innerHTML = `<em>verdict probe:</em> ${META[v].q}`;
|
||||||
|
document.getElementById("keyrow").innerHTML = META[v].keys.split("·").map(k => `<span class="key">${k.trim()}</span>`).join("");
|
||||||
|
note("");
|
||||||
|
VARIANTS[v].mount(document.getElementById("stage"));
|
||||||
|
}
|
||||||
|
|
||||||
|
const cycle = d => { const i = ORDER.indexOf(current); show(ORDER[(i + d + ORDER.length) % ORDER.length]); };
|
||||||
|
document.getElementById("prev").onclick = () => cycle(-1);
|
||||||
|
document.getElementById("next").onclick = () => cycle(1);
|
||||||
|
document.getElementById("reset").onclick = () => { note("reset"); show(current); };
|
||||||
|
document.addEventListener("keydown", e => {
|
||||||
|
const t = e.target;
|
||||||
|
const editing = t && (t.isContentEditable || t.tagName === "INPUT" || t.tagName === "TEXTAREA");
|
||||||
|
if (!editing && (e.key === "ArrowLeft" || e.key === "ArrowRight")) cycle(e.key === "ArrowRight" ? 1 : -1);
|
||||||
|
});
|
||||||
|
|
||||||
|
show(current);
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
Reference in new issue
Block a user