From 86f2f29c749c4e4775c55a91f791dcae5b5c45a6 Mon Sep 17 00:00:00 2001 From: jolo-dev Date: Sun, 2 Mar 2025 12:04:40 +0100 Subject: [PATCH] Addressed changes and Sign commit --- d2js/js/src/index.d.ts | 152 +++++++++++++++++++++++++---------------- 1 file changed, 94 insertions(+), 58 deletions(-) diff --git a/d2js/js/src/index.d.ts b/d2js/js/src/index.d.ts index c147c63e4..33b1ef6d2 100644 --- a/d2js/js/src/index.d.ts +++ b/d2js/js/src/index.d.ts @@ -1,59 +1,65 @@ declare module "@terrastruct/d2" { - interface Options { + export interface Options { /** - * @default 0 - * Set the diagram theme ID. - */ - theme?: number; - /** - * @default -1 - * The theme to use when the viewer's browser is in dark mode. - * When left unset --theme is used for both light and dark mode. - * Be aware that explicit styles set in D2 code will still be - * applied and this may produce unexpected results. We plan on - * resolving this by making style maps in D2 light/dark mode - * specific. See https://github.com/terrastruct/d2/issues/831. - */ - darkTheme?: number; - /** - * Set the diagram layout engine to the passed string. For a - * list of available options, run layout. + * @default "dagre" + * Set the diagram layout engine. */ layout?: "elk" | "dagre"; - /** - * @default 100 - * Pixels padded around the rendered diagram. - */ - pad?: number; - /** - * @default -1 - * Scale the output. E.g., 0.5 to halve the default size. - * Default -1 means that SVG's will fit to screen and all others - * will use their default render size. Setting to 1 turns off - * SVG fitting to screen. - */ - scale?: number; + /** * @default false * Renders the diagram to look like it was sketched by hand. */ sketch?: boolean; + + /** + * @default 0 + * Set the diagram theme ID. + */ + themeId?: number; + + /** + * @default -1 + * The theme to use when the viewer's browser is in dark mode. + */ + darkTheme?: number; + + /** + * @default 100 + * Pixels padded around the rendered diagram. + */ + pad?: number; + + /** + * @default -1 + * Scale the output. E.g., 0.5 to halve the default size. + */ + scale?: number; + /** * @default true * Bundle all assets and layers into the output svg. */ bundle?: boolean; + /** - * Center the SVG in the containing viewbox, such as your - * browser screen. + * Center the SVG in the containing viewbox. */ center?: boolean; } export interface CompileRequest { - fs?: { - index: string; - }; + fs?: Record; + options?: Partial; + [key: string]: unknown; + } + + export interface CompileResult { + result: string; + } + + export interface RenderOptions { + diagram: string; options: Options; } @@ -69,79 +75,109 @@ declare module "@terrastruct/d2" { decoded: string; } + export interface InitData { + wasm: ArrayBuffer | string; + wasmExecContent: string | null; + elkContent: string | null; + wasmExecUrl: string | null; + } + + export type MessageType = "init" | "compile" | "render" | "encode" | "decode"; + export type WorkerMessage = | { type: "ready" } | { type: "error"; error: string } | { - type: "result"; - data: string | EncodedResult | DecodedResult; - }; + type: "result"; + data: + | string + | CompileResult + | RenderResult + | EncodedResult + | DecodedResult; + }; - export interface CompileResult { - result: string; - } - - export interface D2Worker { + export interface WorkerInterface { on(event: "message", listener: (data: WorkerMessage) => void): void; on(event: "error", listener: (error: Error) => void): void; onmessage?: (e: { data: WorkerMessage }) => void; - onerror?: (error: Error) => void; - postMessage(message: { type: string; data: object }): void; + onerror?: (error: { message?: string }) => void; + postMessage(message: { type: MessageType; data: unknown }): void; } export class D2 { readonly ready: Promise; - worker: D2Worker; - currentResolve?: ( - result: string | RenderResult | EncodedResult | DecodedResult + private worker: WorkerInterface; + private currentResolve?: ( + result: + | string + | CompileResult + | RenderResult + | EncodedResult + | DecodedResult, ) => void; - currentReject?: (reason: Error) => void; + private currentReject?: (reason: Error) => void; constructor(); /** * Sets up the message handler for the worker. + * @returns A promise that resolves when the worker is ready. */ - setupMessageHandler(): Promise; + private setupMessageHandler(): Promise; /** * Initializes the worker and related resources. + * @returns A promise that resolves when initialization is complete. */ - init(): Promise; + private init(): Promise; /** * Sends a message to the worker. * @param type The type of message. * @param data The message payload. + * @returns A promise that resolves with the response. */ - sendMessage( - type: string, - data: object - ): Promise; + private sendMessage< + T extends + | string + | CompileResult + | RenderResult + | EncodedResult + | DecodedResult, + >( + type: MessageType, + data: unknown, + ): Promise; /** * Compiles the provided input. * @param input A string representing the source or a CompileRequest. * @param options Optional compilation options. + * @returns A promise that resolves with the compiled result. */ - compile(input: string | CompileRequest, options?: Options): Promise; + compile(input: string, options?: Partial): Promise; + compile(input: CompileRequest): Promise; /** * Renders the given diagram. * @param diagram A diagram definition in string form. * @param options Optional rendering options. + * @returns A promise that resolves with the rendered SVG. */ - render(diagram: string, options?: Options): Promise; + render(diagram: string, options?: Partial): Promise; /** * Encodes the provided script. * @param script The script to encode. + * @returns A promise that resolves with the encoded result. */ encode(script: string): Promise; /** * Decodes the provided encoded string. * @param encoded The encoded data. + * @returns A promise that resolves with the decoded result. */ decode(encoded: string): Promise; }