Skip to content

Commit 4abd6bd

Browse files
feat(api): create snippets (#56)
1 parent fdced4d commit 4abd6bd

File tree

7 files changed

+62
-17
lines changed

7 files changed

+62
-17
lines changed

src/main/services/api/server.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import type { Server } from 'http'
88
import type { Socket } from 'net'
99
import { remove } from 'lodash'
1010
import type { SnippetWithFolder } from '@shared/types/renderer/store/snippets'
11+
import { BrowserWindow } from 'electron'
1112

1213
interface ServerWithDestroy extends Server {
1314
destroy: Function
@@ -82,6 +83,13 @@ export class ApiServer {
8283
res.sendStatus(200)
8384
})
8485

86+
app.post('/snippets/create', (req, res) => {
87+
const windows = BrowserWindow.getAllWindows()
88+
windows[0].webContents.send('api:snippet-create', req.body)
89+
90+
res.sendStatus(200)
91+
})
92+
8593
app.delete('/tags/:id', (req, res) => {
8694
const id = req.params.id
8795
const tags = router.db.get<Tag[]>('tags').value()

src/renderer/App.vue

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,12 @@ import {
2525
onAddNewFragment,
2626
onAddNewFolder,
2727
onCopySnippet,
28-
emitter
28+
emitter,
29+
onCreateSnippet
2930
} from '@/composable'
3031
import { createToast, destroyAllToasts } from 'vercel-toast'
3132
import { useRoute } from 'vue-router'
33+
import type { Snippet } from '@shared/types/main/db'
3234
3335
// По какой то причине необходимо явно установить роут в '/'
3436
// для корректного поведения в продакшен сборке
@@ -177,6 +179,10 @@ ipc.on('main-menu:format-snippet', () => {
177179
ipc.on('main-menu:search', () => {
178180
emitter.emit('search:focus', true)
179181
})
182+
183+
ipc.on('api:snippet-create', (event, body: Snippet) => {
184+
onCreateSnippet(body)
185+
})
180186
</script>
181187

182188
<style lang="scss">

src/renderer/composable/index.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { useFolderStore } from '@/store/folders'
66
import { useSnippetStore } from '@/store/snippets'
77
import { ipc, track } from '@/electron'
88
import type { NotificationRequest } from '@shared/types/main'
9+
import type { Snippet } from '@shared/types/main/db'
910

1011
export const useApi = createFetch({
1112
baseUrl: `http://localhost:${API_PORT}`
@@ -31,6 +32,15 @@ export const onAddNewSnippet = async () => {
3132
track('snippets/add-new')
3233
}
3334

35+
export const onCreateSnippet = async (body: Partial<Snippet>) => {
36+
const snippetStore = useSnippetStore()
37+
38+
await snippetStore.addNewSnippet(body)
39+
await snippetStore.getSnippets()
40+
snippetStore.setSnippetsByAlias('inbox')
41+
track('api/snippet-create')
42+
}
43+
3444
export const onAddNewFragment = () => {
3545
const snippetStore = useSnippetStore()
3646

src/renderer/store/snippets.ts

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -131,24 +131,34 @@ export const useSnippetStore = defineStore('snippets', {
131131
await this.getSnippetsByFolderIds(folderStore.selectedIds!)
132132
}
133133
},
134-
async addNewSnippet () {
134+
async addNewSnippet (body?: Partial<Snippet>) {
135135
const folderStore = useFolderStore()
136-
const body: Partial<Snippet> = {}
137-
138-
body.name = 'Untitled snippet'
139-
body.folderId = folderStore.selectedId || ''
140-
body.isDeleted = false
141-
body.isFavorites = false
142-
body.tagsIds = []
143-
body.content = [
144-
{
145-
label: 'Fragment 1',
146-
language: folderStore.selected?.defaultLanguage || 'plain_text',
147-
value: ''
136+
let _body: Partial<Snippet> = {}
137+
138+
_body.isDeleted = false
139+
_body.isFavorites = false
140+
_body.folderId = ''
141+
_body.tagsIds = []
142+
143+
if (body) {
144+
_body = {
145+
..._body,
146+
name: body.name,
147+
content: body.content
148148
}
149-
]
149+
} else {
150+
_body.name = 'Untitled snippet'
151+
_body.folderId = folderStore.selectedId || ''
152+
_body.content = [
153+
{
154+
label: 'Fragment 1',
155+
language: folderStore.selected?.defaultLanguage || 'plain_text',
156+
value: ''
157+
}
158+
]
159+
}
150160

151-
const { data } = await useApi('/snippets').post(body).json()
161+
const { data } = await useApi('/snippets').post(_body).json()
152162

153163
this.selected = data.value
154164
store.app.set('selectedSnippetId', this.selected!.id)

src/shared/types/main/analytics.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,19 @@ type AppEvents =
2525
| 'empty-trash'
2626
| 'set-theme'
2727
| 'notify'
28+
type ApiEvents = 'snippet-create'
2829

2930
type TrackSnippetEvents = CombineWith<SnippetEvents, 'snippets'>
3031
type TrackFolderEvents = CombineWith<FolderEvents, 'folders'>
3132
type TrackTagEvents = CombineWith<TagEvents, 'tags'>
3233
type TrackAppEvents = CombineWith<AppEvents, 'app'>
34+
type TrackApiEvents = CombineWith<ApiEvents, 'api'>
3335

3436
export type TrackEvents =
3537
| TrackSnippetEvents
3638
| TrackFolderEvents
3739
| TrackTagEvents
3840
| TrackAppEvents
41+
| TrackApiEvents
3942
| 'main'
4043
| 'preferences'

src/shared/types/main/index.d.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,19 @@ type MainAction =
4848
| 'update-available'
4949
| 'open-url'
5050

51+
type ApiAction = 'snippet-create'
52+
5153
export type ContextMenuChannel = CombineWith<ChannelSubject, 'context-menu'>
5254

5355
export type MainMenuChannel = CombineWith<MainMenuAction, 'main-menu'>
5456
export type MainChannel = CombineWith<MainAction, 'main'>
57+
export type ApiChannel = CombineWith<ApiAction, 'api'>
5558

56-
export type Channel = ContextMenuChannel | MainMenuChannel | MainChannel
59+
export type Channel =
60+
| ContextMenuChannel
61+
| MainMenuChannel
62+
| MainChannel
63+
| ApiChannel
5764
export interface ContextMenuRequest {
5865
name?: string
5966
type: ContextMenuType

src/shared/types/renderer/composable/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export type EmitterEvents = {
22
'snippet:focus-name': boolean
33
'snippet:format': boolean
4+
'snippet:create': any
45
'folder:click': any
56
'folder:rename': string
67
'scroll-to:folder': string

0 commit comments

Comments
 (0)