@symbo.ls/smbls-utils 3.2.7
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/LICENSE +21 -0
- package/dist/cjs/browser.js +34 -0
- package/dist/cjs/date.js +31 -0
- package/dist/cjs/detectHeight.js +62 -0
- package/dist/cjs/fibonacci.js +30 -0
- package/dist/cjs/files.js +24 -0
- package/dist/cjs/index.js +78 -0
- package/dist/cjs/load.js +171 -0
- package/dist/cjs/scaling.js +35 -0
- package/dist/cjs/style.js +40 -0
- package/dist/esm/browser.js +14 -0
- package/dist/esm/date.js +11 -0
- package/dist/esm/detectHeight.js +42 -0
- package/dist/esm/fibonacci.js +10 -0
- package/dist/esm/files.js +4 -0
- package/dist/esm/index.js +57 -0
- package/dist/esm/load.js +151 -0
- package/dist/esm/scaling.js +15 -0
- package/dist/esm/style.js +20 -0
- package/dist/iife/index.js +289 -0
- package/package.json +41 -0
- package/src/browser.js +13 -0
- package/src/date.js +10 -0
- package/src/detectHeight.js +42 -0
- package/src/fibonacci.js +9 -0
- package/src/files.js +3 -0
- package/src/index.js +75 -0
- package/src/load.js +236 -0
- package/src/scaling.js +15 -0
- package/src/style.js +23 -0
package/src/load.js
ADDED
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
export const loadJavascriptFile = (
|
|
4
|
+
FILE_URL,
|
|
5
|
+
async = false,
|
|
6
|
+
doc = document,
|
|
7
|
+
type = 'text/javascript'
|
|
8
|
+
) => {
|
|
9
|
+
return new Promise((resolve, reject) => {
|
|
10
|
+
try {
|
|
11
|
+
const scriptEle = doc.createElement('script')
|
|
12
|
+
scriptEle.type = type
|
|
13
|
+
scriptEle.async = async
|
|
14
|
+
scriptEle.src = FILE_URL
|
|
15
|
+
|
|
16
|
+
scriptEle.addEventListener('load', ev => {
|
|
17
|
+
resolve({
|
|
18
|
+
status: true
|
|
19
|
+
})
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
scriptEle.addEventListener('error', ev => {
|
|
23
|
+
reject(
|
|
24
|
+
new Error({
|
|
25
|
+
status: false,
|
|
26
|
+
message: `Failed to load the script ${FILE_URL}`
|
|
27
|
+
})
|
|
28
|
+
)
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
doc.body.appendChild(scriptEle)
|
|
32
|
+
} catch (error) {
|
|
33
|
+
reject(error)
|
|
34
|
+
}
|
|
35
|
+
})
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export const loadJavascriptFileSync = (
|
|
39
|
+
fileUrl,
|
|
40
|
+
doc = document,
|
|
41
|
+
type = 'text/javascript'
|
|
42
|
+
) => {
|
|
43
|
+
return new Promise((resolve, reject) => {
|
|
44
|
+
const scriptEle = doc.createElement('script')
|
|
45
|
+
scriptEle.type = type
|
|
46
|
+
scriptEle.src = fileUrl
|
|
47
|
+
|
|
48
|
+
// Create a blocking overlay
|
|
49
|
+
const blocker = doc.createElement('div')
|
|
50
|
+
blocker.style.cssText =
|
|
51
|
+
'position:fixed;top:0;left:0;right:0;bottom:0;background:rgba(255,255,255,0.8);z-index:9999;'
|
|
52
|
+
doc.body.appendChild(blocker)
|
|
53
|
+
|
|
54
|
+
scriptEle.onload = () => {
|
|
55
|
+
console.log(`Successfully loaded: ${fileUrl}`)
|
|
56
|
+
doc.body.removeChild(blocker)
|
|
57
|
+
resolve()
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
scriptEle.onerror = () => {
|
|
61
|
+
doc.body.removeChild(blocker)
|
|
62
|
+
reject(new Error(`Failed to load: ${fileUrl}`))
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
doc.body.appendChild(scriptEle)
|
|
66
|
+
})
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export const loadJavascriptFileEmbedSync = (
|
|
70
|
+
FILE_URL,
|
|
71
|
+
doc = document,
|
|
72
|
+
fallback,
|
|
73
|
+
type = 'text/javascript'
|
|
74
|
+
) => {
|
|
75
|
+
const xhr = new window.XMLHttpRequest()
|
|
76
|
+
xhr.open('GET', FILE_URL, false) // false makes the request synchronous
|
|
77
|
+
xhr.send()
|
|
78
|
+
|
|
79
|
+
if (xhr.status === 200) {
|
|
80
|
+
const scriptEle = doc.createElement('script')
|
|
81
|
+
scriptEle.type = type
|
|
82
|
+
scriptEle.text = xhr.responseText
|
|
83
|
+
doc.body.appendChild(scriptEle)
|
|
84
|
+
if (typeof fallback === 'function') fallback()
|
|
85
|
+
} else {
|
|
86
|
+
throw new Error(`Failed to load the script ${FILE_URL}`)
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export const loadCssFile = (
|
|
91
|
+
FILE_URL,
|
|
92
|
+
async = false,
|
|
93
|
+
doc = document,
|
|
94
|
+
type = 'text/javascript'
|
|
95
|
+
) => {
|
|
96
|
+
return new Promise((resolve, reject) => {
|
|
97
|
+
try {
|
|
98
|
+
const linkElem = doc.createElement('link')
|
|
99
|
+
linkElem.rel = 'stylesheet'
|
|
100
|
+
linkElem.href = FILE_URL
|
|
101
|
+
|
|
102
|
+
linkElem.addEventListener('load', ev => {
|
|
103
|
+
resolve({
|
|
104
|
+
status: true
|
|
105
|
+
})
|
|
106
|
+
})
|
|
107
|
+
|
|
108
|
+
doc.head.appendChild(linkElem)
|
|
109
|
+
} catch (error) {
|
|
110
|
+
reject(error)
|
|
111
|
+
}
|
|
112
|
+
})
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export const loadJavascript = (
|
|
116
|
+
body,
|
|
117
|
+
async = false,
|
|
118
|
+
doc = document,
|
|
119
|
+
type = 'text/javascript',
|
|
120
|
+
id = 'smbls-script'
|
|
121
|
+
) => {
|
|
122
|
+
try {
|
|
123
|
+
const scriptEle = doc.createElement('script')
|
|
124
|
+
scriptEle.type = type
|
|
125
|
+
scriptEle.async = async
|
|
126
|
+
scriptEle.id = id
|
|
127
|
+
scriptEle.innerHTML = body
|
|
128
|
+
|
|
129
|
+
doc.body.appendChild(scriptEle)
|
|
130
|
+
} catch (error) {
|
|
131
|
+
console.warn(error)
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Dynamically loads a remote JavaScript file and returns a Promise
|
|
137
|
+
* that resolves when the script has completely loaded or rejects on error.
|
|
138
|
+
*
|
|
139
|
+
* @param {string} url - The URL of the remote JavaScript file to load
|
|
140
|
+
* @param {Object} [options] - Optional configuration
|
|
141
|
+
* @param {string} [options.id] - Optional ID to assign to the script element
|
|
142
|
+
* @param {boolean} [options.async=true] - Whether to load the script asynchronously
|
|
143
|
+
* @param {string} [options.type='text/javascript'] - The type attribute for the script
|
|
144
|
+
* @param {string} [options.integrity] - Optional integrity hash for SRI
|
|
145
|
+
* @param {string} [options.crossOrigin] - Optional crossorigin attribute
|
|
146
|
+
* @return {Promise} A promise that resolves when the script loads or rejects on error
|
|
147
|
+
*/
|
|
148
|
+
export function loadRemoteScript (url, options = {}) {
|
|
149
|
+
const { window = globalThis } = options
|
|
150
|
+
const { document = window.document } = options
|
|
151
|
+
|
|
152
|
+
return new Promise((resolve, reject) => {
|
|
153
|
+
// Check if script is already loaded
|
|
154
|
+
const existingScript = document.querySelector(`script[src="${url}"]`)
|
|
155
|
+
if (existingScript) {
|
|
156
|
+
return resolve(existingScript)
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// Create script element
|
|
160
|
+
const script = document.createElement('script')
|
|
161
|
+
script.src = url
|
|
162
|
+
script.async = options.async === true
|
|
163
|
+
script.type = options.type || 'text/javascript'
|
|
164
|
+
|
|
165
|
+
// Add optional attributes
|
|
166
|
+
if (options.id) script.id = options.id
|
|
167
|
+
if (options.integrity) script.integrity = options.integrity
|
|
168
|
+
if (options.crossOrigin) script.crossOrigin = options.crossOrigin
|
|
169
|
+
|
|
170
|
+
// Setup load and error handlers
|
|
171
|
+
script.onload = () => {
|
|
172
|
+
script.onerror = script.onload = null
|
|
173
|
+
resolve(script)
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
script.onerror = () => {
|
|
177
|
+
script.onerror = script.onload = null
|
|
178
|
+
reject(new Error(`Failed to load script: ${url}`))
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
// Append the script to the document head
|
|
182
|
+
document.head.appendChild(script)
|
|
183
|
+
})
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Dynamically loads a remote CSS file and returns a Promise
|
|
188
|
+
* that resolves when the stylesheet has completely loaded or rejects on error.
|
|
189
|
+
*
|
|
190
|
+
* @param {string} url - The URL of the remote CSS file to load
|
|
191
|
+
* @param {Object} [options] - Optional configuration
|
|
192
|
+
* @param {string} [options.id] - Optional ID to assign to the link element
|
|
193
|
+
* @param {string} [options.rel='stylesheet'] - The rel attribute for the link
|
|
194
|
+
* @param {string} [options.media='all'] - The media attribute
|
|
195
|
+
* @param {string} [options.integrity] - Optional integrity hash for SRI
|
|
196
|
+
* @param {string} [options.crossOrigin] - Optional crossorigin attribute
|
|
197
|
+
* @return {Promise} A promise that resolves when the stylesheet loads or rejects on error
|
|
198
|
+
*/
|
|
199
|
+
export async function loadRemoteCSS (url, options = {}) {
|
|
200
|
+
const { window = globalThis } = options
|
|
201
|
+
const { document = window.document } = options
|
|
202
|
+
|
|
203
|
+
return new Promise((resolve, reject) => {
|
|
204
|
+
// Check if stylesheet is already loaded
|
|
205
|
+
const existingLink = document.querySelector(`link[href="${url}"]`)
|
|
206
|
+
if (existingLink) {
|
|
207
|
+
return resolve(existingLink)
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
// Create link element
|
|
211
|
+
const link = document.createElement('link')
|
|
212
|
+
link.href = url
|
|
213
|
+
link.rel = options.rel || 'stylesheet'
|
|
214
|
+
link.type = 'text/css'
|
|
215
|
+
link.media = options.media || 'all'
|
|
216
|
+
|
|
217
|
+
// Add optional attributes
|
|
218
|
+
if (options.id) link.id = options.id
|
|
219
|
+
if (options.integrity) link.integrity = options.integrity
|
|
220
|
+
if (options.crossOrigin) link.crossOrigin = options.crossOrigin
|
|
221
|
+
|
|
222
|
+
// Setup load and error handlers
|
|
223
|
+
link.onload = () => {
|
|
224
|
+
link.onerror = link.onload = null
|
|
225
|
+
resolve(link)
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
link.onerror = () => {
|
|
229
|
+
link.onerror = link.onload = null
|
|
230
|
+
reject(new Error(`Failed to load stylesheet: ${url}`))
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
// Append the link to the document head
|
|
234
|
+
document.head.appendChild(link)
|
|
235
|
+
})
|
|
236
|
+
}
|
package/src/scaling.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
import { isObject, isArray } from '@domql/utils'
|
|
4
|
+
|
|
5
|
+
export const findClosestNumber = (number, arr) => {
|
|
6
|
+
return (isArray(arr) ? arr : Object.values(arr)).reduce((prev, curr) => {
|
|
7
|
+
return (Math.abs(curr - number) < Math.abs(prev - number) ? curr : prev)
|
|
8
|
+
})
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export const findClosestNumberInFactory = (val, factory) => {
|
|
12
|
+
val = parseFloat(val)
|
|
13
|
+
if (isObject(factory)) factory = Object.values(factory)
|
|
14
|
+
return findClosestNumber(val, factory)
|
|
15
|
+
}
|
package/src/style.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
export const getEmotionStylesheet = () => {
|
|
4
|
+
// Access the first stylesheet
|
|
5
|
+
const stylesheet = document.styleSheets[0]
|
|
6
|
+
let str = ''
|
|
7
|
+
if (stylesheet) {
|
|
8
|
+
try {
|
|
9
|
+
// Access the CSS rules
|
|
10
|
+
const cssRules = stylesheet.cssRules || stylesheet.rules // For older browsers, use `rules`
|
|
11
|
+
|
|
12
|
+
// Iterate over the rules and log them
|
|
13
|
+
for (let i = 0; i < cssRules.length; i++) {
|
|
14
|
+
str += cssRules[i].cssText
|
|
15
|
+
}
|
|
16
|
+
} catch (error) {
|
|
17
|
+
console.error('Unable to access CSS rules. This may be due to CORS restrictions:', error)
|
|
18
|
+
}
|
|
19
|
+
} else {
|
|
20
|
+
console.log('No stylesheets found in document.styleSheets[0].')
|
|
21
|
+
}
|
|
22
|
+
return str
|
|
23
|
+
}
|