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

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

@ -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<string, string>;
options?: Partial<Options>;
[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;
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<void>;
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<void>;
private setupMessageHandler(): Promise<void>;
/**
* 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.
* @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<CompileResult | RenderResult | EncodedResult | DecodedResult>;
private sendMessage<
T extends
| string
| CompileResult
| RenderResult
| EncodedResult
| DecodedResult,
>(
type: MessageType,
data: unknown,
): Promise<T>;
/**
* 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<string>;
compile(input: string, options?: Partial<Options>): Promise<CompileResult>;
compile(input: CompileRequest): Promise<CompileResult>;
/**
* 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<string>;
render(diagram: string, options?: Partial<Options>): Promise<RenderResult>;
/**
* Encodes the provided script.
* @param script The script to encode.
* @returns A promise that resolves with the encoded result.
*/
encode(script: string): Promise<EncodedResult>;
/**
* Decodes the provided encoded string.
* @param encoded The encoded data.
* @returns A promise that resolves with the decoded result.
*/
decode(encoded: string): Promise<DecodedResult>;
}