FastComments.com

Razvijanje razširitev

Kontekst

FastComments omogoča razširitev naše osnovne funkcionalnosti s skriptami, ki jim pravimo Extensions.

Extension lahko doda dodatno označevanje (markup) v pripomoček za komentarje, doda poslušalce dogodkov in izvaja poljubno kodo.

Tukaj boste našli primere razširitev, ki jih imamo v produkciji, kot tudi dokumentacijo o tem, kako pisati razširitve.


Življenjski cikel razširitve Internal Link

Skript za vsako razširitev se pridobi in zažene, preden pripomoček za komentarje začne pridobivati prvi niz komentarjev in upodabljati uporabniški vmesnik.

Ob prvem nalaganju bodo na objekt razširitve pripeti naslednji podatki:

  • config - Sklic na objekt config.
  • translations - Sklic na objekt translations.
  • commentsById - Sklic na vse komentarje po id.
  • root - Sklic na korensko DOM vozlišče.

Razširitve naj preglasijo želene funkcije, ki jih bo pripomoček za komentarje poklical ob ustreznih časih.

Opredelitev razširitve Internal Link

Najmanjša možna razširitev bi bila:

Preprosta razširitev
Copy CopyRun External Link
1
2(function () {
3 const extension = FastCommentsUI.extensions.find((extension) => {
4 return extension.id === 'my-extension';
5 });
6})();
7

V ta primer shranite datoteko kot my-extension.js, in jo naredite dostopno na https://example.com/my-extension.min.js.

Ta razširitev ne počne ničesar, ob nalaganju pa pridobi objekt razširitve, ki ga ustvari osnovna knjižnica komentarjev.

This Extension object is a singleton and is not shared with any other extensions.

Da naložimo našo razširitev, moramo pripomočku za komentarje povedati za njo. Na primer:

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

Za funkcionalne primere glejte naslednji razdelek.


Objekt razširitve Internal Link

Objekt razširitve vsebuje naslednjo definicijo:

JSDoc razširitvenega objekta
Copy CopyRun External Link
1
2/**
3 * The FastCommentsUI extension object. Uporablja se za odloženo nalaganje določenih komponent. Na primer, sistem ocen ni
4 * v uporabi pri vseh strankah, zato to razširitev naložimo le takrat, ko jo potrebujemo.
5 *
6 * @typedef {Object} FastCommentsUIExtension
7 * @property {string} id
8 * @property {Element} scriptNode
9 * @property {Element} root - Koreninski DOM element gradnika.
10 * @property {string} [css]
11 * @property {Object} config - Konfiguracijski objekt FastComments.
12 * @property {Object} commentsById - Referenca na objekt z vsemi komentarji po id-ju, ki se ažurno posodablja.
13 * @property {Object} translations - Referenca na vse prevode.
14 * @property {Function} reRenderComment - Referenca na funkcijo, ki jo lahko pokličete za ponovni izris komentarja.
15 * @property {Function} removeCommentAndReRender - Referenca na funkcijo, ki jo lahko pokličete za odstranitev komentarja iz pomnilnika in ponovno upodobitev ustreznega dela DOM-a.
16 * @property {Function} newBroadcastId - Referenca na funkcijo, ki jo lahko pokličete za ustvarjanje novega broadcast id-ja in njegovo dodajanje v lokalni seznam broadcast id-jev, ki jih je treba prezreti.
17 * @property {FastCommentsUIExtensionSetupEventHandlers} [setupEventHandlers]
18 * @property {FastCommentsUIExtensionPrepareCommentForSavingCallback} [prepareCommentForSaving]
19 * @property {FastCommentsUIExtensionNewCommentCallback} [newComment]
20 * @property {FastCommentsUIExtensionReplyAreaFilter} [replyAreaFilter] - Filter HTML-ja za območje komentarjev.
21 * @property {FastCommentsUIExtensionWidgetFilter} [widgetFilter] - Filter HTML-ja za celoten gradnik ob upodabljanju.
22 * @property {FastCommentsUIExtensionCommentTopFilter} [commentFilter] - Filter HTML-ja za vsak komentar pred upodabljanjem.
23 * @property {FastCommentsUIExtensionReplyAreaFilter} [commentMenuFilter] - Filter HTML-ja za meni vsakega komentarja pred upodabljanjem.
24 * @property {FastCommentsUIExtensionMenuFilter} [menuFilter] - Filter HTML-ja za celoten gradnik ob upodabljanju.
25 * @property {FastCommentsUIExtensionReplyAreaTop} [replyAreaTop] - (ZASTARELO) Vrne HTML za dodajanje na vrh območja odgovorov.
26 * @property {FastCommentsUIExtensionWidgetTopCallback} [widgetTop] - (ZASTARELO) Vrne HTML za dodajanje na vrh gradnika.
27 * @property {FastCommentsUIExtensionCommentTopCallback} [commentTop] - (ZASTARELO) Vrne HTML za dodajanje na vrh elementa komentarja.
28 * @property {FastCommentsUIExtensionCommentBottomCallback} [commentBottom] - (ZASTARELO) Vrne HTML za dodajanje na dno elementa komentarja.
29 * @property {FastCommentsUIExtensionMenuBottomCallback} [menuBottom] - (ZASTARELO) Vrne HTML za dodajanje na dno elementa menija za vsak komentar.
30 * @property {FastCommentsUIExtensionRenderCallback} [onRender]
31 * @property {FastCommentsUIExtensionConnectionStatusCallback} [onLiveConnectionStatusUpdate]
32 * @property {FastCommentsUIExtensionInitialRenderCallback} [onInitialRenderComplete]
33 * @property {FastCommentsUIExtensionPresenceUpdateCallback} [onPresenceUpdate]
34 */
35
36/**
37 * @callback FastCommentsUIExtensionSetupEventHandlers
38 * @param {Element} element - Koreninski element.
39 * @param {Object.<string, Function>} clickListeners - Event handlerji za klike, po imenu razreda, ki jih je mogoče spremeniti po referenci.
40 * @returns void
41 */
42
43/**
44 * @callback FastCommentsUIExtensionWidgetTopCallback
45 * @param {Object} moduleData
46 * @returns {string}
47 */
48
49/**
50 * @callback FastCommentsUIExtensionWidgetFilter
51 * @param {Object} moduleData
52 * @param {Object} html
53 * @returns {string}
54 */
55
56/**
57 * @callback FastCommentsUIExtensionCommentTopCallback
58 * @param {Object} comment
59 * @returns {string}
60 */
61
62/**
63 * @callback FastCommentsUIExtensionCommentTopFilter
64 * @param {Object} comment
65 * @param {string} html
66 * @returns {string}
67 */
68
69/**
70 * @callback FastCommentsUIExtensionCommentBottomCallback
71 * @param {Object} comment
72 * @returns {string}
73 */
74
75/**
76 * @callback FastCommentsUIExtensionMenuBottomCallback
77 * @param {Object} comment
78 * @returns {string}
79 */
80
81/**
82 * @callback FastCommentsUIExtensionMenuFilter
83 * @param {Object} comment
84 * @param {string} html
85 * @returns {string}
86 */
87
88/**
89 * @callback FastCommentsUIExtensionRenderCallback
90 * @returns {string}
91 */
92
93/**
94 * @callback FastCommentsUIExtensionConnectionStatusCallback
95 * @param {boolean} isConnected
96 * @returns {void}
97 */
98
99/**
100 * @callback FastCommentsUIExtensionInitialRenderCallback
101 * @returns {void}
102 */
103
104/**
105 * @callback FastCommentsUIExtensionReplyAreaTop
106 * @param {Object|null} currentUser
107 * @param {boolean} isSaving
108 * @param {boolean} isReplyOpen
109 * @param {string|null} parentId
110 * @returns {string}
111 */
112
113/**
114 * @callback FastCommentsUIExtensionReplyAreaFilter
115 * @param {Object|null} currentUser
116 * @param {boolean} isSaving
117 * @param {boolean} isReplyOpen
118 * @param {string|null} parentId
119 * @param {string|null} html
120 * @returns {string}
121 */
122
123/**
124 * @callback FastCommentsUIExtensionPrepareCommentForSavingCallback
125 * @param {Object} comment
126 * @param {string} parentId
127 */
128
129/**
130 * @callback FastCommentsUIExtensionNewCommentCallback
131 * @param {Object} comment
132 */
133
134/**
135 * @callback FastCommentsUIExtensionPresenceUpdateCallback
136 * @param {Object} update
137 */
138

API razširitve 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:

Preprosta razširitev - Nadaljevanje
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.

Manually triggering the re-render of a comment

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

Ponovno upodabljanje komentarja
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 ni potreben, le primer.
20 }
21})();
22