FastComments.com

Add Comments to Your SolidJS App

這是 FastComments 的官方 SolidJS 函式庫。

在你的 SolidJS 應用中嵌入即時評論、聊天及審閱小工具。

儲存庫

在 GitHub 上檢視


安裝 Internal Link

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 不會自動追蹤任意物件的深層變更,因此在第一次渲染後的 config 變更必須被明確推送。每個 widget 都接受一個會回傳 handle 的 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() 在任何時候呼叫都是安全的:

  • 在 script 載入之前:partial 會被暫存並在初始化時套用。
  • 在非同步初始化期間 (reviews-summary, user-activity-feed):partial 會排入佇列,並在 callback 解決時套用。
  • 在初始化之後:它會直接轉發到 live widget 的 .update() 方法。

命令式 handle API

interface WidgetHandle<Config> {
  getInstance: () => WidgetInstance | null;   // 最新的運行中實例(或在掛載前為 null)
  onInstance: (cb: (instance: WidgetInstance) => void) => void; // 在實例就緒時觸發一次
  update: (partial: Partial<Config>) => void; // 合併並推送 config
}

對於 .update() 無法涵蓋的命令式操作,請使用 getInstance(),例如 openProfile:

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

元件 Internal Link


來自 fastcomments-react 的每個元件都以相同的名稱提供:

Component Handle type Embed loaded
FastCommentsCommentWidget FastCommentsCommentWidgetHandle 旗艦即時留言元件
FastCommentsCommentCountWidget FastCommentsCommentCountWidgetHandle 內嵌評論計數徽章
FastCommentsLiveChatWidget FastCommentsLiveChatWidgetHandle 串流式即時聊天元件
FastCommentsCollabChatWidget FastCommentsCollabChatWidgetHandle 以文字為錨點的協作聊天
FastCommentsImageChatWidget FastCommentsImageChatWidgetHandle 基於區域的圖片評論
FastCommentsRecentCommentsWidget FastCommentsRecentCommentsWidgetHandle 近期評論動態
FastCommentsRecentDiscussionsWidget FastCommentsRecentDiscussionsWidgetHandle 近期討論動態
FastCommentsReviewsSummaryWidget FastCommentsReviewsSummaryWidgetHandle 星級評分摘要
FastCommentsTopPagesWidget FastCommentsTopPagesWidgetHandle 最多評論頁面排行榜
FastCommentsUserActivityFeedWidget FastCommentsUserActivityFeedWidgetHandle 每位使用者的活動時間線

附加到現有元素的元件

FastCommentsCollabChatWidget and FastCommentsImageChatWidget mount into a caller-supplied element. Pass a targetRef accessor that returns the element once mounted:

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" 以透過 EU 叢集引導元件流量。

展示 Internal Link

完整的展示應用位於 examples/example-showcase/。 它鏡像 React 展示並涵蓋每一個 元件以及常見流程(深色模式、分頁、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 儲存庫 以取得貢獻指南。