-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
80 lines (68 loc) · 2.72 KB
/
index.html
File metadata and controls
80 lines (68 loc) · 2.72 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
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8"/>
<title>Ace Editor Extension</title>
<style>
body { margin: 0; }
#editor {
min-height: 300px;
}
</style>
<!--
Load the Extensions API that is used to communicate with the containing app.
-->
<script src="https://www.contentstack.com/sdks/contentstack-ui-extensions/dist/latest/ui-extension-sdk.js"></script>
<!--
We use lodash.throttle to avoid spamming the API with changes
-->
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.4/lodash.min.js"></script>
<!--
Include ace editor core script.
-->
<script src="https://cdn.jsdelivr.net/npm/ace-builds@1.2.9/src-min-noconflict/ace.js"></script>
<!--
Add editor theme and syntax highlighting mode scripts.
Find more here: https://www.jsdelivr.com/package/npm/ace-builds?path=src-min
-->
<script src="https://cdn.jsdelivr.net/npm/ace-builds@1.2.9/src-min-noconflict/mode-handlebars.js"></script>
<script src="https://cdn.jsdelivr.net/npm/ace-builds@1.2.9/src-min-noconflict/theme-monokai.js"></script>
</head>
<body>
<pre id="editor"></pre>
<script>
// initialise Field Extension
window.extensionField = {};
// find editor element
var editorElement = document.getElementById("editor");
// initialise variable for ace editor plugin
var editor = {};
ContentstackUIExtension.init().then(function(extension) {
// make extension object globally available
extensionField = extension;
// update the field height
extensionField.window.updateHeight();
// Get current ace editor field value from Contentstack and update the element
var value = extensionField.field.getData() || '';
// Configure Ace editor
editor = ace.edit('editor')
editor.setTheme('ace/theme/monokai')
editor.session.setMode('ace/mode/handlebars')
if(value)
editor.setValue(value)
//
editor.on('focus', function(event, editor) {
extensionField.field.setFocus();
});
// Listen to change events and fire an update every 500ms
editor.on('change', _.throttle(updateFieldValue, 500, { leading: false }))
})
function updateFieldValue () {
var value = editor.getValue()
extensionField.field.setData(value).catch(function(error){
console.log('error in setting data',error)
})
}
</script>
</body>
</html>