Skip to content

Commit b49f1d0

Browse files
committed
refactor(studio): move main logic in runtime
1 parent 99f6f2f commit b49f1d0

File tree

11 files changed

+28
-32
lines changed

11 files changed

+28
-32
lines changed

src/runtime/components/StudioPreviewMode.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<script setup lang="ts">
22
import { onMounted, ref, onUnmounted } from 'vue'
33
import type { Socket } from 'socket.io-client'
4-
import type { DraftSyncData } from '../../types/studio'
4+
import type { DraftSyncData } from '@nuxt/content'
55
import { useCookie, useNuxtApp, useRouter } from '#app'
66
77
const props = defineProps({

src/runtime/internal/schema.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import type { ZodRawShape } from 'zod'
2+
3+
export function getOrderedSchemaKeys(shape: ZodRawShape) {
4+
const keys = new Set([
5+
shape.id ? 'id' : undefined,
6+
shape.title ? 'title' : undefined,
7+
...Object.keys(shape).sort(),
8+
].filter(Boolean))
9+
10+
return Array.from(keys) as string[]
11+
}

src/utils/studio/collection.ts renamed to src/runtime/internal/studio/collection.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import micromatch from 'micromatch'
2-
import type { CollectionInfo } from '../../types/collection'
2+
import type { CollectionInfo } from '@nuxt/content'
33
import { getOrderedSchemaKeys } from '../schema'
44
import { withoutRoot } from './files'
55

src/utils/studio/compatibility.ts renamed to src/runtime/internal/studio/compatibility.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import type { CollectionInfo } from '../../types/collection'
2-
import type { DraftSyncFile } from '../../types/studio'
1+
import type { CollectionInfo, DraftSyncFile } from '@nuxt/content'
32

43
export const v2ToV3ParsedFile = (file: DraftSyncFile, collection: CollectionInfo) => {
54
const mappedFile: Record<string, unknown> = {
File renamed without changes.

src/runtime/internal/studio.ts renamed to src/runtime/internal/studio/index.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import { createApp } from 'vue'
22
import type { AppConfig } from 'nuxt/schema'
3-
import type { CollectionInfo } from '@nuxt/content'
3+
import type { CollectionInfo, FileChangeMessagePayload, FileMessageData, FileSelectMessagePayload, DraftSyncData, PreviewFile, DraftSyncFile } from '@nuxt/content'
44
import { withLeadingSlash } from 'ufo'
5-
import StudioPreviewMode from '../components/StudioPreviewMode.vue'
6-
import { FileMessageType, type FileChangeMessagePayload, type FileMessageData, type FileSelectMessagePayload, type DraftSyncData, type PreviewFile, type DraftSyncFile } from '../../types/studio'
7-
import { createSingleton, deepAssign, deepDelete, defu, generateStemFromPath, mergeDraft, StudioConfigFiles, withoutRoot } from '../../utils/studio'
8-
import { loadDatabaseAdapter } from '../internal/database.client'
9-
import { getCollectionByPath, generateCollectionInsert, generateRecordDeletion, generateRecordSelectByColumn, generateRecordUpdate } from '../../utils/studio/collection'
10-
import { v2ToV3ParsedFile } from '../../utils/studio/compatibility'
5+
import StudioPreviewMode from '../../components/StudioPreviewMode.vue'
6+
import { loadDatabaseAdapter } from '../database.client'
7+
import { v2ToV3ParsedFile } from './compatibility'
8+
import { getCollectionByPath, generateCollectionInsert, generateRecordDeletion, generateRecordSelectByColumn, generateRecordUpdate } from './collection'
9+
import { createSingleton, deepAssign, deepDelete, defu, generateStemFromPath, mergeDraft, StudioConfigFiles, withoutRoot } from './utils'
1110
import { callWithNuxt, refreshNuxtData } from '#app'
1211
import { useAppConfig, useNuxtApp, useRuntimeConfig, useRoute, useRouter, ref } from '#imports'
1312
import { collections } from '#content/studio'
@@ -131,12 +130,12 @@ export function initIframeCommunication() {
131130
const { type, payload = {}, navigate } = e.data || {}
132131

133132
switch (type) {
134-
case FileMessageType.FileSelected: {
133+
case 'nuxt-studio:editor:file-selected': {
135134
await handleFileSelection((payload as FileSelectMessagePayload).path)
136135
break
137136
}
138-
case FileMessageType.FileChanged:
139-
case FileMessageType.MediaChanged: {
137+
case 'nuxt-studio:editor:file-changed':
138+
case 'nuxt-studio:editor:media-changed': {
140139
const { additions = [], deletions = [] } = payload as FileChangeMessagePayload
141140
for (const addition of additions) {
142141
await handleFileUpdate(addition, navigate)
@@ -148,7 +147,7 @@ export function initIframeCommunication() {
148147
rerenderPreview()
149148
break
150149
}
151-
case FileMessageType.ConfigFileChanged: {
150+
case 'nuxt-studio:config:file-changed': {
152151
const { additions = [], deletions = [] } = payload as FileChangeMessagePayload
153152

154153
const appConfig = additions.find(item => [StudioConfigFiles.appConfig, StudioConfigFiles.appConfigV4].includes(item.path))
File renamed without changes.

src/types/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ export type * from './query'
66
export type * from './content'
77
export type * from './tree'
88
export type * from './database'
9+
export type * from './studio'

src/types/studio.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,7 @@ export interface FileSelectMessagePayload {
6666
path: string
6767
}
6868

69-
export const enum FileMessageType {
70-
FileSelected = 'nuxt-studio:editor:file-selected',
71-
FileChanged = 'nuxt-studio:editor:file-changed',
72-
MediaChanged = 'nuxt-studio:editor:media-changed',
73-
ConfigFileChanged = 'nuxt-studio:config:file-changed',
74-
}
69+
export type FileMessageType = 'nuxt-studio:editor:file-selected' | 'nuxt-studio:editor:file-changed' | 'nuxt-studio:editor:media-changed' | 'nuxt-studio:config:file-changed'
7570

7671
export interface FileMessageData {
7772
type: FileMessageType

src/utils/collection.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import type { ZodObject, ZodOptionalDef, ZodRawShape, ZodStringDef, ZodType } from 'zod'
22
import type { Collection, ResolvedCollection, CollectionSource, DefinedCollection, ResolvedCollectionSource } from '../types/collection'
3+
import { getOrderedSchemaKeys } from '../runtime/internal/schema'
34
import { defineLocalSource, defineGitHubSource } from './source'
4-
import { getOrderedSchemaKeys, metaSchema, pageSchema } from './schema'
5+
import { metaSchema, pageSchema } from './schema'
56
import type { ZodFieldType } from './zod'
67
import { getUnderlyingType, ZodToSqlFieldTypes, z, getUnderlyingTypeName } from './zod'
78
import { logger } from './dev'

0 commit comments

Comments
 (0)