typed options

This commit is contained in:
aamunger 2022-11-14 12:31:58 -08:00
parent d7c8a6c272
commit a7997f050b
No known key found for this signature in database
GPG key ID: F2CA0C6303FC6B74
4 changed files with 25 additions and 11 deletions

View file

@ -28,6 +28,11 @@ interface JavaScriptRenderingHook {
preEvaluate(outputItem: OutputItem, element: HTMLElement, script: string, signal: AbortSignal): string | undefined | Promise<string | undefined>;
}
interface RenderOptions {
readonly lineLimit: number;
readonly outputScrolling: boolean;
}
function clearContainer(container: HTMLElement) {
while (container.firstChild) {
container.removeChild(container.firstChild);
@ -153,7 +158,7 @@ function renderError(outputInfo: OutputItem, container: HTMLElement, ctx: Render
container.classList.add('error');
}
function renderStream(outputInfo: OutputItem, container: HTMLElement, error: boolean, ctx: RendererContext<void> & { readonly settings: { readonly lineLimit: number; readonly outputScrolling: boolean } }): void {
function renderStream(outputInfo: OutputItem, container: HTMLElement, error: boolean, ctx: RendererContext<void> & { readonly settings: RenderOptions }): void {
const outputContainer = container.parentElement;
if (!outputContainer) {
// should never happen
@ -170,7 +175,7 @@ function renderStream(outputInfo: OutputItem, container: HTMLElement, error: boo
const text = outputInfo.text();
const element = document.createElement('span');
insertOutput(outputInfo.id, [text], ctx.settings.lineLimit, true, element);
insertOutput(outputInfo.id, [text], ctx.settings.lineLimit, ctx.settings.outputScrolling, element);
outputElement.appendChild(element);
return;
}
@ -180,7 +185,7 @@ function renderStream(outputInfo: OutputItem, container: HTMLElement, error: boo
element.classList.add('output-stream');
const text = outputInfo.text();
insertOutput(outputInfo.id, [text], ctx.settings.lineLimit, true, element);
insertOutput(outputInfo.id, [text], ctx.settings.lineLimit, ctx.settings.outputScrolling, element);
while (container.firstChild) {
container.removeChild(container.firstChild);
}
@ -205,7 +210,7 @@ export const activate: ActivationFunction<void> = (ctx) => {
const htmlHooks = new Set<HtmlRenderingHook>();
const jsHooks = new Set<JavaScriptRenderingHook>();
const latestContext = ctx as (RendererContext<void> & { readonly settings: { readonly lineLimit: number; readonly outputScrolling: boolean } });
const latestContext = ctx as (RendererContext<void> & { readonly settings: RenderOptions });
const style = document.createElement('style');
style.textContent = `

View file

@ -241,13 +241,17 @@ export class BackLayerWebView<T extends ICommonCellInfo> extends Themable {
private generateContent(coreDependencies: string, baseUrl: string) {
const renderersData = this.getRendererData();
const preloadsData = this.getStaticPreloadsData();
const renderOptions = {
lineLimit: this.configurationService.getValue<number>(NotebookSetting.textOutputLineLimit) ?? 30,
outputScrolling: this.configurationService.getValue<boolean>(NotebookSetting.outputScrolling) ?? true
};
const preloadScript = preloadsScriptStr(
this.options,
{ dragAndDropEnabled: this.options.dragAndDropEnabled }, // add other options
{ dragAndDropEnabled: this.options.dragAndDropEnabled },
renderOptions,
renderersData,
preloadsData,
this.workspaceTrustManagementService.isWorkspaceTrusted(),
this.configurationService.getValue<number>(NotebookSetting.textOutputLineLimit) ?? 30,
this.nonce);
const enableCsp = this.configurationService.getValue('notebook.experimental.enableCsp');

View file

@ -64,14 +64,19 @@ export interface PreloadOptions {
dragAndDropEnabled: boolean;
}
export interface RenderOptions {
readonly lineLimit: number;
readonly outputScrolling: boolean;
}
interface PreloadContext {
readonly nonce: string;
readonly style: PreloadStyles;
readonly options: PreloadOptions;
readonly renderOptions: RenderOptions;
readonly rendererData: readonly webviewMessages.RendererMetadata[];
readonly staticPreloadsData: readonly webviewMessages.StaticPreloadMetadata[];
readonly isWorkspaceTrusted: boolean;
readonly lineLimit: number;
}
declare function __import(path: string): Promise<any>;
@ -82,7 +87,7 @@ async function webviewPreloads(ctx: PreloadContext) {
let currentOptions = ctx.options;
let isWorkspaceTrusted = ctx.isWorkspaceTrusted;
const lineLimit = ctx.lineLimit;
const lineLimit = ctx.renderOptions.lineLimit;
const acquireVsCodeApi = globalThis.acquireVsCodeApi;
const vscode = acquireVsCodeApi();
@ -2444,14 +2449,14 @@ async function webviewPreloads(ctx: PreloadContext) {
}();
}
export function preloadsScriptStr(styleValues: PreloadStyles, options: PreloadOptions, renderers: readonly webviewMessages.RendererMetadata[], preloads: readonly webviewMessages.StaticPreloadMetadata[], isWorkspaceTrusted: boolean, lineLimit: number, nonce: string) {
export function preloadsScriptStr(styleValues: PreloadStyles, options: PreloadOptions, renderOptions: RenderOptions, renderers: readonly webviewMessages.RendererMetadata[], preloads: readonly webviewMessages.StaticPreloadMetadata[], isWorkspaceTrusted: boolean, nonce: string) {
const ctx: PreloadContext = {
style: styleValues,
options,
renderOptions,
rendererData: renderers,
staticPreloadsData: preloads,
isWorkspaceTrusted,
lineLimit,
nonce,
};
// TS will try compiling `import()` in webviewPreloads, so use a helper function instead

View file

@ -927,7 +927,7 @@ export const NotebookSetting = {
outputFontSize: 'notebook.outputFontSize',
outputFontFamily: 'notebook.outputFontFamily',
kernelPickerMRU: 'notebook.experimental.kernelPicker.mru',
outputScrolling: 'notebook.outputScrolling',
outputScrolling: 'notebook.expermental.outputScrolling',
} as const;
export const enum CellStatusbarAlignment {