vscode-notebook-renderer: init (#45731)

* vscode-notebook-renderer: init

* fixup! bad username link
This commit is contained in:
Connor Peet 2020-06-26 16:08:32 -07:00 committed by GitHub
parent b878c634d6
commit dedd90e20c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 114 additions and 0 deletions

View File

@ -0,0 +1,49 @@
// Type definitions for non-npm package vscode-notebook-renderer 1.47
// Project: https://github.com/microsoft/vscode-docs/blob/notebook/api/extension-guides/notebook.md
// Definitions by: Connor Peet <https://github.com/connor4312>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// Minimum TypeScript Version: 3.0
// todo: update "Project" link above to docs site, once it becomes available
export interface Disposable {
dispose(): void;
}
export interface VSCodeEvent<T> {
(listener: (e: T) => any, thisArgs?: any, disposables?: Disposable[]): Disposable;
}
export interface NotebookRendererApi<T> {
setState(value: T): void;
getState(): T | undefined;
/**
* Sends a message to the renderer extension code. Can be received in
* the `onDidReceiveMessage` event in `NotebookCommunication`.
*/
postMessage(msg: unknown): void;
/**
* Fired before an output is destroyed, with its output ID, or undefined if
* all cells are about to unmount.
*/
onWillDestroyOutput: VSCodeEvent<{ outputId: string } | undefined>;
/**
* Fired when an output is rendered. The `outputId` provided is the same
* as the one given in `NotebookOutputRenderer.render` in the extension
* API, and `onWillDestroyOutput`.
*/
onDidCreateOutput: VSCodeEvent<{ element: HTMLElement; outputId: string }>;
/**
* Called when the renderer uses `postMessage` on the NotebookCommunication
* instance for this renderer.
*/
onDidReceiveMessage: VSCodeEvent<any>;
}
declare global {
function acquireNotebookRendererApi(rendererId: string): NotebookRendererApi<any>;
}

View File

@ -0,0 +1,24 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6",
"DOM"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictFunctionTypes": true,
"strictNullChecks": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"vscode-notebook-renderer-tests.ts"
]
}

View File

@ -0,0 +1,6 @@
{
"extends": "dtslint/dt.json",
"rules": {
"npm-naming": false
}
}

View File

@ -0,0 +1,35 @@
import { NotebookRendererApi } from 'vscode-notebook-renderer';
const notebookApi: NotebookRendererApi<{ cool: boolean }> = acquireNotebookRendererApi('myRendererId');
const prevState = notebookApi.getState();
// $ExpectError
prevState.cool;
if (prevState) {
console.log('cool?', prevState.cool);
}
notebookApi.setState({ cool: true });
const listener = notebookApi.onDidCreateOutput(({ element, outputId }) => {
// $ExpectType string
outputId;
// $ExpectType HTMLElement
element;
});
listener.dispose();
notebookApi.onDidReceiveMessage(msg => {
// $ExpectType any
msg;
notebookApi.postMessage(msg);
});
notebookApi.onWillDestroyOutput(evt => {
// $ExpectType { outputId: string; } | undefined
evt;
});