FastComments.com

Extensions の開発

コンテキスト

FastComments は、Extensions と呼ぶスクリプトを介してコア機能を拡張する機能を提供します。

An Extension はコメントウィジェットに追加のマークアップを加えたり、イベントリスナーを設定したり、任意のコードを実行したりできます。

ここでは、本番で使用している Extensions の例と、Extensions の書き方に関するドキュメントを紹介します。


拡張機能のライフサイクル Internal Link

各拡張機能のスクリプトは、コメントウィジェットが最初のコメントセットの取得とUIのレンダリングを開始する前にフェッチされ、実行されます。

初回読み込み時に、次のデータが extension オブジェクトに付与されます:

  • config - config オブジェクトへの参照。
  • translations - translations オブジェクトへの参照。
  • commentsById - IDごとの全コメントへの参照。
  • root - ルートDOMノードへの参照。

拡張機能は、コメントウィジェットが適切なタイミングで呼び出す関数を必要に応じてオーバーライドしてください。

拡張機能の定義 Internal Link

作成できる最小の拡張機能は次のとおりです:

シンプルな拡張
Copy CopyRun External Link
1
2(function () {
3 const extension = FastCommentsUI.extensions.find((extension) => {
4 return extension.id === 'my-extension';
5 });
6})();
7

この例のために、これを my-extension.js として保存し、https://example.com/my-extension.min.js で公開してください。

この拡張機能は何もしません。読み込み時にコアのコメントライブラリによって作成された拡張オブジェクトを取得するだけです。

この Extension オブジェクトはシングルトンで、他の拡張機能とは共有されません。

次に、拡張機能を読み込むには、コメントウィジェットにそのことを伝える必要があります。例えば:

Using a Custom Extension
Copy CopyRun External Link
1
2<script async src="https://cdn.fastcomments.com/js/embed-v2-async.min.js"></script>
3<div id="fastcomments-widget"></div>
4<script>
5window.fcConfigs = [{
6 "tenantId": "demo",
7 "extensions": [
8 {
9 "id": "my-extension",
10 "path": "https://example.com/my-extension.min.js"
11 }
12 ]
13}];
14</script>
15

機能的な例については、次のセクションを参照してください。


拡張機能オブジェクト Internal Link

拡張オブジェクトは次の定義で構成されています:

拡張オブジェクトの JSDoc
Copy CopyRun External Link
1
2/**
3 * The FastCommentsUI extension object. 特定のコンポーネントを遅延読み込みするために使用されます。例えば、レビューシステムは全ての顧客が使用するわけではないため、必要な時にのみその拡張を読み込みます。
4 *
5 * @typedef {Object} FastCommentsUIExtension
6 * @property {string} id
7 * @property {Element} scriptNode
8 * @property {Element} root - ウィジェットのルート DOM ノード。
9 * @property {string} [css]
10 * @property {Object} config - FastComments の設定オブジェクト。
11 * @property {Object} commentsById - 全コメントを id ごとに保持するオブジェクトへの参照。常に最新に保たれます。
12 * @property {Object} translations - 全翻訳への参照。
13 * @property {Function} reRenderComment - コメントを再レンダリングするために呼び出せる関数への参照。
14 * @property {Function} removeCommentAndReRender - メモリからコメントを削除し、適切な DOM 部分を再レンダリングするために呼び出せる関数への参照。
15 * @property {Function} newBroadcastId - 新しいブロードキャスト ID を作成し、無視するローカルのブロードキャスト ID リストに追加するために呼び出せる関数への参照。
16 * @property {FastCommentsUIExtensionSetupEventHandlers} [setupEventHandlers]
17 * @property {FastCommentsUIExtensionPrepareCommentForSavingCallback} [prepareCommentForSaving]
18 * @property {FastCommentsUIExtensionNewCommentCallback} [newComment]
19 * @property {FastCommentsUIExtensionReplyAreaFilter} [replyAreaFilter] - コメント入力エリアの HTML をフィルタします。
20 * @property {FastCommentsUIExtensionWidgetFilter} [widgetFilter] - レンダー時にウィジェット全体の HTML をフィルタします。
21 * @property {FastCommentsUIExtensionCommentTopFilter} [commentFilter] - 各コメントをレンダリングする前にその HTML をフィルタします。
22 * @property {FastCommentsUIExtensionReplyAreaFilter} [commentMenuFilter] - 各コメントメニューをレンダリングする前にその HTML をフィルタします。
23 * @property {FastCommentsUIExtensionMenuFilter} [menuFilter] - レンダー時にウィジェット全体の HTML をフィルタします。
24 * @property {FastCommentsUIExtensionReplyAreaTop} [replyAreaTop] - (レガシー)返信エリアの上部に追加する HTML を返します。
25 * @property {FastCommentsUIExtensionWidgetTopCallback} [widgetTop] - (レガシー)ウィジェットの上部に追加する HTML を返します。
26 * @property {FastCommentsUIExtensionCommentTopCallback} [commentTop] - (レガシー)コメント要素の上部に追加する HTML を返します。
27 * @property {FastCommentsUIExtensionCommentBottomCallback} [commentBottom] - (レガシー)コメント要素の下部に追加する HTML を返します。
28 * @property {FastCommentsUIExtensionMenuBottomCallback} [menuBottom] - (レガシー)各コメントのメニュー要素の下部に追加する HTML を返します。
29 * @property {FastCommentsUIExtensionRenderCallback} [onRender]
30 * @property {FastCommentsUIExtensionConnectionStatusCallback} [onLiveConnectionStatusUpdate]
31 * @property {FastCommentsUIExtensionInitialRenderCallback} [onInitialRenderComplete]
32 * @property {FastCommentsUIExtensionPresenceUpdateCallback} [onPresenceUpdate]
33 */
34
35/**
36 * @callback FastCommentsUIExtensionSetupEventHandlers
37 * @param {Element} element - ルート要素。
38 * @param {Object.<string, Function>} clickListeners - クラス名ごとのクリック用イベントハンドラ。参照で変更可能です。
39 * @returns void
40 */
41
42/**
43 * @callback FastCommentsUIExtensionWidgetTopCallback
44 * @param {Object} moduleData
45 * @returns {string}
46 */
47
48/**
49 * @callback FastCommentsUIExtensionWidgetFilter
50 * @param {Object} moduleData
51 * @param {Object} html
52 * @returns {string}
53 */
54
55/**
56 * @callback FastCommentsUIExtensionCommentTopCallback
57 * @param {Object} comment
58 * @returns {string}
59 */
60
61/**
62 * @callback FastCommentsUIExtensionCommentTopFilter
63 * @param {Object} comment
64 * @param {string} html
65 * @returns {string}
66 */
67
68/**
69 * @callback FastCommentsUIExtensionCommentBottomCallback
70 * @param {Object} comment
71 * @returns {string}
72 */
73
74/**
75 * @callback FastCommentsUIExtensionMenuBottomCallback
76 * @param {Object} comment
77 * @returns {string}
78 */
79
80/**
81 * @callback FastCommentsUIExtensionMenuFilter
82 * @param {Object} comment
83 * @param {string} html
84 * @returns {string}
85 */
86
87/**
88 * @callback FastCommentsUIExtensionRenderCallback
89 * @returns {string}
90 */
91
92/**
93 * @callback FastCommentsUIExtensionConnectionStatusCallback
94 * @param {boolean} isConnected
95 * @returns {void}
96 */
97
98/**
99 * @callback FastCommentsUIExtensionInitialRenderCallback
100 * @returns {void}
101 */
102
103/**
104 * @callback FastCommentsUIExtensionReplyAreaTop
105 * @param {Object|null} currentUser
106 * @param {boolean} isSaving
107 * @param {boolean} isReplyOpen
108 * @param {string|null} parentId
109 * @returns {string}
110 */
111
112/**
113 * @callback FastCommentsUIExtensionReplyAreaFilter
114 * @param {Object|null} currentUser
115 * @param {boolean} isSaving
116 * @param {boolean} isReplyOpen
117 * @param {string|null} parentId
118 * @param {string|null} html
119 * @returns {string}
120 */
121
122/**
123 * @callback FastCommentsUIExtensionPrepareCommentForSavingCallback
124 * @param {Object} comment
125 * @param {string} parentId
126 */
127
128/**
129 * @callback FastCommentsUIExtensionNewCommentCallback
130 * @param {Object} comment
131 */
132
133/**
134 * @callback FastCommentsUIExtensionPresenceUpdateCallback
135 * @param {Object} update
136 */
137

拡張機能のAPI Internal Link

Interacting with the Extension is simple, as we simply define references to functions we want invoked.

To build off the example earlier, let's say we want to add HTML to the top of each comment:

シンプルな拡張機能 - 続き
Copy CopyRun External Link
1
2(function () {
3 const extension = FastCommentsUI.extensions.find((extension) => {
4 return extension.id === 'my-extension';
5 });
6
7 extension.commentFilter = function(comment, html) {
8 return `<h3>The user's name is ${comment.commenterName}!</h3>` + html;
9 }
10})();
11

Whenever you return HTML like this, it will get merged into the UI via a dom-diffing algorithm.

コメントの再レンダリングを手動でトリガーする

We can wait for the initial page load and manually re-render a comment by invoking reRenderComment:

コメントの再レンダリング
Copy CopyRun External Link
1
2(function () {
3 const extension = FastCommentsUI.extensions.find((extension) => {
4 return extension.id === 'my-extension';
5 });
6
7 let renderCount = 0;
8
9 extension.commentFilter = function(comment, html) {
10 renderCount++;
11 return `<h3>The render count is ${renderCount}!</h3>` + html;
12 }
13
14 extension.onInitialRenderComplete = function() {
15 setInterval(function() {
16 extension.reRenderComment(extension.commentsById[Object.keys(extension.commentsById)[0]], function renderDone() {
17 console.log('Comment re-render done.');
18 });
19 }, 2000); // タイムアウトは必須ではありません。例として示しています。
20 }
21})();
22