FastComments.com

向您的 SolidJS 应用添加评论

这是 FastComments 的官方 SolidJS 库。

在你的 SolidJS 应用中嵌入实时评论、聊天和评审小部件。

仓库

在 GitHub 上查看


安装 Internal Link

npm

npm install fastcomments-solidjs

用法 Internal Link

import { FastCommentsCommentWidget } from 'fastcomments-solidjs';

export default function App() {
  return <FastCommentsCommentWidget tenantId="demo" urlId="some-page-id" />;
}

响应配置更改(命令式句柄) Internal Link

Solid 不会自动跟踪任意对象的深层变更,因此在首次渲染之后的配置更改必须显式推送。每个组件都接受返回一个句柄的 apiRef;在 createEffect 中调用 handle.update(partial) 以驱动响应性:

import { createEffect, createSignal } from 'solid-js';
import { FastCommentsCommentWidget, type FastCommentsCommentWidgetHandle } from 'fastcomments-solidjs';

export default function Paginated() {
  const [page, setPage] = createSignal(0);
  let handle: FastCommentsCommentWidgetHandle | undefined;
  createEffect(() => handle?.update({ urlId: `product-${page()}` }));

  return (
    <>
      <button onClick={() => setPage(page() + 1)}>next</button>
      <FastCommentsCommentWidget
        apiRef={(h) => (handle = h)}
        tenantId="demo"
        urlId={`product-${page()}`}
      />
    </>
  );
}

update() 在任何时候调用都是安全的:

  • 在脚本加载之前:partial 会被暂存并在初始化时应用。
  • 在异步初始化期间(reviews-summary、user-activity-feed):partial 会被排队并在回调解析时应用。
  • 初始化之后:它会直接转发到实时组件的 .update() 方法。

命令式句柄 API

interface WidgetHandle<Config> {
  getInstance: () => WidgetInstance | null;   // 最新的活动实例(或挂载前为 null)
  onInstance: (cb: (instance: WidgetInstance) => void) => void; // 实例准备好时触发一次
  update: (partial: Partial<Config>) => void; // 合并并推送配置
}

对于 .update() 未覆盖的命令式操作,例如 openProfile,请使用 getInstance()

const openProfile = () =>
  (handle?.getInstance() as { openProfile?: (o: { userId: string }) => void } | null)
    ?.openProfile?.({ userId: 'demo' });

组件 Internal Link

来自 fastcomments-react 的每个小部件都可使用相同的名称:

组件Handle 类型加载的嵌入
FastCommentsCommentWidgetFastCommentsCommentWidgetHandle旗舰实时评论小部件
FastCommentsCommentCountWidgetFastCommentsCommentCountWidgetHandle内联评论计数徽章
FastCommentsLiveChatWidgetFastCommentsLiveChatWidgetHandle流式实时聊天小部件
FastCommentsCollabChatWidgetFastCommentsCollabChatWidgetHandle基于文本锚点的协作聊天
FastCommentsImageChatWidgetFastCommentsImageChatWidgetHandle基于区域的图像评论
FastCommentsRecentCommentsWidgetFastCommentsRecentCommentsWidgetHandle最新评论提要
FastCommentsRecentDiscussionsWidgetFastCommentsRecentDiscussionsWidgetHandle最近讨论提要
FastCommentsReviewsSummaryWidgetFastCommentsReviewsSummaryWidgetHandle星级评分汇总
FastCommentsTopPagesWidgetFastCommentsTopPagesWidgetHandle评论最多页面排行榜
FastCommentsUserActivityFeedWidgetFastCommentsUserActivityFeedWidgetHandle每用户的活动时间线

挂载到现有元素的组件

FastCommentsCollabChatWidgetFastCommentsImageChatWidget 会挂载到调用者提供的元素。传入一个 targetRef 访问器,该访问器在元素挂载后返回该元素:

import { FastCommentsImageChatWidget } from 'fastcomments-solidjs';

export default function ImageChat() {
  let img: HTMLImageElement | undefined;
  return (
    <>
      <img ref={img} src="/screenshot.png" alt="" />
      <FastCommentsImageChatWidget
        tenantId="demo"
        urlId="my-image"
        targetRef={() => img}
      />
    </>
  );
}

区域

传入 region="eu" 以将小部件流量通过欧盟集群路由。

展示 Internal Link

完整的展示应用位于 examples/example-showcase/。它镜像 React showcase 并涵盖每个 小部件以及常见流程(暗色模式、分页、SSO、回调)。

cd examples/example-showcase
npm install
npm run dev

构建 Internal Link

npm install
npm run build       # 库 -> dist/
npm test            # vitest 冒烟测试
npm run build:demo  # 展示 -> demo-dist/

需要帮助?

如果您在使用 SolidJS 库时遇到任何问题或有疑问,请:

贡献

欢迎贡献!请访问 GitHub 仓库 查看贡献指南。