igniteui-webcomponents
Version:
Ignite UI for Web Components is a complete library of UI components, giving you the ability to build modern web applications using encapsulation and the concept of reusable components in a dependency-free approach.
107 lines • 3.57 kB
JavaScript
import { adoptStyles, } from 'lit';
import { last } from '../common/util.js';
export const ChatFileTypeIcons = new Map(Object.entries({
css: 'file_css',
csv: 'file_csv',
doc: 'file_doc',
docx: 'file_doc',
htm: 'file_htm',
html: 'file_html',
js: 'file_js',
json: 'file_json',
pdf: 'file_pdf',
rtf: 'file_rtf',
svg: 'file_svg',
txt: 'file_txt',
url: 'file_link',
xls: 'file_xls',
xlsx: 'file_xls',
xml: 'file_xml',
zip: 'file_zip',
default: 'file_generic',
}));
export function parseAcceptedFileTypes(fileTypes) {
const types = fileTypes.split(',').map((each) => each.trim().toLowerCase());
return {
extensions: new Set(types.filter((t) => t.startsWith('.'))),
mimeTypes: new Set(types.filter((t) => !t.startsWith('.') && !t.endsWith('/*'))),
wildcardTypes: new Set(types.filter((t) => t.endsWith('/*')).map((t) => t.slice(0, -2))),
};
}
export function isAcceptedFileType(file, accepted) {
if (!(accepted && file)) {
return true;
}
const { extensions, mimeTypes, wildcardTypes } = accepted;
const fileType = file.type.toLowerCase();
const fileExtension = `.${last(file.name.split('.'))?.toLowerCase()}`;
const [fileBaseType] = fileType.split('/');
return (extensions.has(fileExtension) ||
mimeTypes.has(fileType) ||
wildcardTypes.has(fileBaseType));
}
export function getChatAcceptedFiles(event, accepted) {
return Array.from(event.dataTransfer?.items ?? [])
.filter((item) => item.kind === 'file' && isAcceptedFileType(item.getAsFile(), accepted))
.map((item) => item.getAsFile());
}
export function getIconName(fileType) {
return fileType?.startsWith('image') ? 'attach_image' : 'attach_document';
}
export function createAttachmentURL(attachment) {
if (attachment.file) {
return URL.createObjectURL(attachment.file);
}
return attachment.url || '';
}
export function getFileExtension(name) {
const parts = name.split('.');
return parts.length > 1 ? last(parts) : '';
}
export function isImageAttachment(attachment) {
if (attachment instanceof File) {
return attachment.type.startsWith('image/');
}
return Boolean(attachment.type === 'image' || attachment.file?.type.startsWith('image/'));
}
class AdoptedStylesController {
get hasAdoptedStyles() {
return this._hasAdoptedStyles;
}
_adoptRootStyles() {
const sheets = [];
for (const sheet of document.styleSheets) {
try {
const constructed = new CSSStyleSheet();
for (const rule of sheet.cssRules) {
if (rule.cssText.startsWith('@import')) {
continue;
}
constructed.insertRule(rule.cssText);
}
sheets.push(constructed);
}
catch { }
}
const ctor = this._host.constructor;
adoptStyles(this._host.shadowRoot, [...ctor.elementStyles, ...sheets]);
}
constructor(host) {
this._hasAdoptedStyles = false;
this._host = host;
host.addController(this);
}
shouldAdoptStyles(condition) {
if (condition) {
this._adoptRootStyles();
this._hasAdoptedStyles = true;
}
}
hostDisconnected() {
this._hasAdoptedStyles = false;
}
}
export function addAdoptedStylesController(host) {
return new AdoptedStylesController(host);
}
//# sourceMappingURL=utils.js.map