Skip to content

Commit

Permalink
proper detection whether HIX is outdated by comparing to previous con…
Browse files Browse the repository at this point in the history
…tent used to get current value
  • Loading branch information
PeterNerlich committed Oct 23, 2023
1 parent 28cf4be commit 3286df3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
2 changes: 2 additions & 0 deletions integreat_cms/release_notes/current/unreleased/2300.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
en: HIX value is no longer instandly outdated, if only some text was selected
de: HIX-Wert ist nicht mehr sofort veraltet, wenn Text lediglich ausgewählt wurde
28 changes: 23 additions & 5 deletions integreat_cms/static/src/js/analytics/hix-widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import { getCsrfToken } from "../utils/csrf-token";
// See https://www.chartjs.org/docs/latest/getting-started/integration.html#bundlers-webpack-rollup-etc for details
Chart.register(DoughnutController, ArcElement, CategoryScale, LinearScale, Tooltip, Title);

let initialContent: string = null;

const updateChart = (chart: Chart, value: number) => {
const hixMaxValue = 20;
const hixThresholdGood = 15;
Expand Down Expand Up @@ -78,20 +80,24 @@ const setHixLabelState = (state: string) => {
const getHixValue = async () => {
const updateButton = document.getElementById("btn-update-hix-value");
let result;
const sentContent = getContent().trim();
await fetch(updateButton.dataset.url, {
method: "POST",
headers: {
"X-CSRFToken": getCsrfToken(),
},
body: JSON.stringify({
text: getContent(),
text: sentContent,
}),
})
.then((response) => response.json())
.then((json) => {
const labelState = json.error ? "error" : "updated";
setHixLabelState(labelState);
result = json.score;
if (!json.error) {
initialContent = sentContent;
}
});
return result;
};
Expand Down Expand Up @@ -170,7 +176,9 @@ window.addEventListener("load", async () => {
};

const initHixValue = async () => {
if (!getContent().trim()) {
initialContent = getContent().trim();

if (!initialContent) {
setHixLabelState("no-content");
return;
}
Expand All @@ -188,9 +196,19 @@ window.addEventListener("load", async () => {
document.querySelectorAll("[data-content-changed]").forEach((element) => {
// make sure initHixValue is called only after tinyMCE is initialized
element.addEventListener("tinyMCEInitialized", initHixValue);
element.addEventListener("contentChanged", () =>
setHixLabelState(getContent().trim() ? "outdated" : "no-content")
);
element.addEventListener("contentChanged", () => {
const content = getContent().trim();
const labelState = (() => {
if (!content) {
return "no-content";
}
if (content !== initialContent) {
return "outdated";
}
return "updated";
})();
return setHixLabelState(labelState);
});
});

// Set listener for update button
Expand Down

0 comments on commit 3286df3

Please sign in to comment.