webmaster442 / ultimatedotnetcheatsheet

The ultimate .NET cheat sheet
https://webmaster442.github.io/ultimatedotnetcheatsheet/
Creative Commons Attribution Share Alike 4.0 International
18 stars 5 forks source link

Bookmark manager and context menu #9

Closed webmaster442 closed 6 months ago

webmaster442 commented 6 months ago
function notify(message) {
    let notify = document.getElementById('notify');
    notify.style.display = 'block';
    notify.innerText = message;
    setTimeout(() => {
        notify.style.display = 'none';
    }, 800);
}

function bookmarkManager(storagePrefix) {
    if (!storagePrefix) {
        throw new Error('Storage prefix is required');
    }

    this.storagePrefix = storagePrefix;

    this.getCurrentTimeString = function () {
        const now = new Date();

        const year = now.getFullYear();
        const month = String(now.getMonth() + 1).padStart(2, '0');
        const day = String(now.getDate()).padStart(2, '0');
        const hour = String(now.getHours()).padStart(2, '0');
        const minute = String(now.getMinutes()).padStart(2, '0');

        return `${year}-${month}-${day} ${hour}:${minute}`;
    }

    this.storeBookmark = function () {
        const bookmarkName = prompt('Bookmark name to save:', this.getCurrentTimeString());
        if (!bookmarkName) {
            return;
        }

        const bookMark = {
            y: window.scrollY,
            x: window.scrollX,
            w: window.innerWidth,
            h: window.innerHeight
        }

        const name = this.storagePrefix + '_' + bookmarkName;

        localStorage.setItem(name, JSON.stringify(bookMark));
    }

    this.listBookmarks = function () {
        const bookmarks = Object.keys(localStorage).filter(key =>
            key.startsWith(this.storagePrefix)
        );
        return bookmarks.map(key => key.replace(this.storagePrefix, ''));
    }

    this.restoreBookmark = function (bookMarkName) {

        const name = this.storagePrefix + '_' + bookMarkName;

        const bookMark = JSON.parse(localStorage.getItem(name));
        if (!bookMark) {
            notify('Bookmark was not found');
            return;
        }

        window.resizeTo(bookMark.w, bookMark.h);
        window.scrollTo(bookMark.x, bookMark.y);
    }
}

if ('serviceWorker' in navigator) {
    navigator.serviceWorker.register('serviceworker.js', { scope: '/ultimatedotnetcheatsheet/' });
}

window.onscroll = function () {
    const scrollBtn = document.getElementById('navigate-top');
    if (document.body.scrollTop > 100
        || document.documentElement.scrollTop > 100) {
        scrollBtn.style.display = 'block';
    }
    else {
        scrollBtn.style.display = 'none';
    }
};

function scrollToTop() {
    document.body.scrollTop = 0;
    document.documentElement.scrollTop = 0;
}

document.addEventListener("DOMContentLoaded", applyCopyButton);

function applyCopyButton() {
    const preElements = document.querySelectorAll('pre');

    preElements.forEach(pre => {
        const copyButton = document.createElement('button');
        copyButton.classList.add('copyButton');
        copyButton.textContent = 'Copy to clipboard';
        pre.appendChild(copyButton);

        copyButton.addEventListener('click', () => {
            const codeToCopy = pre.firstChild.textContent;
            navigator.clipboard.writeText(codeToCopy).then(() => {
                notify('Code copied to clipboard');
            }).catch(err => {
                notify('Failed to copy code: ' + err);
            });
        });
    });
}