File size: 5,782 Bytes
7814104 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 | // ---------------------------------------------------------------------------
// HfUser NodeView
//
// WYSIWYG rendition of the Hugging Face user card, mirroring the DOM
// emitted by `app/src/components/HfUser.astro` in the upstream
// research-article-template so what you see in the editor matches
// what the publisher writes.
//
// Why a dedicated view (vs. the generic AtomicView):
// The generic placeholder shows a dashed card with form fields,
// which made authors guess what the final block looks like. The
// upstream component is small and visual (avatar + name + handle),
// so it's worth reproducing inline and revealing the form only
// when the node is selected (or when the username is still empty,
// right after insertion).
//
// Anchors:
// We use <span> instead of <a> inside the editor so accidental
// clicks don't navigate away from the document. The publisher
// transformer (publisher/transformers/hf-user.ts) re-renders the
// same DOM with real <a href> tags for the static HTML output.
// ---------------------------------------------------------------------------
import { useCallback } from "react";
import { NodeViewWrapper } from "@tiptap/react";
import type { NodeViewProps } from "@tiptap/react";
function FieldRow({
label,
value,
placeholder,
onChange,
}: {
label: string;
value: string;
placeholder?: string;
onChange: (val: string) => void;
}) {
return (
<div style={{ display: "flex", alignItems: "center", gap: 8 }}>
<span
style={{
fontSize: 12,
color: "var(--muted-color)",
minWidth: 90,
}}
>
{label}
</span>
<input
type="text"
value={value}
placeholder={placeholder ?? label}
onChange={(e) => onChange(e.target.value)}
style={{
flex: 1,
minWidth: 0,
padding: "4px 8px",
background: "var(--page-bg)",
border: "1px solid var(--border-color)",
borderRadius: 4,
color: "var(--text-color)",
fontSize: 13,
outline: "none",
}}
/>
</div>
);
}
export function HfUserNodeView({
node,
updateAttributes,
selected,
}: NodeViewProps) {
const username = String(node.attrs.username || "").trim();
const name = String(node.attrs.name || "").trim();
const url = String(node.attrs.url || "").trim();
const displayName = name || username || "username";
const avatarSrc = username
? `https://huggingface.co/api/users/${encodeURIComponent(username)}/avatar`
: "";
const urlPlaceholder = username
? `https://huggingface.co/${username}`
: "https://huggingface.co/…";
const handleAttr = useCallback(
(key: string) => (value: string) => updateAttributes({ [key]: value }),
[updateAttributes],
);
// Show the inline editor on selection or while the card is empty
// (right after insertion, prompting the author to fill at least
// the username).
const showEditor = selected || !username;
return (
<NodeViewWrapper data-component="hfUser">
<div contentEditable={false} style={{ margin: "0.75em 0" }}>
<div
className="hf-user"
style={
selected
? {
outline: "2px solid var(--primary-color)",
outlineOffset: 2,
borderRadius: 12,
}
: undefined
}
>
<div className="hf-user__left">
{avatarSrc ? (
<img
className="hf-user__avatar"
src={avatarSrc}
alt={`${displayName} avatar`}
width={44}
height={44}
loading="lazy"
decoding="async"
referrerPolicy="no-referrer"
/>
) : (
<div
className="hf-user__avatar"
aria-hidden="true"
style={{
display: "flex",
alignItems: "center",
justifyContent: "center",
background: "var(--surface-bg)",
color: "var(--muted-color)",
fontSize: 18,
fontWeight: 700,
}}
>
?
</div>
)}
<span className="hf-user__text">
<span className="hf-user__name">{displayName}</span>
<span className="hf-user__row">
<span className="hf-user__username">
@{username || "username"}
</span>
</span>
</span>
</div>
</div>
{showEditor && (
<div
style={{
display: "flex",
flexDirection: "column",
gap: 6,
marginTop: 8,
padding: "10px 12px",
border: "1px dashed var(--border-color)",
borderRadius: 8,
background: "var(--surface-bg)",
maxWidth: 360,
}}
>
<FieldRow
label="Username"
value={username}
placeholder="username"
onChange={handleAttr("username")}
/>
<FieldRow
label="Display name"
value={name}
placeholder="Full Name"
onChange={handleAttr("name")}
/>
<FieldRow
label="URL"
value={url}
placeholder={urlPlaceholder}
onChange={handleAttr("url")}
/>
</div>
)}
</div>
</NodeViewWrapper>
);
}
HfUserNodeView.displayName = "HfUserView";
|