import Parser from 'https://esm.sh/rss-parser@3.13.0'
const PATH = 'data/third/rss-reader'
const SUB_PATH = PATH + '/subs.json'
const READ_PATH = PATH + '/read_ids.json'
/** @type { EsmPlugin } */
export default (Plugin) => {
const onRun = async () => {
openUI()
}
const openUI = () => {
const { ref, resolveComponent, onMounted } = Vue
const subs = ref([])
const readIds = ref(new Set())
const component = {
template: /*template*/ `
{{ article.title }}
没有更多内容了
`,
setup() {
const active = ref({
items: []
})
const article = ref()
const fetchSub = async (sub) => {
active.value = sub
const res = await Plugins.HttpGet(sub.feedUrl)
const parser = new Parser()
const feed = await parser.parseString(res.body)
Object.assign(sub, feed)
}
const preview = async (a) => {
article.value = a
readIds.value.add(a.id)
}
const viewOriginal = () => {
openWebUI(article.value.link)
}
const jumpOriginal = () => {
Plugins.OpenURI(article.value.link)
}
const isRead = (item) => {
return readIds.value.has(item.id)
}
onMounted(() => {
Plugins.ReadFile(SUB_PATH).then((txt) => {
subs.value = JSON.parse(txt || '[]')
if (subs.value[0]) {
active.value = subs.value[0]
}
})
Plugins.ReadFile(READ_PATH).then((txt) => {
readIds.value = new Set(JSON.parse(txt || '[]'))
})
})
return {
subs,
active,
article,
fetchSub,
preview,
isRead,
readIds,
viewOriginal,
jumpOriginal
}
}
}
const m = Plugins.modal(
{
title: Plugin.name,
width: '90',
height: '90',
py: 0,
submit: false,
maskClosable: true,
cancelText: 'common.close',
async beforeClose() {
await Plugins.WriteFile(READ_PATH, JSON.stringify([...readIds.value]))
}
},
{
default: () => Vue.h(component),
toolbar: () => [
Vue.h(
resolveComponent('Button'),
{
type: 'text',
onClick: () => {
openSubsUI(subs)
}
},
() => '订阅源'
)
]
}
)
m.open()
}
const openSubsUI = (subs) => {
const { ref } = Vue
const list = ref(Plugins.deepClone(subs.value))
const component = {
template: /*template*/ `
`,
setup() {
const onAdd = () => {
list.value.push({
title: '',
feedUrl: '',
items: []
})
}
const onDel = (i) => {
list.value.splice(i, 1)
}
return {
list,
onAdd,
onDel
}
}
}
const m = Plugins.modal(
{
title: '订阅源管理',
async onOk() {
subs.value = list.value
await Plugins.WriteFile(SUB_PATH, JSON.stringify(subs.value, null, 2))
}
},
{
default: () => Vue.h(component)
}
)
m.open()
}
const openWebUI = (link) => {
const component = {
template: /*template*/ `
`
}
const m = Plugins.modal(
{
title: '原文预览',
width: '90',
height: '90',
maskClosable: true,
submit: false,
px: 0,
py: 0,
cancelText: 'common.close'
},
{
default: () => Vue.h(component)
}
)
m.open()
}
return { onRun }
}