-
-
Notifications
You must be signed in to change notification settings - Fork 253
Expand file tree
/
Copy pathsnippets.ts
More file actions
357 lines (312 loc) · 10.4 KB
/
snippets.ts
File metadata and controls
357 lines (312 loc) · 10.4 KB
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
import type { Language } from '../../shared/types/renderer/editor'
import type { SystemFolderAlias } from '@shared/types/renderer/sidebar'
import { sortSnippetsBy, useApi } from '@/composable'
import { i18n, store } from '@/electron'
import type {
Snippet,
SnippetContent,
SnippetsSort,
Tag
} from '@shared/types/main/db'
import { defineStore } from 'pinia'
import { useFolderStore } from './folders'
import type {
SnippetWithFolder,
State,
PreviewType
} 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,
sort: 'updatedAt',
hideSubfolderSnippets: false,
compactMode: false,
isContextState: false,
isMarkdownPreview: false,
isMindmapPreview: false,
isScreenshotPreview: false,
isCodePreview: false
}),
getters: {
snippetsByFilter: (state): SnippetWithFolder[] => {
const folderStore = useFolderStore()
if (folderStore.selectedAlias) {
return state.snippets
}
if (state.hideSubfolderSnippets) {
return state.snippets.filter(
i => i.folderId === folderStore.selectedId
)
}
return state.snippets
},
selectedId: state => state.selected?.id,
selectedIds: state => state.selectedMultiple.map(i => i.id),
selectedIndex () {
// @ts-expect-error
// FIXME: Разобраться с типами
return this.snippetsByFilter.findIndex(i => i.id === this.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 {
const appStore = useAppStore()
if (appStore.editor.showFragments === true) return true
else return this.fragmentCount ? this.fragmentCount > 1 : false
},
isTagsShow (): boolean {
const appStore = useAppStore()
return this.tagsCount ? this.tagsCount > 0 : appStore.showTags
},
isDescriptionShow: state =>
typeof state.selected?.description === 'string'
},
actions: {
async getSnippets () {
const { data } = await useApi('/snippets/embed-folder').get().json()
this.all = data.value
sortSnippetsBy(this.all, this.sort)
},
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)
sortSnippetsBy(this.snippets, this.sort)
},
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 snippet = this.snippets.find(i => i.id === id)
if (!snippet) return
if (snippet.id === this.selectedId) {
for (const props in body) {
(this.selected as any)[props] = (body as any)[props]
}
}
for (const props in body) {
(snippet as any)[props] = (body as any)[props]
}
await useApi(`/snippets/${id}`).patch(body).json()
},
async patchCurrentSnippetContentByKey (
key: keyof SnippetContent,
value: string | Language
) {
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)
}
},
async addNewSnippet (body?: Partial<Snippet>) {
const folderStore = useFolderStore()
let _body: Partial<Snippet> = {}
_body.isDeleted = false
_body.isFavorites = false
_body.folderId = ''
_body.tagsIds = []
_body.description = null
if (body) {
_body = {
..._body,
name: body.name,
content: body.content
}
} else {
_body.name = i18n.t('snippet.untitled')
_body.folderId = folderStore.selectedId || ''
_body.content = [
{
label: `${i18n.t('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: `${i18n.t('fragment')} ${fragmentCount}`,
language: folderStore.selected?.defaultLanguage || 'plain_text',
value: ''
})
body.content = content
await this.patchSnippetsById(id, body)
this.fragment = content.length - 1
},
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)
}
}
},
async setSnippetsByAlias (alias: SystemFolderAlias) {
const folderStore = useFolderStore()
folderStore.selectedAlias = alias
folderStore.selected = undefined
folderStore.selectedId = undefined
folderStore.selectedIds = undefined
await this.getSnippets()
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
sortSnippetsBy(this.snippets, this.sort)
this.selected = snippets[0]
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.findSnippetById(id)
if (snippet) this.selected = snippet
},
findSnippetById (id: string) {
return this.all.find(i => i.id === id)
},
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]
},
setSort (sort: SnippetsSort) {
this.sort = sort
store.app.set('sort', sort)
sortSnippetsBy(this.snippets, this.sort)
},
togglePreview (type: PreviewType) {
switch (type) {
case 'markdown':
this.isMarkdownPreview = !this.isMarkdownPreview
this.isMindmapPreview = false
this.isScreenshotPreview = false
this.isCodePreview = false
break
case 'mindmap':
this.isMarkdownPreview = false
this.isMindmapPreview = !this.isMindmapPreview
this.isScreenshotPreview = false
this.isCodePreview = false
break
case 'screenshot':
this.isMarkdownPreview = false
this.isMindmapPreview = false
this.isScreenshotPreview = !this.isScreenshotPreview
this.isCodePreview = false
break
case 'code':
this.isMarkdownPreview = false
this.isMindmapPreview = false
this.isScreenshotPreview = false
this.isCodePreview = !this.isCodePreview
break
}
}
}
})