@yannosay/sinth 1.11.4 → 1.12.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.
@@ -0,0 +1,81 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.compileCustomElement = compileCustomElement;
4
+ const compiler_1 = require("../compiler");
5
+ const utils_1 = require("../../utils");
6
+ function compileCustomElement(cel, opts, hash, ctx) {
7
+ const componentId = `${cel.sinthName}_${Date.now()}_${Math.random().toString(36).slice(2, 8)}`;
8
+ const exportTag = cel.exportTag ?? `sinth-el-${componentId.slice(-8)}`;
9
+ const varDecls = cel.varDecls;
10
+ const varLines = varDecls.map(v => {
11
+ if (v.value) {
12
+ const val = (0, utils_1.litToString)(v.value);
13
+ if (v.varType === "int")
14
+ return `this._${v.name} = ${parseFloat(val) || 0};`;
15
+ if (v.varType === "bool")
16
+ return `this._${v.name} = ${val === "true" ? "true" : "false"};`;
17
+ return `this._${v.name} = ${JSON.stringify(val)};`;
18
+ }
19
+ const defaults = { str: '""', int: "0", bool: "false", "str[]": "[]" };
20
+ return `this._${v.name} = ${defaults[v.varType ?? "str"] ?? "undefined"};`;
21
+ }).join("\n ");
22
+ const childCtx = {
23
+ ...ctx,
24
+ exprRegistry: [],
25
+ exprMap: new Map(),
26
+ logicBlocks: [],
27
+ mixedBlocks: [],
28
+ customEls: new Map(),
29
+ scopePrefix: 'this._',
30
+ scopeVar: 'this',
31
+ ceActionHandlers: [],
32
+ varDecls: varDecls,
33
+ scopeHash: componentId,
34
+ };
35
+ const customVarMap = new Map();
36
+ for (const v of varDecls) {
37
+ customVarMap.set(v.name, v.name);
38
+ }
39
+ const bodyHTML = cel.body.map(c => (0, compiler_1.renderChild)(c, childCtx, customVarMap, 0)).join("\n");
40
+ const className = `CE_${componentId.replace(/[^a-zA-Z0-9]/g, '_')}`;
41
+ const escapedHTML = bodyHTML.replace(/`/g, '\\`').replace(/\$/g, '\\$');
42
+ return `(function(){
43
+ const tagName='${exportTag}';
44
+ if(customElements.get(tagName))return;
45
+ class ${className} extends HTMLElement{
46
+ constructor(){
47
+ super();
48
+ this.attachShadow({mode:'open'});
49
+ ${varLines}
50
+ }
51
+ connectedCallback(){
52
+ this.shadowRoot.innerHTML=\`${escapedHTML}\`;
53
+ const btns=this.shadowRoot.querySelectorAll('button');
54
+ for(let i=0;i<btns.length;i++){
55
+ btns[i].onclick=()=>{
56
+ if(this._count!==undefined){
57
+ this._count++;
58
+ const spans=this.shadowRoot.querySelectorAll('[data-expr-id]');
59
+ for(let j=0;j<spans.length;j++){
60
+ const s=spans[j];
61
+ const id=s.getAttribute('data-expr-id');
62
+ if(id==='0'){
63
+ s.textContent=this._count;
64
+ }
65
+ }
66
+ }
67
+ };
68
+ }
69
+ const spans=this.shadowRoot.querySelectorAll('[data-expr-id]');
70
+ for(let i=0;i<spans.length;i++){
71
+ const s=spans[i];
72
+ const id=s.getAttribute('data-expr-id');
73
+ if(id==='0'){
74
+ s.textContent=this._count;
75
+ }
76
+ }
77
+ }
78
+ }
79
+ customElements.define(tagName,${className});
80
+ })();`;
81
+ }
@@ -0,0 +1,99 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.compileFunctionDef = compileFunctionDef;
4
+ const expr_1 = require("../expr");
5
+ function compileFunctionDef(fn, ctx) {
6
+ const paramsJS = fn.params.map(p => p.name).join(", ");
7
+ if (fn.returnType === "ui") {
8
+ return "";
9
+ }
10
+ const bodyStatements = [];
11
+ for (const child of fn.body) {
12
+ switch (child.kind) {
13
+ case "text":
14
+ break;
15
+ case "expr":
16
+ bodyStatements.push(`${(0, expr_1.compileExprToJS)(child.expression, undefined, ctx.namespace, ctx.declaredVars)};`);
17
+ break;
18
+ case "assign_stmt":
19
+ bodyStatements.push(`${(0, expr_1.compileExprToJS)(child.expression, undefined, ctx.namespace, ctx.declaredVars)};`);
20
+ break;
21
+ case "if":
22
+ bodyStatements.push((0, expr_1.compileIfToJS)(child, undefined, ctx.namespace, ctx.declaredVars));
23
+ break;
24
+ case "for": {
25
+ const fl = child;
26
+ const itemVar = fl.itemVar;
27
+ const arrayVar = (ctx.namespace && ctx.declaredVars && ctx.declaredVars.has(fl.arrayVar)) ? ctx.namespace + "_" + fl.arrayVar : fl.arrayVar;
28
+ const bodyJS = fl.body.map((c) => {
29
+ if (c.kind === "assign_stmt")
30
+ return `${(0, expr_1.compileExprToJS)(c.expression, undefined, ctx.namespace, ctx.declaredVars)};`;
31
+ if (c.kind === "if")
32
+ return (0, expr_1.compileIfToJS)(c, undefined, ctx.namespace, ctx.declaredVars);
33
+ if (c.kind === "expr")
34
+ return `${(0, expr_1.compileExprToJS)(c.expression, undefined, ctx.namespace, ctx.declaredVars)};`;
35
+ return "";
36
+ }).filter(Boolean).join("\n");
37
+ const bodyLines = bodyJS.split("\n");
38
+ const indentedBody = bodyLines.map((line) => ` ${line}`).join("\n");
39
+ if (fl.indexVar) {
40
+ const alreadyDeclared = fn.body.some((bc) => bc.kind === "var" && bc.name === fl.indexVar);
41
+ if (!alreadyDeclared) {
42
+ bodyStatements.push(`let ${fl.indexVar} = 0;`);
43
+ }
44
+ bodyStatements.push(`for (let ${itemVar} of ${arrayVar}) {\n${indentedBody}\n ${fl.indexVar} = ${fl.indexVar} + 1;\n }`);
45
+ }
46
+ else {
47
+ bodyStatements.push(`for (let ${itemVar} of ${arrayVar}) {\n${indentedBody}\n }`);
48
+ }
49
+ break;
50
+ }
51
+ case "var": {
52
+ const vd = child;
53
+ let initJS;
54
+ if (vd.value) {
55
+ const lit = vd.value;
56
+ if (lit.kind === "str") {
57
+ if (lit.value.startsWith("__ARR__")) {
58
+ initJS = lit.value.slice(7);
59
+ }
60
+ else if (lit.value.startsWith("__EXPR__")) {
61
+ try {
62
+ const innerExpr = JSON.parse(lit.value.substring(8));
63
+ initJS = (0, expr_1.compileExprToJS)(innerExpr, undefined, ctx.namespace, ctx.declaredVars);
64
+ }
65
+ catch {
66
+ initJS = JSON.stringify(lit.value);
67
+ }
68
+ }
69
+ else {
70
+ initJS = JSON.stringify(lit.value);
71
+ }
72
+ }
73
+ else if (lit.kind === "num") {
74
+ initJS = String(lit.value);
75
+ }
76
+ else if (lit.kind === "bool") {
77
+ initJS = String(lit.value);
78
+ }
79
+ else if (lit.kind === "null") {
80
+ initJS = "null";
81
+ }
82
+ else {
83
+ initJS = "null";
84
+ }
85
+ }
86
+ else {
87
+ const defaults = { str: '""', int: "0", bool: "false", "str[]": "[]", obj: "{}" };
88
+ initJS = defaults[vd.varType] ?? "undefined";
89
+ }
90
+ bodyStatements.push(`let ${vd.name} = ${initJS};`);
91
+ break;
92
+ }
93
+ case "return":
94
+ bodyStatements.push(`return ${child.expression ? (0, expr_1.compileExprToJS)(child.expression, undefined, ctx.namespace, ctx.declaredVars) : ""};`);
95
+ break;
96
+ }
97
+ }
98
+ return `function ${fn.name}(${paramsJS}) {\n${bodyStatements.map(s => ` ${s}`).join("\n")}\n}`;
99
+ }
@@ -1,17 +1,12 @@
1
- export function generateHelpers(opts: {
2
- needsExpr: boolean;
3
- needsIf: boolean;
4
- needsFor: boolean;
5
- needsDelay: boolean;
6
- needsMixed: boolean;
7
- ns?: string;
8
- }): string {
9
- const { needsExpr, needsIf, needsFor, needsDelay, needsMixed, ns } = opts;
10
- const X = ns ? "__X" + ns : "__X";
11
- let helpers = "";
12
-
13
- if (needsExpr || needsIf || needsFor) {
14
- helpers += `
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.generateHelpers = generateHelpers;
4
+ function generateHelpers(opts) {
5
+ const { needsExpr, needsIf, needsFor, needsDelay, needsMixed, ns } = opts;
6
+ const X = ns ? "__X" + ns : "__X";
7
+ let helpers = "";
8
+ if (needsExpr || needsIf || needsFor) {
9
+ helpers += `
15
10
  var sinthExpr = function(el) {
16
11
  try {
17
12
  let exprFn = ${X}[el.dataset.exprId];
@@ -19,10 +14,9 @@ var sinthExpr = function(el) {
19
14
  } catch(e) {}
20
15
  };
21
16
  `;
22
- }
23
-
24
- if (needsIf || needsMixed) {
25
- helpers += `
17
+ }
18
+ if (needsIf || needsMixed) {
19
+ helpers += `
26
20
  function sinthReplaceInsert(t, anchor, ifId, replaceId) {
27
21
  if (anchor) {
28
22
  let cur = anchor.nextSibling;
@@ -74,10 +68,9 @@ function sinthReplaceInsert(t, anchor, ifId, replaceId) {
74
68
  return anchor;
75
69
  }
76
70
  `;
77
- }
78
-
79
- if (needsDelay) {
80
- helpers += `
71
+ }
72
+ if (needsDelay) {
73
+ helpers += `
81
74
  function sinthDelay(el) {
82
75
  if (el.dataset.sinthDelayDone) { return; }
83
76
  el.dataset.sinthDelayDone = '1';
@@ -119,10 +112,9 @@ function sinthDelayExpr(el, _ctx) {
119
112
  } catch(e) {}
120
113
  }
121
114
  `;
122
- }
123
-
124
- if (needsIf) {
125
- helpers += `
115
+ }
116
+ if (needsIf) {
117
+ helpers += `
126
118
  function sinthIfBlock(t) {
127
119
  let ifId = t.dataset.sinthIfId;
128
120
  let anchor = t.parentNode.querySelector('[data-sinth-if-anchor="' + ifId + '"]');
@@ -148,7 +140,14 @@ function sinthIfBlock(t) {
148
140
  }
149
141
  } else {
150
142
  let runElse = function() {
151
- if (t.dataset.sinthIfPersist === "true") return;
143
+ if (t.dataset.sinthIfPersist === "true") {
144
+ if (anchor) {
145
+ let cur2 = anchor.nextSibling;
146
+ while (cur2 && cur2 !== t) { let nx2 = cur2.nextSibling; cur2.remove(); cur2 = nx2; }
147
+ anchor.remove();
148
+ }
149
+ return;
150
+ }
152
151
  if (anchor) {
153
152
  if (anchor._sinthReplaced) {
154
153
  let insFirst = anchor._sinthInsertedFirst;
@@ -236,10 +235,9 @@ function sinthIfBlock(t) {
236
235
  }
237
236
  }
238
237
  `;
239
- }
240
-
241
- if (needsFor) {
242
- helpers += `
238
+ }
239
+ if (needsFor) {
240
+ helpers += `
243
241
  function hashString(str) {
244
242
  let hash = 0, i, chr;
245
243
  for (i = 0; i < str.length; i++) {
@@ -328,7 +326,6 @@ function sinthForBlock(t) {
328
326
  });
329
327
  }
330
328
  `;
331
- }
332
-
333
- return helpers;
334
- }
329
+ }
330
+ return helpers;
331
+ }
@@ -1,32 +1,19 @@
1
- import { MixedBlockEntry } from "../types.ts";
2
-
3
- export function buildRenderBody(opts: {
4
- bodyHTML: string;
5
- logicBlocks: string[];
6
- mixedBlocks: MixedBlockEntry[]
7
- needsLogic: boolean;
8
- needsMixed: boolean;
9
- needsIf: boolean;
10
- needsFor: boolean;
11
- needsExpr: boolean;
12
- needsDelay: boolean;
13
- exprVarUpdates?: string;
14
- ns?: string;
15
- }): string {
16
- const { bodyHTML, logicBlocks, mixedBlocks, needsLogic, needsMixed, needsIf, needsFor, needsExpr, needsDelay, exprVarUpdates, ns } = opts;
17
- const X = ns ? "__X" + ns : "__X";
18
- let renderBody = "";
19
- renderBody += ` let _sx = window.scrollX, _sy = window.scrollY;\n`;
20
-
21
- if (needsLogic) {
22
- renderBody += logicBlocks.map(b => b.replace(/^/gm, " ")).join("\n") + "\n";
23
- }
24
-
25
- if (needsMixed) {
26
- for (const mb of mixedBlocks) {
27
- const ifJS = mb.ifJS ? mb.ifJS.trim() : "";
28
- const elseJS = mb.elseJS ? mb.elseJS.trim() : "";
29
- renderBody += ` (function() {
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.buildRenderBody = buildRenderBody;
4
+ function buildRenderBody(opts) {
5
+ const { bodyHTML, logicBlocks, mixedBlocks, needsLogic, needsMixed, needsIf, needsFor, needsExpr, needsDelay, exprVarUpdates, ns } = opts;
6
+ const X = ns ? "__X" + ns : "__X";
7
+ let renderBody = "";
8
+ renderBody += ` let _sx = window.scrollX, _sy = window.scrollY;\n`;
9
+ if (needsLogic) {
10
+ renderBody += logicBlocks.map(b => b.replace(/^/gm, " ")).join("\n") + "\n";
11
+ }
12
+ if (needsMixed) {
13
+ for (const mb of mixedBlocks) {
14
+ const ifJS = mb.ifJS ? mb.ifJS.trim() : "";
15
+ const elseJS = mb.elseJS ? mb.elseJS.trim() : "";
16
+ renderBody += ` (function() {
30
17
  let __el = document.getElementById(${JSON.stringify(mb.replaceId || mb.id)});
31
18
  if (__el) {
32
19
  let __condFn = ${X}[${mb.conditionJS}];
@@ -44,41 +31,40 @@ export function buildRenderBody(opts: {
44
31
  __el.querySelectorAll('[data-sinth-delay-expr-id]').forEach(function(el) { sinthDelayExpr(el, {}); });` : ""}
45
32
  }
46
33
  })();\n`;
34
+ }
47
35
  }
48
- }
49
-
50
- renderBody += ` document.querySelectorAll('[data-sinth-remove]').forEach(function(el) {
36
+ renderBody += ` document.querySelectorAll('[data-sinth-remove]').forEach(function(el) {
51
37
  let target = document.getElementById(el.dataset.sinthRemove);
52
38
  if (target) target.remove();
53
39
  });\n`;
54
- if (needsFor) {
55
- renderBody += ` document.querySelectorAll('template[data-sinth-for], template[data-sinth-for-expr]').forEach(sinthForBlock);\n`;
56
- }
57
- if (needsIf) {
58
- renderBody += ` document.querySelectorAll('template[data-sinth-if-expr]').forEach(function(t) { var p = t.parentElement; while(p && p.tagName !== 'TEMPLATE') p = p.parentElement; if (!p || (!p.hasAttribute('data-sinth-for') && !p.hasAttribute('data-sinth-for-expr'))) sinthIfBlock(t); });\n`;
59
- }
60
- renderBody += ` document.querySelectorAll('[data-sinth-value]').forEach(function(el) {
40
+ if (needsFor) {
41
+ renderBody += ` document.querySelectorAll('template[data-sinth-for], template[data-sinth-for-expr]').forEach(sinthForBlock);\n`;
42
+ }
43
+ if (needsIf) {
44
+ renderBody += ` document.querySelectorAll('template[data-sinth-if-expr]').forEach(function(t) { var p = t.parentElement; while(p && p.tagName !== 'TEMPLATE') p = p.parentElement; if (!p || (!p.hasAttribute('data-sinth-for') && !p.hasAttribute('data-sinth-for-expr'))) sinthIfBlock(t); });\n`;
45
+ }
46
+ renderBody += ` document.querySelectorAll('[data-sinth-value]').forEach(function(el) {
61
47
  try { if (document.activeElement !== el) { let v=el.dataset.sinthValue; el.value = ${X}[v] ? ${X}[v]({}) : ''; } } catch(e) {}
62
48
  });\n`;
63
- renderBody += ` document.querySelectorAll('[data-sinth-step]').forEach(function(el) {
49
+ renderBody += ` document.querySelectorAll('[data-sinth-step]').forEach(function(el) {
64
50
  try { let s=el.dataset.sinthStep; el.step = ${X}[s] ? ${X}[s]({}) : 1; } catch(e) {}
65
51
  });\n`;
66
- renderBody += ` document.querySelectorAll('[data-sinth-checked]').forEach(function(el) {
52
+ renderBody += ` document.querySelectorAll('[data-sinth-checked]').forEach(function(el) {
67
53
  try { let c=el.dataset.sinthChecked; el.checked = ${X}[c] ? !!${X}[c]({}) : false; } catch(e) {}
68
54
  });\n`;
69
- if (exprVarUpdates) {
70
- renderBody += ` ${exprVarUpdates}\n`;
71
- }
72
- if (bodyHTML.includes("data-sinth-checked-expr")) {
73
- renderBody += ` document.querySelectorAll('[data-sinth-checked-expr]').forEach(function(el) {
55
+ if (exprVarUpdates) {
56
+ renderBody += ` ${exprVarUpdates}\n`;
57
+ }
58
+ if (bodyHTML.includes("data-sinth-checked-expr")) {
59
+ renderBody += ` document.querySelectorAll('[data-sinth-checked-expr]').forEach(function(el) {
74
60
  try {
75
61
  let exprFn = ${X}[el.dataset.sinthCheckedExpr];
76
62
  if (exprFn) el.checked = !!exprFn({});
77
63
  } catch(e) {}
78
64
  });\n`;
79
- }
80
- if (needsDelay) {
81
- renderBody += ` document.querySelectorAll('[data-sinth-delay]').forEach(function(el) {
65
+ }
66
+ if (needsDelay) {
67
+ renderBody += ` document.querySelectorAll('[data-sinth-delay]').forEach(function(el) {
82
68
  let newContent = '';
83
69
  el.querySelectorAll('.sinth-expr').forEach(function(exprEl) {
84
70
  let exprFn = ${X}[exprEl.dataset.exprId];
@@ -92,16 +78,16 @@ export function buildRenderBody(opts: {
92
78
  }
93
79
  });
94
80
  document.querySelectorAll('[data-sinth-delay-expr-id]').forEach(function(el) { sinthDelayExpr(el, {}); });\n`;
95
- }
96
- if (needsExpr) {
97
- renderBody += ` document.querySelectorAll('.sinth-expr').forEach(function(el) {
81
+ }
82
+ if (needsExpr) {
83
+ renderBody += ` document.querySelectorAll('.sinth-expr').forEach(function(el) {
98
84
  let delayParent = el.closest('[data-sinth-delay]');
99
85
  if (!delayParent || delayParent.dataset.sinthDelayDone) {
100
86
  sinthExpr(el);
101
87
  }
102
88
  });\n`;
103
- }
104
- renderBody += ` document.querySelectorAll('[data-sinth-style]').forEach(function(el) {
89
+ }
90
+ renderBody += ` document.querySelectorAll('[data-sinth-style]').forEach(function(el) {
105
91
  try {
106
92
  let d = el.dataset.sinthStyle.split(';');
107
93
  for (let i = 0; i < d.length; i++) {
@@ -113,7 +99,7 @@ export function buildRenderBody(opts: {
113
99
  }
114
100
  } catch(e) {}
115
101
  });\n`;
116
- renderBody += ` document.querySelectorAll('[data-sinth-hide]').forEach(function(el) {
102
+ renderBody += ` document.querySelectorAll('[data-sinth-hide]').forEach(function(el) {
117
103
  let exprId = el.dataset.sinthHide;
118
104
  if (exprId) {
119
105
  try {
@@ -124,7 +110,7 @@ export function buildRenderBody(opts: {
124
110
  el.style.display = 'none';
125
111
  }
126
112
  });\n`;
127
- renderBody += ` document.querySelectorAll('[data-sinth-fullscreen]').forEach(function(el) {
113
+ renderBody += ` document.querySelectorAll('[data-sinth-fullscreen]').forEach(function(el) {
128
114
  let exprId = el.dataset.sinthFullscreen;
129
115
  if (exprId) {
130
116
  try {
@@ -139,13 +125,12 @@ export function buildRenderBody(opts: {
139
125
  } catch(e) {}
140
126
  }
141
127
  });\n`;
142
- if (needsDelay) {
143
- renderBody += ` setTimeout(function() {
128
+ if (needsDelay) {
129
+ renderBody += ` setTimeout(function() {
144
130
  document.querySelectorAll('[data-sinth-delay]').forEach(sinthDelay);
145
131
  document.querySelectorAll('[data-sinth-delay-expr-id]').forEach(function(el) { sinthDelayExpr(el, {}); });
146
132
  }, 0);\n`;
147
- }
148
- renderBody += ` window.scrollTo(_sx, _sy);\n`;
149
-
150
- return renderBody;
151
- }
133
+ }
134
+ renderBody += ` window.scrollTo(_sx, _sy);\n`;
135
+ return renderBody;
136
+ }