-
-
Notifications
You must be signed in to change notification settings - Fork 206
/
Copy pathsnippets.ts
286 lines (247 loc) · 8.55 KB
/
snippets.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
import type { Language } from '../../shared/types/renderer/editor'
import type { SystemFolderAlias } from '@shared/types/renderer/sidebar'
import { useApi } from '@/composable'
import { store } from '@/electron'
import type {
Folder,
Snippet,
SnippetContent,
Tag
} from '@shared/types/main/db'
import { defineStore } from 'pinia'
import { useFolderStore } from './folders'
import type {
SnippetWithFolder,
State
} from '@shared/types/renderer/store/snippets'
import { useTagStore } from './tags'
import { useAppStore } from './app'
import { uniqBy } from 'lodash'
export const useSnippetStore = defineStore('snippets', {
state: (): State => ({
all: [],
snippets: [],
selected: undefined,
selectedMultiple: [],
fragment: 0,
searchQuery: undefined,
isContextState: false,
isMarkdownPreview: false
}),
getters: {
selectedId: state => state.selected?.id,
selectedIds: state => state.selectedMultiple.map(i => i.id),
selectedIndex: state =>
state.snippets.findIndex(i => i.id === state.selected?.id),
currentContent: state =>
state.selected?.content?.[state.fragment]?.value || undefined,
currentLanguage: state =>
state.selected?.content?.[state.fragment]?.language,
currentTags (): Tag[] {
const tagStore = useTagStore()
const tags: Tag[] = []
if (this.selected?.tagsIds.length) {
this.selected.tagsIds.forEach(i => {
const tag = tagStore.tags.find(t => t.id === i)
if (tag) tags.push(tag)
})
}
return tags
},
fragmentLabels: state => state.selected?.content?.map(i => i.label),
fragmentCount: state => state.selected?.content?.length,
tagsCount: state => state.selected?.tagsIds?.length,
isFragmentsShow (): boolean {
return this.fragmentCount ? this.fragmentCount > 1 : false
},
isTagsShow (): boolean {
const appStore = useAppStore()
return this.tagsCount ? this.tagsCount > 0 : appStore.showTags
}
},
actions: {
async getSnippets () {
// Почему то не работает _expand
const { data } = await useApi('/snippets?_expand=folder`').get().json()
const { data: folderData } = await useApi('/folders').get().json()
// Поэтому добавляем folder самостоятельно
this.all = data.value.map((i: SnippetWithFolder) => {
const folder = folderData.value.find((f: Folder) => f.id === i.folderId)
if (folder) i.folder = folder
return i
})
this.all.sort((a, b) => (a.createdAt > b.createdAt ? -1 : 1))
},
async getSnippetsByFolderIds (ids: string[]) {
const snippets: SnippetWithFolder[] = []
for (const id of ids) {
const { data } = await useApi<SnippetWithFolder[]>(
`/folders/${id}/snippets?_expand=folder`
)
.get()
.json()
snippets.push(...data.value)
}
this.snippets = snippets
.filter(i => !i.isDeleted)
.sort((a, b) => (a.createdAt > b.createdAt ? -1 : 1))
},
async getSnippetsById (id: string) {
if (id) {
const { data } = await useApi<Snippet>(`/snippets/${id}`).get().json()
this.selected = data.value
store.app.set('selectedSnippetId', id)
} else {
store.app.delete('selectedSnippetId')
this.selected = undefined
}
},
async patchSnippetsById (id: string, body: Partial<Snippet>) {
const { data } = await useApi(`/snippets/${id}`).patch(body).json()
const snippet = this.snippets.find(i => i.id === id)
if (!snippet) return
if (snippet.id === this.selected?.id) {
this.selected.name = data.value.name
}
if (snippet.name !== data.value.name) {
snippet.name = data.value.name
}
await this.getSnippets()
},
async patchCurrentSnippetContentByKey (
key: keyof SnippetContent,
value: string | Language
) {
const folderStore = useFolderStore()
const body: Partial<Snippet> = {}
const content = this.selected?.content
if (content) {
(content[this.fragment] as any)[key] = value
body.content = content
body.updatedAt = new Date().valueOf()
await useApi(`/snippets/${this.selectedId}`).patch(body)
await this.getSnippetsByFolderIds(folderStore.selectedIds!)
}
},
async addNewSnippet () {
const folderStore = useFolderStore()
const body: Partial<Snippet> = {}
body.name = 'Untitled snippet'
body.folderId = folderStore.selectedId
body.isDeleted = false
body.isFavorites = false
body.tagsIds = []
body.content = [
{
label: 'Fragment 1',
language: folderStore.selected?.defaultLanguage || 'plain_text',
value: ''
}
]
const { data } = await useApi('/snippets').post(body).json()
this.selected = data.value
store.app.set('selectedSnippetId', this.selected!.id)
},
async duplicateSnippetById (id: string) {
const snippet = this.snippets.find(i => i.id === id)
if (snippet) {
const body = Object.assign({}, snippet)
body.name = body.name + ' Copy'
const { data } = await useApi('/snippets').post(body).json()
this.selected = data.value
this.fragment = 0
store.app.set('selectedSnippetId', this.selected!.id)
}
},
async addNewFragmentToSnippetsById (id: string) {
const folderStore = useFolderStore()
const content = [...this.selected!.content]
const fragmentCount = content.length + 1
const body: Partial<Snippet> = {}
content.push({
label: `Fragment ${fragmentCount}`,
language: folderStore.selected?.defaultLanguage || 'plain_text',
value: ''
})
body.content = content
await this.patchSnippetsById(id, body)
await this.getSnippetsByFolderIds(folderStore.selectedIds!)
await this.getSnippetsById(id)
},
async deleteCurrentSnippetFragmentByIndex (index: number) {
const body: Partial<Snippet> = {}
const content = [...this.selected!.content]
content.splice(index, 1)
body.content = content
await this.patchSnippetsById(this.selectedId!, body)
await this.getSnippetsById(this.selectedId!)
},
async deleteSnippetsById (id: string) {
await useApi(`/snippets/${id}`).delete()
},
async deleteSnippetsByIds (ids: string[]) {
await useApi('/snippets/delete').post({ ids })
},
async setSnippetsByFolderIds (setFirst?: boolean) {
const folderStore = useFolderStore()
await this.getSnippetsByFolderIds(folderStore.selectedIds!)
if (setFirst) {
this.selected = this.snippets[0]
if (this.selected) {
store.app.set('selectedSnippetId', this.snippets[0].id)
}
}
},
setSnippetsByAlias (alias: SystemFolderAlias) {
const folderStore = useFolderStore()
let snippets: SnippetWithFolder[] = []
if (alias === 'inbox') {
snippets = this.all.filter(i => !i.folderId && !i.isDeleted)
}
if (alias === 'all') {
snippets = this.all.filter(i => !i.isDeleted)
}
if (alias === 'favorites') {
snippets = this.all.filter(i => i.isFavorites && !i.isDeleted)
}
if (alias === 'trash') {
snippets = this.all.filter(i => i.isDeleted)
}
this.snippets = snippets
this.selected = snippets[0]
folderStore.selectedId = undefined
folderStore.selectedIds = undefined
folderStore.selectedAlias = alias
store.app.set('selectedFolderAlias', alias)
store.app.delete('selectedFolderId')
store.app.delete('selectedFolderIds')
},
async setSnippetsByTagId (id: string) {
const snippets: SnippetWithFolder[] = this.all.filter(i =>
i.tagsIds.includes(id)
)
this.snippets = snippets
this.selected = snippets[0]
},
setSnippetById (id: string) {
const snippet = this.all.find(i => i.id === id)
if (snippet) this.selected = snippet
},
async emptyTrash () {
const ids = this.all.filter(i => i.isDeleted).map(i => i.id)
await this.deleteSnippetsByIds(ids)
},
search (query: string) {
const byName = this.all.filter(i =>
i.name.toLowerCase().includes(query.toLowerCase())
)
const byContent = this.all.filter(i => {
return i.content.some(c =>
c.value.toLowerCase().includes(query.toLowerCase())
)
})
this.snippets = uniqBy([...byName, ...byContent], 'id')
this.selected = this.snippets[0]
}
}
})