Skip to content
Merged
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
55 changes: 40 additions & 15 deletions app/assets/javascripts/comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ $(() => {
return !!wrapper.querySelector('[data-inline=true]');
};

const afterThreadRender = () => {
window.MathJax && MathJax.typeset();
window.hljs && hljs.highlightAll();
};

/**
* @param {HTMLElement} wrapper
* @param {string} threadId
Expand All @@ -35,20 +40,32 @@ $(() => {
async function openThread(wrapper, threadId, options) {
const data = await QPixel.getThreadContent(threadId, options);

if (!data) {
QPixel.createNotification('danger', 'Failed to open thread');
return;
}

wrapper.innerHTML = data;

window.MathJax && MathJax.typeset();
window.hljs && hljs.highlightAll();
if (window.MathJax) {
const titleElem = wrapper.querySelector('.js-thread-title');

if (titleElem && QPixel.DOM.isHTMLElement(titleElem)) {
titleElem.dataset.title = titleElem.textContent;
}
}

afterThreadRender();
}

$(document).on('click', '.post--comments-thread.is-inline a', async (ev) => {
$(document).on('click', '.js--comment-link', async (ev) => {
if (ev.ctrlKey || ev.metaKey) {
return;
}

ev.preventDefault();

const $tgt = $(ev.target);
const $tgt = $(ev.currentTarget);
const $threadId = $tgt.data('thread');
const wrapper = getCommentThreadWrapper($tgt);

Expand All @@ -73,13 +90,14 @@ $(() => {
$(document).on('click', '.js-collapse-thread', async (ev) => {
const $tgt = $(ev.target);
const $widget = $tgt.parents('.widget');
const $title = $widget.find('.js-thread-title');
const $embed = $tgt.parents('.post--comments-thread');

const threadId = $widget.data('thread');
const isLocked = $widget.data('locked');
const isDeleted = $widget.data('deleted');
const isArchived = $widget.data('archived');
const threadTitle = $widget.find('.js-thread-title').text();
const threadTitle = $title.data('title') || $title.text();
const replyCount = $widget.data('comments');

const $container = $(`<div class="post--comments-thread is-inline"></div>`);
Expand All @@ -93,7 +111,7 @@ $(() => {

if (isDeleted) {
$container.append(
`<i class="fas fa-trash h-c-red-600 fa-fw" title="Deleted thread" aria-label="Deleted thread"></i>`
`<i class="fas fa-trash h-c-red-600 fa-fw" title="Deleted thread" aria-label="Deleted thread"></i>`,
);
$container.addClass('is-deleted');
}
Expand All @@ -106,6 +124,8 @@ $(() => {
$container.append($link);
$container.append(`(${replyCount} comment${replyCount !== 1 ? 's' : ''})`);
$embed[0].outerHTML = $container[0].outerHTML;

afterThreadRender();
});

$(document).on('click', '.js-comment-edit', async (evt) => {
Expand Down Expand Up @@ -185,7 +205,8 @@ $(() => {
if (isDelete) {
$comment.addClass('deleted-content');
$tgt.removeClass('js-comment-delete').addClass('js-comment-undelete').val('undelete');
} else {
}
else {
$comment.removeClass('deleted-content');
$tgt.removeClass('js-comment-undelete').addClass('js-comment-delete').val('delete');
}
Expand All @@ -202,7 +223,7 @@ $(() => {
const $modal = $($tgt.data('modal'));

const resp = await QPixel.fetch(`/comments/thread/${threadId}/followers`, {
headers: { Accept: 'text/html' }
headers: { Accept: 'text/html' },
});

const data = await resp.text();
Expand Down Expand Up @@ -274,7 +295,7 @@ $(() => {
openThread(wrapper, threadID, { inline });
}
});
})
});

/**
* @param {Element} target
Expand All @@ -287,7 +308,7 @@ $(() => {
const $submitter = $form.find('.js-rename-thread');

const newStripped = QPixel.MD.stripMarkdown($tgt.val(), {
removeLeadingQuote: true
removeLeadingQuote: true,
});

if (newStripped === $tgt.data('old')) {
Expand Down Expand Up @@ -316,7 +337,7 @@ $(() => {
const { old } = dataset;

const newStripped = QPixel.MD.stripMarkdown(newTitle, {
removeLeadingQuote: true
removeLeadingQuote: true,
});

if (newStripped === old) {
Expand Down Expand Up @@ -344,7 +365,8 @@ $(() => {
const inline = isInlineCommentThread(wrapper);
openThread(wrapper, threadID, { inline });
});
} else {
}
else {
QPixel.createNotification('danger', 'Failed to find thread to lock');
}
});
Expand Down Expand Up @@ -434,7 +456,8 @@ $(() => {
.attr('data-user-id', id);
});
QPixel.Popup.getPopup(items, $tgt[0], callback);
} else {
}
else {
QPixel.Popup.destroyAll();
}
}
Expand All @@ -448,7 +471,8 @@ $(() => {
if ($thread.is(':hidden')) {
$thread.show();
$thread.find('.js-comment-field').trigger('focus');
} else {
}
else {
$thread.hide();
}
});
Expand All @@ -463,7 +487,8 @@ $(() => {
if ($reply.is(':hidden')) {
$reply.show();
$reply.find('.js-comment-field').trigger('focus');
} else {
}
else {
$reply.hide();
}
});
Expand Down
4 changes: 4 additions & 0 deletions app/assets/javascripts/qpixel_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,10 @@ window.QPixel = {
headers: { 'Accept': 'text/html' }
});

if (!resp.ok) {
return '';
}

const content = await resp.text();

return content;
Expand Down
Loading