aberdeen 1.6.0 → 1.7.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.
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);
1227
1266
  if (nextPos + 1 >= argLen) {
1228
1267
  applyArg(el, key, args[++argIndex]);
1229
1268
  break;
1230
1269
  }
1231
- if (arg[nextPos + 1] === '"') {
1232
- const endIndex = findFirst(arg, '"', nextPos + 2);
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);
1283
+ if (nextPos + 1 >= argLen) {
1284
+ applyArg(el, key, args[++argIndex]);
1285
+ break;
1286
+ }
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,98 @@ 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 objectToCss(style, prefix) {
1370
+ let css = "";
1371
+ for (const [key, val] of Object.entries(style)) {
1372
+ if (val && typeof val === "object") {
1373
+ if (key.startsWith("@")) {
1374
+ css += `${key}{
1375
+ ${objectToCss(val, prefix)}}
1376
+ `;
1377
+ } else {
1378
+ const sel = key === "&" ? prefix : key.includes("&") ? key.replace(/&/g, prefix) : `${prefix} ${key}`.trim();
1379
+ css += objectToCss(val, sel);
1380
+ }
1381
+ } else if (typeof val === "string") {
1382
+ if (key.startsWith("@")) {
1383
+ css += `${key}{
1384
+ ${styleStringToCss(val, prefix)}}
1385
+ `;
1386
+ } else {
1387
+ const sel = key.includes("&") ? key.replace(/&/g, prefix) : `${prefix} ${key}`.trim();
1388
+ css += styleStringToCss(val, sel);
1389
+ }
1390
+ }
1391
+ }
1392
+ return css;
1393
+ }
1394
+ var KEBAB_SEGMENT = /-([a-z])/g;
1395
+ function toCamel(p) {
1396
+ return p.replace(KEBAB_SEGMENT, (_, l) => l.toUpperCase());
1397
+ }
1398
+ function styleStringToCss(styleStr, selector) {
1399
+ let props = "";
1400
+ for (let pos = 0, len = styleStr.length;pos < len; ) {
1401
+ while (styleStr[pos] === " ")
1402
+ pos++;
1403
+ if (pos >= len)
1404
+ break;
1405
+ const colon = styleStr.indexOf(":", pos);
1406
+ if (colon === -1)
1407
+ break;
1408
+ const key = styleStr.substring(pos, colon);
1409
+ pos = colon + 1;
1410
+ let val;
1411
+ if (styleStr[pos] === " ") {
1412
+ pos++;
1413
+ const semi = styleStr.indexOf(";", pos);
1414
+ val = styleStr.substring(pos, semi === -1 ? len : semi).trim();
1415
+ pos = semi === -1 ? len : semi + 1;
1416
+ } else {
1417
+ const space = styleStr.indexOf(" ", pos);
1418
+ val = styleStr.substring(pos, space === -1 ? len : space);
1419
+ pos = space === -1 ? len : space;
1420
+ }
1421
+ const v = cssVarRef(val);
1422
+ const exp = CSS_SHORT[key] || key;
1423
+ props += typeof exp === "string" ? `${exp}:${v};` : exp.map((p) => `${p}:${v};`).join("");
1424
+ }
1425
+ return props ? `${selector}{${props}}
1426
+ ` : "";
1427
+ }
1312
1428
  function insertGlobalCss(style) {
1313
- return insertCss(style, true);
1429
+ const css = objectToCss(style, "");
1430
+ if (css)
1431
+ $(`style#${css}`);
1314
1432
  }
1315
1433
  var CSS_SHORT = {
1316
1434
  m: "margin",
1317
- mt: "marginTop",
1318
- mb: "marginBottom",
1319
- ml: "marginLeft",
1320
- mr: "marginRight",
1321
- mh: ["marginLeft", "marginRight"],
1322
- mv: ["marginTop", "marginBottom"],
1435
+ mt: "margin-top",
1436
+ mb: "margin-bottom",
1437
+ ml: "margin-left",
1438
+ mr: "margin-right",
1439
+ mh: ["margin-left", "margin-right"],
1440
+ mv: ["margin-top", "margin-bottom"],
1323
1441
  p: "padding",
1324
- pt: "paddingTop",
1325
- pb: "paddingBottom",
1326
- pl: "paddingLeft",
1327
- pr: "paddingRight",
1328
- ph: ["paddingLeft", "paddingRight"],
1329
- pv: ["paddingTop", "paddingBottom"],
1442
+ pt: "padding-top",
1443
+ pb: "padding-bottom",
1444
+ pl: "padding-left",
1445
+ pr: "padding-right",
1446
+ ph: ["padding-left", "padding-right"],
1447
+ pv: ["padding-top", "padding-bottom"],
1330
1448
  w: "width",
1331
1449
  h: "height",
1332
1450
  bg: "background",
1333
1451
  fg: "color",
1334
- r: "borderRadius"
1452
+ r: "border-radius"
1335
1453
  };
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
1454
  function applyArg(el, key, value) {
1365
1455
  if (typeof value === "object" && value !== null && value[TARGET_SYMBOL]) {
1366
1456
  if (key === "bind") {
@@ -1376,13 +1466,13 @@ function applyArg(el, key, value) {
1376
1466
  el.classList.remove(...classes);
1377
1467
  } else if (key[0] === "$") {
1378
1468
  key = key.substring(1);
1379
- const val = value == null || value === false ? "" : typeof value === "string" ? value[0] === "$" ? cssVarRef(value.substring(1)) : value : String(value);
1469
+ const val = value == null || value === false ? "" : typeof value === "string" ? cssVarRef(value) : String(value);
1380
1470
  const expanded = CSS_SHORT[key] || key;
1381
1471
  if (typeof expanded === "string") {
1382
- el.style[expanded] = val;
1472
+ el.style[toCamel(expanded)] = val;
1383
1473
  } else {
1384
1474
  for (const prop of expanded)
1385
- el.style[prop] = val;
1475
+ el.style[toCamel(prop)] = val;
1386
1476
  }
1387
1477
  } else if (value == null) {} else if (key in SPECIAL_PROPS) {
1388
1478
  SPECIAL_PROPS[key](el, value);
@@ -1404,9 +1494,6 @@ var onError = defaultOnError;
1404
1494
  function setErrorHandler(handler) {
1405
1495
  onError = handler || defaultOnError;
1406
1496
  }
1407
- function getParentElement() {
1408
- return currentScope.el;
1409
- }
1410
1497
  function clean(cleaner) {
1411
1498
  currentScope.cleaners.push(cleaner);
1412
1499
  }
@@ -1566,7 +1653,6 @@ export {
1566
1653
  invertString,
1567
1654
  insertGlobalCss,
1568
1655
  insertCss,
1569
- getParentElement,
1570
1656
  dump,
1571
1657
  derive,
1572
1658
  defaultEmitHandler,
@@ -1580,5 +1666,5 @@ export {
1580
1666
  $
1581
1667
  };
1582
1668
 
1583
- //# debugId=30E505D12852725E64756E2164756E21
1669
+ //# debugId=77770EA398EEA24264756E2164756E21
1584
1670
  //# sourceMappingURL=aberdeen.js.map