main

mattermost/focalboard

Last updated at: 29/12/2023 09:51

contentRegistry.tsx

TLDR

This file exports a ContentRegistry class and a contentRegistry object. The ContentRegistry class is responsible for registering content types and providing methods to interact with the registered content types.

Classes

ContentRegistry

This class manages a registry of content types. It provides methods to register a new content type, check if a content type is registered, and retrieve the handler for a specific content type.

Methods

None

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
/* eslint-disable react/require-optimization */
import {IntlShape} from 'react-intl'

import {BlockTypes} from '../../blocks/block'
import {ContentBlock} from '../../blocks/contentBlock'
import {Utils} from '../../utils'

export type ContentHandler = {
    type: BlockTypes
    getDisplayText: (intl: IntlShape) => string
    getIcon: () => JSX.Element
    createBlock: (boardId: string, intl: IntlShape) => Promise<ContentBlock>
    createComponent: (block: ContentBlock, readonly: boolean, onAddElement?: () => void, onDeleteElement?: () => void) => JSX.Element
}

class ContentRegistry {
    private registry: Map<BlockTypes, ContentHandler> = new Map()

    get contentTypes(): BlockTypes[] {
        return [...this.registry.keys()]
    }

    registerContentType(entry: ContentHandler) {
        if (this.isContentType(entry.type)) {
            Utils.logError(`registerContentType, already registered type: ${entry.type}`)
            return
        }
        this.registry.set(entry.type, entry)
    }

    isContentType(type: BlockTypes): boolean {
        return this.registry.has(type)
    }

    getHandler(type: BlockTypes): ContentHandler | undefined {
        return this.registry.get(type)
    }
}

const contentRegistry = new ContentRegistry()

export {contentRegistry}