UNPKG

@craftercms/studio-ui

Version:

Services, components, models & utils to build CrafterCMS authoring extensions.

91 lines (89 loc) 3.4 kB
/* * Copyright (C) 2007-2022 Crafter Software Corporation. All Rights Reserved. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License version 3 as published by * the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ /* * Copyright (C) 2007-2022 Crafter Software Corporation. All Rights Reserved. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License version 3 as published by * the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ export function isNavigable(item) { if (item) { // Assets have a valid previewUrl but we don't want assets to show in the // Guest iFrame but rather as a preview dialog. return item.previewUrl !== null && item.systemType !== 'asset'; } return false; } export function isEditableViaFormEditor(item) { return ['page', 'component', 'taxonomy'].includes(item.systemType); } export function isImage(item) { return item === null || item === void 0 ? void 0 : item.mimeType.startsWith('image/'); } export function isVideo(item) { return item === null || item === void 0 ? void 0 : item.mimeType.startsWith('video/'); } export function isTextContent(mimeType) { return ( /^text\//.test(mimeType) || /^application\/(x-httpd-php|rtf|xhtml\+xml|xml|json|ld\+json|javascript|x-groovy|x-sh|x-yaml|ld+json|x-csh)$/.test( mimeType ) ); } export function isMediaContent(mimeType) { return /^image\//.test(mimeType) || /^video\//.test(mimeType); } export function isPdfDocument(mimeType) { return 'application/pdf' === mimeType; } export function isPreviewable(item) { if ((item === null || item === void 0 ? void 0 : item.systemType) === 'asset') { return isMediaContent(item.mimeType) || isTextContent(item.mimeType) || isPdfDocument(item.mimeType); } else { return ['page', 'component', 'renderingTemplate', 'script', 'taxonomy'].includes( item === null || item === void 0 ? void 0 : item.systemType ); } } export function isFolder(item) { return (item === null || item === void 0 ? void 0 : item.systemType) === 'folder'; } export function getEditorMode(item) { if (item.systemType === 'renderingTemplate') { return 'ftl'; } else if (item.systemType === 'script') { return 'groovy'; } else if (item.mimeType === 'application/javascript') { return 'javascript'; } else if (item.mimeType === 'text/css') { return 'css'; } else { return 'text'; } } export function rand(min, max) { return Math.floor(Math.random() * (max - min + 1) + min); }