UNPKG

13.5 kBTypeScriptView Raw
1import type EventEmitter from 'eventemitter3';
2import type { AnimationTimeline } from './AnimationTimeline';
3import type { BaseStyleProps, Shape } from '../types';
4import type { FederatedEvent } from './FederatedEvent';
5import type { CustomElementRegistry } from './CustomElementRegistry';
6import type { CanvasConfig, InteractivePointerEvent } from '../types';
7import type { DisplayObject } from '../display-objects';
8import type { PointLike } from '../shapes';
9import type { Camera } from '../camera';
10import type { ContextService, EventService, RenderingService } from '../services';
11/**
12 * built-in events for element
13 * @see https://developer.mozilla.org/en-US/docs/Web/API/MutationEvent
14 *
15 * TODO: use MutationObserver instead
16 * @see https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver
17 */
18export declare enum ElementEvent {
19 DESTROY = "destroy",
20 /**
21 * @see https://www.w3.org/TR/DOM-Level-3-Events/#event-type-DOMAttrModified
22 */
23 ATTR_MODIFIED = "DOMAttrModified",
24 /**
25 * it has been inserted
26 * @see https://www.w3.org/TR/DOM-Level-3-Events/#event-type-DOMNodeInserted
27 */
28 INSERTED = "DOMNodeInserted",
29 /**
30 * it has had a child inserted
31 */
32 CHILD_INSERTED = "child-inserted",
33 /**
34 * it is being removed
35 * @see https://www.w3.org/TR/DOM-Level-3-Events/#event-type-DOMNodeRemoved
36 */
37 REMOVED = "removed",
38 /**
39 * it has had a child removed
40 */
41 CHILD_REMOVED = "child-removed",
42 /**
43 * @see https://www.w3.org/TR/DOM-Level-3-Events/#domnodeinsertedintodocument
44 */
45 MOUNTED = "DOMNodeInsertedIntoDocument",
46 /**
47 * @see https://www.w3.org/TR/DOM-Level-3-Events/#domnoderemovedfromdocument
48 */
49 UNMOUNTED = "DOMNodeRemovedFromDocument",
50 BOUNDS_CHANGED = "bounds-changed",
51 GEOMETRY_BOUNDS_CHANGED = "geometry-bounds-changed",
52 /**
53 * trigger when z-index changed
54 */
55 RENDER_ORDER_CHANGED = "render-order-changed"
56}
57export interface IEventTarget {
58 emitter: EventEmitter;
59 on: (type: string, listener: EventListenerOrEventListenerObject | ((...args: any[]) => void), options?: boolean | AddEventListenerOptions) => this;
60 addEventListener: (type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions) => this;
61 off: (type: string, listener: EventListenerOrEventListenerObject | ((...args: any[]) => void), options?: boolean | AddEventListenerOptions) => this;
62 removeEventListener: (type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions) => this;
63 removeAllEventListeners: () => void;
64 dispatchEvent: <T extends FederatedEvent>(e: T) => boolean;
65 emit: (eventName: string, object: object) => void;
66}
67export interface INode extends IEventTarget {
68 shadow: boolean;
69 /**
70 * Returns node's node document's document base URL.
71 */
72 readonly baseURI: string;
73 /**
74 * Returns the children.
75 */
76 readonly childNodes: IChildNode[];
77 /**
78 * Returns the first child.
79 */
80 readonly firstChild: IChildNode | null;
81 /**
82 * Returns true if node is connected and false otherwise.
83 */
84 isConnected: boolean;
85 /**
86 * Returns the last child.
87 */
88 readonly lastChild: IChildNode | null;
89 /**
90 * Returns the next sibling.
91 */
92 readonly nextSibling: IChildNode | null;
93 /**
94 * Returns a string appropriate for the type of node.
95 */
96 readonly nodeName: string;
97 /**
98 * Returns the type of node.
99 */
100 readonly nodeType: number;
101 nodeValue: string | null;
102 /**
103 * Returns the node document. Returns null for documents.
104 */
105 ownerDocument: IDocument | null;
106 /**
107 * Returns the parent element.
108 */
109 readonly parentElement: IElement | null;
110 /**
111 * Returns the parent.
112 */
113 parentNode: (INode & IParentNode) | null;
114 /**
115 * Returns the previous sibling.
116 */
117 readonly previousSibling: IChildNode | null;
118 textContent: string | null;
119 appendChild: <T extends INode>(newChild: T, index?: number) => T;
120 /**
121 * Returns a copy of node. If deep is true, the copy also includes the node's descendants.
122 */
123 cloneNode: (deep?: boolean) => INode;
124 /**
125 * Returns a bitmask indicating the position of other relative to node.
126 */
127 compareDocumentPosition: (other: INode) => number;
128 /**
129 * Returns true if other is an inclusive descendant of node, and false otherwise.
130 */
131 contains: (other: INode | null) => boolean;
132 /**
133 * Returns node's root.
134 */
135 getRootNode: (options?: GetRootNodeOptions) => INode;
136 /**
137 * Returns node's ancestor.
138 */
139 getAncestor: (n: number) => INode | null;
140 /**
141 * Traverse in sub tree.
142 */
143 forEach: (callback: (o: INode) => void | boolean) => void;
144 /**
145 * Returns whether node has children.
146 */
147 hasChildNodes: () => boolean;
148 insertBefore: <T extends INode>(newChild: T, refChild: INode | null) => T;
149 isDefaultNamespace: (namespace: string | null) => boolean;
150 /**
151 * Returns whether node and otherNode have the same properties.
152 */
153 isEqualNode: (otherNode: INode | null) => boolean;
154 isSameNode: (otherNode: INode | null) => boolean;
155 lookupNamespaceURI: (prefix: string | null) => string | null;
156 lookupPrefix: (namespace: string | null) => string | null;
157 /**
158 * Removes empty exclusive Text nodes and concatenates the data of remaining contiguous exclusive Text nodes into the first of their nodes.
159 */
160 normalize: () => void;
161 removeChild: <T extends INode>(oldChild: T, destroy?: boolean) => T;
162 replaceChild: <T extends INode>(newChild: INode, oldChild: T, destroy?: boolean) => T;
163 /**
164 * Destroy itself.
165 */
166 destroy: () => void;
167}
168export interface IParentNode {
169 readonly childElementCount: number;
170 /**
171 * Returns the child elements.
172 */
173 readonly children: IElement[];
174 /**
175 * Returns the first child that is an element, and null otherwise.
176 */
177 readonly firstElementChild: IElement | null;
178 /**
179 * Returns the last child that is an element, and null otherwise.
180 */
181 readonly lastElementChild: IElement | null;
182 /**
183 * Inserts nodes after the last child of node, while replacing strings in nodes with equivalent Text nodes.
184 *
185 * Throws a "HierarchyRequestError" DOMException if the constraints of the node tree are violated.
186 */
187 append: (...nodes: INode[]) => void;
188 /**
189 * Inserts nodes before the first child of node, while replacing strings in nodes with equivalent Text nodes.
190 *
191 * Throws a "HierarchyRequestError" DOMException if the constraints of the node tree are violated.
192 */
193 prepend: (...nodes: INode[]) => void;
194 /**
195 * Returns the first element that is a descendant of node that matches selectors.
196 */
197 querySelector: <E extends IElement = IElement>(selectors: string) => E | null;
198 /**
199 * Returns all element descendants of node that match selectors.
200 */
201 querySelectorAll: <E extends IElement = IElement>(selectors: string) => E[];
202 /**
203 * Similar to querySelector, use custom filter instead of selectors.
204 */
205 find: <E extends IElement = IElement>(filter: (node: E) => boolean) => E | null;
206 /**
207 * Similar to querySelectorAll, use custom filter instead of selectors.
208 */
209 findAll: <E extends IElement = IElement>(filter: (node: E) => boolean) => E[];
210}
211export interface IChildNode extends INode {
212 /**
213 * Inserts nodes just after node, while replacing strings in nodes with equivalent Text nodes.
214 *
215 * Throws a "HierarchyRequestError" DOMException if the constraints of the node tree are violated.
216 */
217 after: (...nodes: INode[]) => void;
218 /**
219 * Inserts nodes just before node, while replacing strings in nodes with equivalent Text nodes.
220 *
221 * Throws a "HierarchyRequestError" DOMException if the constraints of the node tree are violated.
222 */
223 before: (...nodes: INode[]) => void;
224 /**
225 * Removes node.
226 */
227 remove: () => void;
228 /**
229 * Replaces node with nodes, while replacing strings in nodes with equivalent Text nodes.
230 *
231 * Throws a "HierarchyRequestError" DOMException if the constraints of the node tree are violated.
232 */
233 replaceWith: (...nodes: INode[]) => void;
234}
235export interface DisplayObjectConfig<StyleProps> {
236 /**
237 * element's identifier, must be unique in a document.
238 * @see https://developer.mozilla.org/en-US/docs/Web/API/Element/id
239 */
240 id?: string;
241 name?: string;
242 /**
243 * @see https://developer.mozilla.org/zh-CN/docs/Web/API/Element/className
244 */
245 class?: string;
246 className?: string;
247 /**
248 * all styles properties, not read-only
249 * @see https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/style
250 */
251 style?: StyleProps;
252 /**
253 * compatible with G 3.0
254 * @alias style
255 * @deprecated
256 */
257 attrs?: StyleProps;
258 type?: Shape | string;
259 /**
260 * @deprecated use `style.zIndex` instead
261 */
262 zIndex?: number;
263 /**
264 * @deprecated use `style.visibility = 'visible'` instead
265 */
266 visible?: boolean;
267 /**
268 * compatible with G 3.0
269 * @alias interactive
270 * @deprecated
271 */
272 capture?: boolean;
273 /**
274 * enable interaction events for the DisplayObject
275 * @deprecated use `style.pointerEvents = 'auto'` instead
276 */
277 interactive?: boolean;
278}
279export interface IElement<StyleProps = any, ParsedStyleProps = any> extends INode, IChildNode, IParentNode {
280 /**
281 * Returns the value of element's id content attribute. Can be set to change it.
282 */
283 id: string;
284 /**
285 * Returns the qualified name.
286 */
287 tagName: string;
288 name: string;
289 /**
290 * Returns the value of element's class content attribute. Can be set to change it.
291 */
292 className: string;
293 /**
294 * @see https://developer.mozilla.org/en-US/docs/Web/API/Element/attributes
295 */
296 attributes: StyleProps;
297 /**
298 * compatible with `style`
299 * @see https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/style
300 */
301 style: StyleProps & ICSSStyleDeclaration<StyleProps>;
302 parsedStyle: ParsedStyleProps;
303 getElementById: <E extends IElement = IElement>(id: string) => E | null;
304 getElementsByName: <E extends IElement = IElement>(name: string) => E[];
305 getElementsByClassName: <E extends IElement = IElement>(className: string) => E[];
306 getElementsByTagName: <E extends IElement = IElement>(tagName: string) => E[];
307 scrollLeft: number;
308 scrollTop: number;
309 clientLeft: number;
310 clientTop: number;
311 /**
312 * @see https://developer.mozilla.org/en-US/docs/Web/API/Element/setAttribute
313 */
314 setAttribute: <Key extends keyof StyleProps>(attributeName: Key, value: StyleProps[Key], force?: boolean) => void;
315 /**
316 * @see https://developer.mozilla.org/en-US/docs/Web/API/Element/getAttribute
317 */
318 getAttribute: (attributeName: keyof StyleProps) => StyleProps[keyof StyleProps] | undefined;
319 /**
320 * @see https://developer.mozilla.org/en-US/docs/Web/API/Element/removeAttribute
321 */
322 removeAttribute: (attributeName: keyof StyleProps) => void;
323 hasAttribute: (qualifiedName: string) => boolean;
324}
325export interface IDocument extends INode, IParentNode {
326 /**
327 * Returns the Window object of the active document.
328 */
329 readonly defaultView: ICanvas | null;
330 /**
331 * Gets a reference to the root node of the document.
332 */
333 readonly documentElement: IElement;
334 readonly ownerDocument: null;
335 readonly timeline: AnimationTimeline;
336 /**
337 * Creates an instance of the element for the specified tag.
338 */
339 createElement: <T extends DisplayObject<StyleProps>, StyleProps extends BaseStyleProps>(tagName: string, options: DisplayObjectConfig<StyleProps>) => T;
340}
341/**
342 * @see https://developer.mozilla.org/zh-CN/docs/Web/API/CSSStyleDeclaration
343 */
344export interface ICSSStyleDeclaration<StyleProps> {
345 setProperty: <Key extends keyof StyleProps>(propertyName: Key, value: StyleProps[Key], priority?: string) => void;
346 getPropertyValue: (propertyName: keyof StyleProps) => StyleProps[keyof StyleProps] | undefined;
347 removeProperty: (propertyName: keyof StyleProps) => void;
348 item: (index: number) => string;
349}
350export interface ICanvas extends IEventTarget {
351 document: IDocument;
352 customElements: CustomElementRegistry;
353 devicePixelRatio: number;
354 requestAnimationFrame: (callback: FrameRequestCallback) => number;
355 cancelAnimationFrame: (handle: number) => void;
356 supportTouchEvent: boolean;
357 supportPointerEvent: boolean;
358 isTouchEvent: (event: InteractivePointerEvent) => event is TouchEvent;
359 isMouseEvent: (event: InteractivePointerEvent) => event is MouseEvent;
360 render: () => void;
361 destroy: (destroyScenegraph?: boolean) => void;
362 resize: (width: number, height: number) => void;
363 getConfig: () => Partial<CanvasConfig>;
364 getCamera: () => Camera;
365 getContextService: () => ContextService<unknown>;
366 getRenderingService: () => RenderingService;
367 getEventService: () => EventService;
368 client2Viewport: (client: PointLike) => PointLike;
369 viewport2Client: (viewport: PointLike) => PointLike;
370 canvas2Viewport: (canvas: PointLike) => PointLike;
371 viewport2Canvas: (viewport: PointLike) => PointLike;
372}