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 WIDGET_TEXTAREA_ID = NAMESPACE + 'widget-textarea'; var DIALOG_ID = NAMESPACE + 'dialog'; var DIALOG_TEXTAREA_ID = NAMESPACE + 'dialog-textarea'; 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 widget editor. */ function load { document.getElementById(WIDGET_TEXTAREA_ID).value = loadNotes[PAGE_NAME] || ''; }

/** * Stores the contents of the editor into local storage. */ function save { var notes = loadNotes; var value = document.getElementById(WIDGET_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(WIDGET_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 clicked from the notes list. */ function onClickPage(ev) { onSelectPage(ev.target.dataset.page); }

/** * Fires when a page is selected. */ function onSelectPage(page) { var li = document.getElementById(NAMESPACE + 'page__' + selectedPage);

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

li = document.getElementById(NAMESPACE + 'page__' + page);

if (li) { li.classList.add('selected'); }

var textarea = document.getElementById(DIALOG_TEXTAREA_ID); textarea.disabled = !page;

if (page) { textarea.value = loadNotes[page] || ''; textarea.focus; }

selectedPage = page; document.getElementById(DIALOG_ID).showModal; }

/** * 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 {', ' box-sizing: border-box;', ' height: 2.5em;', ' margin: 0;', ' padding: 12px 24px;', ' border-bottom: solid var(--theme-accent-color) 1px;', ' color: var(--theme-page-text-color);', '}',   '#' + DIALOG_ID + ' .dialog-body {', ' box-sizing: border-box;', ' height: calc(100% - 2.5em - 18px);', ' position: relative;', '}',   '#' + DIALOG_ID + ' .pages-list {', ' box-sizing: border-box;', ' position: absolute;', ' top: 0;', ' left: 0;', ' 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);', '}',   '#' + DIALOG_ID + ' textarea {', ' display: block;', ' position: absolute;', ' top: 0;', ' right: 0;', ' box-sizing: border-box;', ' width: calc(35em - 6px);', ' height: 100%;', ' border: none;', ' border-left: solid var(--theme-accent-color) 1px;', ' resize: none;', ' font-family: inherit;', ' font-size: 100%;', ' color: var(--theme-page-text-color);', ' background-color: transparent;', '}',   '#' + DIALOG_ID + ' textarea:focus {', ' outline: none;', '}', ].join('\n'); document.head.appendChild(style); }

/** * Builds the notes editor. */ function buildWidget(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 = WIDGET_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); var textarea = document.createElement('textarea'); textarea.id = DIALOG_TEXTAREA_ID; textarea.autocomplete = false; textarea.placeholder = 'Whatever you write here is visible only to you.'; dialogBody.appendChild(textarea); }

function showDialog { var keys = Array.from(Object.keys(loadNotes)).sort; selectedPage = keys.indexOf(PAGE_NAME) === -1 ? keys[0] : PAGE_NAME; var ul = document.querySelector('#' + DIALOG_ID + ' ul'); ul.innerHTML = ''; 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', onClickPage); 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); }

onSelectPage(selectedPage); }

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

if (!railEl) { return; }

buildStylesheet; buildWidget(railEl); buildDialog; }

init; });