-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
ABaseLayer.ts
120 lines (102 loc) · 3.46 KB
/
ABaseLayer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import type {
ICanvasLayer,
IHTMLLayer,
IMoveAbleLayer,
ISVGLayer,
ILayer,
IHTMLStaticLayer,
ISVGStaticLayer,
ICanvasStaticLayer,
IRenderHint,
} from './interfaces';
import type cy from 'cytoscape';
import type { ICanvasLayerOptions, ISVGLayerOptions, IHTMLLayerOptions } from './public';
export interface ILayerAdapter {
cy: cy.Core;
move(layer: IMoveAbleLayer, offset: number): void;
insert(
where: 'before' | 'after',
layer: IMoveAbleLayer,
type: 'svg' | 'html' | 'canvas' | 'svg-static' | 'html-static' | 'canvas-static',
options?: IHTMLLayerOptions | ICanvasLayerOptions | ISVGLayerOptions
): ILayer;
inVisibleBounds(p: { x: number; y: number } | cy.BoundingBox12): boolean;
}
export abstract class ABaseLayer implements IMoveAbleLayer {
private updateOnRenderEnabled = false;
private updateOnRenderOnceEnabled = false;
constructor(private readonly adapter: ILayerAdapter) {}
inVisibleBounds(p: { x: number; y: number } | cy.BoundingBox12) {
return this.adapter.inVisibleBounds(p);
}
supportsRender() {
return false;
}
renderInto(_ctx: CanvasRenderingContext2D, _hint: IRenderHint): void {
// dummy
}
get updateOnRender() {
return this.updateOnRenderEnabled;
}
set updateOnRender(value: boolean) {
if (this.updateOnRenderEnabled === value) {
return;
}
this.updateOnRenderEnabled = value;
if (value) {
this.cy.on('render', this.update);
} else {
this.cy.off('render', this.update);
}
}
updateOnRenderOnce() {
if (this.updateOnRenderOnceEnabled) {
return;
}
this.updateOnRenderOnceEnabled = true;
this.cy.one('render', () => {
this.updateOnRenderOnceEnabled = false;
this.update();
});
}
abstract readonly update: () => void;
get cy() {
return this.adapter.cy;
}
moveUp() {
this.adapter.move(this, -1);
}
moveDown() {
this.adapter.move(this, 1);
}
moveBack() {
this.adapter.move(this, Number.NEGATIVE_INFINITY);
}
moveFront() {
this.adapter.move(this, Number.POSITIVE_INFINITY);
}
insertBefore(type: 'svg', options?: ISVGLayerOptions): ISVGLayer;
insertBefore(type: 'svg-static', options?: ISVGLayerOptions): ISVGStaticLayer;
insertBefore(type: 'canvas', options?: ICanvasLayerOptions): ICanvasLayer;
insertBefore(type: 'canvas-static', options?: ICanvasLayerOptions): ICanvasStaticLayer;
insertBefore(type: 'html', options?: IHTMLLayerOptions): IHTMLLayer;
insertBefore(type: 'html-static', options?: IHTMLLayerOptions): IHTMLStaticLayer;
insertBefore(
type: 'svg' | 'html' | 'canvas' | 'svg-static' | 'html-static' | 'canvas-static',
options?: IHTMLLayerOptions | ISVGLayerOptions | ICanvasLayerOptions
) {
return this.adapter.insert('before', this, type, options);
}
insertAfter(type: 'svg', options?: ISVGLayerOptions): ISVGLayer;
insertAfter(type: 'svg-static', options?: ISVGLayerOptions): ISVGStaticLayer;
insertAfter(type: 'canvas', options?: ICanvasLayerOptions): ICanvasLayer;
insertAfter(type: 'canvas-static', options?: ICanvasLayerOptions): ICanvasStaticLayer;
insertAfter(type: 'html', options?: IHTMLLayerOptions): IHTMLLayer;
insertAfter(type: 'html-static', options?: IHTMLLayerOptions): IHTMLStaticLayer;
insertAfter(
type: 'svg' | 'html' | 'canvas' | 'svg-static' | 'html-static' | 'canvas-static',
options?: IHTMLLayerOptions | ISVGLayerOptions | ICanvasLayerOptions
) {
return this.adapter.insert('after', this, type, options);
}
}