-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathmisc.ts
More file actions
256 lines (210 loc) · 5.31 KB
/
Copy pathmisc.ts
File metadata and controls
256 lines (210 loc) · 5.31 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import type { ResizeListener } from '@stdlib/misc';
import {
isNumeric,
observeResize,
splitStr,
unobserveResize,
} from '@stdlib/misc';
import { isString, pull } from 'lodash';
import { nanoid } from 'nanoid';
import type { Cookies } from 'quasar';
import type { QDialogOptions } from 'quasar';
export async function asyncDialog<T = any>(opts: QDialogOptions): Promise<T> {
return new Promise((resolve, reject) => {
$quasar()
.dialog(opts)
.onOk(async (output: T) => {
resolve(output);
})
.onCancel(() => {
reject();
});
});
}
export function handleError(error: any, logger = mainLogger) {
if (error == null) {
return;
}
$quasar().notify({
message: isString(error)
? error
: error.response?.data?.errors?.[0]?.message ??
error.response?.data?.message ??
error.message ??
'An error has occurred.',
type: 'negative',
});
logger.error(error);
}
export function shouldRememberSession() {
return (
internals.localStorage.getItem('rememberSession') === 'true' &&
internals.localStorage.getItem('demo') !== 'true'
);
}
export function wrapStorage(storage: Storage) {
const props = {
clear: storage.clear.bind(storage),
getItem: storage.getItem.bind(storage),
key: storage.key.bind(storage),
removeItem: storage.removeItem.bind(storage),
setItem: storage.setItem.bind(storage),
};
return new Proxy(storage, {
get(target, prop) {
if (prop in props) {
return props[prop as keyof typeof props];
} else {
return target[prop as keyof typeof target];
}
},
set(target, prop, value) {
if (prop in props) {
return (props[prop as keyof typeof props] = value);
} else {
return (target[prop as keyof typeof target] = value);
}
},
});
}
export function isCtrlDown(event: KeyboardEvent | MouseEvent) {
return event.ctrlKey || event.metaKey;
}
const _modifiers = ['Alt', 'Control', 'Meta', 'Shift'] as const;
export function modsMatch(
event: KeyboardEvent | MouseEvent,
modifiers: (typeof _modifiers)[number][],
) {
if (modifiers.includes('Control') && $quasar().platform.is.mac) {
pull(modifiers, 'Control');
modifiers.push('Meta');
}
for (const modifier of _modifiers) {
if (modifiers.includes(modifier) !== event.getModifierState(modifier)) {
return false;
}
}
return true;
}
export function getCtrlKeyName() {
return $quasar().platform.is.mac ? 'Cmd' : 'Ctrl';
}
export function getAltKeyName() {
return $quasar().platform.is.mac ? 'Option' : 'Alt';
}
export function sizeToCSS(size: string): string {
if (isNumeric(size)) {
return `${size}px`;
} else {
return 'auto';
}
}
export function getNameInitials(name: string): string {
const nameParts = splitStr(name.trim(), ' ');
let initials = nameParts[0].substring(0, 1).toUpperCase();
if (nameParts.length > 1) {
initials += nameParts[nameParts.length - 1].substring(0, 1).toUpperCase();
}
return initials;
}
export function multiModePath(path: string) {
if (process.env.MODE === 'ssr') {
return path;
} else {
return `?rand=${nanoid()}#${path}`;
}
}
export function useResizeObserver(
elemFunc: () => Element | PromiseLike<Element>,
listener: ResizeListener,
) {
let elem: Element;
onMounted(async () => {
elem = await elemFunc();
observeResize(elem, listener);
});
onBeforeUnmount(async () => {
unobserveResize(elem, listener);
});
}
// setTimeout interceptor
const oldSetTimeout = setTimeout;
globalThis.setTimeout = newSetTimeout as any;
let _isWithinTimeout = false;
function newSetTimeout(
callback: (...args: any[]) => void,
ms?: number,
...args: any[]
): NodeJS.Timeout {
return oldSetTimeout(() => {
_isWithinTimeout = true;
callback(args);
_isWithinTimeout = false;
}, ms);
}
export function isWithinTimeout() {
return _isWithinTimeout;
}
export function getRequestConfig(cookies: Cookies | undefined) {
return process.env.SERVER
? {
headers: {
cookie: Object.entries(cookies?.getAll())
.map(([name, value]) => `${name}=${value}`)
.join(';'),
},
}
: undefined;
}
export async function useAsyncData<T>(
key: string,
fn: () => Promise<T>,
): Promise<T> {
if (process.env.CLIENT && key in appStore().dict) {
const result = appStore().dict[key];
delete appStore().dict[key];
return result;
}
const result = await fn();
if (process.env.SERVER) {
appStore().dict[key] = result;
}
return result;
}
export function debounceTick(func: () => any) {
let scheduled = false;
return () => {
if (scheduled) {
return;
}
scheduled = true;
void nextTick(() => {
scheduled = false;
func();
});
};
}
export function createDoubleClickChecker() {
let doubleClick = false;
const pos = { x: 0, y: 0 };
return (event: MouseEvent) => {
if (doubleClick) {
doubleClick = false;
if (
Math.sqrt(
Math.pow(event.clientX - pos.x, 2) +
Math.pow(event.clientY - pos.y, 2),
) <= 24
) {
return true;
}
} else {
doubleClick = true;
pos.x = event.clientX;
pos.y = event.clientY;
setTimeout(() => {
doubleClick = false;
}, 250);
}
};
}