hyper-scope-rails 1.0.0.alpha.6 → 1.0.0.alpha.7

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.
@@ -2536,6 +2536,7 @@ var _for = (el, exp, ctx) => {
2536
2536
  };
2537
2537
 
2538
2538
  // src/directives/ref.ts
2539
+ var told = false;
2539
2540
  var ref2 = ({
2540
2541
  el,
2541
2542
  ctx: {
@@ -2544,6 +2545,10 @@ var ref2 = ({
2544
2545
  get,
2545
2546
  effect: effect3
2546
2547
  }) => {
2548
+ if (!told) {
2549
+ told = true;
2550
+ console.error(`hyper-scope: ref · $refs are going away. Find the element by selector — $find('[name=memo]') searches this scope. They still work for now.`);
2551
+ }
2547
2552
  let prevRef;
2548
2553
  effect3(() => {
2549
2554
  const ref3 = get();
@@ -2558,6 +2563,240 @@ var ref2 = ({
2558
2563
  };
2559
2564
  };
2560
2565
 
2566
+ // src/shared/functions.ts
2567
+ var isPlainObject2 = function(data) {
2568
+ return typeof data === "object" && Boolean(data) && data.constructor === Object;
2569
+ };
2570
+ var splitCaseString = (s, c = undefined) => {
2571
+ if (typeof c === "string")
2572
+ return s.split(c);
2573
+ if (typeof s !== "string")
2574
+ return [String(s)];
2575
+ s = s.replace(/^#/, "");
2576
+ const k = s.split("-");
2577
+ if (k.length > 1)
2578
+ return k;
2579
+ const _ = s.split("_");
2580
+ if (_.length > 1)
2581
+ return _;
2582
+ return s.replace(/[A-Z][a-z]/g, function(s2) {
2583
+ return "%@" + s2;
2584
+ }).replace(/^%@/, "").split("%@");
2585
+ };
2586
+ var toKebabCase = (s) => {
2587
+ const words = splitCaseString(s);
2588
+ for (let i = 0, l = words.length;i < l; i++)
2589
+ words[i] = words[i].toLowerCase();
2590
+ return words.join("-");
2591
+ };
2592
+
2593
+ // src/shared/domHelper.ts
2594
+ var attrPart = (parts, obj, key, some, one) => {
2595
+ if (!Object.prototype.hasOwnProperty.call(obj, key))
2596
+ return;
2597
+ const value = obj[key];
2598
+ if (value === null)
2599
+ parts.push(some);
2600
+ else if (typeof value === "string" && value.length > 0)
2601
+ parts.push(one(value));
2602
+ };
2603
+ var classPart = (parts, obj, key, one) => {
2604
+ if (!Object.prototype.hasOwnProperty.call(obj, key))
2605
+ return;
2606
+ const value = obj[key];
2607
+ if (value === null)
2608
+ parts.push("[class]");
2609
+ else if (typeof value === "string" && value.length > 0) {
2610
+ value.split(/\s+/).filter((c) => c.length > 0).forEach((c) => parts.push(one(c)));
2611
+ }
2612
+ };
2613
+ var dataPart = (parts, obj, key, op) => {
2614
+ if (!Object.prototype.hasOwnProperty.call(obj, key))
2615
+ return;
2616
+ const data = obj[key];
2617
+ if (data === null)
2618
+ parts.push("[data]");
2619
+ else if (isPlainObject2(data)) {
2620
+ for (const [name, value] of Object.entries(data)) {
2621
+ const dataKey = toKebabCase(name);
2622
+ if (value === null)
2623
+ parts.push(`[data-${dataKey}]`);
2624
+ else if (value !== undefined)
2625
+ parts.push(`[data-${dataKey}${op}"${value}"]`);
2626
+ }
2627
+ }
2628
+ };
2629
+ function querySelectorStringByObject(obj) {
2630
+ const parts = [];
2631
+ attrPart(parts, obj, "id", "[id]", (v) => `#${v}`);
2632
+ attrPart(parts, obj, "name", "[name]", (v) => `[name="${v}"]`);
2633
+ attrPart(parts, obj, "form", "[form]", (v) => `[form="${v}"]`);
2634
+ classPart(parts, obj, "class", (c) => `.${c}`);
2635
+ classPart(parts, obj, "class~", (c) => `[class~="${c}"]`);
2636
+ dataPart(parts, obj, "data", "=");
2637
+ dataPart(parts, obj, "data~", "~=");
2638
+ if (parts.length === 0) {
2639
+ console.warn("hyper-scope: the object gave no selector (id · name · form · class · data).", obj);
2640
+ return null;
2641
+ }
2642
+ return parts.join("");
2643
+ }
2644
+ var isElement = (it) => typeof Element !== "undefined" && it instanceof Element;
2645
+ function selectElement(selector, callback) {
2646
+ let foundNode = null;
2647
+ if (isElement(selector)) {
2648
+ foundNode = selector;
2649
+ } else if (typeof selector === "string") {
2650
+ foundNode = document.querySelector(selector);
2651
+ } else if (isPlainObject2(selector)) {
2652
+ const selectorString = querySelectorStringByObject(selector);
2653
+ if (!selectorString)
2654
+ return null;
2655
+ foundNode = document.querySelector(selectorString);
2656
+ } else if (Array.isArray(selector)) {
2657
+ let currentContext = document;
2658
+ let items = selector;
2659
+ if (items.length > 0) {
2660
+ const firstItem = items[0];
2661
+ if (isElement(firstItem) || firstItem === document) {
2662
+ currentContext = firstItem;
2663
+ items = items.slice(1);
2664
+ } else if (isPlainObject2(firstItem)) {
2665
+ const selectorString = querySelectorStringByObject(firstItem);
2666
+ const foundContext = selectorString ? document.querySelector(selectorString) : null;
2667
+ if (!foundContext)
2668
+ return null;
2669
+ currentContext = foundContext;
2670
+ items = items.slice(1);
2671
+ }
2672
+ }
2673
+ for (let i = 0;i < items.length; i++) {
2674
+ const currentSelector = items[i];
2675
+ const isLastItem = i === items.length - 1;
2676
+ if (isElement(currentSelector)) {
2677
+ if (isLastItem && !currentContext.contains(currentSelector))
2678
+ return null;
2679
+ currentContext = currentSelector;
2680
+ } else if (typeof currentSelector === "string") {
2681
+ const nextNode = currentContext.querySelector(currentSelector);
2682
+ if (!nextNode)
2683
+ return null;
2684
+ currentContext = nextNode;
2685
+ } else if (isPlainObject2(currentSelector)) {
2686
+ const selectorString = querySelectorStringByObject(currentSelector);
2687
+ const nextNode = selectorString ? currentContext.querySelector(selectorString) : null;
2688
+ if (!nextNode)
2689
+ return null;
2690
+ currentContext = nextNode;
2691
+ } else {
2692
+ console.warn("hyper-scope: this kind of selector is not supported.", currentSelector);
2693
+ return null;
2694
+ }
2695
+ }
2696
+ foundNode = currentContext === document ? document.documentElement : currentContext;
2697
+ } else if (selector === document) {
2698
+ foundNode = document.documentElement;
2699
+ } else {
2700
+ console.warn("hyper-scope: this kind of selector is not supported.", selector);
2701
+ return null;
2702
+ }
2703
+ if (typeof callback === "function")
2704
+ return foundNode ? callback(foundNode) : null;
2705
+ return foundNode;
2706
+ }
2707
+ var queryAll = (context, selector) => {
2708
+ try {
2709
+ return Array.from(context.querySelectorAll(selector));
2710
+ } catch (error) {
2711
+ console.error(error);
2712
+ return [];
2713
+ }
2714
+ };
2715
+ function selectElements(selector, callback) {
2716
+ let foundNodes = [];
2717
+ if (Array.isArray(selector)) {
2718
+ if (selector.length > 0) {
2719
+ let contexts = [];
2720
+ const firstItem = selector[0];
2721
+ if (isElement(firstItem)) {
2722
+ contexts = [firstItem];
2723
+ } else if (firstItem === document) {
2724
+ contexts = [document];
2725
+ } else if (typeof firstItem === "string") {
2726
+ contexts = queryAll(document, firstItem);
2727
+ } else if (isPlainObject2(firstItem)) {
2728
+ const selectorString = querySelectorStringByObject(firstItem);
2729
+ contexts = selectorString ? queryAll(document, selectorString) : [];
2730
+ } else {
2731
+ console.warn("hyper-scope: this kind of selector is not supported.", firstItem);
2732
+ contexts = [document];
2733
+ }
2734
+ for (let i = 1;i < selector.length; i++) {
2735
+ const currentSelector = selector[i];
2736
+ const newContexts = [];
2737
+ const isLastItem = i === selector.length - 1;
2738
+ for (const context of contexts) {
2739
+ if (isElement(currentSelector)) {
2740
+ if (!isLastItem || context.contains(currentSelector))
2741
+ newContexts.push(currentSelector);
2742
+ } else if (typeof currentSelector === "string") {
2743
+ newContexts.push(...queryAll(context, currentSelector));
2744
+ } else if (isPlainObject2(currentSelector)) {
2745
+ const selectorString = querySelectorStringByObject(currentSelector);
2746
+ if (selectorString)
2747
+ newContexts.push(...queryAll(context, selectorString));
2748
+ } else {
2749
+ console.warn("hyper-scope: this kind of selector is not supported.", currentSelector);
2750
+ }
2751
+ }
2752
+ contexts = newContexts;
2753
+ }
2754
+ foundNodes = contexts.map((c) => c === document ? document.documentElement : c);
2755
+ }
2756
+ } else if (isElement(selector)) {
2757
+ foundNodes = [selector];
2758
+ } else if (isPlainObject2(selector)) {
2759
+ const selectorString = querySelectorStringByObject(selector);
2760
+ foundNodes = selectorString ? queryAll(document, selectorString) : [];
2761
+ } else if (selector === document) {
2762
+ foundNodes = [document.documentElement];
2763
+ } else if (typeof selector === "string") {
2764
+ foundNodes = queryAll(document, selector);
2765
+ } else {
2766
+ console.warn("hyper-scope: this kind of selector is not supported.", selector);
2767
+ }
2768
+ if (typeof callback === "function") {
2769
+ return foundNodes.map((foundNode) => {
2770
+ const callbackResult = callback(foundNode);
2771
+ return typeof callbackResult === "undefined" ? foundNode : callbackResult;
2772
+ });
2773
+ }
2774
+ return foundNodes;
2775
+ }
2776
+
2777
+ // src/select.ts
2778
+ var select = (selector, callback) => selectElement(selector, callback);
2779
+ var selects = (selector, callback) => selectElements(selector, callback);
2780
+ var from = (root, selector) => [root, ...Array.isArray(selector) ? selector : [selector]];
2781
+ var scopeFinders = (root) => ({
2782
+ $find: (selector, callback) => {
2783
+ const found = selectElement(from(root, selector));
2784
+ const inside = found && root.contains(found) ? found : null;
2785
+ if (typeof callback === "function")
2786
+ return inside ? callback(inside) : null;
2787
+ return inside;
2788
+ },
2789
+ $finds: (selector, callback) => {
2790
+ const found = selectElements(from(root, selector)).filter((el) => root.contains(el));
2791
+ if (typeof callback !== "function")
2792
+ return found;
2793
+ return found.map((el) => {
2794
+ const result = callback(el);
2795
+ return typeof result === "undefined" ? el : result;
2796
+ });
2797
+ }
2798
+ });
2799
+
2561
2800
  // src/mirror.ts
2562
2801
  var mirror = (value) => {
2563
2802
  if (value === null || typeof value !== "object")
@@ -2565,7 +2804,7 @@ var mirror = (value) => {
2565
2804
  const json = JSON.stringify(value);
2566
2805
  return json === undefined ? undefined : JSON.parse(json);
2567
2806
  };
2568
- var hyperTools = Object.freeze({ nextTick, reactive, mirror });
2807
+ var hyperTools = Object.freeze({ nextTick, reactive, mirror, select, selects });
2569
2808
 
2570
2809
  // src/async.ts
2571
2810
  var asyncStatuses = ["idle", "busy", "resolved", "rejected"];
@@ -2786,7 +3025,7 @@ var injectInto = (scope, tokens, provides) => {
2786
3025
  };
2787
3026
  var provideRE = /^h-provide(-json)?:(.+)$/;
2788
3027
  var computedRE = /^h-computed:(.+)$/;
2789
- var isPlainObject2 = (value) => value !== null && typeof value === "object" && !Array.isArray(value);
3028
+ var isPlainObject3 = (value) => value !== null && typeof value === "object" && !Array.isArray(value);
2790
3029
  var readJson = (el, attr) => {
2791
3030
  const raw = el.getAttribute(attr) || "";
2792
3031
  try {
@@ -2804,7 +3043,7 @@ var readScopeJson = (el) => {
2804
3043
  return null;
2805
3044
  const data = readJson(el, "h-scope-json");
2806
3045
  el.removeAttribute("h-scope-json");
2807
- if (isPlainObject2(data))
3046
+ if (isPlainObject3(data))
2808
3047
  return data;
2809
3048
  console.error(`hyper-scope: h-scope-json takes a JSON object — the scope's initial state.`);
2810
3049
  return null;
@@ -2877,7 +3116,7 @@ var applyProvides = (el, ctx, keep = false) => {
2877
3116
  const jsonAttr = `h-provide-json:${hyphenate(key)}`;
2878
3117
  const state = json ? (() => {
2879
3118
  const data = readJson(el, attrNameOf(el, jsonAttr));
2880
- if (isPlainObject2(data))
3119
+ if (isPlainObject3(data))
2881
3120
  return data;
2882
3121
  console.error(`hyper-scope: ${jsonAttr} takes a JSON object — the provider's initial state.`);
2883
3122
  return {};
@@ -2931,9 +3170,9 @@ var makeProvider = (el, key, exp, state, ctx, out) => {
2931
3170
  inject
2932
3171
  };
2933
3172
  result = es.run(() => result.call(place, tools));
2934
- return isPlainObject2(result) ? reactive(result) : result;
3173
+ return isPlainObject3(result) ? reactive(result) : result;
2935
3174
  }
2936
- if (isPlainObject2(result))
3175
+ if (isPlainObject3(result))
2937
3176
  return reactive(state ? layOver(el, state, result, hyphenate(key)) : result);
2938
3177
  if (state) {
2939
3178
  result === undefined || console.error(`hyper-scope: h-provide:${hyphenate(key)} — with h-provide-json the expression must give an object or a setup function.`);
@@ -3060,6 +3299,7 @@ var openScope = (el, parent, inherit, inject) => {
3060
3299
  });
3061
3300
  const props = reactive(readProps(el));
3062
3301
  bindProps(el, parentScope, props, (fn) => ctx.effect(fn));
3302
+ const finders = scopeFinders(el);
3063
3303
  defineScopeHelpers(scope, {
3064
3304
  $scope: { value: scope },
3065
3305
  $host: { value: host },
@@ -3073,6 +3313,8 @@ var openScope = (el, parent, inherit, inject) => {
3073
3313
  $emit: {
3074
3314
  value: (name, detail, bubbles = true) => el.dispatchEvent(new CustomEvent(name, { detail, bubbles, cancelable: true }))
3075
3315
  },
3316
+ $find: { value: finders.$find },
3317
+ $finds: { value: finders.$finds },
3076
3318
  $dispatch: {
3077
3319
  value: (target, name, detail = null, bubbles = true) => dispatchTo(target, name, detail, bubbles)
3078
3320
  },
@@ -4207,11 +4449,11 @@ var moveInto = (current, value) => {
4207
4449
  return true;
4208
4450
  }
4209
4451
  if (isReactive(current) && value && typeof value === "object" && !isRef(value)) {
4210
- const from = toRaw(value);
4211
- if (Array.isArray(current) && Array.isArray(from))
4212
- current.splice(0, current.length, ...from);
4213
- else if (!Array.isArray(current) && !Array.isArray(from))
4214
- Object.assign(current, from);
4452
+ const from2 = toRaw(value);
4453
+ if (Array.isArray(current) && Array.isArray(from2))
4454
+ current.splice(0, current.length, ...from2);
4455
+ else if (!Array.isArray(current) && !Array.isArray(from2))
4456
+ Object.assign(current, from2);
4215
4457
  else
4216
4458
  return false;
4217
4459
  return true;
@@ -4386,6 +4628,8 @@ var reserved = new Set([
4386
4628
  "$inject",
4387
4629
  "$async",
4388
4630
  "$await",
4631
+ "$find",
4632
+ "$finds",
4389
4633
  "$refs",
4390
4634
  "$nextTick",
4391
4635
  "$hyper",
@@ -4585,6 +4829,8 @@ var stop2 = () => {
4585
4829
  export {
4586
4830
  stop2 as stop,
4587
4831
  start,
4832
+ selects,
4833
+ select,
4588
4834
  ref,
4589
4835
  reactive,
4590
4836
  plugin,
@@ -4,6 +4,6 @@ module HyperScope
4
4
  module Rails
5
5
  # The single source of the gem version. Write prereleases with dots (1.0.0.alpha.1) —
6
6
  # RubyGems rewrites a hyphen as ".pre.". A published version can never be reused, so bump it before releasing.
7
- VERSION = "1.0.0.alpha.6"
7
+ VERSION = "1.0.0.alpha.7"
8
8
  end
9
9
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hyper-scope-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0.alpha.6
4
+ version: 1.0.0.alpha.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - super-loops