Skip to content

Commit f1999fb

Browse files
committed
编写今日热榜小部件
1 parent f97b545 commit f1999fb

File tree

1 file changed

+135
-0
lines changed

1 file changed

+135
-0
lines changed

src/scripts/newsTop.tsx

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
/**
2+
* 今日热榜小部件
3+
*/
4+
5+
import {
6+
isLaunchInsideApp,
7+
setTransparentBackground,
8+
showActionSheet,
9+
showModal,
10+
showNotification,
11+
showPreviewOptions,
12+
useStorage,
13+
} from '@app/lib/help'
14+
import {Col} from '@app/lib/components'
15+
import {WstackProps} from '@app/types/widget'
16+
import {FC} from 'react'
17+
18+
const {setStorage, getStorage} = useStorage('newsTop-xiaoming')
19+
20+
/**文字颜色*/
21+
const textColor = getStorage<string>('textColor') || '#222222'
22+
23+
/**透明背景*/
24+
const transparentBg: Image | string = getStorage<Image>('transparentBg') || '#ffffff'
25+
26+
/**背景颜色或背景图链接*/
27+
const boxBg = getStorage<string>('boxBg') || '#ffffff'
28+
29+
// 好看的颜色
30+
const colors = {
31+
red: '#e54d42',
32+
orange: '#f37b1d',
33+
yellow: '#fbbd08',
34+
olive: '#8dc63f',
35+
green: '#39b54a',
36+
cyan: '#1cbbb4',
37+
blue: '#0081ff',
38+
purple: '#6739b6',
39+
mauve: '#9c26b0',
40+
pink: '#e03997',
41+
brown: '#a5673f',
42+
grey: '#8799a3',
43+
black: '#000000',
44+
}
45+
46+
class NewsTop {
47+
async init() {
48+
if (isLaunchInsideApp()) {
49+
return await this.showMenu()
50+
}
51+
const widget = (await this.render()) as ListWidget
52+
Script.setWidget(widget)
53+
Script.complete()
54+
}
55+
56+
//渲染组件
57+
async render(): Promise<unknown> {
58+
if (isLaunchInsideApp()) {
59+
await showNotification({title: '稍等片刻', body: '小部件渲染中...', sound: 'alert'})
60+
}
61+
// 多久(毫秒)更新一次小部件(默认1小时)
62+
const updateInterval = 1 * 60 * 60 * 1000
63+
// 渲染尺寸
64+
const size = config.widgetFamily
65+
return (
66+
<wbox
67+
padding={[0, 0, 0, 0]}
68+
updateDate={new Date(Date.now() + updateInterval)}
69+
background={boxBg.match('透明背景') ? transparentBg : boxBg}
70+
>
71+
{size === 'small' && this.renderSmall()}
72+
{size === 'medium' && this.renderMedium()}
73+
{size === 'large' && this.renderLarge()}
74+
</wbox>
75+
)
76+
}
77+
78+
// 渲染小尺寸
79+
renderSmall() {
80+
return <wstack flexDirection="column"></wstack>
81+
}
82+
83+
// 渲染中尺寸
84+
renderMedium() {
85+
return <wstack></wstack>
86+
}
87+
88+
// 渲染大尺寸
89+
renderLarge() {
90+
return <wstack flexDirection="column"></wstack>
91+
}
92+
93+
// 显示菜单
94+
async showMenu() {
95+
const selectIndex = await showActionSheet({
96+
title: '菜单',
97+
itemList: ['设置颜色', '设置透明背景', '预览组件'],
98+
})
99+
switch (selectIndex) {
100+
case 0:
101+
const {texts, cancel} = await showModal({
102+
title: '设置全局背景和颜色',
103+
content: '如果为空,则还原默认',
104+
inputItems: [
105+
{
106+
text: getStorage<string>('boxBg') || '',
107+
placeholder: '这里填全局背景,可以是颜色、图片链接',
108+
},
109+
{
110+
text: getStorage<string>('textColor') || '',
111+
placeholder: '这里填文字颜色',
112+
},
113+
],
114+
})
115+
if (cancel) return
116+
setStorage('boxBg', texts[0])
117+
setStorage('textColor', texts[1])
118+
await showNotification({title: '设置完成', sound: 'default'})
119+
break
120+
case 1:
121+
const img: Image | null = (await setTransparentBackground()) || null
122+
if (img) {
123+
setStorage('transparentBg', img)
124+
setStorage('boxBg', '透明背景')
125+
await showNotification({title: '设置透明背景成功', sound: 'default'})
126+
}
127+
break
128+
case 2:
129+
await showPreviewOptions(this.render.bind(this))
130+
break
131+
}
132+
}
133+
}
134+
135+
EndAwait(() => new NewsTop().init())

0 commit comments

Comments
 (0)