This commit is contained in:
jolo-dev 2025-02-23 18:53:25 +01:00
parent b5999e8c83
commit 3a2d32712a

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

@ -1,157 +1,148 @@
declare module "@terrastruct/d2" { declare module "@terrastruct/d2" {
interface Options { interface Options {
/** /**
* @default 0 * @default 0
* Set the diagram theme ID. * Set the diagram theme ID.
*/ */
theme?: number; theme?: number;
/** /**
* @default -1 * @default -1
* The theme to use when the viewer's browser is in dark mode. * 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. * When left unset --theme is used for both light and dark mode.
* Be aware that explicit styles set in D2 code will still be * Be aware that explicit styles set in D2 code will still be
* applied and this may produce unexpected results. We plan on * applied and this may produce unexpected results. We plan on
* resolving this by making style maps in D2 light/dark mode * resolving this by making style maps in D2 light/dark mode
* specific. See https://github.com/terrastruct/d2/issues/831. * specific. See https://github.com/terrastruct/d2/issues/831.
*/ */
darkTheme?: number; darkTheme?: number;
/** /**
* Set the diagram layout engine to the passed string. For a * Set the diagram layout engine to the passed string. For a
* list of available options, run layout. * list of available options, run layout.
*/ */
layout?: "elk" | "dagre"; layout?: "elk" | "dagre";
/** /**
* @default 100 * @default 100
* Pixels padded around the rendered diagram. * Pixels padded around the rendered diagram.
*/ */
pad?: number; pad?: number;
/** /**
* @default -1 * @default -1
* Scale the output. E.g., 0.5 to halve the default size. * 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 * Default -1 means that SVG's will fit to screen and all others
* will use their default render size. Setting to 1 turns off * will use their default render size. Setting to 1 turns off
* SVG fitting to screen. * SVG fitting to screen.
*/ */
scale?: number; 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 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, such as your
* browser screen. * browser screen.
*/ */
center?: boolean; center?: boolean;
} }
export interface CompileRequest { export interface CompileRequest {
fs?: { fs?: {
index: string; index: string;
}; };
options: Options; options: Options;
} }
export interface RenderResult { export interface RenderResult {
svg: string; svg: string;
} }
export interface EncodedResult { export interface EncodedResult {
encoded: string; encoded: string;
} }
export interface DecodedResult { export interface DecodedResult {
decoded: string; decoded: string;
} }
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 | EncodedResult | DecodedResult;
}; };
export interface CompileResult { export interface CompileResult {
result: string; result: string;
} }
export interface D2Worker { 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: Error) => void;
postMessage(message: { type: string; data: object }): void; postMessage(message: { type: string; data: object }): void;
} }
export class D2 { export class D2 {
readonly ready: Promise<void>; readonly ready: Promise<void>;
worker: D2Worker; worker: D2Worker;
currentResolve?: ( currentResolve?: (
result: result: string | RenderResult | EncodedResult | DecodedResult
| string ) => void;
| RenderResult currentReject?: (reason: Error) => void;
| EncodedResult
| DecodedResult,
) => void;
currentReject?: (reason: Error) => void;
constructor(); constructor();
/** /**
* Sets up the message handler for the worker. * Sets up the message handler for the worker.
*/ */
setupMessageHandler(): Promise<void>; setupMessageHandler(): Promise<void>;
/** /**
* Initializes the worker and related resources. * Initializes the worker and related resources.
*/ */
init(): Promise<void>; 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.
*/ */
sendMessage( sendMessage(
type: string, type: string,
data: object, data: object
): Promise< ): Promise<CompileResult | RenderResult | EncodedResult | DecodedResult>;
CompileResult | RenderResult | EncodedResult | DecodedResult
>;
/** /**
* 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.
*/ */
compile( compile(input: string | CompileRequest, options?: Options): Promise<string>;
input: string | CompileRequest,
options?: Options,
): Promise<string>;
/** /**
* 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.
*/ */
render(diagram: string, options?: Options): Promise<string>; render(diagram: string, options?: Options): Promise<string>;
/** /**
* Encodes the provided script. * Encodes the provided script.
* @param script The script to encode. * @param script The script to encode.
*/ */
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.
*/ */
decode(encoded: string): Promise<DecodedResult>; decode(encoded: string): Promise<DecodedResult>;
} }
} }