@tinacms/vercel-previews 0.0.0-20230516154119 → 0.0.0-20230516165451

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.d.ts CHANGED
@@ -1,21 +1,16 @@
1
- import type { Plugin } from 'tinacms';
2
- declare function encodeEditInfo(path: string, value: string, id: string): string;
3
- export interface PreviewHelperPlugin extends Plugin {
4
- __type: 'preview-helper';
5
- encode: typeof encodeEditInfo;
6
- }
7
- export declare function createSourceMapEncoder(encodeAtPath?: (path: string, value: any) => boolean): PreviewHelperPlugin;
8
- export declare const hashFromQuery: (input: string) => string;
9
- export declare const withSourceMaps: <T extends {
1
+ export declare const vercelEditInfo: <T extends object & {
2
+ _content_source?: {
3
+ queryId: string;
4
+ path: (number | string)[];
5
+ };
6
+ }>(obj: T, field?: Exclude<keyof T, "__typename" | "_sys">, index?: number) => string;
7
+ export declare const useVisualEditing: <T extends object>({ data, query, variables, redirect, stringEncoding, }: {
8
+ data: T;
10
9
  query: string;
11
10
  variables: object;
12
- data: object;
13
- }>(payload: T, options?: {
14
- encodeStrings?: boolean;
15
- encodeAtPath?: (path: string, value: any) => boolean;
16
- }) => (T & {
17
- data: object;
18
- }) | (T & {
19
- data: object;
20
- });
21
- export {};
11
+ redirect: string;
12
+ stringEncoding: boolean | {
13
+ skipPaths: (path: string, value: string) => boolean;
14
+ };
15
+ }) => T;
16
+ export declare const hashFromQuery: (input: string) => string;
package/dist/index.es.js CHANGED
@@ -1,23 +1,121 @@
1
1
  import { vercelStegaEncode } 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 = (obj, field, index) => {
19
+ var _a, _b, _c;
20
+ if (obj._content_source) {
21
+ if (!field) {
22
+ return [
23
+ (_a = obj._content_source) == null ? void 0 : _a.queryId,
24
+ obj._content_source.path.join(".")
25
+ ].join("---");
26
+ }
27
+ if (typeof index === "number") {
28
+ return [
29
+ (_b = obj._content_source) == null ? void 0 : _b.queryId,
30
+ [...obj._content_source.path, field, index].join(".")
31
+ ].join("---");
32
+ }
33
+ return [
34
+ (_c = obj._content_source) == null ? void 0 : _c.queryId,
35
+ [...obj._content_source.path, field].join(".")
36
+ ].join("---");
37
+ }
38
+ return "";
39
+ };
40
+ const vercelEditInfo = (obj, field, index) => {
41
+ const fieldName = tinaField(obj, field, index);
42
+ return JSON.stringify({ origin: "tinacms", data: { fieldName } });
43
+ };
44
+ const useVisualEditing = ({
45
+ data,
46
+ query,
47
+ variables,
48
+ redirect,
49
+ stringEncoding
50
+ }) => {
51
+ const stringifiedQuery = JSON.stringify({
52
+ query,
53
+ variables
54
+ });
55
+ const id = React.useMemo(() => hashFromQuery(stringifiedQuery), [stringifiedQuery]);
56
+ const { edit } = useEditState();
57
+ const handleOpenEvent = useCallback((event) => {
58
+ var _a, _b;
59
+ if (edit) {
60
+ parent.postMessage({ type: "field:selected", fieldName: (_b = (_a = event.detail) == null ? void 0 : _a.data) == null ? void 0 : _b.fieldName }, window.location.origin);
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
+ }, [edit]);
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((item, index) => appendMetadata(item, [...path, index], id2));
90
+ }
91
+ const transformedObj = {};
92
+ for (const [key, value] of Object.entries(obj)) {
93
+ const currentPath = [...path, key];
94
+ if ([
95
+ "__typename",
96
+ "_sys",
97
+ "_internalSys",
98
+ "_values",
99
+ "_internalValues",
100
+ "_content_source",
101
+ "_tina_metadata"
102
+ ].includes(key)) {
103
+ transformedObj[key] = value;
104
+ } else {
105
+ transformedObj[key] = appendMetadata(value, currentPath, id2);
106
+ }
107
+ }
108
+ return { ...transformedObj, _content_source: { queryId: id2, path } };
109
+ }
110
+ return appendMetadata(data, [], id);
111
+ };
2
112
  function encodeEditInfo(path, value, id) {
3
113
  const res = `${vercelStegaEncode({
4
114
  origin: "tinacms",
5
- data: { fieldName: `${id}---${path}` }
115
+ data: { fieldName: `${id}---${path.join(".")}` }
6
116
  })}${value}`;
7
117
  return res;
8
118
  }
9
- function createSourceMapEncoder(encodeAtPath) {
10
- return {
11
- __type: "preview-helper",
12
- name: "preview-helper",
13
- encode: (path, value, id) => {
14
- if (encodeAtPath(path, value)) {
15
- return encodeEditInfo(path, value, id);
16
- }
17
- return value;
18
- }
19
- };
20
- }
21
119
  const hashFromQuery = (input) => {
22
120
  let hash = 0;
23
121
  for (let i = 0; i < input.length; i++) {
@@ -28,69 +126,4 @@ const hashFromQuery = (input) => {
28
126
  const alphanumericHash = nonNegativeHash.toString(36);
29
127
  return alphanumericHash;
30
128
  };
31
- const withSourceMaps = (payload, options) => {
32
- const stringifiedQuery = JSON.stringify({
33
- query: payload.query,
34
- variables: payload.variables
35
- });
36
- const id = hashFromQuery(stringifiedQuery);
37
- if (options == null ? void 0 : options.encodeStrings) {
38
- const callback = (options == null ? void 0 : options.encodeAtPath) || (() => true);
39
- const dataWithMetadata = transformString(payload.data, (value, path) => {
40
- const pathString = path.join(".");
41
- if (callback(pathString, value)) {
42
- return encodeEditInfo(pathString, value, id);
43
- }
44
- return value;
45
- });
46
- return { ...payload, data: dataWithMetadata };
47
- } else {
48
- const dataWithMetadata = appendMetadata(payload.data, [], id);
49
- return { ...payload, data: dataWithMetadata };
50
- }
51
- };
52
- function isValidHttpUrl(string) {
53
- try {
54
- new URL(string);
55
- return true;
56
- } catch (_) {
57
- return false;
58
- }
59
- }
60
- function transformString(obj, callback, path = []) {
61
- if (typeof obj !== "object" || obj === null) {
62
- return obj;
63
- }
64
- if (Array.isArray(obj)) {
65
- return obj.map((item, index) => transformString(item, callback, [...path, index]));
66
- }
67
- const transformedObj = {};
68
- for (const [key, value] of Object.entries(obj)) {
69
- const currentPath = [...path, key];
70
- if (typeof value === "string") {
71
- if (["__typename"].includes(key) || isValidHttpUrl(value)) {
72
- transformedObj[key] = value;
73
- } else {
74
- transformedObj[key] = callback(value, currentPath);
75
- }
76
- } else {
77
- transformedObj[key] = transformString(value, callback, currentPath);
78
- }
79
- }
80
- return transformedObj;
81
- }
82
- function appendMetadata(obj, path = [], id) {
83
- if (typeof obj !== "object" || obj === null) {
84
- return obj;
85
- }
86
- if (Array.isArray(obj)) {
87
- return obj.map((item, index) => appendMetadata(item, [...path, index], id));
88
- }
89
- const transformedObj = {};
90
- for (const [key, value] of Object.entries(obj)) {
91
- const currentPath = [...path, key];
92
- transformedObj[key] = appendMetadata(value, currentPath, id);
93
- }
94
- return { ...transformedObj, _content_source: { queryId: id, path } };
95
- }
96
- export { createSourceMapEncoder, hashFromQuery, withSourceMaps };
129
+ export { hashFromQuery, useVisualEditing, vercelEditInfo };
package/dist/index.js CHANGED
@@ -1,26 +1,127 @@
1
1
  (function(global, factory) {
2
- typeof exports === "object" && typeof module !== "undefined" ? factory(exports, require("@vercel/stega")) : typeof define === "function" && define.amd ? define(["exports", "@vercel/stega"], factory) : (global = typeof globalThis !== "undefined" ? globalThis : global || self, factory(global["@tinacms/vercel-previews"] = {}, global.NOOP));
3
- })(this, function(exports2, stega) {
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
4
  "use strict";
5
+ function _interopDefaultLegacy(e) {
6
+ return e && typeof e === "object" && "default" in e ? e : { "default": e };
7
+ }
8
+ var React__default = /* @__PURE__ */ _interopDefaultLegacy(React);
9
+ function useEditState() {
10
+ const [edit, setEdit] = React__default["default"].useState(false);
11
+ React__default["default"].useEffect(() => {
12
+ if (typeof window !== "undefined") {
13
+ parent.postMessage({ type: "isEditMode" }, window.location.origin);
14
+ window.addEventListener("message", (event) => {
15
+ var _a;
16
+ if (((_a = event.data) == null ? void 0 : _a.type) === "tina:editMode") {
17
+ setEdit(true);
18
+ }
19
+ });
20
+ }
21
+ }, []);
22
+ return { edit };
23
+ }
24
+ const tinaField = (obj, field, index) => {
25
+ var _a, _b, _c;
26
+ if (obj._content_source) {
27
+ if (!field) {
28
+ return [
29
+ (_a = obj._content_source) == null ? void 0 : _a.queryId,
30
+ obj._content_source.path.join(".")
31
+ ].join("---");
32
+ }
33
+ if (typeof index === "number") {
34
+ return [
35
+ (_b = obj._content_source) == null ? void 0 : _b.queryId,
36
+ [...obj._content_source.path, field, index].join(".")
37
+ ].join("---");
38
+ }
39
+ return [
40
+ (_c = obj._content_source) == null ? void 0 : _c.queryId,
41
+ [...obj._content_source.path, field].join(".")
42
+ ].join("---");
43
+ }
44
+ return "";
45
+ };
46
+ const vercelEditInfo = (obj, field, index) => {
47
+ const fieldName = tinaField(obj, field, index);
48
+ return JSON.stringify({ origin: "tinacms", data: { fieldName } });
49
+ };
50
+ const useVisualEditing = ({
51
+ data,
52
+ query,
53
+ variables,
54
+ redirect,
55
+ stringEncoding
56
+ }) => {
57
+ const stringifiedQuery = JSON.stringify({
58
+ query,
59
+ variables
60
+ });
61
+ const id = React__default["default"].useMemo(() => hashFromQuery(stringifiedQuery), [stringifiedQuery]);
62
+ const { edit } = useEditState();
63
+ const handleOpenEvent = React.useCallback((event) => {
64
+ var _a, _b;
65
+ if (edit) {
66
+ parent.postMessage({ type: "field:selected", fieldName: (_b = (_a = event.detail) == null ? void 0 : _a.data) == null ? void 0 : _b.fieldName }, window.location.origin);
67
+ } else {
68
+ const tinaAdminBasePath = redirect.startsWith("/") ? redirect : `/${redirect}`;
69
+ const tinaAdminPath = `${tinaAdminBasePath}/index.html#/~${window.location.pathname}?active-field=${event.detail.data.fieldName}`;
70
+ window.location.assign(tinaAdminPath);
71
+ }
72
+ }, [edit]);
73
+ React.useEffect(() => {
74
+ window.addEventListener("edit:open", handleOpenEvent);
75
+ return () => {
76
+ window.removeEventListener("edit:open", handleOpenEvent);
77
+ };
78
+ }, [redirect, edit]);
79
+ function appendMetadata(obj, path = [], id2) {
80
+ if (typeof obj !== "object" || obj === null) {
81
+ if (typeof obj === "string" && stringEncoding) {
82
+ if (typeof stringEncoding === "boolean") {
83
+ if (stringEncoding) {
84
+ return encodeEditInfo(path, obj, id2);
85
+ }
86
+ } else if (stringEncoding.skipPaths) {
87
+ if (!stringEncoding.skipPaths(path.join("."), obj)) {
88
+ return encodeEditInfo(path, obj, id2);
89
+ }
90
+ }
91
+ }
92
+ return obj;
93
+ }
94
+ if (Array.isArray(obj)) {
95
+ return obj.map((item, index) => appendMetadata(item, [...path, index], id2));
96
+ }
97
+ const transformedObj = {};
98
+ for (const [key, value] of Object.entries(obj)) {
99
+ const currentPath = [...path, key];
100
+ if ([
101
+ "__typename",
102
+ "_sys",
103
+ "_internalSys",
104
+ "_values",
105
+ "_internalValues",
106
+ "_content_source",
107
+ "_tina_metadata"
108
+ ].includes(key)) {
109
+ transformedObj[key] = value;
110
+ } else {
111
+ transformedObj[key] = appendMetadata(value, currentPath, id2);
112
+ }
113
+ }
114
+ return { ...transformedObj, _content_source: { queryId: id2, path } };
115
+ }
116
+ return appendMetadata(data, [], id);
117
+ };
5
118
  function encodeEditInfo(path, value, id) {
6
119
  const res = `${stega.vercelStegaEncode({
7
120
  origin: "tinacms",
8
- data: { fieldName: `${id}---${path}` }
121
+ data: { fieldName: `${id}---${path.join(".")}` }
9
122
  })}${value}`;
10
123
  return res;
11
124
  }
12
- function createSourceMapEncoder(encodeAtPath) {
13
- return {
14
- __type: "preview-helper",
15
- name: "preview-helper",
16
- encode: (path, value, id) => {
17
- if (encodeAtPath(path, value)) {
18
- return encodeEditInfo(path, value, id);
19
- }
20
- return value;
21
- }
22
- };
23
- }
24
125
  const hashFromQuery = (input) => {
25
126
  let hash = 0;
26
127
  for (let i = 0; i < input.length; i++) {
@@ -31,73 +132,8 @@
31
132
  const alphanumericHash = nonNegativeHash.toString(36);
32
133
  return alphanumericHash;
33
134
  };
34
- const withSourceMaps = (payload, options) => {
35
- const stringifiedQuery = JSON.stringify({
36
- query: payload.query,
37
- variables: payload.variables
38
- });
39
- const id = hashFromQuery(stringifiedQuery);
40
- if (options == null ? void 0 : options.encodeStrings) {
41
- const callback = (options == null ? void 0 : options.encodeAtPath) || (() => true);
42
- const dataWithMetadata = transformString(payload.data, (value, path) => {
43
- const pathString = path.join(".");
44
- if (callback(pathString, value)) {
45
- return encodeEditInfo(pathString, value, id);
46
- }
47
- return value;
48
- });
49
- return { ...payload, data: dataWithMetadata };
50
- } else {
51
- const dataWithMetadata = appendMetadata(payload.data, [], id);
52
- return { ...payload, data: dataWithMetadata };
53
- }
54
- };
55
- function isValidHttpUrl(string) {
56
- try {
57
- new URL(string);
58
- return true;
59
- } catch (_) {
60
- return false;
61
- }
62
- }
63
- function transformString(obj, callback, path = []) {
64
- if (typeof obj !== "object" || obj === null) {
65
- return obj;
66
- }
67
- if (Array.isArray(obj)) {
68
- return obj.map((item, index) => transformString(item, callback, [...path, index]));
69
- }
70
- const transformedObj = {};
71
- for (const [key, value] of Object.entries(obj)) {
72
- const currentPath = [...path, key];
73
- if (typeof value === "string") {
74
- if (["__typename"].includes(key) || isValidHttpUrl(value)) {
75
- transformedObj[key] = value;
76
- } else {
77
- transformedObj[key] = callback(value, currentPath);
78
- }
79
- } else {
80
- transformedObj[key] = transformString(value, callback, currentPath);
81
- }
82
- }
83
- return transformedObj;
84
- }
85
- function appendMetadata(obj, path = [], id) {
86
- if (typeof obj !== "object" || obj === null) {
87
- return obj;
88
- }
89
- if (Array.isArray(obj)) {
90
- return obj.map((item, index) => appendMetadata(item, [...path, index], id));
91
- }
92
- const transformedObj = {};
93
- for (const [key, value] of Object.entries(obj)) {
94
- const currentPath = [...path, key];
95
- transformedObj[key] = appendMetadata(value, currentPath, id);
96
- }
97
- return { ...transformedObj, _content_source: { queryId: id, path } };
98
- }
99
- exports2.createSourceMapEncoder = createSourceMapEncoder;
100
135
  exports2.hashFromQuery = hashFromQuery;
101
- exports2.withSourceMaps = withSourceMaps;
136
+ exports2.useVisualEditing = useVisualEditing;
137
+ exports2.vercelEditInfo = vercelEditInfo;
102
138
  Object.defineProperties(exports2, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
103
139
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tinacms/vercel-previews",
3
- "version": "0.0.0-20230516154119",
3
+ "version": "0.0.0-20230516165451",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "module": "./dist/index.es.js",
@@ -17,17 +17,11 @@
17
17
  "types": "./dist/index.d.ts",
18
18
  "import": "./dist/index.es.js",
19
19
  "require": "./dist/index.js"
20
- },
21
- "./dist/react": {
22
- "types": "./dist/react.d.ts",
23
- "import": "./dist/react.es.js",
24
- "require": "./dist/react.js"
25
20
  }
26
21
  },
27
22
  "buildConfig": {
28
23
  "entryPoints": [
29
- "src/index.ts",
30
- "src/react.tsx"
24
+ "src/index.ts"
31
25
  ]
32
26
  },
33
27
  "bugs": {
@@ -47,7 +41,7 @@
47
41
  },
48
42
  "dependencies": {
49
43
  "@vercel/stega": "^0.0.4",
50
- "tinacms": "0.0.0-20230516154119"
44
+ "tinacms": "0.0.0-20230516165451"
51
45
  },
52
46
  "peerDependencies": {
53
47
  "react": ">=16.14"
@@ -56,7 +50,6 @@
56
50
  "scripts": {
57
51
  "types": "pnpm tsc",
58
52
  "build": "tinacms-scripts build",
59
- "test": "jest --env=jsdom --passWithNoTests",
60
- "test-watch": "jest --watch --env=jsdom --passWithNoTests"
53
+ "test": "echo 'no tests for vercel previews'"
61
54
  }
62
55
  }
package/dist/react.d.ts DELETED
@@ -1,7 +0,0 @@
1
- export declare const vercelEditInfo: <T extends object & {
2
- _content_source?: {
3
- queryId: string;
4
- path: (number | string)[];
5
- };
6
- }>(obj: T, field?: Exclude<keyof T, "__typename" | "_sys">, index?: number) => string;
7
- export declare const useEditOpen: (redirect: string) => void;
package/dist/react.es.js DELETED
@@ -1,62 +0,0 @@
1
- import React, { useCallback, useEffect } from "react";
2
- function useEditState() {
3
- const [edit, setEdit] = React.useState(false);
4
- React.useEffect(() => {
5
- if (typeof window !== "undefined") {
6
- parent.postMessage({ type: "isEditMode" }, window.location.origin);
7
- window.addEventListener("message", (event) => {
8
- var _a;
9
- if (((_a = event.data) == null ? void 0 : _a.type) === "tina:editMode") {
10
- setEdit(true);
11
- }
12
- });
13
- }
14
- }, []);
15
- return { edit };
16
- }
17
- const tinaField = (obj, field, index) => {
18
- var _a, _b, _c;
19
- if (obj._content_source) {
20
- if (!field) {
21
- return [
22
- (_a = obj._content_source) == null ? void 0 : _a.queryId,
23
- obj._content_source.path.join(".")
24
- ].join("---");
25
- }
26
- if (typeof index === "number") {
27
- return [
28
- (_b = obj._content_source) == null ? void 0 : _b.queryId,
29
- [...obj._content_source.path, field, index].join(".")
30
- ].join("---");
31
- }
32
- return [
33
- (_c = obj._content_source) == null ? void 0 : _c.queryId,
34
- [...obj._content_source.path, field].join(".")
35
- ].join("---");
36
- }
37
- return "";
38
- };
39
- const vercelEditInfo = (obj, field, index) => {
40
- const fieldName = tinaField(obj, field, index);
41
- return JSON.stringify({ origin: "tinacms", data: { fieldName } });
42
- };
43
- const useEditOpen = (redirect) => {
44
- const { edit } = useEditState();
45
- const handleOpenEvent = useCallback((event) => {
46
- var _a, _b;
47
- if (edit) {
48
- parent.postMessage({ type: "field:selected", fieldName: (_b = (_a = event.detail) == null ? void 0 : _a.data) == null ? void 0 : _b.fieldName }, window.location.origin);
49
- } else {
50
- const tinaAdminBasePath = redirect.startsWith("/") ? redirect : `/${redirect}`;
51
- const tinaAdminPath = `${tinaAdminBasePath}/index.html#/~${window.location.pathname}?active-field=${event.detail.data.fieldName}`;
52
- window.location.assign(tinaAdminPath);
53
- }
54
- }, [edit]);
55
- useEffect(() => {
56
- window.addEventListener("edit:open", handleOpenEvent);
57
- return () => {
58
- window.removeEventListener("edit:open", handleOpenEvent);
59
- };
60
- }, [redirect, edit]);
61
- };
62
- export { useEditOpen, vercelEditInfo };
package/dist/react.js DELETED
@@ -1,72 +0,0 @@
1
- (function(global, factory) {
2
- typeof exports === "object" && typeof module !== "undefined" ? factory(exports, require("react")) : typeof define === "function" && define.amd ? define(["exports", "react"], factory) : (global = typeof globalThis !== "undefined" ? globalThis : global || self, factory(global["@tinacms/vercel-previews"] = {}, global.NOOP));
3
- })(this, function(exports2, React) {
4
- "use strict";
5
- function _interopDefaultLegacy(e) {
6
- return e && typeof e === "object" && "default" in e ? e : { "default": e };
7
- }
8
- var React__default = /* @__PURE__ */ _interopDefaultLegacy(React);
9
- function useEditState() {
10
- const [edit, setEdit] = React__default["default"].useState(false);
11
- React__default["default"].useEffect(() => {
12
- if (typeof window !== "undefined") {
13
- parent.postMessage({ type: "isEditMode" }, window.location.origin);
14
- window.addEventListener("message", (event) => {
15
- var _a;
16
- if (((_a = event.data) == null ? void 0 : _a.type) === "tina:editMode") {
17
- setEdit(true);
18
- }
19
- });
20
- }
21
- }, []);
22
- return { edit };
23
- }
24
- const tinaField = (obj, field, index) => {
25
- var _a, _b, _c;
26
- if (obj._content_source) {
27
- if (!field) {
28
- return [
29
- (_a = obj._content_source) == null ? void 0 : _a.queryId,
30
- obj._content_source.path.join(".")
31
- ].join("---");
32
- }
33
- if (typeof index === "number") {
34
- return [
35
- (_b = obj._content_source) == null ? void 0 : _b.queryId,
36
- [...obj._content_source.path, field, index].join(".")
37
- ].join("---");
38
- }
39
- return [
40
- (_c = obj._content_source) == null ? void 0 : _c.queryId,
41
- [...obj._content_source.path, field].join(".")
42
- ].join("---");
43
- }
44
- return "";
45
- };
46
- const vercelEditInfo = (obj, field, index) => {
47
- const fieldName = tinaField(obj, field, index);
48
- return JSON.stringify({ origin: "tinacms", data: { fieldName } });
49
- };
50
- const useEditOpen = (redirect) => {
51
- const { edit } = useEditState();
52
- const handleOpenEvent = React.useCallback((event) => {
53
- var _a, _b;
54
- if (edit) {
55
- parent.postMessage({ type: "field:selected", fieldName: (_b = (_a = event.detail) == null ? void 0 : _a.data) == null ? void 0 : _b.fieldName }, window.location.origin);
56
- } else {
57
- const tinaAdminBasePath = redirect.startsWith("/") ? redirect : `/${redirect}`;
58
- const tinaAdminPath = `${tinaAdminBasePath}/index.html#/~${window.location.pathname}?active-field=${event.detail.data.fieldName}`;
59
- window.location.assign(tinaAdminPath);
60
- }
61
- }, [edit]);
62
- React.useEffect(() => {
63
- window.addEventListener("edit:open", handleOpenEvent);
64
- return () => {
65
- window.removeEventListener("edit:open", handleOpenEvent);
66
- };
67
- }, [redirect, edit]);
68
- };
69
- exports2.useEditOpen = useEditOpen;
70
- exports2.vercelEditInfo = vercelEditInfo;
71
- Object.defineProperties(exports2, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
72
- });