-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroot-commands.js
More file actions
133 lines (115 loc) · 3.5 KB
/
root-commands.js
File metadata and controls
133 lines (115 loc) · 3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import { ROOT_COMMANDS } from './consts.js';
import { _disableSource, _enableSource, _finalizeEvent } from './utils.js';
/**
* @typedef {Event & {source: Element, command: string}} CommandEvent
* @property {Element} source - The element that triggered the command
* @property {string} command - The command string
*/
let _commandRegistryOpen = true;
const _commands = new Map();
/**
* Checks if the registry is open
*
* @returns {boolean} If the registry is open
*/
export const isCommandRootRegistryOpen = () => _commandRegistryOpen;
/**
* Registers a command to be handled in a `CommandEvent`
*
* @param {string} command The command name
* @param {(event: CommandEvent) => void} callback The callback to call
*/
export function registerRootCommand(command, callback) {
if (typeof command !== 'string' || ! command.startsWith('--')) {
throw new TypeError(`Invalid command "${command}". Commands must be strings prefixed with "--".`);
} else if (typeof callback !== 'function') {
throw new TypeError('Callbacks for commands must be functions.');
} else if (_commands.has(command)) {
throw new Error(`Command "${command}" already registered.`);
} else if (! _commandRegistryOpen) {
throw new Error('Attempting to register command with a closed registry.');
} else {
_commands.set(command, callback);
}
}
/**
* Closes registration of new commands
*
* @returns {boolean} If the call closed the registry
*/
export function closeCommandRootRegistry() {
if (_commandRegistryOpen) {
_commandRegistryOpen = false;
return true;
} else {
return false;
}
}
/**
* Handles a `CommandEvent` with a built-in or registered handler
*
* @param {CommandEvent} event The event to handle
*/
export function handleRootCommand(event) {
if (! event.defaultPrevented) {
const [command, ...args] = event.command.split(':');
switch(command) {
case ROOT_COMMANDS.reload:
location.reload();
_finalizeEvent(event);
break;
case ROOT_COMMANDS.back:
history.back();
_finalizeEvent(event);
break;
case ROOT_COMMANDS.forward:
history.forward();
_finalizeEvent(event);
break;
case ROOT_COMMANDS.print:
globalThis.print();
_finalizeEvent(event);
break;
case ROOT_COMMANDS.share:
if (navigator.share instanceof Function) {
_disableSource(event);
_finalizeEvent(event);
const {
shareTitle: title = document.title,
shareUrl: url = location.href,
shareText: text,
} = event.source.dataset;
navigator.share({ title, url, text })
.then(() => _enableSource(event))
.catch(globalThis.reportError);
} else {
event.source.disabled = true;
}
break;
case ROOT_COMMANDS.exitFullscreen:
_finalizeEvent(event);
if (document.fullscreenElement instanceof Element) {
document.exitFullscreen();
}
break;
default:
if (_commands.has(command)) {
_disableSource(event);
_finalizeEvent(event);
Promise.try(_commands.get(command), event, ...args)
.catch(globalThis.reportError)
.finally(() => _enableSource(event));
}
}
}
}
/**
* Adds `command` listener on a given target/root element
*
* @param {object} config
* @param {Element} [options.target=document.documentElement] The target element/root (defaults to `<html>`)
* @param {AbortSignal} [options.signal] An optional `signal` to remove `command` listener
*/
export function initRootCommands({ target = document.documentElement, signal } = {}) {
target.addEventListener('command', handleRootCommand, { signal });
}