@xtia/jel 0.1.2 → 0.2.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.
Files changed (3) hide show
  1. package/index.d.ts +10 -5
  2. package/index.js +68 -65
  3. package/package.json +2 -2
package/index.d.ts CHANGED
@@ -22,8 +22,7 @@ interface ElementDescriptor {
22
22
  [key in keyof CSSStyleDeclaration]: string | number;
23
23
  }> & Record<string, string | number>;
24
24
  }
25
- type HelperSelectorString<T extends string> = T | `${T}${"#" | "."}${any}`;
26
- type DomHelper = ((<T extends keyof HTMLElementTagNameMap>(tagName: T, descriptor: ElementDescriptor) => DomEntity<HTMLElementTagNameMap[T]>) & (<T extends keyof HTMLElementTagNameMap>(selector: HelperSelectorString<T>, content?: DOMContent) => DomEntity<HTMLElementTagNameMap[T]>) & ((selector: string, content?: DOMContent) => DomEntity<any>) & (<T extends HTMLElement>(element: T) => DomEntity<T>) & {
25
+ type DomHelper = ((<T extends keyof HTMLElementTagNameMap>(tagName: T, descriptor: ElementDescriptor) => DomEntity<HTMLElementTagNameMap[T]>) & (<T extends keyof HTMLElementTagNameMap>(selector: `${T}#${string}`, content?: DOMContent) => DomEntity<HTMLElementTagNameMap[T]>) & (<T extends keyof HTMLElementTagNameMap>(selector: `${T}.${string}`, content?: DOMContent) => DomEntity<HTMLElementTagNameMap[T]>) & (<T extends keyof HTMLElementTagNameMap>(selector: T, content?: DOMContent) => DomEntity<HTMLElementTagNameMap[T]>) & (<T extends HTMLElement>(element: T) => DomEntity<T>) & {
27
26
  [T in keyof HTMLElementTagNameMap]: (descriptor: ElementDescriptor) => DomEntity<HTMLElementTagNameMap[T]>;
28
27
  } & {
29
28
  [T in keyof HTMLElementTagNameMap]: (content?: DOMContent) => DomEntity<HTMLElementTagNameMap[T]>;
@@ -31,8 +30,6 @@ type DomHelper = ((<T extends keyof HTMLElementTagNameMap>(tagName: T, descripto
31
30
  type JelComponentData = {
32
31
  dom: DOMContent;
33
32
  };
34
- export declare const $: DomHelper;
35
- declare const componentDataSymbol: unique symbol;
36
33
  type ElementAPI<T extends HTMLElement> = {
37
34
  readonly element: T;
38
35
  content: DOMContent;
@@ -46,5 +43,13 @@ type ElementAPI<T extends HTMLElement> = {
46
43
  append(...content: DOMContent[]): void;
47
44
  remove(): void;
48
45
  };
49
- export declare function definePart<Spec, API extends object | void, EventDataMap extends Record<string, any> = {}>(defaultOptions: Spec, init: (spec: Spec, append: (content: DOMContent) => void, trigger: <K extends keyof EventDataMap>(eventId: K, eventData: EventDataMap[K]) => void) => API): JelConstructor<Spec, API, EventDataMap>;
46
+ type OptionalKeys<T> = {
47
+ [K in keyof T]-?: {} extends Pick<T, K> ? K : never;
48
+ }[keyof T];
49
+ type Optionals<T> = {
50
+ [K in OptionalKeys<T>]-?: Exclude<T[K], undefined>;
51
+ };
52
+ export declare const $: DomHelper;
53
+ declare const componentDataSymbol: unique symbol;
54
+ export declare function definePart<Spec, API extends object | void = void, EventDataMap extends Record<string, any> = {}>(defaultOptions: Optionals<Spec>, init: (spec: Required<Spec>, append: (content: DOMContent) => void, trigger: <K extends keyof EventDataMap>(eventId: K, eventData: EventDataMap[K]) => void) => API): JelConstructor<Spec, API, EventDataMap>;
50
55
  export {};
package/index.js CHANGED
@@ -19,8 +19,9 @@ function createElement(tag, descriptor) {
19
19
  descriptor = { content: descriptor };
20
20
  var ent = getWrappedElement(document.createElement(tag));
21
21
  var applyClasses = function (classes) {
22
- if (Array.isArray(classes))
22
+ if (Array.isArray(classes)) {
23
23
  return classes.forEach(function (c) { return applyClasses(c); });
24
+ }
24
25
  if (typeof classes == "string") {
25
26
  classes.trim().split(/\s+/).forEach(function (c) { return ent.classes.add(c); });
26
27
  return;
@@ -32,7 +33,7 @@ function createElement(tag, descriptor) {
32
33
  });
33
34
  };
34
35
  applyClasses(descriptor.classes || []);
35
- if (descriptor.attribs)
36
+ if (descriptor.attribs) {
36
37
  Object.entries(descriptor.attribs).forEach(function (_a) {
37
38
  var k = _a[0], v = _a[1];
38
39
  if (v === false) {
@@ -40,6 +41,7 @@ function createElement(tag, descriptor) {
40
41
  }
41
42
  ent.element.setAttribute(k, v === true ? k : v);
42
43
  });
44
+ }
43
45
  var addContent = function (content) {
44
46
  if (Array.isArray(content))
45
47
  return content.forEach(function (c) { return addContent(c); });
@@ -48,19 +50,23 @@ function createElement(tag, descriptor) {
48
50
  };
49
51
  if (descriptor.content)
50
52
  addContent(descriptor.content);
51
- descriptor.style && Object.entries(descriptor.style).forEach(function (_a) {
52
- var prop = _a[0], val = _a[1];
53
- if (/\-/.test(prop)) {
54
- ent.element.style.setProperty(prop, val.toString());
55
- }
56
- else {
57
- ent.element.style[prop] = val;
58
- }
59
- });
60
- descriptor.on && Object.entries(descriptor.on).forEach(function (_a) {
61
- var eventName = _a[0], handler = _a[1];
62
- return ent.on(eventName, handler);
63
- });
53
+ if (descriptor.style) {
54
+ Object.entries(descriptor.style).forEach(function (_a) {
55
+ var prop = _a[0], val = _a[1];
56
+ if (/\-/.test(prop)) {
57
+ ent.element.style.setProperty(prop, val.toString());
58
+ }
59
+ else {
60
+ ent.element.style[prop] = val;
61
+ }
62
+ });
63
+ }
64
+ if (descriptor.on) {
65
+ Object.entries(descriptor.on).forEach(function (_a) {
66
+ var eventName = _a[0], handler = _a[1];
67
+ return ent.on(eventName, handler);
68
+ });
69
+ }
64
70
  return ent;
65
71
  }
66
72
  ;
@@ -72,7 +78,7 @@ var isContent = function (value) {
72
78
  || !value;
73
79
  };
74
80
  exports.$ = new Proxy(createElement, {
75
- apply: function (create, _0, _a) {
81
+ apply: function (create, _, _a) {
76
82
  var _b;
77
83
  var selectorOrTagName = _a[0], contentOrDescriptor = _a[1];
78
84
  if (selectorOrTagName instanceof HTMLElement)
@@ -105,53 +111,6 @@ exports.$ = new Proxy(createElement, {
105
111
  });
106
112
  var componentDataSymbol = Symbol("jelComponentData");
107
113
  var elementWrapCache = new WeakMap();
108
- function definePart(defaultOptions, init) {
109
- return (function (partialSpec) {
110
- var _a, _b;
111
- if (partialSpec === void 0) { partialSpec = {}; }
112
- var spec = __assign(__assign({}, defaultOptions), partialSpec);
113
- var eventHandlers = {};
114
- var addEventListener = function (eventId, fn) {
115
- if (!eventHandlers[eventId])
116
- eventHandlers[eventId] = [];
117
- eventHandlers[eventId].push(fn);
118
- };
119
- if (spec.on)
120
- Object.entries(spec.on).forEach(function (_a) {
121
- var eventId = _a[0], handler = _a[1];
122
- addEventListener(eventId, handler);
123
- });
124
- var entity;
125
- var content = [];
126
- var append = function (c) {
127
- if (entity)
128
- throw new Error("Component root content can only be added during initialisation");
129
- content.push(c);
130
- };
131
- var trigger = function (eventId, data) {
132
- var _a;
133
- (_a = eventHandlers[eventId]) === null || _a === void 0 ? void 0 : _a.forEach(function (fn) { return fn.call(entity, data); });
134
- };
135
- var api = init(spec, append, trigger);
136
- entity = api ? Object.create(api, (_a = {},
137
- _a[componentDataSymbol] = {
138
- value: {
139
- dom: content
140
- }
141
- },
142
- _a.on = {
143
- value: addEventListener,
144
- },
145
- _a)) : (_b = {},
146
- _b[componentDataSymbol] = {
147
- dom: content,
148
- },
149
- _b.on = addEventListener,
150
- _b);
151
- return entity;
152
- });
153
- }
154
- ;
155
114
  var attribsProxy = {
156
115
  get: function (element, key) {
157
116
  return element.getAttribute(key);
@@ -165,8 +124,6 @@ function getWrappedElement(element) {
165
124
  var _a;
166
125
  if (!elementWrapCache.has(element)) {
167
126
  var recursiveAppend_1 = function (c) {
168
- if (c === undefined)
169
- debugger;
170
127
  if (c === null)
171
128
  return;
172
129
  if (Array.isArray(c)) {
@@ -244,3 +201,49 @@ function getWrappedElement(element) {
244
201
  function isJelEntity(content) {
245
202
  return typeof content == "object" && !!content && componentDataSymbol in content;
246
203
  }
204
+ function definePart(defaultOptions, init) {
205
+ return (function (spec) {
206
+ var _a, _b;
207
+ var fullSpec = __assign(__assign({}, defaultOptions), spec);
208
+ var eventHandlers = {};
209
+ var addEventListener = function (eventId, fn) {
210
+ if (!eventHandlers[eventId])
211
+ eventHandlers[eventId] = [];
212
+ eventHandlers[eventId].push(fn);
213
+ };
214
+ if (fullSpec.on)
215
+ Object.entries(fullSpec.on).forEach(function (_a) {
216
+ var eventId = _a[0], handler = _a[1];
217
+ addEventListener(eventId, handler);
218
+ });
219
+ var entity;
220
+ var content = [];
221
+ var append = function (c) {
222
+ if (entity)
223
+ throw new Error("Component root content can only be added during initialisation");
224
+ content.push(c);
225
+ };
226
+ var trigger = function (eventId, data) {
227
+ var _a;
228
+ (_a = eventHandlers[eventId]) === null || _a === void 0 ? void 0 : _a.forEach(function (fn) { return fn.call(entity, data); });
229
+ };
230
+ var api = init(fullSpec, append, trigger);
231
+ entity = api ? Object.create(api, (_a = {},
232
+ _a[componentDataSymbol] = {
233
+ value: {
234
+ dom: content
235
+ }
236
+ },
237
+ _a.on = {
238
+ value: addEventListener,
239
+ },
240
+ _a)) : (_b = {},
241
+ _b[componentDataSymbol] = {
242
+ dom: content,
243
+ },
244
+ _b.on = addEventListener,
245
+ _b);
246
+ return entity;
247
+ });
248
+ }
249
+ ;
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "@xtia/jel",
3
- "version": "0.1.2",
3
+ "version": "0.2.0",
4
4
  "repository": {
5
5
  "url": "https://github.com/tiadrop/jel-ts",
6
6
  "type": "github"
7
7
  },
8
- "description": "Lightweight DOM wrapper",
8
+ "description": "Lightweight DOM manipulation and componentisation",
9
9
  "main": "index.js",
10
10
  "keywords": [
11
11
  "dom"