-
Notifications
You must be signed in to change notification settings - Fork 13
/
index.html
176 lines (147 loc) · 4.95 KB
/
index.html
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<!-- Core extensions CSS from Contentful. -->
<link rel="stylesheet" href="//contentful.github.io/ui-extensions-sdk/cf-extension.css">
</head>
<body>
<!-- Extension DOM Elements -->
<div class="cf-form-field">
<input type="color" class="colour-picker">
<input type="text" class="cf-form-input draft-text">
</div>
<!-- Contenful UI Extensions SDK API -->
<script src="https://contentful.github.io/ui-extensions-sdk/cf-extension-api.js"></script>
<style>
input[type="color"] {
height: 50px;
width: 50px;
border-radius: 3px;
cursor: pointer;
}
</style>
<!-- Colour Picker UI Extension -->
<script>
function cpExtension(extensionsApi) {
extensionsApi.window.startAutoResizer();
// Hex Colour RegExp
var hexColour = new RegExp(/^#(?:[0-9a-f]{3}){1,2}$/i);
// DOM elements.
var draftTextEl = document.querySelector('.draft-text');
var colourPickerEl = document.querySelector('.colour-picker');
// Disable the input as we need to wait for an async call to load the published entry.
draftTextEl.disabled = true;
var entry = extensionsApi.entry;
var field = extensionsApi.field;
// Currently published version of entry.
var publishedVersion = extensionsApi.entry.getSys().publishedVersion;
var publishedValue;
// Current locale
var fieldLocale = extensionsApi.field.locale;
var fieldId = extensionsApi.field.id;
// Callbacks for changes on the field or the entry.sys object.
// Detach functions will be used to clean up the callbacks.
var detachValueChanged = field.onValueChanged(onValueChanged);
var detachSysChanged = entry.onSysChanged(onSysChanged);
// Add in event listeners
window.addEventListener('onbeforeunload', onBeforeUnloadHandler);
draftTextEl.addEventListener('input', keyboardInputHandler);
colourPickerEl.addEventListener('change', updateField);
// Check for published version
if (publishedVersion) {
// Load published value from CDA.
loadPublishedValue(entry.getSys().id)
.then(function (entries) {
console.log(JSON.stringify(entries))
publishedValue = entries.items[0].fields[fieldId][fieldLocale];
draftTextEl.disabled = false;
colourPickerEl.disabled = false;
renderColour();
}
)
.catch(function (error) {
console.log(error)
});
} else {
draftTextEl.disabled = false;
colourPickerEl.disabled = false;
renderColourOnly();
}
/**
* Event handler for keyboard input.
* @param event The event object.
*/
function keyboardInputHandler(event) {
renderColour();
}
/**
* Event handler triggered before the window unloads.
* @param event The event object.
*/
function onBeforeUnloadHandler(event) {
field.setValue(draftTextEl.value);
window.removeEventListener('onbeforeunload', onBeforeUnloadHandler);
draftTextEl.removeEventListener('input', keyboardInputHandler);
colourPickerEl.removeEventListener('change', updateField);
detachValueChanged();
detachSysChanged();
}
/**
* Gets the published value from the API.
* @param entryId The id of the entry to retrieve.
* @return Promise A Promise object of the call.
*/
function loadPublishedValue(entryId) {
return extensionsApi.space.getPublishedEntries({'sys.id': entryId});
}
/**
* Calls the callback every time the value of the field is changed by some external event
* (e.g. when multiple editors are working on the same entry).
* @param val The newly changed value.
*/
function onValueChanged(val) {
if (val) {
draftTextEl.value = val;
}
}
/**
* Calls the callback every time the value of the system object is changed
* @param sys The sys object of the entry.
*/
function onSysChanged(sys) {
if (sys.publishedVersion && publishedVersion !== sys.publishedVersion) {
publishedValue = draftTextEl.value;
publishedVersion = sys.publishedVersion;
return renderColour();
}
}
/**
* Render a new colour in the colour input and update value
*/
function renderColour() {
if (hexColour.test(draftTextEl.value)){
colourPickerEl.value = draftTextEl.value;
}
return field.setValue(draftTextEl.value);
}
/**
* Render a new colour in the colour input
*/
function renderColourOnly() {
if (hexColour.test(draftTextEl.value)){
colourPickerEl.value = draftTextEl.value;
}
}
/**
* Update field value based on colour picker value
*/
function updateField() {
draftTextEl.value = colourPickerEl.value;
return field.setValue(draftTextEl.value);
}
}
window.contentfulExtension.init(cpExtension);
</script>
</body>
</html>