import { $ID, ElementType, SchemaId, Scope, SharedScope, } from '../interfaces/editors.interface' export const urlToSchemaId = ( val: string, delimiter = '-', ): { id: string scope: Scope } => { if (!val.includes(delimiter)) { return { id: val, scope: 'shared' satisfies SharedScope } } const typeFragments = val.split(delimiter) if (typeFragments.length > 2) { // `saint-gobain` or something like that (LEGACY SHIT) const last = typeFragments.pop() return { id: last || '', scope: typeFragments.join('-') } } return { id: typeFragments[1], scope: typeFragments[0], } } export const getSchemaId = (type: ElementType | $ID): SchemaId => { /* Explainer: Element types created in editors V2 use $scope/$type encoded into type as $scope-$type. If type contains `-`, it is split into scope and true type. If it doesn't, "shared" is used as $scope as a fallback. */ // If type contains `/`, it is already a schemaId or $id if (type.includes('/')) { if (type.startsWith('#')) { return type.replace('#', '') as SchemaId } return type as SchemaId } if (type.includes('-')) { // V2 type const typeFragments = type.split('-') if (typeFragments.length > 2) { // `saint-gobain` or something like that (LEGACY SHIT) const last = typeFragments.pop() return `${typeFragments.join('-')}/${last}` } return `${typeFragments[0]}/${typeFragments[1]}` } // Legacy element type return type.startsWith('shared') ? (type as SchemaId) : `shared/${type}` } export const getElementTypeString = ( $id: $ID | SchemaId, omitScope = true, ): ElementType => { const [scope, type] = $id.replace('#', '').split('/') if (omitScope && scope === 'shared') { return type } return `${scope}-${type}` }