content-engine-lib 1.0.1 → 1.0.4

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/dist/index.d.ts CHANGED
@@ -1,4 +1,28 @@
1
1
  import { editor_p_TYP, hydrator_p_TYP, renderer_p_TYP } from './u/types';
2
+ declare const call_0: <K extends "msg">(event: K, ...args: Parameters<{
3
+ msg: (payload: {
4
+ type: string;
5
+ _p?: any;
6
+ _$p?: any;
7
+ custom?: object;
8
+ where?: {
9
+ key: string;
10
+ value: string;
11
+ };
12
+ }) => Promise<void>;
13
+ }[K]>) => Promise<void>;
14
+ declare const listen_1: <K extends "msg">(event: K, fn: {
15
+ msg: (payload: {
16
+ type: string;
17
+ _p?: any;
18
+ _$p?: any;
19
+ custom?: object;
20
+ where?: {
21
+ key: string;
22
+ value: string;
23
+ };
24
+ }) => Promise<void>;
25
+ }[K]) => () => void;
2
26
  declare const renderer: (_p: renderer_p_TYP) => Promise<{
3
27
  set: (_$p: {
4
28
  data: {
@@ -32,4 +56,4 @@ declare const editor: (_p: editor_p_TYP) => Promise<{
32
56
  style: string;
33
57
  }>;
34
58
  }>;
35
- export { renderer as ce_renderer, hydrator as ce_hydrator, editor as ce_editor };
59
+ export { renderer as ce_renderer, hydrator as ce_hydrator, editor as ce_editor, call_0 as ce_call, listen_1 as ce_listen, };
package/dist/index.es.js CHANGED
@@ -1,9 +1,9 @@
1
- const h = () => ({
1
+ const g = () => ({
2
2
  set: () => "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function(t) {
3
- const a = Math.random() * 16 | 0;
4
- return (t == "x" ? a : a & 3 | 8).toString(16);
3
+ const e = Math.random() * 16 | 0;
4
+ return (t == "x" ? e : e & 3 | 8).toString(16);
5
5
  })
6
- }), f = async () => ({
6
+ }), h = async () => ({
7
7
  f: {
8
8
  name: (t) => `${t.name}${t.id}`
9
9
  }
@@ -13,106 +13,136 @@ const h = () => ({
13
13
  try {
14
14
  if (!t.el_id)
15
15
  throw new Error("[el_id] is required");
16
- const a = t.name, n = document.getElementById(t.el_id);
17
- if (!n)
16
+ const e = t.name, r = document.getElementById(t.el_id);
17
+ if (!r)
18
18
  throw new Error("[el_id] is invalid");
19
- ((_) => {
20
- const r = (s) => {
19
+ ((a) => {
20
+ const n = (o) => {
21
21
  ((i) => {
22
- const l = i.getAttribute("data-ce");
23
- if (!l)
22
+ const s = i.getAttribute("data-ce");
23
+ if (!s)
24
24
  return;
25
- const o = JSON.parse(l).filter((e) => e?.k.startsWith("t-"));
26
- if (o.length != 0)
27
- for (const e of o) {
28
- const y = e.k, m = e.v.split(" ");
29
- if (y == `t-${a}-class`)
30
- for (const u of m)
31
- i.classList.add(u);
25
+ const c = JSON.parse(s).filter((l) => l?.k.startsWith("t-"));
26
+ if (c.length != 0)
27
+ for (const l of c) {
28
+ const m = l.k, f = l.v.split(" ");
29
+ if (m == `t-${e}-class`)
30
+ for (const b of f)
31
+ i.classList.add(b);
32
32
  else
33
- for (const u of m)
34
- i.classList.remove(u);
33
+ for (const b of f)
34
+ i.classList.remove(b);
35
35
  }
36
- })(s);
36
+ })(o);
37
37
  };
38
- for (const s of _.getElementsByTagName("*"))
39
- r(s);
40
- r(_);
41
- })(n);
42
- } catch (a) {
43
- const n = `err: [theme] ${a}`;
44
- throw console.log(n), n;
38
+ for (const o of a.getElementsByTagName("*"))
39
+ n(o);
40
+ n(a);
41
+ })(r);
42
+ } catch (e) {
43
+ const r = `err: [theme] ${e}`;
44
+ throw console.log(r), r;
45
45
  }
46
46
  }
47
47
  });
48
- function x(t, a = 1e3) {
49
- let n = {
48
+ function p(t, e = 1e3) {
49
+ let r = {
50
50
  cnt: 0
51
51
  };
52
- return new Promise((_) => {
53
- const r = () => {
54
- console.log(`[setInterval] is running.. [count=${n.cnt}]`);
52
+ return new Promise((a) => {
53
+ const n = () => {
54
+ console.log(`[setInterval] is running.. [count=${r.cnt}]`);
55
55
  try {
56
- t() && (clearInterval(s), _());
56
+ t() && (clearInterval(o), a());
57
57
  } catch {
58
- console.log(`warn: [wait_until] ignoring the exception in setInterval and will check again after [interval=${a}]`);
58
+ console.log(`warn: [wait_until] ignoring the exception in setInterval and will check again after [interval=${e}]`);
59
59
  }
60
- n.cnt += 1;
61
- }, s = setInterval(() => {
62
- r();
63
- }, a);
64
- r();
60
+ r.cnt += 1;
61
+ }, o = setInterval(() => {
62
+ n();
63
+ }, e);
64
+ n();
65
65
  });
66
66
  }
67
+ let z = class {
68
+ listeners = {};
69
+ on = (e, r) => ((this.listeners[e] ||= []).push(r), () => this.off(e, r));
70
+ off = (e, r) => {
71
+ this.listeners[e] = this.listeners[e]?.filter((a) => a !== r);
72
+ };
73
+ /** Sequential execution (await each listener) */
74
+ emit = async (e, ...r) => {
75
+ for (const a of this.listeners[e] ?? [])
76
+ await a(...r);
77
+ };
78
+ /** Parallel execution (await all listeners) */
79
+ emitParallel = async (e, ...r) => {
80
+ await Promise.all(
81
+ (this.listeners[e] ?? []).map((a) => a(...r))
82
+ );
83
+ };
84
+ //Error-safe emit
85
+ emitSafe = async (e, ...r) => {
86
+ for (const a of this.listeners[e] ?? [])
87
+ try {
88
+ await a(...r);
89
+ } catch (n) {
90
+ this.listeners.error?.forEach(
91
+ (o) => o(n)
92
+ );
93
+ }
94
+ };
95
+ };
96
+ const u = () => new z();
67
97
  console.log("content_engine_lib");
68
- let c = {
98
+ let y = {
69
99
  lib: {
70
100
  inbuilt_lib: [],
71
101
  // <any>[], // [`text`,`table`,`editor`]
72
102
  l: {},
73
103
  set: async (t) => {
74
- const a = t?.lib || [];
75
- for (const [n, _] of a.entries()) {
76
- const r = _, s = `${r.name}:${t.run_from}`, i = `${t.run_from}_src`;
77
- let l = r[i];
104
+ const e = t?.lib || [];
105
+ for (const [r, a] of e.entries()) {
106
+ const n = a, o = `${n.name}:${t.run_from}`, i = `${t.run_from}_src`;
107
+ let s = n[i];
78
108
  const d = `${t.run_from}_src`;
79
- let o = t?.lazy_lib?.[d] || null;
80
- if (o && (o = o.replace("{*}", `${r.name}`)), console.log(`_lazy_src: ${o}`), console.log(`_src: ${l}`), c.lib.l.hasOwnProperty(`${s}`) == !1) {
81
- if (/^[a-zA-Z0-9]/.test(l) && l.includes("/") == !1 && c.lib.inbuilt_lib.indexOf(`${r.name}`) === -1)
82
- if (o)
83
- l = o;
109
+ let c = t?.lazy_lib?.[d] || null;
110
+ if (c && (c = c.replace("{*}", `${n.name}`)), console.log(`_lazy_src: ${c}`), console.log(`_src: ${s}`), y.lib.l.hasOwnProperty(`${o}`) == !1) {
111
+ if (/^[a-zA-Z0-9]/.test(s) && s.includes("/") == !1 && y.lib.inbuilt_lib.indexOf(`${n.name}`) === -1)
112
+ if (c)
113
+ s = c;
84
114
  else
85
- throw `[lib-name=${r.name},lib-src=${l}] not allowed or available in in-build mode. Need to use lazy-lib config.`;
86
- if (l.startsWith("./") || l.startsWith("../")) {
87
- const e = await import(
115
+ throw `[lib-name=${n.name},lib-src=${s}] not allowed or available in in-build mode. Need to use lazy-lib config.`;
116
+ if (s.startsWith("./") || s.startsWith("../")) {
117
+ const l = await import(
88
118
  /* @vite-ignore */
89
119
  /* webpackIgnore: true */
90
- `${l}`
120
+ `${s}`
91
121
  );
92
- c.lib.l[`${s}`] = {
93
- lib: e,
94
- src: l
122
+ y.lib.l[`${o}`] = {
123
+ lib: l,
124
+ src: s
95
125
  };
96
126
  }
97
- if (l.startsWith("http://") || l.startsWith("https://")) {
98
- const e = await import(
127
+ if (s.startsWith("http://") || s.startsWith("https://")) {
128
+ const l = await import(
99
129
  /* @vite-ignore */
100
130
  /* webpackIgnore: true */
101
- `${l}`
131
+ `${s}`
102
132
  );
103
- c.lib.l[`${s}`] = {
104
- lib: e,
105
- src: l
133
+ y.lib.l[`${o}`] = {
134
+ lib: l,
135
+ src: s
106
136
  };
107
137
  }
108
138
  }
109
139
  }
110
- console.log(await c.lib.get_all({}));
140
+ console.log(await y.lib.get_all({}));
111
141
  },
112
142
  get: async (t) => {
113
- let a = null;
114
- const n = `${t.name}:${t.run_from}`;
115
- return c.lib.l.hasOwnProperty(`${n}`) == !1 && await c.lib.set({
143
+ let e = null;
144
+ const r = `${t.name}:${t.run_from}`;
145
+ return y.lib.l.hasOwnProperty(`${r}`) == !1 && await y.lib.set({
116
146
  lib: [
117
147
  {
118
148
  renderer_src: t.name,
@@ -123,100 +153,123 @@ let c = {
123
153
  ],
124
154
  run_from: t.run_from,
125
155
  lazy_lib: t.lazy_lib
126
- }), a = c.lib.l[`${n}`], a;
156
+ }), e = y.lib.l[`${r}`], e;
127
157
  },
128
- get_all: async (t) => c.lib.l
158
+ get_all: async (t) => y.lib.l
129
159
  },
130
160
  path: {
131
161
  set: (t) => {
132
- let a = "", n = "";
133
- const _ = t.src.split("/");
134
- if (t.src.indexOf("://localhost") !== -1 || t.src.indexOf("://127.0.0.1") !== -1 || (n = "/dist"), _.indexOf(t.type) !== -1)
135
- for (const [r, s] of _.entries()) {
136
- let i = r == 0 ? "" : "/";
137
- if (a += `${i}${s}`, s == t.type)
138
- return `${a}${n}${t.name}`;
162
+ let e = "", r = "";
163
+ const a = t.src.split("/");
164
+ if (t.src.indexOf("://localhost") !== -1 || t.src.indexOf("://127.0.0.1") !== -1 || (r = "/dist"), a.indexOf(t.type) !== -1)
165
+ for (const [n, o] of a.entries()) {
166
+ let i = n == 0 ? "" : "/";
167
+ if (e += `${i}${o}`, o == t.type)
168
+ return `${e}${r}${t.name}`;
139
169
  }
140
170
  else
141
- for (const [r, s] of _.entries()) {
142
- let i = r == 0 ? "" : "/";
143
- if (a += `${i}${s}`, s == "src")
144
- return `${a}${n}${t.name}`;
171
+ for (const [n, o] of a.entries()) {
172
+ let i = n == 0 ? "" : "/";
173
+ if (e += `${i}${o}`, o == "src")
174
+ return `${e}${r}${t.name}`;
145
175
  }
146
- return `${a}${n}${t.name}`;
176
+ return `${e}${r}${t.name}`;
147
177
  }
148
178
  }
149
179
  };
150
- const $ = async (t) => {
151
- const a = await f();
152
- return await c.lib.set({ lib: t.lib, run_from: "renderer", lazy_lib: t.lazy_lib }), {
153
- set: async (n, _) => {
180
+ const x = u(), $ = u(), k = x.on, W = x.emit, E = $.emit, C = $.on, O = async (t) => {
181
+ const e = await h();
182
+ return await y.lib.set({ lib: t.lib, run_from: "renderer", lazy_lib: t.lazy_lib }), {
183
+ set: async (r, a) => {
154
184
  console.log("--renderer [set]");
155
- let r = {
185
+ let n = {
156
186
  r: "",
157
187
  style: "",
158
188
  head: ""
159
189
  // `<test>head-1</test>`
160
190
  };
161
191
  return await (async () => {
162
- for (const i of n.data?.value?.l || n.data.l) {
163
- const l = await await c.lib.get({ name: i.type, run_from: "renderer", lazy_lib: t.lazy_lib }), o = await (await l.lib.index({
192
+ for (const i of r.data?.value?.l || r.data.l) {
193
+ const s = await await y.lib.get({ name: i.type, run_from: "renderer", lazy_lib: t.lazy_lib }), c = await (await s.lib.index({
164
194
  f: {
165
- name: (e) => a.f.name({ id: i.id, name: e }),
166
- get_lib: async (e) => await await c.lib.get({ name: e.name, run_from: e.run_from, lazy_lib: t.lazy_lib }),
167
- set_theme: async (e) => await (await w()).set(e),
168
- path: (e) => c.path.set({ src: l.src, type: i.type, name: e })
195
+ name: (l) => e.f.name({ id: i.id, name: l }),
196
+ get_lib: async (l) => await await y.lib.get({ name: l.name, run_from: l.run_from, lazy_lib: t.lazy_lib }),
197
+ set_theme: async (l) => await (await w()).set(l),
198
+ path: (l) => y.path.set({ src: s.src, type: i.type, name: l })
169
199
  }
170
200
  })).set({
171
201
  data: {
172
202
  curr: i
173
203
  }
174
- }, _);
175
- r.r += o?.r || "", r.style += o?.style || "", r.head += o?.head || "";
204
+ }, a);
205
+ n.r += c?.r || "", n.style += c?.style || "", n.head += c?.head || "";
176
206
  }
177
- })(), r;
207
+ })(), n;
178
208
  }
179
209
  };
180
- }, g = async (t) => {
181
- const a = await f();
182
- return await c.lib.set({ lib: t.lib, run_from: "hydrator", lazy_lib: t.lazy_lib }), {
183
- set: async (n, _) => {
210
+ }, I = async (t) => {
211
+ const e = await h();
212
+ return await y.lib.set({ lib: t.lib, run_from: "hydrator", lazy_lib: t.lazy_lib }), {
213
+ set: async (r, a) => {
184
214
  console.log("--hydrator [set]");
185
- let r = {
215
+ let n = {
186
216
  r: "",
187
217
  style: ""
188
218
  };
189
- const s = async () => {
190
- for (const i of n.data?.value?.l || n.data.l) {
191
- const l = await await c.lib.get({ name: i.type, run_from: "hydrator", lazy_lib: t.lazy_lib }), o = await (await l.lib.index({
219
+ const o = async () => {
220
+ for (const i of r.data?.value?.l || r.data.l) {
221
+ const s = await await y.lib.get({ name: i.type, run_from: "hydrator", lazy_lib: t.lazy_lib }), d = s.lib, c = u(), l = c.on, m = await (await d.index({
222
+ /**@my module can use it to set custom variables. */
223
+ my: {},
224
+ //NOTE: We cannot add or use any variable of this object, It's reserved for module.
192
225
  f: {
193
- name: (e) => a.f.name({ id: i.id, name: e }),
194
- get_lib: async (e) => await await c.lib.get({ name: e.name, run_from: e.run_from, lazy_lib: t.lazy_lib }),
195
- set_theme: async (e) => await (await w()).set(e),
196
- path: (e) => c.path.set({ src: l.src, type: i.type, name: e })
226
+ name: (_) => e.f.name({ id: i.id, name: _ }),
227
+ get_lib: async (_) => await await y.lib.get({ name: _.name, run_from: _.run_from, lazy_lib: t.lazy_lib }),
228
+ set_theme: async (_) => await (await w()).set(_),
229
+ path: (_) => y.path.set({ src: s.src, type: i.type, name: _ }),
230
+ //set..
231
+ call: E,
232
+ listen: l,
233
+ //set..
234
+ new_emitter: () => u()
197
235
  }
198
236
  })).set({
199
237
  data: {
200
238
  curr: i
201
239
  }
202
- }, _);
203
- r.style += o.style;
240
+ }, a);
241
+ k("msg", async (_) => {
242
+ try {
243
+ if (Object.keys(_.where || {}).length == 0) {
244
+ await c.emit("msg", _);
245
+ return;
246
+ }
247
+ } catch {
248
+ }
249
+ try {
250
+ if (i?.[_.where?.key || ""] == _.where?.value) {
251
+ await c.emit("msg", _);
252
+ return;
253
+ }
254
+ } catch {
255
+ }
256
+ }), n.style += m.style;
204
257
  }
205
258
  };
206
- return await x(
259
+ return await p(
207
260
  () => document.readyState === "complete" || typeof window < "u",
208
261
  50
209
- ), await s(), r;
262
+ ), await o(), n;
210
263
  }
211
264
  };
212
- }, z = async (t) => (await f(), await c.lib.set({ lib: t.lib, run_from: "editor", lazy_lib: t.lazy_lib }), {
213
- set: async (a, n) => {
265
+ }, S = async (t) => (await h(), await y.lib.set({ lib: t.lib, run_from: "editor", lazy_lib: t.lazy_lib }), {
266
+ set: async (e, r) => {
214
267
  console.log("--editor [set]");
215
- let _ = {
268
+ let a = {
216
269
  data: {
217
270
  l: [
218
271
  {
219
- id: h().set(),
272
+ id: g().set(),
220
273
  type: "editor",
221
274
  data: {
222
275
  l: []
@@ -224,38 +277,40 @@ const $ = async (t) => {
224
277
  }
225
278
  ]
226
279
  }
227
- }, r = {
280
+ }, n = {
228
281
  r: "",
229
282
  style: ""
230
283
  };
231
- const s = $, i = g, l = await s({
284
+ const o = O, i = I, s = await o({
232
285
  lib: t.lib,
233
286
  lazy_lib: t.lazy_lib
234
287
  }), d = await i({
235
288
  lib: t.lib,
236
289
  lazy_lib: t.lazy_lib
237
- }), o = await l.set({
238
- data: _.data
290
+ }), c = await s.set({
291
+ data: a.data
239
292
  });
240
293
  return (async () => {
241
- const e = await d.set({
242
- data: _.data
294
+ const l = await d.set({
295
+ data: a.data
243
296
  }, {
244
- change: (y) => {
245
- n?.change(y);
297
+ change: (m) => {
298
+ r?.change(m);
246
299
  }
247
300
  });
248
- ((y) => {
249
- const b = document.createElement("style");
250
- b.innerHTML = `${e.style}`, y.appendChild(b);
301
+ ((m) => {
302
+ const _ = document.createElement("style");
303
+ _.innerHTML = `${l.style}`, m.appendChild(_);
251
304
  })(document.head);
252
- })(), r.r = o.r, r.style = `
253
- ${o.style}
254
- `, r;
305
+ })(), n.r = c.r, n.style = `
306
+ ${c.style}
307
+ `, n;
255
308
  }
256
309
  });
257
310
  export {
258
- z as ce_editor,
259
- g as ce_hydrator,
260
- $ as ce_renderer
311
+ W as ce_call,
312
+ S as ce_editor,
313
+ I as ce_hydrator,
314
+ C as ce_listen,
315
+ O as ce_renderer
261
316
  };
package/dist/index.umd.js CHANGED
@@ -1,3 +1,3 @@
1
- (function(d,u){typeof exports=="object"&&typeof module<"u"?u(exports):typeof define=="function"&&define.amd?define(["exports"],u):(d=typeof globalThis<"u"?globalThis:d||self,u(d["content-engine-lib"]={}))})(this,(function(d){"use strict";const u=()=>({set:()=>"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,function(e){const a=Math.random()*16|0;return(e=="x"?a:a&3|8).toString(16)})}),b=async()=>({f:{name:e=>`${e.name}${e.id}`}}),w=async()=>({set:async e=>{console.log("--theme");try{if(!e.el_id)throw new Error("[el_id] is required");const a=e.name,r=document.getElementById(e.el_id);if(!r)throw new Error("[el_id] is invalid");(_=>{const n=s=>{(i=>{const l=i.getAttribute("data-ce");if(!l)return;const c=JSON.parse(l).filter(t=>t?.k.startsWith("t-"));if(c.length!=0)for(const t of c){const f=t.k,g=t.v.split(" ");if(f==`t-${a}-class`)for(const h of g)i.classList.add(h);else for(const h of g)i.classList.remove(h)}})(s)};for(const s of _.getElementsByTagName("*"))n(s);n(_)})(r)}catch(a){const r=`err: [theme] ${a}`;throw console.log(r),r}}});function p(e,a=1e3){let r={cnt:0};return new Promise(_=>{const n=()=>{console.log(`[setInterval] is running.. [count=${r.cnt}]`);try{e()&&(clearInterval(s),_())}catch{console.log(`warn: [wait_until] ignoring the exception in setInterval and will check again after [interval=${a}]`)}r.cnt+=1},s=setInterval(()=>{n()},a);n()})}console.log("content_engine_lib");let o={lib:{inbuilt_lib:[],l:{},set:async e=>{const a=e?.lib||[];for(const[r,_]of a.entries()){const n=_,s=`${n.name}:${e.run_from}`,i=`${e.run_from}_src`;let l=n[i];const y=`${e.run_from}_src`;let c=e?.lazy_lib?.[y]||null;if(c&&(c=c.replace("{*}",`${n.name}`)),console.log(`_lazy_src: ${c}`),console.log(`_src: ${l}`),o.lib.l.hasOwnProperty(`${s}`)==!1){if(/^[a-zA-Z0-9]/.test(l)&&l.includes("/")==!1&&o.lib.inbuilt_lib.indexOf(`${n.name}`)===-1)if(c)l=c;else throw`[lib-name=${n.name},lib-src=${l}] not allowed or available in in-build mode. Need to use lazy-lib config.`;if(l.startsWith("./")||l.startsWith("../")){const t=await import(`${l}`);o.lib.l[`${s}`]={lib:t,src:l}}if(l.startsWith("http://")||l.startsWith("https://")){const t=await import(`${l}`);o.lib.l[`${s}`]={lib:t,src:l}}}}console.log(await o.lib.get_all({}))},get:async e=>{let a=null;const r=`${e.name}:${e.run_from}`;return o.lib.l.hasOwnProperty(`${r}`)==!1&&await o.lib.set({lib:[{renderer_src:e.name,hydrator_src:e.name,editor_src:e.name,name:e.name}],run_from:e.run_from,lazy_lib:e.lazy_lib}),a=o.lib.l[`${r}`],a},get_all:async e=>o.lib.l},path:{set:e=>{let a="",r="";const _=e.src.split("/");if(e.src.indexOf("://localhost")!==-1||e.src.indexOf("://127.0.0.1")!==-1||(r="/dist"),_.indexOf(e.type)!==-1)for(const[n,s]of _.entries()){let i=n==0?"":"/";if(a+=`${i}${s}`,s==e.type)return`${a}${r}${e.name}`}else for(const[n,s]of _.entries()){let i=n==0?"":"/";if(a+=`${i}${s}`,s=="src")return`${a}${r}${e.name}`}return`${a}${r}${e.name}`}}};const $=async e=>{const a=await b();return await o.lib.set({lib:e.lib,run_from:"renderer",lazy_lib:e.lazy_lib}),{set:async(r,_)=>{console.log("--renderer [set]");let n={r:"",style:"",head:""};return await(async()=>{for(const i of r.data?.value?.l||r.data.l){const l=await await o.lib.get({name:i.type,run_from:"renderer",lazy_lib:e.lazy_lib}),c=await(await l.lib.index({f:{name:t=>a.f.name({id:i.id,name:t}),get_lib:async t=>await await o.lib.get({name:t.name,run_from:t.run_from,lazy_lib:e.lazy_lib}),set_theme:async t=>await(await w()).set(t),path:t=>o.path.set({src:l.src,type:i.type,name:t})}})).set({data:{curr:i}},_);n.r+=c?.r||"",n.style+=c?.style||"",n.head+=c?.head||""}})(),n}}},x=async e=>{const a=await b();return await o.lib.set({lib:e.lib,run_from:"hydrator",lazy_lib:e.lazy_lib}),{set:async(r,_)=>{console.log("--hydrator [set]");let n={r:"",style:""};const s=async()=>{for(const i of r.data?.value?.l||r.data.l){const l=await await o.lib.get({name:i.type,run_from:"hydrator",lazy_lib:e.lazy_lib}),c=await(await l.lib.index({f:{name:t=>a.f.name({id:i.id,name:t}),get_lib:async t=>await await o.lib.get({name:t.name,run_from:t.run_from,lazy_lib:e.lazy_lib}),set_theme:async t=>await(await w()).set(t),path:t=>o.path.set({src:l.src,type:i.type,name:t})}})).set({data:{curr:i}},_);n.style+=c.style}};return await p(()=>document.readyState==="complete"||typeof window<"u",50),await s(),n}}},z=async e=>(await b(),await o.lib.set({lib:e.lib,run_from:"editor",lazy_lib:e.lazy_lib}),{set:async(a,r)=>{console.log("--editor [set]");let _={data:{l:[{id:u().set(),type:"editor",data:{l:[]}}]}},n={r:"",style:""};const s=$,i=x,l=await s({lib:e.lib,lazy_lib:e.lazy_lib}),y=await i({lib:e.lib,lazy_lib:e.lazy_lib}),c=await l.set({data:_.data});return(async()=>{const t=await y.set({data:_.data},{change:f=>{r?.change(f)}});(f=>{const m=document.createElement("style");m.innerHTML=`${t.style}`,f.appendChild(m)})(document.head)})(),n.r=c.r,n.style=`
1
+ (function(y,m){typeof exports=="object"&&typeof module<"u"?m(exports):typeof define=="function"&&define.amd?define(["exports"],m):(y=typeof globalThis<"u"?globalThis:y||self,m(y["content-engine-lib"]={}))})(this,(function(y){"use strict";const m=()=>({set:()=>"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,function(e){const t=Math.random()*16|0;return(e=="x"?t:t&3|8).toString(16)})}),w=async()=>({f:{name:e=>`${e.name}${e.id}`}}),g=async()=>({set:async e=>{console.log("--theme");try{if(!e.el_id)throw new Error("[el_id] is required");const t=e.name,n=document.getElementById(e.el_id);if(!n)throw new Error("[el_id] is invalid");(a=>{const r=o=>{(s=>{const i=s.getAttribute("data-ce");if(!i)return;const c=JSON.parse(i).filter(l=>l?.k.startsWith("t-"));if(c.length!=0)for(const l of c){const u=l.k,h=l.v.split(" ");if(u==`t-${t}-class`)for(const x of h)s.classList.add(x);else for(const x of h)s.classList.remove(x)}})(o)};for(const o of a.getElementsByTagName("*"))r(o);r(a)})(n)}catch(t){const n=`err: [theme] ${t}`;throw console.log(n),n}}});function O(e,t=1e3){let n={cnt:0};return new Promise(a=>{const r=()=>{console.log(`[setInterval] is running.. [count=${n.cnt}]`);try{e()&&(clearInterval(o),a())}catch{console.log(`warn: [wait_until] ignoring the exception in setInterval and will check again after [interval=${t}]`)}n.cnt+=1},o=setInterval(()=>{r()},t);r()})}let E=class{listeners={};on=(t,n)=>((this.listeners[t]||=[]).push(n),()=>this.off(t,n));off=(t,n)=>{this.listeners[t]=this.listeners[t]?.filter(a=>a!==n)};emit=async(t,...n)=>{for(const a of this.listeners[t]??[])await a(...n)};emitParallel=async(t,...n)=>{await Promise.all((this.listeners[t]??[]).map(a=>a(...n)))};emitSafe=async(t,...n)=>{for(const a of this.listeners[t]??[])try{await a(...n)}catch(r){this.listeners.error?.forEach(o=>o(r))}}};const b=()=>new E;console.log("content_engine_lib");let _={lib:{inbuilt_lib:[],l:{},set:async e=>{const t=e?.lib||[];for(const[n,a]of t.entries()){const r=a,o=`${r.name}:${e.run_from}`,s=`${e.run_from}_src`;let i=r[s];const f=`${e.run_from}_src`;let c=e?.lazy_lib?.[f]||null;if(c&&(c=c.replace("{*}",`${r.name}`)),console.log(`_lazy_src: ${c}`),console.log(`_src: ${i}`),_.lib.l.hasOwnProperty(`${o}`)==!1){if(/^[a-zA-Z0-9]/.test(i)&&i.includes("/")==!1&&_.lib.inbuilt_lib.indexOf(`${r.name}`)===-1)if(c)i=c;else throw`[lib-name=${r.name},lib-src=${i}] not allowed or available in in-build mode. Need to use lazy-lib config.`;if(i.startsWith("./")||i.startsWith("../")){const l=await import(`${i}`);_.lib.l[`${o}`]={lib:l,src:i}}if(i.startsWith("http://")||i.startsWith("https://")){const l=await import(`${i}`);_.lib.l[`${o}`]={lib:l,src:i}}}}console.log(await _.lib.get_all({}))},get:async e=>{let t=null;const n=`${e.name}:${e.run_from}`;return _.lib.l.hasOwnProperty(`${n}`)==!1&&await _.lib.set({lib:[{renderer_src:e.name,hydrator_src:e.name,editor_src:e.name,name:e.name}],run_from:e.run_from,lazy_lib:e.lazy_lib}),t=_.lib.l[`${n}`],t},get_all:async e=>_.lib.l},path:{set:e=>{let t="",n="";const a=e.src.split("/");if(e.src.indexOf("://localhost")!==-1||e.src.indexOf("://127.0.0.1")!==-1||(n="/dist"),a.indexOf(e.type)!==-1)for(const[r,o]of a.entries()){let s=r==0?"":"/";if(t+=`${s}${o}`,o==e.type)return`${t}${n}${e.name}`}else for(const[r,o]of a.entries()){let s=r==0?"":"/";if(t+=`${s}${o}`,o=="src")return`${t}${n}${e.name}`}return`${t}${n}${e.name}`}}};const $=b(),p=b(),P=$.on,S=$.emit,T=p.emit,I=p.on,z=async e=>{const t=await w();return await _.lib.set({lib:e.lib,run_from:"renderer",lazy_lib:e.lazy_lib}),{set:async(n,a)=>{console.log("--renderer [set]");let r={r:"",style:"",head:""};return await(async()=>{for(const s of n.data?.value?.l||n.data.l){const i=await await _.lib.get({name:s.type,run_from:"renderer",lazy_lib:e.lazy_lib}),c=await(await i.lib.index({f:{name:l=>t.f.name({id:s.id,name:l}),get_lib:async l=>await await _.lib.get({name:l.name,run_from:l.run_from,lazy_lib:e.lazy_lib}),set_theme:async l=>await(await g()).set(l),path:l=>_.path.set({src:i.src,type:s.type,name:l})}})).set({data:{curr:s}},a);r.r+=c?.r||"",r.style+=c?.style||"",r.head+=c?.head||""}})(),r}}},k=async e=>{const t=await w();return await _.lib.set({lib:e.lib,run_from:"hydrator",lazy_lib:e.lazy_lib}),{set:async(n,a)=>{console.log("--hydrator [set]");let r={r:"",style:""};const o=async()=>{for(const s of n.data?.value?.l||n.data.l){const i=await await _.lib.get({name:s.type,run_from:"hydrator",lazy_lib:e.lazy_lib}),f=i.lib,c=b(),l=c.on,u=await(await f.index({my:{},f:{name:d=>t.f.name({id:s.id,name:d}),get_lib:async d=>await await _.lib.get({name:d.name,run_from:d.run_from,lazy_lib:e.lazy_lib}),set_theme:async d=>await(await g()).set(d),path:d=>_.path.set({src:i.src,type:s.type,name:d}),call:T,listen:l,new_emitter:()=>b()}})).set({data:{curr:s}},a);P("msg",async d=>{try{if(Object.keys(d.where||{}).length==0){await c.emit("msg",d);return}}catch{}try{if(s?.[d.where?.key||""]==d.where?.value){await c.emit("msg",d);return}}catch{}}),r.style+=u.style}};return await O(()=>document.readyState==="complete"||typeof window<"u",50),await o(),r}}},W=async e=>(await w(),await _.lib.set({lib:e.lib,run_from:"editor",lazy_lib:e.lazy_lib}),{set:async(t,n)=>{console.log("--editor [set]");let a={data:{l:[{id:m().set(),type:"editor",data:{l:[]}}]}},r={r:"",style:""};const o=z,s=k,i=await o({lib:e.lib,lazy_lib:e.lazy_lib}),f=await s({lib:e.lib,lazy_lib:e.lazy_lib}),c=await i.set({data:a.data});return(async()=>{const l=await f.set({data:a.data},{change:u=>{n?.change(u)}});(u=>{const d=document.createElement("style");d.innerHTML=`${l.style}`,u.appendChild(d)})(document.head)})(),r.r=c.r,r.style=`
2
2
  ${c.style}
3
- `,n}});d.ce_editor=z,d.ce_hydrator=x,d.ce_renderer=$,Object.defineProperty(d,Symbol.toStringTag,{value:"Module"})}));
3
+ `,r}});y.ce_call=S,y.ce_editor=W,y.ce_hydrator=k,y.ce_listen=I,y.ce_renderer=z,Object.defineProperty(y,Symbol.toStringTag,{value:"Module"})}));
@@ -0,0 +1,12 @@
1
+ type AsyncFn = (...args: any[]) => Promise<any> | any;
2
+ declare class index<T extends Record<string, AsyncFn>> {
3
+ private listeners;
4
+ on: <K extends keyof T>(event: K, fn: T[K]) => () => void;
5
+ off: <K extends keyof T>(event: K, fn: T[K]) => void;
6
+ /** Sequential execution (await each listener) */
7
+ emit: <K extends keyof T>(event: K, ...args: Parameters<T[K]>) => Promise<void>;
8
+ /** Parallel execution (await all listeners) */
9
+ emitParallel: <K extends keyof T>(event: K, ...args: Parameters<T[K]>) => Promise<void>;
10
+ emitSafe: <K extends keyof T>(event: K, ...args: Parameters<T[K]>) => Promise<void>;
11
+ }
12
+ export { index as Emitter };
@@ -0,0 +1,21 @@
1
+ import { Emitter } from '../emitter';
2
+ declare const index: () => Emitter<{
3
+ msg: (payload: {
4
+ /**eg=> `on:change`, `on:add` etc. */
5
+ type: string;
6
+ /**can pass existing `_p` variable. */
7
+ _p?: any;
8
+ /**can pass existing `_$p` variable. */
9
+ _$p?: any;
10
+ /**can be used to pass custom data. */
11
+ custom?: object;
12
+ /**can create condition for listeners */
13
+ where?: {
14
+ /**eg=> @id (Module-Id), @type (Module-Type) */
15
+ key: string;
16
+ /**eg=> xx776-6564-6547 (Module-Id), text (Module-Type) */
17
+ value: string;
18
+ };
19
+ }) => Promise<void>;
20
+ }>;
21
+ export { index as new_emitter };
@@ -20,4 +20,4 @@ type renderer_p_TYP = {
20
20
  };
21
21
  type hydrator_p_TYP = renderer_p_TYP;
22
22
  type editor_p_TYP = renderer_p_TYP;
23
- export type { renderer_p_TYP, hydrator_p_TYP, editor_p_TYP };
23
+ export type { renderer_p_TYP, hydrator_p_TYP, editor_p_TYP, };
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "content-engine-lib",
3
- "version": "1.0.1",
3
+ "version": "1.0.4",
4
4
  "description": "A lightweight library for content engine functionality",
5
- "keywords": ["content", "engine", "library"],
5
+ "keywords": ["content", "engine", "library", "content-engine"],
6
6
  "author": "Coreutility",
7
7
  "license": "MIT",
8
8
  "type": "module",