import { IRichTextObjectResolverInput , IRichTextObjectContentItemData , IRichTextObjectLinkData , IRichTextObjectImageData , IRichTextObjectHtmlElementData , IRichTextObjectItem , IRichTextObjectAtribute , IRichTextObjectResult } from '../rich-text-resolver.models' ; import { browserParser , IParser , IParserElement } from '../../../parser' ; import { guidHelper } from '../../../utilities' ; import { BaseRichTextResolver } from '../base/base-rich-text-resolver' ; export class RichTextObjectResolver extends BaseRichTextResolver < IRichTextObjectResolverInput , IRichTextObjectResult > { private readonly sdkIdAttributeName : string = 'sdk-elem-id' ; private readonly rootId : string = 'root' ; private readonly rootTag : string = 'p' ; constructor ( parser ? ) { super ( browserParser , parser ) ; } resolveRichText ( input ) { const result = this . resolveRichTextInternal ( input . element . value , input , { type : 'root' , attributes : [ ] , children : [ ] , data : { } , tag : this . rootTag , _sdkElemId : this . rootId } ) ; if ( input . cleanSdkIds === true ) { this . cleanSdkIds ( result ) ; } return { data : result } ; } private resolveRichTextInternal ( html , input , result ) { super . getParser ( ) . parse ( html , input . element , { elementResolver : ( element ) => { element . setAttribute ( this . sdkIdAttributeName , guidHelper . genereateGuid ( ) ) ; } , contentItemResolver : ( element , itemCodename , linkedItemIndex , linkedItem ) => { const data = { codename : itemCodename , item : linkedItem } ; const parentSdkId = this . getSdkIdFromElement ( element . parentElement ) ; const parentItem = this . findItemWithSdkId ( parentSdkId , result ) ; if ( parentItem ) { parentItem . children . push ( { type : 'linkedItem' , attributes : this . getAttributes ( element ) , tag : element . tag . toLowerCase ( ) , data : data , children : [ ] , _sdkElemId : this . getSdkIdFromElement ( element ) } ) ; } } , genericElementResolver : ( element ) => { const data = { text : element . text ?? '' , html : element . html ?? '' } ; const parentSdkId = this . getSdkIdFromElement ( element . parentElement ) ; const parentItem = this . findItemWithSdkId ( parentSdkId , result ) ; if ( parentItem ) { parentItem . children . push ( { type : 'htmlElement' , attributes : this . getAttributes ( element ) , tag : element . tag . toLowerCase ( ) , data : data , children : [ ] , _sdkElemId : this . getSdkIdFromElement ( element ) } ) ; } } , imageResolver : ( element , imageId , image ) => { const data = { imageId : imageId , image : image } ; const parentSdkId = this . getSdkIdFromElement ( element . parentElement ) ; const parentItem = this . findItemWithSdkId ( parentSdkId , result ) ; if ( parentItem ) { parentItem . children . push ( { type : 'image' , attributes : this . getAttributes ( element ) , tag : element . tag . toLowerCase ( ) , data : data , children : [ ] , _sdkElemId : this . getSdkIdFromElement ( element ) } ) ; } } , urlResolver : ( element , linkId , linkText , link ) => { const data = { linkId : linkId , linkText : linkText , link : link } ; const parentSdkId = this . getSdkIdFromElement ( element . parentElement ) ; const parentItem = this . findItemWithSdkId ( parentSdkId , result ) ; if ( parentItem ) { parentItem . children . push ( { type : 'link' , attributes : this . getAttributes ( element ) , tag : element . tag . toLowerCase ( ) , data : data , children : [ ] , _sdkElemId : this . getSdkIdFromElement ( element ) } ) ; } } } , input . linkedItems ?? [ ] ) ; return result ; } private findItemWithSdkId ( sdkId , item ) { if ( item . _sdkElemId === sdkId ) { return item ; } for ( const child of item . children ) { const foundItem = this . findItemWithSdkId ( sdkId , child ) ; if ( foundItem ) { return foundItem ; } } return undefined ; } private getAttributes ( element ) { return element ?. attributes ?? [ ] ; } private getSdkIdFromElement ( element ) { if ( ! element ) { return this . rootId ; } const value = element . attributes . find ( ( m ) => m . name === this . sdkIdAttributeName ) ?. value ?? this . rootId ; return value ; } private cleanSdkIds ( item ) { item . attributes = item . attributes . filter ( ( m ) => m . name !== this . sdkIdAttributeName ) ; item . _sdkElemId = '' ; for ( const child of item . children ) { this . cleanSdkIds ( child ) ; } } } export const createRichTextObjectResolver = ( parser ? ) => new RichTextObjectResolver ( parser ) ;