import {ContentState, convertFromRaw, convertToRaw, ContentBlock} from 'draft-js'; import {isQumuWidget, postProccessQumuEmbed} from '../../components/embeds/QumuWidget'; import {logger} from 'core/services/logger'; import * as tableHelpers from 'core/editor3/helpers/table'; import {editor3StateToHtml} from './editor3StateToHtml'; /** * @ngdoc class * @name AtomicBlockParser * @description AtomicBlockParser is a helper class for `editor3StateToHtml`. It parses * Editor3 atomic blocks (image, table, embed, etc.). * @param {Object} contentState * @param {Array=} disabled A set of disabled elements (ie. ['table'] will ignore tables. */ export class AtomicBlockParser { contentState: any; disabled: any; rawState: any; constructor(contentState, disabled = []) { this.contentState = contentState; this.disabled = disabled; this.rawState = convertToRaw(contentState); } /** * @ngdoc method * @name AtomicBlockParser#parse * @param {Object} contentBlock * @returns {string} HTML * @description Returns the HTML representation of the passed contentBlock. */ parse(contentBlock: ContentBlock): string | undefined { const entityKey = contentBlock.getEntityAt(0); if (!entityKey) { return; } const entity = this.contentState.getEntity(entityKey); const data = entity.getData(); const rawKey = this.getRawKey(data); switch (entity.getType()) { case 'MEDIA': return this.parseMedia(data, rawKey).trim(); case 'EMBED': return this.parseEmbed(data).trim(); case 'TABLE': return this.parseTable(tableHelpers.getData(this.contentState, contentBlock.getKey())).trim(); default: logger.warn(`Editor3: Cannot generate HTML for entity type of ${entity.getType()}`, data); } } /** * Get entity key from raw state, which will be sent to backend and which is used for associations * * @param {Object} data * @return Number */ getRawKey(data) { return Object.keys(this.rawState.entityMap).find((key) => this.rawState.entityMap[key].data === data); } /** * @ngdoc method * @name AtomicBlockParser#parseEmbed * @param {Object} data Entity data. * @returns {string} HTML * @description Returns the HTML representation of an atomic 'EMBED' block having * the passed entity data. */ parseEmbed({data, description}) { let {html} = data; const descriptionHtml = typeof description === 'string' && description.length > 0 ? `

${description}

` : ''; const finalHtml = isQumuWidget(html) ? postProccessQumuEmbed(html) : data.html; return `
${finalHtml}${descriptionHtml}
`; } /** * @ngdoc method * @name AtomicBlockParser#parseMedia * @param {Object} data Entity data. * @param {String} rawKey Entity output key. * @returns {string} HTML * @description Returns the HTML representation of an atomic 'MEDIA' block having * the passed entity data. */ parseMedia(data, rawKey) { const {media} = data; const rendition = media.renditions.original || media.renditions.viewImage; const href = rendition.href; const alt = media.alt_text || ''; const mediaType = media.type; let type, content, desc = media.description_text; switch (mediaType) { case 'video': type = 'Video'; content = `