import { IResolvedRichTextHtmlResult , IAsyncRichTextHtmlResolverInput } from '../rich-text-resolver.models' ; import { asyncBrowserParser , IAsyncParser , parserConfiguration } from '../../../parser' ; import { BaseAsyncRichTextResolver } from '../base/base-async-rich-text-resolver' ; export class AsyncRichTextHtmlResolver extends BaseAsyncRichTextResolver < IAsyncRichTextHtmlResolverInput , IResolvedRichTextHtmlResult > { constructor ( parser ? ) { super ( asyncBrowserParser , parser ) ; } async resolveRichTextAsync ( input ) { return await this . resolveRichTextInternalAsync ( input . element . value , input ) ; } private async resolveRichTextInternalAsync ( html , input ) { const parsedResult = await super . getAsyncParser ( ) . parseAsync ( html , input . element , { elementResolverAsync : async ( element ) => { } , contentItemResolverAsync : async ( element , itemCodename , linkedItemIndex , linkedItem ) => { const parentElement = element . parentElement ; if ( parentElement ) { const resolvedItemHtml = input . contentItemResolverAsync ? ( await input . contentItemResolverAsync ( itemCodename , linkedItem ) ) . contentItemHtml ?? '' : '' ; const innerHtml = ( await this . resolveRichTextInternalAsync ( resolvedItemHtml , input ) ) . html ; element . setInnerHtml ( innerHtml ) ; } } , genericElementResolverAsync : async ( element ) => { } , imageResolverAsync : async ( element , imageId , image ) => { const imageResult = input . imageResolverAsync ? await input . imageResolverAsync ( imageId , image ) : undefined ; if ( imageResult ?. imageHtml ) { element . setOuterHtml ( imageResult . imageHtml ) ; } else if ( imageResult ?. imageUrl ) { element . setAttribute ( parserConfiguration . imageElementData . srcAttribute , imageResult . imageUrl ) ; } } , urlResolverAsync : async ( element , linkId , linkText , link ) => { if ( input . urlResolverAsync ) { const urlResult = await input . urlResolverAsync ( linkId , linkText , link ) ; if ( urlResult ?. linkHtml ) { element . setOuterHtml ( urlResult . linkHtml ) ; } else if ( urlResult ?. linkUrl ) { element . setAttribute ( 'href' , urlResult . linkUrl ) ; } } } } , input . linkedItems ?? [ ] ) ; return { componentCodenames : parsedResult . componentCodenames , linkedItemCodenames : parsedResult . linkedItemCodenames , html : parsedResult . result } ; } } export const createAsyncRichTextHtmlResolver = ( parser ? ) => new AsyncRichTextHtmlResolver ( parser ) ;