forked from hygraph/hygraph-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuix.js
101 lines (89 loc) · 2.77 KB
/
uix.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
console.log(window.GCUIX);
const declaration = {
extensionType: "field",
fieldType: "JSON",
name: "Asset Focal point",
description: "",
features: ["FieldRenderer", "TableRenderer"],
};
const originalImage = document.getElementById("originalImage");
const preview_1_1 = document.getElementById("preview-1-1");
const preview_1_2 = document.getElementById("preview-1-2");
const preview_2_1 = document.getElementById("preview-2-1");
const coords = document.getElementById("coords");
const expandButton = document.getElementById("expand");
let isExpanded = false;
function updateExpandButton() {
expandButton.innerText = isExpanded ? "Collapse" : "Expand";
}
document.addEventListener("DOMContentLoaded", () => {
try {
window.GCUIX.init({
declaration,
onProps: function (props) {
const { isExpanded: isSdkExpanded, value } = props;
if (value) {
coords.innerText = JSON.stringify(value);
}
if (isSdkExpanded !== isExpanded) {
isExpanded = isSdkExpanded;
updateExpandButton();
}
},
}).then((initialState) => {
const { status, props } = initialState;
if (status === "ok") {
const {
value,
onChange,
form,
model,
field,
locale,
expand,
isTableCell,
isReadOnly,
} = props;
if (isTableCell) {
document.body.classList.add("isTableCell");
return;
}
if (model.apiId !== "Asset") {
throw new Error(
"The Focal Point UI extension is meant to be used only on the Asset model",
);
}
if (field.isLocalized === false) {
throw new Error("Focal Point field must be localized");
}
expandButton.addEventListener("click", () => {
expand(!isExpanded);
if (!isExpanded) {
document.body.classList.add("fullScreen");
} else {
document.body.classList.remove("fullScreen");
}
});
form.subscribeToFieldState(
`localization_${locale}.file`,
({ value }) => {
const currentUrl = value?.handle
? `https://media.graphcms.com/${value.handle}`
: "https://via.placeholder.com/900x675?text=This+is+a+placeholder+image";
[originalImage, preview_1_1, preview_1_2, preview_2_1].forEach(
(img) => {
if (img.getAttribute("src") !== currentUrl) {
img.setAttribute("src", currentUrl);
}
},
);
},
{ value: true },
);
makeFocalPoint(value || undefined, onChange, isReadOnly);
}
});
} catch (error) {
console.error(error);
}
});