Add sticky note support to obsidian

This commit is contained in:
Idrees Hassan
2025-11-14 00:06:49 -05:00
parent 3ec124a1b3
commit 7639c7c36a
12 changed files with 253 additions and 68 deletions

View File

@@ -90,7 +90,7 @@ const UPDATE_INTERVAL = 1000 / 60; // 60 FPS
const AFK_TIME = isDebug() ? 0 : 1000 * 5;
const PET_BOOST_DURATION = 1000 * 60 * 5;
const PET_MENU_COOLDOWN = 1000;
const URL_CHECK_INTERVAL = 500;
const URL_CHECK_INTERVAL = 250;
const HOP_DELAY = 500;
// Random event chances per tick
@@ -431,7 +431,7 @@ Promise.all([
setInterval(() => {
const currentPath = getContext().getPath().split("?")[0];
if (currentPath !== lastPath) {
log("Path changed, updating sticky notes");
log("Path changed, updating sticky notes: " + currentPath);
lastPath = currentPath;
drawStickyNotes(stickyNotes, save, deleteStickyNote);
}

View File

@@ -1,6 +1,7 @@
import { debug, log, error } from "./shared.js";
const SAVE_KEY = "birbSaveData";
const ROOT_PATH = "";
/**
* @typedef {import('./application.js').BirbSaveData} BirbSaveData
@@ -231,7 +232,6 @@ class BrowserExtensionContext extends Context {
}
export class ObsidianContext extends Context {
/**
* @override
* @returns {boolean}
@@ -246,8 +246,12 @@ export class ObsidianContext extends Context {
* @returns {Promise<BirbSaveData|{}>}
*/
async getSaveData() {
// @ts-expect-error
return await OBSIDIAN_PLUGIN.loadData() ?? {};
return new Promise((resolve) => {
// @ts-expect-error
OBSIDIAN_PLUGIN.loadData().then((data) => {
resolve(data ?? {});
});
});
}
/**
@@ -256,7 +260,7 @@ export class ObsidianContext extends Context {
*/
async putSaveData(saveData) {
// @ts-expect-error
return await OBSIDIAN_PLUGIN.saveData(saveData);
await OBSIDIAN_PLUGIN.saveData(saveData);
}
/** @override */
@@ -277,8 +281,36 @@ export class ObsidianContext extends Context {
}
/** @override */
areStickyNotesEnabled() {
return false;
getPath() {
// @ts-expect-error
const file = app.workspace.getActiveFile();
if (file && this.getActiveEditorElement()) {
return file.path;
} else {
return ROOT_PATH;
}
}
/** @override */
getActivePage() {
if (this.getPath() === ROOT_PATH) {
// Root page, use document element
return document.documentElement
}
return this.getActiveEditorElement() ?? document.documentElement;
}
/** @override */
isPathApplicable(path) {
return path === this.getPath();
}
/** @returns {HTMLElement|null} */
getActiveEditorElement() {
// @ts-expect-error
const activeLeaf = app.workspace.activeLeaf;
const leafElement = activeLeaf?.view?.containerEl;
return leafElement?.querySelector(".cm-scroller") ?? null;
}
}

View File

@@ -67,8 +67,9 @@ export function onClick(element, action) {
* @param {HTMLElement|null} element The element to detect drag events on
* @param {boolean} [parent] Whether to move the parent element when the child is dragged
* @param {(top: number, left: number) => void} [callback] Callback for when element is moved
* @param {HTMLElement} [pageElement] The page element to constrain movement within
*/
export function makeDraggable(element, parent = true, callback = () => { }) {
export function makeDraggable(element, parent = true, callback = () => { }, pageElement) {
if (!element) {
return;
}
@@ -114,9 +115,12 @@ export function makeDraggable(element, parent = true, callback = () => { }) {
});
document.addEventListener("mousemove", (e) => {
const page = pageElement || document.documentElement;
const maxX = page.scrollWidth - elementToMove.clientWidth;
const maxY = page.scrollHeight - elementToMove.clientHeight;
if (isMouseDown) {
elementToMove.style.left = `${Math.max(0, e.clientX - offsetX)}px`;
elementToMove.style.top = `${Math.max(0, e.clientY - offsetY)}px`;
elementToMove.style.left = `${Math.max(0, Math.min(maxX, e.clientX - offsetX))}px`;
elementToMove.style.top = `${Math.max(0, Math.min(maxY, e.clientY - offsetY))}px`;
}
});

View File

@@ -69,7 +69,7 @@ export function renderStickyNote(stickyNote, page, onSave, onDelete) {
stickyNote.top = top;
stickyNote.left = left;
onSave();
});
}, page);
if (closeButton) {
makeClosable(() => {

View File

@@ -119,6 +119,7 @@
flex-grow: 1;
user-select: none;
color: var(--birb-background-color);
white-space: nowrap;
}
.birb-window-close {