132 lines
5.1 KiB
TypeScript
132 lines
5.1 KiB
TypeScript
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
// Copyright (C) 2026 ZiroEDA and contributors.
|
|
// Portions derived from KiCad, copyright The KiCad Developers. See NOTICE.md.
|
|
/**
|
|
* `HTML_MESSAGE_BOX` (`common/dialogs/html_message_box.cpp`) — the box KiCad
|
|
* puts up when a batch of work has things to say afterwards. GerbView's file
|
|
* loader is one of many callers:
|
|
*
|
|
* HTML_MESSAGE_BOX mbox( this, _( "Errors" ) );
|
|
* mbox.ListSet( reporter.GetMessages() );
|
|
* mbox.ShowModal(); gerbview/files.cpp:417-420
|
|
*
|
|
* `ListSet` is the whole of the layout — a `<ul>` with one `<li>` per message
|
|
* (`html_message_box.cpp:138-152`) — and the messages arrive carrying their own
|
|
* `<b>` and `<i>`, which is where the bold heading and italic filename in a
|
|
* live Errors box come from. So this renders a list and lets each message's
|
|
* own markup through; it does not decide either.
|
|
*
|
|
* Shared rather than GerbView-local because upstream's is in `common/` and has
|
|
* dozens of callers.
|
|
*
|
|
* ## Two entry points, as upstream has two
|
|
*
|
|
* `ListSet` is one, and `AddHTML_Text( html )` is the other
|
|
* (`html_message_box.cpp`), which takes markup already built and appends it
|
|
* whole. `PCB_TEXT::ShowSyntaxHelp` is that second caller: it converts
|
|
* `pcb_text_help_md.h` and hands the result over. So this takes `messages` OR
|
|
* `html`, and the difference between them is not cosmetic —
|
|
*
|
|
* - a `messages` item is a REPORT, built from a board's own contents (a file
|
|
* name, a footprint's reference), so its markup is filtered down to the
|
|
* three tags upstream's callers emit;
|
|
* - `html` is a constant compiled into the program, so it is rendered as
|
|
* markup.
|
|
*
|
|
* Handing a report to the `html` path would be handing a board's contents to a
|
|
* renderer, which is the injection `htmlMessageParts` exists to prevent. The
|
|
* prop names are what keep the two apart at every call site.
|
|
*/
|
|
|
|
import { useEffect, useRef, type JSX } from 'react';
|
|
import { DialogShim } from '../dialog_shim.js';
|
|
import { Button } from '@ziroeda/wx/controls.js';
|
|
|
|
/**
|
|
* One message's markup, as `<li>` content.
|
|
*
|
|
* The only tags upstream's callers emit are `<b>`, `<i>` and `<br>`, so those
|
|
* three are rendered and anything else is shown as text. That is deliberate:
|
|
* a message can carry a filename a person chose, and handing arbitrary markup
|
|
* to a renderer because it arrived in an error string is how a filename
|
|
* becomes an injection.
|
|
*/
|
|
export function htmlMessageParts(message: string): JSX.Element[] {
|
|
const out: JSX.Element[] = [];
|
|
const re = /<(b|i)>([\s\S]*?)<\/\1>|<br\s*\/?>/gi;
|
|
let last = 0;
|
|
let m: RegExpExecArray | null = re.exec(message);
|
|
let key = 0;
|
|
const text = (s: string): void => {
|
|
if (s !== '') out.push(<span key={key++}>{s}</span>);
|
|
};
|
|
while (m !== null) {
|
|
text(message.slice(last, m.index));
|
|
if (m[1] === undefined) out.push(<br key={key++} />);
|
|
else if (m[1].toLowerCase() === 'b') out.push(<b key={key++}>{m[2]}</b>);
|
|
else out.push(<i key={key++}>{m[2]}</i>);
|
|
last = m.index + m[0].length;
|
|
m = re.exec(message);
|
|
}
|
|
text(message.slice(last));
|
|
return out;
|
|
}
|
|
|
|
export function HtmlMessageBox({
|
|
caption,
|
|
messages,
|
|
html,
|
|
className,
|
|
onClose,
|
|
}: {
|
|
/** The dialog title — `_( "Errors" )` for the file loader. */
|
|
caption: string;
|
|
/** `ListSet`: `reporter.GetMessages()`, one per `<li>`. */
|
|
messages?: readonly string[];
|
|
/**
|
|
* `AddHTML_Text( html )`: markup the CALLER built, appended whole.
|
|
*
|
|
* Only ever a constant of ours — see this file's header for why that
|
|
* distinction is load-bearing.
|
|
*/
|
|
html?: string;
|
|
/** A caller that states its own size, as `SetDialogSizeInDU` does. */
|
|
className?: string;
|
|
onClose: () => void;
|
|
}): JSX.Element {
|
|
// wxID_OK is the only button and has the focus, so Enter closes.
|
|
const okRef = useRef<HTMLButtonElement>(null);
|
|
|
|
return (
|
|
<DialogShim
|
|
title={caption}
|
|
onClose={onClose}
|
|
initialFocus={okRef}
|
|
className={`ze-htmlmsg${className ? ` ${className}` : ''}`}
|
|
>
|
|
<div className="ze-htmlmsg-body">
|
|
{html !== undefined ? (
|
|
// `AddHTML_Text`. The markup is ours; a report never reaches here.
|
|
// biome-ignore lint/security/noDangerouslySetInnerHtml: a compiled-in constant, never a report
|
|
<div dangerouslySetInnerHTML={{ __html: html }} />
|
|
) : (
|
|
<ul>
|
|
{(messages ?? []).map((msg, i) => (
|
|
// The messages are a report in order, and two files can fail
|
|
// the same way with the same text, so the index is the identity.
|
|
// biome-ignore lint/suspicious/noArrayIndexKey: report order IS the identity
|
|
<li key={i}>{htmlMessageParts(msg)}</li>
|
|
))}
|
|
</ul>
|
|
)}
|
|
</div>
|
|
{/* `m_sdbSizer1`, a wxStdDialogButtonSizer holding the one wxID_OK
|
|
(dialog_display_html_text_base.cpp:23-28): the shared right-aligned
|
|
footer, not the native message box's full-width row. */}
|
|
<div className="ze-modal-footer">
|
|
<Button label="OK" isDefault buttonRef={okRef} onClick={onClose} />
|
|
</div>
|
|
</DialogShim>
|
|
);
|
|
}
|