aberdeen 1.6.0 → 1.7.1

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/aberdeen.js CHANGED
@@ -154,9 +154,6 @@ var lastPrio = 0;
154
154
 
155
155
  class Scope {
156
156
  prio = --lastPrio;
157
- onChange(index) {
158
- queue(this);
159
- }
160
157
  remove() {
161
158
  const lastNode = this.getLastNode();
162
159
  if (lastNode)
@@ -176,6 +173,7 @@ class DelayedOneTimeRunner {
176
173
 
177
174
  class ContentScope extends Scope {
178
175
  cleaners;
176
+ changes;
179
177
  constructor(cleaners = []) {
180
178
  super();
181
179
  this.cleaners = cleaners;
@@ -196,7 +194,38 @@ class ContentScope extends Scope {
196
194
  sortedQueue?.remove(this);
197
195
  this.lastChild = undefined;
198
196
  }
197
+ onChange(target, index, newData, oldData) {
198
+ if (!this.changes) {
199
+ this.changes = new Map;
200
+ queue(this);
201
+ }
202
+ let targetDelta = this.changes.get(target);
203
+ if (!targetDelta) {
204
+ targetDelta = new Map;
205
+ this.changes.set(target, targetDelta);
206
+ }
207
+ if (targetDelta.has(index)) {
208
+ if (targetDelta.get(index) === newData)
209
+ targetDelta.delete(index);
210
+ } else {
211
+ targetDelta.set(index, oldData);
212
+ }
213
+ }
214
+ fetchHasChanges() {
215
+ if (!this.changes)
216
+ return false;
217
+ for (const targetDelta of this.changes.values()) {
218
+ if (targetDelta.size > 0) {
219
+ delete this.changes;
220
+ return true;
221
+ }
222
+ }
223
+ delete this.changes;
224
+ return false;
225
+ }
199
226
  queueRun() {
227
+ if (!this.fetchHasChanges())
228
+ return;
200
229
  this.remove();
201
230
  topRedrawScope = this;
202
231
  this.redraw();
@@ -205,9 +234,6 @@ class ContentScope extends Scope {
205
234
  getInsertAfterNode() {
206
235
  return this.getLastNode() || this.getPrecedingNode();
207
236
  }
208
- onChange() {
209
- queue(this);
210
- }
211
237
  getChildPrevSibling() {
212
238
  return this.lastChild;
213
239
  }
@@ -362,7 +388,7 @@ class OnEachScope extends Scope {
362
388
  target;
363
389
  byIndex = new Map;
364
390
  sortedSet = new ReverseSortedSet("sortKey");
365
- changedIndexes = new Set;
391
+ changedIndexes = new Map;
366
392
  constructor(proxy, renderer, makeSortKey) {
367
393
  super();
368
394
  this.renderer = renderer;
@@ -385,15 +411,22 @@ class OnEachScope extends Scope {
385
411
  getPrecedingNode() {
386
412
  return findLastNodeInPrevSiblings(this.prevSibling);
387
413
  }
388
- onChange(index) {
389
- if (!(this.target instanceof Array) || typeof index === "number")
390
- this.changedIndexes.add(index);
391
- queue(this);
414
+ onChange(target, index, newData, oldData) {
415
+ if (!(target instanceof Array) || typeof index === "number") {
416
+ if (this.changedIndexes.has(index)) {
417
+ if (this.changedIndexes.get(index) === newData) {
418
+ this.changedIndexes.delete(index);
419
+ }
420
+ } else {
421
+ this.changedIndexes.set(index, oldData);
422
+ queue(this);
423
+ }
424
+ }
392
425
  }
393
426
  queueRun() {
394
427
  const indexes = this.changedIndexes;
395
- this.changedIndexes = new Set;
396
- for (const index of indexes) {
428
+ this.changedIndexes = new Map;
429
+ for (const index of indexes.keys()) {
397
430
  const oldScope = this.byIndex.get(index);
398
431
  if (oldScope)
399
432
  oldScope.remove();
@@ -466,6 +499,8 @@ class OnEachItemScope extends ContentScope {
466
499
  queueRun() {
467
500
  if (currentScope !== ROOT_SCOPE)
468
501
  internalError(4);
502
+ if (!this.fetchHasChanges())
503
+ return;
469
504
  if (this.sortKey !== undefined) {
470
505
  const lastNode = this.getActualLastNode();
471
506
  if (lastNode)
@@ -593,23 +628,26 @@ function isEmpty(proxied) {
593
628
  if (target instanceof Array) {
594
629
  subscribe(target, "length", (index, newData, oldData) => {
595
630
  if (!newData !== !oldData)
596
- queue(scope);
631
+ scope.onChange(target, EMPTY, !newData, !oldData);
597
632
  });
598
633
  return !target.length;
599
634
  }
600
635
  if (target instanceof Map) {
601
636
  subscribe(target, MAP_SIZE_SYMBOL, (index, newData, oldData) => {
602
637
  if (!newData !== !oldData)
603
- queue(scope);
638
+ scope.onChange(target, EMPTY, !newData, !oldData);
604
639
  });
605
640
  return !target.size;
606
641
  }
607
- const result = isObjEmpty(target);
642
+ let oldEmpty = isObjEmpty(target);
608
643
  subscribe(target, ANY_SYMBOL, (index, newData, oldData) => {
609
- if (result ? oldData === EMPTY : newData === EMPTY)
610
- queue(scope);
644
+ if (newData === EMPTY !== (oldData === EMPTY)) {
645
+ const newEmpty = isObjEmpty(target);
646
+ scope.onChange(target, EMPTY, newEmpty, oldEmpty);
647
+ oldEmpty = newEmpty;
648
+ }
611
649
  });
612
- return result;
650
+ return oldEmpty;
613
651
  }
614
652
  function count(proxied) {
615
653
  if (proxied instanceof Array)
@@ -643,7 +681,7 @@ function defaultEmitHandler(target, index, newData, oldData) {
643
681
  if (typeof observer === "function")
644
682
  observer(index, newData, oldData);
645
683
  else
646
- observer.onChange(index);
684
+ observer.onChange(target, index, newData, oldData);
647
685
  }
648
686
  }
649
687
  }
@@ -1065,14 +1103,21 @@ var NO_COPY = Symbol("NO_COPY");
1065
1103
  Promise.prototype[NO_COPY] = true;
1066
1104
  var cssVars = optProxy({});
1067
1105
  function setSpacingCssVars(base = 1, unit = "rem") {
1068
- for (let i = 1;i <= 12; i++) {
1106
+ for (let i = 0;i <= 12; i++) {
1069
1107
  cssVars[i] = 2 ** (i - 3) * base + unit;
1070
1108
  }
1071
1109
  }
1110
+ var CSS_VAR_PATTERN = /(\([^)]*\))|("[^"]*")|(^| )\$(\w+)/g;
1072
1111
  var DIGIT_FIRST = /^\d/;
1073
- function cssVarRef(name) {
1074
- const varName = DIGIT_FIRST.test(name) ? `m${name}` : name;
1075
- return `var(--${varName})`;
1112
+ function cssVarRef(value) {
1113
+ if (value.indexOf("$") < 0)
1114
+ return value;
1115
+ return value.replace(CSS_VAR_PATTERN, (match, parens, quoted, prefix, name) => {
1116
+ if (parens || quoted)
1117
+ return match;
1118
+ const varName = DIGIT_FIRST.test(name) ? `m${name}` : name;
1119
+ return `${prefix}var(--${varName})`;
1120
+ });
1076
1121
  }
1077
1122
  if (typeof document !== "undefined") {
1078
1123
  leakScope(() => {
@@ -1095,20 +1140,16 @@ if (typeof document !== "undefined") {
1095
1140
  });
1096
1141
  });
1097
1142
  }
1143
+ var darkModeState;
1098
1144
  function darkMode() {
1099
- if (typeof window === "undefined" || !window.matchMedia)
1100
- return false;
1101
- const mediaQuery = window.matchMedia("(prefers-color-scheme: dark)");
1102
- const changed = proxy(false);
1103
- changed.value;
1104
- function onChange() {
1105
- changed.value = true;
1145
+ if (!darkModeState) {
1146
+ if (typeof window === "undefined" || !window.matchMedia)
1147
+ return false;
1148
+ const mediaQuery = window.matchMedia("(prefers-color-scheme: dark)");
1149
+ darkModeState = proxy({ value: mediaQuery.matches });
1150
+ mediaQuery.addEventListener("change", () => darkModeState.value = mediaQuery.matches);
1106
1151
  }
1107
- mediaQuery.addEventListener("change", onChange);
1108
- clean(() => {
1109
- mediaQuery.removeEventListener("change", onChange);
1110
- });
1111
- return mediaQuery.matches;
1152
+ return darkModeState.value;
1112
1153
  }
1113
1154
  function clone(src) {
1114
1155
  if (NO_COPY in src)
@@ -1220,16 +1261,32 @@ function $(...args) {
1220
1261
  for (let pos = 0;pos < argLen; pos = nextPos + 1) {
1221
1262
  nextPos = findFirst(arg, " .=:#", pos);
1222
1263
  const next = arg[nextPos];
1223
- if (next === ":" || next === "=") {
1224
- let key = arg.substring(pos, nextPos);
1225
- if (next === ":")
1226
- key = "$" + key;
1264
+ if (next === ":") {
1265
+ const key = "$" + arg.substring(pos, nextPos);
1266
+ if (nextPos + 1 >= argLen) {
1267
+ applyArg(el, key, args[++argIndex]);
1268
+ break;
1269
+ }
1270
+ if (arg[nextPos + 1] === " ") {
1271
+ const endIndex = findFirst(arg, ";", nextPos + 2);
1272
+ const value = arg.substring(nextPos + 2, endIndex).trim();
1273
+ applyArg(el, key, value);
1274
+ nextPos = endIndex;
1275
+ } else {
1276
+ const endIndex = findFirst(arg, " ", nextPos + 1);
1277
+ const value = arg.substring(nextPos + 1, endIndex);
1278
+ applyArg(el, key, value);
1279
+ nextPos = endIndex;
1280
+ }
1281
+ } else if (next === "=") {
1282
+ const key = arg.substring(pos, nextPos);
1227
1283
  if (nextPos + 1 >= argLen) {
1228
1284
  applyArg(el, key, args[++argIndex]);
1229
1285
  break;
1230
1286
  }
1231
- if (arg[nextPos + 1] === '"') {
1232
- const endIndex = findFirst(arg, '"', nextPos + 2);
1287
+ const afterEquals = arg[nextPos + 1];
1288
+ if (afterEquals === '"' || afterEquals === "'" || afterEquals === "`") {
1289
+ const endIndex = findFirst(arg, afterEquals, nextPos + 2);
1233
1290
  const value = arg.substring(nextPos + 2, endIndex);
1234
1291
  applyArg(el, key, value);
1235
1292
  nextPos = endIndex;
@@ -1302,65 +1359,105 @@ function findFirst(str, chars, startPos) {
1302
1359
  return strLen;
1303
1360
  }
1304
1361
  var cssCount = 0;
1305
- function insertCss(style, global = false) {
1306
- const prefix = global ? "" : `.AbdStl${++cssCount}`;
1307
- const css = styleToCss(style, prefix);
1362
+ function insertCss(style) {
1363
+ const prefix = `.AbdStl${++cssCount}`;
1364
+ const css = typeof style === "string" ? styleStringToCss(style, prefix) : objectToCss(style, prefix);
1308
1365
  if (css)
1309
1366
  $(`style#${css}`);
1310
1367
  return prefix;
1311
1368
  }
1369
+ function combinePrefixSelector(prefix, key) {
1370
+ const sel = [];
1371
+ for (const p of prefix.split(",")) {
1372
+ for (const k of key.split(",")) {
1373
+ sel.push(k.includes("&") ? k.trim().replace(/&/g, p) : `${p} ${k.trim()}`.trim());
1374
+ }
1375
+ }
1376
+ return sel.join(",");
1377
+ }
1378
+ function objectToCss(style, prefix) {
1379
+ let css = "";
1380
+ for (const [key, val] of Object.entries(style)) {
1381
+ if (val && typeof val === "object") {
1382
+ if (key.startsWith("@")) {
1383
+ css += `${key}{
1384
+ ${objectToCss(val, prefix)}}
1385
+ `;
1386
+ } else {
1387
+ css += objectToCss(val, combinePrefixSelector(prefix, key));
1388
+ }
1389
+ } else if (typeof val === "string") {
1390
+ if (key.startsWith("@")) {
1391
+ css += `${key}{
1392
+ ${styleStringToCss(val, prefix)}}
1393
+ `;
1394
+ } else {
1395
+ css += styleStringToCss(val, combinePrefixSelector(prefix, key));
1396
+ }
1397
+ }
1398
+ }
1399
+ return css;
1400
+ }
1401
+ var KEBAB_SEGMENT = /-([a-z])/g;
1402
+ function toCamel(p) {
1403
+ return p.replace(KEBAB_SEGMENT, (_, l) => l.toUpperCase());
1404
+ }
1405
+ function styleStringToCss(styleStr, selector) {
1406
+ let props = "";
1407
+ for (let pos = 0, len = styleStr.length;pos < len; ) {
1408
+ while (styleStr[pos] === " ")
1409
+ pos++;
1410
+ if (pos >= len)
1411
+ break;
1412
+ const colon = styleStr.indexOf(":", pos);
1413
+ if (colon === -1)
1414
+ break;
1415
+ const key = styleStr.substring(pos, colon);
1416
+ pos = colon + 1;
1417
+ let val;
1418
+ if (styleStr[pos] === " ") {
1419
+ pos++;
1420
+ const semi = styleStr.indexOf(";", pos);
1421
+ val = styleStr.substring(pos, semi === -1 ? len : semi).trim();
1422
+ pos = semi === -1 ? len : semi + 1;
1423
+ } else {
1424
+ const space = styleStr.indexOf(" ", pos);
1425
+ val = styleStr.substring(pos, space === -1 ? len : space);
1426
+ pos = space === -1 ? len : space;
1427
+ }
1428
+ const v = cssVarRef(val);
1429
+ const exp = CSS_SHORT[key] || key;
1430
+ props += typeof exp === "string" ? `${exp}:${v};` : exp.map((p) => `${p}:${v};`).join("");
1431
+ }
1432
+ return props ? `${selector}{${props}}
1433
+ ` : "";
1434
+ }
1312
1435
  function insertGlobalCss(style) {
1313
- return insertCss(style, true);
1436
+ const css = objectToCss(style, "");
1437
+ if (css)
1438
+ $(`style#${css}`);
1314
1439
  }
1315
1440
  var CSS_SHORT = {
1316
1441
  m: "margin",
1317
- mt: "marginTop",
1318
- mb: "marginBottom",
1319
- ml: "marginLeft",
1320
- mr: "marginRight",
1321
- mh: ["marginLeft", "marginRight"],
1322
- mv: ["marginTop", "marginBottom"],
1442
+ mt: "margin-top",
1443
+ mb: "margin-bottom",
1444
+ ml: "margin-left",
1445
+ mr: "margin-right",
1446
+ mh: ["margin-left", "margin-right"],
1447
+ mv: ["margin-top", "margin-bottom"],
1323
1448
  p: "padding",
1324
- pt: "paddingTop",
1325
- pb: "paddingBottom",
1326
- pl: "paddingLeft",
1327
- pr: "paddingRight",
1328
- ph: ["paddingLeft", "paddingRight"],
1329
- pv: ["paddingTop", "paddingBottom"],
1449
+ pt: "padding-top",
1450
+ pb: "padding-bottom",
1451
+ pl: "padding-left",
1452
+ pr: "padding-right",
1453
+ ph: ["padding-left", "padding-right"],
1454
+ pv: ["padding-top", "padding-bottom"],
1330
1455
  w: "width",
1331
1456
  h: "height",
1332
1457
  bg: "background",
1333
1458
  fg: "color",
1334
- r: "borderRadius"
1459
+ r: "border-radius"
1335
1460
  };
1336
- function styleToCss(style, prefix) {
1337
- let props = "";
1338
- let rules = "";
1339
- for (const kOr of Object.keys(style)) {
1340
- const v = style[kOr];
1341
- for (const k of kOr.split(/, ?/g)) {
1342
- if (v && typeof v === "object") {
1343
- if (k.startsWith("@")) {
1344
- rules += `${k}{
1345
- ${styleToCss(v, prefix)}}
1346
- `;
1347
- } else {
1348
- rules += styleToCss(v, k.includes("&") ? k.replace(/&/g, prefix) : `${prefix} ${k}`);
1349
- }
1350
- } else {
1351
- const val = v == null || v === false ? "" : typeof v === "string" ? v[0] === "$" ? cssVarRef(v.substring(1)) : v : String(v);
1352
- const expanded = CSS_SHORT[k] || k;
1353
- for (const prop of Array.isArray(expanded) ? expanded : [expanded]) {
1354
- props += `${prop.replace(/[A-Z]/g, (letter) => `-${letter.toLowerCase()}`)}:${val};`;
1355
- }
1356
- }
1357
- }
1358
- }
1359
- if (props)
1360
- rules = `${prefix.trimStart() || "*"}{${props}}
1361
- ${rules}`;
1362
- return rules;
1363
- }
1364
1461
  function applyArg(el, key, value) {
1365
1462
  if (typeof value === "object" && value !== null && value[TARGET_SYMBOL]) {
1366
1463
  if (key === "bind") {
@@ -1376,13 +1473,13 @@ function applyArg(el, key, value) {
1376
1473
  el.classList.remove(...classes);
1377
1474
  } else if (key[0] === "$") {
1378
1475
  key = key.substring(1);
1379
- const val = value == null || value === false ? "" : typeof value === "string" ? value[0] === "$" ? cssVarRef(value.substring(1)) : value : String(value);
1476
+ const val = value == null || value === false ? "" : typeof value === "string" ? cssVarRef(value) : String(value);
1380
1477
  const expanded = CSS_SHORT[key] || key;
1381
1478
  if (typeof expanded === "string") {
1382
- el.style[expanded] = val;
1479
+ el.style[toCamel(expanded)] = val;
1383
1480
  } else {
1384
1481
  for (const prop of expanded)
1385
- el.style[prop] = val;
1482
+ el.style[toCamel(prop)] = val;
1386
1483
  }
1387
1484
  } else if (value == null) {} else if (key in SPECIAL_PROPS) {
1388
1485
  SPECIAL_PROPS[key](el, value);
@@ -1404,9 +1501,6 @@ var onError = defaultOnError;
1404
1501
  function setErrorHandler(handler) {
1405
1502
  onError = handler || defaultOnError;
1406
1503
  }
1407
- function getParentElement() {
1408
- return currentScope.el;
1409
- }
1410
1504
  function clean(cleaner) {
1411
1505
  currentScope.cleaners.push(cleaner);
1412
1506
  }
@@ -1566,7 +1660,6 @@ export {
1566
1660
  invertString,
1567
1661
  insertGlobalCss,
1568
1662
  insertCss,
1569
- getParentElement,
1570
1663
  dump,
1571
1664
  derive,
1572
1665
  defaultEmitHandler,
@@ -1580,5 +1673,5 @@ export {
1580
1673
  $
1581
1674
  };
1582
1675
 
1583
- //# debugId=30E505D12852725E64756E2164756E21
1676
+ //# debugId=453EEBC759CEDD8964756E2164756E21
1584
1677
  //# sourceMappingURL=aberdeen.js.map