-
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathPage.razor.js
More file actions
175 lines (145 loc) · 5.54 KB
/
Copy pathPage.razor.js
File metadata and controls
175 lines (145 loc) · 5.54 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
export function registerEventListeners(dotNetObj) {
const keyDownHandler = (/** @type {KeyboardEvent} */ e) => {
const ctrl = (e.ctrlKey || e.metaKey);
if (ctrl && e.key === 's') {
e.preventDefault();
dotNetObj.invokeMethodAsync('CompileAndRenderAsync');
} else if (ctrl && e.key === ';') {
e.preventDefault();
// Instead of just copying the URL directly in JavaScript,
// invoke the.NET method so the URL is updated to reflect the current state and
// the UI displays "copied" checkmark afterwards.
dotNetObj.invokeMethodAsync('CopyUrlToClipboardAsync');
}
};
let lastClipboardText = null;
let clipboardTimerId = null;
let clipboardMonitoringStarted = false;
const readClipboard = async () => {
try {
const text = await navigator.clipboard.readText();
if (text !== lastClipboardText) {
lastClipboardText = text;
dotNetObj.invokeMethodAsync('OnClipboardTextChanged', text);
}
} catch (e) {
console.error(e);
}
};
const focusHandler = () => {
if (clipboardTimerId === null) {
clipboardTimerId = setInterval(readClipboard, 1000);
}
readClipboard();
};
const blurHandler = () => {
if (clipboardTimerId !== null) {
clearInterval(clipboardTimerId);
clipboardTimerId = null;
}
};
const startClipboardMonitoring = () => {
if (clipboardMonitoringStarted) {
return;
}
clipboardMonitoringStarted = true;
window.addEventListener('focus', focusHandler);
window.addEventListener('blur', blurHandler);
if (document.hasFocus()) {
clipboardTimerId = setInterval(readClipboard, 1000);
readClipboard();
}
};
document.addEventListener('keydown', keyDownHandler);
// Monitor clipboard but only once we have been granted permissions
// (to avoid obtrusive permission popups).
if (navigator.permissions?.query) {
navigator.permissions.query({ name: 'clipboard-read' }).then((status) => {
if (status.state === 'granted') {
startClipboardMonitoring();
} else {
status.onchange = () => {
if (status.state === 'granted') {
startClipboardMonitoring();
}
};
}
});
}
return () => {
document.removeEventListener('keydown', keyDownHandler);
if (clipboardMonitoringStarted) {
window.removeEventListener('focus', focusHandler);
window.removeEventListener('blur', blurHandler);
if (clipboardTimerId !== null) {
clearInterval(clipboardTimerId);
}
}
};
}
export function saveMonacoEditorViewState(editorId) {
const result = blazorMonaco.editor.getEditor(editorId)?.saveViewState();
return { Inner: result ? DotNet.createJSObjectReference(result) : null };
}
export function restoreMonacoEditorViewState(editorId, state) {
blazorMonaco.editor.getEditor(editorId)?.restoreViewState(state);
}
/** @type {Map<string, MutationObserver[]>} */
const virtualKeyboardObservers = new Map();
export function setVirtualKeyboardDisabled(editorId, disabled) {
virtualKeyboardObservers.get(editorId)?.forEach((o) => o.disconnect());
virtualKeyboardObservers.delete(editorId);
const root = document.getElementById(editorId);
if (!root) {
console.warn(`setVirtualKeyboardDisabled: could not find editor container #${editorId}`);
return;
}
if (!disabled) {
root.querySelector('.native-edit-context')?.removeAttribute('inputmode');
return;
}
const observers = [];
virtualKeyboardObservers.set(editorId, observers);
let current = null;
let currentAttrObserver = null;
const ensureInputMode = (el) => {
if (el.getAttribute('inputmode') !== 'none') {
el.setAttribute('inputmode', 'none');
}
};
const attach = () => {
const el = root.querySelector('.native-edit-context');
// Skip early if the element is missing or unchanged.
if (!el || el === current) {
return;
}
current = el;
// Stop watching the previous (now replaced) element.
if (currentAttrObserver) {
currentAttrObserver.disconnect();
observers.splice(observers.indexOf(currentAttrObserver), 1);
}
ensureInputMode(el);
// Re-apply if Monaco clears `inputmode` on this element (cheap: one attribute).
currentAttrObserver = new MutationObserver(() => ensureInputMode(el));
currentAttrObserver.observe(el, { attributes: true, attributeFilter: ['inputmode'] });
observers.push(currentAttrObserver);
};
// Watch only for the input element being (re)created.
const rootObserver = new MutationObserver(attach);
rootObserver.observe(root, { childList: true, subtree: true });
observers.push(rootObserver);
attach();
}
export function dispose() {
for (const observers of virtualKeyboardObservers.values()) {
observers.forEach((o) => o.disconnect());
}
virtualKeyboardObservers.clear();
}
export function copyUrlToClipboard(urlPrefix) {
navigator.clipboard.writeText(urlPrefix ? `${urlPrefix}${location.hash}` : location.href);
}
export function getClipboardText() {
return navigator.clipboard.readText();
}