forked from contentful/extensions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
188 lines (154 loc) · 5.24 KB
/
index.html
File metadata and controls
188 lines (154 loc) · 5.24 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
<!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">
<!-- Styles for added / removed text -->
<style>
.diff-text .added {
font-weight: bold;
color: #5cb85c;
}
.diff-text .removed {
text-decoration: line-through;
font-weight: bold;
color: #950B02;
}
</style>
</head>
<body>
<!-- Extension DOM Elements -->
<div class="cf-form-field">
<input type="text" class="cf-form-input draft-text">
<div class="cf-form-hint">
<span class="diff-text"></span>
</div>
</div>
<!-- Contenful UI Extensions SDK API -->
<script src="https://unpkg.com/contentful-ui-extensions-sdk@3"></script>
<!-- Diff Lib -->
<script src="//cdnjs.cloudflare.com/ajax/libs/jsdiff/2.2.2/diff.min.js"></script>
<!-- Diffing UI Extension -->
<script>
function diffExtension(extensionsApi) {
extensionsApi.window.startAutoResizer();
// DOM elements.
var draftTextEl = document.querySelector('.draft-text');
var diffTextEl = document.querySelector('.diff-text');
// 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);
window.addEventListener('onbeforeunload', onBeforeUnloadHandler);
draftTextEl.addEventListener('input', keyboardInputHandler);
if (publishedVersion) {
// Load published value from CDA.
loadPublishedValue(entry.getSys().id)
.then(function (entries) {
var field = entries.items[0].fields[fieldId];
if (field !== undefined) {
publishedValue = field[fieldLocale];
} else {
// An empty string was published.
publishedValue = "";
}
draftTextEl.disabled = false;
renderDiff();
}
)
.catch(function (error) {
console.log(error)
});
} else {
draftTextEl.disabled = false;
renderDiff();
}
/**
* Event handler for keyboard input.
* @param event The event object.
*/
function keyboardInputHandler(event) {
renderDiff();
field.setValue(draftTextEl.value);
}
/**
* Event handler triggered before the window unloads.
* @param event The event object.
*/
function onBeforeUnloadHandler(event) {
window.removeEventListener('onbeforeunload', onBeforeUnloadHandler);
draftTextEl.removeEventListener('input', keyboardInputHandler);
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})
}
/**
* Uses diff lib to render diff to DOM of extension.
*/
function renderDiff() {
// Do not render if there is no published value.
if (!publishedVersion) {
diffTextEl.innerHTML = '<i>Entry not yet published.</i>';
return;
}
// Do not render if there is no diff.
if (publishedValue === draftTextEl.value) {
diffTextEl.innerHTML = '<i>Draft and published value are equal.</i>';
return;
}
// Clear diff DOM element.
diffTextEl.innerHTML = '';
var diff = JsDiff.diffWordsWithSpace(publishedValue, draftTextEl.value);
diff.forEach(function (part) {
var className = part.added ? 'added' : part.removed ? 'removed' : '';
var spanEl = document.createElement('span');
spanEl.className = className;
spanEl.appendChild(document.createTextNode(part.value));
diffTextEl.appendChild(spanEl);
});
}
/**
* 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) {
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;
renderDiff();
}
}
}
window.contentfulExtension.init(diffExtension);
</script>
</body>
</html>