-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathexport.ts
165 lines (137 loc) · 4.89 KB
/
export.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import { loadValue } from '@logux/client'
import { atom, onMount } from 'nanostores'
import {
type CategoryValue,
feedsByCategory,
getCategories,
getCategoryTitle
} from './category.ts'
import { type FeedValue, getFeeds } from './feed.ts'
import { readonlyExport } from './lib/stores.ts'
import { getPosts, type PostValue } from './post.ts'
type FeedWithPosts = {
posts?: PostValue[]
} & FeedValue
let $categories = atom<CategoryValue[]>([])
let $allFeeds = atom<FeedValue[]>([])
let $exportingFeedsByCategory = atom<[CategoryValue, FeedWithPosts[]][]>([])
let $exporting = atom<boolean>(false)
export const exportingFeedsByCategory = readonlyExport(
$exportingFeedsByCategory
)
export const exporting = readonlyExport($exporting)
onMount(exportingFeedsByCategory, () => {
Promise.all([loadValue(getCategories()), loadValue(getFeeds())]).then(
([categoriesValue, feedsValue]) => {
$categories.set(categoriesValue.list)
$allFeeds.set(feedsValue.list)
$exportingFeedsByCategory.set(
feedsByCategory($categories.get(), $allFeeds.get())
)
selectAllExportingFeeds()
}
)
})
let $selectedCategories = atom<string[]>([])
let $selectedFeeds = atom<string[]>([])
export const exportingCategories = readonlyExport($selectedCategories)
export const exportingFeeds = readonlyExport($selectedFeeds)
export function selectAllExportingFeeds(): void {
let selectedCategoriesSet = new Set<string>()
let selectedFeedsSet = new Set<string>()
$exportingFeedsByCategory
.get()
.forEach(([category, feeds]: [CategoryValue, FeedWithPosts[]]) => {
selectedCategoriesSet.add(category.id)
feeds.forEach(feed => selectedFeedsSet.add(feed.id))
})
$selectedCategories.set(Array.from(selectedCategoriesSet))
$selectedFeeds.set(Array.from(selectedFeedsSet))
}
export function clearExportingSelections(): void {
$selectedCategories.set([])
$selectedFeeds.set([])
}
export function getOPMLBlob(): Blob {
$exporting.set(true)
let opml =
'<?xml version="1.0" encoding="UTF-8"?>\n<opml version="2.0">\n<head>\n<title>Feeds</title>\n</head>\n<body>\n'
$exportingFeedsByCategory
.get()
.forEach(([category, feeds]: [CategoryValue, FeedWithPosts[]]) => {
if ($selectedCategories.get().includes(category.id)) {
opml += `<outline text="${getCategoryTitle(category)}">\n`
feeds.forEach(feed => {
if ($selectedFeeds.get().includes(feed.id)) {
opml += `<outline text="${feed.title}" type="${feed.loader}" xmlUrl="${feed.url}" />\n`
}
})
opml += `</outline>\n`
}
})
opml += '</body>\n</opml>'
$exporting.set(false)
return new Blob([opml], { type: 'application/xml' })
}
export type InternalExport = {
data: [CategoryValue, FeedWithPosts[]][]
type: 'feedsByCategory'
}
export async function getInternalBlob(isIncludePosts: boolean): Promise<Blob> {
$exporting.set(true)
let posts: PostValue[]
if (isIncludePosts) {
posts = await loadValue(getPosts()).then(value => value.list)
}
let enrichedData = $exportingFeedsByCategory
.get()
.map(([category, feeds]: [CategoryValue, FeedWithPosts[]]) => {
let categoryTitle = getCategoryTitle(category)
if (isIncludePosts) {
feeds.forEach(feed => {
feed.posts = posts.filter(post => post.feedId === feed.id)
})
}
return [{ ...category, title: categoryTitle }, feeds]
})
let jsonStr = JSON.stringify({
data: enrichedData,
type: 'feedsByCategory' // mark for validation in import
})
$exporting.set(false)
return new Blob([jsonStr], { type: 'application/json' })
}
export function toggleExportingCategory(categoryId: string): void {
let selectedCategories = new Set($selectedCategories.get())
let selectedFeeds = new Set($selectedFeeds.get())
let feeds = $allFeeds.get().filter(feed => feed.categoryId === categoryId)
if (selectedCategories.has(categoryId)) {
selectedCategories.delete(categoryId)
feeds.forEach(feed => selectedFeeds.delete(feed.id))
} else {
selectedCategories.add(categoryId)
feeds.forEach(feed => selectedFeeds.add(feed.id))
}
$selectedCategories.set(Array.from(selectedCategories))
$selectedFeeds.set(Array.from(selectedFeeds))
}
export function toggleExportingFeed(feedId: string, categoryId: string): void {
let selectedCategories = new Set($selectedCategories.get())
let selectedFeeds = new Set($selectedFeeds.get())
if (selectedFeeds.has(feedId)) {
selectedFeeds.delete(feedId)
let remainingFeedsInCategory = $allFeeds
.get()
.filter(
feed => feed.categoryId === categoryId && selectedFeeds.has(feed.id)
)
if (remainingFeedsInCategory.length === 0) {
selectedCategories.delete(categoryId)
}
} else {
selectedFeeds.add(feedId)
selectedCategories.add(categoryId)
}
$selectedCategories.set(Array.from(selectedCategories))
$selectedFeeds.set(Array.from(selectedFeeds))
}