Skip to content

Commit 1e6fff5

Browse files
committed
2 parents 70b056f + 40a5f3d commit 1e6fff5

File tree

3 files changed

+102
-1
lines changed

3 files changed

+102
-1
lines changed

.yfm

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,10 @@ resources:
88
- _assets/b24loc.js?20240925-02
99
- _assets/b24addons.js?20241208-01
1010

11-
search: true
11+
search: true
12+
13+
extensions:
14+
- './extensions/feedback-control'
15+
16+
feedbackControl:
17+
endpoint: https://util.1c-bitrix.ru/rest-doc/feedback-control.php
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
exports.Extension = void 0;
4+
const node_assert_1 = require("node:assert");
5+
const node_path_1 = require("node:path");
6+
const program_1 = require("@diplodoc/cli/lib/program");
7+
const cli_1 = require("@diplodoc/cli");
8+
class Extension {
9+
apply(program) {
10+
(0, program_1.getHooks)(program).Config.tap('FeedbackControl', (config) => {
11+
if (!config.feedbackControl || "boolean" === typeof config.feedbackControl) {
12+
return config;
13+
}
14+
(0, node_assert_1.ok)(config.feedbackControl.endpoint !== '', 'feedbackControl.endpoint must be not empty');
15+
return config;
16+
});
17+
(0, cli_1.getBuildHooks)(program).BeforeRun.for('html').tap('FeedbackControl', (run) => {
18+
if (!program.config.feedbackControl) {
19+
return;
20+
}
21+
(0, cli_1.getEntryHooks)(run.entry).Page.tap('FeedbackControl', (template) => {
22+
const controlConfig = program.config.feedbackControl === true ? {} : program.config.feedbackControl;
23+
template.addScript('_extensions/feedback-control-extension.js', {
24+
position: 'leading',
25+
attrs: {
26+
defer: void 0
27+
}
28+
});
29+
template.addScript(`window.feedbackControlExtensionInit(${JSON.stringify(controlConfig)})`, {
30+
position: 'state',
31+
inline: true,
32+
});
33+
});
34+
});
35+
(0, cli_1.getBuildHooks)(program).AfterRun.for('html').tapPromise('FeedbackControl', async (run) => {
36+
if (!program.config.feedbackControl) {
37+
return;
38+
}
39+
const extensionFilePath = (0, node_path_1.join)(__dirname, 'resources', 'feedback-control-extension.js');
40+
try {
41+
await run.copy(extensionFilePath, (0, node_path_1.join)(run.output, '_extensions', 'feedback-control-extension.js'));
42+
}
43+
catch (error) {
44+
run.logger.warn(`Unable copy the feedback-control extension script ${extensionFilePath}.`, error);
45+
}
46+
});
47+
}
48+
}
49+
exports.Extension = Extension;
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
"use strict";
2+
window.feedbackControlExtensionInit = (options) => {
3+
const diplodocDataRef = window.__DATA__;
4+
if (diplodocDataRef.data.leading) {
5+
return;
6+
}
7+
options = Object.assign({}, { endpoint: void 0 }, options);
8+
const route = window.location.pathname;
9+
const storageKey = `feedback:${route}`;
10+
const sendData = async (data) => {
11+
await fetch(options.endpoint, {
12+
method: 'POST',
13+
headers: {
14+
'Content-Type': 'application/json;charset=utf-8'
15+
},
16+
body: JSON.stringify({
17+
route,
18+
...data
19+
})
20+
});
21+
};
22+
const sendLocalState = (data) => {
23+
if (data.type === 'indeterminate') {
24+
window.localStorage.removeItem(storageKey);
25+
return;
26+
}
27+
window.localStorage.setItem(storageKey, JSON.stringify(data));
28+
};
29+
const getLocalState = () => {
30+
const data = window.localStorage.getItem(storageKey);
31+
return data ? JSON.parse(data) : null;
32+
};
33+
const localState = getLocalState();
34+
if ((localState === null || localState === void 0 ? void 0 : localState.type) === 'like') {
35+
diplodocDataRef.data.isLiked = true;
36+
}
37+
if ((localState === null || localState === void 0 ? void 0 : localState.type) === 'dislike') {
38+
diplodocDataRef.data.isDisliked = true;
39+
}
40+
diplodocDataRef.data.onSendFeedback = (data) => {
41+
sendLocalState(data);
42+
if (options.endpoint !== void 0) {
43+
void sendData(data);
44+
}
45+
};
46+
};

0 commit comments

Comments
 (0)