Element.d.ts 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. import Transformable, { TransformProp } from './core/Transformable';
  2. import { AnimationEasing } from './animation/easing';
  3. import Animator from './animation/Animator';
  4. import { ZRenderType } from './zrender';
  5. import { Dictionary, ElementEventName, ZRRawEvent, BuiltinTextPosition, MapToType } from './core/types';
  6. import Path from './graphic/Path';
  7. import BoundingRect, { RectLike } from './core/BoundingRect';
  8. import Eventful from './core/Eventful';
  9. import ZRText from './graphic/Text';
  10. import { TextPositionCalculationResult } from './contain/text';
  11. import Polyline from './graphic/shape/Polyline';
  12. import Group from './graphic/Group';
  13. import Point from './core/Point';
  14. export interface ElementAnimateConfig {
  15. duration?: number;
  16. delay?: number;
  17. easing?: AnimationEasing;
  18. during?: (percent: number) => void;
  19. done?: Function;
  20. aborted?: Function;
  21. scope?: string;
  22. force?: boolean;
  23. additive?: boolean;
  24. setToFinal?: boolean;
  25. }
  26. export interface ElementTextConfig {
  27. position?: BuiltinTextPosition | (number | string)[];
  28. rotation?: number;
  29. layoutRect?: RectLike;
  30. offset?: number[];
  31. origin?: (number | string)[] | 'center';
  32. distance?: number;
  33. local?: boolean;
  34. insideFill?: string;
  35. insideStroke?: string;
  36. outsideFill?: string;
  37. outsideStroke?: string;
  38. inside?: boolean;
  39. autoOverflowArea?: boolean;
  40. }
  41. export interface ElementTextGuideLineConfig {
  42. anchor?: Point;
  43. showAbove?: boolean;
  44. candidates?: ('left' | 'top' | 'right' | 'bottom')[];
  45. }
  46. export interface ElementEvent {
  47. type: ElementEventName;
  48. event: ZRRawEvent;
  49. target: Element;
  50. topTarget: Element;
  51. cancelBubble: boolean;
  52. offsetX: number;
  53. offsetY: number;
  54. gestureEvent: string;
  55. pinchX: number;
  56. pinchY: number;
  57. pinchScale: number;
  58. wheelDelta: number;
  59. zrByTouch: boolean;
  60. which: number;
  61. stop: (this: ElementEvent) => void;
  62. }
  63. export declare type ElementEventCallback<Ctx, Impl> = (this: CbThis<Ctx, Impl>, e: ElementEvent) => boolean | void;
  64. declare type CbThis<Ctx, Impl> = unknown extends Ctx ? Impl : Ctx;
  65. interface ElementEventHandlerProps {
  66. onclick: ElementEventCallback<unknown, unknown>;
  67. ondblclick: ElementEventCallback<unknown, unknown>;
  68. onmouseover: ElementEventCallback<unknown, unknown>;
  69. onmouseout: ElementEventCallback<unknown, unknown>;
  70. onmousemove: ElementEventCallback<unknown, unknown>;
  71. onmousewheel: ElementEventCallback<unknown, unknown>;
  72. onmousedown: ElementEventCallback<unknown, unknown>;
  73. onmouseup: ElementEventCallback<unknown, unknown>;
  74. oncontextmenu: ElementEventCallback<unknown, unknown>;
  75. ondrag: ElementEventCallback<unknown, unknown>;
  76. ondragstart: ElementEventCallback<unknown, unknown>;
  77. ondragend: ElementEventCallback<unknown, unknown>;
  78. ondragenter: ElementEventCallback<unknown, unknown>;
  79. ondragleave: ElementEventCallback<unknown, unknown>;
  80. ondragover: ElementEventCallback<unknown, unknown>;
  81. ondrop: ElementEventCallback<unknown, unknown>;
  82. }
  83. export interface ElementProps extends Partial<ElementEventHandlerProps>, Partial<Pick<Transformable, TransformProp>> {
  84. name?: string;
  85. ignore?: boolean;
  86. isGroup?: boolean;
  87. draggable?: boolean | 'horizontal' | 'vertical';
  88. silent?: boolean;
  89. ignoreHostSilent?: boolean;
  90. ignoreClip?: boolean;
  91. globalScaleRatio?: number;
  92. textConfig?: ElementTextConfig;
  93. textContent?: ZRText;
  94. clipPath?: Path;
  95. drift?: Element['drift'];
  96. extra?: Dictionary<unknown>;
  97. anid?: string;
  98. }
  99. export declare const PRESERVED_NORMAL_STATE = "__zr_normal__";
  100. declare const PRIMARY_STATES_KEYS: ["x" | "y" | "originX" | "originY" | "anchorX" | "anchorY" | "rotation" | "scaleX" | "scaleY" | "skewX" | "skewY", "ignore"];
  101. export declare type ElementStatePropNames = (typeof PRIMARY_STATES_KEYS)[number] | 'textConfig';
  102. export declare type ElementState = Pick<ElementProps, ElementStatePropNames> & ElementCommonState;
  103. export declare type ElementCommonState = {
  104. hoverLayer?: boolean | number;
  105. };
  106. export declare type ElementCalculateTextPosition = (out: TextPositionCalculationResult, style: ElementTextConfig, rect: RectLike) => TextPositionCalculationResult;
  107. export declare type InHoverLayerKind = typeof IN_HOVER_LAYER_KIND_NO | typeof IN_HOVER_LAYER_KIND_ONLY_STYLE_CHANGE;
  108. export declare const IN_HOVER_LAYER_KIND_NO = 0;
  109. export declare const IN_HOVER_LAYER_KIND_ONLY_STYLE_CHANGE = 1;
  110. interface Element<Props extends ElementProps = ElementProps> extends Transformable, Eventful<{
  111. [key in ElementEventName]: (e: ElementEvent) => void | boolean;
  112. } & {
  113. [key in string]: (...args: any) => void | boolean;
  114. }>, ElementEventHandlerProps {
  115. }
  116. declare class Element<Props extends ElementProps = ElementProps> {
  117. id: number;
  118. type: string;
  119. name: string;
  120. ignore: boolean;
  121. silent: boolean;
  122. ignoreHostSilent: boolean;
  123. isGroup: boolean;
  124. draggable: boolean | 'horizontal' | 'vertical';
  125. dragging: boolean;
  126. parent: Group;
  127. animators: Animator<any>[];
  128. ignoreClip: boolean;
  129. __hostTarget: Element;
  130. __zr: ZRenderType;
  131. __dirty: number;
  132. __isRendered: boolean;
  133. __inHover: InHoverLayerKind;
  134. __clipPaths?: Path[];
  135. private _clipPath?;
  136. private _textContent?;
  137. private _textGuide?;
  138. textConfig?: ElementTextConfig;
  139. textGuideLineConfig?: ElementTextGuideLineConfig;
  140. anid: string;
  141. extra: Dictionary<unknown>;
  142. currentStates?: string[];
  143. prevStates?: string[];
  144. states: Dictionary<ElementState>;
  145. stateTransition: ElementAnimateConfig;
  146. stateProxy?: (stateName: string, targetStates?: string[]) => ElementState;
  147. protected _normalState: ElementState;
  148. private _innerTextDefaultStyle;
  149. constructor(props?: Props);
  150. protected _init(props?: Props): void;
  151. drift(dx: number, dy: number, e?: ElementEvent): void;
  152. beforeUpdate(): void;
  153. afterUpdate(): void;
  154. update(): void;
  155. updateInnerText(forceUpdate?: boolean): void;
  156. protected canBeInsideText(): boolean;
  157. protected getInsideTextFill(): string | undefined;
  158. protected getInsideTextStroke(textFill: string): string | undefined;
  159. protected getOutsideFill(): string | undefined;
  160. protected getOutsideStroke(textFill: string): string;
  161. traverse<Context>(cb: (this: Context, el: Element<Props>) => void, context?: Context): void;
  162. protected attrKV(key: string, value: unknown): void;
  163. hide(): void;
  164. show(): void;
  165. attr(keyOrObj: Props): this;
  166. attr<T extends keyof Props>(keyOrObj: T, value: Props[T]): this;
  167. saveCurrentToNormalState(toState: ElementState): void;
  168. protected _innerSaveToNormal(toState: ElementState): void;
  169. protected _savePrimaryToNormal(toState: Dictionary<any>, normalState: Dictionary<any>, primaryKeys: readonly string[]): void;
  170. hasState(): boolean;
  171. getState(name: string): ElementState;
  172. ensureState(name: string): ElementState;
  173. clearStates(noAnimation?: boolean): void;
  174. useState(stateName: string, keepCurrentStates?: boolean, noAnimation?: boolean, forceUseHoverLayer?: boolean): ElementState;
  175. useStates(states: string[], noAnimation?: boolean, forceUseHoverLayer?: boolean): void;
  176. isSilent(): boolean;
  177. private _updateAnimationTargets;
  178. removeState(state: string): void;
  179. replaceState(oldState: string, newState: string, forceAdd: boolean): void;
  180. toggleState(state: string, enable: boolean): void;
  181. protected _mergeStates(states: ElementState[]): ElementState;
  182. protected _applyStateObj(stateName: string, state: ElementState, normalState: ElementState, keepCurrentStates: boolean, transition: boolean, animationCfg: ElementAnimateConfig): void;
  183. private _attachComponent;
  184. private _detachComponent;
  185. getClipPath(): Path<import("./graphic/Path").PathProps>;
  186. setClipPath(clipPath: Path): void;
  187. removeClipPath(): void;
  188. getTextContent(): ZRText;
  189. setTextContent(textEl: ZRText): void;
  190. setTextConfig(cfg: ElementTextConfig): void;
  191. removeTextConfig(): void;
  192. removeTextContent(): void;
  193. getTextGuideLine(): Polyline;
  194. setTextGuideLine(guideLine: Polyline): void;
  195. removeTextGuideLine(): void;
  196. markRedraw(): void;
  197. dirty(): void;
  198. addSelfToZr(zr: ZRenderType): void;
  199. removeSelfFromZr(zr: ZRenderType): void;
  200. animate(key?: string, loop?: boolean, allowDiscreteAnimation?: boolean): Animator<any>;
  201. addAnimator(animator: Animator<any>, key: string): void;
  202. updateDuringAnimation(key: string): void;
  203. stopAnimation(scope?: string, forwardToLast?: boolean): this;
  204. animateTo(target: Props, cfg?: ElementAnimateConfig, animationProps?: MapToType<Props, boolean>): void;
  205. animateFrom(target: Props, cfg: ElementAnimateConfig, animationProps?: MapToType<Props, boolean>): void;
  206. protected _transitionState(stateName: string, target: Props, cfg?: ElementAnimateConfig, animationProps?: MapToType<Props, boolean>): void;
  207. getBoundingRect(): BoundingRect;
  208. getPaintRect(): BoundingRect;
  209. calculateTextPosition: ElementCalculateTextPosition;
  210. protected static initDefaultProps: void;
  211. }
  212. export default Element;