@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.
Files changed (3) hide show
  1. package/dist/index.js +128 -130
  2. package/package.json +3 -10
  3. package/dist/index.mjs +0 -137
package/dist/index.js CHANGED
@@ -1,139 +1,137 @@
1
- (function(global, factory) {
2
- typeof exports === "object" && typeof module !== "undefined" ? factory(exports, require("@vercel/stega"), require("react")) : typeof define === "function" && define.amd ? define(["exports", "@vercel/stega", "react"], factory) : (global = typeof globalThis !== "undefined" ? globalThis : global || self, factory(global["@tinacms/vercel-previews"] = {}, global.NOOP, global.NOOP));
3
- })(this, function(exports2, stega, React) {
4
- "use strict";
5
- function useEditState() {
6
- const [edit, setEdit] = React.useState(false);
7
- React.useEffect(() => {
8
- if (typeof window !== "undefined") {
9
- parent.postMessage({ type: "isEditMode" }, window.location.origin);
10
- window.addEventListener("message", (event) => {
11
- var _a;
12
- if (((_a = event.data) == null ? void 0 : _a.type) === "tina:editMode") {
13
- setEdit(true);
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
- const fullPath = typeof index === "number" ? [...path, property, index] : [...path, property];
30
- return `${queryId}---${fullPath.join(".")}`;
31
- };
32
- const vercelEditInfo = (obj, field, index) => {
33
- const fieldName = tinaField(obj, field, index);
34
- return JSON.stringify({ origin: "tinacms", data: { fieldName } });
35
- };
36
- const useVisualEditing = ({
37
- data,
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
- enabled,
41
- redirect,
42
- stringEncoding
43
- }) => {
44
- const stringifiedQuery = JSON.stringify({
45
- query,
46
- variables
47
- });
48
- const id = React.useMemo(
49
- () => hashFromQuery(stringifiedQuery),
50
- [stringifiedQuery]
51
- );
52
- const { edit } = useEditState();
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
- const transformedObj = {};
96
- for (const [key, value] of Object.entries(obj)) {
97
- const currentPath = [...path, key];
98
- if ([
99
- "__typename",
100
- "_sys",
101
- "_internalSys",
102
- "_values",
103
- "_internalValues",
104
- "_content_source",
105
- "_tina_metadata"
106
- ].includes(key)) {
107
- transformedObj[key] = value;
108
- } else {
109
- transformedObj[key] = appendMetadata(value, currentPath, id2);
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 { ...transformedObj, _content_source: { queryId: id2, path } };
86
+ return obj;
113
87
  }
114
- if (enabled) {
115
- return appendMetadata(data, [], id);
88
+ if (Array.isArray(obj)) {
89
+ return obj.map(
90
+ (item, index) => appendMetadata(item, [...path, index], id2)
91
+ );
116
92
  }
117
- return data;
118
- };
119
- function encodeEditInfo(path, value, id) {
120
- return stega.vercelStegaCombine(value, {
121
- origin: "tina.io",
122
- data: { fieldName: `${id}---${path.join(".")}` }
123
- });
124
- }
125
- const hashFromQuery = (input) => {
126
- let hash = 0;
127
- for (let i = 0; i < input.length; i++) {
128
- const char = input.charCodeAt(i);
129
- hash = (hash << 5) - hash + char & 4294967295;
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
- const nonNegativeHash = Math.abs(hash);
132
- const alphanumericHash = nonNegativeHash.toString(36);
133
- return alphanumericHash;
134
- };
135
- exports2.hashFromQuery = hashFromQuery;
136
- exports2.useVisualEditing = useVisualEditing;
137
- exports2.vercelEditInfo = vercelEditInfo;
138
- Object.defineProperty(exports2, Symbol.toStringTag, { value: "Module" });
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
- "version": "0.1.11",
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": "2.10.1"
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
- };