Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/main/api/dto/folders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Elysia, { t } from 'elysia'

const foldersAdd = t.Object({
name: t.String(),
parentId: t.Optional(t.Union([t.Number(), t.Null()])),
})

const foldersUpdate = t.Object({
Expand Down
9 changes: 5 additions & 4 deletions src/main/api/routes/folders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,18 +90,19 @@ app
'/',
({ body }) => {
const db = useDB()
const { name } = body
const { name, parentId } = body
const now = Date.now()

// (parentId)
const { maxOrder } = db
.prepare(
`
SELECT COALESCE(MAX(orderIndex), -1) as maxOrder
FROM folders
WHERE parentId IS NULL
WHERE parentId ${parentId ? '= ?' : 'IS NULL'}
`,
)
.get() as { maxOrder: number }
.get(...(parentId ? [parentId] : [])) as { maxOrder: number }

const newOrder = maxOrder + 1

Expand All @@ -120,7 +121,7 @@ app
const { lastInsertRowid } = stmt.run(
name,
'plain_text',
null,
parentId ?? null,
0,
now,
now,
Expand Down
9 changes: 7 additions & 2 deletions src/main/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable node/prefer-global/process */
import { readFileSync } from 'node:fs'
import { existsSync, readFileSync } from 'node:fs'
import path from 'node:path'
import { app, BrowserWindow, Menu } from 'electron'
import { initApi } from './api'
Expand Down Expand Up @@ -115,7 +115,12 @@ else {
}

try {
const jsonDbPath = `${store.preferences.get('storagePath')}/db.json`
const storagePath = store.preferences.get('storagePath')
const jsonDbPath = `${storagePath}/db.json`

if (!existsSync(jsonDbPath))
return

const jsonData = readFileSync(jsonDbPath, 'utf8')

migrateJsonToSqlite(JSON.parse(jsonData))
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/components/sidebar/folders/Tree.vue
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ provide(treeKeys, {
</ContextMenu.Item>
</template>
<template v-else>
<ContextMenu.Item @click="createFolderAndSelect">
<ContextMenu.Item @click="createFolderAndSelect(contextNode?.id)">
{{ i18n.t("action.new.folder") }}
</ContextMenu.Item>
<ContextMenu.Separator />
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/components/sidebar/library/Library.vue
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ function onResizeTagList(val: number[]) {
</div>
<UiActionButton
:tooltip="i18n.t('action.new.folder')"
@click="createFolderAndSelect"
@click="createFolderAndSelect()"
>
<Plus class="h-4 w-4" />
</UiActionButton>
Expand Down
11 changes: 8 additions & 3 deletions src/renderer/composables/useFolders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,12 +281,17 @@ async function getFolders(shouldEnsureVisibility = true) {
}
}

async function createFolder() {
async function createFolder(parentId?: number) {
try {
const { data } = await api.folders.postFolders({
name: i18n.t('folder.untitled'),
...(parentId !== undefined && { parentId }),
})

if (parentId) {
await updateFolder(parentId, { isOpen: 1 })
}

await getFolders(false)

return data.id
Expand All @@ -296,9 +301,9 @@ async function createFolder() {
}
}

async function createFolderAndSelect() {
async function createFolderAndSelect(parentId?: number) {
const { clearSnippetsState } = useSnippets()
const id = await createFolder()
const id = await createFolder(parentId)

if (id) {
await selectFolder(Number(id))
Expand Down
5 changes: 4 additions & 1 deletion src/renderer/services/api/generated/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,9 @@ export type SnippetsResponse = {

export interface FoldersAdd {
name: string;
parentId?:
| (string | (string | (string | (string | (string | (string | number))))))
| null;
}

export type FoldersResponse = {
Expand Down Expand Up @@ -465,7 +468,7 @@ export class HttpClient<SecurityDataType = unknown> {

/**
* @title massCode API
* @version 3.11.0
* @version 4.2.2
*
* Development documentation
*/
Expand Down