2017-11-28 22:46:42 +00:00
|
|
|
/* global applyOnMessage installed */
|
|
|
|
'use strict';
|
|
|
|
|
2017-12-05 21:14:21 +00:00
|
|
|
// eslint-disable-next-line no-var
|
|
|
|
var hotkeys = (() => {
|
|
|
|
let togglablesShown;
|
|
|
|
let togglables;
|
|
|
|
let enabled = false;
|
|
|
|
let ready = false;
|
2017-11-28 22:46:42 +00:00
|
|
|
|
2017-12-05 21:14:21 +00:00
|
|
|
window.addEventListener('showStyles:done', function _() {
|
|
|
|
window.removeEventListener('showStyles:done', _);
|
|
|
|
togglablesShown = true;
|
|
|
|
togglables = getTogglables();
|
|
|
|
ready = true;
|
|
|
|
setState(true);
|
|
|
|
initHotkeyInfo();
|
|
|
|
});
|
2017-11-28 22:46:42 +00:00
|
|
|
|
2017-12-08 20:16:59 +00:00
|
|
|
window.addEventListener('resize', adjustInfoPosition);
|
|
|
|
|
2017-12-05 21:14:21 +00:00
|
|
|
return {setState};
|
|
|
|
|
|
|
|
function setState(newState = !enabled) {
|
|
|
|
if (!ready) {
|
|
|
|
throw new Error('hotkeys no ready');
|
|
|
|
}
|
|
|
|
if (newState !== enabled) {
|
|
|
|
window[`${newState ? 'add' : 'remove'}EventListener`]('keydown', onKeyDown);
|
|
|
|
enabled = newState;
|
|
|
|
}
|
|
|
|
}
|
2017-11-28 22:46:42 +00:00
|
|
|
|
|
|
|
function onKeyDown(event) {
|
2017-12-05 21:14:21 +00:00
|
|
|
if (event.ctrlKey || event.altKey || event.metaKey || !enabled) {
|
2017-11-28 22:46:42 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
let entry;
|
2017-12-06 23:11:50 +00:00
|
|
|
const {which: k, key} = event;
|
|
|
|
if (key ? key >= '0' && key <= '9' : k >= 48 && k <= 57 || k >= 96 && k <= 105) {
|
2017-11-28 22:46:42 +00:00
|
|
|
// 0-9, numpad 0-9
|
2017-12-06 23:11:50 +00:00
|
|
|
const i = key === '0' ? 9 : key ? Number(key) - 1 : k === 48 || k === 96 ? 9 : k - (k > 96 ? 97 : 49);
|
|
|
|
entry = installed.children[i];
|
|
|
|
} else if (key ? key === '`' || key === '*' && !event.shiftKey : k === 192 || k === 106) {
|
2017-11-28 22:46:42 +00:00
|
|
|
// backtick ` and numpad *
|
|
|
|
invertTogglables();
|
2017-12-06 23:11:50 +00:00
|
|
|
} else if (key ? key === '-' : k === 109) {
|
2017-11-28 22:46:42 +00:00
|
|
|
// numpad -
|
|
|
|
toggleState(installed.children, 'enabled', false);
|
2017-12-06 23:11:50 +00:00
|
|
|
} else if (key ? key === '+' : k === 107) {
|
2017-11-28 22:46:42 +00:00
|
|
|
// numpad +
|
|
|
|
toggleState(installed.children, 'disabled', true);
|
2017-12-06 23:11:50 +00:00
|
|
|
} else if (key ? key.length === 1 : k >= 65 && k <= 90) {
|
|
|
|
// any single character
|
|
|
|
const letter = new RegExp(key ? '^' + key : '^\\x' + k.toString(16), 'i');
|
|
|
|
entry = [...installed.children].find(entry => letter.test(entry.textContent));
|
2017-11-28 22:46:42 +00:00
|
|
|
}
|
|
|
|
if (!entry) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const target = $(event.shiftKey ? '.style-edit-link' : '.checker', entry);
|
|
|
|
target.dispatchEvent(new MouseEvent('click'));
|
|
|
|
}
|
|
|
|
|
|
|
|
function getTogglables() {
|
|
|
|
const all = [...installed.children];
|
|
|
|
const enabled = [];
|
|
|
|
for (const entry of all) {
|
|
|
|
if (entry.classList.contains('enabled')) {
|
|
|
|
enabled.push(entry.id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return enabled.length ? enabled : all.map(entry => entry.id);
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
if (!match && $('.checker', entry).checked !== enable || entry.classList.contains(match)) {
|
|
|
|
results.push(entry.id);
|
|
|
|
task = task.then(() => saveStyleSafe({
|
|
|
|
id: entry.styleId,
|
|
|
|
enabled: enable,
|
|
|
|
notify: false,
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (results.length) {
|
|
|
|
task.then(refreshAllTabs);
|
|
|
|
}
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
|
|
|
function refreshAllTabs() {
|
|
|
|
getStylesSafe({matchUrl: location.href, enabled: true, asHash: true})
|
|
|
|
.then(styles => applyOnMessage({method: 'styleReplaceAll', styles}));
|
2017-12-09 15:15:57 +00:00
|
|
|
queryTabs().then(tabs =>
|
|
|
|
tabs.forEach(tab => (!FIREFOX || tab.width) &&
|
|
|
|
refreshTab(tab)));
|
|
|
|
}
|
|
|
|
|
|
|
|
function refreshTab(tab) {
|
|
|
|
const tabId = tab.id;
|
2017-12-09 18:19:03 +00:00
|
|
|
chrome.webNavigation.getAllFrames({tabId}, frames => {
|
|
|
|
frames = frames && frames[0] ? frames : [{frameId: 0}];
|
2017-12-09 15:15:57 +00:00
|
|
|
frames.forEach(({frameId}) =>
|
|
|
|
getStylesSafe({matchUrl: tab.url, enabled: true, asHash: true}).then(styles => {
|
|
|
|
const message = {method: 'styleReplaceAll', tabId, frameId, styles};
|
|
|
|
invokeOrPostpone(tab.active, sendMessage, message, ignoreChromeError);
|
|
|
|
if (frameId === 0) {
|
2017-11-28 22:46:42 +00:00
|
|
|
setTimeout(BG.updateIcon, 0, tab, styles);
|
2017-12-09 15:15:57 +00:00
|
|
|
}
|
2017-12-09 18:19:03 +00:00
|
|
|
}));
|
|
|
|
ignoreChromeError();
|
|
|
|
});
|
2017-11-28 22:46:42 +00:00
|
|
|
}
|
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;
|
2017-12-08 20:16:59 +00:00
|
|
|
window.addEventListener('resize', adjustInfoPosition);
|
2017-12-01 21:49:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function open() {
|
2017-12-08 20:16:59 +00:00
|
|
|
window.removeEventListener('resize', adjustInfoPosition);
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
const height = 4 +
|
|
|
|
container.firstElementChild.scrollHeight +
|
|
|
|
container.lastElementChild.scrollHeight +
|
2017-12-03 05:19:31 +00:00
|
|
|
parseFloat(getComputedStyle(container.lastElementChild).paddingBottom);
|
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-08 21:03:07 +00:00
|
|
|
const style = $('#hotkey-info').style;
|
2017-12-08 20:16:59 +00:00
|
|
|
if (installed.scrollHeight > installed.clientHeight) {
|
|
|
|
const entryRight = installed.firstElementChild.getBoundingClientRect().right;
|
|
|
|
style.setProperty('right', window.innerWidth - entryRight + 'px', 'important');
|
|
|
|
}
|
|
|
|
const installedBottom = installed.getBoundingClientRect().bottom + window.scrollY;
|
|
|
|
style.setProperty('bottom', window.innerHeight - installedBottom + 'px', 'important');
|
|
|
|
}
|
2017-12-05 21:14:21 +00:00
|
|
|
})();
|