diff --git a/static/common.js b/static/common.js new file mode 100644 index 0000000..f3e3542 --- /dev/null +++ b/static/common.js @@ -0,0 +1,27 @@ +const $ = document.querySelector.bind(document) +const $$ = (...args) => Array.prototype.slice.call(document.querySelectorAll(...args)) +const isMac = /Macintosh/.test(window.navigator.userAgent) +const arrToStr = a => Array.isArray(a) ? a.join('') : a +const strToArr = a => Array.isArray(a) ? a : [a] + +const rrh = { + html(s, ...parts) { + s = s.reduce((acc, cur, i) => (`${acc}${cur}${parts[i] ? arrToStr(parts[i]) : ''}`), '') + const wrapper = document.createElement('div') + wrapper.innerHTML = s + return wrapper.children[0] + }, + + l10nMap: {}, + l10n(text, translations) { + // Choose the translation on load to be consistent with the + // server-rendered interface. + if (translations) { + translations.en = text + this.l10nMap[text] = translations[navigator.languages + .map(lang => lang.split('-')[0]) + .find(lang => translations[lang])] || text + } + return this.l10nMap[text] || text + }, +} \ No newline at end of file diff --git a/static/default.css b/static/default.css index 548b76f..bec3dea 100644 --- a/static/default.css +++ b/static/default.css @@ -309,7 +309,7 @@ kbd { top: 0; left: 50%; width: 100%; - max-width: 700px; + max-width: 800px; margin: 96px auto; padding: 24px; transform: translate(-50%, 0); @@ -357,9 +357,16 @@ kbd { } .shortcuts-help .dialog__content { - display: grid; - grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); - grid-column-gap: 32px; + columns: 300px 2; + column-gap: 32px; + overflow: hidden; +} + +.shortcuts-group { + /* Don't break a shortcut group between columns */ + break-inside: avoid; + page-break-inside: avoid; + overflow: hidden; } .shortcuts-group-heading { diff --git a/static/shortcuts.js b/static/shortcuts.js index ce10c74..010b8ff 100644 --- a/static/shortcuts.js +++ b/static/shortcuts.js @@ -1,369 +1,321 @@ -const $ = document.querySelector.bind(document); -const $$ = (...args) => Array.prototype.slice.call(document.querySelectorAll(...args)); +rrh.l10n('List of shortcuts', { ru: 'Горячие клавиши' }) +rrh.l10n('Close this dialog', { ru: 'Закрыть диалог' }) -const isMac = /Macintosh/.test(window.navigator.userAgent); +rrh.l10n('Common', { ru: 'Общее' }) +rrh.l10n('Home', { ru: 'Главная' }) +rrh.l10n('Hypha', { ru: 'Гифа' }) + +rrh.l10n('Editor', { ru: 'Редактор' }) + +rrh.l10n('Format', { ru: 'Форматирование' }) + +rrh.shortcuts = { + // map is the whole shortcut graph. active points to the node of the + // shortcut graph that's currently "selected". When the user presses a + // key, the code finds the key node in the active subgraph and sets + // active to the found node. On each key press we get a narrower view + // until we reach a leaf and execute the action. View this property in + // the JavaScript console of your browser for easier understanding. + map: {}, + groups: [], + + addGroup(group) { + this.groups.push(group) + }, + + addBindingToGroup(name, binding) { + let group = this.groups.find(group => group.name === name) + if (!group) { + console.warn('Shortcut group', name, 'not found') + return + } + group.bind(binding) + }, + + _register(shortcuts, action, other = {}) { + // shortcuts looks like this: ['g r', 'Ctrl+r'] + // Every item of shortcuts is a sequential key chord. + for (let chord of strToArr(shortcuts)) { + let leaf = this.map + let keys = chord.trim().split(' ') + for (let key of keys) { + // If there's no existing edge, create one + if (!leaf[key]) leaf[key] = {} + leaf = leaf[key] + if (leaf.action) throw new Error(`Shortcut ${chord} already exists`) + } + // Now we've traversed to the leaf. Bind the shortcut + leaf.action = action + Object.assign(leaf, other) + } + }, + + _handleKeyDown(event) { + if (event.defaultPrevented) return + if (['Control', 'Alt', 'Shift', 'Meta'].includes(event.key)) return + if ((!event.ctrlKey && !event.metaKey && !event.shiftKey && !event.altKey) && + event.target instanceof Node && isTextField(event.target)) return + + let shortcut = keyEventToShortcut(event) + + if (!this.active[shortcut]) { + this._resetActive() + return + } + + this.active = this.active[shortcut] + if (this.active.action && (!this.active.element || event.target === this.active.element)) { + event.stopPropagation() + if (this.active.force) event.preventDefault() + this.active.action(event) + this._resetActive() + return + } + + if (this.timeout) clearTimeout(this.timeout) + this.timeout = window.setTimeout(() => this._resetActive(), 1500) + }, + + _resetActive() { + this.active = this.map + if (this.timeout) { + clearTimeout(this.timeout) + this.timeout = null + } + }, +} +window.addEventListener('keydown', event => rrh.shortcuts._handleKeyDown(event)) +rrh.shortcuts._resetActive() + +// Convert a KeyboardEvent into a shortcut string for matching by +// ShortcutHandler. function keyEventToShortcut(event) { - let elideShift = event.key.toUpperCase() === event.key && event.shiftKey; + let elideShift = event.key.toUpperCase() === event.key && event.shiftKey return (event.ctrlKey ? 'Ctrl+' : '') + (event.altKey ? 'Alt+' : '') + (event.metaKey ? 'Meta+' : '') + (!elideShift && event.shiftKey ? 'Shift+' : '') + - (event.key === ',' ? 'Comma' : event.key === ' ' ? 'Space' : event.key); + (event.key === ',' ? 'Comma' : event.key === ' ' ? 'Space' : event.key) } +// Prettify the shortcut string by replacing modifiers and arrow codes with +// Unicode symbol for presentation to the user. function prettifyShortcut(shortcut) { - let keys = shortcut.split('+'); + let keys = shortcut.split('+') if (isMac) { - let cmdIdx = keys.indexOf('Meta'); + let cmdIdx = keys.indexOf('Meta') if (cmdIdx !== -1 && keys.length - cmdIdx > 2) { - let tmp = keys[cmdIdx + 1]; - keys[cmdIdx + 1] = 'Meta'; - keys[cmdIdx] = tmp; + let tmp = keys[cmdIdx + 1] + keys[cmdIdx + 1] = 'Meta' + keys[cmdIdx] = tmp } } - let lastKey = keys[keys.length - 1]; + // Add Shift into shortcut strings like Ctrl+L + let lastKey = keys[keys.length - 1] if (!keys.includes('Shift') && lastKey.toUpperCase() === lastKey && lastKey.toLowerCase() !== lastKey) { - keys.splice(keys.length - 1, 0, 'Shift'); + keys.splice(keys.length - 1, 0, 'Shift') } for (let i = 0; i < keys.length; i++) { if (isMac) { - switch (keys[i]) { - case 'Ctrl': keys[i] = '⌃'; break; - case 'Alt': keys[i] = '⌥'; break; - case 'Shift': keys[i] = '⇧'; break; - case 'Meta': keys[i] = '⌘'; break; - } + if (keys[i] === 'Ctrl') keys[i] = '⌃' + if (keys[i] === 'Alt') keys[i] = '⌥' + if (keys[i] === 'Shift') keys[i] = '⇧' + if (keys[i] === 'Meta') keys[i] = '⌘' + } else { + if (keys[i] === 'Meta') keys[i] = 'Win' } if (i === keys.length - 1 && i > 0 && keys[i].length === 1) { - keys[i] = keys[i].toUpperCase(); + // Make every key uppercase. This does not introduce any ambiguous + // cases because we insert a Shift modifier for upper-case keys + // earlier. + keys[i] = keys[i].toUpperCase() } - switch (keys[i]) { - case 'ArrowLeft': keys[i] = '←'; break; - case 'ArrowRight': keys[i] = '→'; break; - case 'ArrowUp': keys[i] = '↑'; break; - case 'ArrowDown': keys[i] = '↓'; break; - case 'Comma': keys[i] = ','; break; - case 'Enter': keys[i] = '↩'; break; - case ' ': keys[i] = 'Space'; break; - } - - keys[i] = `${keys[i]}`; + if (keys[i] === 'ArrowLeft') keys[i] = '←' + if (keys[i] === 'ArrowRight') keys[i] = '→' + if (keys[i] === 'ArrowUp') keys[i] = '↑' + if (keys[i] === 'ArrowDown') keys[i] = '↓' + if (keys[i] === 'Comma') keys[i] = ',' + if (keys[i] === 'Enter') keys[i] = '↩' + if (keys[i] === ' ') keys[i] = 'Space' + keys[i] = `${keys[i]}` } - return keys.join(isMac ? '' : ' + '); + return keys.join(isMac ? '' : ' + ') } function isTextField(element) { - let name = element.nodeName.toLowerCase(); + let name = element.nodeName.toLowerCase() return name === 'textarea' || name === 'select' || (name === 'input' && !['submit', 'reset', 'checkbox', 'radio'].includes(element.type)) || - element.isContentEditable; + element.isContentEditable } -let notTextField = event => !(event.target instanceof Node && isTextField(event.target)); +class Shortcut { + constructor(shortcuts, target, description, other) { + this.shortcuts = shortcuts + this.target = target + this.description = rrh.l10n(description) + Object.assign(this, other) + } +} -let allShortcuts = []; -let shortcutsGroup = null; - -class ShortcutHandler { - constructor(element, override, filter = () => true) { - this.element = element; - this.map = {}; - this.active = this.map; - this.override = override; - this.filter = filter; - this.timeout = null; - - this.handleKeyDown = this.handleKeyDown.bind(this); - this.resetActive = this.resetActive.bind(this); - this.addEventListeners(); +class ShortcutGroup { + constructor(name, element = null, bindings = []) { + this.name = rrh.l10n(name) + this.shortcuts = [] + this.element = element + bindings.forEach(binding => this.bind(binding)) } - addEventListeners() { - this.element.addEventListener('keydown', this.handleKeyDown); - } - - add(text, action, description = null, shownInHelp = true) { - let shortcuts = text.trim().split(',').map(shortcut => shortcut.trim().split(' ')); - - if (shortcutsGroup && shownInHelp) { - shortcutsGroup.push({ - action, - shortcut: text, + bind({ shortcuts, target, description, ...other }) { + shortcuts = strToArr(shortcuts).map(s => s.trim()) + if (!other.element) other.element = this.element + if (target instanceof Function) { + this.shortcuts.push({ shortcuts, description }) + rrh.shortcuts._register(shortcuts, target, other) + } else if (target instanceof Node) { + this.bind({ + shortcuts, + target: () => { + if (isTextField(target)) target.focus() + target.click() + }, + description, ...other, + }) + } else if (Array.isArray(target) && (target.length === 0 || target[0] instanceof Node)) { + this.shortcuts.push({ + shortcuts: shortcuts.map(s => `${s} 1 — 9`), description, }) - } - - for (let shortcut of shortcuts) { - let node = this.map; - for (let key of shortcut) { - if (!node[key]) { - node[key] = {}; - } - node = node[key]; - if (node.action) { - delete node.action; - delete node.shortcut; - delete node.description; - } + for (let i = 0; i < target.length && i < 9; i++) { + let element = target[i] + rrh.shortcuts._register(shortcuts.map(s => `${s} ${i + 1}`), () => { + if (isTextField(element)) element.focus() + else element.click() + }, other) } - - node.action = action; - node.shortcut = shortcut; - node.description = description; - } - } - - group(...args) { - if (typeof args[0] === 'string') this.fakeItem(args.shift()); - shortcutsGroup = []; - - args[0].bind(this)(); - - if (shortcutsGroup && shortcutsGroup.length) allShortcuts.push(shortcutsGroup); - shortcutsGroup = null; - } - - bindElement(shortcut, element, ...other) { - element = typeof element === 'string' ? $(element) : element; - if (!element) return; - this.add(shortcut, () => { - if (isTextField(element)) { - element.focus(); - } else { - element.click(); - } - }, ...other); - } - - bindLink(shortcut, link, ...other) { - this.add(shortcut, () => window.location.href = link, ...other); - } - - bindCollection(prefix, elements, collectionDescription, itemDescription) { - this.fakeItem(prefix + ' 1 – 9', collectionDescription); - - if (typeof elements === 'string') { - elements = $$(elements); - } else if (Array.isArray(elements)) { - elements = elements.map(el => typeof el === 'string' ? $(el) : el); - } - - for (let i = 1; i <= elements.length && i < 10; i++) { - this.bindElement(`${prefix} ${i}`, elements[i - 1], `${itemDescription} #${i}`, false); - } - } - - fakeItem(shortcut, description = null) { - let list = shortcutsGroup || allShortcuts; - list.push({ - shortcut: description ? shortcut : null, - description: description || shortcut, - }); - } - - handleKeyDown(event) { - if (event.defaultPrevented) return; - if (['Control', 'Alt', 'Shift', 'Meta'].includes(event.key)) return; - if (!this.filter(event)) return; - - let shortcut = keyEventToShortcut(event); - - if (!this.active[shortcut]) { - this.resetActive(); - return; - } - - this.active = this.active[shortcut]; - if (this.active.action) { - event.stopPropagation(); - this.active.action(event); - if (this.override) event.preventDefault(); - this.resetActive(); - return; - } - - if (this.timeout) clearTimeout(this.timeout); - this.timeout = window.setTimeout(this.resetActive, 1500); - } - - resetActive() { - this.active = this.map; - if (this.timeout) { - clearTimeout(this.timeout) - this.timeout = null; + } else if (typeof target === 'string') { + this.bind({ + shortcuts, + target: () => window.location.href = target, + description, + ...other, + }) + } else if (target !== undefined && target !== null) { + throw new Error('Invalid target type') } } } -class ShortcutsHelpDialog { - constructor() { - let template = $('#dialog-template'); - let clonedTemplate = template.content.cloneNode(true); - this.backdrop = clonedTemplate.children[0]; - this.dialog = clonedTemplate.children[1]; +function openHelp() { + if ($('.shortcuts-help')) return - this.dialog.classList.add('shortcuts-help'); - this.dialog.hidden = true; - this.backdrop.hidden = true; + document.body.overflow = 'hidden' + let prevActiveElement = document.activeElement - document.body.appendChild(this.backdrop); - document.body.appendChild(this.dialog); + let backdrop = rrh.html`
` + backdrop.onclick = close + document.body.appendChild(backdrop) - this.close = this.close.bind(this); + let dialog = rrh.html` +