UNPKG

squarified

Version:
519 lines (498 loc) 20.5 kB
// eslint-disable-next-line @typescript-eslint/no-explicit-any type Any = any type AnyObject = Record<keyof Any, Any> type NonNull = NonNullable<unknown> interface MatrixLoc { a: number; b: number; c: number; d: number; e: number; f: number; } declare class Matrix2D implements MatrixLoc { a: number; b: number; c: number; d: number; e: number; f: number; constructor(loc?: Partial<MatrixLoc>); create(loc: MatrixLoc): this; transform(x: number, y: number, scaleX: number, scaleY: number, rotation: number, skewX: number, skewY: number): this; translation(x: number, y: number): this; scale(a: number, d: number): this; private skew; private roate; } declare const enum DisplayType { Graph = "Graph", Box = "Box", Text = "Text", RoundRect = "RoundRect" } declare abstract class Display { parent: Display | null; id: number; matrix: Matrix2D; abstract get __instanceOf__(): DisplayType; constructor(); destory(): void; } interface GraphStyleSheet { stroke: string; opacity: number; font: string; lineWidth: number; } interface LocOptions { width: number; height: number; x: number; y: number; scaleX: number; scaleY: number; rotation: number; skewX: number; skewY: number; } interface GraphOptions extends LocOptions { [key: string]: unknown; } interface InstructionAssignMappings { fillStyle: (arg: string) => void; strokeStyle: (arg: string) => void; font: (arg: string) => void; lineWidth: (arg: number) => void; textAlign: (arg: CanvasTextAlign) => void; textBaseline: (arg: CanvasTextBaseline) => void; } interface InstructionWithFunctionCall extends CanvasDrawImage { fillRect: (x: number, y: number, w: number, h: number) => void; strokeRect: (x: number, y: number, w: number, h: number) => void; fillText: (text: string, x: number, y: number, maxWidth?: number) => void; beginPath: () => void; moveTo: (x: number, y: number) => void; arcTo: (x1: number, y1: number, x2: number, y2: number, radius: number) => void; closePath: () => void; fill: () => void; stroke: () => void; } type Mod<T extends InstructionAssignMappings & InstructionWithFunctionCall = InstructionAssignMappings & InstructionWithFunctionCall, K extends keyof T = keyof T> = T[K] extends (...args: Any) => Any ? [K, Parameters<T[K]>] : never; interface Instruction extends InstructionAssignMappings, InstructionWithFunctionCall { mods: Array<{ mod: Mod; type: number; }>; } declare function createInstruction(): Instruction; declare abstract class S extends Display { width: number; height: number; x: number; y: number; scaleX: number; scaleY: number; rotation: number; skewX: number; skewY: number; constructor(options?: Partial<LocOptions>); } declare abstract class Graph extends S { instruction: ReturnType<typeof createInstruction>; __options__: Partial<LocOptions>; abstract style: GraphStyleSheet; constructor(options?: Partial<GraphOptions>); abstract create(): void; abstract clone(): Graph; abstract get __shape__(): DisplayType; render(ctx: CanvasRenderingContext2D): void; get __instanceOf__(): DisplayType.Graph; } declare abstract class C extends Display { elements: Display[]; constructor(); abstract get __instanceOf__(): DisplayType; add(...elements: Display[]): void; remove(...elements: Display[]): void; destory(): void; } declare class Box extends C { elements: Display[]; constructor(); add(...elements: Display[]): void; remove(...elements: Display[]): void; destory(): void; get __instanceOf__(): DisplayType.Box; clone(): Box; } interface RGBColor { r: number; g: number; b: number; a?: number; } interface HLSColor { h: number; l: number; s: number; a?: number; } interface ColorDecoratorResultHLS { mode?: 'hsl'; desc: HLSColor; } interface ColorDecoratorResultRGB { mode: 'rgb'; desc: RGBColor; } type ColorDecoratorResult = ColorDecoratorResultHLS | ColorDecoratorResultRGB; type RectStyleOptions = GraphStyleSheet & { fill: ColorDecoratorResult; padding?: number; }; type RectOptions = GraphOptions & { style: Partial<RectStyleOptions>; }; type RoundRectStyleOptions = RectStyleOptions & { radius: number; }; type RoundRectOptions = RectOptions & { style: Partial<RoundRectStyleOptions>; }; declare class RoundRect extends Graph { style: Required<RoundRectStyleOptions>; constructor(options?: Partial<RoundRectOptions>); get __shape__(): DisplayType; create(): void; clone(): RoundRect; } interface TextOptions extends Omit<GraphOptions, 'style'> { text: string; style: Partial<GraphStyleSheet & { font: string; textAlign: CanvasTextAlign; baseline: CanvasTextBaseline; lineWidth: number; fill: string; }>; } declare class Text extends Graph { text: string; style: Required<TextOptions['style']>; constructor(options?: Partial<TextOptions>); create(): void; clone(): Text; get __shape__(): DisplayType; } type EventCallback<P = Any[]> = P extends Any[] ? (...args: P) => Any : never; type DefaultEventDefinition = Record<string, EventCallback>; type BindThisParameter<T, C = unknown> = T extends (...args: infer P) => infer R ? (this: C, ...args: P) => R : never; interface EventCollectionData<EvtDefinition extends DefaultEventDefinition, C = unknown> { name: string; handler: BindThisParameter<EvtDefinition[keyof EvtDefinition], C>; ctx: C; silent: boolean; } type EventCollections<EvtDefinition extends DefaultEventDefinition> = Record<keyof EvtDefinition, EventCollectionData<EvtDefinition>[]>; declare class Event<EvtDefinition extends DefaultEventDefinition = DefaultEventDefinition> { eventCollections: EventCollections<EvtDefinition>; constructor(); on<C, Evt extends keyof EvtDefinition>(evt: Evt, handler: BindThisParameter<EvtDefinition[Evt], unknown extends C ? this : C>, c?: C): void; off(evt: keyof EvtDefinition, handler?: BindThisParameter<EvtDefinition[keyof EvtDefinition], unknown>): void; silent(evt: keyof EvtDefinition, handler?: BindThisParameter<EvtDefinition[keyof EvtDefinition], unknown>): void; active(evt: keyof EvtDefinition, handler?: BindThisParameter<EvtDefinition[keyof EvtDefinition], unknown>): void; emit(evt: keyof EvtDefinition, ...args: Parameters<EvtDefinition[keyof EvtDefinition]>): void; bindWithContext<C>(c: C): (evt: keyof EvtDefinition, handler: BindThisParameter<EvtDefinition[keyof EvtDefinition], unknown extends C ? this : C>) => void; } interface RenderViewportOptions { width: number; height: number; devicePixelRatio: number; shaow?: boolean; } declare class Render { options: RenderViewportOptions; private container; constructor(to: HTMLElement, options: RenderViewportOptions); clear(width: number, height: number): void; get canvas(): HTMLCanvasElement; get ctx(): CanvasRenderingContext2D; initOptions(userOptions?: Partial<RenderViewportOptions>): void; destory(): void; } type ApplyTo = string | HTMLElement; declare class Schedule<D extends DefaultEventDefinition = DefaultEventDefinition> extends Box { render: Render; to: HTMLElement; event: Event<D>; constructor(to: ApplyTo, renderOptions?: Partial<RenderViewportOptions>); update(): void; execute(render: Render, graph?: Display): void; } type Series<T> = { max: T; min: T; }; interface GraphicLayout { rectRadius?: number; rectGap?: number; titleAreaHeight?: Series<number>; } interface GraphicFont { color?: string; fontSize?: Series<number>; family?: string; } interface GraphicConfig { layout?: GraphicLayout; font?: GraphicFont; } declare function sortChildrenByKey<T extends AnyObject, K extends keyof T = 'weight'>(data: T[], ...keys: K[]): T[]; declare function c2m<T extends AnyObject & { groups: Any[]; }, K extends keyof T>(data: T, key: K, modifier?: (data: T) => T): T & { weight: number; }; declare function flatten<T extends AnyObject & { groups: T[]; }>(data: T[]): Omit<T, "groups">[]; type Module = ReturnType<typeof c2m>; declare function bindParentForModule<T extends Module & { parent: Module; }>(modules: Module[], parent?: Module): T[]; type NativeModule = ReturnType<typeof bindParentForModule>[number] & { id: string; parent: NativeModule | null; groups: NativeModule[]; }; declare function getNodeDepth(node: NativeModule): number; declare function visit<T extends AnyObject>(data: T[], fn: (data: T) => boolean | void): T | null; declare function findRelativeNode(p: { x: number; y: number; }, layoutNodes: LayoutModule[]): LayoutModule | null; declare function findRelativeNodeById(id: string, layoutNodes: LayoutModule[]): LayoutModule | null; type Rect = { w: number; h: number; x: number; y: number; }; type SquarifiedModule = NativeModule & { parent: SquarifiedModule | null; groups: SquarifiedModule[]; isCombinedNode?: boolean; originalNodeCount?: number; originalNodes?: SquarifiedModule[]; }; interface LayoutModule { node: SquarifiedModule; layout: [number, number, number, number]; children: LayoutModule[]; config: { titleAreaHeight: number; rectGap: number; rectRadius: number; }; } declare function squarify(data: NativeModule[], rect: Rect, config: Required<GraphicLayout>, scale?: number): LayoutModule[]; declare function hashCode(str: string): number; declare function perferNumeric(s: string | number): boolean; declare function noop(): void; declare function createRoundBlock(x: number, y: number, width: number, height: number, style?: Partial<RoundRectStyleOptions>): RoundRect; declare function createTitleText(text: string, x: number, y: number, font: string, color: string): Text; declare const raf: ((callback: FrameRequestCallback) => number) & typeof requestAnimationFrame; declare function createCanvasElement(): HTMLCanvasElement; declare function applyCanvasTransform(ctx: CanvasRenderingContext2D, matrix: Matrix2D, dpr: number): void; interface InheritedCollectionsWithParamter<T = Any> { name: string; fn: (instance: T) => (...args: Any[]) => Any; } type MixinHelpWithParamater<T extends InheritedCollectionsWithParamter[]> = T extends [infer L, ...infer R] ? L extends InheritedCollectionsWithParamter ? R extends InheritedCollectionsWithParamter[] ? { [key in L['name']]: ReturnType<L['fn']>; } & MixinHelpWithParamater<R> : Record<string, never> : Record<string, never> : Record<string, never>; declare function mixin<T extends AnyObject, const M extends InheritedCollectionsWithParamter<T>[]>(app: T, methods: M): T & MixinHelpWithParamater<M>; declare function typedForIn<T extends NonNullable<object>>(obj: T, callback: (key: keyof T, value: T[keyof T]) => void): void; declare function stackMatrixTransform(graph: S, e: number, f: number, scale: number): void; declare function stackMatrixTransformWithGraphAndLayer(graphs: Display[], e: number, f: number, scale: number): void; interface EffectOptions { duration: number; onStop?: () => void; deps?: Array<() => boolean>; } declare function smoothFrame(callback: (progress: number, cleanup: () => void) => void, opts: EffectOptions): void; interface DuckE { which: number; } declare function isScrollWheelOrRightButtonOnMouseupAndDown<E extends DuckE = DuckE>(e: E): boolean; declare class DefaultMap<K, V> extends Map<K, V> { private defaultFactory; constructor(defaultFactory: () => V, entries?: readonly [K, V][] | null); get(key: K): V; getOrInsert(key: K, value?: V): V; } interface PluginContext { resolveModuleById: (id: string) => LayoutModule | null; getPluginMetadata: <M = Any>(pluginName: string) => M | null; get instance(): Component; } interface OnModuleInitResult { colorMappings?: ColorMappings; } interface OnLayoutCalculatedResult { layoutNodes?: LayoutModule[]; } interface PluginHooks { onLoad?: (this: PluginContext, treemapContext: BasicTreemapInstance, domEvent: DOMEvent) => void | Record<string, Any>; onModuleInit?: (this: PluginContext, modules: LayoutModule[]) => OnModuleInitResult | void; onDOMEventTriggered?: <N extends DOMEventType>(this: PluginContext, name: N, event: DOMEventMetadata<N>, module: LayoutModule | null, domEvent: DOMEvent) => void; onLayoutCalculated?: (this: PluginContext, layoutNodes: LayoutModule[], rect: Rect, viewport: Required<GraphicLayout>) => OnLayoutCalculatedResult | void; onResize?: (this: PluginContext, domEvent: DOMEvent) => void; onDispose?: (this: PluginContext) => void; } type BasicPluginHooks = Pick<PluginHooks, 'onLoad' | 'onDOMEventTriggered' | 'onResize' | 'onDispose'>; type CascadedPluginHooks = Pick<PluginHooks, 'onModuleInit' | 'onLayoutCalculated'>; interface Plugin<T = string, M = Any> extends PluginHooks { name: T; meta?: M; } declare function definePlugin<T extends string = string, M = Any, P extends Plugin<T, M> = Plugin<T, M>>(plugin: P): P; declare class PluginDriver<T extends Component> { private plugins; private pluginContext; constructor(component: T); use(plugin: Plugin): void; runHook<K extends keyof BasicPluginHooks>(hookName: K, ...args: Parameters<NonNullable<BasicPluginHooks[K]>>): void; cascadeHook<K extends keyof CascadedPluginHooks>(hookName: K, ...args: Parameters<NonNullable<CascadedPluginHooks[K]>>): ReturnType<NonNullable<CascadedPluginHooks[K]>>; getPluginMetadata<M = Any>(pluginName: string): M | null; } type ColorMappings = Record<string, ColorDecoratorResult>; interface AABB { x: number; y: number; width: number; height: number; } declare class Component extends Schedule { pluginDriver: PluginDriver<Component>; data: NativeModule[]; colorMappings: ColorMappings; rectLayer: Box; textLayer: Box; layoutNodes: LayoutModule[]; config: GraphicConfig; caches: DefaultMap<string, number>; constructor(config: GraphicConfig, ...args: ConstructorParameters<typeof Schedule>); clearFontCacheInAABB(aabb: AABB): void; private getModulesInAABB; getViewportAABB(matrixE: number, matrixF: number): AABB; private getAABBUnion; handleTransformCacheInvalidation(oldMatrix: { e: number; f: number; }, newMatrix: { e: number; f: number; }): void; private isAABBIntersecting; private drawBroundRect; private drawText; draw(flush?: boolean, update?: boolean): void; cleanup(): void; calculateLayoutNodes(data: NativeModule[], rect: Parameters<typeof squarify>[1], scale?: number): LayoutModule[]; } declare const DOM_EVENTS: readonly ["click", "mousedown", "mousemove", "mouseup", "mouseover", "mouseout", "wheel", "contextmenu"]; type DOMEventType = typeof DOM_EVENTS[number]; interface DOMEventMetadata<T extends keyof HTMLElementEventMap = Any> { native: HTMLElementEventMap[T]; readonly kind: T; } type DOMEventCallback<T extends DOMEventType> = (metadata: DOMEventMetadata<T>) => void; interface PrimitiveEventMetadata<T extends keyof HTMLElementEventMap> { native: HTMLElementEventMap[T]; module: LayoutModule | null; } type ExposedEventCallback<T extends DOMEventType> = (metadata: PrimitiveEventMetadata<T>) => void; type ExposedEventDefinition = { [K in DOMEventType]: BindThisParameter<ExposedEventCallback<K>, AnyObject>; }; interface ExposedEventMethods<C = AnyObject, D = ExposedEventDefinition> { on<Evt extends keyof D | (string & {})>(evt: Evt, handler: BindThisParameter<Evt extends keyof D ? D[Evt] : Any, unknown extends C ? this : C>): void; off<Evt extends keyof D>(evt: keyof D, handler?: BindThisParameter<D[Evt], unknown extends C ? this : C>): void; } type DOMEVEntDefinition = { [K in DOMEventType]: BindThisParameter<DOMEventCallback<K>, unknown>; } & { __exposed__: <D extends DOMEventType | (string & {})>(type: D, metadata: D extends DOMEventType ? PrimitiveEventMetadata<D> : Any) => void; }; declare const STATE_TRANSITION: { readonly IDLE: "IDLE"; readonly PRESSED: "PRESSED"; readonly DRAGGING: "DRAGGING"; readonly ZOOMING: "ZOOMING"; readonly MOVE: "MOVE"; readonly SCALING: "SCALING"; readonly PANNING: "PANNING"; }; type StateTransition = typeof STATE_TRANSITION[keyof typeof STATE_TRANSITION]; declare class StateManager { current: StateTransition; constructor(); canTransition(to: StateTransition): boolean; transition(to: StateTransition): boolean; reset(): void; isInState(state: StateTransition): boolean; } declare function isWheelEvent(metadata: DOMEventMetadata<DOMEventType>): metadata is DOMEventMetadata<'wheel'>; declare function isMouseEvent(metadata: DOMEventMetadata<DOMEventType>): metadata is DOMEventMetadata<'mousedown' | 'mouseup' | 'mousemove'>; declare function isClickEvent(metadata: DOMEventMetadata<DOMEventType>): metadata is DOMEventMetadata<'click'>; declare function isContextMenuEvent(metadata: DOMEventMetadata<DOMEventType>): metadata is DOMEventMetadata<'contextmenu'>; declare function bindDOMEvent(el: HTMLElement, evt: DOMEventType, dom: DOMEvent): { evt: "click" | "mousedown" | "mousemove" | "mouseup" | "mouseover" | "mouseout" | "wheel" | "contextmenu"; handler: (e: unknown) => void; }; declare class DOMEvent extends Event<DOMEVEntDefinition> { domEvents: Array<ReturnType<typeof bindDOMEvent>>; el: HTMLElement | null; currentModule: LayoutModule | null; component: Component; matrix: Matrix2D; stateManager: StateManager; constructor(component: Component); destory(): void; private dispatch; findRelativeNode(e: DOMEventMetadata): LayoutModule | null; } interface CreateTreemapOptions<P extends Plugin[]> { plugins: P; graphic?: GraphicConfig; } interface TreemapOptions { data: Module[]; } type UnionToIntersection<U> = (U extends Any ? (k: U) => void : never) extends (k: infer I) => void ? I : never; type PluginMixins<P extends readonly Plugin[]> = UnionToIntersection<{ [K in keyof P]: P[K] extends { onLoad?: (ctx: Any, component: Any) => infer R; } ? R extends object ? R : NonNull : NonNull; }[number]>; interface BasicTreemapInstance { init: (el: HTMLElement) => void; dispose: () => void; resize: () => void; setOptions: (options: TreemapOptions) => void; } declare function createTreemap<const P extends readonly Plugin[]>(options?: CreateTreemapOptions<P>): ({ init: (el: HTMLElement) => void; dispose: () => void; resize: () => void; setOptions: (options: TreemapOptions) => void; } & { on: (evt: any, handler: (this: Event<DefaultEventDefinition>, ...args: any[]) => any, c?: any) => void; } & { off: (evt: string, handler?: ((this: unknown, ...args: any[]) => any) | undefined) => void; } & Record<string, never>) & BasicTreemapInstance & ExposedEventMethods & PluginMixins<P>; type TreemapInstance<P extends readonly Plugin[]> = BasicTreemapInstance & ExposedEventMethods & PluginMixins<P>; export { noop as A, createRoundBlock as F, createTitleText as H, raf as I, createCanvasElement as J, applyCanvasTransform as K, mixin as Q, typedForIn as R, stackMatrixTransform as U, stackMatrixTransformWithGraphAndLayer as V, smoothFrame as W, isScrollWheelOrRightButtonOnMouseupAndDown as X, DefaultMap as Y, DOMEvent as b, createTreemap as d, c2m as f, findRelativeNode as g, findRelativeNodeById as h, flatten as i, getNodeDepth as j, definePlugin as m, isClickEvent as q, isContextMenuEvent as r, sortChildrenByKey as s, isMouseEvent as t, isWheelEvent as u, visit as v, hashCode as y, perferNumeric as z }; export type { BasicTreemapInstance as B, ColorMappings as C, DOMEventType as D, ExposedEventCallback as E, GraphicLayout as G, LayoutModule as L, Module as M, NativeModule as N, InheritedCollectionsWithParamter as O, PluginContext as P, Series as S, TreemapOptions as T, DOMEventMetadata as a, CreateTreemapOptions as c, TreemapInstance as e, Plugin as k, PluginHooks as l, ExposedEventDefinition as n, ExposedEventMethods as o, PrimitiveEventMetadata as p, GraphicFont as w, GraphicConfig as x };