chnobli 0.1.4 → 0.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (238) hide show
  1. package/.prettierignore +4 -0
  2. package/.prettierrc +6 -0
  3. package/.vscode/settings.json +4 -0
  4. package/auto-docs/easing.js.html +342 -0
  5. package/auto-docs/fonts/OpenSans-Bold-webfont.eot +0 -0
  6. package/auto-docs/fonts/OpenSans-Bold-webfont.svg +1830 -0
  7. package/auto-docs/fonts/OpenSans-Bold-webfont.woff +0 -0
  8. package/auto-docs/fonts/OpenSans-BoldItalic-webfont.eot +0 -0
  9. package/auto-docs/fonts/OpenSans-BoldItalic-webfont.svg +1830 -0
  10. package/auto-docs/fonts/OpenSans-BoldItalic-webfont.woff +0 -0
  11. package/auto-docs/fonts/OpenSans-Italic-webfont.eot +0 -0
  12. package/auto-docs/fonts/OpenSans-Italic-webfont.svg +1830 -0
  13. package/auto-docs/fonts/OpenSans-Italic-webfont.woff +0 -0
  14. package/auto-docs/fonts/OpenSans-Light-webfont.eot +0 -0
  15. package/auto-docs/fonts/OpenSans-Light-webfont.svg +1831 -0
  16. package/auto-docs/fonts/OpenSans-Light-webfont.woff +0 -0
  17. package/auto-docs/fonts/OpenSans-LightItalic-webfont.eot +0 -0
  18. package/auto-docs/fonts/OpenSans-LightItalic-webfont.svg +1835 -0
  19. package/auto-docs/fonts/OpenSans-LightItalic-webfont.woff +0 -0
  20. package/auto-docs/fonts/OpenSans-Regular-webfont.eot +0 -0
  21. package/auto-docs/fonts/OpenSans-Regular-webfont.svg +1831 -0
  22. package/auto-docs/fonts/OpenSans-Regular-webfont.woff +0 -0
  23. package/auto-docs/global.html +4810 -0
  24. package/auto-docs/index.html +65 -0
  25. package/auto-docs/scripts/linenumber.js +25 -0
  26. package/auto-docs/scripts/prettify/Apache-License-2.0.txt +202 -0
  27. package/auto-docs/scripts/prettify/lang-css.js +2 -0
  28. package/auto-docs/scripts/prettify/prettify.js +28 -0
  29. package/auto-docs/styles/jsdoc-default.css +358 -0
  30. package/auto-docs/styles/prettify-jsdoc.css +111 -0
  31. package/auto-docs/styles/prettify-tomorrow.css +132 -0
  32. package/dist/animation/Animation.d.ts +36 -0
  33. package/dist/animation/Animation.d.ts.map +1 -0
  34. package/dist/animation/Animation.js +230 -0
  35. package/dist/animation/Animation.js.map +1 -0
  36. package/dist/animation/animation.d.ts +36 -0
  37. package/dist/animation/animation.d.ts.map +1 -0
  38. package/dist/animation/animation.js +230 -0
  39. package/dist/animation/animation.js.map +1 -0
  40. package/dist/animation/public.d.ts +55 -0
  41. package/dist/animation/public.d.ts.map +1 -0
  42. package/dist/animation/public.js +104 -0
  43. package/dist/animation/public.js.map +1 -0
  44. package/dist/chnobli.d.ts +15 -0
  45. package/dist/chnobli.d.ts.map +1 -0
  46. package/dist/chnobli.js +63 -0
  47. package/dist/chnobli.js.map +1 -0
  48. package/dist/property/BasicProperty.d.ts +38 -0
  49. package/dist/property/BasicProperty.d.ts.map +1 -0
  50. package/dist/property/BasicProperty.js +72 -0
  51. package/dist/property/BasicProperty.js.map +1 -0
  52. package/dist/property/ClassProperty.d.ts +32 -0
  53. package/dist/property/ClassProperty.d.ts.map +1 -0
  54. package/dist/property/ClassProperty.js +108 -0
  55. package/dist/property/ClassProperty.js.map +1 -0
  56. package/dist/property/Property.d.ts +13 -0
  57. package/dist/property/Property.d.ts.map +1 -0
  58. package/dist/property/Property.js +20 -0
  59. package/dist/property/Property.js.map +1 -0
  60. package/dist/property/StyleProperty.d.ts +25 -0
  61. package/dist/property/StyleProperty.d.ts.map +1 -0
  62. package/dist/property/StyleProperty.js +221 -0
  63. package/dist/property/StyleProperty.js.map +1 -0
  64. package/dist/property/TransformProperty.d.ts +15 -0
  65. package/dist/property/TransformProperty.d.ts.map +1 -0
  66. package/dist/property/TransformProperty.js +43 -0
  67. package/dist/property/TransformProperty.js.map +1 -0
  68. package/dist/property/TransformXYProperty.d.ts +28 -0
  69. package/dist/property/TransformXYProperty.d.ts.map +1 -0
  70. package/dist/property/TransformXYProperty.js +65 -0
  71. package/dist/property/TransformXYProperty.js.map +1 -0
  72. package/dist/responsive/ResponsiveEvent.d.ts +17 -0
  73. package/dist/responsive/ResponsiveEvent.d.ts.map +1 -0
  74. package/dist/responsive/ResponsiveEvent.js +45 -0
  75. package/dist/responsive/ResponsiveEvent.js.map +1 -0
  76. package/dist/responsive/TestEvent.d.ts +11 -0
  77. package/dist/responsive/TestEvent.d.ts.map +1 -0
  78. package/dist/responsive/TestEvent.js +26 -0
  79. package/dist/responsive/TestEvent.js.map +1 -0
  80. package/dist/responsive/event.d.ts +10 -0
  81. package/dist/responsive/event.d.ts.map +1 -0
  82. package/dist/responsive/event.js +39 -0
  83. package/dist/responsive/event.js.map +1 -0
  84. package/dist/scroll/Scroll.d.ts +27 -0
  85. package/dist/scroll/Scroll.d.ts.map +1 -0
  86. package/dist/scroll/Scroll.js +132 -0
  87. package/dist/scroll/Scroll.js.map +1 -0
  88. package/dist/scroll/ScrollEvent.d.ts +17 -0
  89. package/dist/scroll/ScrollEvent.d.ts.map +1 -0
  90. package/dist/scroll/ScrollEvent.js +39 -0
  91. package/dist/scroll/ScrollEvent.js.map +1 -0
  92. package/dist/scroll/public.d.ts +7 -0
  93. package/dist/scroll/public.d.ts.map +1 -0
  94. package/dist/scroll/public.js +17 -0
  95. package/dist/scroll/public.js.map +1 -0
  96. package/dist/stagger/stagger.d.ts +13 -0
  97. package/dist/stagger/stagger.d.ts.map +1 -0
  98. package/dist/stagger/stagger.js +35 -0
  99. package/dist/stagger/stagger.js.map +1 -0
  100. package/dist/target/BaseTarget.old.d.ts +33 -0
  101. package/dist/target/BaseTarget.old.d.ts.map +1 -0
  102. package/dist/target/BaseTarget.old.js +73 -0
  103. package/dist/target/BaseTarget.old.js.map +1 -0
  104. package/dist/target/DomTarget.d.ts +41 -0
  105. package/dist/target/DomTarget.d.ts.map +1 -0
  106. package/dist/target/DomTarget.js +230 -0
  107. package/dist/target/DomTarget.js.map +1 -0
  108. package/dist/target/Target.d.ts +22 -0
  109. package/dist/target/Target.d.ts.map +1 -0
  110. package/dist/target/Target.js +21 -0
  111. package/dist/target/Target.js.map +1 -0
  112. package/dist/target/TestDomNode.d.ts +14 -0
  113. package/dist/target/TestDomNode.d.ts.map +1 -0
  114. package/dist/target/TestDomNode.js +55 -0
  115. package/dist/target/TestDomNode.js.map +1 -0
  116. package/dist/timeline/Nested.d.ts +30 -0
  117. package/dist/timeline/Nested.d.ts.map +1 -0
  118. package/dist/timeline/Nested.js +51 -0
  119. package/dist/timeline/Nested.js.map +1 -0
  120. package/dist/timeline/TimeLine.d.ts +32 -0
  121. package/dist/timeline/TimeLine.d.ts.map +1 -0
  122. package/dist/timeline/TimeLine.js +288 -0
  123. package/dist/timeline/TimeLine.js.map +1 -0
  124. package/dist/timeline/public.d.ts +120 -0
  125. package/dist/timeline/public.d.ts.map +1 -0
  126. package/dist/timeline/public.js +301 -0
  127. package/dist/timeline/public.js.map +1 -0
  128. package/dist/timing/AnimationFrameTicker.d.ts +32 -0
  129. package/dist/timing/AnimationFrameTicker.d.ts.map +1 -0
  130. package/dist/timing/AnimationFrameTicker.js +134 -0
  131. package/dist/timing/AnimationFrameTicker.js.map +1 -0
  132. package/dist/timing/Ease.d.ts +6 -0
  133. package/dist/timing/Ease.d.ts.map +1 -0
  134. package/dist/timing/Ease.js +15 -0
  135. package/dist/timing/Ease.js.map +1 -0
  136. package/dist/timing/TestTicker.d.ts +19 -0
  137. package/dist/timing/TestTicker.d.ts.map +1 -0
  138. package/dist/timing/TestTicker.js +81 -0
  139. package/dist/timing/TestTicker.js.map +1 -0
  140. package/dist/timing/Ticker.d.ts +14 -0
  141. package/dist/timing/Ticker.d.ts.map +1 -0
  142. package/dist/timing/Ticker.js +2 -0
  143. package/dist/timing/Ticker.js.map +1 -0
  144. package/dist/timing/Timing.d.ts +62 -0
  145. package/dist/timing/Timing.d.ts.map +1 -0
  146. package/dist/timing/Timing.js +257 -0
  147. package/dist/timing/Timing.js.map +1 -0
  148. package/dist/timing/easing.d.ts +158 -0
  149. package/dist/timing/easing.d.ts.map +1 -0
  150. package/dist/timing/easing.js +273 -0
  151. package/dist/timing/easing.js.map +1 -0
  152. package/dist/utils/Events.d.ts +9 -0
  153. package/dist/utils/Events.d.ts.map +1 -0
  154. package/dist/utils/Events.js +35 -0
  155. package/dist/utils/Events.js.map +1 -0
  156. package/dist/utils/internal.d.ts +2 -0
  157. package/dist/utils/internal.d.ts.map +1 -0
  158. package/dist/utils/internal.js +6 -0
  159. package/dist/utils/internal.js.map +1 -0
  160. package/dist/utils/motionpath.d.ts +16 -0
  161. package/dist/utils/motionpath.d.ts.map +1 -0
  162. package/dist/utils/motionpath.js +41 -0
  163. package/dist/utils/motionpath.js.map +1 -0
  164. package/dist/utils/utils.d.ts +43 -0
  165. package/dist/utils/utils.d.ts.map +1 -0
  166. package/dist/utils/utils.js +93 -0
  167. package/dist/utils/utils.js.map +1 -0
  168. package/dist/values/ColorValue.d.ts +12 -0
  169. package/dist/values/ColorValue.d.ts.map +1 -0
  170. package/dist/values/ColorValue.js +133 -0
  171. package/dist/values/ColorValue.js.map +1 -0
  172. package/dist/values/StyleValue.d.ts +40 -0
  173. package/dist/values/StyleValue.d.ts.map +1 -0
  174. package/dist/values/StyleValue.js +149 -0
  175. package/dist/values/StyleValue.js.map +1 -0
  176. package/dist/values/Value.d.ts +13 -0
  177. package/dist/values/Value.d.ts.map +1 -0
  178. package/dist/values/Value.js +38 -0
  179. package/dist/values/Value.js.map +1 -0
  180. package/dist/values/defaultcolors.d.ts +2 -0
  181. package/dist/values/defaultcolors.d.ts.map +1 -0
  182. package/dist/values/defaultcolors.js +149 -0
  183. package/dist/values/defaultcolors.js.map +1 -0
  184. package/package.json +11 -7
  185. package/src/animation/{animation.js → Animation.ts} +56 -54
  186. package/src/animation/{public.js → public.ts} +23 -18
  187. package/src/{chnobli.js → chnobli.ts} +11 -12
  188. package/src/property/BasicProperty.ts +91 -0
  189. package/src/property/ClassProperty.ts +132 -0
  190. package/src/property/Property.ts +33 -0
  191. package/src/property/StyleProperty.ts +273 -0
  192. package/src/property/TransformProperty.ts +56 -0
  193. package/src/property/TransformXYProperty.ts +88 -0
  194. package/src/responsive/ResponsiveEvent.ts +60 -0
  195. package/src/responsive/{testevent.js → TestEvent.ts} +12 -10
  196. package/src/scroll/{scroll.js → Scroll.ts} +53 -35
  197. package/src/scroll/ScrollEvent.ts +53 -0
  198. package/src/scroll/public.ts +20 -0
  199. package/src/stagger/stagger.ts +45 -0
  200. package/src/target/{basetarget.js → BaseTarget.old.ts} +25 -26
  201. package/src/target/DomTarget.ts +281 -0
  202. package/src/target/Target.ts +48 -0
  203. package/src/target/{testdomnode.js → TestDomNode.ts} +18 -15
  204. package/src/tests/{properties.test.js → properties.test.ts} +54 -49
  205. package/src/tests/timeline.test.ts +290 -0
  206. package/src/tests/{timing.test.js → timing.test.ts} +25 -22
  207. package/src/tests/{usecases.test.js → usecases.test.ts} +13 -10
  208. package/src/tests/{values.test.js → values.test.ts} +21 -21
  209. package/src/timeline/Nested.ts +67 -0
  210. package/src/timeline/{timeline.js → TimeLine.ts} +126 -56
  211. package/src/timeline/{public.js → public.ts} +112 -73
  212. package/src/timing/{ticker.js → AnimationFrameTicker.ts} +36 -21
  213. package/src/timing/{ease.js → Ease.ts} +6 -5
  214. package/src/timing/{testticker.js → TestTicker.ts} +27 -24
  215. package/src/timing/Ticker.ts +11 -0
  216. package/src/timing/{timing.js → Timing.ts} +72 -63
  217. package/src/timing/{easing.js → easing.ts} +52 -43
  218. package/src/utils/{events.js → Events.ts} +12 -11
  219. package/src/utils/internal.ts +10 -0
  220. package/src/utils/{motionpath.js → motionpath.ts} +36 -13
  221. package/src/utils/utils.ts +110 -0
  222. package/src/values/{colorvalue.js → ColorValue.ts} +46 -47
  223. package/src/values/StyleValue.ts +186 -0
  224. package/src/values/{value.js → Value.ts} +14 -12
  225. package/src/values/{defaultcolors.js → defaultcolors.ts} +2 -2
  226. package/tsconfig.build.json +19 -0
  227. package/tsconfig.json +17 -108
  228. package/src/animation/property.js +0 -556
  229. package/src/responsive/event.js +0 -46
  230. package/src/scroll/event.js +0 -41
  231. package/src/scroll/public.js +0 -17
  232. package/src/stagger/stagger.js +0 -42
  233. package/src/target/domtarget.js +0 -266
  234. package/src/target/target.js +0 -22
  235. package/src/tests/timeline.test.js +0 -234
  236. package/src/utils/internal.js +0 -7
  237. package/src/utils/utils.js +0 -75
  238. package/src/values/stylevalue.js +0 -148
@@ -0,0 +1,53 @@
1
+ export type Callback = (
2
+ a: { y: number; height: number },
3
+ opts: { remove: () => void },
4
+ ) => void;
5
+
6
+ export default class ScrollEvent {
7
+ _listeners: Set<Callback>;
8
+
9
+ constructor() {
10
+ this._listeners = new Set();
11
+
12
+ window.addEventListener('scroll', e => this._onScroll(e));
13
+ }
14
+
15
+ static global() {
16
+ if (typeof window === 'undefined') return null;
17
+
18
+ // @ts-ignore
19
+ if (typeof window.chnobliScroll === 'undefined')
20
+ // @ts-ignore
21
+ window.chnobliScroll = new ScrollEvent();
22
+ // @ts-ignore
23
+ return window.chnobliScroll;
24
+ }
25
+
26
+ add(fn: Callback) {
27
+ this._listeners.add(fn);
28
+
29
+ return {
30
+ remove: () => {
31
+ this.remove(fn);
32
+ },
33
+ };
34
+ }
35
+
36
+ remove(fn: Callback) {
37
+ this._listeners.delete(fn);
38
+ }
39
+
40
+ private _onScroll(_e: Event) {
41
+ for (const fn of this._listeners) {
42
+ fn(
43
+ {
44
+ y: window.scrollY,
45
+ height: window.innerHeight,
46
+ },
47
+ {
48
+ remove: () => this.remove(fn),
49
+ },
50
+ );
51
+ }
52
+ }
53
+ }
@@ -0,0 +1,20 @@
1
+ import PublicTimeline from '../timeline/public';
2
+ import Scroll from './Scroll';
3
+
4
+ export default class PublicScroll {
5
+ private _inner: Scroll;
6
+
7
+ /*
8
+ {
9
+ start: el | px
10
+ startView: 'top'
11
+ }
12
+ */
13
+ constructor(props: Record<string, any> = {}) {
14
+ this._inner = new Scroll(props);
15
+ }
16
+
17
+ add(timeline: PublicTimeline) {
18
+ this._inner.addTimeline(timeline);
19
+ }
20
+ }
@@ -0,0 +1,45 @@
1
+ /**
2
+ * Staggering allows to have a different value for every target
3
+ * allowing to easely offset each target by a few pixels
4
+ */
5
+
6
+ function isStagger(value: any) {
7
+ return (
8
+ typeof value === 'object' &&
9
+ value !== null &&
10
+ typeof value.__isStagger__ === 'function'
11
+ );
12
+ }
13
+
14
+ export function callStagger(value: any, i: number, l: any = null) {
15
+ if (isStagger(value)) return value.call(i, l);
16
+
17
+ return value;
18
+ }
19
+
20
+ export function staggerMap(value: any, fn: (v: any) => any) {
21
+ if (isStagger(value))
22
+ return new Stagger((...args: any[]) => fn(value.call(...args)));
23
+
24
+ return fn(value);
25
+ }
26
+
27
+ export default class Stagger {
28
+ private fn: (...args: any[]) => any;
29
+
30
+ constructor(value: any) {
31
+ if (typeof value === 'function') {
32
+ this.fn = value;
33
+ return;
34
+ }
35
+
36
+ // if (value === )
37
+ throw new Error('unknown stagger value ' + value);
38
+ }
39
+
40
+ call(...args: any[]) {
41
+ return this.fn(...args);
42
+ }
43
+
44
+ __isStagger__() {}
45
+ }
@@ -1,8 +1,10 @@
1
- import StyleValue from '../values/stylevalue.js';
1
+ import Value from '../values/Value';
2
+ import StyleValue from '../values/StyleValue';
2
3
 
4
+ export default class BaseTarget<T> {
5
+ target: T;
3
6
 
4
- export default class BaseTarget {
5
- constructor(target) {
7
+ constructor(target: T) {
6
8
  this.target = target;
7
9
 
8
10
  // this might be an object, a dom node
@@ -13,74 +15,71 @@ export default class BaseTarget {
13
15
  return 'unknown';
14
16
  }
15
17
 
16
- getValue(name) {
18
+ getValue(name: string): Value | null {
17
19
  throw new Error('cannot get value of ' + name);
18
20
  }
19
21
 
20
- setValue(name, _value) {
22
+ setValue(name: string, _value: Value) {
21
23
  throw new Error('cannot set value of ' + name);
22
24
  }
23
25
 
24
- removeValue(name) {
26
+ removeValue(name: string) {
25
27
  throw new Error('cannot remove value of ' + name);
26
28
  }
27
29
 
28
- getTransformValue(name) {
30
+ getTransformValue(name: string): Value | null {
29
31
  return this.getValue(name);
30
32
  }
31
33
 
32
- setTransformValue(name, value) {
34
+ setTransformValue(name: string, value: Value) {
33
35
  return this.setValue(name, value);
34
36
  }
35
37
 
36
- removeTransformValue(name) {
38
+ removeTransformValue(name: string) {
37
39
  return this.removeValue(name);
38
40
  }
39
41
 
40
42
  /**
41
43
  * returns nothing or a style value
42
44
  */
43
- getStyleValue(name) {
45
+ getStyleValue(name: string): StyleValue | null {
44
46
  const val = this.getValue(name);
45
- if (!val)
46
- return val;
47
- return StyleValue([val]);
47
+ if (!val) return val;
48
+ return new StyleValue([val]);
48
49
  }
49
50
 
50
51
  /**
51
52
  * Expects a style value
52
53
  */
53
- setStyleValue(name, value) {
54
- return this.setValue(name, value);
54
+ setStyleValue(name: string, value: StyleValue) {
55
+ return this.setValue(name, value.intoValue());
55
56
  }
56
57
 
57
- removeStyleValue(name) {
58
+ removeStyleValue(name: string) {
58
59
  return this.removeValue(name);
59
60
  }
60
61
 
61
- hasClass(name) {
62
+ hasClass(name: string): boolean {
62
63
  throw new Error('cannot know if class exists ' + name);
63
64
  }
64
65
 
65
- addClass(name) {
66
+ addClass(name: string) {
66
67
  throw new Error('cannot add class ' + name);
67
68
  }
68
69
 
69
- removeClass(name) {
70
+ removeClass(name: string) {
70
71
  throw new Error('cannot remove class ' + name);
71
72
  }
72
73
 
73
74
  /**
74
75
  * Tries to unify both values so their units match
75
- *
76
+ *
76
77
  * a and b need to be a Value
77
78
  */
78
- unifyValues(name, a, b) {
79
- if (!a.unit && b.unit)
80
- a.unit = b.unit;
79
+ unifyValues(name: string, a: Value, b: Value): [Value, Value] {
80
+ if (!a.unit && b.unit) a.unit = b.unit;
81
81
 
82
- if (!b.unit && a.unit)
83
- b.unit = a.unit;
82
+ if (!b.unit && a.unit) b.unit = a.unit;
84
83
 
85
84
  if (a.unit !== b.unit)
86
85
  throw new Error(`${name} cannot unify ${a.unit} and ${b.unit}`);
@@ -89,4 +88,4 @@ export default class BaseTarget {
89
88
  }
90
89
 
91
90
  apply() {}
92
- }
91
+ }
@@ -0,0 +1,281 @@
1
+ import Value from '../values/Value';
2
+ import StyleValue from '../values/StyleValue';
3
+ import { Target } from './Target';
4
+
5
+ export type Element = HTMLElement | SVGElement;
6
+
7
+ export type ExternalFunctions = {
8
+ getComputedStyle: (el: Element) => CSSStyleDeclaration;
9
+ getRootElement: () => Element;
10
+ };
11
+
12
+ export default class DomTarget implements Target {
13
+ private target: Element;
14
+
15
+ private _extFns: ExternalFunctions;
16
+
17
+ private transformValues: Map<string, (Value | null)[]>;
18
+ private styleValues: Map<string, StyleValue | undefined>;
19
+ private values: Map<string, Value>;
20
+
21
+ /**
22
+ * target a dom node or svg
23
+ *
24
+ * extFns: {getComputedStyle, getRootElement}
25
+ */
26
+ constructor(target: Element, extFns: ExternalFunctions) {
27
+ this.target = target;
28
+ this._extFns = extFns;
29
+
30
+ // for the moment
31
+
32
+ this.transformValues = new Map();
33
+ this.styleValues = new Map();
34
+ this.values = new Map();
35
+ }
36
+
37
+ inner() {
38
+ return this.target;
39
+ }
40
+
41
+ type() {
42
+ return 'dom';
43
+ }
44
+
45
+ getValue(name: string): Value | null {
46
+ return this.values.get(name) ?? null;
47
+ }
48
+
49
+ setValue(name: string, value: Value) {
50
+ this.values.set(name, value);
51
+ }
52
+
53
+ removeValue(name: string) {
54
+ this.values.delete(name);
55
+ }
56
+
57
+ getTransformValue(name: string): Value | null {
58
+ const [fn, idx] = TRANSFORM_PROPS[name] ?? [];
59
+ if (!fn) return null;
60
+
61
+ const r = this.transformValues.get(fn);
62
+ return r?.[idx ?? 0] ?? null;
63
+ }
64
+
65
+ setTransformValue(name: string, value: Value) {
66
+ const [fn, idx] = TRANSFORM_PROPS[name] ?? [];
67
+ if (!fn) throw new Error('unknown transform ' + name);
68
+
69
+ let vals = this.transformValues.get(fn);
70
+ if (!vals) vals = Array(TRANSFORM_FUNCTIONS[fn].length).fill(null);
71
+
72
+ if (idx === null) vals = vals.map(() => value.clone());
73
+ else vals[idx] = value;
74
+
75
+ this.transformValues.set(fn, vals);
76
+ }
77
+
78
+ removeTransformValue(name: string) {
79
+ const [fn, idx] = TRANSFORM_PROPS[name] ?? [];
80
+ if (!fn) return;
81
+
82
+ if (idx === null) {
83
+ this.transformValues.delete(fn);
84
+ return;
85
+ }
86
+
87
+ let vals = this.transformValues.get(fn);
88
+ if (!vals) return;
89
+
90
+ vals[idx] = null;
91
+
92
+ if (vals.every(v => v === null)) this.transformValues.delete(fn);
93
+ }
94
+
95
+ getStyleValue(name: string): StyleValue | null {
96
+ const v = this.styleValues.get(name) ?? null;
97
+
98
+ if (v !== null) return v;
99
+
100
+ // let's try to get it from the dom
101
+ const style = this._extFns.getComputedStyle(this.target);
102
+ const styleV = style.getPropertyValue(name);
103
+
104
+ if (typeof styleV === 'undefined' || styleV === null) {
105
+ return styleV;
106
+ }
107
+
108
+ try {
109
+ const value = StyleValue.parse(styleV);
110
+ this.styleValues.set(name, value);
111
+
112
+ return value;
113
+ } catch (e) {
114
+ console.log('could not parse value', styleV);
115
+ return v;
116
+ }
117
+ }
118
+
119
+ setStyleValue(name: string, value: StyleValue) {
120
+ this.styleValues.set(name, value);
121
+ }
122
+
123
+ removeStyleValue(name: string) {
124
+ this.styleValues.set(name, undefined);
125
+ }
126
+
127
+ hasClass(name: string): boolean {
128
+ return this.target.classList.contains(name);
129
+ }
130
+
131
+ addClass(name: string) {
132
+ return this.target.classList.add(name);
133
+ }
134
+
135
+ removeClass(name: string) {
136
+ return this.target.classList.remove(name);
137
+ }
138
+
139
+ private _getRem(): number {
140
+ const rootEl = this._extFns.getRootElement();
141
+ const style = this._extFns.getComputedStyle(rootEl);
142
+ const fontSize = Value.parse(style.fontSize);
143
+ if (fontSize.unit !== 'px')
144
+ throw new Error('rem could not be determined');
145
+
146
+ return fontSize.num;
147
+ }
148
+
149
+ private _convertToPx(name: string, value: Value) {
150
+ if (value.num === 0) return new Value(0, 'px');
151
+
152
+ switch (value.unit) {
153
+ case 'px':
154
+ return value;
155
+ case 'rem':
156
+ return new Value(value.num * this._getRem(), 'px');
157
+ default:
158
+ throw new Error(
159
+ `cannot convert ${name} from ${value.unit} to px`,
160
+ );
161
+ }
162
+ }
163
+
164
+ // value needs to be a unit with pixels
165
+ private _convertFromPx(name: string, value: Value, unit: string) {
166
+ if (value.num === 0) return new Value(0, unit);
167
+
168
+ let v;
169
+ switch (unit) {
170
+ case 'px':
171
+ return value;
172
+
173
+ case 'rem':
174
+ return new Value(value.num / this._getRem(), 'rem');
175
+
176
+ case '%':
177
+ v = this.getStyleValue(name)?.intoValue();
178
+ if (!v || v.unit !== 'px')
179
+ throw new Error('cannot get style value of ' + name);
180
+
181
+ return new Value((value.num / v.num) * 100, '%');
182
+
183
+ default:
184
+ throw new Error(`cannot convert ${name} from px to ${unit}`);
185
+ }
186
+ }
187
+
188
+ unifyValues(name: string, a: Value, b: Value): [Value, Value] {
189
+ if (a.unit === b.unit) return [a, b];
190
+
191
+ if (!a.unit && b.unit) a.unit = b.unit;
192
+
193
+ if (!b.unit && a.unit) b.unit = a.unit;
194
+
195
+ const aPx = this._convertToPx(name, a);
196
+ const aBUnit = this._convertFromPx(name, aPx, b.unit!);
197
+
198
+ return [aBUnit, b];
199
+ }
200
+
201
+ apply() {
202
+ this.target.style.transform = Array.from(this.transformValues.entries())
203
+ .map(([k, vals]) => {
204
+ // since we allow null in default Values we now need to make
205
+ // sure either a value or a default value are set for the
206
+ // transform function
207
+
208
+ const defVals = TRANSFORM_FUNCTIONS[k];
209
+ if (vals.length !== defVals.length)
210
+ throw new Error("transform values don't match");
211
+
212
+ const nArray = Array(vals.length);
213
+
214
+ for (let i = 0; i < vals.length; i++) {
215
+ let val = vals[i];
216
+
217
+ if (val === null) val = new Value(defVals[i][0]);
218
+
219
+ nArray[i] = val.withDefaultUnit(defVals[i][1]);
220
+ }
221
+
222
+ return `${k}(${nArray.join(',')})`;
223
+ })
224
+ .join(' ');
225
+
226
+ // style
227
+ for (const [k, v] of this.styleValues.entries()) {
228
+ if (v === undefined) {
229
+ this.target.style.removeProperty(k);
230
+ // is this a problem?
231
+ this.styleValues.delete(k);
232
+ } else {
233
+ this.target.style.setProperty(k, v.toString());
234
+ }
235
+ }
236
+
237
+ // values
238
+ for (const [k, _v] of this.values.entries()) {
239
+ console.log('what todo with this ' + k);
240
+ }
241
+ }
242
+ }
243
+
244
+ const TRANSFORM_PROPS: Record<string, [string, number | null]> = {
245
+ x: ['translate3d', 0],
246
+ y: ['translate3d', 1],
247
+ z: ['translate3d', 2],
248
+ scale: ['scale3d', null],
249
+ scaleX: ['scale3d', 0],
250
+ scaleY: ['scale3d', 1],
251
+ scaleZ: ['scale3d', 2],
252
+ rotate: ['rotate3d', null],
253
+ rotateX: ['rotate3d', 0],
254
+ rotateY: ['rotate3d', 1],
255
+ rotateZ: ['rotate3d', 2],
256
+ skew: ['skew', 0],
257
+ skewX: ['skew', 0],
258
+ skewY: ['skew', 1],
259
+ };
260
+
261
+ const TRANSFORM_FUNCTIONS: Record<string, [number, string | null][]> = {
262
+ translate3d: [
263
+ [0, 'px'],
264
+ [0, 'px'],
265
+ [0, 'px'],
266
+ ],
267
+ scale3d: [
268
+ [1, null],
269
+ [1, null],
270
+ [1, null],
271
+ ],
272
+ rotate3d: [
273
+ [0, 'deg'],
274
+ [0, 'deg'],
275
+ [0, 'deg'],
276
+ ],
277
+ skew: [
278
+ [0, 'deg'],
279
+ [0, 'deg'],
280
+ ],
281
+ };
@@ -0,0 +1,48 @@
1
+ import Value from '../values/Value';
2
+ import DomTarget from './DomTarget';
3
+ import StyleValue from '../values/StyleValue';
4
+
5
+ export function newTarget(target: any): Target {
6
+ if (typeof window === 'undefined') {
7
+ if (typeof target.__simulatedDom__ === 'function') {
8
+ return new DomTarget(target, {
9
+ getComputedStyle: target.__getComputedStyleFn__(),
10
+ getRootElement: target.__getRootElementFn__(),
11
+ });
12
+ }
13
+ } else {
14
+ if (target instanceof HTMLElement || target instanceof SVGElement) {
15
+ return new DomTarget(target, {
16
+ getComputedStyle: e => window.getComputedStyle(e),
17
+ getRootElement: () => document.documentElement,
18
+ });
19
+ }
20
+ }
21
+
22
+ throw new Error('unknown target');
23
+ }
24
+
25
+ export interface Target {
26
+ inner(): any;
27
+ type(): string;
28
+
29
+ getValue(name: string): Value | null;
30
+ setValue(name: string, value: Value): void;
31
+ removeValue(name: string): void;
32
+
33
+ getTransformValue(name: string): Value | null;
34
+ setTransformValue(name: string, value: Value): void;
35
+ removeTransformValue(name: string): void;
36
+
37
+ getStyleValue(name: string): StyleValue | null;
38
+ setStyleValue(name: string, value: StyleValue): void;
39
+ removeStyleValue(name: string): void;
40
+
41
+ hasClass(name: string): boolean;
42
+ addClass(name: string): void;
43
+ removeClass(name: string): void;
44
+
45
+ unifyValues(name: string, a: Value, b: Value): [Value, Value];
46
+
47
+ apply(): void;
48
+ }
@@ -1,33 +1,37 @@
1
- let ROOT_ELEMENT = null;
1
+ let ROOT_ELEMENT: TestDomNode | null = null;
2
2
 
3
3
  export default class TestDomNode {
4
+ computedStyle: any;
5
+ style: any;
6
+ classList: any;
7
+
4
8
  constructor() {
5
9
  this.computedStyle = {
6
- getPropertyValue(k) {
10
+ getPropertyValue(k: string) {
7
11
  return this[k];
8
- }
12
+ },
9
13
  };
10
14
 
11
15
  this.style = {
12
- setProperty: (k, v) => {
16
+ setProperty: (k: string, v: string) => {
13
17
  this.style[k] = v;
14
18
  },
15
- removeProperty: (k) => {
19
+ removeProperty: (k: string) => {
16
20
  delete this.style[k];
17
- }
21
+ },
18
22
  };
19
23
 
20
24
  this.classList = {
21
- set: new Set,
22
- contains(name) {
25
+ set: new Set(),
26
+ contains(name: string) {
23
27
  return this.set.has(name);
24
28
  },
25
- add(name) {
29
+ add(name: string) {
26
30
  this.set.add(name);
27
31
  },
28
- remove(name) {
32
+ remove(name: string) {
29
33
  this.set.delete(name);
30
- }
34
+ },
31
35
  };
32
36
  }
33
37
 
@@ -35,7 +39,7 @@ export default class TestDomNode {
35
39
  this.computedStyle = { ...this.style };
36
40
  }
37
41
 
38
- static getComputedStyle(el) {
42
+ static getComputedStyle(el: TestDomNode): CSSStyleDeclaration {
39
43
  return el.computedStyle;
40
44
  }
41
45
 
@@ -55,7 +59,6 @@ export function el() {
55
59
  }
56
60
 
57
61
  export function getRootElement() {
58
- if (!ROOT_ELEMENT)
59
- ROOT_ELEMENT = el();
62
+ if (!ROOT_ELEMENT) ROOT_ELEMENT = el();
60
63
  return ROOT_ELEMENT;
61
- }
64
+ }