cbvirtua 1.0.2 → 1.0.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/core/resizer.js CHANGED
@@ -1,6 +1,13 @@
1
1
  import { getCurrentDocument, getCurrentWindow } from "./environment";
2
2
  import { ACTION_ITEM_RESIZE, ACTION_VIEWPORT_RESIZE, } from "./store";
3
3
  import { exists, max } from "./utils";
4
+ // const _ResizeObserver = window.ResizeObserver;
5
+ // window.ResizeObserver = class ResizeObserver extends _ResizeObserver {
6
+ // constructor(callback) {
7
+ // callback = debounce(callback, 20);
8
+ // super(callback);
9
+ // }
10
+ // }
4
11
  const createResizeObserver = (cb) => {
5
12
  let ro;
6
13
  return {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cbvirtua",
3
- "version": "1.0.2",
3
+ "version": "1.0.4",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -0,0 +1,46 @@
1
+ import { isRTLDocument } from "../core/environment";
2
+
3
+ export default {
4
+ props: {
5
+ children: { type: Object, required: true },
6
+ resizer: { type: Function, required: true },
7
+ index: { type: Number, required: true },
8
+ offset: { type: Number, required: true },
9
+ hide: { type: Boolean },
10
+ isHorizontal: { type: Boolean },
11
+ element: { type: String, required: true },
12
+ },
13
+ watch: {
14
+ index(_index) {
15
+ this.$nextTick(() => {
16
+ this.cleanupObserver = null;
17
+ const root = this.$refs.root;
18
+ this.cleanupObserver = this.resizer(root, _index);
19
+ })
20
+ }
21
+ },
22
+ mounted() {
23
+ const root = this.$refs.root;
24
+ this.cleanupObserver = this.resizer(root, this.index);
25
+ },
26
+ render() {
27
+ const { children, offset, hide, element: Element, isHorizontal, } = this;
28
+ console.log(hide)
29
+ const style = {
30
+ margin: 0,
31
+ padding: 0,
32
+ position: "absolute",
33
+ [isHorizontal ? "height" : "width"]: "100%",
34
+ [isHorizontal ? "top" : "left"]: "0px",
35
+ [isHorizontal ? (isRTLDocument() ? "right" : "left") : "top"]: offset + "px",
36
+ visibility: hide ? "hidden" : "visible",
37
+ };
38
+
39
+ if (isHorizontal) {
40
+ style.display = "flex";
41
+ }
42
+ return (<Element ref={"root"} style={style}>
43
+ {children}
44
+ </Element>);
45
+ }
46
+ }
package/vue2/VList.js ADDED
@@ -0,0 +1,161 @@
1
+ import { SCROLL_IDLE, UPDATE_SCROLL_STATE, UPDATE_SCROLL_EVENT, UPDATE_SCROLL_END_EVENT, UPDATE_SIZE_STATE, overscanEndIndex, overscanStartIndex, createVirtualStore, ACTION_ITEMS_LENGTH_CHANGE, getScrollSize, getMinContainerSize, } from "../core/store";
2
+ import { createResizer } from "../core/resizer";
3
+ import { createScroller } from "../core/scroller";
4
+ import { default as ListItem } from "./ListItem";
5
+ import { exists } from "../core/utils";
6
+ const props = {
7
+ data: { type: Array, required: true },
8
+ overscan: { type: Number, default: 4 },
9
+ itemSize: Number,
10
+ shift: Boolean,
11
+ horizontal: Boolean,
12
+ };
13
+ export default {
14
+ props: props,
15
+ data() {
16
+ return {
17
+ store: {},
18
+ rerender: [],
19
+ JumpCount: 0,
20
+ range: []
21
+ }
22
+ },
23
+ watch: {
24
+ data(_data) {
25
+ const count = _data.length;
26
+ this.store._update(ACTION_ITEMS_LENGTH_CHANGE, [count, this.shift]);
27
+ },
28
+ JumpCount(count, prevCount) {
29
+ this.$nextTick(() => {
30
+ if (count === prevCount)
31
+ return;
32
+ const jump = this.store._flushJump();
33
+ if (!jump)
34
+ return;
35
+ this.scroller._fixScrollJump(jump);
36
+ })
37
+ },
38
+ range([start, end], [prevStart, prevEnd]) {
39
+ this.$nextTick(() => {
40
+ if (prevStart === start && prevEnd === end)
41
+ return;
42
+ this.$emit("rangeChange", start, end);
43
+ })
44
+ }
45
+
46
+ },
47
+ created() {
48
+ const isHorizontal = this.horizontal;
49
+ const store = this.store = createVirtualStore(
50
+ this.data.length,
51
+ this.itemSize ?? 40,
52
+ undefined,
53
+ undefined,
54
+ !this.itemSize
55
+ );
56
+ const resizer = this.resizer = createResizer(store, isHorizontal);
57
+ const scroller = this.scroller = createScroller(store, isHorizontal);
58
+ this.scrollToIndex = scroller._scrollToIndex;
59
+ this.scrollTo = scroller._scrollTo;
60
+ this.scrollBy = scroller._scrollBy;
61
+ const rerender = store._getStateVersion();
62
+ const unsubscribeStore = this.unsubscribeStore = store._subscribe(UPDATE_SCROLL_STATE + UPDATE_SIZE_STATE, () => {
63
+ this.rerender = store._getStateVersion();
64
+ this.JumpCount = store._getJumpCount();
65
+ });
66
+ const unsubscribeOnScroll = this.unsubscribeOnScroll = store._subscribe(UPDATE_SCROLL_EVENT, () => {
67
+ this.$emit("scroll", store._getScrollOffset());
68
+ });
69
+ const unsubscribeOnScrollEnd = this.unsubscribeOnScrollEnd = store._subscribe(UPDATE_SCROLL_END_EVENT, () => {
70
+ this.$emit("scrollEnd");
71
+ });
72
+ },
73
+ mounted() {
74
+ const root = this.$refs.root;
75
+ if (!root)
76
+ return;
77
+ this.resizer._observeRoot(root);
78
+ this.scroller._observe(root);
79
+
80
+ },
81
+ methods: {
82
+ scrollOffset() {
83
+ return this.store._getScrollOffset();
84
+ },
85
+ scrollSize() {
86
+ return this.getScrollSize(store);
87
+ },
88
+ viewportSize() {
89
+ return this.store._getViewportSize();
90
+ },
91
+ scrollToIndex() { },
92
+ scrollTo() { },
93
+ scrollBy() { }
94
+ },
95
+ render() {
96
+ this.rerender;
97
+ const count = this.data.length;
98
+ const store = this.store;
99
+ const isHorizontal = this.horizontal;
100
+ const [startIndex, endIndex] = this.store._getRange();
101
+ const scrollDirection = store._getScrollDirection();
102
+ const totalSize = store._getTotalSize();
103
+ // https://github.com/inokawa/virtua/issues/252#issuecomment-1822861368
104
+ const minSize = getMinContainerSize(store);
105
+
106
+ const items = [];
107
+ for (let i = overscanStartIndex(startIndex, this.overscan, scrollDirection), j = overscanEndIndex(endIndex, this.overscan, scrollDirection, count); i <= j; i++) {
108
+ const e = this.$scopedSlots.default(this.data[i])[0];
109
+ const key = e.key;
110
+ items.push(
111
+ <ListItem
112
+ key={exists(key) ? key : "_" + i}
113
+ resizer={this.resizer._observeItem}
114
+ index={i}
115
+ offset={store._getItemOffset(i)}
116
+ hide={store._isUnmeasuredItem(i)}
117
+ element="div"
118
+ children={e}
119
+ isHorizontal={isHorizontal}
120
+ />
121
+ );
122
+ }
123
+
124
+ return (
125
+ <div
126
+ ref={"root"}
127
+ style={{
128
+ display: isHorizontal ? "inline-block" : "block",
129
+ [isHorizontal ? "overflowX" : "overflowY"]: "auto",
130
+ overflowAnchor: "none",
131
+ contain: "strict",
132
+ width: "100%",
133
+ height: "100%",
134
+ }}
135
+ >
136
+ <div
137
+ style={{
138
+ // contain: "content",
139
+ overflowAnchor: "none", // opt out browser's scroll anchoring because it will conflict to scroll anchoring of virtualizer
140
+ flex: "none", // flex style on parent can break layout
141
+ position: "relative",
142
+ visibility: "hidden",
143
+ width: isHorizontal ? totalSize + "px" : "100%",
144
+ height: isHorizontal ? "100%" : totalSize + "px",
145
+ [isHorizontal ? "minWidth" : "minHeight"]: minSize + "px",
146
+ pointerEvents: scrollDirection !== SCROLL_IDLE ? "none" : "auto",
147
+ }}
148
+ >
149
+ {items}
150
+ </div>
151
+ </div>
152
+ );
153
+ },
154
+ destoryed() {
155
+ this.unsubscribeStore();
156
+ this.unsubscribeOnScroll();
157
+ this.unsubscribeOnScrollEnd();
158
+ this.resizer._dispose();
159
+ this.scroller._dispose();
160
+ },
161
+ }