|
1 |
| -import RenderLayer from "./RenderLayer"; |
2 |
| -import { make } from "./FrameUtils"; |
3 |
| -import * as EventTypes from "./EventTypes"; |
4 |
| -import { emptyObject } from "./utils"; |
| 1 | +import RenderLayer from './RenderLayer' |
| 2 | +import { make } from './FrameUtils' |
| 3 | +import * as EventTypes from './EventTypes' |
| 4 | +import { emptyObject } from './utils' |
5 | 5 |
|
6 |
| -let LAYER_GUID = 1; |
| 6 | +let LAYER_GUID = 1 |
7 | 7 |
|
8 | 8 | export default class CanvasComponent {
|
9 | 9 | constructor(type) {
|
10 |
| - this.type = type; |
11 |
| - this.subscriptions = new Map(); |
12 |
| - this.listeners = new Map(); |
13 |
| - this.node = new RenderLayer(this); |
14 |
| - this._layerId = LAYER_GUID++; |
| 10 | + this.type = type |
| 11 | + this.subscriptions = new Map() |
| 12 | + this.listeners = new Map() |
| 13 | + this.node = new RenderLayer(this) |
| 14 | + this._layerId = LAYER_GUID++ |
15 | 15 | }
|
16 | 16 |
|
17 | 17 | putEventListener = (type, listener) => {
|
18 |
| - const subscriptions = this.subscriptions; |
19 |
| - const listeners = this.listeners; |
| 18 | + const subscriptions = this.subscriptions |
| 19 | + const listeners = this.listeners |
20 | 20 |
|
21 |
| - let isListenerDifferent = false; |
| 21 | + let isListenerDifferent = false |
22 | 22 | if (listeners.get(type) !== listener) {
|
23 |
| - listeners.set(type, listener); |
24 |
| - isListenerDifferent = true; |
| 23 | + listeners.set(type, listener) |
| 24 | + isListenerDifferent = true |
25 | 25 | }
|
26 | 26 |
|
27 | 27 | if (listener) {
|
28 | 28 | // Add subscription if this is the first listener of the given type
|
29 | 29 | // or the new listener is different from the current listener.
|
30 | 30 | if (!subscriptions.has(type) || isListenerDifferent) {
|
31 |
| - subscriptions.set(type, this.node.subscribe(type, listener, this)); |
| 31 | + subscriptions.set(type, this.node.subscribe(type, listener, this)) |
32 | 32 | }
|
33 | 33 | } else {
|
34 |
| - const subscription = subscriptions.get(type); |
| 34 | + const subscription = subscriptions.get(type) |
35 | 35 | if (subscription) {
|
36 |
| - subscription(); |
37 |
| - subscriptions.delete(type); |
| 36 | + subscription() |
| 37 | + subscriptions.delete(type) |
38 | 38 | }
|
39 | 39 | }
|
40 |
| - }; |
| 40 | + } |
41 | 41 |
|
42 | 42 | destroyEventListeners = () => {
|
43 |
| - this.listeners.clear(); |
44 |
| - this.subscriptions.clear(); |
45 |
| - this.node.destroyEventListeners(); |
46 |
| - }; |
| 43 | + this.listeners.clear() |
| 44 | + this.subscriptions.clear() |
| 45 | + this.node.destroyEventListeners() |
| 46 | + } |
47 | 47 |
|
48 | 48 | setStyleFromProps = (layer, props) => {
|
49 |
| - let style = emptyObject; |
| 49 | + let style = emptyObject |
50 | 50 |
|
51 | 51 | if (props.style) {
|
52 |
| - style = props.style; |
53 |
| - layer._originalStyle = style; |
| 52 | + style = props.style |
| 53 | + layer._originalStyle = style |
54 | 54 | } else {
|
55 |
| - layer._originalStyle = null; |
| 55 | + layer._originalStyle = null |
56 | 56 | }
|
57 | 57 |
|
58 | 58 | if (!layer.frame) {
|
59 |
| - layer.frame = make(0, 0, 0, 0); |
| 59 | + layer.frame = make(0, 0, 0, 0) |
60 | 60 | }
|
61 | 61 |
|
62 |
| - const frame = layer.frame; |
63 |
| - const l = style.left || 0; |
64 |
| - const t = style.top || 0; |
65 |
| - const w = style.width || 0; |
66 |
| - const h = style.height || 0; |
| 62 | + const frame = layer.frame |
| 63 | + const l = style.left || 0 |
| 64 | + const t = style.top || 0 |
| 65 | + const w = style.width || 0 |
| 66 | + const h = style.height || 0 |
67 | 67 |
|
68 |
| - if (frame.x !== l) frame.x = l; |
69 |
| - if (frame.y !== t) frame.y = t; |
70 |
| - if (frame.width !== w) frame.width = w; |
71 |
| - if (frame.height !== h) frame.height = h; |
| 68 | + if (frame.x !== l) frame.x = l |
| 69 | + if (frame.y !== t) frame.y = t |
| 70 | + if (frame.width !== w) frame.width = w |
| 71 | + if (frame.height !== h) frame.height = h |
72 | 72 |
|
73 | 73 | // Common layer properties
|
74 |
| - if (layer.alpha !== style.alpha) layer.alpha = style.alpha; |
| 74 | + if (layer.alpha !== style.alpha) layer.alpha = style.alpha |
75 | 75 |
|
76 | 76 | if (layer.backgroundColor !== style.backgroundColor)
|
77 |
| - layer.backgroundColor = style.backgroundColor; |
| 77 | + layer.backgroundColor = style.backgroundColor |
78 | 78 |
|
79 | 79 | if (layer.borderColor !== style.borderColor)
|
80 |
| - layer.borderColor = style.borderColor; |
| 80 | + layer.borderColor = style.borderColor |
81 | 81 |
|
82 | 82 | if (layer.borderWidth !== style.borderWidth)
|
83 |
| - layer.borderWidth = style.borderWidth; |
| 83 | + layer.borderWidth = style.borderWidth |
84 | 84 |
|
85 | 85 | if (layer.borderRadius !== style.borderRadius)
|
86 |
| - layer.borderRadius = style.borderRadius; |
| 86 | + layer.borderRadius = style.borderRadius |
87 | 87 |
|
88 |
| - if (layer.clipRect !== style.clipRect) layer.clipRect = style.clipRect; |
| 88 | + if (layer.clipRect !== style.clipRect) layer.clipRect = style.clipRect |
89 | 89 |
|
90 |
| - if (layer.scale !== style.scale) layer.scale = style.scale; |
| 90 | + if (layer.scale !== style.scale) layer.scale = style.scale |
91 | 91 |
|
92 | 92 | if (
|
93 | 93 | layer.translateX !== style.translateX ||
|
94 | 94 | layer.translateY !== style.translateY
|
95 | 95 | ) {
|
96 |
| - layer.translateX = style.translateX; |
97 |
| - layer.translateY = style.translateY; |
| 96 | + layer.translateX = style.translateX |
| 97 | + layer.translateY = style.translateY |
98 | 98 | }
|
99 | 99 |
|
100 |
| - if (layer.zIndex !== style.zIndex) layer.zIndex = style.zIndex; |
| 100 | + if (layer.zIndex !== style.zIndex) layer.zIndex = style.zIndex |
101 | 101 |
|
102 | 102 | // Shadow
|
103 | 103 | if (layer.shadowColor !== style.shadowColor)
|
104 |
| - layer.shadowColor = style.shadowColor; |
| 104 | + layer.shadowColor = style.shadowColor |
105 | 105 |
|
106 | 106 | if (layer.shadowBlur !== style.shadowBlur)
|
107 |
| - layer.shadowBlur = style.shadowBlur; |
| 107 | + layer.shadowBlur = style.shadowBlur |
108 | 108 |
|
109 | 109 | if (layer.shadowOffsetX !== style.shadowOffsetX)
|
110 |
| - layer.shadowOffsetX = style.shadowOffsetX; |
| 110 | + layer.shadowOffsetX = style.shadowOffsetX |
111 | 111 |
|
112 | 112 | if (layer.shadowOffsetY !== style.shadowOffsetY)
|
113 |
| - layer.shadowOffsetY = style.shadowOffsetY; |
114 |
| - }; |
| 113 | + layer.shadowOffsetY = style.shadowOffsetY |
| 114 | + } |
115 | 115 |
|
116 | 116 | applyCommonLayerProps = (prevProps, props) => {
|
117 |
| - const layer = this.node; |
| 117 | + const layer = this.node |
118 | 118 |
|
119 | 119 | // Generate backing store ID as needed.
|
120 | 120 | if (props.useBackingStore && layer.backingStoreId !== this._layerId) {
|
121 |
| - layer.backingStoreId = this._layerId; |
| 121 | + layer.backingStoreId = this._layerId |
122 | 122 | } else if (!props.useBackingStore && layer.backingStoreId) {
|
123 |
| - layer.backingStoreId = null; |
| 123 | + layer.backingStoreId = null |
124 | 124 | }
|
125 | 125 |
|
126 | 126 | // Register events
|
127 | 127 | for (const type in EventTypes) {
|
128 | 128 | if (prevProps[type] !== props[type]) {
|
| 129 | + // eslint-disable-next-line no-console |
129 | 130 | console.log(props[type], type)
|
130 |
| - this.putEventListener(EventTypes[type], props[type]); |
| 131 | + this.putEventListener(EventTypes[type], props[type]) |
131 | 132 | }
|
132 | 133 | }
|
133 | 134 |
|
134 |
| - this.setStyleFromProps(layer, props); |
135 |
| - }; |
| 135 | + this.setStyleFromProps(layer, props) |
| 136 | + } |
136 | 137 |
|
137 |
| - getLayer = () => this.node; |
| 138 | + getLayer = () => this.node |
138 | 139 |
|
139 | 140 | /**
|
140 | 141 | * Resets all the state on this CanvasComponent so it can be added to a pool for re-use.
|
141 | 142 | *
|
142 | 143 | * @return {RenderLayer}
|
143 | 144 | */
|
144 | 145 | reset = () => {
|
145 |
| - this.destroyEventListeners(); |
146 |
| - this._originalStyle = null; |
147 |
| - this.node.reset(this); |
148 |
| - }; |
| 146 | + this.destroyEventListeners() |
| 147 | + this._originalStyle = null |
| 148 | + this.node.reset(this) |
| 149 | + } |
149 | 150 | }
|
0 commit comments