This repository was archived by the owner on Nov 13, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaiplugs-monaco.html
106 lines (93 loc) · 2.67 KB
/
aiplugs-monaco.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
<link rel="import" href="../polymer/polymer-element.html">
<dom-module id="aiplugs-monaco">
<template>
<style>
:host {
display: flex;
flex-direction: column;
}
#container {
flex: 1;
}
#editor {
width: 100%;
height: 100%;;
}
textarea {
display: none;
}
</style>
<div id="container">
<iframe id="editor" scrolling="no" frameborder="0"></iframe>
<textarea name="[[name]]" value="{{value::input}}"></textarea>
</div>
</template>
<script>
class AiplugsMonaco extends Polymer.Element {
static get is() { return 'aiplugs-monaco'; }
static get properties() {
return {
name: String,
value: String,
opts: {
type: Object,
value: {}
},
jsonschema: {
type: String,
observer: '_setJsonSchema'
}
};
}
ready() {
super.ready();
const id = this.name;
const src = `${location}/../../docs/monaco-iframe.html?parent=${location.origin}&id=${id}`;
const origin = new URL(src).origin;
const event = type => `monaco#${id}.${type}`;
const fire = (type, payload = {}) => {
if (this.$.editor.contentWindow) {
payload.type = event(type);
this.$.editor.contentWindow.postMessage(JSON.stringify(payload), origin);
}
}
this._fire = fire;
this.$.editor.src = src;
const self = this;
this.__onmessage = function onMessage(evt) {
if (evt.origin !== origin)
return
if (!evt.data)
return
const data = JSON.parse(evt.data)
const type = data.type
if (type === event('change')) {
self.value = data.value;
}
else if (type === event('created')) {
fire('setup', { opts: self.opts });
fire('update', { value: self.value });
self._setJsonSchema(self.jsonschema);
}
}
window.addEventListener('message', this.__onmessage)
}
disconnectedCallback() {
super.disconnectedCallback();
window.removeEventListener('message', this.__onmessage);
}
_setJsonSchema(uri) {
if (uri) {
fetch(uri, {mode: 'cors'}).then(res => {
if (res.ok) {
res.json().then(schema => {
this._fire('append.jsonschema', { uri, schema });
})
}
})
}
}
}
window.customElements.define(AiplugsMonaco.is, AiplugsMonaco);
</script>
</dom-module>