g-comments 0.2.0 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/react.js CHANGED
@@ -1,5 +1,5 @@
1
1
  "use client";
2
- import { createTranslator, PACKAGE_NAME, currentUrlWithoutHash, findComment, createComposerTimer, normalizeForum, resolveLocale, clientRegistry, STORAGE_PREFIX, isCommentsError, CommentsError, encodeRef, defaultPageUrl } from './chunk-WACVXJHD.js';
2
+ import { createTranslator, PACKAGE_NAME, currentUrlWithoutHash, findComment, findByNumber, createComposerTimer, normalizeForum, resolveLocale, clientRegistry, STORAGE_PREFIX, isCommentsError, CommentsError, encodeRef, defaultPageUrl } from './chunk-EWWBVJ2B.js';
3
3
  import { createContext, useState, useRef, useCallback, useEffect, useMemo, Fragment as Fragment$1, useId, useContext, useLayoutEffect, useSyncExternalStore } from 'react';
4
4
  import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
5
5
 
@@ -351,12 +351,15 @@ function forumThemeCss(theme) {
351
351
  }
352
352
  function forumFontFacesCss(theme) {
353
353
  let css = "";
354
+ const seen = /* @__PURE__ */ new Set();
354
355
  for (const font of Object.values(theme.fonts)) {
355
356
  for (const face of font?.faces ?? []) {
356
357
  const weight = String(face.weight);
357
- if (SAFE_FAMILY.test(face.family) && FONT_SOURCE.test(face.src) && FONT_WEIGHT.test(weight)) {
358
- css += `@font-face{font-family:"${face.family}";src:${face.src};font-weight:${weight};font-display:swap;}
358
+ const rule = `@font-face{font-family:"${face.family}";src:${face.src};font-weight:${weight};font-display:swap;}
359
359
  `;
360
+ if (SAFE_FAMILY.test(face.family) && FONT_SOURCE.test(face.src) && FONT_WEIGHT.test(weight) && !seen.has(rule)) {
361
+ seen.add(rule);
362
+ css += rule;
360
363
  }
361
364
  }
362
365
  }
@@ -378,6 +381,14 @@ function forumStylesheetUrls(theme, forum) {
378
381
  }
379
382
  return urls;
380
383
  }
384
+ function forumThemeStyleText(theme, forum) {
385
+ const imports = forumStylesheetUrls(theme, forum).map((href) => `@import url(${JSON.stringify(href)}) layer(gc-forum);
386
+ `).join("");
387
+ return `@layer gc-defaults, gc-forum;
388
+ ${imports}@layer gc-forum{
389
+ ${forumThemeCss(theme)}}
390
+ `;
391
+ }
381
392
  function documentOf(root) {
382
393
  return root.nodeType === 9 ? root : root.ownerDocument;
383
394
  }
@@ -402,15 +413,8 @@ function applyForumTheme(root, theme, forum) {
402
413
  replaceNodes(container, STYLE_ATTRIBUTE, forum, key, () => {
403
414
  const style = doc.createElement("style");
404
415
  style.setAttribute(STYLE_ATTRIBUTE, key);
405
- style.textContent = forumThemeCss(theme);
406
- const links = forumStylesheetUrls(theme, forum).map((href) => {
407
- const link = doc.createElement("link");
408
- link.rel = "stylesheet";
409
- link.href = href;
410
- link.setAttribute(STYLE_ATTRIBUTE, key);
411
- return link;
412
- });
413
- return [style, ...links];
416
+ style.textContent = forumThemeStyleText(theme, forum);
417
+ return [style];
414
418
  });
415
419
  const faces = forumFontFacesCss(theme);
416
420
  if (faces) {
@@ -1655,7 +1659,7 @@ function UserAvatar({
1655
1659
  return /* @__PURE__ */ jsx("span", { className: cx("gc-avatar", "gc-avatar--placeholder", className), "aria-hidden": "true", children: initial3 });
1656
1660
  }
1657
1661
  function Meta({ className }) {
1658
- const { comment } = useItemContext("Comments.Meta");
1662
+ const { comment, depth } = useItemContext("Comments.Meta");
1659
1663
  const { t, locale, client, elements } = useRootContext("Comments.Meta");
1660
1664
  const author = comment.author;
1661
1665
  const name = author ? author.name || author.username : null;
@@ -1676,9 +1680,31 @@ function Meta({ className }) {
1676
1680
  t("pinned")
1677
1681
  ] }),
1678
1682
  /* @__PURE__ */ jsx("a", { className: "gc-comment__time", href: `#gc-${comment.id}`, children: /* @__PURE__ */ jsx(RelativeTime, { iso: comment.created_at, locale }) }),
1679
- comment.edited && /* @__PURE__ */ jsx("span", { className: "gc-comment__edited", title: formatAbsolute(comment.updated_at, locale), children: t("edited") })
1683
+ comment.edited && /* @__PURE__ */ jsx("span", { className: "gc-comment__edited", title: formatAbsolute(comment.updated_at, locale), children: t("edited") }),
1684
+ comment.reply_to && depth === 0 && /* @__PURE__ */ jsx(ReplyReference, { reference: comment.reply_to })
1680
1685
  ] });
1681
1686
  }
1687
+ function ReplyReference({ reference }) {
1688
+ const { t, snapshot, focusComment } = useRootContext("Comments.Meta");
1689
+ const label = t("replyingTo", { username: reference.username ?? "" });
1690
+ const parent = snapshot ? findByNumber(snapshot.comments, reference.number) : null;
1691
+ if (!parent) {
1692
+ return /* @__PURE__ */ jsx("span", { className: "gc-comment__reply-to", children: label });
1693
+ }
1694
+ const id = parent.node.id;
1695
+ return /* @__PURE__ */ jsx(
1696
+ "a",
1697
+ {
1698
+ className: "gc-comment__reply-to",
1699
+ href: `#gc-${id}`,
1700
+ onClick: (event) => {
1701
+ event.preventDefault();
1702
+ focusComment(id, { scroll: true });
1703
+ },
1704
+ children: label
1705
+ }
1706
+ );
1707
+ }
1682
1708
  function RelativeTime({ iso, locale }) {
1683
1709
  const [now, setNow] = useState(Date.now);
1684
1710
  useEffect(() => onMinuteTick(() => setNow(Date.now())), []);