User:AlternateTorg/PersonalNotes/draft.js

/* * This script adds a "Personal Notes" editor to the right rail on the page. * Anything written in this editor is stored locally in the user's browser, and * is only visible to that user. */ (function { 'use strict'; var NAMESPACE = '__com_walkertribe_rjw_fandom-personal-notes__'; var COMMUNITY_ID = NAMESPACE + mw.config.get('wgDBname'); var PAGE_NAME = mw.config.get('wgPageName'); var RIGHT_RAIL_SELECTOR = 'aside .right-rail-wrapper'; var TEXTAREA_ID = NAMESPACE + 'textarea'; var DIALOG_ID = NAMESPACE + 'dialog'; var MIN_TEXTAREA_HEIGHT = 100; var AUTO_SAVE_DELAY_MS = 1000;

var timer; var selectedPage;

/** * Loads all the notes for this community. The return value is an object where * the notes for each page are keyed under that page's name. * * @returns {Object} - the notes */ function loadNotes { var data = localStorage.getItem(COMMUNITY_ID); return data ? JSON.parse(data) : {}; }

/** * Loads any stored data for this page into the editor. */ function load { document.getElementById(TEXTAREA_ID).value = loadNotes[PAGE_NAME] || ''; }

/** * Stores the contents of the editor into local storage. */ function save { var notes = loadNotes; var value = document.getElementById(TEXTAREA_ID).value;

if (value.length) { notes[PAGE_NAME] = value; } else { delete notes[PAGE_NAME]; }

localStorage.setItem(COMMUNITY_ID, JSON.stringify(notes)); }

/** * Fires when the editor loses focus. Saves the contents. */ function onBlur { clearTimeout(timer); save; }

/** * Fires when the editor contents change. Auto-adjusts the height of the field, * and sets a timer to save the contents if the user stops typing. */ function onInput { clearTimeout(timer); var editor = document.getElementById(TEXTAREA_ID); editor.style.height = 'auto'; editor.style.height = Math.max(editor.scrollHeight, MIN_TEXTAREA_HEIGHT) + 'px'; timer = setTimeout(save, AUTO_SAVE_DELAY_MS); }

/** * Fires when a page is selected from the notes list. */ function onSelectPage(ev) { var page = ev.target.dataset.page; var oldLi = document.getElementById(NAMESPACE + 'page__' + selectedPage);

if (oldLi) { oldLi.classList.remove('selected'); }

document.getElementById(NAMESPACE + 'page__' + page).classList.add('selected'); selectedPage = page; }

/** * Injects the stylesheet into the page. */ function buildStylesheet { var style = document.createElement('style'); style.textContent = [ '/* PersonalNotes styles */', '.' + NAMESPACE + 'widget {', ' margin-top: 24px;', ' border-bottom: 1px solid var(--theme-border-color);', '}', 	'.' + NAMESPACE + 'widget textarea {', ' display: block;', ' box-sizing: border-box;', ' width: 100%;', ' resize: none;', ' overflow: hidden;', ' font-family: inherit;', ' font-size: 100%;', ' color: var(--theme-page-text-color);', ' background-color: transparent;', ' border-color: transparent;', '}',   '.' + NAMESPACE + 'widget textarea:focus {', ' border-color: unset;', '}',   '.' + NAMESPACE + 'widget .button-panel {', ' text-align: right;', ' margin: 3px 0 24px 0;', '}',   '.' + NAMESPACE + 'widget button {', ' border: none;', ' color: var(--theme-page-background-color);', ' background-color: var(--theme-accent-color);', ' border-radius: 6px;', ' padding: 3px 12px;', ' cursor: pointer;', '}',   '#' + DIALOG_ID + ' {', ' box-sizing: border-box;', ' position: fixed;', ' height: 50%;', ' width: 60em;', ' background-color: var(--theme-page-background-color);', ' box-shadow: 0 0 3em #000;', ' border: solid var(--theme-accent-color) 3px;', ' border-radius: 6px;', ' padding: 0;', ' overflow: hidden;', '}',   '#' + DIALOG_ID + '::backdrop {', ' background-color: rgba(0, 0, 0, 0.7);', '}',   '#' + DIALOG_ID + ' .close {', ' box-sizing: border-box;', ' position: absolute;', ' width: 2em;', ' text-align: center;', ' top: 0;', ' right: 0;', ' font-size: 2em;', ' color: var(--theme-page-text-color);', ' cursor: pointer;', '}',   '#' + DIALOG_ID + ' h2 {', ' height: 2.5em;', ' margin: 0;', ' padding: 12px 24px;', ' color: var(--theme-page-text-color);', '}',   '#' + DIALOG_ID + ' .dialog-body {', ' height: calc(100% - 2.5em);', '}',   '#' + DIALOG_ID + ' .pages-list {', ' box-sizing: border-box;', ' width: 25em;', ' height: 100%;', ' overflow-x: hidden;', ' overflow-y: auto;', ' color: var(--theme-page-text-color);', '}',   '#' + DIALOG_ID + ' .pages-list li {', ' padding: 6px 48px 6px 24px;', ' line-height: 0.95;', ' text-indent: -12px;', ' cursor: pointer;', ' position: relative;', '}',   '#' + DIALOG_ID + ' .pages-list li.selected {', ' color: var(--theme-page-background-color);', ' background-color: var(--theme-accent-color);', '}',   '#' + DIALOG_ID + ' .pages-list li a {', ' position: absolute;', ' top: 0;', ' right: 0;', ' font-size: 2em;', ' display: none;', '}',   '#' + DIALOG_ID + ' .pages-list a:hover {', ' text-decoration: none;', '}',   '#' + DIALOG_ID + ' .pages-list li:not(.current):hover a {', ' display: block;', '}',   '#' + DIALOG_ID + ' .pages-list li.selected a {', ' color: var(--theme-page-background-color);', '}',   '#' + DIALOG_ID + ' .pages-list li.selected a:hover {', ' color: var(--theme-page-background-color);', '}', ].join('\n'); document.head.appendChild(style); }

/** * Builds the notes editor. */ function buildEditor(railEl) { var widgetDiv = document.createElement('div'); widgetDiv.className = NAMESPACE + 'widget'; var h2 = document.createElement('h2'); h2.className = 'rail-module__header'; h2.textContent = 'Personal Notes'; widgetDiv.appendChild(h2); var textarea = document.createElement('textarea'); textarea.id = TEXTAREA_ID; textarea.autocomplete = false; textarea.placeholder = 'Whatever you write here is visible only to you.'; widgetDiv.appendChild(textarea); var buttonPanel = document.createElement('div'); buttonPanel.className = 'button-panel'; var button = document.createElement('button'); button.textContent = 'View notes list'; button.addEventListener('click', showDialog); buttonPanel.appendChild(button); widgetDiv.appendChild(buttonPanel); railEl.insertBefore(widgetDiv, railEl.firstChild); load; onInput; textarea.addEventListener('input', onInput); textarea.addEventListener('blur', onBlur); }

/** * Build the notes dialog. */ function buildDialog { var dialog = document.createElement('dialog'); dialog.id = DIALOG_ID; var closeButton = document.createElement('div'); closeButton.className = 'close'; closeButton.textContent = '×'; closeButton.addEventListener('click', function { 	dialog.close;  }); dialog.appendChild(closeButton); var h2 = document.createElement('h2'); h2.className = 'mw-headline'; h2.textContent = 'Notes List'; dialog.appendChild(h2); var dialogBody = document.createElement('div'); dialogBody.className = 'dialog-body'; buildDialogContent(dialogBody); dialog.appendChild(dialogBody); document.body.appendChild(dialog); }

/** * Build the contents of the dialog. */ function buildDialogContent(dialogBody) { var pagesList = document.createElement('div'); pagesList.className = 'pages-list'; var ul = document.createElement('ul'); pagesList.appendChild(ul); dialogBody.appendChild(pagesList); }

function showDialog { selectedPage = PAGE_NAME; var ul = document.querySelector('#' + DIALOG_ID + ' ul'); ul.innerHTML = ''; var keys = Array.from(Object.keys(loadNotes)).sort; var li;

for (var i = 0; i < keys.length; i++) { var title = keys[i]; li = document.createElement('li'); li.id = NAMESPACE + 'page__' + title; li.dataset.page = title; li.textContent = title.replaceAll('_', ' '); li.addEventListener('click', onSelectPage); li.classList.toggle('current', title === PAGE_NAME); li.classList.toggle('selected', title === selectedPage); var a = document.createElement('a'); a.href = mw.util.getUrl(title); a.target = '_blank'; a.textContent = '⮫'; a.title = 'Open page'; a.addEventListener('click', function(ev) {     ev.stopPropagation;    }); li.appendChild(a); ul.appendChild(li); }

document.getElementById(DIALOG_ID).showModal; }

function init { var railEl = document.querySelector(RIGHT_RAIL_SELECTOR);

if (!railEl) { return; }

buildStylesheet; buildEditor(railEl); buildDialog; }

init; });