import { MessageEmbeddedImage , MessageImage , MessageImages , MessageRemoteImage , PartialMessageState , } from '../../logic/messages/messagesTypes' ; import { setEmbeddedAttr } from './messageEmbeddeds' ; import { ATTRIBUTES } from './messageRemotes' ; const REGEXP_FIXER = ( ( ) => { const str = ATTRIBUTES . map ( ( key ) => `proton-${ key }` ) . join ( '|' ) ; return `(${ str })` ; } ) ( ) ; export const getAnchor = ( document , image ) => { if ( ! document ) { return null ; } return document . querySelector ( `.proton-image-anchor[data-proton-${ image . type }="${ image . id }"]` ) as HTMLElement | null ; } ; export const getRemoteImages = ( { messageImages } ) => ( messageImages ?. images . filter ( ( { type } ) => type === 'remote' ) || [ ] ) as MessageRemoteImage [ ] ; export const getEmbeddedImages = ( { messageImages } ) => ( messageImages ?. images . filter ( ( { type } ) => type === 'embedded' ) || [ ] ) as MessageEmbeddedImage [ ] ; export const updateImages = ( original , flagChanges , remoteImages , embeddedImages ) : MessageImages => { const messageImages = { ... { hasRemoteImages : false , hasEmbeddedImages : false , showRemoteImages : false , showEmbeddedImages : false , images : [ ] , } , ... ( original || { } ) , ... ( flagChanges || { } ) , } ; const remotes = getRemoteImages ( { messageImages } ) ; const embeddeds = getEmbeddedImages ( { messageImages } ) ; const images = [ ... ( remoteImages !== undefined ? remoteImages : remotes ) , ... ( embeddedImages !== undefined ? embeddedImages : embeddeds ) , ] ; return { ... messageImages , images } ; } ; export const insertImageAnchor = ( id , type , match ) : string => { const anchor = document . createElement ( 'span' ) ; anchor . classList . add ( 'proton-image-anchor' ) ; anchor . setAttribute ( `data-proton-${ type }` , id ) ; match . parentElement ?. replaceChild ( anchor , match ) ; return id ; } ; export const restoreImages = ( inputDocument , images ) => { if ( ! inputDocument || ! images ) { return inputDocument ; } const document = inputDocument . cloneNode ( true ) as Element ; const { showEmbeddedImages , showRemoteImages } = images ; images . images . forEach ( ( image ) => { const anchor = getAnchor ( document , image ) ; const parent = anchor ?. parentElement ; const { original } = image ; if ( ! anchor || ! parent || ! original ) { return ; } if ( image . type === 'embedded' ) { setEmbeddedAttr ( image . cid , image . cloc , original ) ; original . classList . add ( 'proton-embedded' ) ; if ( showEmbeddedImages ) { original . setAttribute ( 'src' , image ?. url || '' ) ; } } if ( image . type === 'remote' ) { if ( showRemoteImages ) { original . setAttribute ( 'src' , image ?. url || '' ) ; } } parent . replaceChild ( original , anchor ) ; } ) ; return document ; } ; /**
 * Restore all prefixed attributes
 */ export const restoreAllPrefixedAttributes = ( content ) => { const regex = new RegExp ( REGEXP_FIXER , 'g' ) ; return content . replace ( regex , ( _ , $1 ) => $1 . substring ( 7 ) ) ; } ;