qiling-web-virtual-list 0.0.0-stage → 0.1.2
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/LICENSE +21 -0
- package/README.md +32 -2
- package/dist/index.js +26 -0
- package/dist/types/index.d.ts +3 -0
- package/dist/types/ql-virtual-list.d.ts +81 -0
- package/dist/types/types.d.ts +8 -0
- package/package.json +55 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 炼器师
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,33 @@
|
|
|
1
|
-
#
|
|
1
|
+
# qiling-web-virtual-list
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
QiLing Web 虚拟列表拓展包。只渲染可视区与缓冲区的虚拟滚动列表。
|
|
4
|
+
|
|
5
|
+
## 安装
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install qiling-web qiling-web-virtual-list
|
|
9
|
+
# 或
|
|
10
|
+
bun add qiling-web qiling-web-virtual-list
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
> `qiling-web-virtual-list` 通过 peer dependency 使用 `qiling-web`,请同时安装核心包。
|
|
14
|
+
|
|
15
|
+
## 使用
|
|
16
|
+
|
|
17
|
+
```js
|
|
18
|
+
import 'qiling-web'
|
|
19
|
+
import 'qiling-web-virtual-list'
|
|
20
|
+
|
|
21
|
+
// 导入即注册:ql-virtual-list
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
组件标签:`ql-virtual-list`。
|
|
25
|
+
|
|
26
|
+
## 文档
|
|
27
|
+
|
|
28
|
+
- [QiLing Web 文档](http://47.93.3.131:18105/docs/)
|
|
29
|
+
- [源码仓库](https://gitee.com/i-o-i/qiling-web)
|
|
30
|
+
|
|
31
|
+
## License
|
|
32
|
+
|
|
33
|
+
MIT
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import{Registry as p}from"qiling-web/internal";import{Ql as r}from"qiling-web";import{QlElement as c}from"qiling-web";class n extends c{constructor(){super(...arguments);this.state={items:r.signal([]),scrollTop:r.signal(0),viewportHeight:r.signal(0),version:r.signal(0),visible:r.computed(()=>{this.state.version.value;let t=this.state.items.value,i=this.actions.itemHeight(),e=this.actions.buffer(),s=this.state.scrollTop.value,a=this.state.viewportHeight.value,h=Math.max(0,Math.floor(s/i)-e),u=Math.min(t.length,Math.ceil((s+a)/i)+e),l=[];for(let o=h;o<u;o+=1)l.push({item:t[o],index:o});return l})};this.#i=null;this.#t=null;this.#e=!1;this.layout={type:"div",ref:"viewport",props:{class:"viewport"},on:{scroll:()=>this.actions.scrolled()},children:[{type:"div",slot:"spacer",props:{class:"spacer"}},{type:"div",slot:"window",props:{class:"window"}}]};this.actions={itemHeight(){return Math.max(1,Number(this.getProp("itemHeight"))||32)},buffer(){return Math.max(0,Number(this.getProp("buffer"))||0)},totalHeight(){return this.state.items.value.length*this.actions.itemHeight()},offset(){return Math.max(0,Math.floor(this.state.scrollTop.value/this.actions.itemHeight())-this.actions.buffer())*this.actions.itemHeight()},scrolled(){let t=this.ref("viewport");if(!t)return;this.state.scrollTop.value=t.scrollTop,this.actions.checkReachEnd()},checkReachEnd(){let t=this.ref("viewport");if(!t)return;let i=Math.max(0,Number(this.getProp("reachEndOffset"))||0),e=t.scrollTop+t.clientHeight>=t.scrollHeight-i;if(e&&!this.#e)this.#e=!0,this.emit("ql-reach-end");else if(!e)this.#e=!1},measure(){let t=this.ref("viewport");if(!t)return;if(this.state.viewportHeight.value=t.clientHeight,t.scrollHeight>t.clientHeight)t.setAttribute("tabindex","0");else t.removeAttribute("tabindex");this.actions.checkReachEnd()}}}static tag="ql-virtual-list";static props={itemHeight:{type:"number",default:32,description:"每项高度(px,定高虚拟滚动)"},buffer:{type:"number",default:4,description:"可视区上下各多渲染几项(滚动缓冲)"},height:{type:"length",default:"",description:"容器高度(缺省由外部约束)"},reachEndOffset:{type:"number",default:64,description:"距底部多少像素算「接近底部」(0 表示必须滚到底)"}};static emits={"ql-reach-end":{desc:"滚动接近底部时触发(用于触底加载)"}};static cssVars={height:"--ql-virtual-height",itemHeight:"--ql-virtual-item-height"};static styles=`
|
|
2
|
+
:host { display: block; }
|
|
3
|
+
.viewport {
|
|
4
|
+
position: relative;
|
|
5
|
+
box-sizing: border-box;
|
|
6
|
+
/* 视口必须有**固定高度** —— 虚拟化的前提(高度不定就等于全渲染) */
|
|
7
|
+
height: var(--ql-virtual-height, 240px);
|
|
8
|
+
overflow-y: auto;
|
|
9
|
+
overscroll-behavior: contain;
|
|
10
|
+
outline: none;
|
|
11
|
+
}
|
|
12
|
+
/* 键盘可达时才可聚焦(内容溢出时由 measure() 加 tabindex,规范 §4.4⑤) */
|
|
13
|
+
.viewport:focus-visible { box-shadow: inset 0 0 0 2px var(--ql-color-primary); }
|
|
14
|
+
/* 只负责撑出滚动条长度,不参与布局 */
|
|
15
|
+
.spacer { width: 1px; pointer-events: none; }
|
|
16
|
+
/* 窗口随滚动位移;里面只放可视区 + 缓冲的那些项 */
|
|
17
|
+
.window {
|
|
18
|
+
position: absolute;
|
|
19
|
+
top: 0;
|
|
20
|
+
left: 0;
|
|
21
|
+
right: 0;
|
|
22
|
+
will-change: transform;
|
|
23
|
+
}
|
|
24
|
+
/* 每项按 itemHeight 定高 —— 与虚拟化假设一致,避免内容把行高撑歪 */
|
|
25
|
+
.window > * { box-sizing: border-box; height: var(--ql-virtual-item-height, 32px); }
|
|
26
|
+
`;#i;#t;#e;set items(t){this.state.items.value=Array.isArray(t)?t:[],this.#e=!1}get items(){return this.state.items.value}set renderItem(t){this.#i=typeof t==="function"?t:null,this.state.version.value+=1}get renderItem(){return this.#i}render(t){t.spacer?.bind("style",()=>`height:${this.actions.totalHeight()}px`),t.window?.bind("style",()=>`transform:translateY(${this.actions.offset()}px)`),t.window?.add((i)=>{i.each(()=>this.state.visible.value,(e,s)=>{this.#i?.(e.item,e.index,s)},(e)=>e.index)})}onMounted(){if(this.actions.measure(),typeof ResizeObserver<"u"){this.#t=new ResizeObserver(()=>this.actions.measure()),this.#t.observe(this);let t=this.ref("viewport");if(t)this.#t.observe(t)}}onUnmounted(){this.#t?.disconnect(),this.#t=null}}p.define(n);export{n as QlVirtualList};
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { Ql } from 'qiling-web';
|
|
2
|
+
import type { GroupMethod } from 'qiling-web';
|
|
3
|
+
import { QlElement } from 'qiling-web';
|
|
4
|
+
import type { CssVarMap } from 'qiling-web';
|
|
5
|
+
import type { PropSchema } from 'qiling-web';
|
|
6
|
+
import type { VirtualEntry, VirtualItemRenderer } from './types';
|
|
7
|
+
/**
|
|
8
|
+
* 虚拟滚动列表:**只渲染可视区 + 缓冲**,滚动时复用 DOM。
|
|
9
|
+
*
|
|
10
|
+
* 用于「上万条也要流畅」的场景(日志、大表格、长选项列表)。
|
|
11
|
+
*
|
|
12
|
+
* 用法(两处注入,都保持声明式):
|
|
13
|
+
* ```js
|
|
14
|
+
* const list = document.querySelector('ql-virtual-list')
|
|
15
|
+
* list.items = rows // 数据注入(数组不能用 prop 表达)
|
|
16
|
+
* list.renderItem = (row, index, view) => { // 逐项渲染
|
|
17
|
+
* view.add('ql-text').text(row.name)
|
|
18
|
+
* }
|
|
19
|
+
* ```
|
|
20
|
+
*
|
|
21
|
+
* 事件:`ql-reach-end`(滚动接近底部,用于触底加载)。
|
|
22
|
+
*/
|
|
23
|
+
export declare class QlVirtualList extends QlElement {
|
|
24
|
+
#private;
|
|
25
|
+
static tag: string;
|
|
26
|
+
static props: PropSchema;
|
|
27
|
+
static emits: {
|
|
28
|
+
'ql-reach-end': {
|
|
29
|
+
desc: string;
|
|
30
|
+
};
|
|
31
|
+
};
|
|
32
|
+
static cssVars: CssVarMap;
|
|
33
|
+
static styles: string;
|
|
34
|
+
/**
|
|
35
|
+
* 2. 状态:滚动位置与可视高度是**窗口计算的输入**,必须响应式。
|
|
36
|
+
*
|
|
37
|
+
* 这也是组件侧第一次用到 `state` —— 其余 63 个组件都靠 `props` 驱动,
|
|
38
|
+
* 不需要内部可变状态。
|
|
39
|
+
*/
|
|
40
|
+
state: {
|
|
41
|
+
/** 数据源 */
|
|
42
|
+
items: import("@preact/signals-core").Signal<unknown[]>;
|
|
43
|
+
/** 滚动位置 */
|
|
44
|
+
scrollTop: import("@preact/signals-core").Signal<number>;
|
|
45
|
+
/** 容器可视高度(尺寸变化时由 ResizeObserver 更新) */
|
|
46
|
+
viewportHeight: import("@preact/signals-core").Signal<number>;
|
|
47
|
+
/** 渲染回调的版本号 —— 换回调时靠它触发窗口重算 */
|
|
48
|
+
version: import("@preact/signals-core").Signal<number>;
|
|
49
|
+
/** 可视区(含缓冲)的项 */
|
|
50
|
+
visible: import("@preact/signals-core").ReadonlySignal<VirtualEntry[]>;
|
|
51
|
+
};
|
|
52
|
+
/** 数据注入(数组无法用 prop 表达)—— 规范 §4.1 的「数据注入契约」 */
|
|
53
|
+
set items(value: unknown[] | null);
|
|
54
|
+
get items(): unknown[];
|
|
55
|
+
/** 逐项渲染回调(注入) */
|
|
56
|
+
set renderItem(fn: VirtualItemRenderer | null);
|
|
57
|
+
get renderItem(): VirtualItemRenderer | null;
|
|
58
|
+
/** 骨架:滚动视口(内含撑高垫层与位移窗口);会增减的项在 `render(area)` 里装 */
|
|
59
|
+
layout: {
|
|
60
|
+
type: string;
|
|
61
|
+
ref: string;
|
|
62
|
+
props: {
|
|
63
|
+
class: string;
|
|
64
|
+
};
|
|
65
|
+
on: {
|
|
66
|
+
scroll: () => any;
|
|
67
|
+
};
|
|
68
|
+
children: {
|
|
69
|
+
type: string;
|
|
70
|
+
slot: string;
|
|
71
|
+
props: {
|
|
72
|
+
class: string;
|
|
73
|
+
};
|
|
74
|
+
}[];
|
|
75
|
+
};
|
|
76
|
+
protected render(area: Record<string, Ql>): void;
|
|
77
|
+
/** 3. 操作:业务动作。**不挂 `Ql.desc()` 即内部方法**,不对外暴露 */
|
|
78
|
+
actions: Record<string, GroupMethod>;
|
|
79
|
+
protected onMounted(): void;
|
|
80
|
+
protected onUnmounted(): void;
|
|
81
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,57 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "qiling-web-virtual-list",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.2",
|
|
4
|
+
"description": "QiLing UI 拓展组件:虚拟滚动列表",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./dist/types/index.d.ts",
|
|
10
|
+
"default": "./dist/index.js"
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"dist",
|
|
15
|
+
"LICENSE",
|
|
16
|
+
"README.md"
|
|
17
|
+
],
|
|
18
|
+
"sideEffects": [
|
|
19
|
+
"./dist/index.js"
|
|
20
|
+
],
|
|
21
|
+
"peerDependencies": {
|
|
22
|
+
"qiling-web": "^0.1.2"
|
|
23
|
+
},
|
|
24
|
+
"devDependencies": {
|
|
25
|
+
"qiling-web": "workspace:*"
|
|
26
|
+
},
|
|
27
|
+
"scripts": {
|
|
28
|
+
"clean": "rm -rf dist",
|
|
29
|
+
"build": "bun run clean && bun run build:ui && bun run build:types",
|
|
30
|
+
"build:ui": "bun build src/index.ts --outdir=dist --target=browser --format=esm --minify --splitting --entry-naming=index.js --chunk-naming=chunk-[hash].js --min-chunk-size=16384 --external qiling-web --external qiling-web/internal",
|
|
31
|
+
"build:types": "tsc -p tsconfig.build.json",
|
|
32
|
+
"typecheck": "tsc --noEmit -p tsconfig.json",
|
|
33
|
+
"prepublishOnly": "bun run build && bun run typecheck"
|
|
34
|
+
},
|
|
35
|
+
"repository": {
|
|
36
|
+
"type": "git",
|
|
37
|
+
"url": "git+https://gitee.com/i-o-i/qiling-web.git",
|
|
38
|
+
"directory": "packages/qiling-web-virtual-list"
|
|
39
|
+
},
|
|
40
|
+
"homepage": "http://47.93.3.131:18105/docs/",
|
|
41
|
+
"bugs": {
|
|
42
|
+
"url": "https://gitee.com/i-o-i/qiling-web/issues"
|
|
43
|
+
},
|
|
44
|
+
"keywords": [
|
|
45
|
+
"qiling",
|
|
46
|
+
"qiling-web",
|
|
47
|
+
"web-components",
|
|
48
|
+
"ui",
|
|
49
|
+
"framework"
|
|
50
|
+
],
|
|
51
|
+
"author": {
|
|
52
|
+
"name": "炼器师"
|
|
53
|
+
},
|
|
54
|
+
"publishConfig": {
|
|
55
|
+
"access": "public"
|
|
56
|
+
}
|
|
57
|
+
}
|