fix: #10256, allow quote tooltip on mobile

isekai-main
Barış Soner Uşaklı 3 years ago
parent 08f2a05053
commit fb3f4f9a13

@ -466,67 +466,67 @@ define('forum/topic/postTools', [
warning.modal(); warning.modal();
} }
const selectionChangeFn = utils.debounce(selectionChange, 100);
function handleSelectionTooltip() { function handleSelectionTooltip() {
hooks.onPage('action:posts.loaded', delayedTooltip); hooks.onPage('action:posts.loaded', delayedTooltip);
$(document).off('mouseup', delayedTooltip).on('mouseup', delayedTooltip); $(document).off('selectionchange', selectionChangeFn).on('selectionchange', selectionChangeFn);
$(document).off('selectionchange', selectionChange).on('selectionchange', selectionChange);
} }
let selectionEmpty = true;
function selectionChange() { function selectionChange() {
selectionEmpty = window.getSelection().toString() === ''; const selectionEmpty = window.getSelection().toString() === '';
if (selectionEmpty) { if (selectionEmpty) {
$('[component="selection/tooltip"]').addClass('hidden'); $('[component="selection/tooltip"]').addClass('hidden');
} else {
delayedTooltip();
} }
} }
function delayedTooltip() { async function delayedTooltip() {
setTimeout(async function () { let selectionTooltip = $('[component="selection/tooltip"]');
let selectionTooltip = $('[component="selection/tooltip"]'); selectionTooltip.addClass('hidden');
selectionTooltip.addClass('hidden'); if (selectionTooltip.attr('data-ajaxify') === '1') {
if (selectionTooltip.attr('data-ajaxify') === '1') { selectionTooltip.remove();
selectionTooltip.remove(); return;
}
const selection = window.getSelection();
if (selection.focusNode && selection.type === 'Range' && ajaxify.data.template.topic) {
const focusNode = $(selection.focusNode);
const anchorNode = $(selection.anchorNode);
const firstPid = anchorNode.parents('[data-pid]').attr('data-pid');
const lastPid = focusNode.parents('[data-pid]').attr('data-pid');
if (firstPid !== lastPid || !focusNode.parents('[component="post/content"]').length || !anchorNode.parents('[component="post/content"]').length) {
return; return;
} }
const postEl = focusNode.parents('[data-pid]');
const selectionRange = selection.getRangeAt(0);
if (!postEl.length || selectionRange.collapsed) {
return;
}
const rects = selectionRange.getClientRects();
const lastRect = rects[rects.length - 1];
const selection = window.getSelection(); if (!selectionTooltip.length) {
if (selection.focusNode && selection.type === 'Range' && ajaxify.data.template.topic && !selectionEmpty) { selectionTooltip = await app.parseAndTranslate('partials/topic/selection-tooltip', ajaxify.data);
const focusNode = $(selection.focusNode); selectionTooltip.addClass('hidden').appendTo('body');
const anchorNode = $(selection.anchorNode);
const firstPid = anchorNode.parents('[data-pid]').attr('data-pid');
const lastPid = focusNode.parents('[data-pid]').attr('data-pid');
if (firstPid !== lastPid || !focusNode.parents('[component="post/content"]').length || !anchorNode.parents('[component="post/content"]').length) {
return;
}
const postEl = focusNode.parents('[data-pid]');
const selectionRange = selection.getRangeAt(0);
if (!postEl.length || selectionRange.collapsed) {
return;
}
const rects = selectionRange.getClientRects();
const lastRect = rects[rects.length - 1];
if (!selectionTooltip.length) {
selectionTooltip = await app.parseAndTranslate('partials/topic/selection-tooltip', ajaxify.data);
selectionTooltip.addClass('hidden').appendTo('body');
}
selectionTooltip.off('click').on('click', '[component="selection/tooltip/quote"]', function () {
selectionTooltip.addClass('hidden');
onQuoteClicked(postEl.find('[component="post/quote"]'), ajaxify.data.tid);
});
selectionTooltip.removeClass('hidden');
$(window).one('action:ajaxify.start', function () {
selectionTooltip.attr('data-ajaxify', 1).addClass('hidden');
$(document).off('selectionchange', selectionChange);
});
const tooltipWidth = selectionTooltip.outerWidth(true);
selectionTooltip.css({
top: lastRect.bottom + $(window).scrollTop(),
left: tooltipWidth > lastRect.width ? lastRect.left : lastRect.left + lastRect.width - tooltipWidth,
});
} }
}, 0); selectionTooltip.off('click').on('click', '[component="selection/tooltip/quote"]', function () {
selectionTooltip.addClass('hidden');
onQuoteClicked(postEl.find('[component="post/quote"]'), ajaxify.data.tid);
});
selectionTooltip.removeClass('hidden');
$(window).one('action:ajaxify.start', function () {
selectionTooltip.attr('data-ajaxify', 1).addClass('hidden');
$(document).off('selectionchange', selectionChangeFn);
});
const tooltipWidth = selectionTooltip.outerWidth(true);
selectionTooltip.css({
top: lastRect.bottom + $(window).scrollTop(),
left: tooltipWidth > lastRect.width ? lastRect.left : lastRect.left + lastRect.width - tooltipWidth,
});
}
} }
return PostTools; return PostTools;

Loading…
Cancel
Save