dothtml-interfaces 0.3.13 → 0.4.0

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dothtml-interfaces",
3
- "version": "0.3.13",
3
+ "version": "0.4.0",
4
4
  "description": "Dependency injection interfaces for DOTHtml.",
5
5
  "main": "src/index.d.ts",
6
6
  "types": "src/index.d.ts",
@@ -1,6 +1,5 @@
1
1
 
2
2
  import { IDotCore, IDotDocument } from "./i-dot";
3
- import IDotCss from "./styles/i-dot-css";
4
3
 
5
4
  // export type EventNames<T> = T extends { allowedEvents: infer E } ? E : never;
6
5
 
@@ -18,7 +17,7 @@ export interface FrameworkItems {
18
17
  readonly shadowRoot: ShadowRoot;
19
18
  readonly isRendered: boolean;
20
19
  readonly tagName: string;
21
- readonly styles: Array<string>;
20
+ // readonly styles: Array<string>; // Can't have this anymore because it's a memory leak.
22
21
  readonly args: Array<any>;
23
22
  // readonly styleElement: HTMLStyleElement;
24
23
  readonly sharedStyles: CSSStyleSheet[];
@@ -31,7 +30,7 @@ export interface FrameworkItems {
31
30
  // TODO: there's a weird problem where if a constructor is not provided, it's not possible have a custom builder.
32
31
  // It should be the contsructor that depends on the builder, not the other way around. If we can't get this working,
33
32
  // it might just be better to rethink how stuff gets passed into components.
34
- export default interface IComponent/*<TProps extends Array<string> = [], TEvents extends Array<string> = []>*/ {
33
+ export default interface IDotComponent/*<TProps extends Array<string> = [], TEvents extends Array<string> = []>*/ {
35
34
 
36
35
  readonly _?: FrameworkItems;
37
36
 
@@ -77,8 +76,9 @@ export default interface IComponent/*<TProps extends Array<string> = [], TEvents
77
76
  * A function that returns a string containing CSS rules to be applied to all instances of the component.
78
77
  * It will only be called exactly once per component. We use a function so that you can return a potentially
79
78
  * large string containing CSS without introducing a memory leak. The reason it's not static is so that
80
- * it will work in JavaScript, and because static members are not valid in DI interfaces.
81
- * @returns A string containing CSS rules.
79
+ * it will work in JavaScript, and because static members are not valid in DI interfaces. All styles are scoped
80
+ * to the component's shadow DOM.
81
+ * @returns A string (or array of strings) containing imported CSS rules.
82
82
  */
83
- stylize?(): string;
83
+ stylize?(): string|Array<string>;
84
84
  }
package/src/i-dot.d.ts CHANGED
@@ -1,15 +1,15 @@
1
1
 
2
- import IComponent, { FrameworkItems } from "./i-component";
2
+ import IDotComponent from "./i-dot-component";
3
3
  import IDotCss from "./styles/i-dot-css";
4
4
  import IEventBus from "./i-event-bus";
5
- import {IReactive} from "./i-reactive";
5
+ import {IBoundReactive, IReactive} from "./i-reactive";
6
6
  import IDotcssProp from "./styles/i-css-prop";
7
7
 
8
8
  type DotContentPrimitive = string | number | boolean;
9
- type DotContentBasic = DotContentPrimitive | Node | Element | NodeList | IComponent | IDotDocument//typeof DotDocument;
10
- export type DotContent = DotContentBasic | Array<DotContent> | IReactive;//|(()=>DotContent);
9
+ type DotContentBasic = DotContentPrimitive | Node | Element | NodeList | IDotComponent | IDotDocument//typeof DotDocument;
10
+ export type DotContent = DotContentBasic | Array<DotContent> | IBoundReactive;//|(()=>DotContent);
11
11
 
12
- type AttrVal<T = string | number | boolean> = T | IReactive<T>;
12
+ type AttrVal<T = string | number | boolean> = T | IBoundReactive<T>;
13
13
 
14
14
  /**
15
15
  * Global interface containing elements.
@@ -25,7 +25,7 @@ export interface IDotDocument {
25
25
  /**
26
26
  * A conditional function, analogous to if. Renders the specified DOT if a condition is met. Dynamic binding is possible when condition and callback are functions.
27
27
  */
28
- when(condition: IReactive | boolean, DotContent): IDotConditionalDocument;
28
+ when(condition: IBoundReactive | boolean, DotContent): IDotConditionalDocument;
29
29
 
30
30
  // Main functions.
31
31
  // TODO: please make this into a test case.
@@ -46,16 +46,16 @@ export interface IDotDocument {
46
46
  /**
47
47
  * Creates a generic HTML node that can render a string, HTML nodes, or dotHTML content.
48
48
  */
49
- html(content: string | number | boolean | IReactive): IDotDocument;
49
+ html(content: string | number | boolean | IBoundReactive): IDotDocument;
50
50
  /**
51
51
  * Creates a text node that will render as a string, rather than being parsed as markup.
52
52
  */
53
- text(content: string | number | boolean | IReactive): IDotDocument;
53
+ text(content: string | number | boolean | IBoundReactive): IDotDocument;
54
54
  /**
55
55
  * Mounts a component.
56
56
  * TODO: add second arg.
57
57
  */
58
- mount<T extends IComponent>(component: T): IDotDocument;
58
+ mount<T extends IDotComponent>(component: T): IDotDocument;
59
59
  // mount<T extends IComponent>(init: (c: IMountedComponent<T>) => IMountedComponent<T> | void, component: T): IDotDocument;
60
60
  // mount(component: IComponent, init: (init=>IMountedComponent): IMountedComponent|void): IDotDocument;
61
61
  /**
@@ -65,7 +65,7 @@ export interface IDotDocument {
65
65
  */
66
66
  iterate(n: number, callback: (i: number) => DotContent): IDotDocument;
67
67
  each<T>(a: Array<T> | { [key: string | number]: T }, callback: (x: T, i: number, k: string | number) => DotContent): IDotDocument;
68
- each<T>(a: IReactive<any, Array<T> | { [key: string | number]: T }>, callback: (x: T, i: IReactive<number>, k: string | number) => DotContent): IDotDocument;
68
+ each<T>(a: IBoundReactive<any, Array<T> | { [key: string | number]: T }>, callback: (x: T, i: IBoundReactive<number>, k: string | number) => DotContent): IDotDocument;
69
69
 
70
70
  /**
71
71
  * Removes the targeted document and everything in it.
@@ -372,7 +372,7 @@ export interface IDotCore extends IDotDocument {
372
372
  bus: IEventBus;
373
373
  window: IDotWindowBuilder;
374
374
 
375
- watch<Ti = IReactive | Array<any> | { [key: string | number]: any } | string | number | boolean, To = Ti>(initValue?: Ti, props?: { key?: string, transformer?: (value: Ti) => To }): IReactive<Ti, To>;
375
+ watch<Ti = IBoundReactive | Array<any> | { [key: string | number]: any } | string | number | boolean>(initValue?: Ti, key?: (Ti extends Array<any> | { [key: string | number]: any } ? string : never)): IReactive<Ti>;
376
376
 
377
377
  // Keep these around for a bit to show how it was done before in case I need to change anything prior to the v6 launch.
378
378
  // component<T extends IComponent>(Base: new (...args: Parameters<T['build']>) => T): new (...args: Parameters<T['build']>) => T;
@@ -382,8 +382,8 @@ export interface IDotCore extends IDotDocument {
382
382
  // Works but doesn't infer types from the component.
383
383
  // There's room for improvement here but it's not clear to me how to do it.
384
384
  // What I'd like to do is have the types tied to the IComponent interface rather than the component factory function.
385
- component<TProps extends string[] = [], TEvents extends string[] = []>(Base: new () => IComponent, styles?: string|IDotcssProp|Array<string|IDotcssProp>)
386
- : new (attrs?: ComponentArgs<TProps, TEvents>) => IComponent & { new (attrs?: ComponentArgs<TProps, TEvents>): IComponent };
385
+ component<TProps extends string[] = [], TEvents extends string[] = []>(Base: new () => IDotComponent, styles?: string|IDotcssProp|Array<string|IDotcssProp>)
386
+ : new (attrs?: ComponentArgs<TProps, TEvents>) => IDotComponent & { new (attrs?: ComponentArgs<TProps, TEvents>): IDotComponent };
387
387
 
388
388
  useStyles(document: Document, styles: Styles): HTMLStyleElement;
389
389
  }
@@ -399,7 +399,7 @@ export interface IDotWindowWrapper{
399
399
  }
400
400
 
401
401
  export interface IDotWindowBuilder {
402
- (options: {content: IDotDocument, width?: number, height?: number, title?: string}): IDotWindowWrapper;
402
+ (options: {content: IDotComponent|IDotDocument, width?: number, height?: number, title?: string}): IDotWindowWrapper;
403
403
  }
404
404
 
405
405
  export interface IDotConditionalDocument extends IDotDocument {
@@ -407,7 +407,7 @@ export interface IDotConditionalDocument extends IDotDocument {
407
407
  * A conditional catch, analogous to else if. Can be used after a when function. Evaluates if the previous when's condition was false.
408
408
  * Renders the specified DOT if a condition is met. Dynamic binding is possible when condition and callback are functions.
409
409
  */
410
- otherwiseWhen(condition: IReactive | boolean, callback: DotContent): IDotConditionalDocument;
410
+ otherwiseWhen(condition: IBoundReactive | boolean, callback: DotContent): IDotConditionalDocument;
411
411
  /**
412
412
  * A conditional final catch, analogous to else. Can be used after a when or otherwiseWhen function. Evaluates if the previous when/otherwiseWhen evaluated to false.
413
413
  * Renders the specified DOT if a condition is met. Dynamic binding is possible when callback is a function.
@@ -432,7 +432,7 @@ export interface IDotGlobalAttrs {
432
432
  // TODO: this needs to be redone now.
433
433
  // The better way might be using the new reactive system instead of references.
434
434
  // For now I'll leave it like this:
435
- ref?: IReactive<HTMLElement>;
435
+ // ref?: IReactive<HTMLElement>;
436
436
 
437
437
  /** @deprecated Deprecated in HTML5. Use CSS. */
438
438
  bgColor?: AttrVal<unknown>;
@@ -995,7 +995,7 @@ interface IDotTrack extends IDotGlobalAttrs {
995
995
 
996
996
  interface IDotVideo extends IDotGlobalAttrs {
997
997
  autoPlay?: AttrVal<boolean>;
998
- buffered?: IReactive<unknown>; // Managed by browser not user. TODO: we can possibly use events to update observable objects.
998
+ buffered?: IBoundReactive<unknown>; // Managed by browser not user. TODO: we can possibly use events to update observable objects.
999
999
  controls?: AttrVal<boolean>;
1000
1000
  crossOrigin?: AttrVal<"anonymous"> | AttrVal<"use-credentials">;
1001
1001
  height?: AttrVal<number>;
@@ -1,25 +1,34 @@
1
1
 
2
- export interface IReactive<Ti = any, To = Ti>{
2
+ export interface IReactive<T = any> extends IBoundReactive<T, T>{
3
3
  // The untransformed value.
4
- _value: Ti;
4
+ _value: T;
5
5
  // Get the value.
6
- getValue(): To;
6
+ getValue(): T;
7
7
  // Set the value.
8
- setValue(v: Ti|null|undefined);
8
+ setValue(v: T|null|undefined);
9
9
 
10
10
  // Key is used for observable array proxy bindings.
11
11
  // If a key is provided, it's used to uniquely identify array elements.
12
12
  // If a key is not provided, identification is done automatically by the framework by comparing object references.
13
13
  key: string;
14
- // Optional transformer that can transform the input.
15
- transform?: (input: Ti)=>To;
16
14
  // subscribeNode(node: Node): number;
17
15
  // subscribeAttr(node: HTMLElement, attributeName: string): number;
18
- subscribeCallback(callback: (value: To)=>void): number;
16
+ subscribeCallback(callback: (value: T)=>void): number;
19
17
  detachBinding(id: number);
20
18
  updateObservers(): void;
19
+
20
+ bindAs<Td = string>(transform: {
21
+ display?: (v: T)=>Td;
22
+ read?: (v: string)=>T;
23
+ }): IBoundReactive<T, Td>;
21
24
  }
22
25
 
23
26
  export interface IReactiveWatcher<T = any>{
24
27
  observerUpdate(value: T, obsreverId: number): void;
28
+ }
29
+
30
+ export interface IBoundReactive<T = any, Td = T>{
31
+ _source: IReactive<T>;
32
+ _get: ()=>Td;
33
+ _set: (v: string)=>void;
25
34
  }
package/src/index.d.ts CHANGED
@@ -6,7 +6,7 @@ export { default as IDotCss } from "./styles/i-dot-css";
6
6
  export * from "./styles/i-dot-css";
7
7
 
8
8
 
9
- export { default as IComponent, FrameworkItems } from "./i-component";
9
+ export { default as IDotComponent, FrameworkItems } from "./i-dot-component";
10
10
 
11
- export { IReactive, IReactiveWatcher } from "./i-reactive";
11
+ export { IReactive, IReactiveWatcher, IBoundReactive } from "./i-reactive";
12
12
  export { default as IEventBus } from "./i-event-bus";
@@ -1,11 +1,11 @@
1
- import { IReactive } from "../i-reactive";
1
+ import { IBoundReactive } from "../i-reactive";
2
2
 
3
3
  // Global keyword values.
4
- export type GKV = IReactive<string>|"inherit"|"initial"|"unset"|"revert"|"revert-layer";
4
+ export type GKV = IBoundReactive<string>|"inherit"|"initial"|"unset"|"revert"|"revert-layer";
5
5
 
6
6
  export type ComplexType = string;
7
7
 
8
- export type ValueOrReactive<T> = T|IReactive<T>;
8
+ export type ValueOrReactive<T> = T|IBoundReactive<T>;
9
9
 
10
10
  // BASIC TYPES
11
11
  export type Str = `"${string|""}"`|`'${string|""}'`; // TODO: wherever str is required, we could just inject quotes...
@@ -1,11 +1,11 @@
1
1
 
2
- import { IReactive } from "../i-reactive";
3
2
  import { GKV, ValueOrReactive } from "./css-types";
4
3
  import IFilterProp from "./complex-css-types/i-filter-prop";
5
4
  import IShadowProp from "./complex-css-types/i-shadow-prop";
6
5
  import ITransformationProp from "./complex-css-types/i-transformation-prop";
7
6
  import ColorProp from "./mapped-types/color-props";
8
7
  import LengthProp from "./mapped-types/length-prop";
8
+ import { IBoundReactive } from "../i-reactive";
9
9
 
10
10
  type BackgroundPositionShorthand2D = string;
11
11
  type BackgroundRepeatValues2d = "no-repeat"|"repeat"|"space"|"round";
@@ -93,7 +93,7 @@ export default interface IDotcssProp extends
93
93
 
94
94
  background?: GKV|string; // TODO: shorthand. Requires advanced typing.
95
95
  backgroundAttachment?: GKV|"scroll"|"fixed"|"local"; // ✔️
96
- backgroundBlendMode?: GKV|Array<IReactive<string>|"darken"|"luminocity"|"normal">; // ✔️
96
+ backgroundBlendMode?: GKV|Array<IBoundReactive<string>|"darken"|"luminocity"|"normal">; // ✔️
97
97
  backgroundPosition?: GKV|BackgroundPositionShorthand2D;
98
98
  backgroundRepeat?: GKV|BackgroundRepeatValues2d;
99
99
  backgroundClip?: GKV|string;
@@ -1,11 +1,4 @@
1
- // import {IReactive} from "../i-reactive";
2
- // import { GKV, Color, ComplexType, Percentage } from "./css-types";
3
- // import IDotcssProp from "./i-css-prop";
4
- // import IFilterProp from "./i-filter-prop";
5
- // import IShadowProp from "./i-shadow-prop";
6
- // import { ITransformationProp } from "./i-transformation-prop";
7
- // import ColorProp from "./mapped-types/color-props";
8
- // import LengthProp from "./mapped-types/length-prop";
1
+
9
2
 
10
3
  import IAtColorProfileBuilder from "./at-rules/i-at-color-profile-builder";
11
4
  import IAtCounterStyleBuilder from "./at-rules/i-at-counter-style-builder";
@@ -1,6 +1,6 @@
1
- import { IReactive } from "../../i-reactive";
1
+ import { IBoundReactive } from "../../i-reactive";
2
2
 
3
- type V = IReactive<any> | number | string;
3
+ type V = IBoundReactive<any> | number | string;
4
4
 
5
5
  type AngleUnitSuffix = "" | "Deg" | "Grad" | "Rad" | "Turn";
6
6
 
@@ -1,6 +1,6 @@
1
- import { IReactive } from "../../i-reactive";
1
+ import { IBoundReactive } from "../../i-reactive";
2
2
 
3
- type V = IReactive<number|string> | number | string;
3
+ type V = IBoundReactive<number|string> | number | string;
4
4
 
5
5
  type ColorUnitSuffix = ""
6
6
  | "Rgb"
@@ -20,7 +20,7 @@ type SpecialColors = "currentcolor"|"transparent";
20
20
 
21
21
  type ColorProp<Prefix extends string> = {
22
22
  [Key in ColorUnitSuffix as `${Prefix}${Key}`]?: (
23
- (Key extends "" ? IReactive<string> | NamedColor | SystemColor | SpecialColors | `#${string}` | `${"rgb"|"rgba"|"hsl"|"hsla"|"hwb"|"lab"|"lch"|"oklab"|"oklch"|"color"}(${string})` : void) |
23
+ (Key extends "" ? IBoundReactive<string> | NamedColor | SystemColor | SpecialColors | `#${string}` | `${"rgb"|"rgba"|"hsl"|"hsla"|"hwb"|"lab"|"lch"|"oklab"|"oklch"|"color"}(${string})` : void) |
24
24
  (Key extends "Rgb" | "Hsl" | "HslDeg" | "HslGrad" | "HslRad" | "HslTurn" | "Hwb" | "HwbDeg" | "HwbGrad" | "HwbRad" | "HwbTurn" | "Lab" | "Lch" | "LchDeg" | "LchGrad" | "LchRad" | "LchTurn" | "Oklch" | "OklchDeg" | "OklchGrad" | "OklchRad" | "OklchTurn" ? [V, V, V] | [V, V, V, V] : void) |
25
25
  (Key extends "Srgb" | "SrgbLinear" | "DisplayP3" | "A98Rgb" | "ProphotoRgb" | "Rec2020" | "Xyz" | "XyzD50" | "XyzD65" ? [V, V, V] : void)
26
26
  );
@@ -1,12 +1,12 @@
1
- import { IReactive } from "../../i-reactive";
1
+ import { IBoundReactive } from "../../i-reactive";
2
2
  import { GKV } from "../css-types";
3
3
  // import { NumericLength } from "./css-types";
4
4
 
5
5
  type LengthUnitSuffix = "" | "Cm" | "Ch" | "Em" | "Ex" | "In" | "Mm" | "P" | "Pc" | "Pt" | "Px" | "Rem" | "Vh" | "Vw" | "VMax" | "VMin";
6
6
 
7
- type V<S> = IReactive<any> | number | S;
7
+ type V<S> = IBoundReactive<any> | number | S;
8
8
 
9
- type LengthProp<Prefix extends string, Qty extends 1|2|3|4 = 1, S extends string|IReactive<any> = GKV> = {
9
+ type LengthProp<Prefix extends string, Qty extends 1|2|3|4 = 1, S extends string|IBoundReactive<any> = GKV> = {
10
10
  [Key in LengthUnitSuffix as `${Prefix}${Key}`]?: (
11
11
  (Qty extends 1 ? V<S>|[V<S>] : void) |
12
12
  (Qty extends 2 ? [V<S>, V<S>] : void) |