Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Propagate changes of md-rendered checkboxes to the server #14258

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions web_src/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import initHeatmap from './features/heatmap.js';
import initProject from './features/projects.js';
import initServiceWorker from './features/serviceworker.js';
import initMarkdownAnchors from './markdown/anchors.js';
import initMarkdownCheckboxes from './markdown/checkboxes.js';
import renderMarkdownContent from './markdown/content.js';
import attachTribute from './features/tribute.js';
import createColorPicker from './features/colorpicker.js';
Expand Down Expand Up @@ -2644,6 +2645,8 @@ $(document).ready(async () => {
renderMarkdownContent(),
initGithook(),
]);

initMarkdownCheckboxes();
});

function changeHash(hash) {
Expand Down
70 changes: 70 additions & 0 deletions web_src/js/markdown/checkboxes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
const checkboxMarkdownPattern = /\[[ x]]/g;

/**
* Attaches `change` handlers to markdown rendered checkboxes in comments.
* When a checkbox value changes, the corresponding [ ] or [x] in the markdown string is set accordingly and sent to the server.
* On success it updates the raw-content on error it resets the checkbox to its original value.
*/
export default function initMarkdownCheckboxes() {
$('.comment .segment').each((_, segment) => {
const $segment = $(segment);
const $checkboxes = $segment.find('.render-content.markdown input:checkbox');

const onChange = async (ev, checkboxIndex) => {
const $checkbox = $(ev.target);
const checkboxMarkdown = $checkbox.is(':checked') ? '[x]' : '[ ]';

const $rawContent = $segment.find('.raw-content');
const oldContent = $rawContent.text();
const newContent = oldContent.replace(checkboxMarkdownPattern, replaceNthMatchWith(checkboxIndex, checkboxMarkdown));

if (newContent !== oldContent) {
disableAll($checkboxes);

try {
const url = $segment.find('.edit-content-zone').data('update-url');
const context = $segment.find('.edit-content-zone').data('context');

await submit(newContent, url, context);
$rawContent.text(newContent);
} catch (e) {
$checkbox.prop('checked', !$checkbox.is(':checked'));

console.error(e);
} finally {
enableAll($checkboxes);
}
}
};

enableAll($checkboxes);
$checkboxes.each((checkboxIndex, checkboxElement) => {
$(checkboxElement).on('change', (ev) => onChange(ev, checkboxIndex));
});
});
}

function enableAll ($checkboxes) { $checkboxes.removeAttr('disabled') }
function disableAll ($checkboxes) { $checkboxes.attr('disabled', 'disabled') }

function submit (content, url, context) {
const csrf = window.config.csrf;

return $.post(url, {
_csrf: csrf,
context,
content,
});
}

function replaceNthMatchWith(n, replaceWith) {
let matchIndex = 0;

return (match) => {
if (n === matchIndex++) {
return replaceWith;
}

return match;
};
}