add js to quote part of a post
This commit is contained in:
@ -8,6 +8,125 @@
|
||||
ta.focus();
|
||||
})
|
||||
}
|
||||
|
||||
function supportsPopover() {
|
||||
return Object.hasOwn(HTMLElement.prototype, "popover");
|
||||
}
|
||||
|
||||
if (supportsPopover()){
|
||||
let quotedPostContainer = null;
|
||||
function isQuoteSelectionValid() {
|
||||
const selection = document.getSelection();
|
||||
|
||||
if (!selection || selection.rangeCount === 0 || selection.isCollapsed) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const range = selection.getRangeAt(0);
|
||||
const commonAncestor = range.commonAncestorContainer;
|
||||
|
||||
const ancestorElement = commonAncestor.nodeType === Node.TEXT_NODE
|
||||
? commonAncestor.parentNode
|
||||
: commonAncestor;
|
||||
|
||||
const container = ancestorElement.closest(".post-inner");
|
||||
if (!container) {
|
||||
return false;
|
||||
}
|
||||
const success = container.contains(ancestorElement);
|
||||
if (success) {
|
||||
quotedPostContainer = container;
|
||||
}
|
||||
return success;
|
||||
}
|
||||
|
||||
let quotePopover = null;
|
||||
let isSelecting = false;
|
||||
|
||||
document.addEventListener("mousedown", () => {
|
||||
isSelecting = true;
|
||||
})
|
||||
|
||||
document.addEventListener("mouseup", () => {
|
||||
isSelecting = false;
|
||||
handlePossibleSelection();
|
||||
})
|
||||
|
||||
document.addEventListener("keyup", (e) => {
|
||||
if (e.shiftKey && (e.key.startsWith('Arrow') || e.key === 'Home' || e.key === 'End')) {
|
||||
handlePossibleSelection();
|
||||
}
|
||||
})
|
||||
|
||||
function handlePossibleSelection() {
|
||||
setTimeout(() => {
|
||||
const valid = isQuoteSelectionValid();
|
||||
if (isSelecting || !valid) {
|
||||
removePopover();
|
||||
return;
|
||||
}
|
||||
|
||||
const selection = document.getSelection();
|
||||
const selectionStr = selection.toString().trim();
|
||||
if (selection.isCollapsed || selectionStr === "") {
|
||||
removePopover();
|
||||
return;
|
||||
}
|
||||
|
||||
showPopover();
|
||||
}, 50)
|
||||
}
|
||||
|
||||
function removePopover() {
|
||||
quotePopover?.hidePopover();
|
||||
}
|
||||
|
||||
function createPopover() {
|
||||
quotePopover = document.createElement("div");
|
||||
quotePopover.popover = "auto";
|
||||
quotePopover.className = "quote-popover";
|
||||
|
||||
const quoteButton = document.createElement("button");
|
||||
quoteButton.textContent = "Quote fragment"
|
||||
quoteButton.className = "reduced"
|
||||
quotePopover.appendChild(quoteButton);
|
||||
|
||||
document.body.appendChild(quotePopover);
|
||||
return quoteButton;
|
||||
}
|
||||
|
||||
function showPopover() {
|
||||
if (!quotePopover) {
|
||||
const quoteButton = createPopover();
|
||||
quoteButton.addEventListener("click", () => {
|
||||
console.log("Quoting:", document.getSelection().toString());
|
||||
const postPermalink = quotedPostContainer.dataset.postPermalink;
|
||||
const authorUsername = quotedPostContainer.dataset.authorUsername;
|
||||
console.log(postPermalink, authorUsername);
|
||||
if (ta.value.trim() !== "") {
|
||||
ta.value += "\n"
|
||||
}
|
||||
ta.value += `[url=${postPermalink}]${authorUsername} said:[/url]\n[quote]${document.getSelection().toString()}[/quote]\n`;
|
||||
ta.scrollIntoView()
|
||||
ta.focus();
|
||||
|
||||
document.getSelection().empty();
|
||||
removePopover();
|
||||
})
|
||||
}
|
||||
|
||||
const range = document.getSelection().getRangeAt(0);
|
||||
const rect = range.getBoundingClientRect();
|
||||
const scrollY = window.scrollY || window.pageYOffset;
|
||||
|
||||
quotePopover.style.setProperty("top", `${rect.top + scrollY - 55}px`)
|
||||
quotePopover.style.setProperty("left", `${rect.left + rect.width/2}px`)
|
||||
|
||||
if (!quotePopover.matches(':popover-open')) {
|
||||
quotePopover.showPopover();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const deleteDialog = document.getElementById("delete-dialog");
|
||||
const deleteDialogCloseButton = document.getElementById("post-delete-dialog-close");
|
||||
|
Reference in New Issue
Block a user