@jsb188/react 1.0.3 → 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/types/dom.d.ts DELETED
@@ -1,7 +0,0 @@
1
-
2
- // Common react types
3
-
4
- export type ReactSpanElement = React.DetailedHTMLProps<React.HTMLAttributes<HTMLSpanElement>, HTMLSpanElement>;
5
- export type ReactDivElement = React.DetailedHTMLProps<React.HTMLAttributes<HTMLDivElement>, HTMLDivElement>;
6
- export type ReactButtonElement = React.DetailedHTMLProps<React.ButtonHTMLAttributes<HTMLButtonElement>, HTMLButtonElement>;
7
- export type ReactLabelElement = React.DetailedHTMLProps<React.LabelHTMLAttributes<HTMLLabelElement>, HTMLLabelElement>;
package/utils/dom.ts DELETED
@@ -1,303 +0,0 @@
1
- import { useEffect, useState } from 'react';
2
-
3
- /**
4
- * Check if parent has class name
5
- */
6
-
7
- export function parentHasClassName(el: HTMLElement | null, cnRegex: RegExp, count = 0): boolean {
8
- if (!el || (!el.className && !el.parentNode)) {
9
- return false;
10
- } else if (
11
- typeof el.className === 'string' && cnRegex && cnRegex.test(el.className)
12
- ) {
13
- // console.log('!!!!!!!!!!', el && el.className, '::::', cnRegex);
14
- // NOTE: FontAwesome elements may return an SVG as className
15
- // So a "string" check is required
16
- return true;
17
- }
18
-
19
- // if (el) {
20
- // console.log(':::>', el.className, cnRegex, cnRegex.test(el.className));
21
- // }
22
-
23
- return (
24
- !!el.parentNode &&
25
- count < 10 &&
26
- parentHasClassName(el.parentNode as HTMLElement, cnRegex, count + 1)
27
- );
28
- }
29
-
30
- /**
31
- * Detect click outside
32
- */
33
-
34
- export function useOnClickOutside(
35
- ref: React.RefObject<HTMLElement | undefined>,
36
- listening: boolean,
37
- listenToResize: boolean,
38
- ignoreClassName: string | null,
39
- onClickOutside?: (e: MouseEvent | Event, isResize: boolean) => void,
40
- ) {
41
- const isListening = listening && typeof onClickOutside === 'function';
42
-
43
- useEffect(() => {
44
- if (isListening) {
45
- const handleClickOutside = (e: MouseEvent) => {
46
- if (ref.current && !ref.current.contains(e.target as Node)) {
47
- // console.log('(1) in');
48
- const overlayEl = document.getElementById('root-overlay');
49
- // const check = !overlayEl || !overlayEl.contains(e.target);
50
-
51
- // Bring this back if you want to allow from outside the ref
52
- const check1 = !overlayEl || !overlayEl.contains(e.target as Node);
53
- const check2 = !ignoreClassName ||
54
- !parentHasClassName(
55
- e.target as HTMLElement,
56
- new RegExp('\\b' + ignoreClassName + '\\b'),
57
- );
58
- // const check3 = !selfContainerClassName || !parentHasClassName(e.target, new RegExp('\\b' + selfContainerClassName + '\\b'));
59
- // if (check1 && check2 && check3) {
60
-
61
- if (check1 && check2) {
62
- onClickOutside(e, false);
63
- }
64
- }
65
- };
66
-
67
- let handleResize: (e: Event) => void;
68
- if (listenToResize) {
69
- handleResize = (e: Event) => onClickOutside(e, true);
70
- globalThis.addEventListener('resize', handleResize);
71
- }
72
-
73
- document.addEventListener('mousedown', handleClickOutside);
74
-
75
- return () => {
76
- document.removeEventListener('mousedown', handleClickOutside);
77
-
78
- if (handleResize) {
79
- globalThis.removeEventListener('resize', handleResize);
80
- }
81
- };
82
- }
83
- }, [isListening]);
84
- }
85
-
86
- /**
87
- * Focus element if it exists
88
- */
89
-
90
- export function focusElement(elemId: string) {
91
- const el = document.getElementById(elemId);
92
- if (el) {
93
- el.focus();
94
- }
95
- }
96
-
97
- /**
98
- * Get platform shortcut
99
- */
100
-
101
- export function platformShortcut(key: string) {
102
- const isMac = /Mac|iPhone|iPod|iPad/i.test(globalThis.navigator?.platform);
103
- return (isMac ? '⌘' : '^') + key;
104
- }
105
-
106
- /**
107
- * Quick helper for copying a text to clipboard
108
- */
109
-
110
- export function useCopyToClipboard(textToCopy: string, resetTimeMS: number = 2250): [boolean, () => void] {
111
- const [copied, setCopied] = useState(false);
112
-
113
- useEffect(() => {
114
- if (copied) {
115
- const timer = setTimeout(() => {
116
- setCopied(false);
117
- }, resetTimeMS);
118
- return () => clearTimeout(timer);
119
- }
120
- }, [copied]);
121
-
122
- const onClickCopy = () => {
123
- const el = document.createElement('textarea');
124
- el.value = textToCopy;
125
- document.body.appendChild(el);
126
- el.select();
127
- document.execCommand('copy');
128
- document.body.removeChild(el);
129
- setCopied(true);
130
- };
131
-
132
- return [copied, onClickCopy];
133
- }
134
-
135
- /**
136
- * Remove HTML from text when pasting to conten editable
137
- */
138
-
139
- export async function removeHTMLfromPaste(e: React.ClipboardEvent) {
140
- if (typeof navigator?.clipboard?.readText === 'function') {
141
- // This is not available on older browsers
142
- const clipboardText = await navigator.clipboard.readText();
143
- return clipboardText;
144
- }
145
-
146
- // @ts-expect-error - Older browser deprecation support
147
- const html = (e.originalEvent || e).clipboardData.getData('text/html');
148
- const result = document.createElement('div');
149
- result.innerHTML = html;
150
-
151
- result.querySelectorAll('style').forEach((el) => el.remove());
152
- result.querySelectorAll('meta').forEach((el) => el.remove());
153
- result.querySelectorAll('link').forEach((el) => el.remove());
154
-
155
- result.querySelectorAll('img').forEach(img => {
156
- const altText = img.alt || '';
157
- const textNode = document.createTextNode(altText);
158
- img.replaceWith(textNode);
159
- });
160
-
161
- result.querySelectorAll('br').forEach(br => {
162
- const textNode = document.createTextNode('\n');
163
- br.replaceWith(textNode);
164
- });
165
-
166
- // result.querySelectorAll('p').forEach(p => {
167
- // const textNode = document.createTextNode('\n');
168
- // p.replaceWith(textNode);
169
- // });
170
-
171
- // result.querySelectorAll('div').forEach(div => {
172
- // const textNode = document.createTextNode('\n');
173
- // div.replaceWith(textNode);
174
- // });
175
-
176
- const text = result.innerHTML
177
- .replace(/<\/?[^`]+?\/?>/gmi, '\n')
178
- .replace(/\n[\s]*\n/gmi, '\n')
179
- .trim();
180
-
181
- // NOTE: If copied from inside the content editable, "double line breaks" will become single.
182
- // So result.innerText would be correct in this case.
183
- // But if html is copied from outside, then reuslt.innerText would remove all line breaks,
184
- // Making text.replace(..) correct.
185
- // There's no way to tell where the text was copied, so use text as the output.
186
- // This is a small issue becasue all modern browsers will have access to navigator.clipboard.readText()
187
-
188
- return text;
189
- }
190
-
191
- /**
192
- * Get text from the entire DOM element;
193
- * including text form of all child elements.
194
- */
195
-
196
- export function getTextFromDOMElement(el: HTMLElement): string {
197
- const clone = el.cloneNode(true) as HTMLElement;
198
-
199
- // Replace all <img> elements with their alt text
200
- clone.querySelectorAll('img').forEach(img => {
201
- const altText = img.alt || '';
202
- const textNode = document.createTextNode(altText);
203
- img.replaceWith(textNode);
204
- });
205
-
206
- clone.querySelectorAll('br').forEach(br => {
207
- const textNode = document.createTextNode('\n');
208
- br.replaceWith(textNode);
209
- });
210
-
211
- return clone.textContent || '';
212
- }
213
-
214
- /**
215
- * Remove HTML and paste text at caret position
216
- */
217
-
218
- export async function pasteTextAtCaret(e: React.ClipboardEvent, cb: (text: string) => void) {
219
- e.preventDefault();
220
- const text = await removeHTMLfromPaste(e);
221
-
222
- if (globalThis.getSelection) {
223
- // IE9 and non-IE
224
- const sel = globalThis.getSelection();
225
- if (sel?.getRangeAt && sel.rangeCount) {
226
- let range = sel.getRangeAt(0);
227
- range.deleteContents();
228
-
229
- // Range.createContextualFragment() would be useful here but is
230
- // non-standard and not supported in all browsers (IE9, for one)
231
- const el = document.createElement('div');
232
- el.innerHTML = text;
233
-
234
- let node;
235
- let lastNode;
236
- const frag = document.createDocumentFragment();
237
-
238
- while ((node = el.firstChild)) {
239
- lastNode = frag.appendChild(node);
240
- }
241
-
242
- range.insertNode(frag);
243
-
244
- // Preserve the selection
245
- if (lastNode) {
246
- range = range.cloneRange();
247
- range.setStartAfter(lastNode);
248
- range.collapse(true);
249
- sel.removeAllRanges();
250
- sel.addRange(range);
251
- }
252
- }
253
- } else if (
254
- // @ts-expect-error - Deprecation support
255
- document.selection && document.selection.type != 'Control'
256
- ) {
257
- // @ts-expect-error - IE < 9
258
- document.selection.createRange().pasteHTML(text);
259
- }
260
-
261
- const activeEl = document.activeElement;
262
- if (activeEl) {
263
- cb((activeEl as HTMLElement).innerText || '');
264
- }
265
- }
266
-
267
- /**
268
- * Take the current position of caret in content editable;
269
- * then update the innerHTML and then set the caret back to the same position.
270
- */
271
-
272
- export function updateInnerHTMLRepositionCaret(
273
- el: HTMLElement,
274
- nextHTMLText: string,
275
- ) {
276
- const sel = globalThis.getSelection();
277
- if (sel) {
278
- const lenDiff = nextHTMLText.length - el.innerText.length;
279
- const range = sel.getRangeAt(0);
280
- const start = range.startOffset + lenDiff;
281
- const end = range.endOffset + lenDiff;
282
-
283
- el.innerHTML = nextHTMLText;
284
-
285
- const newMaxLen = el.innerText.length;
286
- if (newMaxLen > start && newMaxLen > end) {
287
- const newRange = new Range();
288
- newRange.setStart(el.childNodes[0], Math.min(start, newMaxLen));
289
- newRange.setEnd(el.childNodes[0], Math.min(end, newMaxLen));
290
- } else {
291
- // This means start/end position exceeds the new length;
292
- // this would happen when you try to replace text with a DOM element (such as <img />)
293
- // For this situation, set the caret at end of the text.
294
-
295
- el.focus();
296
- const range = document.createRange();
297
- range.selectNodeContents(el);
298
- range.collapse(false);
299
- sel.removeAllRanges();
300
- sel.addRange(range);
301
- }
302
- }
303
- }
@@ -1,57 +0,0 @@
1
-
2
- let BROWSER_HIDDEN_TIME = 0;
3
-
4
- /**
5
- * Check if browser (or app) is hidden
6
- * NOTE: Change this function for Mobile
7
- */
8
-
9
- export function isAppHidden() {
10
- return globalThis.document?.hidden;
11
- }
12
-
13
- /**
14
- * Keep track of browser hidden time
15
- */
16
-
17
- export function setAppVisibilityTime() {
18
- BROWSER_HIDDEN_TIME = globalThis.document?.hidden ? Date.now() : 0;
19
- }
20
-
21
- /**
22
- * When browser is un-focused for a while, all timeouts/intervals are paused.
23
- * When the user resumes focus on the browser, all the paused timeouts/intervals are executed at once.
24
- * This will cause a huge spike in CPU, causing the browser to freeze for a few seconds.
25
- * To avoid this, we can use the Page Visibility API to detect when the browser is not visible.
26
- */
27
-
28
- export function setTimeoutPriority(fn: () => void, ms: number, ignoreIfLowPriority: boolean = false) {
29
-
30
- // NOTE: You can check if browser is visible or hidden with:
31
- // document.visibilityState === 'visible' or document.hidden === false
32
- // document.hidden has more browser support than document.visibilityState
33
-
34
- if (!globalThis.document?.hidden || !requestIdleCallback) {
35
- // Browser is not tabbed out, so continue with normal timer
36
- return setTimeout(fn, ms);
37
- }
38
-
39
- // Browser is tabbed out, so there *might* be a reason for low priority execution mode check
40
-
41
- const now = Date.now();
42
- const isLowPriorityMode = (now - BROWSER_HIDDEN_TIME) > 900000; // 15 minutes
43
-
44
- // console.log('isLowPriorityMode', isLowPriorityMode, (now - BROWSER_HIDDEN_TIME));
45
-
46
- if (isLowPriorityMode && ignoreIfLowPriority) {
47
- // Exit without executing the function
48
- return 0;
49
- }
50
-
51
- // During low priority mode, use requestIdleCallback() instead,
52
- // which will execute the function in order when it's possible.
53
- return requestIdleCallback(fn, { timeout: ms });
54
- }
55
-
56
- // More tips:
57
- // Use requestIdleCallback() for heavy compute scripts that may get in the way of user experience or create JS lag