simplyview 3.0.1 → 3.0.3

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.
@@ -1,10 +1,4 @@
1
1
  (() => {
2
- var __defProp = Object.defineProperty;
3
- var __export = (target, all) => {
4
- for (var name in all)
5
- __defProp(target, name, { get: all[name], enumerable: true });
6
- };
7
-
8
2
  // src/activate.mjs
9
3
  var listeners = /* @__PURE__ */ new Map();
10
4
  var activate = {
@@ -66,10 +60,6 @@
66
60
  });
67
61
 
68
62
  // src/action.mjs
69
- var action_exports = {};
70
- __export(action_exports, {
71
- actions: () => actions
72
- });
73
63
  function actions(options) {
74
64
  if (options.app) {
75
65
  const actionHandler = {
@@ -84,10 +74,6 @@
84
74
  }
85
75
 
86
76
  // src/route.mjs
87
- var route_exports = {};
88
- __export(route_exports, {
89
- routes: () => routes
90
- });
91
77
  function routes(options) {
92
78
  return new SimplyRoute(options);
93
79
  }
@@ -131,11 +117,11 @@
131
117
  matches = route.match.exec(path);
132
118
  if (matches && matches.length) {
133
119
  var params = {};
134
- route.params.forEach((key, i) => {
120
+ route.params.forEach((key, i2) => {
135
121
  if (key == "*") {
136
122
  key = "remainder";
137
123
  }
138
- params[key] = matches[i + 1];
124
+ params[key] = matches[i2 + 1];
139
125
  });
140
126
  Object.assign(params, options);
141
127
  args.route = route;
@@ -150,6 +136,8 @@
150
136
  if (path && path[path.length - 1] != "/") {
151
137
  return this.match(path + "/", options);
152
138
  }
139
+ console.log(path, this.routeInfo);
140
+ process.exit();
153
141
  return false;
154
142
  }
155
143
  runListeners(action, params) {
@@ -262,8 +250,7 @@
262
250
  function getRegexpFromRoute(route) {
263
251
  return new RegExp("^" + route.replace(/:\w+/g, "([^/]+)").replace(/:\*/, "(.*)"));
264
252
  }
265
- function parseRoutes(routes2) {
266
- let routeInfo = [];
253
+ function parseRoutes(routes2, routeInfo) {
267
254
  const paths = Object.keys(routes2);
268
255
  const matchParams = /:(\w+|\*)/g;
269
256
  for (let path of paths) {
@@ -285,10 +272,6 @@
285
272
  }
286
273
 
287
274
  // src/command.mjs
288
- var command_exports = {};
289
- __export(command_exports, {
290
- commands: () => commands
291
- });
292
275
  var SimplyCommands = class {
293
276
  constructor(options = {}) {
294
277
  if (!options.app) {
@@ -311,7 +294,7 @@
311
294
  return;
312
295
  }
313
296
  const shouldContinue = this[command.name].call(options.app, command.source, command.value);
314
- if (shouldContinue === false) {
297
+ if (shouldContinue !== true) {
315
298
  evt.preventDefault();
316
299
  evt.stopPropagation();
317
300
  return false;
@@ -409,11 +392,14 @@
409
392
  ];
410
393
 
411
394
  // src/key.mjs
412
- var key_exports = {};
413
- __export(key_exports, {
414
- keys: () => keys
395
+ var KEY = Object.freeze({
396
+ Compose: 229,
397
+ Control: 17,
398
+ Meta: 224,
399
+ Alt: 18,
400
+ Shift: 16
415
401
  });
416
- var SimplyKeys = class {
402
+ var SimplyKey = class {
417
403
  constructor(options = {}) {
418
404
  if (!options.app) {
419
405
  options.app = {};
@@ -423,7 +409,7 @@
423
409
  }
424
410
  Object.assign(this, options.keys);
425
411
  const keyHandler = (e) => {
426
- if (e.isComposing || e.keyCode === 229) {
412
+ if (e.isComposing || e.keyCode === KEY.Compose) {
427
413
  return;
428
414
  }
429
415
  if (e.defaultPrevented) {
@@ -436,864 +422,88 @@
436
422
  if (e.target.closest("[data-simply-keyboard]")) {
437
423
  selectedKeyboard = e.target.closest("[data-simply-keyboard]").dataset.simplyKeyboard;
438
424
  }
439
- let key = "";
440
- if (e.ctrlKey && e.keyCode != 17) {
441
- key += "Control+";
442
- }
443
- if (e.metaKey && e.keyCode != 224) {
444
- key += "Meta+";
445
- }
446
- if (e.altKey && e.keyCode != 18) {
447
- key += "Alt+";
448
- }
449
- if (e.shiftKey && e.keyCode != 16) {
450
- key += "Shift+";
451
- }
452
- key += e.key;
453
- if (this[selectedKeyboard] && this[selectedKeyboard][key]) {
454
- let keyboard = this[selectedKeyboard];
455
- keyboard[key].call(options.app, e);
456
- }
457
- };
458
- options.app.container.addEventListener("keydown", keyHandler);
459
- }
460
- };
461
- function keys(options = {}) {
462
- return new SimplyKeys(options);
463
- }
464
-
465
- // src/state.mjs
466
- var state_exports = {};
467
- __export(state_exports, {
468
- batch: () => batch,
469
- clockEffect: () => clockEffect,
470
- destroy: () => destroy,
471
- effect: () => effect,
472
- signal: () => signal,
473
- throttledEffect: () => throttledEffect,
474
- untracked: () => untracked
475
- });
476
- var iterate = Symbol("iterate");
477
- if (!Symbol.xRay) {
478
- Symbol.xRay = Symbol("xRay");
479
- }
480
- var signalHandler = {
481
- get: (target, property, receiver) => {
482
- if (property === Symbol.xRay) {
483
- return target;
484
- }
485
- const value = target?.[property];
486
- notifyGet(receiver, property);
487
- if (typeof value === "function") {
488
- if (Array.isArray(target)) {
489
- return (...args) => {
490
- let l = target.length;
491
- let result = value.apply(receiver, args);
492
- if (l != target.length) {
493
- notifySet(receiver, makeContext("length", { was: l, now: target.length }));
494
- }
495
- return result;
496
- };
497
- } else if (target instanceof Set || target instanceof Map) {
498
- return (...args) => {
499
- let s = target.size;
500
- let result = value.apply(target, args);
501
- if (s != target.size) {
502
- notifySet(receiver, makeContext("size", { was: s, now: target.size }));
503
- }
504
- if (["set", "add", "clear", "delete"].includes(property)) {
505
- notifySet(receiver, makeContext({ entries: {}, forEach: {}, has: {}, keys: {}, values: {}, [Symbol.iterator]: {} }));
506
- }
507
- return result;
508
- };
509
- } else if (target instanceof HTMLElement || target instanceof Number || target instanceof String || target instanceof Boolean) {
510
- return value.bind(target);
511
- } else {
512
- return value.bind(receiver);
513
- }
514
- }
515
- if (value && typeof value == "object") {
516
- return signal(value);
517
- }
518
- return value;
519
- },
520
- set: (target, property, value, receiver) => {
521
- value = value?.[Symbol.xRay] || value;
522
- let current = target[property];
523
- if (current !== value) {
524
- target[property] = value;
525
- notifySet(receiver, makeContext(property, { was: current, now: value }));
526
- }
527
- if (typeof current === "undefined") {
528
- notifySet(receiver, makeContext(iterate, {}));
529
- }
530
- return true;
531
- },
532
- has: (target, property) => {
533
- let receiver = signals.get(target);
534
- if (receiver) {
535
- notifyGet(receiver, property);
536
- }
537
- return Object.hasOwn(target, property);
538
- },
539
- deleteProperty: (target, property) => {
540
- if (typeof target[property] !== "undefined") {
541
- let current = target[property];
542
- delete target[property];
543
- let receiver = signals.get(target);
544
- notifySet(receiver, makeContext(property, { delete: true, was: current }));
545
- }
546
- return true;
547
- },
548
- defineProperty: (target, property, descriptor) => {
549
- if (typeof target[property] === "undefined") {
550
- let receiver = signals.get(target);
551
- notifySet(receiver, makeContext(iterate, {}));
552
- }
553
- return Object.defineProperty(target, property, descriptor);
554
- },
555
- ownKeys: (target) => {
556
- let receiver = signals.get(target);
557
- notifyGet(receiver, iterate);
558
- return Reflect.ownKeys(target);
559
- }
560
- };
561
- var signals = /* @__PURE__ */ new WeakMap();
562
- function signal(v) {
563
- if (!signals.has(v)) {
564
- signals.set(v, new Proxy(v, signalHandler));
565
- }
566
- return signals.get(v);
567
- }
568
- var batchedListeners = /* @__PURE__ */ new Set();
569
- var batchMode = 0;
570
- function notifySet(self, context = {}) {
571
- let listeners2 = [];
572
- context.forEach((change, property) => {
573
- let propListeners = getListeners(self, property);
574
- if (propListeners?.length) {
575
- for (let listener of propListeners) {
576
- addContext(listener, makeContext(property, change));
577
- }
578
- listeners2 = listeners2.concat(propListeners);
579
- }
580
- });
581
- listeners2 = new Set(listeners2.filter(Boolean));
582
- if (listeners2) {
583
- if (batchMode) {
584
- batchedListeners = batchedListeners.union(listeners2);
585
- } else {
586
- const currentEffect = computeStack[computeStack.length - 1];
587
- for (let listener of Array.from(listeners2)) {
588
- if (listener != currentEffect && listener?.needsUpdate) {
589
- listener();
590
- }
591
- clearContext(listener);
592
- }
593
- }
594
- }
595
- }
596
- function makeContext(property, change) {
597
- let context = /* @__PURE__ */ new Map();
598
- if (typeof property === "object") {
599
- for (let prop in property) {
600
- context.set(prop, property[prop]);
601
- }
602
- } else {
603
- context.set(property, change);
604
- }
605
- return context;
606
- }
607
- function addContext(listener, context) {
608
- if (!listener.context) {
609
- listener.context = context;
610
- } else {
611
- context.forEach((change, property) => {
612
- listener.context.set(property, change);
613
- });
614
- }
615
- listener.needsUpdate = true;
616
- }
617
- function clearContext(listener) {
618
- delete listener.context;
619
- delete listener.needsUpdate;
620
- }
621
- function notifyGet(self, property) {
622
- let currentCompute = computeStack[computeStack.length - 1];
623
- if (currentCompute) {
624
- setListeners(self, property, currentCompute);
625
- }
626
- }
627
- var listenersMap = /* @__PURE__ */ new WeakMap();
628
- var computeMap = /* @__PURE__ */ new WeakMap();
629
- function getListeners(self, property) {
630
- let listeners2 = listenersMap.get(self);
631
- return listeners2 ? Array.from(listeners2.get(property) || []) : [];
632
- }
633
- function setListeners(self, property, compute) {
634
- if (!listenersMap.has(self)) {
635
- listenersMap.set(self, /* @__PURE__ */ new Map());
636
- }
637
- let listeners2 = listenersMap.get(self);
638
- if (!listeners2.has(property)) {
639
- listeners2.set(property, /* @__PURE__ */ new Set());
640
- }
641
- listeners2.get(property).add(compute);
642
- if (!computeMap.has(compute)) {
643
- computeMap.set(compute, /* @__PURE__ */ new Map());
644
- }
645
- let connectedSignals = computeMap.get(compute);
646
- if (!connectedSignals.has(property)) {
647
- connectedSignals.set(property, /* @__PURE__ */ new Set());
648
- }
649
- connectedSignals.get(property).add(self);
650
- }
651
- function clearListeners(compute) {
652
- let connectedSignals = computeMap.get(compute);
653
- if (connectedSignals) {
654
- connectedSignals.forEach((property) => {
655
- property.forEach((s) => {
656
- let listeners2 = listenersMap.get(s);
657
- if (listeners2.has(property)) {
658
- listeners2.get(property).delete(compute);
659
- }
660
- });
661
- });
662
- }
663
- }
664
- var computeStack = [];
665
- var effectStack = [];
666
- var effectMap = /* @__PURE__ */ new WeakMap();
667
- var signalStack = [];
668
- function effect(fn) {
669
- if (effectStack.findIndex((f) => fn == f) !== -1) {
670
- throw new Error("Recursive update() call", { cause: fn });
671
- }
672
- effectStack.push(fn);
673
- let connectedSignal = signals.get(fn);
674
- if (!connectedSignal) {
675
- connectedSignal = signal({
676
- current: null
677
- });
678
- signals.set(fn, connectedSignal);
679
- }
680
- const computeEffect = function computeEffect2() {
681
- if (signalStack.findIndex((s) => s == connectedSignal) !== -1) {
682
- throw new Error("Cyclical dependency in update() call", { cause: fn });
683
- }
684
- clearListeners(computeEffect2);
685
- computeStack.push(computeEffect2);
686
- signalStack.push(connectedSignal);
687
- let result;
688
- try {
689
- result = fn(computeEffect2, computeStack, signalStack);
690
- } finally {
691
- computeStack.pop();
692
- signalStack.pop();
693
- if (result instanceof Promise) {
694
- result.then((result2) => {
695
- connectedSignal.current = result2;
696
- });
697
- } else {
698
- connectedSignal.current = result;
699
- }
700
- }
701
- };
702
- computeEffect.fn = fn;
703
- effectMap.set(connectedSignal, computeEffect);
704
- computeEffect();
705
- return connectedSignal;
706
- }
707
- function destroy(connectedSignal) {
708
- const computeEffect = effectMap.get(connectedSignal)?.deref();
709
- if (!computeEffect) {
710
- return;
711
- }
712
- clearListeners(computeEffect);
713
- let fn = computeEffect.fn;
714
- signals.remove(fn);
715
- effectMap.delete(connectedSignal);
716
- }
717
- function batch(fn) {
718
- batchMode++;
719
- let result;
720
- try {
721
- result = fn();
722
- } finally {
723
- if (result instanceof Promise) {
724
- result.then(() => {
725
- batchMode--;
726
- if (!batchMode) {
727
- runBatchedListeners();
728
- }
729
- });
730
- } else {
731
- batchMode--;
732
- if (!batchMode) {
733
- runBatchedListeners();
734
- }
735
- }
736
- }
737
- return result;
738
- }
739
- function runBatchedListeners() {
740
- let copyBatchedListeners = Array.from(batchedListeners);
741
- batchedListeners = /* @__PURE__ */ new Set();
742
- const currentEffect = computeStack[computeStack.length - 1];
743
- for (let listener of copyBatchedListeners) {
744
- if (listener != currentEffect && listener?.needsUpdate) {
745
- listener();
746
- }
747
- clearContext(listener);
748
- }
749
- }
750
- function throttledEffect(fn, throttleTime) {
751
- if (effectStack.findIndex((f) => fn == f) !== -1) {
752
- throw new Error("Recursive update() call", { cause: fn });
753
- }
754
- effectStack.push(fn);
755
- let connectedSignal = signals.get(fn);
756
- if (!connectedSignal) {
757
- connectedSignal = signal({
758
- current: null
759
- });
760
- signals.set(fn, connectedSignal);
761
- }
762
- let throttled = false;
763
- let hasChange = true;
764
- const computeEffect = function computeEffect2() {
765
- if (signalStack.findIndex((s) => s == connectedSignal) !== -1) {
766
- throw new Error("Cyclical dependency in update() call", { cause: fn });
767
- }
768
- if (throttled && throttled > Date.now()) {
769
- hasChange = true;
770
- return;
771
- }
772
- clearListeners(computeEffect2);
773
- computeStack.push(computeEffect2);
774
- signalStack.push(connectedSignal);
775
- let result;
776
- try {
777
- result = fn(computeEffect2, computeStack, signalStack);
778
- } finally {
779
- hasChange = false;
780
- computeStack.pop();
781
- signalStack.pop();
782
- if (result instanceof Promise) {
783
- result.then((result2) => {
784
- connectedSignal.current = result2;
785
- });
786
- } else {
787
- connectedSignal.current = result;
788
- }
789
- }
790
- throttled = Date.now() + throttleTime;
791
- globalThis.setTimeout(() => {
792
- if (hasChange) {
793
- computeEffect2();
794
- }
795
- }, throttleTime);
796
- };
797
- computeEffect();
798
- return connectedSignal;
799
- }
800
- function clockEffect(fn, clock) {
801
- let connectedSignal = signals.get(fn);
802
- if (!connectedSignal) {
803
- connectedSignal = signal({
804
- current: null
805
- });
806
- signals.set(fn, connectedSignal);
807
- }
808
- let lastTick = -1;
809
- let hasChanged = true;
810
- const computeEffect = function computeEffect2() {
811
- if (lastTick < clock.time) {
812
- if (hasChanged) {
813
- clearListeners(computeEffect2);
814
- computeStack.push(computeEffect2);
815
- lastTick = clock.time;
816
- let result;
817
- try {
818
- result = fn(computeEffect2, computeStack);
819
- } finally {
820
- computeStack.pop();
821
- if (result instanceof Promise) {
822
- result.then((result2) => {
823
- connectedSignal.current = result2;
824
- });
825
- } else {
826
- connectedSignal.current = result;
827
- }
828
- hasChanged = false;
425
+ let keyCombination = [];
426
+ if (e.ctrlKey && e.keyCode != KEY.Control) {
427
+ keyCombination.push("Control");
428
+ }
429
+ if (e.metaKey && e.keyCode != KEY.Meta) {
430
+ keyCombination.push("Meta");
431
+ }
432
+ if (e.altKey && e.keyCode != KEY.Alt) {
433
+ keyCombination.push("Alt");
434
+ }
435
+ if (e.shiftKey && e.keyCode != KEY.Shift) {
436
+ keyCombination.push("Shift");
437
+ }
438
+ keyCombination.push(e.key.toLowerCase());
439
+ let keyboards = [];
440
+ let keyboardElement = event.target.closest("[data-simply-keyboard]");
441
+ while (keyboardElement) {
442
+ keyboards.push(keyboardElement.dataset.simplyKeyboard);
443
+ keyboardElement = keyboardElement.parentNode.closest("[data-simply-keyboard]");
444
+ }
445
+ keyboards.push("");
446
+ let keyboard, subkeyboard;
447
+ let separators = ["+", "-"];
448
+ for (i in keyboards) {
449
+ keyboard = keyboards[i];
450
+ if (keyboard == "") {
451
+ subkeyboard = "default";
452
+ } else {
453
+ subkeyboard = keyboard;
454
+ keyboard += ".";
829
455
  }
830
- } else {
831
- lastTick = clock.time;
832
- }
833
- } else {
834
- hasChanged = true;
835
- }
836
- };
837
- computeEffect();
838
- return connectedSignal;
839
- }
840
- function untracked(fn) {
841
- const remember = computeStack.slice();
842
- computeStack = [];
843
- try {
844
- return fn();
845
- } finally {
846
- computeStack = remember;
847
- }
848
- }
849
-
850
- // src/bind.mjs
851
- var SimplyBind = class {
852
- constructor(options) {
853
- this.bindings = /* @__PURE__ */ new Map();
854
- const defaultOptions = {
855
- container: document.body,
856
- attribute: "data-bind",
857
- transformers: [],
858
- defaultTransformers: [defaultTransformer]
859
- };
860
- if (!options?.root) {
861
- throw new Error("bind needs at least options.root set");
862
- }
863
- this.options = Object.assign({}, defaultOptions, options);
864
- const attribute = this.options.attribute;
865
- const render = (el) => {
866
- this.bindings.set(el, throttledEffect(() => {
867
- const context = {
868
- templates: el.querySelectorAll(":scope > template"),
869
- path: this.getBindingPath(el)
870
- };
871
- context.value = getValueByPath(this.options.root, context.path);
872
- context.element = el;
873
- runTransformers(context);
874
- }, 100));
875
- };
876
- const runTransformers = (context) => {
877
- let transformers = this.options.defaultTransformers || [];
878
- if (context.element.dataset.transform) {
879
- context.element.dataset.transform.split(" ").filter(Boolean).forEach((t) => {
880
- if (this.options.transformers[t]) {
881
- transformers.push(this.options.transformers[t]);
882
- } else {
883
- console.warn("No transformer with name " + t + " configured", { cause: context.element });
884
- }
885
- });
886
- }
887
- let next;
888
- for (let transformer of transformers) {
889
- next = /* @__PURE__ */ ((next2, transformer2) => {
890
- return (context2) => {
891
- return transformer2.call(this, context2, next2);
892
- };
893
- })(next, transformer);
894
- }
895
- next(context);
896
- };
897
- const applyBindings = (bindings2) => {
898
- for (let bindingEl of bindings2) {
899
- render(bindingEl);
900
- }
901
- };
902
- const updateBindings = (changes) => {
903
- for (const change of changes) {
904
- if (change.type == "childList" && change.addedNodes) {
905
- for (let node of change.addedNodes) {
906
- if (node instanceof HTMLElement) {
907
- let bindings2 = Array.from(node.querySelectorAll(`[${attribute}]`));
908
- if (node.matches(`[${attribute}]`)) {
909
- bindings2.unshift(node);
910
- }
911
- if (bindings2.length) {
912
- applyBindings(bindings2);
913
- }
456
+ for (let separator of separators) {
457
+ let keyString = keyCombination.join(separator);
458
+ if (this[subkeyboard] && typeof this[subkeyboard][keyString] == "function") {
459
+ let _continue = this[subkeyboard][keyString].call(this[subkeyboard], e);
460
+ if (!_continue) {
461
+ e.preventDefault();
462
+ return;
914
463
  }
915
464
  }
916
- }
917
- }
918
- };
919
- this.observer = new MutationObserver((changes) => {
920
- updateBindings(changes);
921
- });
922
- this.observer.observe(options.container, {
923
- subtree: true,
924
- childList: true
925
- });
926
- const bindings = this.options.container.querySelectorAll("[" + this.options.attribute + "]:not(template)");
927
- if (bindings.length) {
928
- applyBindings(bindings);
929
- }
930
- }
931
- /**
932
- * Finds the first matching template and creates a new DocumentFragment
933
- * with the correct data bind attributes in it (prepends the current path)
934
- */
935
- applyTemplate(context) {
936
- const path = context.path;
937
- const templates = context.templates;
938
- const list = context.list;
939
- const index = context.index;
940
- const parent = context.parent;
941
- const value = list ? list[index] : context.value;
942
- let template = this.findTemplate(templates, value);
943
- if (!template) {
944
- let result = new DocumentFragment();
945
- result.innerHTML = "<!-- no matching template -->";
946
- return result;
947
- }
948
- let clone = template.content.cloneNode(true);
949
- if (!clone.children?.length) {
950
- throw new Error("template must contain a single html element", { cause: template });
951
- }
952
- if (clone.children.length > 1) {
953
- throw new Error("template must contain a single root node", { cause: template });
954
- }
955
- const bindings = clone.querySelectorAll("[" + this.options.attribute + "]");
956
- const attribute = this.options.attribute;
957
- for (let binding of bindings) {
958
- const bind2 = binding.getAttribute(attribute);
959
- if (bind2.substring(0, "#root.".length) == "#root.") {
960
- binding.setAttribute(attribute, bind2.substring("#root.".length));
961
- } else if (bind2 == "#value" && index != null) {
962
- binding.setAttribute(attribute, path + "." + index);
963
- } else if (index != null) {
964
- binding.setAttribute(attribute, path + "." + index + "." + bind2);
965
- } else {
966
- binding.setAttribute(attribute, parent + "." + bind2);
967
- }
968
- }
969
- if (typeof index !== "undefined") {
970
- clone.children[0].setAttribute(attribute + "-key", index);
971
- }
972
- clone.children[0].$bindTemplate = template;
973
- return clone;
974
- }
975
- getBindingPath(el) {
976
- return el.getAttribute(this.options.attribute);
977
- }
978
- /**
979
- * Finds the first template from an array of templates that
980
- * matches the given value.
981
- */
982
- findTemplate(templates, value) {
983
- const templateMatches = (t) => {
984
- let path = this.getBindingPath(t);
985
- let currentItem;
986
- if (path) {
987
- if (path.substr(0, 6) == "#root.") {
988
- currentItem = getValueByPath(this.options.root, path);
989
- } else {
990
- currentItem = getValueByPath(value, path);
991
- }
992
- } else {
993
- currentItem = value;
994
- }
995
- const strItem = "" + currentItem;
996
- let matches = t.getAttribute(this.options.attribute + "-match");
997
- if (matches) {
998
- if (matches === "#empty" && !currentItem) {
999
- return t;
1000
- } else if (matches === "#notempty" && currentItem) {
1001
- return t;
1002
- }
1003
- if (strItem.match(matches)) {
1004
- return t;
1005
- }
1006
- }
1007
- if (!matches) {
1008
- if (currentItem) {
1009
- return t;
1010
- }
1011
- }
1012
- };
1013
- let template = Array.from(templates).find(templateMatches);
1014
- let rel = template?.getAttribute("rel");
1015
- if (rel) {
1016
- let replacement = document.querySelector("template#" + rel);
1017
- if (!replacement) {
1018
- throw new Error("Could not find template with id " + rel);
1019
- }
1020
- template = replacement;
1021
- }
1022
- return template;
1023
- }
1024
- destroy() {
1025
- this.bindings.forEach((binding) => {
1026
- destroy(binding);
1027
- });
1028
- this.bindings = /* @__PURE__ */ new Map();
1029
- this.observer.disconnect();
1030
- }
1031
- };
1032
- function bind(options) {
1033
- return new SimplyBind(options);
1034
- }
1035
- function matchValue(a, b) {
1036
- if (a == "#empty" && !b) {
1037
- return true;
1038
- }
1039
- if (b == "#empty" && !a) {
1040
- return true;
1041
- }
1042
- if ("" + a == "" + b) {
1043
- return true;
1044
- }
1045
- return false;
1046
- }
1047
- function getValueByPath(root, path) {
1048
- let parts = path.split(".");
1049
- let curr = root;
1050
- let part, prevPart;
1051
- while (parts.length && curr) {
1052
- part = parts.shift();
1053
- if (part == "#key") {
1054
- return prevPart;
1055
- } else if (part == "#value") {
1056
- return curr;
1057
- } else if (part == "#root") {
1058
- curr = root;
1059
- } else {
1060
- part = decodeURIComponent(part);
1061
- curr = curr[part];
1062
- prevPart = part;
1063
- }
1064
- }
1065
- return curr;
1066
- }
1067
- function defaultTransformer(context) {
1068
- const el = context.element;
1069
- const templates = context.templates;
1070
- const templatesCount = templates.length;
1071
- const path = context.path;
1072
- const value = context.value;
1073
- const attribute = this.options.attribute;
1074
- if (Array.isArray(value) && templates?.length) {
1075
- transformArrayByTemplates.call(this, context);
1076
- } else if (typeof value == "object" && templates?.length) {
1077
- transformObjectByTemplates.call(this, context);
1078
- } else if (templates?.length) {
1079
- transformLiteralByTemplates.call(this, context);
1080
- } else if (el.tagName == "INPUT") {
1081
- transformInput.call(this, context);
1082
- } else if (el.tagName == "BUTTON") {
1083
- transformButton.call(this, context);
1084
- } else if (el.tagName == "SELECT") {
1085
- transformSelect.call(this, context);
1086
- } else if (el.tagName == "A") {
1087
- transformAnchor.call(this, context);
1088
- } else {
1089
- transformElement.call(this, context);
1090
- }
1091
- return context;
1092
- }
1093
- function transformArrayByTemplates(context) {
1094
- const el = context.element;
1095
- const templates = context.templates;
1096
- const templatesCount = templates.length;
1097
- const path = context.path;
1098
- const value = context.value;
1099
- const attribute = this.options.attribute;
1100
- let items = el.querySelectorAll(":scope > [" + attribute + "-key]");
1101
- let lastKey = 0;
1102
- let skipped = 0;
1103
- context.list = value;
1104
- for (let item of items) {
1105
- let currentKey = parseInt(item.getAttribute(attribute + "-key"));
1106
- if (currentKey > lastKey) {
1107
- context.index = lastKey;
1108
- el.insertBefore(this.applyTemplate(context), item);
1109
- } else if (currentKey < lastKey) {
1110
- item.remove();
1111
- } else {
1112
- let bindings = Array.from(item.querySelectorAll(`[${attribute}]`));
1113
- if (item.matches(`[${attribute}]`)) {
1114
- bindings.unshift(item);
1115
- }
1116
- let needsReplacement = bindings.find((b) => {
1117
- let databind = b.getAttribute(attribute);
1118
- return databind.substr(0, 5) !== "#root" && databind.substr(0, path.length) !== path;
1119
- });
1120
- if (!needsReplacement) {
1121
- if (item.$bindTemplate) {
1122
- let newTemplate = this.findTemplate(templates, value[lastKey]);
1123
- if (newTemplate != item.$bindTemplate) {
1124
- needsReplacement = true;
1125
- if (!newTemplate) {
1126
- skipped++;
465
+ if (typeof this[subkeyboard + keyString] == "function") {
466
+ let _continue = this[subkeyboard + keyString].call(this, e);
467
+ if (!_continue) {
468
+ e.preventDefault();
469
+ return;
1127
470
  }
1128
471
  }
1129
- }
1130
- }
1131
- if (needsReplacement) {
1132
- context.index = lastKey;
1133
- el.replaceChild(this.applyTemplate(context), item);
1134
- }
1135
- }
1136
- lastKey++;
1137
- if (lastKey >= value.length) {
1138
- break;
1139
- }
1140
- }
1141
- items = el.querySelectorAll(":scope > [" + attribute + "-key]");
1142
- let length = items.length + skipped;
1143
- if (length > value.length) {
1144
- while (length > value.length) {
1145
- let child = el.querySelectorAll(":scope > :not(template)")?.[length - 1];
1146
- child?.remove();
1147
- length--;
1148
- }
1149
- } else if (length < value.length) {
1150
- while (length < value.length) {
1151
- context.index = length;
1152
- el.appendChild(this.applyTemplate(context));
1153
- length++;
1154
- }
1155
- }
1156
- }
1157
- function transformObjectByTemplates(context) {
1158
- const el = context.element;
1159
- const templates = context.templates;
1160
- const templatesCount = templates.length;
1161
- const path = context.path;
1162
- const value = context.value;
1163
- const attribute = this.options.attribute;
1164
- context.list = value;
1165
- let list = Object.entries(value);
1166
- let items = el.querySelectorAll(":scope > [" + attribute + "-key]");
1167
- let current = 0;
1168
- let skipped = 0;
1169
- for (let item of items) {
1170
- if (current >= list.length) {
1171
- break;
1172
- }
1173
- let key = list[current][0];
1174
- current++;
1175
- let keypath = path + "." + key;
1176
- let needsReplacement;
1177
- const databind = item.getAttribute(attribute);
1178
- if (databind && databind.substr(0, keypath.length) != keypath) {
1179
- needsReplacement = true;
1180
- } else {
1181
- let bindings = Array.from(item.querySelectorAll(`[${attribute}]`));
1182
- needsReplacement = bindings.find((b) => {
1183
- const db = b.getAttribute(attribute);
1184
- return db.substr(0, 5) !== "#root" && db.substr(0, keypath.length) !== keypath;
1185
- });
1186
- if (!needsReplacement) {
1187
- if (item.$bindTemplate) {
1188
- let newTemplate = this.findTemplate(templates, value[key]);
1189
- if (newTemplate != item.$bindTemplate) {
1190
- needsReplacement = true;
1191
- if (!newTemplate) {
1192
- skipped++;
472
+ if (this[selectedKeyboard] && this[selectedKeyboard][keyString]) {
473
+ let targets = options.app.container.querySelectorAll('[data-simply-accesskey="' + keyboard + keyString + '"]');
474
+ if (targets.length) {
475
+ targets.forEach((t) => t.click());
476
+ e.preventDefault();
1193
477
  }
1194
478
  }
1195
479
  }
1196
480
  }
1197
- }
1198
- if (needsReplacement) {
1199
- context.index = key;
1200
- let clone = this.applyTemplate(context);
1201
- el.replaceChild(clone, item);
1202
- }
1203
- }
1204
- items = el.querySelectorAll(":scope > [" + attribute + "-key]");
1205
- let length = items.length + skipped;
1206
- if (length > list.length) {
1207
- while (length > list.length) {
1208
- let child = el.querySelectorAll(":scope > :not(template)")?.[length - 1];
1209
- child?.remove();
1210
- length--;
1211
- }
1212
- } else if (length < list.length) {
1213
- while (length < list.length) {
1214
- context.index = list[length][0];
1215
- el.appendChild(this.applyTemplate(context));
1216
- length++;
1217
- }
1218
- }
1219
- }
1220
- function transformLiteralByTemplates(context) {
1221
- const el = context.element;
1222
- const templates = context.templates;
1223
- const value = context.value;
1224
- const attribute = this.options.attribute;
1225
- const rendered = el.querySelector(":scope > :not(template)");
1226
- const template = this.findTemplate(templates, value);
1227
- context.parent = el.parentElement?.closest(`[${attribute}]`)?.getAttribute(attribute) || "#root";
1228
- if (rendered) {
1229
- if (template) {
1230
- if (rendered?.$bindTemplate != template) {
1231
- const clone = this.applyTemplate(context);
1232
- el.replaceChild(clone, rendered);
1233
- }
1234
- } else {
1235
- el.removeChild(rendered);
1236
- }
1237
- } else if (template) {
1238
- const clone = this.applyTemplate(context);
1239
- el.appendChild(clone);
481
+ };
482
+ options.app.container.addEventListener("keydown", keyHandler);
1240
483
  }
484
+ };
485
+ function keys(options = {}) {
486
+ return new SimplyKey(options);
1241
487
  }
1242
- function transformInput(context) {
1243
- const el = context.element;
1244
- const value = context.value;
1245
- if (el.type == "checkbox" || el.type == "radio") {
1246
- if (matchValue(el.value, value)) {
1247
- el.checked = true;
488
+
489
+ // src/view.mjs
490
+ function view(options) {
491
+ if (options.app) {
492
+ options.app.view = options.view || {};
493
+ const load = () => {
494
+ const data = options.app.view;
495
+ const path = globalThis.editor.data.getDataPath(options.app.container || document.body);
496
+ options.app.view = globalThis.editor.currentData[path];
497
+ Object.assign(options.app.view, data);
498
+ };
499
+ if (globalThis.editor && globalThis.editor.currentData) {
500
+ load();
1248
501
  } else {
1249
- el.checked = false;
1250
- }
1251
- } else if (!matchValue(el.value, value)) {
1252
- el.value = "" + value;
1253
- }
1254
- }
1255
- function transformButton(context) {
1256
- const el = context.element;
1257
- const value = context.value;
1258
- if (!matchValue(el.value, value)) {
1259
- el.value = "" + value;
1260
- }
1261
- }
1262
- function transformSelect(context) {
1263
- const el = context.element;
1264
- const value = context.value;
1265
- if (el.multiple) {
1266
- if (Array.isArray(value)) {
1267
- for (let option of el.options) {
1268
- if (value.indexOf(option.value) === false) {
1269
- option.selected = false;
1270
- } else {
1271
- option.selected = true;
1272
- }
1273
- }
502
+ document.addEventListener("simply-content-loaded", load);
1274
503
  }
504
+ return options.app.view;
1275
505
  } else {
1276
- let option = el.options.find((o) => matchValue(o.value, value));
1277
- if (option) {
1278
- option.selected = true;
1279
- }
1280
- }
1281
- }
1282
- function transformAnchor(context) {
1283
- const el = context.element;
1284
- const value = context.value;
1285
- if (value?.innerHTML && !matchValue(el.innerHTML, value.innerHTML)) {
1286
- el.innerHTML = "" + value.innerHTML;
1287
- }
1288
- if (value?.href && !matchValue(el.href, value.href)) {
1289
- el.href = "" + value.href;
1290
- }
1291
- }
1292
- function transformElement(context) {
1293
- const el = context.element;
1294
- const value = context.value;
1295
- if (!matchValue(el.innerHTML, value)) {
1296
- el.innerHTML = "" + value;
506
+ return options.view;
1297
507
  }
1298
508
  }
1299
509
 
@@ -1301,10 +511,6 @@
1301
511
  var SimplyApp = class {
1302
512
  constructor(options = {}) {
1303
513
  this.container = options.container || document.body;
1304
- if (!options.state) {
1305
- options.state = {};
1306
- }
1307
- this.state = signal(options.state);
1308
514
  if (options.commands) {
1309
515
  this.commands = commands({ app: this, container: this.container, commands: options.commands });
1310
516
  }
@@ -1317,14 +523,9 @@
1317
523
  if (options.actions) {
1318
524
  this.actions = actions({ app: this, actions: options.actions });
1319
525
  }
1320
- let bindOptions = { container: this.container, root: this.state };
1321
- if (options.defaultTransformers) {
1322
- bindOptions.defaultTransformers = options.defaultTransformers;
1323
- }
1324
- if (options.transformers) {
1325
- bindOptions.transformers = options.transformers;
526
+ if (options.view) {
527
+ this.view = view({ app: this, view: options.view });
1326
528
  }
1327
- this.bind = bind(bindOptions);
1328
529
  }
1329
530
  };
1330
531
  function app(options = {}) {
@@ -1381,7 +582,7 @@
1381
582
  var waitForPreviousScripts = async () => {
1382
583
  return new Promise(function(resolve) {
1383
584
  var next = globalThis.document.createElement("script");
1384
- next.src = rebaseHref("simply.include.next.js", currentScriptURL);
585
+ next.src = "https://cdn.jsdelivr.net/gh/simplyedit/simplyview/dist/simply.include.next.js";
1385
586
  next.async = false;
1386
587
  globalThis.document.addEventListener("simply-include-next", () => {
1387
588
  head.removeChild(next);
@@ -1443,17 +644,19 @@
1443
644
  }
1444
645
  let scriptsFragment = globalThis.document.createDocumentFragment();
1445
646
  const scripts = fragment.querySelectorAll("script");
1446
- for (let script of scripts) {
1447
- let placeholder = globalThis.document.createComment(script.src || "inline script");
1448
- script.parentNode.insertBefore(placeholder, script);
1449
- script.dataset.simplyLocation = scriptLocations.length;
1450
- scriptLocations.push(placeholder);
1451
- scriptsFragment.appendChild(script);
647
+ if (scripts.length) {
648
+ for (let script of scripts) {
649
+ let placeholder = globalThis.document.createComment(script.src || "inline script");
650
+ script.parentNode.insertBefore(placeholder, script);
651
+ script.dataset.simplyLocation = scriptLocations.length;
652
+ scriptLocations.push(placeholder);
653
+ scriptsFragment.appendChild(script);
654
+ }
655
+ globalThis.setTimeout(function() {
656
+ include.scripts(Array.from(scriptsFragment.children), link ? link.href : globalThis.location.href);
657
+ }, 10);
1452
658
  }
1453
659
  link.parentNode.insertBefore(fragment, link ? link : null);
1454
- globalThis.setTimeout(function() {
1455
- include.scripts(scriptsFragment.childNodes, link ? link.href : globalThis.location.href);
1456
- }, 10);
1457
660
  }
1458
661
  };
1459
662
  var included = {};
@@ -1501,157 +704,16 @@
1501
704
  observe();
1502
705
  handleChanges2();
1503
706
 
1504
- // src/model.mjs
1505
- var model_exports = {};
1506
- __export(model_exports, {
1507
- columns: () => columns,
1508
- filter: () => filter,
1509
- model: () => model,
1510
- paging: () => paging,
1511
- sort: () => sort
1512
- });
1513
- var SimplyModel = class {
1514
- /**
1515
- * Creates a new datamodel, with a state property that contains
1516
- * all the data passed to this constructor
1517
- * @param state Object with all the data for this model
1518
- */
1519
- constructor(state) {
1520
- this.state = signal(state);
1521
- if (!this.state.options) {
1522
- this.state.options = {};
1523
- }
1524
- this.effects = [{ current: state.data }];
1525
- this.view = signal(state.data);
1526
- }
1527
- /**
1528
- * Adds an effect to run whenever a signal it depends on
1529
- * changes. this.state is the usual signal.
1530
- * The `fn` function param is not itself an effect, but must return
1531
- * and effect function. `fn` takes one param, which is the data signal.
1532
- * This signal will always have at least a `current` property.
1533
- * The result of the effect function is pushed on to the this.effects
1534
- * list. And the last effect added is set as this.view
1535
- */
1536
- addEffect(fn) {
1537
- const dataSignal = this.effects[this.effects.length - 1];
1538
- this.view = fn.call(this, dataSignal);
1539
- this.effects.push(this.view);
1540
- }
1541
- };
1542
- function model(options) {
1543
- return new SimplyModel(options);
1544
- }
1545
- function sort(options = {}) {
1546
- return function(data) {
1547
- this.state.options.sort = Object.assign({
1548
- direction: "asc",
1549
- sortBy: null,
1550
- sortFn: (a, b) => {
1551
- const sort2 = this.state.options.sort;
1552
- const sortBy = sort2.sortBy;
1553
- if (!sort2.sortBy) {
1554
- return 0;
1555
- }
1556
- const larger = sort2.direction == "asc" ? 1 : -1;
1557
- const smaller = sort2.direction == "asc" ? -1 : 1;
1558
- if (typeof a?.[sortBy] === "undefined") {
1559
- if (typeof b?.[sortBy] === "undefined") {
1560
- return 0;
1561
- }
1562
- return larger;
1563
- }
1564
- if (typeof b?.[sortBy] === "undefined") {
1565
- return smaller;
1566
- }
1567
- if (a[sortBy] < b[sortBy]) {
1568
- return smaller;
1569
- } else if (a[sortBy] > b[sortBy]) {
1570
- return larger;
1571
- } else {
1572
- return 0;
1573
- }
1574
- }
1575
- }, options);
1576
- return effect(() => {
1577
- const sort2 = this.state.options.sort;
1578
- if (sort2?.sortBy && sort2?.direction) {
1579
- return data.current.toSorted(sort2?.sortFn);
1580
- }
1581
- return data.current;
1582
- });
1583
- };
1584
- }
1585
- function paging(options = {}) {
1586
- return function(data) {
1587
- this.state.options.paging = Object.assign({
1588
- page: 1,
1589
- pageSize: 20,
1590
- max: 1
1591
- }, options);
1592
- return effect(() => {
1593
- return batch(() => {
1594
- const paging2 = this.state.options.paging;
1595
- if (!paging2.pageSize) {
1596
- paging2.pageSize = 20;
1597
- }
1598
- paging2.max = Math.ceil(this.state.data.length / paging2.pageSize);
1599
- paging2.page = Math.max(1, Math.min(paging2.max, paging2.page));
1600
- const start = (paging2.page - 1) * paging2.pageSize;
1601
- const end = start + paging2.pageSize;
1602
- return data.current.slice(start, end);
1603
- });
1604
- });
1605
- };
1606
- }
1607
- function filter(options) {
1608
- if (!options?.name || typeof options.name !== "string") {
1609
- throw new Error("filter requires options.name to be a string");
1610
- }
1611
- if (!options.matches || typeof options.matches !== "function") {
1612
- throw new Error("filter requires options.matches to be a function");
1613
- }
1614
- return function(data) {
1615
- this.state.options[options.name] = options;
1616
- return effect(() => {
1617
- if (this.state.options[options.name].enabled) {
1618
- return data.filter(this.state.options.matches);
1619
- }
1620
- });
1621
- };
1622
- }
1623
- function columns(options = {}) {
1624
- if (!options || typeof options !== "object" || Object.keys(options).length === 0) {
1625
- throw new Error("columns requires options to be an object with at least one property");
1626
- }
1627
- return function(data) {
1628
- this.state.options.columns = options;
1629
- return effect(() => {
1630
- return data.current.map((input) => {
1631
- let result = {};
1632
- for (let key of Object.keys(this.state.options.columns)) {
1633
- if (!this.state.options.columns[key].hidden) {
1634
- result[key] = input[key];
1635
- }
1636
- }
1637
- return result;
1638
- });
1639
- });
1640
- };
1641
- }
1642
-
1643
707
  // src/everything.mjs
1644
708
  var simply = {
1645
709
  activate,
1646
- action: action_exports,
710
+ action: actions,
1647
711
  app,
1648
- bind,
1649
- command: command_exports,
712
+ command: commands,
1650
713
  include,
1651
- key: key_exports,
1652
- model: model_exports,
1653
- route: route_exports,
1654
- state: state_exports
714
+ key: keys,
715
+ route: routes,
716
+ view
1655
717
  };
1656
718
  window.simply = simply;
1657
719
  var everything_default = simply;