vscode#CustomDocument TypeScript Examples

The following examples show how to use vscode#CustomDocument. 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: SoundPlayerDocument.ts    From vscode-sound-player with MIT License 6 votes vote down vote up
export class SoundPlayerDocument implements CustomDocument {

    readonly parseResult: Promise<AudioData | ErrorMessage>

    constructor(readonly uri: Uri) {
        this.parseResult = this.loadAndParse(uri)
    }

    private async loadAndParse(uri: Uri): Promise<AudioData | ErrorMessage> {
        const buffer = await LoadArrayBuffer(uri)
        try {
            const result = await DecodeAudio(buffer)
            const auidoData: AudioData = {
                type: 'audioData',
                numberOfChannels: result.numberOfChannels,
                length: result.length,
                sampleRate: result.sampleRate,
                channels: []
            }
            for (let i = 0; i < result.numberOfChannels; i++) {
                auidoData.channels.push(...result.getChannelData(i))
            }
            return auidoData
        } catch (e) {
            const error: ErrorMessage = {
                type: 'error',
                message: LogError(e)
            }
            return error
        }
    }

    // eslint-disable-next-line @typescript-eslint/no-empty-function
    dispose(): void { }

}
Example #2
Source File: DrawioEditorProviderBinary.ts    From vscode-drawio with GNU General Public License v3.0 4 votes vote down vote up
export class DrawioBinaryDocument implements CustomDocument {
	private readonly onChangeEmitter = new EventEmitter<void>();
	public readonly onChange = this.onChangeEmitter.event;

	private readonly onInstanceSaveEmitter = new EventEmitter<void>();
	public readonly onInstanceSave = this.onInstanceSaveEmitter.event;

	private _drawioClient: CustomizedDrawioClient | undefined;

	private get drawioClient(): CustomizedDrawioClient {
		return this._drawioClient!;
	}

	private _isDirty = false;
	public get isDirty() {
		return this._isDirty;
	}

	private currentXml: string | undefined;

	public constructor(
		public readonly uri: Uri,
		public readonly backupId: string | undefined
	) {}

	public setDrawioClient(drawioClient: CustomizedDrawioClient): void {
		if (this._drawioClient) {
			throw new Error("Client already set!");
		}
		this._drawioClient = drawioClient;

		drawioClient.onInit.sub(async () => {
			if (this.currentXml) {
				this.drawioClient.loadXmlLike(this.currentXml);
			} else if (this.backupId) {
				const backupFile = Uri.parse(this.backupId);
				const content = await workspace.fs.readFile(backupFile);
				const xml = BufferImpl.from(content).toString("utf-8");
				await this.drawioClient.loadXmlLike(xml);
				this._isDirty = true; // because of backup
			} else {
				this.loadFromDisk();
			}
		});

		drawioClient.onChange.sub((change) => {
			this.currentXml = change.newXml;
			this._isDirty = true;
			this.onChangeEmitter.fire();
		});

		drawioClient.onSave.sub((change) => {
			this.onInstanceSaveEmitter.fire();
		});
	}

	public async loadFromDisk(): Promise<void> {
		this._isDirty = false;
		if (this.uri.fsPath.endsWith(".png")) {
			const buffer = await workspace.fs.readFile(this.uri);
			await this.drawioClient.loadPngWithEmbeddedXml(buffer);
		} else {
			throw new Error("Invalid file extension");
		}
	}

	public save(): Promise<void> {
		this._isDirty = false;
		return this.saveAs(this.uri);
	}

	public async saveAs(target: Uri): Promise<void> {
		const buffer = await this.drawioClient.export(extname(target.path));
		await workspace.fs.writeFile(target, buffer);
	}

	public async backup(destination: Uri): Promise<CustomDocumentBackup> {
		const xml = await this.drawioClient.getXml();
		await workspace.fs.writeFile(
			destination,
			BufferImpl.from(xml, "utf-8")
		);
		return {
			id: destination.toString(),
			delete: async () => {
				try {
					await workspace.fs.delete(destination);
				} catch {
					// no op
				}
			},
		};
	}

	public dispose(): void {
		// no op
	}
}