Skip to content

Commit c43b918

Browse files
committed
up
1 parent f97c6b1 commit c43b918

File tree

8 files changed

+55
-31
lines changed

8 files changed

+55
-31
lines changed

src/app/src/components/shared/item/ItemCardForm.vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { useStudio } from '../../../composables/useStudio'
99
import { StudioItemActionId } from '../../../types'
1010
import { stripNumericPrefix } from '../../../utils/string'
1111
import { defineShortcuts } from '#imports'
12+
import { upperFirst } from 'scule'
1213
1314
const { context } = useStudio()
1415
@@ -93,7 +94,7 @@ function onSubmit(_event: FormSubmitEvent<Schema>) {
9394
case StudioItemActionId.CreateDocument:
9495
params = {
9596
fsPath: withoutLeadingSlash(joinURL(props.parentItem.fsPath, `${state.name}.${state.extension}`)),
96-
content: `New ${state.name} file`,
97+
content: `# ${upperFirst(state.name)} file`,
9798
}
9899
break
99100
case StudioItemActionId.CreateFolder:

src/app/src/composables/useContext.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { useRoute } from 'vue-router'
1919
import { findDescendantsFileItemsFromId } from '../utils/tree'
2020
import type { useDraftMedias } from './useDraftMedias'
2121
import { joinURL } from 'ufo'
22+
import { upperFirst } from 'scule'
2223

2324
export const useContext = createSharedComposable((
2425
host: StudioHost,
@@ -80,9 +81,12 @@ export const useContext = createSharedComposable((
8081
const navigationDocumentFsPath = joinURL(fsPath, '.navigation.yml')
8182

8283
const navigationDocument = await host.document.create(navigationDocumentFsPath, `title: ${folderName}`)
83-
const rootDocument = await host.document.create(rootDocumentFsPath, `New ${folderName} root file`)
84+
const rootDocument = await host.document.create(rootDocumentFsPath, `# ${upperFirst(folderName)} root file`)
8485

8586
await activeTree.value.draft.create(navigationDocument)
87+
88+
unsetActionInProgress()
89+
8690
const rootDocumentDraftItem = await activeTree.value.draft.create(rootDocument)
8791

8892
await activeTree.value.selectItemById(rootDocumentDraftItem.id)

src/app/src/composables/useDraftDocuments.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import { generateContentFromDocument } from '../utils/content'
88
import { findDescendantsFromId, getDraftStatus } from '../utils/draft'
99
import { createSharedComposable } from '@vueuse/core'
1010
import { useHooks } from './useHooks'
11-
import { stripNumericPrefix } from '../utils/string'
1211
import { joinURL } from 'ufo'
1312

1413
const storage = createStorage({
@@ -178,15 +177,13 @@ export const useDraftDocuments = createSharedComposable((host: StudioHost, git:
178177
throw new Error(`Database item not found for document ${id}`)
179178
}
180179

181-
const nameWithoutExtension = newFsPath.split('/').pop()!.split('.').slice(0, -1).join('.')
182-
const newRoutePath = `${currentDbItem.path!.split('/').slice(0, -1).join('/')}/${nameWithoutExtension}`
183180
const content = await generateContentFromDocument(currentDbItem)
184181

185182
// Delete renamed draft item
186183
await remove([id])
187184

188185
// Create new draft item
189-
const newDbItem = await host.document.create(newFsPath, newRoutePath, content!)
186+
const newDbItem = await host.document.create(newFsPath, content!)
190187
return await create(newDbItem, currentDbItem)
191188
}
192189

src/app/src/composables/useStudio.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ export const useStudio = createSharedComposable(() => {
2424
const isReady = ref(false)
2525
const ui = useUI(host)
2626
const draftDocuments = useDraftDocuments(host, git)
27-
const documentTree = useTree(StudioFeature.Content, host, draftDocuments)
27+
const documentTree = useTree(StudioFeature.Content, host, ui, draftDocuments)
2828

2929
const draftMedias = useDraftMedias(host, git)
30-
const mediaTree = useTree(StudioFeature.Media, host, draftMedias)
30+
const mediaTree = useTree(StudioFeature.Media, host, ui, draftMedias)
3131

3232
const context = useContext(host, documentTree, mediaTree)
3333

@@ -40,6 +40,10 @@ export const useStudio = createSharedComposable(() => {
4040

4141
host.on.routeChange(async (to: RouteLocationNormalized, _from: RouteLocationNormalized) => {
4242
if (ui.isOpen.value && ui.config.value.syncEditorAndRoute) {
43+
if (documentTree.currentItem.value.routePath === to.path) {
44+
return
45+
}
46+
4347
await documentTree.selectByRoute(to)
4448
}
4549
// setTimeout(() => {

src/app/src/composables/useTree.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ import type { useDraftMedias } from './useDraftMedias'
55
import { buildTree, findItemFromId, findItemFromRoute, ROOT_ITEM, findParentFromId } from '../utils/tree'
66
import type { RouteLocationNormalized } from 'vue-router'
77
import { useHooks } from './useHooks'
8+
import type { useUI } from './useUI'
89

9-
export const useTree = (type: StudioFeature, host: StudioHost, draft: ReturnType<typeof useDraftDocuments | typeof useDraftMedias>) => {
10+
export const useTree = (type: StudioFeature, host: StudioHost, ui: ReturnType<typeof useUI>, draft: ReturnType<typeof useDraftDocuments | typeof useDraftMedias>) => {
1011
const hooks = useHooks()
1112

1213
const tree = ref<TreeItem[]>([])
@@ -40,7 +41,7 @@ export const useTree = (type: StudioFeature, host: StudioHost, draft: ReturnType
4041
async function select(item: TreeItem) {
4142
currentItem.value = item || ROOT_ITEM
4243
if (item?.type === 'file') {
43-
if (type === StudioFeature.Content) {
44+
if (type === StudioFeature.Content && ui.config.value.syncEditorAndRoute) {
4445
host.app.navigateTo(item.routePath!)
4546
}
4647

src/app/src/utils/content.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ export async function generateDocumentFromContent(id: string, content: string):
8282
return null
8383
}
8484

85-
async function generateDocumentFromYAMLContent(id: string, content: string): Promise<DatabaseItem | null> {
85+
async function generateDocumentFromYAMLContent(id: string, content: string): Promise<DatabaseItem> {
8686
const { data } = parseFrontMatter(`---\n${content}\n---`)
8787

8888
// Keep array contents under `body` key
@@ -93,14 +93,16 @@ async function generateDocumentFromYAMLContent(id: string, content: string): Pro
9393
}
9494

9595
return {
96+
id,
97+
extension: ContentFileExtension.YAML,
98+
stem: id.split('.').slice(0, -1).join('.'),
9699
meta: {},
97100
...parsed,
98101
body: parsed.body || parsed,
99-
id,
100-
} as unknown as DatabaseItem
102+
} as DatabaseItem
101103
}
102104

103-
async function generateDocumentFromJSONContent(id: string, content: string): Promise<DatabaseItem | null> {
105+
async function generateDocumentFromJSONContent(id: string, content: string): Promise<DatabaseItem> {
104106
let parsed: Record<string, unknown> = destr(content)
105107

106108
// Keep array contents under `body` key
@@ -112,14 +114,16 @@ async function generateDocumentFromJSONContent(id: string, content: string): Pro
112114
}
113115

114116
return {
117+
id,
118+
extension: ContentFileExtension.JSON,
119+
stem: id.split('.').slice(0, -1).join('.'),
115120
meta: {},
116121
...parsed,
117122
body: parsed.body || parsed,
118-
id,
119-
} as unknown as DatabaseItem
123+
} as DatabaseItem
120124
}
121125

122-
async function generateDocumentFromMarkdownContent(id: string, content: string): Promise<DatabaseItem | null> {
126+
async function generateDocumentFromMarkdownContent(id: string, content: string): Promise<DatabaseItem> {
123127
const document = await parseMarkdown(content, {
124128
remark: {
125129
plugins: {
@@ -144,12 +148,14 @@ async function generateDocumentFromMarkdownContent(id: string, content: string):
144148
return {
145149
id,
146150
meta: {},
151+
extension: ContentFileExtension.Markdown,
152+
stem: id.split('.').slice(0, -1).join('.'),
147153
body: {
148154
...body,
149155
toc: document.toc,
150156
},
151157
...document.data,
152-
} as unknown as DatabaseItem
158+
} as DatabaseItem
153159
}
154160

155161
export async function generateContentFromDocument(document: DatabaseItem): Promise<string | null> {

src/app/src/utils/database.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { compressTree } from '@nuxt/content/runtime'
22
import { type DatabasePageItem, ContentFileExtension } from '../types'
33
import { stringify } from 'minimark/stringify'
4-
import { MDCRoot } from '@nuxtjs/mdc'
5-
import { MarkdownRoot } from '@nuxt/content'
4+
import type { MDCRoot } from '@nuxtjs/mdc'
5+
import type { MarkdownRoot } from '@nuxt/content'
66

77
export function isEqual(document1: DatabasePageItem, document2: DatabasePageItem) {
88
function withoutLastStyles(body: MarkdownRoot) {
@@ -18,10 +18,10 @@ export function isEqual(document1: DatabasePageItem, document2: DatabasePageItem
1818
// Compare body first
1919
if (document1.extension === ContentFileExtension.Markdown) {
2020
const minifiedBody1 = withoutLastStyles(
21-
document1.body.type === 'minimark' ? document1.body : compressTree(document1.body as unknown as MDCRoot)
21+
document1.body.type === 'minimark' ? document1.body : compressTree(document1.body as unknown as MDCRoot),
2222
)
2323
const minifiedBody2 = withoutLastStyles(
24-
document2.body.type === 'minimark' ? document2.body : compressTree(document2.body as unknown as MDCRoot)
24+
document2.body.type === 'minimark' ? document2.body : compressTree(document2.body as unknown as MDCRoot),
2525
)
2626

2727
if (stringify(minifiedBody1) !== stringify(minifiedBody2)) {

src/module/src/runtime/utils/content.ts

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export async function generateDocumentFromContent(id: string, content: string):
5757
return null
5858
}
5959

60-
async function generateDocumentFromYAMLContent(id: string, content: string): Promise<DatabaseItem | null> {
60+
async function generateDocumentFromYAMLContent(id: string, content: string): Promise<DatabaseItem> {
6161
const { data } = parseFrontMatter(`---\n${content}\n---`)
6262

6363
// Keep array contents under `body` key
@@ -68,14 +68,16 @@ async function generateDocumentFromYAMLContent(id: string, content: string): Pro
6868
}
6969

7070
return {
71+
id,
72+
extension: ContentFileExtension.YAML,
73+
stem: id.split('.').slice(0, -1).join('.'),
7174
meta: {},
7275
...parsed,
7376
body: parsed.body || parsed,
74-
id,
75-
} as unknown as DatabaseItem
77+
} as DatabaseItem
7678
}
7779

78-
async function generateDocumentFromJSONContent(id: string, content: string): Promise<DatabaseItem | null> {
80+
async function generateDocumentFromJSONContent(id: string, content: string): Promise<DatabaseItem> {
7981
let parsed: Record<string, unknown> = destr(content)
8082

8183
// Keep array contents under `body` key
@@ -87,14 +89,16 @@ async function generateDocumentFromJSONContent(id: string, content: string): Pro
8789
}
8890

8991
return {
92+
id,
93+
extension: ContentFileExtension.JSON,
94+
stem: id.split('.').slice(0, -1).join('.'),
9095
meta: {},
9196
...parsed,
9297
body: parsed.body || parsed,
93-
id,
94-
} as unknown as DatabaseItem
98+
} as DatabaseItem
9599
}
96100

97-
async function generateDocumentFromMarkdownContent(id: string, content: string): Promise<DatabaseItem | null> {
101+
async function generateDocumentFromMarkdownContent(id: string, content: string): Promise<DatabaseItem> {
98102
const document = await parseMarkdown(content, {
99103
remark: {
100104
plugins: {
@@ -107,6 +111,8 @@ async function generateDocumentFromMarkdownContent(id: string, content: string):
107111
},
108112
})
109113

114+
console.log('document =>', document)
115+
110116
// Remove nofollow from links
111117
visit(document.body, (node: Node) => (node as MDCElement).type === 'element' && (node as MDCElement).tag === 'a', (node: Node) => {
112118
if ((node as MDCElement).props?.rel?.join(' ') === 'nofollow') {
@@ -119,7 +125,12 @@ async function generateDocumentFromMarkdownContent(id: string, content: string):
119125
return {
120126
id,
121127
meta: {},
122-
body,
128+
extension: ContentFileExtension.Markdown,
129+
stem: id.split('.').slice(0, -1).join('.'),
130+
body: {
131+
...body,
132+
toc: document.toc,
133+
},
123134
...document.data,
124-
} as unknown as DatabaseItem
135+
} as DatabaseItem
125136
}

0 commit comments

Comments
 (0)