2016-06-07 05:57:52 +08:00
|
|
|
import ko from 'ko';
|
|
|
|
|
2022-03-07 20:47:03 +08:00
|
|
|
let keyScopeFake = 'all';
|
2021-01-27 07:26:31 +08:00
|
|
|
|
2021-08-20 21:40:07 +08:00
|
|
|
export const
|
2022-02-24 18:19:19 +08:00
|
|
|
ScopeMenu = 'Menu',
|
2021-01-27 07:26:31 +08:00
|
|
|
|
2021-08-20 21:40:07 +08:00
|
|
|
doc = document,
|
2016-06-07 05:57:52 +08:00
|
|
|
|
2021-08-20 21:40:07 +08:00
|
|
|
$htmlCL = doc.documentElement.classList,
|
2021-02-15 22:20:22 +08:00
|
|
|
|
2021-08-20 21:40:07 +08:00
|
|
|
elementById = id => doc.getElementById(id),
|
2016-06-07 05:57:52 +08:00
|
|
|
|
2021-08-20 21:40:07 +08:00
|
|
|
Settings = rl.settings,
|
|
|
|
SettingsGet = Settings.get,
|
2022-02-08 20:48:11 +08:00
|
|
|
SettingsCapa = Settings.capa,
|
2016-06-07 05:57:52 +08:00
|
|
|
|
2022-03-08 17:52:08 +08:00
|
|
|
dropdowns = [],
|
2021-08-20 21:40:07 +08:00
|
|
|
dropdownVisibility = ko.observable(false).extend({ rateLimit: 0 }),
|
2021-02-04 06:36:36 +08:00
|
|
|
|
2021-08-20 21:40:07 +08:00
|
|
|
moveAction = ko.observable(false),
|
|
|
|
leftPanelDisabled = ko.observable(false),
|
2021-01-27 07:26:31 +08:00
|
|
|
|
2021-08-20 21:40:07 +08:00
|
|
|
createElement = (name, attr) => {
|
|
|
|
let el = doc.createElement(name);
|
|
|
|
attr && Object.entries(attr).forEach(([k,v]) => el.setAttribute(k,v));
|
|
|
|
return el;
|
|
|
|
},
|
2017-02-09 01:48:53 +08:00
|
|
|
|
2022-02-11 19:21:16 +08:00
|
|
|
fireEvent = (name, detail) => dispatchEvent(new CustomEvent(name, {detail:detail})),
|
|
|
|
|
2022-03-04 21:43:58 +08:00
|
|
|
formFieldFocused = () => doc.activeElement && doc.activeElement.matches('input,textarea'),
|
|
|
|
|
2022-03-09 22:08:22 +08:00
|
|
|
addShortcut = (...args) => shortcuts.add(...args),
|
|
|
|
|
2022-03-04 21:43:58 +08:00
|
|
|
registerShortcut = (keys, modifiers, scopes, method) =>
|
2022-03-09 22:08:22 +08:00
|
|
|
addShortcut(keys, modifiers, scopes, event => formFieldFocused() ? true : method(event)),
|
2022-03-04 21:43:58 +08:00
|
|
|
|
2022-02-24 18:19:19 +08:00
|
|
|
addEventsListener = (element, events, fn, options) =>
|
|
|
|
events.forEach(event => element.addEventListener(event, fn, options)),
|
|
|
|
|
|
|
|
addEventsListeners = (element, events) =>
|
|
|
|
Object.entries(events).forEach(([event, fn]) => element.addEventListener(event, fn)),
|
|
|
|
|
2022-03-07 20:47:03 +08:00
|
|
|
// keys / shortcuts
|
|
|
|
keyScopeReal = ko.observable('all'),
|
2021-08-20 21:40:07 +08:00
|
|
|
keyScope = value => {
|
2022-03-07 20:47:03 +08:00
|
|
|
if (!value) {
|
2021-07-21 20:14:31 +08:00
|
|
|
return keyScopeFake;
|
2021-01-27 07:26:31 +08:00
|
|
|
}
|
2022-03-07 20:47:03 +08:00
|
|
|
if (ScopeMenu !== value) {
|
|
|
|
keyScopeFake = value;
|
|
|
|
if (dropdownVisibility()) {
|
|
|
|
value = ScopeMenu;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
keyScopeReal(value);
|
|
|
|
shortcuts.setScope(value);
|
2021-07-21 20:14:31 +08:00
|
|
|
};
|
2021-08-20 21:40:07 +08:00
|
|
|
|
|
|
|
dropdownVisibility.subscribe(value => {
|
|
|
|
if (value) {
|
2022-02-24 18:19:19 +08:00
|
|
|
keyScope(ScopeMenu);
|
|
|
|
} else if (ScopeMenu === shortcuts.getScope()) {
|
2021-08-20 21:40:07 +08:00
|
|
|
keyScope(keyScopeFake);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-11-06 19:27:37 +08:00
|
|
|
leftPanelDisabled.toggle = () => leftPanelDisabled(!leftPanelDisabled());
|
2021-08-20 21:40:07 +08:00
|
|
|
leftPanelDisabled.subscribe(value => {
|
|
|
|
value && moveAction() && moveAction(false);
|
|
|
|
$htmlCL.toggle('rl-left-panel-disabled', value);
|
|
|
|
});
|
|
|
|
|
|
|
|
moveAction.subscribe(value => value && leftPanelDisabled() && leftPanelDisabled(false));
|