1
0
Fork 0
polaris-task-force/src/components/frontend/wiki/WikiEditorHighlightLayer.tsx
Z8MB1E 55a03e8094 feat(wiki): add editor undo/redo, template dialog, highlighting, and formatting
- useEditorHistory: undo/redo stack over the textarea body
- WikiTemplateDialog: insert {{Template}} transclusions from the
  toolbar, with per-template {{param}} placeholder prompts
- WikiEditorHighlightLayer: mirrored overlay highlighting headings,
  directives, and template placeholders over the textarea
- editorHighlight.ts: tokenizer driving the overlay
- formatMarkdown.ts + createFormatUpdate: auto-format the document
- editorFormatting.ts: Tab/Shift+Tab indent/outdent, list
  continuation on Enter, inline bold/italic/code formatting, infobox
  snippet
- WikiEditor: editor and live preview side by side, preview renders
  template CSS/HTML via WikiPreparedBody
2026-09-06 05:32:51 -04:00

49 lines
1.6 KiB
TypeScript

"use client";
import { forwardRef, useMemo, type HTMLAttributes, type ReactNode } from "react";
import { cn } from "@/lib/utils";
import { highlightMarkdownLines } from "@/lib/wiki/editorHighlight";
interface WikiEditorHighlightLayerProps extends HTMLAttributes<HTMLDivElement> {
readonly body: string;
}
/**
* Colored text layer rendered underneath the editor textarea (which uses
* transparent text). Sizing must mirror the textarea exactly: same padding,
* font, and a transparent border so text starts at the same 1px + padding
* offset inside the shared wrapper's border.
*/
export const WikiEditorHighlightLayer = forwardRef<
HTMLDivElement,
WikiEditorHighlightLayerProps
>(function WikiEditorHighlightLayer({ body, className, ...rest }, ref) {
const lines = useMemo(() => highlightMarkdownLines(body), [body]);
const nodes: ReactNode[] = [];
lines.forEach((segments, lineIndex) => {
if (lineIndex > 0) nodes.push("\n");
segments.forEach((segment, segmentIndex) => {
if (segment.className) {
nodes.push(
<span key={`${lineIndex}-${segmentIndex}`} className={segment.className}>
{segment.text}
</span>,
);
} else {
nodes.push(segment.text);
}
});
});
return (
<div
ref={ref}
aria-hidden="true"
className={cn("pointer-events-none absolute inset-0 overflow-hidden", className)}
{...rest}
>
<div className="whitespace-pre-wrap break-words border border-transparent px-3 py-2 font-mono text-sm text-foreground/90">
{nodes}
</div>
</div>
);
});