SPB Git

Repository Public

frame.tsx 6.6 KB 106 lines tsx

1/**2 * =============================================================================3 *                  SP BOUCHER | SOFTWARE & RESEARCH4 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project          : Note SPBoucher AI8 * File             : src/components/editor/nodes/frame.tsx9 * Module           : components10 * Repository       : https://git.spboucher.ai/note-spboucher-ai11 * Project Website  : https://note.spboucher.ai12 * Created          : 2026-11-1913 * Last Modified    : 2026-11-1914 *15 * Author           : Simon-Pierre Boucher16 * Position         : Professor of Finance17 * Institution      : Université du Québec en Outaouais (UQO)18 * Organization     : SP Boucher19 * Website          : https://spboucher.ai20 * Git              : https://git.spboucher.ai21 *22 * Preferred Contact: contact@spboucher.ai23 * Development      : dev@spboucher.ai24 * Support          : support@spboucher.ai25 * Security/Admin   : admin@spboucher.ai26 *27 * Copyright (c) 202628 * Simon-Pierre Boucher29 * All rights reserved where applicable. This file is governed by the30 * applicable project license, copyright notices and relevant law.31 * Existing third-party rights and notices remain unaffected.32 *33 * License          : None specified (no license file in this repository)34 *35 * Do not embed credentials, private keys, access tokens,36 * certificates or confidential personal information.37 *38 * =============================================================================39 * END OF SP BOUCHER SOURCE HEADER40 * =============================================================================41 */4243"use client";44/**45 * file: src/components/editor/nodes/frame.tsx46 * description: shared chrome and contract of the Next Generation node views. Every new block is a leaf atom rendered by a React47 *   component that receives `LeafViewProps` and wraps its content in `<NodeFrame>` (card, header, actions, selection ring). The frame48 *   never widens the page: bodies scroll inside themselves. `data-nv-stop` marks the area where ProseMirror must leave pointer, key and49 *   clipboard events to the block (canvases, text layers, grids).50 * author: Simon-Pierre Boucher — contact@spboucher.ai51 */52import * as React from "react";53import { NodeViewWrapper } from "@tiptap/react";54import type { Editor } from "@tiptap/core";55import { cn } from "@/lib/utils";5657export interface LeafViewProps<A> {58  /** The node's attrs exactly as stored (plus the block `id`). Treat as immutable. */59  attrs: A & { id?: string | null };60  /** Merge a patch into the attrs (one undoable transaction). No-op when the editor is read-only. */61  update: (patch: Partial<A>) => void;62  editable: boolean;63  /** True while the block is the ProseMirror node selection. */64  selected: boolean;65  /** Page being edited (attachments are uploaded against it); null in previews. */66  pageId: string | null;67  editor: Editor;68  getPos: () => number | undefined;69  deleteNode: () => void;70}7172export function NodeFrame({ kind, icon, label, title, actions, children, selected, tone = "neutral", bare, className, bodyClassName, stop = true }: {73  /** node type name — becomes data-node (tests and CSS hook) */ kind: string; icon?: React.ReactNode; /** small uppercase type label, e.g. "Diagram" */ label: string; /** optional title next to the label */ title?: React.ReactNode;74  /** buttons on the right of the header (use `NodeButton`) */ actions?: React.ReactNode; children?: React.ReactNode; selected?: boolean; tone?: "neutral" | "ai" | "accent"; /** no card, no header (inline-looking blocks) */ bare?: boolean; className?: string; bodyClassName?: string;75  /** false = let ProseMirror handle events in the body too (purely presentational bodies) */ stop?: boolean;76}) {77  return (78    <NodeViewWrapper as="div" className={cn("nb", bare && "nb-bare", className)} data-node={kind} data-tone={tone} data-selected={selected ? "true" : undefined}>79      {!bare && (80        <div className="nb-head" contentEditable={false}>81          <span className="nb-icon" aria-hidden>{icon}</span><span className="nb-label">{label}</span>82          {title != null && title !== "" && <span className="nb-title">{title}</span>}83          {actions && <span className="nb-actions">{actions}</span>}84        </div>85      )}86      <div className={cn("nb-body", bodyClassName)} contentEditable={false} {...(stop ? { "data-nv-stop": "" } : {})}>{children}</div>87    </NodeViewWrapper>88  );89}90/** Header / toolbar button of a node view (32 px, 44 px on touch). Always give `label` (tooltip + accessible name). */91export function NodeButton({ label, onClick, children, active, disabled, tone, className, text }: { label: string; onClick?: (e: React.MouseEvent) => void; children?: React.ReactNode; active?: boolean; disabled?: boolean; tone?: "ai" | "danger" | "accent"; className?: string; /** show `label` as visible text after the icon */ text?: boolean }) {92  return <button type="button" title={label} aria-label={label} aria-pressed={active} disabled={disabled} onMouseDown={(e) => e.preventDefault()} onClick={onClick} data-tone={tone} className={cn("nb-btn", text && "nb-btn-text", active && "is-active", className)}>{children}{text && <span>{label}</span>}</button>;93}94/** Neutral empty / unavailable / error state inside a block. */95export function NodeState({ icon, title, hint, children, tone = "neutral" }: { icon?: React.ReactNode; title: string; hint?: React.ReactNode; children?: React.ReactNode; tone?: "neutral" | "danger" }) {96  return <div className="nb-state" data-tone={tone}>{icon && <span className="nb-state-icon" aria-hidden>{icon}</span>}<div className="min-w-0"><div className="nb-state-title">{title}</div>{hint && <div className="nb-state-hint">{hint}</div>}</div>{children && <div className="nb-state-actions">{children}</div>}</div>;97}98/** Debounced callback that is flushed on unmount (attrs edited in a textarea are written a moment after typing stops, never per key). */99export function useDebounced<T extends unknown[]>(fn: (...a: T) => void, ms: number) {100  const ref = React.useRef(fn); ref.current = fn; const t = React.useRef<ReturnType<typeof setTimeout> | null>(null); const last = React.useRef<T | null>(null);101  const flush = React.useCallback(() => { if (t.current) { clearTimeout(t.current); t.current = null; } if (last.current) { const a = last.current; last.current = null; ref.current(...a); } }, []);102  React.useEffect(() => flush, [flush]);103  const call = React.useCallback((...a: T) => { last.current = a; if (t.current) clearTimeout(t.current); t.current = setTimeout(flush, ms); }, [flush, ms]);104  return Object.assign(call, { flush });105}106
Files main
Menu

Theme