@v-c/virtual-list 1.0.4 → 1.0.5

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/List.cjs CHANGED
@@ -143,7 +143,7 @@ var List_default = /* @__PURE__ */ (0, vue.defineComponent)({
143
143
  fillerOffset.value = 0;
144
144
  return;
145
145
  }
146
- if (heights.id === 0) {
146
+ if (heights.id.value === 0) {
147
147
  const safeItemHeight = itemHeight$1;
148
148
  const safeListHeight = height;
149
149
  const startIndex$1 = Math.max(0, Math.floor(offsetTop.value / safeItemHeight));
@@ -373,7 +373,7 @@ var List_default = /* @__PURE__ */ (0, vue.defineComponent)({
373
373
  offsetX: offsetLeft.value,
374
374
  offsetY: fillerOffset.value || 0,
375
375
  rtl: isRTL.value,
376
- getSize: getSize.value
376
+ getSize
377
377
  });
378
378
  const Component = props.component;
379
379
  return (0, vue.createVNode)("div", (0, vue.mergeProps)({ "ref": containerRef }, (0, _v_c_util_dist_props_util.pureAttrs)(attrs), {
package/dist/List.js CHANGED
@@ -137,7 +137,7 @@ var List_default = /* @__PURE__ */ defineComponent({
137
137
  fillerOffset.value = 0;
138
138
  return;
139
139
  }
140
- if (heights.id === 0) {
140
+ if (heights.id.value === 0) {
141
141
  const safeItemHeight = itemHeight$1;
142
142
  const safeListHeight = height;
143
143
  const startIndex$1 = Math.max(0, Math.floor(offsetTop.value / safeItemHeight));
@@ -367,7 +367,7 @@ var List_default = /* @__PURE__ */ defineComponent({
367
367
  offsetX: offsetLeft.value,
368
368
  offsetY: fillerOffset.value || 0,
369
369
  rtl: isRTL.value,
370
- getSize: getSize.value
370
+ getSize
371
371
  });
372
372
  const Component = props.component;
373
373
  return createVNode("div", mergeProps({ "ref": containerRef }, pureAttrs(attrs), {
@@ -2,29 +2,37 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
2
  const require_rolldown_runtime = require("../_virtual/rolldown_runtime.cjs");
3
3
  let vue = require("vue");
4
4
  function useGetSize(mergedData, getKey, heights, itemHeight) {
5
- return (0, vue.computed)(() => {
6
- return (startKey, endKey) => {
7
- let topIndex = 0;
8
- let bottomIndex = mergedData.value.length - 1;
9
- if (startKey !== void 0 && startKey !== null) topIndex = mergedData.value.findIndex((item) => getKey(item) === startKey);
10
- if (endKey !== void 0 && endKey !== null) bottomIndex = mergedData.value.findIndex((item) => getKey(item) === endKey);
11
- let top = 0;
12
- for (let i = 0; i < topIndex; i += 1) {
13
- const key = getKey(mergedData.value[i]);
14
- const cacheHeight = heights.get(key);
15
- top += cacheHeight === void 0 ? itemHeight.value : cacheHeight;
16
- }
17
- let bottom = 0;
18
- for (let i = mergedData.value.length - 1; i > bottomIndex; i -= 1) {
19
- const key = getKey(mergedData.value[i]);
20
- const cacheHeight = heights.get(key);
21
- bottom += cacheHeight === void 0 ? itemHeight.value : cacheHeight;
5
+ let key2Index = /* @__PURE__ */ new Map();
6
+ let bottomList = [];
7
+ (0, vue.watch)([
8
+ mergedData,
9
+ () => heights.id.value,
10
+ itemHeight
11
+ ], () => {
12
+ key2Index = /* @__PURE__ */ new Map();
13
+ bottomList = [];
14
+ });
15
+ const getSize = (startKey, endKey = startKey) => {
16
+ let startIndex = key2Index.get(startKey);
17
+ let endIndex = key2Index.get(endKey);
18
+ if (startIndex === void 0 || endIndex === void 0) {
19
+ const dataLen = mergedData.value.length;
20
+ for (let i = bottomList.length; i < dataLen; i += 1) {
21
+ const item = mergedData.value[i];
22
+ const key = getKey(item);
23
+ key2Index.set(key, i);
24
+ const cacheHeight = heights.get(key) ?? itemHeight.value;
25
+ bottomList[i] = (bottomList[i - 1] || 0) + cacheHeight;
26
+ if (key === startKey) startIndex = i;
27
+ if (key === endKey) endIndex = i;
28
+ if (startIndex !== void 0 && endIndex !== void 0) break;
22
29
  }
23
- return {
24
- top,
25
- bottom
26
- };
30
+ }
31
+ return {
32
+ top: bottomList[startIndex - 1] || 0,
33
+ bottom: bottomList[endIndex]
27
34
  };
28
- });
35
+ };
36
+ return getSize;
29
37
  }
30
38
  exports.useGetSize = useGetSize;
@@ -1,7 +1,4 @@
1
- import { ComputedRef, Ref } from 'vue';
2
- import { GetKey } from '../interface';
1
+ import { Ref } from 'vue';
2
+ import { GetKey, GetSize } from '../interface';
3
3
  import { default as CacheMap } from '../utils/CacheMap';
4
- export declare function useGetSize<T>(mergedData: Ref<T[]>, getKey: GetKey<T>, heights: CacheMap, itemHeight: Ref<number>): ComputedRef<(startKey: any, endKey?: any) => {
5
- top: number;
6
- bottom: number;
7
- }>;
4
+ export declare function useGetSize<T>(mergedData: Ref<T[]>, getKey: GetKey<T>, heights: CacheMap, itemHeight: Ref<number>): GetSize;
@@ -1,28 +1,36 @@
1
- import { computed } from "vue";
1
+ import { watch } from "vue";
2
2
  function useGetSize(mergedData, getKey, heights, itemHeight) {
3
- return computed(() => {
4
- return (startKey, endKey) => {
5
- let topIndex = 0;
6
- let bottomIndex = mergedData.value.length - 1;
7
- if (startKey !== void 0 && startKey !== null) topIndex = mergedData.value.findIndex((item) => getKey(item) === startKey);
8
- if (endKey !== void 0 && endKey !== null) bottomIndex = mergedData.value.findIndex((item) => getKey(item) === endKey);
9
- let top = 0;
10
- for (let i = 0; i < topIndex; i += 1) {
11
- const key = getKey(mergedData.value[i]);
12
- const cacheHeight = heights.get(key);
13
- top += cacheHeight === void 0 ? itemHeight.value : cacheHeight;
14
- }
15
- let bottom = 0;
16
- for (let i = mergedData.value.length - 1; i > bottomIndex; i -= 1) {
17
- const key = getKey(mergedData.value[i]);
18
- const cacheHeight = heights.get(key);
19
- bottom += cacheHeight === void 0 ? itemHeight.value : cacheHeight;
3
+ let key2Index = /* @__PURE__ */ new Map();
4
+ let bottomList = [];
5
+ watch([
6
+ mergedData,
7
+ () => heights.id.value,
8
+ itemHeight
9
+ ], () => {
10
+ key2Index = /* @__PURE__ */ new Map();
11
+ bottomList = [];
12
+ });
13
+ const getSize = (startKey, endKey = startKey) => {
14
+ let startIndex = key2Index.get(startKey);
15
+ let endIndex = key2Index.get(endKey);
16
+ if (startIndex === void 0 || endIndex === void 0) {
17
+ const dataLen = mergedData.value.length;
18
+ for (let i = bottomList.length; i < dataLen; i += 1) {
19
+ const item = mergedData.value[i];
20
+ const key = getKey(item);
21
+ key2Index.set(key, i);
22
+ const cacheHeight = heights.get(key) ?? itemHeight.value;
23
+ bottomList[i] = (bottomList[i - 1] || 0) + cacheHeight;
24
+ if (key === startKey) startIndex = i;
25
+ if (key === endKey) endIndex = i;
26
+ if (startIndex !== void 0 && endIndex !== void 0) break;
20
27
  }
21
- return {
22
- top,
23
- bottom
24
- };
28
+ }
29
+ return {
30
+ top: bottomList[startIndex - 1] || 0,
31
+ bottom: bottomList[endIndex]
25
32
  };
26
- });
33
+ };
34
+ return getSize;
27
35
  }
28
36
  export { useGetSize };
@@ -2,9 +2,11 @@ Object.defineProperties(exports, {
2
2
  __esModule: { value: true },
3
3
  [Symbol.toStringTag]: { value: "Module" }
4
4
  });
5
+ const require_rolldown_runtime = require("../_virtual/rolldown_runtime.cjs");
6
+ let vue = require("vue");
5
7
  var CacheMap = class {
6
8
  maps;
7
- id = 0;
9
+ id = (0, vue.shallowRef)(0);
8
10
  diffRecords = /* @__PURE__ */ new Map();
9
11
  constructor() {
10
12
  this.maps = Object.create(null);
@@ -12,7 +14,7 @@ var CacheMap = class {
12
14
  set(key, value) {
13
15
  this.diffRecords.set(key, this.maps[key]);
14
16
  this.maps[key] = value;
15
- this.id += 1;
17
+ this.id.value += 1;
16
18
  }
17
19
  get(key) {
18
20
  return this.maps[key];
@@ -1,7 +1,7 @@
1
1
  import { Key } from '@v-c/util/dist/type';
2
2
  declare class CacheMap {
3
3
  maps: Record<string, number>;
4
- id: number;
4
+ id: import('vue').ShallowRef<number, number>;
5
5
  diffRecords: Map<Key, number>;
6
6
  constructor();
7
7
  set(key: Key, value: number): void;
@@ -1,6 +1,7 @@
1
+ import { shallowRef } from "vue";
1
2
  var CacheMap = class {
2
3
  maps;
3
- id = 0;
4
+ id = shallowRef(0);
4
5
  diffRecords = /* @__PURE__ */ new Map();
5
6
  constructor() {
6
7
  this.maps = Object.create(null);
@@ -8,7 +9,7 @@ var CacheMap = class {
8
9
  set(key, value) {
9
10
  this.diffRecords.set(key, this.maps[key]);
10
11
  this.maps[key] = value;
11
- this.id += 1;
12
+ this.id.value += 1;
12
13
  }
13
14
  get(key) {
14
15
  return this.maps[key];
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@v-c/virtual-list",
3
3
  "type": "module",
4
- "version": "1.0.4",
4
+ "version": "1.0.5",
5
5
  "description": "Vue Virtual List Component",
6
6
  "author": "",
7
7
  "license": "MIT",
@@ -32,7 +32,7 @@
32
32
  },
33
33
  "dependencies": {
34
34
  "@v-c/resize-observer": "^1.0.3",
35
- "@v-c/util": "^1.0.4"
35
+ "@v-c/util": "^1.0.5"
36
36
  },
37
37
  "scripts": {
38
38
  "build": "vite build",