vscode#CustomEditorProvider TypeScript Examples

The following examples show how to use vscode#CustomEditorProvider. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DrawioEditorProviderBinary.ts    From vscode-drawio with GNU General Public License v3.0 4 votes vote down vote up
export class DrawioEditorProviderBinary
	implements CustomEditorProvider<DrawioBinaryDocument>
{
	private readonly onDidChangeCustomDocumentEmitter = new EventEmitter<
		CustomDocumentContentChangeEvent<DrawioBinaryDocument>
	>();

	public readonly onDidChangeCustomDocument =
		this.onDidChangeCustomDocumentEmitter.event;

	public constructor(
		private readonly drawioEditorService: DrawioEditorService
	) {}

	public saveCustomDocument(
		document: DrawioBinaryDocument,
		cancellation: CancellationToken
	): Promise<void> {
		return document.save();
	}

	public saveCustomDocumentAs(
		document: DrawioBinaryDocument,
		destination: Uri,
		cancellation: CancellationToken
	): Promise<void> {
		return document.saveAs(destination);
	}

	public revertCustomDocument(
		document: DrawioBinaryDocument,
		cancellation: CancellationToken
	): Promise<void> {
		return document.loadFromDisk();
	}

	public async backupCustomDocument(
		document: DrawioBinaryDocument,
		context: CustomDocumentBackupContext,
		cancellation: CancellationToken
	): Promise<CustomDocumentBackup> {
		return document.backup(context.destination);
	}

	public async openCustomDocument(
		uri: Uri,
		openContext: CustomDocumentOpenContext,
		token: CancellationToken
	): Promise<DrawioBinaryDocument> {
		const document = new DrawioBinaryDocument(uri, openContext.backupId);
		document.onChange(() => {
			this.onDidChangeCustomDocumentEmitter.fire({
				document,
			});
		});
		document.onInstanceSave(() => {
			commands.executeCommand("workbench.action.files.save");
		});

		return document;
	}

	public async resolveCustomEditor(
		document: DrawioBinaryDocument,
		webviewPanel: WebviewPanel,
		token: CancellationToken
	): Promise<void> {
		try {
			const editor =
				await this.drawioEditorService.createDrawioEditorInWebview(
					webviewPanel,
					{ kind: "drawio", document },
					{ isReadOnly: false }
				);

			document.setDrawioClient(editor.drawioClient);
		} catch (e) {
			window.showErrorMessage(`Failed to open diagram: ${e}`);
			throw e;
		}
	}
}