@tinacms/vercel-previews 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +52 -76
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,19 +1,25 @@
|
|
|
1
1
|
## Getting started
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
⚠️ You must be a Vercel Enterprise customer for Visual Editing ⚠️
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
> For a quick start, use [the starter](https://github.com/tinacms/vercel-edit-demo)
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
To use Vercel's [visual editing](https://vercel.com/blog/visual-editing) features with Tina your site should already be configured to work with Tina's `useTina` hook. Refer to this guide to get started with [Tina's visual edting](https://tina.io/docs/contextual-editing/overview/).
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
Vercel's visual editing mode works by adding metadata to your page about where the content comes from. To get started, add the preview package:
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
```
|
|
12
|
+
npm install @tinacms/vercel-previews
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
There are two ways to indicate that a field is editable, **automatically** by enabling string-encoding and **manually** by adding data attributes to editable elements on your page.
|
|
16
|
+
|
|
17
|
+
### Enabling string-encoding
|
|
12
18
|
|
|
13
19
|
```tsx
|
|
14
20
|
// pages/[slug].tsx
|
|
15
21
|
import { useTina } from 'tinacms/dist/react'
|
|
16
|
-
import {
|
|
22
|
+
import { useVisualEditing } from '@tinacms/vercel-previews'
|
|
17
23
|
|
|
18
24
|
export const Post = (props) => {
|
|
19
25
|
const { data: tinaData } = useTina(props)
|
|
@@ -40,6 +46,46 @@ export const Post = (props) => {
|
|
|
40
46
|
}
|
|
41
47
|
})
|
|
42
48
|
|
|
49
|
+
return (
|
|
50
|
+
<div>
|
|
51
|
+
<h1>
|
|
52
|
+
{data.title}
|
|
53
|
+
</h1>
|
|
54
|
+
</div>
|
|
55
|
+
)
|
|
56
|
+
}
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
### Manually adding data attributes
|
|
60
|
+
|
|
61
|
+
String-encoding can break things like links or images. If you decide to turn string-encoding off you can still enable
|
|
62
|
+
visual editing with the `[data-vercel-edit-info]` attribute.
|
|
63
|
+
|
|
64
|
+
If an element has a `[data-vercel-edit-info]` attribute on it, it will be considered editable.
|
|
65
|
+
Tina provides a helper function to add the necessary metadata to your element:
|
|
66
|
+
|
|
67
|
+
#### The `vercelEditInfo` helper
|
|
68
|
+
|
|
69
|
+
```tsx
|
|
70
|
+
// pages/[slug].tsx
|
|
71
|
+
import { useTina } from 'tinacms/dist/react'
|
|
72
|
+
import { vercelEditInfo, useVisualEditing } from '@tinacms/vercel-previews'
|
|
73
|
+
|
|
74
|
+
export const Post = (props) => {
|
|
75
|
+
const { data: tinaData } = useTina(props)
|
|
76
|
+
const data = useVisualEditing({
|
|
77
|
+
data: tinaData,
|
|
78
|
+
// metadata is derived from the query and variables
|
|
79
|
+
query: props.query,
|
|
80
|
+
variables: props.variables,
|
|
81
|
+
// When clicking on an editable element for the first time, redirect to the TinaCMS app
|
|
82
|
+
redirect: '/admin',
|
|
83
|
+
// Only enable visual editing on preview deploys
|
|
84
|
+
enabled: props.visualEditingEnabled
|
|
85
|
+
// stringEncoding automatically adds metadata to strings
|
|
86
|
+
stringEncoding: false
|
|
87
|
+
})
|
|
88
|
+
|
|
43
89
|
return (
|
|
44
90
|
<div>
|
|
45
91
|
<h1 data-vercel-edit-info={vercelEditInfo(data, 'title')}>
|
|
@@ -75,73 +121,3 @@ export const NewsletterBlock = (props) => {
|
|
|
75
121
|
)
|
|
76
122
|
}
|
|
77
123
|
```
|
|
78
|
-
|
|
79
|
-
### Encoded Strings
|
|
80
|
-
|
|
81
|
-
If an element’s `textContent` contains an encoded string, it will be considered editable.
|
|
82
|
-
This feature is not enabled by default, to enable:
|
|
83
|
-
|
|
84
|
-
> NOTE: Most likely, you won't want all of your fields to be encoded, the encoding process
|
|
85
|
-
> only applies to string values, but it alters them in a way that makes them unusable for
|
|
86
|
-
> anything other than rendering. Take note of the `encodeAtPath` callback function, which
|
|
87
|
-
> controls whether a value at the given path recieves the encoding info.
|
|
88
|
-
|
|
89
|
-
#### Add the plugin to the TinaCMS config:
|
|
90
|
-
|
|
91
|
-
```js
|
|
92
|
-
// tina/config.ts
|
|
93
|
-
export default defineConfig({
|
|
94
|
-
cmsCallback: (cms) => {
|
|
95
|
-
cms.plugins.add(createSourceMapEncoder(encodeAtPath))
|
|
96
|
-
|
|
97
|
-
return cms
|
|
98
|
-
},
|
|
99
|
-
})
|
|
100
|
-
|
|
101
|
-
// Export this so it can be re-used in the next section
|
|
102
|
-
export const encodeAtPath = (path, value) => {
|
|
103
|
-
if (path === 'page.blocks.0.headline') {
|
|
104
|
-
console.log(path)
|
|
105
|
-
return true
|
|
106
|
-
}
|
|
107
|
-
return false
|
|
108
|
-
}
|
|
109
|
-
```
|
|
110
|
-
|
|
111
|
-
### Use the `withSourceMaps` helper
|
|
112
|
-
|
|
113
|
-
```js
|
|
114
|
-
// pages/[slug].tsx
|
|
115
|
-
import { encodeAtPath } from '../tina/config'
|
|
116
|
-
|
|
117
|
-
export const getStaticProps = async ({ params }) => {
|
|
118
|
-
const tinaProps = await client.queries.contentQuery({
|
|
119
|
-
relativePath: `${params.filename}.md`,
|
|
120
|
-
})
|
|
121
|
-
return {
|
|
122
|
-
props: withSourceMaps(tinaProps, { encodeStrings: true, encodeAtPath }),
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
```
|
|
126
|
-
|
|
127
|
-
## Listen for clicks on a visual element
|
|
128
|
-
|
|
129
|
-
When a user clicks to edit a visual element, Tina will redirect to the same URL
|
|
130
|
-
within an iframe, and will select the appropriate form and field for editing. After
|
|
131
|
-
the first redirect, subsequent clicks will no longer redirect, but will activate
|
|
132
|
-
the selected field.
|
|
133
|
-
|
|
134
|
-
To set this up, register the hook, provide the redirect URL you set up
|
|
135
|
-
in your `tina/config.ts` file:
|
|
136
|
-
|
|
137
|
-
```jsx
|
|
138
|
-
// pages/[slug].tsx
|
|
139
|
-
import { useEditOpen } from '@tinacms/vercel-previews/dist/react'
|
|
140
|
-
|
|
141
|
-
export default function Page(props) {
|
|
142
|
-
const { data } = useTina(props)
|
|
143
|
-
useEditOpen('/admin')
|
|
144
|
-
|
|
145
|
-
return <MyPage {...data} />
|
|
146
|
-
}
|
|
147
|
-
```
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tinacms/vercel-previews",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.2",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"types": "dist/index.d.ts",
|
|
6
6
|
"module": "./dist/index.es.js",
|
|
@@ -41,7 +41,7 @@
|
|
|
41
41
|
},
|
|
42
42
|
"dependencies": {
|
|
43
43
|
"@vercel/stega": "^0.0.4",
|
|
44
|
-
"tinacms": "1.5.
|
|
44
|
+
"tinacms": "1.5.7"
|
|
45
45
|
},
|
|
46
46
|
"peerDependencies": {
|
|
47
47
|
"react": ">=16.14"
|