2021-01-01 14:27:58 +00:00
|
|
|
/* global $ $$ $create */// dom.js
|
|
|
|
/* global API */// msg.js
|
|
|
|
/* global debounce */// toolbox.js
|
|
|
|
/* global t */// localization.js
|
2017-11-28 22:46:42 +00:00
|
|
|
'use strict';
|
|
|
|
|
2018-11-07 06:09:29 +00:00
|
|
|
const hotkeys = (() => {
|
2017-12-14 02:50:58 +00:00
|
|
|
const entries = document.getElementsByClassName('entry');
|
2021-01-01 14:27:58 +00:00
|
|
|
let togglablesShown = true;
|
|
|
|
let togglables = getTogglables();
|
|
|
|
let enabled;
|
|
|
|
|
|
|
|
window.on('resize', adjustInfoPosition);
|
|
|
|
initHotkeyInfo();
|
|
|
|
|
|
|
|
return {
|
|
|
|
setState(newState = !enabled) {
|
|
|
|
if (!newState !== !enabled) {
|
|
|
|
window[newState ? 'on' : 'off']('keydown', onKeyDown);
|
|
|
|
enabled = newState;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
2017-11-28 22:46:42 +00:00
|
|
|
|
|
|
|
function onKeyDown(event) {
|
2020-10-04 05:30:02 +00:00
|
|
|
if (event.ctrlKey || event.altKey || event.metaKey || !enabled ||
|
|
|
|
/^(text|search)$/.test((document.activeElement || {}).type)) {
|
2017-11-28 22:46:42 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
let entry;
|
2020-10-13 18:14:54 +00:00
|
|
|
let {key, code, shiftKey} = event;
|
2017-12-14 02:50:58 +00:00
|
|
|
|
2020-10-13 18:14:54 +00:00
|
|
|
if (key >= '0' && key <= '9') {
|
|
|
|
entry = entries[(Number(key) || 10) - 1];
|
|
|
|
} else if (code >= 'Digit0' && code <= 'Digit9') {
|
2017-12-14 02:50:58 +00:00
|
|
|
entry = entries[(Number(code.slice(-1)) || 10) - 1];
|
2020-10-13 18:14:54 +00:00
|
|
|
} else if (key === '`' || key === '*' || code === 'Backquote' || code === 'NumpadMultiply') {
|
2017-11-28 22:46:42 +00:00
|
|
|
invertTogglables();
|
2020-10-13 18:14:54 +00:00
|
|
|
} else if (key === '-' || code === 'NumpadSubtract') {
|
2017-12-14 02:50:58 +00:00
|
|
|
toggleState(entries, 'enabled', false);
|
2020-10-13 18:14:54 +00:00
|
|
|
} else if (key === '+' || code === 'NumpadAdd') {
|
2017-12-14 02:50:58 +00:00
|
|
|
toggleState(entries, 'disabled', true);
|
2020-10-13 18:14:54 +00:00
|
|
|
} else if (key.length === 1) {
|
|
|
|
shiftKey = false; // typing ':' etc. needs Shift so we hide it here to avoid opening editor
|
|
|
|
key = key.toLocaleLowerCase();
|
|
|
|
entry = [...entries].find(e => e.innerText.toLocaleLowerCase().startsWith(key));
|
2017-11-28 22:46:42 +00:00
|
|
|
}
|
|
|
|
if (!entry) {
|
|
|
|
return;
|
|
|
|
}
|
2020-11-20 14:00:20 +00:00
|
|
|
const target = $(shiftKey ? '.style-edit-link' : 'input', entry);
|
2017-12-14 02:50:58 +00:00
|
|
|
target.dispatchEvent(new MouseEvent('click', {cancelable: true}));
|
2017-11-28 22:46:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function getTogglables() {
|
2017-12-14 02:50:58 +00:00
|
|
|
const enabledOrAll = $('.entry.enabled') ? $$('.entry.enabled') : [...entries];
|
|
|
|
return enabledOrAll.map(entry => entry.id);
|
2017-11-28 22:46:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function countEnabledTogglables() {
|
|
|
|
let num = 0;
|
|
|
|
for (const id of togglables) {
|
|
|
|
num += $(`#${id}`).classList.contains('enabled');
|
|
|
|
}
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
|
|
|
function invertTogglables() {
|
|
|
|
togglables = togglables.length ? togglables : getTogglables();
|
|
|
|
togglablesShown = countEnabledTogglables() > togglables.length / 2;
|
|
|
|
toggleState(togglables, null, !togglablesShown);
|
|
|
|
togglablesShown = !togglablesShown;
|
|
|
|
}
|
|
|
|
|
|
|
|
function toggleState(list, match, enable) {
|
|
|
|
const results = [];
|
|
|
|
let task = Promise.resolve();
|
|
|
|
for (let entry of list) {
|
|
|
|
entry = typeof entry === 'string' ? $('#' + entry) : entry;
|
2020-11-20 14:00:20 +00:00
|
|
|
if (!match && $('input', entry).checked !== enable || entry.classList.contains(match)) {
|
2017-11-28 22:46:42 +00:00
|
|
|
results.push(entry.id);
|
2018-11-07 06:09:29 +00:00
|
|
|
task = task
|
2021-01-01 14:27:58 +00:00
|
|
|
.then(() => API.styles.toggle(entry.styleId, enable))
|
2018-11-07 06:09:29 +00:00
|
|
|
.then(() => {
|
|
|
|
entry.classList.toggle('enabled', enable);
|
|
|
|
entry.classList.toggle('disabled', !enable);
|
2020-11-20 14:00:20 +00:00
|
|
|
$('input', entry).checked = enable;
|
2018-11-07 06:09:29 +00:00
|
|
|
});
|
2017-11-28 22:46:42 +00:00
|
|
|
}
|
|
|
|
}
|
2018-01-03 15:26:31 +00:00
|
|
|
if (results.length) task.then(API.refreshAllTabs);
|
2017-11-28 22:46:42 +00:00
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
2017-12-01 21:49:59 +00:00
|
|
|
function initHotkeyInfo() {
|
|
|
|
const container = $('#hotkey-info');
|
|
|
|
let title;
|
|
|
|
container.onclick = ({target}) => {
|
|
|
|
if (target.localName === 'button') {
|
|
|
|
close();
|
|
|
|
} else if (!container.dataset.active) {
|
|
|
|
open();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
function close() {
|
|
|
|
delete container.dataset.active;
|
|
|
|
document.body.style.height = '';
|
|
|
|
container.title = title;
|
2021-01-01 14:27:58 +00:00
|
|
|
window.on('resize', adjustInfoPosition);
|
2017-12-01 21:49:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function open() {
|
2021-01-01 14:27:58 +00:00
|
|
|
window.off('resize', adjustInfoPosition);
|
2017-12-08 20:16:59 +00:00
|
|
|
debounce.unregister(adjustInfoPosition);
|
2017-12-01 21:49:59 +00:00
|
|
|
title = container.title;
|
|
|
|
container.title = '';
|
2017-12-08 20:16:59 +00:00
|
|
|
container.style = '';
|
2017-12-01 21:49:59 +00:00
|
|
|
container.dataset.active = true;
|
|
|
|
if (!container.firstElementChild) {
|
|
|
|
buildElement();
|
|
|
|
}
|
2017-12-12 14:04:58 +00:00
|
|
|
const height = 3 +
|
2017-12-01 21:49:59 +00:00
|
|
|
container.firstElementChild.scrollHeight +
|
2017-12-12 14:04:58 +00:00
|
|
|
container.lastElementChild.scrollHeight;
|
2017-12-01 21:49:59 +00:00
|
|
|
if (height > document.body.clientHeight) {
|
|
|
|
document.body.style.height = height + 'px';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function buildElement() {
|
|
|
|
const keysToElements = line =>
|
|
|
|
line
|
|
|
|
.split(/(<.*?>)/)
|
|
|
|
.map(s => (!s.startsWith('<') ? s :
|
2017-12-03 21:12:09 +00:00
|
|
|
$create('mark', s.slice(1, -1))));
|
2017-12-01 21:49:59 +00:00
|
|
|
const linesToElements = text =>
|
|
|
|
text
|
|
|
|
.trim()
|
|
|
|
.split('\n')
|
|
|
|
.map((line, i, array) =>
|
2017-12-03 21:12:09 +00:00
|
|
|
$create(i < array.length - 1 ? {
|
2017-12-01 21:49:59 +00:00
|
|
|
tag: 'p',
|
|
|
|
appendChild: keysToElements(line),
|
|
|
|
} : {
|
|
|
|
tag: 'a',
|
|
|
|
target: '_blank',
|
|
|
|
href: 'https://github.com/openstyles/stylus/wiki/Popup',
|
|
|
|
textContent: line,
|
|
|
|
}));
|
|
|
|
[
|
|
|
|
linesToElements(t('popupHotkeysInfo')),
|
2017-12-03 21:12:09 +00:00
|
|
|
$create('button', t('confirmOK')),
|
2017-12-01 21:49:59 +00:00
|
|
|
].forEach(child => {
|
2017-12-03 21:12:09 +00:00
|
|
|
container.appendChild($create('div', child));
|
2017-12-01 21:49:59 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2017-12-08 20:16:59 +00:00
|
|
|
|
|
|
|
function adjustInfoPosition(debounced) {
|
|
|
|
if (debounced !== true) {
|
|
|
|
debounce(adjustInfoPosition, 100, true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2017-12-05 21:14:21 +00:00
|
|
|
})();
|
2021-01-01 14:27:58 +00:00
|
|
|
|
|
|
|
hotkeys.setState(true);
|