Scriptables/Scripts/「源码」小组件示例.js
2022-06-02 17:00:01 +08:00

107 lines
2.9 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: orange; icon-glyph: comments;
//
// iOS 桌面组件脚本 @「小件件」
// 开发说明:请从 Widget 类开始编写,注释请勿修改
// https://x.im3x.cn
//
// 添加require是为了vscode中可以正确引入包以获得自动补全等功能
if (typeof require === 'undefined') require = importModule
const {
Base
} = require("./「小件件」开发环境")
// @组件代码开始
class Widget extends Base {
/**
* 传递给组件的参数,可以是桌面 Parameter 数据,也可以是外部如 URLScheme 等传递的数据
* @param {string} arg 自定义参数
*/
constructor(arg) {
super(arg)
this.name = '示例小组件'
this.desc = '「小件件」—— 原创精美实用小组件'
}
/**
* 渲染函数,函数名固定
* 可以根据 this.widgetFamily 来判断小组件尺寸,以返回不同大小的内容
*/
async render() {
const data = await this.getData()
switch (this.widgetFamily) {
case 'large':
return await this.renderLarge(data)
case 'medium':
return await this.renderMedium(data)
default:
return await this.renderSmall(data)
}
}
/**
* 渲染小尺寸组件
*/
async renderSmall(data) {
let w = new ListWidget()
await this.renderHeader(w, data['logo'], data['title'])
const t = w.addText(data['content'])
t.font = Font.lightSystemFont(16)
w.addSpacer()
w.url = this.actionUrl('open-url', data['url'])
return w
}
/**
* 渲染中尺寸组件
*/
async renderMedium(data, num = 3) {
let w = new ListWidget()
await this.renderHeader(w, data['logo'], data['title'])
data['data'].slice(0, num).map(d => {
const cell = w.addStack()
cell.centerAlignContent()
const cell_box = cell.addStack()
cell_box.size = new Size(3, 15)
cell_box.backgroundColor = new Color('#ff837a', 0.6)
cell.addSpacer(10)
const cell_text = cell.addText(d['title'])
cell_text.font = Font.lightSystemFont(16)
cell.url = this.actionUrl("open-url", d['url'])
cell.addSpacer()
w.addSpacer(10)
})
w.addSpacer()
return w
}
/**
* 渲染大尺寸组件
*/
async renderLarge(data) {
return await this.renderMedium(data, 10)
}
/**
* 获取数据函数,函数名可不固定
*/
async getData() {
const api = 'https://x.im3x.cn/v1/test-api.json'
return await this.httpGet(api, true, false)
}
/**
* 自定义注册点击事件,用 actionUrl 生成一个触发链接,点击后会执行下方对应的 action
* @param {string} url 打开的链接
*/
async actionOpenUrl(url) {
Safari.openInApp(url, false)
}
}
// @组件代码结束
const {
Testing
} = require("./「小件件」开发环境")
await Testing(Widget)