From f45d1fed72c05efae9f48e30e4bbb5f4bd6b3592 Mon Sep 17 00:00:00 2001 From: rdunk Date: Fri, 2 Feb 2024 14:37:12 +0000 Subject: [PATCH] feat: support visual editing --- .nuxtrc | 4 + index.d.ts | 55 ++- package.json | 7 +- 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 | 118 ++++-- src/module.ts | 256 +++++++++++-- src/runtime/client.ts | 1 - src/runtime/composables.ts | 19 +- 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 | 5 + src/runtime/visual-editing/plugins/server.ts | 21 ++ src/types/client.ts | 3 + 19 files changed, 894 insertions(+), 93 deletions(-) create mode 100644 .nuxtrc 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/.nuxtrc b/.nuxtrc new file mode 100644 index 000000000..86df7d9d2 --- /dev/null +++ b/.nuxtrc @@ -0,0 +1,4 @@ +# sanity.apiVersion=2021-03-25 +# sanity.stega=true +# sanity.visualEditing.token=token +# sanity.visualEditing.studioUrl="http://localhost:3333" \ No newline at end of file diff --git a/index.d.ts b/index.d.ts index 893e63406..6c094815e 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 560efb4a8..2dd7e4841 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.3.10", + "@sanity/overlays": "^2.3.9", + "@sanity/preview-url-secret": "^1.4.1", "chalk": "^5.3.0", "defu": "^6.1.3", "knitwork": "^1.0.0", @@ -61,7 +64,9 @@ "@nuxt/module-builder": "0.5.5", "@nuxt/schema": "3.9.0", "@nuxt/test-utils": "3.9.0", - "@sanity/client": "6.11.2", + "@sanity/client": "6.12.3", + "@sanity/core-loader": "^1.3.3", + "@sanity/overlays": "^2.3.1", "@vitest/coverage-v8": "1.1.1", "@vue/test-utils": "2.4.3", "bumpp": "9.2.1", diff --git a/playground/cms/sanity.config.ts b/playground/cms/sanity.config.ts index 69df8204d..a2b3fdd6b 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 52dc8882c..da6ff4f05 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 fef5a34b1..4571438cf 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 90c6b96d7..329cd9b02 100644 --- a/playground/pages/movie/[slug].vue +++ b/playground/pages/movie/[slug].vue @@ -1,7 +1,9 @@