@uploadcare/file-uploader
Version:
Building blocks for Uploadcare products integration
79 lines (76 loc) • 2.54 kB
JavaScript
/**
* @license
* MIT License
*
* Copyright (c) 2025 Uploadcare (hello@uploadcare.com). All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
// src/abstract/defineComponents.ts
function defineComponents(blockExports) {
for (const blockName in blockExports) {
let tagName = [...blockName].reduce((name, char) => {
if (char.toUpperCase() === char) {
char = `-${char.toLowerCase()}`;
}
name += char;
return name;
}, "");
if (tagName.startsWith("-")) {
tagName = tagName.replace("-", "");
}
if (!tagName.startsWith("uc-")) {
tagName = `uc-${tagName}`;
}
if (blockExports[blockName].reg) {
blockExports[blockName].reg(tagName);
}
}
}
// src/abstract/loadFileUploaderFrom.ts
var UC_WINDOW_KEY = "UC";
function loadFileUploaderFrom(url, register = false) {
return new Promise((resolve, reject) => {
if (typeof document !== "object") {
resolve(null);
return;
}
if (typeof window === "object" && window[UC_WINDOW_KEY]) {
resolve(window[UC_WINDOW_KEY]);
return;
}
const script = document.createElement("script");
script.async = true;
script.src = url;
script.onerror = () => {
reject();
};
script.onload = () => {
const blocks = window[UC_WINDOW_KEY];
register && defineComponents(blocks);
resolve(blocks);
};
document.head.appendChild(script);
});
}
export {
UC_WINDOW_KEY,
loadFileUploaderFrom
};