Move sticky note element to context

This commit is contained in:
Idrees Hassan
2025-11-13 22:53:44 -05:00
parent fbbfd25f30
commit 3ec124a1b3
9 changed files with 93 additions and 38 deletions

View File

@@ -900,6 +900,14 @@
return window.location.href;
}
/**
* @returns {HTMLElement} The current active page element where sticky notes can be applied
*/
getActivePage() {
// Default to root element
return document.documentElement;
}
/**
* Checks if a path is applicable given the context
* @param {string} path Can be a site URL or another context-specific path
@@ -1173,11 +1181,12 @@
/**
* @param {StickyNote} stickyNote
* @param {HTMLElement} page
* @param {() => void} onSave
* @param {() => void} onDelete
* @returns {HTMLElement}
*/
function renderStickyNote(stickyNote, onSave, onDelete) {
function renderStickyNote(stickyNote, page, onSave, onDelete) {
const noteElement = makeElement("birb-window");
noteElement.classList.add("birb-sticky-note");
@@ -1202,7 +1211,7 @@
noteElement.style.top = `${stickyNote.top}px`;
noteElement.style.left = `${stickyNote.left}px`;
document.body.appendChild(noteElement);
page.appendChild(noteElement);
makeDraggable(header, true, (top, left) => {
stickyNote.top = top;
@@ -1253,10 +1262,11 @@
const existingNotes = document.querySelectorAll(".birb-sticky-note");
existingNotes.forEach(note => note.remove());
// Render all sticky notes
const pageElement = getContext().getActivePage();
const context = getContext();
for (let stickyNote of stickyNotes) {
if (context.isPathApplicable(stickyNote.site)) {
renderStickyNote(stickyNote, onSave, () => onDelete(stickyNote));
renderStickyNote(stickyNote, pageElement, onSave, () => onDelete(stickyNote));
}
}
}
@@ -1270,9 +1280,10 @@
const id = Date.now().toString();
const site = getContext().getPath();
const stickyNote = new StickyNote(id, site, "");
const element = renderStickyNote(stickyNote, onSave, () => onDelete(stickyNote));
element.style.left = `${window.innerWidth / 2 - element.offsetWidth / 2}px`;
element.style.top = `${window.scrollY + window.innerHeight / 2 - element.offsetHeight / 2}px`;
const page = getContext().getActivePage();
const element = renderStickyNote(stickyNote, page, onSave, () => onDelete(stickyNote));
element.style.left = `${page.clientWidth / 2 - element.offsetWidth / 2}px`;
element.style.top = `${page.scrollTop + page.clientHeight / 2 - element.offsetHeight / 2}px`;
stickyNote.top = parseInt(element.style.top, 10);
stickyNote.left = parseInt(element.style.left, 10);
stickyNotes.push(stickyNote);
@@ -1946,7 +1957,7 @@
insertModal(`${birdBirb()} Mode`, message);
}),
new Separator(),
new MenuItem("2025.11.13.64", () => { alert("Thank you for using Pocket Bird! You are on version: 2025.11.13.64"); }, false),
new MenuItem("2025.11.13.80", () => { alert("Thank you for using Pocket Bird! You are on version: 2025.11.13.80"); }, false),
];
const styleElement = document.createElement("style");