Addressed changes and Sign commit

This commit is contained in:
jolo-dev 2025-03-02 12:04:40 +01:00
parent 3a2d32712a
commit 86f2f29c74
No known key found for this signature in database

152
d2js/js/src/index.d.ts vendored
View file

@ -1,59 +1,65 @@
declare module "@terrastruct/d2" { declare module "@terrastruct/d2" {
interface Options { export interface Options {
/** /**
* @default 0 * @default "dagre"
* Set the diagram theme ID. * Set the diagram layout engine.
*/
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.
*/ */
layout?: "elk" | "dagre"; 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 * @default false
* Renders the diagram to look like it was sketched by hand. * Renders the diagram to look like it was sketched by hand.
*/ */
sketch?: boolean; 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 * @default true
* Bundle all assets and layers into the output svg. * Bundle all assets and layers into the output svg.
*/ */
bundle?: boolean; bundle?: boolean;
/** /**
* Center the SVG in the containing viewbox, such as your * Center the SVG in the containing viewbox.
* browser screen.
*/ */
center?: boolean; center?: boolean;
} }
export interface CompileRequest { export interface CompileRequest {
fs?: { fs?: Record<string, string>;
index: string; options?: Partial<Options>;
}; [key: string]: unknown;
}
export interface CompileResult {
result: string;
}
export interface RenderOptions {
diagram: string;
options: Options; options: Options;
} }
@ -69,79 +75,109 @@ declare module "@terrastruct/d2" {
decoded: string; 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 = export type WorkerMessage =
| { type: "ready" } | { type: "ready" }
| { type: "error"; error: string } | { type: "error"; error: string }
| { | {
type: "result"; type: "result";
data: string | EncodedResult | DecodedResult; data:
}; | string
| CompileResult
| RenderResult
| EncodedResult
| DecodedResult;
};
export interface CompileResult { export interface WorkerInterface {
result: string;
}
export interface D2Worker {
on(event: "message", listener: (data: WorkerMessage) => void): void; on(event: "message", listener: (data: WorkerMessage) => void): void;
on(event: "error", listener: (error: Error) => void): void; on(event: "error", listener: (error: Error) => void): void;
onmessage?: (e: { data: WorkerMessage }) => void; onmessage?: (e: { data: WorkerMessage }) => void;
onerror?: (error: Error) => void; onerror?: (error: { message?: string }) => void;
postMessage(message: { type: string; data: object }): void; postMessage(message: { type: MessageType; data: unknown }): void;
} }
export class D2 { export class D2 {
readonly ready: Promise<void>; readonly ready: Promise<void>;
worker: D2Worker; private worker: WorkerInterface;
currentResolve?: ( private currentResolve?: (
result: string | RenderResult | EncodedResult | DecodedResult result:
| string
| CompileResult
| RenderResult
| EncodedResult
| DecodedResult,
) => void; ) => void;
currentReject?: (reason: Error) => void; private currentReject?: (reason: Error) => void;
constructor(); constructor();
/** /**
* Sets up the message handler for the worker. * Sets up the message handler for the worker.
* @returns A promise that resolves when the worker is ready.
*/ */
setupMessageHandler(): Promise<void>; private setupMessageHandler(): Promise<void>;
/** /**
* Initializes the worker and related resources. * Initializes the worker and related resources.
* @returns A promise that resolves when initialization is complete.
*/ */
init(): Promise<void>; private init(): Promise<void>;
/** /**
* Sends a message to the worker. * Sends a message to the worker.
* @param type The type of message. * @param type The type of message.
* @param data The message payload. * @param data The message payload.
* @returns A promise that resolves with the response.
*/ */
sendMessage( private sendMessage<
type: string, T extends
data: object | string
): Promise<CompileResult | RenderResult | EncodedResult | DecodedResult>; | CompileResult
| RenderResult
| EncodedResult
| DecodedResult,
>(
type: MessageType,
data: unknown,
): Promise<T>;
/** /**
* Compiles the provided input. * Compiles the provided input.
* @param input A string representing the source or a CompileRequest. * @param input A string representing the source or a CompileRequest.
* @param options Optional compilation options. * @param options Optional compilation options.
* @returns A promise that resolves with the compiled result.
*/ */
compile(input: string | CompileRequest, options?: Options): Promise<string>; compile(input: string, options?: Partial<Options>): Promise<CompileResult>;
compile(input: CompileRequest): Promise<CompileResult>;
/** /**
* Renders the given diagram. * Renders the given diagram.
* @param diagram A diagram definition in string form. * @param diagram A diagram definition in string form.
* @param options Optional rendering options. * @param options Optional rendering options.
* @returns A promise that resolves with the rendered SVG.
*/ */
render(diagram: string, options?: Options): Promise<string>; render(diagram: string, options?: Partial<Options>): Promise<RenderResult>;
/** /**
* Encodes the provided script. * Encodes the provided script.
* @param script The script to encode. * @param script The script to encode.
* @returns A promise that resolves with the encoded result.
*/ */
encode(script: string): Promise<EncodedResult>; encode(script: string): Promise<EncodedResult>;
/** /**
* Decodes the provided encoded string. * Decodes the provided encoded string.
* @param encoded The encoded data. * @param encoded The encoded data.
* @returns A promise that resolves with the decoded result.
*/ */
decode(encoded: string): Promise<DecodedResult>; decode(encoded: string): Promise<DecodedResult>;
} }