From c94fbea3d0f3b5e975d43c945e4202eacefc6a02 Mon Sep 17 00:00:00 2001 From: rdunk Date: Fri, 2 Feb 2024 17:56:13 +0000 Subject: [PATCH 01/15] feat: support visual editing --- index.d.ts | 55 ++- package.json | 3 + playground/cms/sanity.config.ts | 14 +- playground/nuxt.config.js | 7 + playground/pages/index.vue | 49 +-- playground/pages/movie/[slug].vue | 15 +- pnpm-lock.yaml | 88 +++-- src/module.ts | 257 +++++++++++-- src/runtime/client.ts | 1 - src/runtime/composables.ts | 18 +- src/runtime/nitro-imports.ts | 15 +- src/runtime/plugin.ts | 2 +- src/runtime/visual-editing/composables.ts | 358 +++++++++++++++++++ src/runtime/visual-editing/draft/disable.ts | 6 + src/runtime/visual-editing/draft/enable.ts | 32 ++ src/runtime/visual-editing/plugins/client.ts | 6 + src/runtime/visual-editing/plugins/server.ts | 21 ++ src/types/client.ts | 3 + 18 files changed, 858 insertions(+), 92 deletions(-) create mode 100644 src/runtime/visual-editing/composables.ts create mode 100644 src/runtime/visual-editing/draft/disable.ts create mode 100644 src/runtime/visual-editing/draft/enable.ts create mode 100644 src/runtime/visual-editing/plugins/client.ts create mode 100644 src/runtime/visual-editing/plugins/server.ts create mode 100644 src/types/client.ts diff --git a/index.d.ts b/index.d.ts index 893e6340..6c094815 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,7 +1,60 @@ -import { SanityHelper } from './src/runtime/composables' +import type { ClientPerspective, StegaConfig } from '@sanity/client' +import type { SanityHelper } from '#sanity-composables' + +type nullish = null | undefined | void declare module '#app' { interface NuxtApp { _sanity?: Record } } + +declare module 'nuxt/schema' { + interface RuntimeConfig { + sanity: { + visualEditing: + | { + draftMode: + | false + | { + enable: string + disable: string + } + mode: 'global' | 'component' + studioUrl: string + draftModeId: string + token: string + } + | undefined + } + } + + interface PublicRuntimeConfig { + sanity: { + additionalClients: Record + apiVersion: string + dataset: string + disableSmartCdn: boolean + perspective: ClientPerspective + projectId: string + stega: StegaConfig + token: string + useCdn: boolean + visualEditing: + | { + draftMode: + | false + | { + enable: string + disable: string + } + mode: 'global' | 'component' + studioUrl: string + } + | nullish + } + withCredentials: boolean + } +} + +export {} diff --git a/package.json b/package.json index 7163db04..a0878b9c 100755 --- a/package.json +++ b/package.json @@ -49,6 +49,9 @@ "dependencies": { "@nuxt/kit": "^3.9.0", "@portabletext/types": "^2.0.8", + "@sanity/core-loader": "^1.4.0", + "@sanity/overlays": "^2.3.9", + "@sanity/preview-url-secret": "^1.6.0", "chalk": "^5.3.0", "defu": "^6.1.3", "knitwork": "^1.0.0", diff --git a/playground/cms/sanity.config.ts b/playground/cms/sanity.config.ts index 69df8204..a2b3fdd6 100644 --- a/playground/cms/sanity.config.ts +++ b/playground/cms/sanity.config.ts @@ -1,7 +1,9 @@ -import { createConfig } from 'sanity' import { deskTool } from 'sanity/desk' +import { presentationTool } from 'sanity/presentation' +import { createConfig } from 'sanity' import { visionTool } from '@sanity/vision' import { schemaTypes } from './schemas' +import { debugSecrets } from '@sanity/preview-url-secret/sanity-plugin-debug-secrets' export default createConfig({ name: 'default', @@ -12,6 +14,16 @@ export default createConfig({ plugins: [ deskTool(), visionTool(), + presentationTool({ + previewUrl: { + origin: 'http://localhost:3000', + draftMode: { + enable: '/draft/enable', + disable: '/draft/disable', + }, + }, + }), + debugSecrets(), ], schema: { diff --git a/playground/nuxt.config.js b/playground/nuxt.config.js index 52dc8882..da6ff4f0 100644 --- a/playground/nuxt.config.js +++ b/playground/nuxt.config.js @@ -9,8 +9,15 @@ export default defineNuxtConfig({ globalHelper: true, projectId: 'j1o4tmjp', dataset: 'production', + apiVersion: '2021-03-25', additionalClients: { another: {}, }, + visualEditing: { + stega: true, + draftMode: true, + token: process.env.NUXT_SANITY_VISUAL_EDITING_TOKEN, + studioUrl: 'http://localhost:3333', + }, }, }) diff --git a/playground/pages/index.vue b/playground/pages/index.vue index fef5a34b..7ede7840 100644 --- a/playground/pages/index.vue +++ b/playground/pages/index.vue @@ -1,27 +1,30 @@ @@ -38,7 +41,7 @@ interface QueryResult { slug: string } -const sanity = useSanity() - -const { data: movies } = await useAsyncData('movies', () => sanity.fetch(query)) +const { data: movies, encodeDataAttribute } = await useSanityQuery< + QueryResult[] +>(query) diff --git a/playground/pages/movie/[slug].vue b/playground/pages/movie/[slug].vue index 90c6b96d..fd6fc1c3 100644 --- a/playground/pages/movie/[slug].vue +++ b/playground/pages/movie/[slug].vue @@ -1,7 +1,9 @@