nesquick 0.0.15 → 0.0.17

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.
@@ -10,11 +10,16 @@ function functionizeProps(props) {
10
10
  }
11
11
  }
12
12
  }
13
+ const SVGNamespaces = new Map([
14
+ ["xlink", "http://www.w3.org/1999/xlink"],
15
+ ["xml", "http://www.w3.org/XML/1998/namespace"]
16
+ ]);
13
17
  class NesquickComponent {
14
18
  constructor(_render, props) {
15
19
  this._render = _render;
16
20
  this.props = props;
17
21
  this._subscriptions = new State_1.Subscriptions();
22
+ this._xmlns = null;
18
23
  this._children = [];
19
24
  this.props = props;
20
25
  }
@@ -22,22 +27,108 @@ class NesquickComponent {
22
27
  State_1.subscriptions.set(this._subscriptions);
23
28
  if (typeof this._render === "function") {
24
29
  functionizeProps(this.props);
25
- const res = this._render(this.props).render(document);
30
+ const element = this._render(this.props);
31
+ if (this._xmlns) {
32
+ element.setXmlns(this._xmlns);
33
+ }
34
+ const res = element.render(document);
26
35
  State_1.subscriptions.reset();
27
36
  return res;
28
37
  }
29
- const element = document.createElement(this._render);
30
- if (this.props != null) {
31
- this._renderProps(element, this.props);
38
+ if (this.props?.xmlns != null) {
39
+ let namespace = typeof this.props.xmlns === "function" ? this.props.xmlns() : this.props.xmlns;
40
+ if (namespace != null) {
41
+ namespace = String(namespace);
42
+ this._xmlns = {
43
+ ns: namespace,
44
+ attributes: namespace === "http://www.w3.org/2000/svg" ? SVGNamespaces : null
45
+ };
46
+ }
47
+ }
48
+ else if (this._render === "svg") {
49
+ this._xmlns = {
50
+ ns: "http://www.w3.org/2000/svg",
51
+ attributes: SVGNamespaces
52
+ };
53
+ }
54
+ let element;
55
+ if (this._xmlns?.ns) {
56
+ element = document.createElementNS(this._xmlns.ns, this._render);
57
+ if (this.props != null) {
58
+ if (this._xmlns.attributes) {
59
+ this._renderPropsNs(this._xmlns.attributes, element, this.props);
60
+ }
61
+ else {
62
+ this._renderProps(element, this.props);
63
+ }
64
+ }
65
+ }
66
+ else {
67
+ element = document.createElement(this._render);
68
+ if (this.props != null) {
69
+ this._renderProps(element, this.props);
70
+ }
32
71
  }
33
72
  this._renderChildren(document, element, this.props.children);
34
73
  this.props = {};
35
74
  State_1.subscriptions.reset();
36
75
  return element;
37
76
  }
77
+ setXmlns(xmlns) {
78
+ this._xmlns = xmlns;
79
+ }
80
+ _getAttributeNs(attributes, k) {
81
+ const index = k.indexOf(":");
82
+ if (index > -1) {
83
+ const ns = k.substring(0, index);
84
+ const name = k.substring(index + 1);
85
+ const namespace = attributes.get(ns);
86
+ if (namespace != null) {
87
+ return {
88
+ namespace: namespace,
89
+ name: name
90
+ };
91
+ }
92
+ }
93
+ return null;
94
+ }
95
+ _renderPropsNs(attributes, element, props) {
96
+ for (const k in props) {
97
+ if (k !== "children" && k !== "xmlns") {
98
+ if (typeof props[k] === "function") {
99
+ if (k.startsWith("on")) {
100
+ element[k.toLowerCase()] = props[k];
101
+ }
102
+ else {
103
+ const attribute = this._getAttributeNs(attributes, k);
104
+ if (attribute) {
105
+ (0, State_1.useRender)(props[k], v => {
106
+ element.setAttributeNS(attribute.namespace, attribute.name, String(v));
107
+ });
108
+ }
109
+ else {
110
+ (0, State_1.useRender)(props[k], v => {
111
+ element.setAttribute(k, String(v));
112
+ });
113
+ }
114
+ }
115
+ }
116
+ else {
117
+ const attribute = this._getAttributeNs(attributes, k);
118
+ if (attribute) {
119
+ element.setAttributeNS(attribute.namespace, attribute.name, String(props[k]));
120
+ }
121
+ else {
122
+ element.setAttribute(k, String(props[k]));
123
+ }
124
+ }
125
+ }
126
+ }
127
+ }
128
+ ;
38
129
  _renderProps(element, props) {
39
130
  for (const k in props) {
40
- if (k !== "children") {
131
+ if (k !== "children" && k !== "xmlns") {
41
132
  if (typeof props[k] === "function") {
42
133
  if (k.startsWith("on")) {
43
134
  element[k.toLowerCase()] = props[k];
@@ -136,6 +227,9 @@ class NesquickComponent {
136
227
  if (child instanceof NesquickFragment_1.NesquickFragment || Array.isArray(child)) {
137
228
  nesquickChild.component = null;
138
229
  nesquickChild.fragment = Array.isArray(child) ? new NesquickFragment_1.NesquickFragment(child) : child;
230
+ if (this._xmlns) {
231
+ nesquickChild.fragment.setXmlns(this._xmlns);
232
+ }
139
233
  const node = nesquickChild.fragment.render(document);
140
234
  const lastChild = node.lastChild;
141
235
  if (nesquickChild.node) {
@@ -149,6 +243,9 @@ class NesquickComponent {
149
243
  else if (child instanceof NesquickComponent) {
150
244
  nesquickChild.component = child;
151
245
  nesquickChild.fragment = null;
246
+ if (this._xmlns) {
247
+ child.setXmlns(this._xmlns);
248
+ }
152
249
  const node = child.render(document);
153
250
  if (nesquickChild.node) {
154
251
  parent.replaceChild(node, nesquickChild.node);
@@ -3,6 +3,7 @@ export type Children = Child | Child[];
3
3
  export type ChildFunc = () => Exclude<Child, ChildFunc> | Exclude<Child, ChildFunc>[];
4
4
  export type ComponentProps = Record<string, any>;
5
5
  export type FunctionComponent<P extends ComponentProps = {}> = (props: P) => NesquickComponent<P>;
6
+ export type VeactDocument = Pick<Document, "createElement" | "createElementNS" | "createTextNode" | "createDocumentFragment" | "createComment">;
6
7
  type NesquickChild = {
7
8
  node: Node | null;
8
9
  } & ({
@@ -17,20 +18,28 @@ export type NesquickParent = {
17
18
  replaceChild(newChild: Node, oldChild: Node): void;
18
19
  insertBefore(node: Node, child: Node | null): void;
19
20
  };
21
+ type XmlNs = {
22
+ ns: string;
23
+ attributes: Map<string, string> | null;
24
+ };
20
25
  export declare class NesquickComponent<P extends ComponentProps = {}> {
21
26
  private _render;
22
27
  protected props: P;
23
28
  private _subscriptions;
29
+ private _xmlns;
24
30
  protected _children: NesquickChild[];
25
31
  constructor(_render: string | FunctionComponent<P>, props: P);
26
- render(document: Document): Node;
32
+ render(document: VeactDocument): Node;
33
+ setXmlns(xmlns: XmlNs | null): void;
34
+ private _getAttributeNs;
35
+ private _renderPropsNs;
27
36
  private _renderProps;
28
- protected _renderChildren(document: Document, parent: NesquickParent, children?: Children): void;
37
+ protected _renderChildren(document: VeactDocument, parent: NesquickParent, children?: Children): void;
29
38
  protected _pushChild(): NesquickChild;
30
39
  protected _spliceChild(i: number): NesquickChild;
31
40
  protected _swapChilds(parent: NesquickParent, i1: number, i2: number): void;
32
41
  protected _removeChild(i: number): void;
33
- protected _renderChild(document: Document, parent: NesquickParent, nesquickChild: NesquickChild, child: Exclude<Child, ChildFunc> | Exclude<Child, ChildFunc>[]): void;
42
+ protected _renderChild(document: VeactDocument, parent: NesquickParent, nesquickChild: NesquickChild, child: Exclude<Child, ChildFunc> | Exclude<Child, ChildFunc>[]): void;
34
43
  dispose(): void;
35
44
  }
36
45
  import { NesquickFragment } from "./NesquickFragment";
@@ -1,12 +1,12 @@
1
1
  import { JSX } from "./jsx-runtime";
2
- import { NesquickComponent, NesquickParent } from "./NesquickComponent";
2
+ import { NesquickComponent, NesquickParent, VeactDocument } from "./NesquickComponent";
3
3
  export declare class NesquickFragment extends NesquickComponent<{
4
4
  children: any[];
5
5
  }> implements NesquickParent {
6
6
  private _lastNode;
7
7
  private _fragment;
8
8
  constructor(children: any[]);
9
- render(document: Document): Node;
9
+ render(document: VeactDocument): Node;
10
10
  getDocument(): Document | null;
11
11
  getParent(): Node | null;
12
12
  appendChild(child: Node): void;
@@ -11,14 +11,11 @@ type UserProp<T> = T extends (...args: infer A) => infer R ? (((...args: A) => R
11
11
  type ComponentProp<T> = T extends {
12
12
  readonly [WrappedFunctionType]?: infer R;
13
13
  } ? (T | R) : T extends (...args: any[]) => any ? T : (T | (() => T));
14
- type UnFunction<T> = T extends {
15
- readonly [WrappedFunctionType]?: infer R;
16
- } ? (T | R) : T;
17
14
  type UserProps<T> = {
18
- [K in keyof T]: K extends "children" ? T[K] : UserProp<T[K]>;
15
+ [K in keyof T]: UserProp<T[K]>;
19
16
  };
20
17
  type JSXProps<T> = keyof T extends never ? {} : {
21
- [K in keyof T]: K extends "children" ? UnFunction<T[K]> : ComponentProp<T[K]>;
18
+ [K in keyof T]: ComponentProp<T[K]>;
22
19
  };
23
20
  export type Generic<T> = T extends (...args: any) => infer R ? R : T;
24
21
  export { UserProps as Props };
@@ -33,9 +30,10 @@ export declare namespace JSX {
33
30
  export type JSXSVGEvent<T extends EventTarget> = {
34
31
  [K in keyof SVGElementEventMap as `on${Capitalize<K>}`]?: (e: JSXEvent<SVGElementEventMap[K], T>) => void;
35
32
  };
36
- export interface Props<T extends EventTarget = HTMLElement> extends JSXHTMLEvent<T> {
33
+ export interface Props<T extends EventTarget = HTMLElement> extends JSXHTMLEvent<T>, JSXSVGEvent<T> {
37
34
  [k: string]: any;
38
35
  style?: StyleProps;
36
+ xmlns?: string;
39
37
  }
40
38
  export type StyleProps = {
41
39
  [K in keyof CSSStyleDeclaration]?: CSSStyleDeclaration[K] extends Function ? never : CSSStyleDeclaration[K] | (() => CSSStyleDeclaration[K]);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nesquick",
3
- "version": "0.0.15",
3
+ "version": "0.0.17",
4
4
  "description": "React-like library with focus on drawing performance",
5
5
  "types": "./lib/types",
6
6
  "main": "./lib",