import { IQuestion , QuestionHtmlModel , Serializer } from 'survey-core' ; import { SurveyPDF } from '../survey' ; import { IPoint , DocController } from '../doc_controller' ; import { FlatQuestion } from './flat_question' ; import { FlatRepository } from './flat_repository' ; import { IPdfBrick } from '../pdf_render/pdf_brick' ; import { SurveyHelper } from '../helper_survey' ; export type IHTMLRenderType = 'auto' | 'standard' | 'image' ; export class FlatHTML extends FlatQuestion { protected question : QuestionHtmlModel ; public constructor ( protected survey , question , controller ) { super ( survey , question , controller ) ; } private chooseRender ( html ) { if ( /<[^>]*style[^<]*>/ . test ( html ) || /<[^>]*table[^<]*>/ . test ( html ) || /&\w+;/ . test ( html ) ) { return 'image' ; } return 'standard' ; } public async generateFlatsContent ( point ) { let renderAs = < IHTMLRenderType > this . question . renderAs ; if ( renderAs === 'auto' ) renderAs = this . controller . htmlRenderAs ; if ( renderAs === 'auto' ) renderAs = this . chooseRender ( SurveyHelper . getLocString ( this . question . locHtml ) ) ; const html = SurveyHelper . createHtmlContainerBlock ( SurveyHelper . getLocString ( this . question . locHtml ) , this . controller , renderAs ) ; if ( renderAs === 'image' ) { const width = SurveyHelper . getPageAvailableWidth ( this . controller ) ; const { url , aspect } = await SurveyHelper . htmlToImage ( html , width , this . controller ) ; const height = width / aspect ; return [ SurveyHelper . createImageFlat ( point , this . question , this . controller , url , width , height ) ] ; } return [ SurveyHelper . splitHtmlRect ( this . controller , await SurveyHelper . createHTMLFlat ( point , this . question , this . controller , html ) ) ] ; } } Serializer . removeProperty ( 'html' , 'renderAs' ) ; Serializer . addProperty ( 'html' , { name : 'renderAs' , default : 'auto' , choices : [ 'auto' , 'standard' , 'image' ] } ) ; FlatRepository . getInstance ( ) . register ( 'html' , FlatHTML ) ;