8000 The Outline in the left margin on wide screens by kiran-brahma · Pull Request #88 · kiran-brahma/writing-tool · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { selectionAnchor as selectionAnchorFor } from "./core/judgeSelection";
import { sectionAt, sections } from "./core/sections";
import { DocumentEditor, type HighlightHit } from "./editor/DocumentEditor";
import { FindingCallout } from "./editor/FindingCallout";
import { MarginOutline } from "./editor/OutlinePanel";
import { StatusLine } from "./editor/StatusLine";
import { calloutFindings } from "./editor/callout";
import { railPresentation } from "./editor/railPresentation";
Expand Down Expand Up @@ -643,6 +644,13 @@ export default function App() {
jumpRequest={jumpRequest}
>
calloutOpen={calloutOpen}
leftMargin={
<MarginOutline
sections={outlineSections}
activeHeadingBlockIndex={activeSection?.headingBlockIndex ?? null}
>
/>
}
pageHeading={
<DocumentTitleField
key={document.id}
Expand Down
28 changes: 21 additions & 7 deletions src/editor/DocumentEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ export interface DocumentEditorProps {
* the prose so it reads as the top of the Document (story 196).
*/
pageHeading?: ReactNode;
/**
* Story 241: what sits in the left margin beside the column, at 1440px and
* wider only. It stays put while the prose scrolls, and it is hidden by the
* stylesheet below that width, never by a script.
*/
leftMargin?: ReactNode;
}

/**
Expand All @@ -92,6 +98,7 @@ export function DocumentEditor({
onHighlightClick,
calloutOpen = false,
pageHeading,
leftMargin,
}: DocumentEditorProps) {
const [, refresh] = useReducer((count: number) => count + 1, 0);
const lastTargetRef = useRef(-1);
Expand Down Expand Up @@ -309,14 +316,21 @@ export function DocumentEditor({
* The gutter is reserved on both edges, so a scrollbar never pulls the
* centred column off the toolbar above it and the Status line below.
*/}
<div
ref={scrollRef}
className="min-h-0 flex-1 overflow-y-auto py-8 [scrollbar-gutter:stable_both-edges]"
>
<div className="obelus-column">
{pageHeading}
<EditorContent editor={editor} />
<div className="relative flex min-h-0 flex-1 flex-col">
<div
ref={scrollRef}
className="min-h-0 flex-1 overflow-y-auto py-8 [scrollbar-gutter:stable_both-edges]"
>
<div className="obelus-column">
{pageHeading}
<EditorContent editor={editor} />
</div>
</div>
{leftMargin !== undefined && (
<div className="obelus-left-margin">
<div className="obelus-left-margin-body">{leftMargin}</div>
</div>
)}
</div>
</div>
);
Expand Down
88 changes: 66 additions & 22 deletions src/editor/OutlinePanel.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Section } from "../core/sections";
import { PANEL_GLOSSES, type HelpSectionId } from "../help/helpContent";
import { outlineEntries } from "./outlineEntries";

/**
* Story 25: the Document outline derived from the Writer's headings. It is the
Expand Down Expand Up @@ -47,30 +48,73 @@ export function OutlinePanel({
</p>
) : (
<ol className="max-h-56 overflow-y-auto py-1">
{sections.map((section) => {
const active = section.headingBlockIndex === activeHeadingBlockIndex;
return (
<li key={`${section.headingBlockIndex}:${section.heading}`}>
<button
type="button"
=> onJump(section.headingBlockIndex)}
title={`Jump to ${section.heading}`}
aria-current={active ? "true" : undefined}
style={{ paddingLeft: `${(section.level - 1) * 12 + 16}px` }}
className={[
"w-full truncate py-1 pr-4 text-left text-sm focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-focus focus-visible:ring-inset",
active
? "bg-sunk-strong font-medium text-ink"
: "text-quiet-ink hover:bg-sunk-strong/60",
].join(" ")}
>
{section.heading}
</button>
</li>
);
})}
{outlineEntries(sections, activeHeadingBlockIndex).map((entry) => (
<li key={`${entry.headingBlockIndex}:${entry.heading}`}>
<button
type="button"
=> onJump(entry.headingBlockIndex)}
title={`Jump to ${entry.heading}`}
aria-current={entry.current ? "true" : undefined}
style={{ paddingLeft: `${(entry.level - 1) * 12 + 16}px` }}
className={[
"w-full truncate py-1 pr-4 text-left text-sm focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-focus focus-visible:ring-inset",
entry.current
? "bg-sunk-strong font-medium text-ink"
: "text-quiet-ink hover:bg-sunk-strong/60",
].join(" ")}
>
{entry.heading}
</button>
</li>
))}
</ol>
)}
</section>
);
}

export interface MarginOutlineProps {
sections: Section[];
/** The heading block the cursor is inside, or null when it is in the preamble. */
activeHeadingBlockIndex: number | null;
onJump: (blockIndex: number) => void;
}

/**
* Stories 241 and 243: the Outline in the left margin at 1440px and wider, in
* the space the capped column leaves empty beside the page. It is a quiet list
* of headings, without the Rail section's chrome, that marks the Section the
* cursor is in and jumps on click exactly as the Rail's Outline does. Both
* render; the stylesheet shows one, so no script decides where the Outline is.
*/
export function MarginOutline({ sections, activeHeadingBlockIndex, onJump }: MarginOutlineProps) {
return (
<nav aria-label="Outline" className="py-8 pr-3 pl-4">
<h2 className="mb-2 px-2 text-xs font-semibold text-muted-ink">Outline</h2>
{sections.length === 0 && (
<p className="px-2 text-xs text-muted-ink">Add a heading to build your outline.</p>
)}
<ol>
{outlineEntries(sections, activeHeadingBlockIndex).map((entry) => (
<li key={`${entry.headingBlockIndex}:${entry.heading}`}>
<button
type="button"
=> onJump(entry.headingBlockIndex)}
title={`Jump to ${entry.heading}`}
aria-current={entry.current ? "true" : undefined}
style={{ paddingLeft: `${(entry.level - 1) * 10 + 8}px` }}
className={[
"block w-full truncate border-l-2 py-0.5 pr-2 text-left text-sm focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-focus",
entry.current
? "border-ink font-medium text-ink"
: "border-transparent text-muted-ink hover:text-quiet-ink",
].join(" ")}
>
{entry.heading}
</button>
</li>
))}
</ol>
</nav>
);
}
19 changes: 12 additions & 7 deletions src/editor/WorkingOrderRail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -668,17 +668,22 @@ export function WorkingOrderRail({
>
<span className="text-base font-semibold">Reference</span>
<span className="text-xs text-muted-ink">
{referenceOpen ? "Hide outline and metrics" : "Show outline and metrics"}
{referenceOpen ? "Hide " : "Show "}
<span className="obelus-rail-outline">outline and </span>metrics
</span>
</button>
{referenceOpen && (
<>
<OutlinePanel
sections={outlineSections}
activeHeadingBlockIndex={activeHeadingBlockIndex}
>
>
/>
{/* Story 242: below 1440px the Outline is here; at 1440px and
wider the stylesheet hides it, as the margin Outline shows. */}
<div className="obelus-rail-outline">
<OutlinePanel
sections={outlineSections}
activeHeadingBlockIndex={activeHeadingBlockIndex}
>
>
/>
</div>
<MetricsPanel
canonical={handle.document?.canonical ?? ""}
tree={handle.document?.tree ?? emptyDocTree()}
Expand Down
59 changes: 59 additions & 0 deletions src/editor/outlineEntries.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { describe, expect, it } from "vitest";
import type { BlockNode, DocTree } from "../core/docTree";
import { sectionAt, sections } from "../core/sections";
import { outlineEntries } from "./outlineEntries";

function doc(...content: BlockNode[]): DocTree {
return { type: "doc", content };
}

function paragraph(text: string): BlockNode {
return { type: "paragraph", content: [{ type: "text", text }] };
}

function heading(level: number, text: string): BlockNode {
return { type: "heading", attrs: { level }, content: [{ type: "text", text }] };
}

const TREE = doc(
paragraph("A preamble."),
heading(1, "Opening"),
paragraph("First body."),
heading(2, "Detail"),
paragraph("Second body."),
heading(1, "Close"),
);

/** The headings marked current with the cursor in the given block, as the shell derives it. */
function markedWithCursorIn(blockIndex: number): string[] {
const current = sectionAt(TREE, blockIndex)?.headingBlockIndex ?? null;
return outlineEntries(sections(TREE), current)
.filter((entry) => entry.current)
.map((entry) => entry.heading);
}

describe("outlineEntries", () => {
it("lists every Section in document order with its level and jump target", () => {
expect(outlineEntries(sections(TREE), null)).toEqual([
{ heading: "Opening", level: 1, headingBlockIndex: 1, current: false },
{ heading: "Detail", level: 2, headingBlockIndex: 3, current: false },
{ heading: "Close", level: 1, headingBlockIndex: 5, current: false },
]);
});

it("marks the one Section the cursor is in, on its heading or in its body", () => {
expect(markedWithCursorIn(1)).toEqual(["Opening"]);
expect(markedWithCursorIn(2)).toEqual(["Opening"]);
expect(markedWithCursorIn(3)).toEqual(["Detail"]);
expect(markedWithCursorIn(4)).toEqual(["Detail"]);
expect(markedWithCursorIn(5)).toEqual(["Close"]);
});

it("marks nothing while the cursor is in the preamble", () => {
expect(markedWithCursorIn(0)).toEqual([]);
});

it("is empty for a Document with no headings", () => {
expect(outlineEntries([], null)).toEqual([]);
});
});
29 changes: 29 additions & 0 deletions src/editor/outlineEntries.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import type { Section } from "../core/sections";

/** One row of the Outline, as either placement draws it. */
export interface OutlineEntry {
heading: string;
level: number;
/** The heading's top-level block index: the row's key, and where a click jumps. */
headingBlockIndex: number;
/** Whether the cursor is in this Section. */
current: boolean;
}

/**
* Story 243: the Outline's rows, with the Section the cursor is in marked. The
* margin Outline and the Rail's Outline both draw from this, so they mark the
* same Section and jump to the same block wherever the Outline is shown.
* `currentHeadingBlockIndex` is null in the preamble, which is no Section.
*/
export function outlineEntries(
sections: readonly Section[],
currentHeadingBlockIndex: number | null,
): OutlineEntry[] {
return sections.map((section) => ({
heading: section.heading,
level: section.level,
headingBlockIndex: section.headingBlockIndex,
current: section.headingBlockIndex === currentHeadingBlockIndex,
}));
}
35 changes: 35 additions & 0 deletions src/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,41 @@
padding-inline: 2rem;
}

/*
* The Outline's placement (stories 241 and 242). Both the margin Outline and
* the Rail's Outline render; this breakpoint alone shows one, so resizing
* across it swaps them without a reload and no script decides. At 1440px and
* wider the Outline sits in the left margin the capped column leaves empty,
* held in view while the prose scrolls; below it, it stays in the Rail. The
* margin clips rather than scrolls, so the sticky list inside it follows
* whichever element scrolls the prose, and a long Outline scrolls on its own.
*/
.obelus-left-margin {
display: none;
}

@media (min-width: 1440px) {
.obelus-left-margin {
display: block;
position: absolute;
inset-block: 0;
left: 0;
width: calc((100% - 37rem - 4rem) / 2);
overflow: clip;
}

.obelus-left-margin-body {
position: sticky;
top: 0;
max-height: 100vh;
overflow-y: auto;
}

.obelus-rail-outline {
display: none;
}
}

/*
* The rich text surface. Tailwind's preflight resets list and heading styling,
* so the editor restates what a Writer expects from a document. These are the
Expand Down
0