Skip to content

Commit 0ecf43b

Browse files
authored
Merge pull request #34 from cnjack/master
添加typescript支持
2 parents b3b0071 + 835fbdb commit 0ecf43b

File tree

6 files changed

+64
-4
lines changed

6 files changed

+64
-4
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
.idea
22
.DS_Store
33
package-lock.json
4+
yarn.lock
45

56
logs
67
*.log

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,11 @@ npm install --save miniprogram-recycle-view
139139
})
140140
```
141141

142+
`typescript`支持,使用如下方式引入
143+
```typescript
144+
import * as recycleContext from 'miniprogram-recycle-view';
145+
```
146+
142147
​ 页面必须通过 Component 构造器定义,页面引入了`miniprogram-recycle-view`包之后,会在 wx 对象下面新增接口`createRecycleContext`函数创建`RecycleContext`对象来管理 recycle-view 定义的的数据,`createRecycleContext`接收类型为1个 Object 的参数,Object 参数的每一个 key 的介绍如下:
143148

144149
| 参数名 | 类型 | 描述 |

src/index.d.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
declare namespace recycleContext {
2+
interface itemSize {
3+
width: number;
4+
height: number;
5+
}
6+
7+
type Component = any;
8+
type Page = any;
9+
10+
type itemSizeFunc<T> = (item: T, index: number) => itemSize
11+
12+
interface options<T> {
13+
id: string;
14+
dataKey: string;
15+
page: Component | Page;
16+
itemSize: itemSizeFunc<T> | itemSize;
17+
useInPage?: boolean;
18+
root?: Page;
19+
}
20+
21+
interface position {
22+
left: number;
23+
top: number;
24+
width: number;
25+
height: number;
26+
}
27+
28+
interface RecycleContext<T> {
29+
append(list: T[], callback?: () => void): void
30+
appendList(list: T[], callback?: () => void): void
31+
splice(begin: number, deleteCount: number, appendList: T[], callback?: () => void): void;
32+
updateList(beginIndex: number, list: T[], callback?: () => void): void
33+
update(beginIndex: number, list: T[], callback?: () => void): void
34+
destroy(): void
35+
forceUpdate(callback: () => void, reinitSlot: boolean): void
36+
getBoundingClientRect(index: number | undefined): position | position[]
37+
getScrollTop(): number;
38+
transformRpx(rpx: number, addPxSuffix?: string): number;
39+
getViewportItems(inViewportPx: number): T[]
40+
getList(): T[]
41+
}
42+
}
43+
declare function recycleContext<T>(op: recycleContext.options<T>): recycleContext.RecycleContext<T>
44+
45+
export = recycleContext;

src/utils/recycle-context.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,9 @@ RecycleContext.prototype.getTotalHeight = function () {
574574
}
575575
// 返回完整的列表数据
576576
RecycleContext.prototype.getList = function () {
577+
if (!recycleData[this.id]) {
578+
return [];
579+
}
577580
return recycleData[this.id].list
578581
}
579582
module.exports = RecycleContext

tools/build.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -231,10 +231,16 @@ class BuildTask {
231231
/**
232232
* copy resources to dist folder
233233
*/
234-
gulp.task(`${id}-copy`, gulp.parallel(done => {
234+
gulp.task(`${id}-copy`, gulp.parallel(async done => {
235235
const copyList = this.copyList
236-
const copyFileList = copyList.map(dir => path.join(dir, '**/*.!(wxss)'))
237-
236+
const anAsyncFunction = async dir => {
237+
const isFileExist = await _.checkFileExists(path.join(srcPath, dir));
238+
if (!isFileExist){
239+
return path.join(dir, '**/*.!(wxss)')
240+
}
241+
return dir
242+
}
243+
const copyFileList = await Promise.all(copyList.map(item => anAsyncFunction(item)))
238244
if (copyFileList.length) return copy(copyFileList)
239245

240246
return done()

tools/config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,5 +62,5 @@ module.exports = {
6262
assetFilter: assetFilename => assetFilename.endsWith('.js')
6363
}
6464
},
65-
copy: ['./wxml', './wxss', './wxs', './images'],
65+
copy: ['./wxml', './wxss', './wxs', './images', './index.d.ts'],
6666
}

0 commit comments

Comments
 (0)