forked from googlearchive/ace-element
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ace-element.html
183 lines (181 loc) · 5.74 KB
/
ace-element.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
177
178
179
180
181
182
183
<!-- can remove when https://code.google.com/p/chromium/issues/detail?id=336698
is addressed. -->
<meta charset="UTF-8">
<!--
Copyright 2012 The Polymer Authors. All rights reserved.
Use of this source code is governed by a BSD-style
license that can be found in the LICENSE file.
-->
<link rel="import" href="../polymer/polymer.html">
<script src="src-min-noconflict/ace.js"></script>
<polymer-element name="ace-element" attributes="theme mode value readonly wrap fontSize tabSize">
<template>
<style type="text/css" media="screen">
:host {
display: block;
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
}
.ajaxorg-ace {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
</style>
<div class="ajaxorg-ace" id="editor"></div>
</template>
<script>
(function(style) {
Polymer('ace-element', {
mode: 'javascript',
theme: 'monokai',
readonly: false,
value: null,
wrap: false,
// allow styling from the outside world!
//applyAuthorStyles: true,
registerCallback: function(polymerElt) {
var selectors = [
'#ace_editor',
'#ace-tm'
];
var content = polymerElt.templateContent();
for (var i=0, l=selectors.length, s, n; (i<l) && (s=selectors[i]); i++) {
n = document.querySelector(s);
if (n) {
content.appendChild(cloneStyle(n));
}
}
},
// TODO(sorvell): to work in IE reliably, can only be
// created in enteredView. However, api that wants to access this.editor
// may be used before that. Instead of making each function bail if
// this.editor is not set, we create a dummy editor. At editor
// initialization time, any pending changes are synch'd.
ready: function() {
this.editor = ace.edit(document.createElement('div'));
},
attached: function() {
this.initializeEditor();
},
initializeEditor: function() {
ace.config.set('basePath', this.resolvePath('src-min-noconflict/'));
this.editor = ace.edit(this.$.editor);
this.editor.focus();
this.modeChanged();
this.themeChanged();
this.readonlyChanged();
this.wrapChanged();
this.tabSizeChanged();
this.editor.on("blur", this.editorBlurAction.bind(this));
this.editor.on('change', this.editorChangeAction.bind(this));
this.value = this.textContent;
},
fontSizeChanged: function() {
this.$.editor.style.fontSize = this.fontSize;
},
modeChanged: function() {
this.editor.getSession().setMode('ace/mode/' + this.mode);
},
themeChanged: function() {
this.editor.setTheme('ace/theme/' + this.theme);
this._whenMatches(document.head, '#ace-' + this.theme, 'applyTheme');
},
_whenMatches: function(node, selector, method) {
var m = node.querySelector(selector);
if (m) {
if (this[method]) {
this[method].call(this, m);
}
} else {
var cb = this._whenMatches.bind(this, node, selector, method);
this.onMutation(node, cb);
}
},
applyTheme: function(style) {
if (style) {
this.shadowRoot.appendChild(cloneStyle(style));
}
},
valueChanged: function() {
this.editorValue = this.value;
this.editor.clearSelection();
this.editor.resize();
//this.editor.focus();
},
readonlyChanged: function() {
this.editor.setReadOnly(this.readonly);
this.editor.setHighlightActiveLine(!this.readonly);
this.editor.setHighlightGutterLine(!this.readonly);
this.editor.renderer.$cursorLayer.element.style.opacity = this.readonly ? 0 : 1;
},
wrapChanged: function() {
this.editor.getSession().setUseWrapMode(this.wrap);
},
tabSizeChanged: function() {
if (this.tabSize) {
this.editor.getSession().setTabSize(this.tabSize);
}
},
foldTag: function(tag) {
var s = this.editor.getSession();
if (!s.foldWidgets) {
return
}
var pos = this.find('<' + tag, {start: 0});
// check if tag found
if (!pos) {
return;
}
var start = pos.start.row;
// find the fold range
var range = s.getFoldWidgetRange(start);
// check if can be folded
if (range) {
var end = range.end.row;
// fold
s.foldAll(start, end);
}
},
find: function() {
return this.editor.find.apply(this.editor, arguments);
},
gotoLine: function() {
this.editor.gotoLine.apply(this.editor, arguments);
this.editor.getSession().selection.moveCursorLineStart();
this.editor.clearSelection();
},
editorBlurAction: function(event) {
if (this._value !== null && this._value != this.editorValue) {
this.fire('editor-change', {value: this.editorValue, oldValue: this._value});
}
this._value = this.editorValue;
},
editorChangeAction: function() {
this.fire('editor-input', {value: this.editorValue, oldValue: this._value});
},
get editorValue() {
return this.editor.getValue();
},
set editorValue(value) {
this._value = value;
this.editor.setValue(value);
},
focus: function() {
this.editor.focus();
}
});
// IE safe style clone
function cloneStyle(style) {
var s = document.createElement('style');
s.textContent = style.textContent;
return s;
}
})();
</script>
</polymer-element>