File tree Expand file tree Collapse file tree 7 files changed +62
-17
lines changed Expand file tree Collapse file tree 7 files changed +62
-17
lines changed Original file line number Diff line number Diff line change @@ -8,6 +8,7 @@ import type { Server } from 'http'
88import type { Socket } from 'net'
99import { remove } from 'lodash'
1010import type { SnippetWithFolder } from '@shared/types/renderer/store/snippets'
11+ import { BrowserWindow } from 'electron'
1112
1213interface 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 ( )
Original file line number Diff line number Diff line change @@ -25,10 +25,12 @@ import {
2525 onAddNewFragment ,
2626 onAddNewFolder ,
2727 onCopySnippet ,
28- emitter
28+ emitter ,
29+ onCreateSnippet
2930} from ' @/composable'
3031import { createToast , destroyAllToasts } from ' vercel-toast'
3132import { 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', () => {
177179ipc .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">
Original file line number Diff line number Diff line change @@ -6,6 +6,7 @@ import { useFolderStore } from '@/store/folders'
66import { useSnippetStore } from '@/store/snippets'
77import { ipc , track } from '@/electron'
88import type { NotificationRequest } from '@shared/types/main'
9+ import type { Snippet } from '@shared/types/main/db'
910
1011export 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+
3444export const onAddNewFragment = ( ) => {
3545 const snippetStore = useSnippetStore ( )
3646
Original file line number Diff line number Diff 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 )
Original file line number Diff line number Diff line change @@ -25,16 +25,19 @@ type AppEvents =
2525 | 'empty-trash'
2626 | 'set-theme'
2727 | 'notify'
28+ type ApiEvents = 'snippet-create'
2829
2930type TrackSnippetEvents = CombineWith < SnippetEvents , 'snippets' >
3031type TrackFolderEvents = CombineWith < FolderEvents , 'folders' >
3132type TrackTagEvents = CombineWith < TagEvents , 'tags' >
3233type TrackAppEvents = CombineWith < AppEvents , 'app' >
34+ type TrackApiEvents = CombineWith < ApiEvents , 'api' >
3335
3436export type TrackEvents =
3537 | TrackSnippetEvents
3638 | TrackFolderEvents
3739 | TrackTagEvents
3840 | TrackAppEvents
41+ | TrackApiEvents
3942 | 'main'
4043 | 'preferences'
Original file line number Diff line number Diff line change @@ -48,12 +48,19 @@ type MainAction =
4848 | 'update-available'
4949 | 'open-url'
5050
51+ type ApiAction = 'snippet-create'
52+
5153export type ContextMenuChannel = CombineWith < ChannelSubject , 'context-menu' >
5254
5355export type MainMenuChannel = CombineWith < MainMenuAction , 'main-menu' >
5456export 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
5764export interface ContextMenuRequest {
5865 name ?: string
5966 type : ContextMenuType
Original file line number Diff line number Diff line change 11export 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
You can’t perform that action at this time.
0 commit comments