@@ -486,7 +486,7 @@ strong {
486486} ;
487487
488488// Function to convert report markdown to Chartifact format
489- const convertToChartifact = async ( reportMarkdown : string , reportStyle : string , charts : Chart [ ] , tables : DictTable [ ] , conceptShelfItems : FieldItem [ ] , config : ClientConfig ) : Promise < string > => {
489+ export const convertToChartifact = ( reportMarkdown : string , reportStyle : string , charts : Chart [ ] , tables : DictTable [ ] , conceptShelfItems : FieldItem [ ] , config : ClientConfig ) => {
490490 try {
491491 // Extract chart IDs from the report markdown images
492492 // Images are in format: [IMAGE(chart-id)]
@@ -590,3 +590,57 @@ ${JSON.stringify(modifiedSpec, null, 2)}
590590 }
591591} ;
592592
593+
594+ // Function to open Chartifact in a new tab and send markdown via postMessage
595+ export const openChartifactViewer = async ( chartifactMarkdown : string ) => {
596+ try {
597+ // Open the Chartifact viewer in a new tab
598+ const chartifactWindow = window . open (
599+ 'https://microsoft.github.io/chartifact/view/' ,
600+ '_blank'
601+ ) ;
602+
603+ if ( ! chartifactWindow ) {
604+ //showMessage('Failed to open Chartifact viewer. Please allow popups.', 'error');
605+ return ;
606+ }
607+
608+ // Listen for hostStatus messages from the Chartifact viewer
609+ const handleMessage = ( event : MessageEvent ) => {
610+ // Verify the message is from the Chartifact viewer
611+ if ( event . origin !== 'https://microsoft.github.io' ) {
612+ return ;
613+ }
614+
615+ const message = event . data ;
616+
617+ // Check if this is a hostStatus message
618+ if ( message . type === 'hostStatus' && message . hostStatus === 'ready' ) {
619+ // Send the render request when the host is ready
620+ const renderRequest : {
621+ type : 'hostRenderRequest' ;
622+ title : string ;
623+ markdown ?: string ;
624+ interactiveDocument ?: any ;
625+ } = {
626+ type : 'hostRenderRequest' ,
627+ title : 'Data Formulator Report' ,
628+ markdown : chartifactMarkdown
629+ } ;
630+
631+ chartifactWindow . postMessage ( renderRequest , 'https://microsoft.github.io' ) ;
632+
633+ // Remove the event listener after sending
634+ window . removeEventListener ( 'message' , handleMessage ) ;
635+ }
636+ } ;
637+
638+ // Add event listener for messages from the Chartifact viewer
639+ window . addEventListener ( 'message' , handleMessage ) ;
640+
641+ //showMessage('Opened Chartifact viewer in a new tab', 'success');
642+ } catch ( error ) {
643+ console . error ( 'Error opening Chartifact viewer:' , error ) ;
644+ //showMessage('Failed to prepare Chartifact report', 'error');
645+ }
646+ } ;
0 commit comments