@tinacms/vercel-previews 0.1.11 → 0.1.13
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/dist/index.js +128 -130
- package/package.json +3 -10
- package/dist/index.mjs +0 -137
package/dist/index.js
CHANGED
|
@@ -1,139 +1,137 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
}
|
|
15
|
-
});
|
|
16
|
-
}
|
|
17
|
-
}, []);
|
|
18
|
-
return { edit };
|
|
19
|
-
}
|
|
20
|
-
const tinaField = (object, property, index) => {
|
|
21
|
-
const contentSource = object == null ? void 0 : object._content_source;
|
|
22
|
-
if (!contentSource) {
|
|
23
|
-
return "";
|
|
24
|
-
}
|
|
25
|
-
const { queryId, path } = contentSource;
|
|
26
|
-
if (!property) {
|
|
27
|
-
return `${queryId}---${path.join(".")}`;
|
|
1
|
+
import { vercelStegaCombine } from "@vercel/stega";
|
|
2
|
+
import React, { useCallback, useEffect } from "react";
|
|
3
|
+
function useEditState() {
|
|
4
|
+
const [edit, setEdit] = React.useState(false);
|
|
5
|
+
React.useEffect(() => {
|
|
6
|
+
if (typeof window !== "undefined") {
|
|
7
|
+
parent.postMessage({ type: "isEditMode" }, window.location.origin);
|
|
8
|
+
window.addEventListener("message", (event) => {
|
|
9
|
+
var _a;
|
|
10
|
+
if (((_a = event.data) == null ? void 0 : _a.type) === "tina:editMode") {
|
|
11
|
+
setEdit(true);
|
|
12
|
+
}
|
|
13
|
+
});
|
|
28
14
|
}
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
15
|
+
}, []);
|
|
16
|
+
return { edit };
|
|
17
|
+
}
|
|
18
|
+
const tinaField = (object, property, index) => {
|
|
19
|
+
const contentSource = object == null ? void 0 : object._content_source;
|
|
20
|
+
if (!contentSource) {
|
|
21
|
+
return "";
|
|
22
|
+
}
|
|
23
|
+
const { queryId, path } = contentSource;
|
|
24
|
+
if (!property) {
|
|
25
|
+
return `${queryId}---${path.join(".")}`;
|
|
26
|
+
}
|
|
27
|
+
const fullPath = typeof index === "number" ? [...path, property, index] : [...path, property];
|
|
28
|
+
return `${queryId}---${fullPath.join(".")}`;
|
|
29
|
+
};
|
|
30
|
+
const vercelEditInfo = (obj, field, index) => {
|
|
31
|
+
const fieldName = tinaField(obj, field, index);
|
|
32
|
+
return JSON.stringify({ origin: "tinacms", data: { fieldName } });
|
|
33
|
+
};
|
|
34
|
+
const useVisualEditing = ({
|
|
35
|
+
data,
|
|
36
|
+
query,
|
|
37
|
+
variables,
|
|
38
|
+
enabled,
|
|
39
|
+
redirect,
|
|
40
|
+
stringEncoding
|
|
41
|
+
}) => {
|
|
42
|
+
const stringifiedQuery = JSON.stringify({
|
|
38
43
|
query,
|
|
39
|
-
variables
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
()
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
const handleOpenEvent = React.useCallback(
|
|
54
|
-
(event) => {
|
|
55
|
-
var _a, _b;
|
|
56
|
-
if (edit) {
|
|
57
|
-
parent.postMessage(
|
|
58
|
-
{ type: "field:selected", fieldName: (_b = (_a = event.detail) == null ? void 0 : _a.data) == null ? void 0 : _b.fieldName },
|
|
59
|
-
window.location.origin
|
|
60
|
-
);
|
|
61
|
-
} else {
|
|
62
|
-
const tinaAdminBasePath = redirect.startsWith("/") ? redirect : `/${redirect}`;
|
|
63
|
-
const tinaAdminPath = `${tinaAdminBasePath}/index.html#/~${window.location.pathname}?active-field=${event.detail.data.fieldName}`;
|
|
64
|
-
window.location.assign(tinaAdminPath);
|
|
65
|
-
}
|
|
66
|
-
},
|
|
67
|
-
[edit]
|
|
68
|
-
);
|
|
69
|
-
React.useEffect(() => {
|
|
70
|
-
window.addEventListener("edit:open", handleOpenEvent);
|
|
71
|
-
return () => {
|
|
72
|
-
window.removeEventListener("edit:open", handleOpenEvent);
|
|
73
|
-
};
|
|
74
|
-
}, [redirect, edit]);
|
|
75
|
-
function appendMetadata(obj, path = [], id2) {
|
|
76
|
-
if (typeof obj !== "object" || obj === null) {
|
|
77
|
-
if (typeof obj === "string" && stringEncoding) {
|
|
78
|
-
if (typeof stringEncoding === "boolean") {
|
|
79
|
-
if (stringEncoding) {
|
|
80
|
-
return encodeEditInfo(path, obj, id2);
|
|
81
|
-
}
|
|
82
|
-
} else if (stringEncoding.skipPaths) {
|
|
83
|
-
if (!stringEncoding.skipPaths(path.join("."), obj)) {
|
|
84
|
-
return encodeEditInfo(path, obj, id2);
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
return obj;
|
|
89
|
-
}
|
|
90
|
-
if (Array.isArray(obj)) {
|
|
91
|
-
return obj.map(
|
|
92
|
-
(item, index) => appendMetadata(item, [...path, index], id2)
|
|
44
|
+
variables
|
|
45
|
+
});
|
|
46
|
+
const id = React.useMemo(
|
|
47
|
+
() => hashFromQuery(stringifiedQuery),
|
|
48
|
+
[stringifiedQuery]
|
|
49
|
+
);
|
|
50
|
+
const { edit } = useEditState();
|
|
51
|
+
const handleOpenEvent = useCallback(
|
|
52
|
+
(event) => {
|
|
53
|
+
var _a, _b;
|
|
54
|
+
if (edit) {
|
|
55
|
+
parent.postMessage(
|
|
56
|
+
{ type: "field:selected", fieldName: (_b = (_a = event.detail) == null ? void 0 : _a.data) == null ? void 0 : _b.fieldName },
|
|
57
|
+
window.location.origin
|
|
93
58
|
);
|
|
59
|
+
} else {
|
|
60
|
+
const tinaAdminBasePath = redirect.startsWith("/") ? redirect : `/${redirect}`;
|
|
61
|
+
const tinaAdminPath = `${tinaAdminBasePath}/index.html#/~${window.location.pathname}?active-field=${event.detail.data.fieldName}`;
|
|
62
|
+
window.location.assign(tinaAdminPath);
|
|
94
63
|
}
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
64
|
+
},
|
|
65
|
+
[edit]
|
|
66
|
+
);
|
|
67
|
+
useEffect(() => {
|
|
68
|
+
window.addEventListener("edit:open", handleOpenEvent);
|
|
69
|
+
return () => {
|
|
70
|
+
window.removeEventListener("edit:open", handleOpenEvent);
|
|
71
|
+
};
|
|
72
|
+
}, [redirect, edit]);
|
|
73
|
+
function appendMetadata(obj, path = [], id2) {
|
|
74
|
+
if (typeof obj !== "object" || obj === null) {
|
|
75
|
+
if (typeof obj === "string" && stringEncoding) {
|
|
76
|
+
if (typeof stringEncoding === "boolean") {
|
|
77
|
+
if (stringEncoding) {
|
|
78
|
+
return encodeEditInfo(path, obj, id2);
|
|
79
|
+
}
|
|
80
|
+
} else if (stringEncoding.skipPaths) {
|
|
81
|
+
if (!stringEncoding.skipPaths(path.join("."), obj)) {
|
|
82
|
+
return encodeEditInfo(path, obj, id2);
|
|
83
|
+
}
|
|
110
84
|
}
|
|
111
85
|
}
|
|
112
|
-
return
|
|
86
|
+
return obj;
|
|
113
87
|
}
|
|
114
|
-
if (
|
|
115
|
-
return
|
|
88
|
+
if (Array.isArray(obj)) {
|
|
89
|
+
return obj.map(
|
|
90
|
+
(item, index) => appendMetadata(item, [...path, index], id2)
|
|
91
|
+
);
|
|
116
92
|
}
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
93
|
+
const transformedObj = {};
|
|
94
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
95
|
+
const currentPath = [...path, key];
|
|
96
|
+
if ([
|
|
97
|
+
"__typename",
|
|
98
|
+
"_sys",
|
|
99
|
+
"_internalSys",
|
|
100
|
+
"_values",
|
|
101
|
+
"_internalValues",
|
|
102
|
+
"_content_source",
|
|
103
|
+
"_tina_metadata"
|
|
104
|
+
].includes(key)) {
|
|
105
|
+
transformedObj[key] = value;
|
|
106
|
+
} else {
|
|
107
|
+
transformedObj[key] = appendMetadata(value, currentPath, id2);
|
|
108
|
+
}
|
|
130
109
|
}
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
110
|
+
return { ...transformedObj, _content_source: { queryId: id2, path } };
|
|
111
|
+
}
|
|
112
|
+
if (enabled) {
|
|
113
|
+
return appendMetadata(data, [], id);
|
|
114
|
+
}
|
|
115
|
+
return data;
|
|
116
|
+
};
|
|
117
|
+
function encodeEditInfo(path, value, id) {
|
|
118
|
+
return vercelStegaCombine(value, {
|
|
119
|
+
origin: "tina.io",
|
|
120
|
+
data: { fieldName: `${id}---${path.join(".")}` }
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
const hashFromQuery = (input) => {
|
|
124
|
+
let hash = 0;
|
|
125
|
+
for (let i = 0; i < input.length; i++) {
|
|
126
|
+
const char = input.charCodeAt(i);
|
|
127
|
+
hash = (hash << 5) - hash + char & 4294967295;
|
|
128
|
+
}
|
|
129
|
+
const nonNegativeHash = Math.abs(hash);
|
|
130
|
+
const alphanumericHash = nonNegativeHash.toString(36);
|
|
131
|
+
return alphanumericHash;
|
|
132
|
+
};
|
|
133
|
+
export {
|
|
134
|
+
hashFromQuery,
|
|
135
|
+
useVisualEditing,
|
|
136
|
+
vercelEditInfo
|
|
137
|
+
};
|
package/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tinacms/vercel-previews",
|
|
3
|
-
"
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "0.1.13",
|
|
4
5
|
"main": "dist/index.js",
|
|
5
6
|
"types": "dist/index.d.ts",
|
|
6
|
-
"module": "./dist/index.mjs",
|
|
7
7
|
"keywords": [
|
|
8
8
|
"tinacms",
|
|
9
9
|
"cms",
|
|
@@ -12,13 +12,6 @@
|
|
|
12
12
|
"files": [
|
|
13
13
|
"dist"
|
|
14
14
|
],
|
|
15
|
-
"exports": {
|
|
16
|
-
".": {
|
|
17
|
-
"types": "./dist/index.d.ts",
|
|
18
|
-
"import": "./dist/index.mjs",
|
|
19
|
-
"require": "./dist/index.js"
|
|
20
|
-
}
|
|
21
|
-
},
|
|
22
15
|
"buildConfig": {
|
|
23
16
|
"entryPoints": [
|
|
24
17
|
"src/index.ts"
|
|
@@ -41,7 +34,7 @@
|
|
|
41
34
|
},
|
|
42
35
|
"dependencies": {
|
|
43
36
|
"@vercel/stega": "^0.0.5",
|
|
44
|
-
"tinacms": "
|
|
37
|
+
"tinacms": "3.0.1"
|
|
45
38
|
},
|
|
46
39
|
"peerDependencies": {
|
|
47
40
|
"react": ">=16.14"
|
package/dist/index.mjs
DELETED
|
@@ -1,137 +0,0 @@
|
|
|
1
|
-
import { vercelStegaCombine } from "@vercel/stega";
|
|
2
|
-
import React, { useCallback, useEffect } from "react";
|
|
3
|
-
function useEditState() {
|
|
4
|
-
const [edit, setEdit] = React.useState(false);
|
|
5
|
-
React.useEffect(() => {
|
|
6
|
-
if (typeof window !== "undefined") {
|
|
7
|
-
parent.postMessage({ type: "isEditMode" }, window.location.origin);
|
|
8
|
-
window.addEventListener("message", (event) => {
|
|
9
|
-
var _a;
|
|
10
|
-
if (((_a = event.data) == null ? void 0 : _a.type) === "tina:editMode") {
|
|
11
|
-
setEdit(true);
|
|
12
|
-
}
|
|
13
|
-
});
|
|
14
|
-
}
|
|
15
|
-
}, []);
|
|
16
|
-
return { edit };
|
|
17
|
-
}
|
|
18
|
-
const tinaField = (object, property, index) => {
|
|
19
|
-
const contentSource = object == null ? void 0 : object._content_source;
|
|
20
|
-
if (!contentSource) {
|
|
21
|
-
return "";
|
|
22
|
-
}
|
|
23
|
-
const { queryId, path } = contentSource;
|
|
24
|
-
if (!property) {
|
|
25
|
-
return `${queryId}---${path.join(".")}`;
|
|
26
|
-
}
|
|
27
|
-
const fullPath = typeof index === "number" ? [...path, property, index] : [...path, property];
|
|
28
|
-
return `${queryId}---${fullPath.join(".")}`;
|
|
29
|
-
};
|
|
30
|
-
const vercelEditInfo = (obj, field, index) => {
|
|
31
|
-
const fieldName = tinaField(obj, field, index);
|
|
32
|
-
return JSON.stringify({ origin: "tinacms", data: { fieldName } });
|
|
33
|
-
};
|
|
34
|
-
const useVisualEditing = ({
|
|
35
|
-
data,
|
|
36
|
-
query,
|
|
37
|
-
variables,
|
|
38
|
-
enabled,
|
|
39
|
-
redirect,
|
|
40
|
-
stringEncoding
|
|
41
|
-
}) => {
|
|
42
|
-
const stringifiedQuery = JSON.stringify({
|
|
43
|
-
query,
|
|
44
|
-
variables
|
|
45
|
-
});
|
|
46
|
-
const id = React.useMemo(
|
|
47
|
-
() => hashFromQuery(stringifiedQuery),
|
|
48
|
-
[stringifiedQuery]
|
|
49
|
-
);
|
|
50
|
-
const { edit } = useEditState();
|
|
51
|
-
const handleOpenEvent = useCallback(
|
|
52
|
-
(event) => {
|
|
53
|
-
var _a, _b;
|
|
54
|
-
if (edit) {
|
|
55
|
-
parent.postMessage(
|
|
56
|
-
{ type: "field:selected", fieldName: (_b = (_a = event.detail) == null ? void 0 : _a.data) == null ? void 0 : _b.fieldName },
|
|
57
|
-
window.location.origin
|
|
58
|
-
);
|
|
59
|
-
} else {
|
|
60
|
-
const tinaAdminBasePath = redirect.startsWith("/") ? redirect : `/${redirect}`;
|
|
61
|
-
const tinaAdminPath = `${tinaAdminBasePath}/index.html#/~${window.location.pathname}?active-field=${event.detail.data.fieldName}`;
|
|
62
|
-
window.location.assign(tinaAdminPath);
|
|
63
|
-
}
|
|
64
|
-
},
|
|
65
|
-
[edit]
|
|
66
|
-
);
|
|
67
|
-
useEffect(() => {
|
|
68
|
-
window.addEventListener("edit:open", handleOpenEvent);
|
|
69
|
-
return () => {
|
|
70
|
-
window.removeEventListener("edit:open", handleOpenEvent);
|
|
71
|
-
};
|
|
72
|
-
}, [redirect, edit]);
|
|
73
|
-
function appendMetadata(obj, path = [], id2) {
|
|
74
|
-
if (typeof obj !== "object" || obj === null) {
|
|
75
|
-
if (typeof obj === "string" && stringEncoding) {
|
|
76
|
-
if (typeof stringEncoding === "boolean") {
|
|
77
|
-
if (stringEncoding) {
|
|
78
|
-
return encodeEditInfo(path, obj, id2);
|
|
79
|
-
}
|
|
80
|
-
} else if (stringEncoding.skipPaths) {
|
|
81
|
-
if (!stringEncoding.skipPaths(path.join("."), obj)) {
|
|
82
|
-
return encodeEditInfo(path, obj, id2);
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
return obj;
|
|
87
|
-
}
|
|
88
|
-
if (Array.isArray(obj)) {
|
|
89
|
-
return obj.map(
|
|
90
|
-
(item, index) => appendMetadata(item, [...path, index], id2)
|
|
91
|
-
);
|
|
92
|
-
}
|
|
93
|
-
const transformedObj = {};
|
|
94
|
-
for (const [key, value] of Object.entries(obj)) {
|
|
95
|
-
const currentPath = [...path, key];
|
|
96
|
-
if ([
|
|
97
|
-
"__typename",
|
|
98
|
-
"_sys",
|
|
99
|
-
"_internalSys",
|
|
100
|
-
"_values",
|
|
101
|
-
"_internalValues",
|
|
102
|
-
"_content_source",
|
|
103
|
-
"_tina_metadata"
|
|
104
|
-
].includes(key)) {
|
|
105
|
-
transformedObj[key] = value;
|
|
106
|
-
} else {
|
|
107
|
-
transformedObj[key] = appendMetadata(value, currentPath, id2);
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
return { ...transformedObj, _content_source: { queryId: id2, path } };
|
|
111
|
-
}
|
|
112
|
-
if (enabled) {
|
|
113
|
-
return appendMetadata(data, [], id);
|
|
114
|
-
}
|
|
115
|
-
return data;
|
|
116
|
-
};
|
|
117
|
-
function encodeEditInfo(path, value, id) {
|
|
118
|
-
return vercelStegaCombine(value, {
|
|
119
|
-
origin: "tina.io",
|
|
120
|
-
data: { fieldName: `${id}---${path.join(".")}` }
|
|
121
|
-
});
|
|
122
|
-
}
|
|
123
|
-
const hashFromQuery = (input) => {
|
|
124
|
-
let hash = 0;
|
|
125
|
-
for (let i = 0; i < input.length; i++) {
|
|
126
|
-
const char = input.charCodeAt(i);
|
|
127
|
-
hash = (hash << 5) - hash + char & 4294967295;
|
|
128
|
-
}
|
|
129
|
-
const nonNegativeHash = Math.abs(hash);
|
|
130
|
-
const alphanumericHash = nonNegativeHash.toString(36);
|
|
131
|
-
return alphanumericHash;
|
|
132
|
-
};
|
|
133
|
-
export {
|
|
134
|
-
hashFromQuery,
|
|
135
|
-
useVisualEditing,
|
|
136
|
-
vercelEditInfo
|
|
137
|
-
};
|