@yasanchezz/vue-infinite-grid 0.2.3 → 0.2.4

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/VueGrid.d.ts CHANGED
@@ -1,7 +1,5 @@
1
- declare const __VLS_export: <GridRow extends {
2
- id: string;
3
- sticky?: boolean;
4
- }>(__VLS_props: NonNullable<Awaited<typeof __VLS_setup>>["props"], __VLS_ctx?: __VLS_PrettifyLocal<Pick<NonNullable<Awaited<typeof __VLS_setup>>, "attrs" | "emit" | "slots">>, __VLS_exposed?: NonNullable<Awaited<typeof __VLS_setup>>["expose"], __VLS_setup?: Promise<{
1
+ import { TableRow } from './types';
2
+ declare const __VLS_export: <GridRow extends TableRow>(__VLS_props: NonNullable<Awaited<typeof __VLS_setup>>["props"], __VLS_ctx?: __VLS_PrettifyLocal<Pick<NonNullable<Awaited<typeof __VLS_setup>>, "attrs" | "emit" | "slots">>, __VLS_exposed?: NonNullable<Awaited<typeof __VLS_setup>>["expose"], __VLS_setup?: Promise<{
5
3
  props: import('vue').PublicProps & __VLS_PrettifyLocal<{
6
4
  container?: (Element | null | undefined) | ((container: Element | null) => Element | null | undefined);
7
5
  rowInitialHeight: number;
@@ -0,0 +1,4 @@
1
+ export type TableRow = {
2
+ id: string;
3
+ sticky?: boolean;
4
+ };
@@ -0,0 +1,2 @@
1
+ export declare function countStickyBefore(array: number[], index: number): number;
2
+ export declare function isSameArray<T>(left: readonly T[], right: readonly T[]): boolean;
@@ -1,2 +1,2 @@
1
- @layer VueGrid{.infinite-grid[data-v-e50d93db]{overflow-anchor:none;align-content:start;width:max-content;display:grid;overflow:clip}.grid-row[data-v-e50d93db]{grid-column:1/-1;grid-template-columns:subgrid;display:grid}.grid-row--sticky[data-v-e50d93db]{position:sticky;top:0}.content[data-v-e50d93db]{display:contents}.before[data-v-e50d93db],.after[data-v-e50d93db]{pointer-events:none;grid-column:1/-1}}
1
+ @layer VueGrid{.infinite-grid[data-v-d94c77db]{overflow-anchor:none;align-content:start;width:max-content;display:grid;overflow:clip}.grid-row[data-v-d94c77db]{grid-column:1/-1;grid-template-columns:subgrid;display:grid}.grid-row--sticky[data-v-d94c77db]{position:sticky;top:0}.content[data-v-d94c77db]{display:contents}.before[data-v-d94c77db],.after[data-v-d94c77db]{pointer-events:none;grid-column:1/-1}}
2
2
  /*$vite$:1*/
@@ -1,6 +1,21 @@
1
- import { Fragment as e, computed as t, createElementBlock as n, createElementVNode as r, defineComponent as i, normalizeClass as a, normalizeStyle as o, onMounted as s, onUnmounted as c, openBlock as l, ref as u, renderList as ee, renderSlot as te, shallowRef as d, useTemplateRef as f, watch as p, watchEffect as m } from "vue";
1
+ import { Fragment as e, computed as t, createElementBlock as n, createElementVNode as r, defineComponent as i, normalizeClass as a, normalizeStyle as o, onMounted as s, onUnmounted as ee, openBlock as c, ref as l, renderList as te, renderSlot as ne, shallowRef as re, useTemplateRef as u, watch as d, watchEffect as f } from "vue";
2
+ //#region src/utils.ts
3
+ function ie(e, t) {
4
+ let n = 0, r = e.length;
5
+ for (; n < r;) {
6
+ let i = n + r >>> 1;
7
+ e[i] < t ? n = i + 1 : r = i;
8
+ }
9
+ return n;
10
+ }
11
+ function ae(e, t) {
12
+ if (e.length !== t.length) return !1;
13
+ for (let [n, r] of e.entries()) if (r !== t[n]) return !1;
14
+ return !0;
15
+ }
16
+ //#endregion
2
17
  //#region src/VueGrid.vue?vue&type=script&setup=true&lang.ts
3
- var h = ["data-index"], g = /*#__PURE__*/ ((e, t) => {
18
+ var p = ["data-index"], m = /*#__PURE__*/ ((e, t) => {
4
19
  let n = e.__vccOpts || e;
5
20
  for (let [e, r] of t) n[e] = r;
6
21
  return n;
@@ -20,163 +35,171 @@ var h = ["data-index"], g = /*#__PURE__*/ ((e, t) => {
20
35
  },
21
36
  rowsOffset: { default: 0 }
22
37
  },
23
- setup(i, { expose: g }) {
24
- let _ = d([]), v = f("grid"), y = f("content"), b = t(() => i.container instanceof Function ? i.container(v.value) ?? document.documentElement : i.container ?? document.documentElement), x = t(() => b.value === document.documentElement || b.value === document.body ? window : b.value), S = /* @__PURE__ */ new Map(), C = i.rowsOffset || i.rowInitialHeight * Math.trunc(i.rowsCount / 5) || 1e3, w, T, E = /* @__PURE__ */ new Map();
25
- function D() {
26
- E = new Map(i.rows.map((e, t) => [e.id, t]));
38
+ setup(i, { expose: m }) {
39
+ let h = re([]), g = u("grid"), _ = u("content"), v = t(() => i.rows.map((e, t) => e.sticky ? t : void 0).filter((e) => e != null)), y = t(() => i.container instanceof Function ? i.container(g.value) ?? document.documentElement : i.container ?? document.documentElement), b = t(() => y.value === document.documentElement || y.value === document.body ? window : y.value), x = /* @__PURE__ */ new Map(), S = i.rowsOffset || i.rowInitialHeight * Math.trunc(i.rowsCount / 5) || 1e3, C, w, T = /* @__PURE__ */ new Map();
40
+ function E() {
41
+ T = new Map(i.rows.map((e, t) => [e.id, t]));
27
42
  }
28
- let O = new Float32Array(i.rows.length);
29
- function k() {
30
- O = new Float32Array(i.rows.map((e) => S.get(e.id) ?? i.rowInitialHeight));
43
+ let D = new Float32Array(i.rows.length);
44
+ function O() {
45
+ D = new Float32Array(i.rows.map((e) => x.get(e.id) ?? i.rowInitialHeight));
31
46
  }
32
- function A(e, t) {
33
- S.set(e, t);
34
- let n = E.get(e);
35
- n != null && (O[n] = t);
47
+ function oe(e, t) {
48
+ x.set(e, t);
49
+ let n = T.get(e);
50
+ n != null && (D[n] = t);
36
51
  }
37
- function j(e) {
38
- return O.at(e) ?? i.rowInitialHeight;
52
+ function k(e) {
53
+ return D.at(e) ?? i.rowInitialHeight;
39
54
  }
40
- let M = i.rowsCount;
41
- function N() {
55
+ let A = i.rowsCount;
56
+ function j() {
42
57
  if (i.rowsCountAutoCalculated) {
43
- let e = window.innerHeight + i.rowInitialHeight, t = z - C, n = t < e ? Math.ceil(e / t * M) : M;
44
- M = Number.isFinite(n) && n >= i.rowsCount ? n : i.rowsCount;
58
+ let e = window.innerHeight + i.rowInitialHeight, t = L - S, n = t < e ? Math.ceil(e / t * A) : A;
59
+ A = Number.isFinite(n) && n >= i.rowsCount ? n : i.rowsCount;
45
60
  }
46
61
  }
47
- let P = 0;
48
- function F() {
49
- let e = 0, t = v.value;
50
- for (; t instanceof HTMLElement && t != b.value;) e += t.offsetTop, t = t.offsetParent;
51
- P = e;
62
+ let M = 0;
63
+ function N() {
64
+ let e = 0, t = g.value;
65
+ for (; t instanceof HTMLElement && t != y.value;) e += t.offsetTop, t = t.offsetParent;
66
+ M = e;
52
67
  }
53
- let I = u({ height: "0px" }), L = 0;
54
- function R() {
55
- L = O.reduce((e, t) => e + t, 0), I.value.height = `${L}px`;
68
+ let P = l({ height: "0px" }), F = 0;
69
+ function I() {
70
+ F = D.reduce((e, t) => e + t, 0), P.value.height = `${F}px`;
56
71
  }
57
- let z = 0, B = 0, V = M;
58
- function H() {
59
- z = O.slice(B, V).reduce((e, t) => e + t, 0);
72
+ let L = 0, R = 0, z = A;
73
+ function B() {
74
+ L = D.slice(R, z).reduce((e, t) => e + t, 0);
60
75
  }
61
- let U = u({ height: "0px" }), W = 0;
62
- function G() {
63
- let e = b.value.scrollTop - C - P, t = 0;
76
+ let V = l({ height: "0px" }), H = 0;
77
+ function U() {
78
+ let e = y.value.scrollTop - S - M, t = 0;
64
79
  for (let n of i.rows.keys()) {
65
- let r = j(n);
80
+ let r = k(n);
66
81
  if (t + r > e) break;
67
82
  t += r;
68
83
  }
69
- let n = L - z;
70
- W = t > n ? n : t;
84
+ let n = F - L;
85
+ H = t > n ? n : t;
71
86
  }
72
- let K = 0;
73
- function q() {
74
- let e = W - K;
75
- e = e < 0 ? 0 : e, U.value.height = `${e}px`;
87
+ let W = 0;
88
+ function G() {
89
+ let e = H - W;
90
+ e = e < 0 ? 0 : e, V.value.height = `${e}px`;
76
91
  }
77
- let J = u({ height: "0px" }), Y = 0;
78
- function X() {
79
- Y = L - W - z, J.value.height = `${Y}px`;
92
+ let K = l({ height: "0px" }), q = 0;
93
+ function J() {
94
+ q = F - H - L, K.value.height = `${q}px`;
80
95
  }
81
- function ne() {
82
- F(), H(), R(), G(), q(), X();
83
- let e = 0, t = 0, n, r;
84
- for (let [a, o] of i.rows.entries()) {
85
- let i = j(a);
86
- if (e += i, e > W) {
87
- t = a;
96
+ function se() {
97
+ N(), B(), I(), U(), G(), J();
98
+ let e = 0, t = 0;
99
+ for (let n of i.rows.keys()) {
100
+ let r = k(n);
101
+ if (e += r, e > H) {
102
+ t = n;
88
103
  break;
89
104
  }
90
- o.sticky && (n = o, r = a, K = i);
91
105
  }
92
- [B, V] = t + M > i.rows.length || !Y ? [i.rows.length - M, i.rows.length] : [t, t + M], B < 0 && (B = 0);
93
- let a = n && r != null && r < B ? [n] : [], o = a.length, s = i.rows.slice(B, V), c = a.concat(s);
94
- o || (K = 0), (c.at(o) !== _.value.at(o) || c.length !== _.value.length) && (_.value = c), H(), R(), G(), q(), X();
106
+ [R, z] = t + A > i.rows.length || !q ? [i.rows.length - A, i.rows.length] : [t, t + A], R < 0 && (R = 0);
107
+ let n = ie(v.value, R), r = v.value.slice(0, n), a = r.map((e) => i.rows.at(e)).filter((e) => e != null);
108
+ W = r.reduce((e, t) => e + k(t), 0);
109
+ let o = i.rows.slice(R, z), s = a.concat(o);
110
+ ae(s, h.value) || (h.value = s), B(), I(), U(), G(), J();
95
111
  }
96
- function Z() {
97
- cancelAnimationFrame(w), w = requestAnimationFrame(ne);
112
+ function Y() {
113
+ cancelAnimationFrame(C), C = requestAnimationFrame(se);
98
114
  }
99
- let Q = new ResizeObserver((e) => {
115
+ let X = new ResizeObserver((e) => {
100
116
  for (let t of e) {
101
117
  let e = t.target, n = t.borderBoxSize.at(0)?.blockSize ?? i.rowInitialHeight;
102
118
  if (e instanceof HTMLElement) {
103
119
  let t = e.dataset.index;
104
- t != null && A(t, n);
120
+ t != null && oe(t, n);
105
121
  }
106
122
  }
107
- cancelAnimationFrame(T), T = requestAnimationFrame(() => {
108
- H(), N(), Z();
123
+ cancelAnimationFrame(w), w = requestAnimationFrame(() => {
124
+ B(), j(), Y();
109
125
  });
110
- }), $ = new MutationObserver((e) => {
111
- for (let t of e) t.addedNodes.values().filter((e) => e instanceof HTMLElement).forEach((e) => Q.observe(e)), t.removedNodes.values().filter((e) => e instanceof HTMLElement).forEach((e) => Q.unobserve(e));
126
+ }), Z = new MutationObserver((e) => {
127
+ for (let t of e) t.addedNodes.values().filter((e) => e instanceof HTMLElement).forEach((e) => X.observe(e)), t.removedNodes.values().filter((e) => e instanceof HTMLElement).forEach((e) => X.unobserve(e));
112
128
  });
113
- m(() => {
114
- $.disconnect(), y.value && $.observe(y.value, { childList: !0 });
115
- }), p(() => i.rows, () => {
116
- D(), k(), Z();
117
- }, { flush: "post" }), p(x, (e, t) => {
118
- t?.removeEventListener("scroll", Z), e.addEventListener("scroll", Z, { passive: !0 });
119
- }, { immediate: !0 }), s(() => {
120
- D(), k(), Z();
121
- }), c(() => {
122
- Q.disconnect(), $.disconnect(), x.value.removeEventListener("scroll", Z), S.clear(), E.clear(), O = /* @__PURE__ */ new Float32Array();
123
- }), g({
129
+ f(() => {
130
+ Z.disconnect(), _.value && Z.observe(_.value, { childList: !0 });
131
+ }), d(() => i.rows, () => {
132
+ E(), O(), Y();
133
+ }, { flush: "post" }), d(b, (e, t, n) => {
134
+ e.addEventListener("scroll", Y, { passive: !0 }), n(() => e.removeEventListener("scroll", Y));
135
+ }, { immediate: !0 });
136
+ let Q = /* @__PURE__ */ new Set();
137
+ function $(e) {
138
+ let t = _.value?.querySelector(`[data-index="${CSS.escape(e)}"]`);
139
+ return t instanceof HTMLElement ? t : null;
140
+ }
141
+ s(() => {
142
+ E(), O(), Y();
143
+ }), ee(() => {
144
+ cancelAnimationFrame(C), cancelAnimationFrame(w), X.disconnect(), Z.disconnect(), Q.forEach((e) => e()), x.clear(), T.clear(), D = /* @__PURE__ */ new Float32Array();
145
+ }), m({
124
146
  init() {
125
- H(), N(), Z();
147
+ B(), j(), Y();
126
148
  },
127
149
  async scrollTo(e) {
128
- let t = 0;
129
- for (let [n, r] of i.rows.entries()) {
130
- if (r.id === e) break;
131
- t += j(n);
132
- }
133
- return b.value.scrollTo(0, t + P), new Promise((t) => {
134
- let n = b.value.querySelector(`[data-index="${e}"]`);
135
- if (n instanceof HTMLElement) return t(n);
136
- let r = new MutationObserver((n) => {
137
- let i = n.flatMap((e) => Array.from(e.addedNodes)).filter((e) => e instanceof HTMLElement).find((t) => t.dataset.index === e);
138
- i && (t(i), r.disconnect());
139
- });
140
- m(() => {
141
- r.disconnect(), y.value && r.observe(y.value, { childList: !0 });
142
- }), c(() => r.disconnect());
150
+ let t = T.get(e);
151
+ if (t == null) throw Error(`VueGrid: row with id "${e}" not found`);
152
+ let n = D.subarray(0, t).reduce((e, t) => e + t, 0);
153
+ return y.value.scrollTo(0, n + M), new Promise((t, n) => {
154
+ let r = $(e);
155
+ if (r) return t(r);
156
+ if (!_.value) return n(/* @__PURE__ */ Error("VueGrid: component is not mounted"));
157
+ let i = new MutationObserver(() => {
158
+ let n = $(e);
159
+ n && (a(), t(n));
160
+ }), a = () => {
161
+ i.disconnect(), Q.delete(o);
162
+ }, o = () => {
163
+ a(), n(/* @__PURE__ */ Error("VueGrid: component was unmounted before the row was rendered"));
164
+ };
165
+ Q.add(o), i.observe(_.value, { childList: !0 });
143
166
  });
144
167
  }
145
168
  });
146
- function re(e) {
169
+ function ce(e) {
147
170
  let t = "grid-row";
148
171
  return {
149
172
  [t]: !0,
150
173
  [`${t}--sticky`]: e.sticky
151
174
  };
152
175
  }
153
- return (t, i) => (l(), n("div", {
176
+ return (t, i) => (c(), n("div", {
154
177
  ref_key: "grid",
155
- ref: v,
178
+ ref: g,
156
179
  class: "infinite-grid",
157
- style: o(I.value)
180
+ style: o(P.value)
158
181
  }, [
159
182
  r("div", {
160
183
  class: "before",
161
- style: o(U.value)
184
+ style: o(V.value)
162
185
  }, null, 4),
163
186
  r("div", {
164
187
  ref_key: "content",
165
- ref: y,
188
+ ref: _,
166
189
  class: "content"
167
- }, [(l(!0), n(e, null, ee(_.value, (e) => (l(), n("div", {
190
+ }, [(c(!0), n(e, null, te(h.value, (e) => (c(), n("div", {
168
191
  key: e.id,
169
- class: a(re(e)),
192
+ class: a(ce(e)),
170
193
  "data-index": e.id
171
- }, [te(t.$slots, "row", { row: e }, void 0, !0)], 10, h))), 128))], 512),
194
+ }, [ne(t.$slots, "row", { row: e }, void 0, !0)], 10, p))), 128))], 512),
172
195
  r("div", {
173
196
  class: "after",
174
- style: o(J.value)
197
+ style: o(K.value)
175
198
  }, null, 4)
176
199
  ], 4));
177
200
  }
178
- }), [["__scopeId", "data-v-e50d93db"]]), _ = { install(e) {
179
- e.component("VueGrid", g);
201
+ }), [["__scopeId", "data-v-d94c77db"]]), h = { install(e) {
202
+ e.component("VueGrid", m);
180
203
  } };
181
204
  //#endregion
182
- export { g as VueGrid, _ as default };
205
+ export { m as VueGrid, h as default };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yasanchezz/vue-infinite-grid",
3
- "version": "0.2.3",
3
+ "version": "0.2.4",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "dist"