FastComments.com

확장 프로그램 작성

확장 기능 개발

개요

FastComments는 우리가 확장(Extensions)이라고 부르는 스크립트를 통해 핵심 기능을 확장할 수 있는 기능을 제공합니다.

An Extension은 댓글 위젯에 추가 마크업을 더하고, 이벤트 리스너를 추가하며, 임의의 코드를 실행할 수 있습니다.

여기에는 우리가 프로덕션에서 사용 중인 확장 예제들과 확장을 작성하는 방법에 대한 문서가 있습니다.


확장 프로그램의 수명 주기 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 * FastCommentsUI 확장 객체입니다. 특정 컴포넌트를 지연 로드하는 데 사용됩니다. 예를 들어, 리뷰 시스템은 모든 고객이 사용하지 않으므로
4 * 우리가 원할 때만 해당 확장을 로드합니다.
5 *
6 * @typedef {Object} FastCommentsUIExtension
7 * @property {string} id
8 * @property {Element} scriptNode
9 * @property {Element} root - 위젯 루트 DOM 노드.
10 * @property {string} [css]
11 * @property {Object} config - FastComments 설정 객체.
12 * @property {Object} commentsById - ID별 모든 댓글을 포함하는 객체에 대한 참조이며, 최신 상태를 유지합니다.
13 * @property {Object} translations - 모든 번역에 대한 참조.
14 * @property {Function} reRenderComment - 댓글을 다시 렌더링할 수 있는 함수에 대한 참조.
15 * @property {Function} removeCommentAndReRender - 댓글을 메모리에서 제거하고 DOM의 해당 부분을 다시 렌더링할 수 있는 함수에 대한 참조.
16 * @property {Function} newBroadcastId - 새 방송 ID를 생성하고 무시할 로컬 방송 ID 목록에 추가할 수 있는 함수에 대한 참조.
17 * @property {FastCommentsUIExtensionSetupEventHandlers} [setupEventHandlers]
18 * @property {FastCommentsUIExtensionPrepareCommentForSavingCallback} [prepareCommentForSaving] - 게시될 댓글과 함께 호출됩니다. false를 반환하면 제출을 취소합니다(예: 연결된 설문조사가 완전하지 않은 경우).
19 * @property {FastCommentsUIExtensionNewCommentCallback} [newComment]
20 * @property {FastCommentsUIExtensionReplyAreaFilter} [replyAreaFilter] - 댓글 영역의 HTML을 필터링합니다.
21 * @property {FastCommentsUIExtensionWidgetFilter} [widgetFilter] - 렌더링 시 전체 위젯의 HTML을 필터링합니다.
22 * @property {FastCommentsUIExtensionCommentTopFilter} [commentFilter] - 렌더링 전에 각 댓글의 HTML을 필터링합니다.
23 * @property {FastCommentsUIExtensionReplyAreaFilter} [commentMenuFilter] - 렌더링 전에 각 댓글 메뉴의 HTML을 필터링합니다.
24 * @property {FastCommentsUIExtensionMenuFilter} [menuFilter] - 렌더링 시 전체 위젯의 HTML을 필터링합니다.
25 * @property {FastCommentsUIExtensionReplyAreaTop} [replyAreaTop] - (LEGACY) 답글 영역 상단에 추가할 HTML을 반환합니다.
26 * @property {FastCommentsUIExtensionWidgetTopCallback} [widgetTop] - (LEGACY) 위젯 상단에 추가할 HTML을 반환합니다.
27 * @property {FastCommentsUIExtensionCommentTopCallback} [commentTop] - (LEGACY) 댓글 요소 상단에 추가할 HTML을 반환합니다.
28 * @property {FastCommentsUIExtensionCommentBottomCallback} [commentBottom] - (LEGACY) 댓글 요소 하단에 추가할 HTML을 반환합니다.
29 * @property {FastCommentsUIExtensionCommentBottomCallback} [commentContentBottom] - 댓글 텍스트 뒤, 댓글 내용 요소 내부에 추가할 HTML을 반환합니다(설문조사에 사용).
30 * @property {Function} [replyAreaInputBottom] - 텍스트 입력 아래, 댓글 입력 프레임 내부에 추가할 HTML을 반환합니다(인라인 설문 편집기에 사용). 부모 댓글 ID를 받으며, 루트 답글 박스인 경우 null을 받습니다.
31 * @property {Function} [onPollUpdate] - 페이지의 설문 투표 수가 변경될 때 실시간 이벤트와 함께 호출됩니다.
32 * @property {Function} isSiteAdmin - 뷰어가 테넌트의 관리자 또는 모더레이터인지 여부를 반환합니다. 첫 번째 가져오기 후에 알 수 있습니다.
33 * @property {FastCommentsUIExtensionMenuBottomCallback} [menuBottom] - (LEGACY) 각 댓글에 대한 메뉴 요소 하단에 추가할 HTML을 반환합니다.
34 * @property {FastCommentsUIExtensionRenderCallback} [onRender]
35 * @property {FastCommentsUIExtensionConnectionStatusCallback} [onLiveConnectionStatusUpdate]
36 * @property {FastCommentsUIExtensionInitialRenderCallback} [onInitialRenderComplete]
37 * @property {FastCommentsUIExtensionPresenceUpdateCallback} [onPresenceUpdate]
38 */
39
40/**
41 * @callback FastCommentsUIExtensionSetupEventHandlers
42 * @param {Element} element - 루트 요소.
43 * @param {Object.<string, Function>} clickListeners - 클래스 이름별 클릭 이벤트 핸들러이며, 참조를 통해 수정할 수 있습니다.
44 * @returns void
45 */
46
47/**
48 * @callback FastCommentsUIExtensionWidgetTopCallback
49 * @param {Object} moduleData
50 * @returns {string}
51 */
52
53/**
54 * @callback FastCommentsUIExtensionWidgetFilter
55 * @param {Object} moduleData
56 * @param {Object} html
57 * @returns {string}
58 */
59
60/**
61 * @callback FastCommentsUIExtensionCommentTopCallback
62 * @param {Object} comment
63 * @returns {string}
64 */
65
66/**
67 * @callback FastCommentsUIExtensionCommentTopFilter
68 * @param {Object} comment
69 * @param {string} html
70 * @returns {string}
71 */
72
73/**
74 * @callback FastCommentsUIExtensionCommentBottomCallback
75 * @param {Object} comment
76 * @returns {string}
77 */
78
79/**
80 * @callback FastCommentsUIExtensionMenuBottomCallback
81 * @param {Object} comment
82 * @returns {string}
83 */
84
85/**
86 * @callback FastCommentsUIExtensionMenuFilter
87 * @param {Object} comment
88 * @param {string} html
89 * @returns {string}
90 */
91
92/**
93 * @callback FastCommentsUIExtensionRenderCallback
94 * @returns {string}
95 */
96
97/**
98 * @callback FastCommentsUIExtensionConnectionStatusCallback
99 * @param {boolean} isConnected
100 * @returns {void}
101 */
102
103/**
104 * @callback FastCommentsUIExtensionInitialRenderCallback
105 * @returns {void}
106 */
107
108/**
109 * @callback FastCommentsUIExtensionReplyAreaTop
110 * @param {Object|null} currentUser
111 * @param {boolean} isSaving
112 * @param {boolean} isReplyOpen
113 * @param {string|null} parentId
114 * @returns {string}
115 */
116
117/**
118 * @callback FastCommentsUIExtensionReplyAreaFilter
119 * @param {Object|null} currentUser
120 * @param {boolean} isSaving
121 * @param {boolean} isReplyOpen
122 * @param {string|null} parentId
123 * @param {string|null} html
124 * @returns {string}
125 */
126
127/**
128 * @callback FastCommentsUIExtensionPrepareCommentForSavingCallback
129 * @param {Object} comment
130 * @param {string} parentId
131 */
132
133/**
134 * @callback FastCommentsUIExtensionNewCommentCallback
135 * @param {Object} comment
136 */
137
138/**
139 * @callback FastCommentsUIExtensionPresenceUpdateCallback
140 * @param {Object} update
141 */
142

확장 API Internal Link

Extension과 상호작용하는 것은 간단합니다. 호출되길 원하는 함수들에 대한 참조를 정의하기만 하면 됩니다.

앞의 예제에서 이어서, 각 댓글의 상단에 HTML을 추가한다고 가정해봅시다:

간단한 확장 - 계속
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

이와 같이 HTML을 반환하면, dom-diffing 알고리즘을 통해 UI에 병합됩니다.

댓글을 수동으로 다시 렌더링하기

초기 페이지 로드를 기다린 다음 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); // timeout not required, just an example.
20 }
21})();
22