@@ -43,10 +43,18 @@ export interface PreloadOptions {
4343 dragAndDropEnabled : boolean ;
4444}
4545
46+ interface PreloadContext {
47+ readonly style : PreloadStyles ;
48+ readonly options : PreloadOptions ;
49+ readonly rendererData : readonly RendererMetadata [ ] ;
50+ readonly isWorkspaceTrusted : boolean ;
51+ }
52+
4653declare function __import ( path : string ) : Promise < any > ;
4754
48- async function webviewPreloads ( style : PreloadStyles , options : PreloadOptions , rendererData : readonly RendererMetadata [ ] ) {
49- let currentOptions = options ;
55+ async function webviewPreloads ( ctx : PreloadContext ) {
56+ let currentOptions = ctx . options ;
57+ let isWorkspaceTrusted = ctx . isWorkspaceTrusted ;
5058
5159 const acquireVsCodeApi = globalThis . acquireVsCodeApi ;
5260 const vscode = acquireVsCodeApi ( ) ;
@@ -133,6 +141,7 @@ async function webviewPreloads(style: PreloadStyles, options: PreloadOptions, re
133141 getRenderer ( id : string ) : Promise < any | undefined > ;
134142 postMessage ?( message : unknown ) : void ;
135143 onDidReceiveMessage ?: Event < unknown > ;
144+ readonly workspace : { readonly isTrusted : boolean } ;
136145 }
137146
138147 interface ScriptModule {
@@ -207,7 +216,7 @@ async function webviewPreloads(style: PreloadStyles, options: PreloadOptions, re
207216 if ( entry . target . id === observedElementInfo . id && entry . contentRect ) {
208217 if ( observedElementInfo . output ) {
209218 if ( entry . contentRect . height !== 0 ) {
210- entry . target . style . padding = `${ style . outputNodePadding } px 0 ${ style . outputNodePadding } px 0` ;
219+ entry . target . style . padding = `${ ctx . style . outputNodePadding } px 0 ${ ctx . style . outputNodePadding } px 0` ;
211220 } else {
212221 entry . target . style . padding = `0px` ;
213222 }
@@ -549,12 +558,12 @@ async function webviewPreloads(style: PreloadStyles, options: PreloadOptions, re
549558 if ( offsetHeight !== 0 && cps . padding === '0px' ) {
550559 // we set padding to zero if the output height is zero (then we can have a zero-height output DOM node)
551560 // thus we need to ensure the padding is accounted when updating the init height of the output
552- dimensionUpdater . updateHeight ( outputId , offsetHeight + style . outputNodePadding * 2 , {
561+ dimensionUpdater . updateHeight ( outputId , offsetHeight + ctx . style . outputNodePadding * 2 , {
553562 isOutput : true ,
554563 init : true ,
555564 } ) ;
556565
557- outputNode . style . padding = `${ style . outputNodePadding } px 0 ${ style . outputNodePadding } px 0` ;
566+ outputNode . style . padding = `${ ctx . style . outputNodePadding } px 0 ${ ctx . style . outputNodePadding } px 0` ;
558567 } else {
559568 dimensionUpdater . updateHeight ( outputId , outputNode . offsetHeight , {
560569 isOutput : true ,
@@ -657,6 +666,12 @@ async function webviewPreloads(style: PreloadStyles, options: PreloadOptions, re
657666 currentOptions = event . data . options ;
658667 viewModel . toggleDragDropEnabled ( currentOptions . dragAndDropEnabled ) ;
659668 break ;
669+
670+ case 'updateWorkspaceTrust' : {
671+ isWorkspaceTrusted = event . data . isTrusted ;
672+ viewModel . rerenderMarkupCells ( ) ;
673+ break ;
674+ }
660675 }
661676 } ) ;
662677
@@ -700,6 +715,9 @@ async function webviewPreloads(style: PreloadStyles, options: PreloadOptions, re
700715 // TODO: This is async so that we can return a promise to the API in the future.
701716 // Currently the API is always resolved before we call `createRendererContext`.
702717 getRenderer : async ( id : string ) => renderers . getRenderer ( id ) ?. api ,
718+ workspace : {
719+ get isTrusted ( ) { return isWorkspaceTrusted ; }
720+ }
703721 } ;
704722
705723 if ( messaging ) {
@@ -722,7 +740,7 @@ async function webviewPreloads(style: PreloadStyles, options: PreloadOptions, re
722740
723741 // Squash any errors extends errors. They won't prevent the renderer
724742 // itself from working, so just log them.
725- await Promise . all ( rendererData
743+ await Promise . all ( ctx . rendererData
726744 . filter ( d => d . extends === this . data . id )
727745 . map ( d => this . loadExtension ( d . id ) . catch ( console . error ) ) ,
728746 ) ;
@@ -807,7 +825,7 @@ async function webviewPreloads(style: PreloadStyles, options: PreloadOptions, re
807825 private readonly _renderers = new Map < /* id */ string , Renderer > ( ) ;
808826
809827 constructor ( ) {
810- for ( const renderer of rendererData ) {
828+ for ( const renderer of ctx . rendererData ) {
811829 this . _renderers . set ( renderer . id , new Renderer ( renderer , async ( extensionId ) => {
812830 const ext = this . _renderers . get ( extensionId ) ;
813831 if ( ! ext ) {
@@ -936,6 +954,12 @@ async function webviewPreloads(style: PreloadStyles, options: PreloadOptions, re
936954 cell ?. unhide ( ) ;
937955 }
938956
957+ public rerenderMarkupCells ( ) {
958+ for ( const cell of this . _markupCells . values ( ) ) {
959+ cell . rerender ( ) ;
960+ }
961+ }
962+
939963 private getExpectedMarkupCell ( id : string ) : MarkupCell | undefined {
940964 const cell = this . _markupCells . get ( id ) ;
941965 if ( ! cell ) {
@@ -1166,6 +1190,10 @@ async function webviewPreloads(style: PreloadStyles, options: PreloadOptions, re
11661190 }
11671191 }
11681192
1193+ public rerender ( ) {
1194+ this . updateContentAndRender ( this . _content ) ;
1195+ }
1196+
11691197 public hide ( ) {
11701198 this . element . style . visibility = 'hidden' ;
11711199 }
@@ -1409,14 +1437,18 @@ export interface RendererMetadata {
14091437 readonly messaging : boolean ;
14101438}
14111439
1412- export function preloadsScriptStr ( styleValues : PreloadStyles , options : PreloadOptions , renderers : readonly RendererMetadata [ ] ) {
1413- // TS will try compiling `import()` in webviePreloads, so use an helper function instead
1440+ export function preloadsScriptStr ( styleValues : PreloadStyles , options : PreloadOptions , renderers : readonly RendererMetadata [ ] , isWorkspaceTrusted : boolean ) {
1441+ const ctx : PreloadContext = {
1442+ style : styleValues ,
1443+ options,
1444+ rendererData : renderers ,
1445+ isWorkspaceTrusted
1446+ } ;
1447+ // TS will try compiling `import()` in webviewPreloads, so use an helper function instead
14141448 // of using `import(...)` directly
14151449 return `
14161450 const __import = (x) => import(x);
14171451 (${ webviewPreloads } )(
1418- JSON.parse(decodeURIComponent("${ encodeURIComponent ( JSON . stringify ( styleValues ) ) } ")),
1419- JSON.parse(decodeURIComponent("${ encodeURIComponent ( JSON . stringify ( options ) ) } ")),
1420- JSON.parse(decodeURIComponent("${ encodeURIComponent ( JSON . stringify ( renderers ) ) } "))
1452+ JSON.parse(decodeURIComponent("${ encodeURIComponent ( JSON . stringify ( ctx ) ) } "))
14211453 )\n//# sourceURL=notebookWebviewPreloads.js\n` ;
14221454}
0 commit comments