From 9098bfb63a61e12130088b1c2148ad258ef767b9 Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Mon, 29 Dec 2025 21:26:52 +0200 Subject: [PATCH] chore(client): prototype implementation to communicate data through note context --- CONTEXT_DATA_EXAMPLE.md | 194 +++++++++++++++++++++ apps/client/src/components/app_context.ts | 5 + apps/client/src/components/note_context.ts | 64 +++++++ apps/client/src/widgets/react/hooks.tsx | 110 ++++++++++++ 4 files changed, 373 insertions(+) create mode 100644 CONTEXT_DATA_EXAMPLE.md diff --git a/CONTEXT_DATA_EXAMPLE.md b/CONTEXT_DATA_EXAMPLE.md new file mode 100644 index 000000000..ecd383eac --- /dev/null +++ b/CONTEXT_DATA_EXAMPLE.md @@ -0,0 +1,194 @@ +# Context Data Pattern - Usage Examples + +This document shows how to use the new context data pattern to communicate between type widgets (inside splits) and shared components (sidebars, toolbars). + +## Architecture + +Data is stored directly on the `NoteContext` object: +- **Type widgets** (PDF, Text, Code) publish data using `useSetContextData()` +- **Sidebar/toolbar components** read data using `useGetContextData()` +- Data is automatically cleared when switching notes +- Components automatically re-render when data changes + +## Example 1: PDF Page Navigation + +### Publishing from PDF Viewer (inside split) + +```tsx +// In Pdf.tsx +import { useSetContextData } from "../../react/hooks"; + +interface PdfPageInfo { + pageNumber: number; + title: string; +} + +export default function PdfPreview({ note, blob }) { + const { noteContext } = useActiveNoteContext(); + const [pages, setPages] = useState([]); + + useEffect(() => { + // When PDF loads, extract page information + const pageInfo = extractPagesFromPdf(); + setPages(pageInfo); + }, [blob]); + + // Publish pages to context data + useSetContextData(noteContext, "pdfPages", pages); + + return