329 lines
10 KiB
HTML
329 lines
10 KiB
HTML
|
|
<!--
|
||
|
|
@license MIT
|
||
|
|
Copyright (c) 2015 Horacio "LostInBrittany" Gonzalez
|
||
|
|
|
||
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||
|
|
|
||
|
|
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||
|
|
|
||
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||
|
|
|
||
|
|
@element ace-widget
|
||
|
|
@blurb LostInBrittany's Ace (http://ace.c9.io/) widget
|
||
|
|
@homepage index.html
|
||
|
|
@demo demo/index.html
|
||
|
|
-->
|
||
|
|
<script src="../ace-builds/src-noconflict/ace.js"></script>
|
||
|
|
|
||
|
|
<script src="../ace-builds/src-noconflict/ext-language_tools.js"></script>
|
||
|
|
<script src="../ace-builds/src-noconflict/snippets/snippets.js"></script>
|
||
|
|
<link rel="import" href="../polymer/polymer.html">
|
||
|
|
<link rel="import" href="./ace-widget-shadow-dom.html">
|
||
|
|
|
||
|
|
<dom-module id="ace-widget">
|
||
|
|
<template>
|
||
|
|
<style>
|
||
|
|
:host {
|
||
|
|
display: block;
|
||
|
|
width: 100%;
|
||
|
|
}
|
||
|
|
|
||
|
|
#editor {
|
||
|
|
border: 1px solid #e3e3e3;
|
||
|
|
border-radius: 4px;
|
||
|
|
@apply --ace-widget-editor;
|
||
|
|
}
|
||
|
|
</style>
|
||
|
|
<div id="editor"></div>
|
||
|
|
</template>
|
||
|
|
|
||
|
|
<script>
|
||
|
|
/* global ace, AceWidgetShadowDom */
|
||
|
|
Polymer({
|
||
|
|
is: 'ace-widget',
|
||
|
|
properties: {
|
||
|
|
theme: {
|
||
|
|
type: String,
|
||
|
|
value: 'ace/theme/eclipse',
|
||
|
|
observer: 'themeChanged',
|
||
|
|
},
|
||
|
|
mode: {
|
||
|
|
type: String,
|
||
|
|
notify: true,
|
||
|
|
observer: 'modeChanged',
|
||
|
|
},
|
||
|
|
value: {
|
||
|
|
type: String,
|
||
|
|
notify: true,
|
||
|
|
observer: 'valueChanged',
|
||
|
|
},
|
||
|
|
readonly: {
|
||
|
|
type: Boolean,
|
||
|
|
value: false,
|
||
|
|
observer: 'readonlyChanged',
|
||
|
|
},
|
||
|
|
softtabs: {
|
||
|
|
type: Boolean,
|
||
|
|
value: true,
|
||
|
|
observer: 'softtabsChanged',
|
||
|
|
},
|
||
|
|
wrap: {
|
||
|
|
type: Boolean,
|
||
|
|
value: false,
|
||
|
|
observer: 'wrapChanged',
|
||
|
|
},
|
||
|
|
fontSize: {
|
||
|
|
type: String,
|
||
|
|
value: '14px',
|
||
|
|
observer: 'fontSizeChanged',
|
||
|
|
},
|
||
|
|
tabSize: {
|
||
|
|
type: Number,
|
||
|
|
value: 4,
|
||
|
|
observer: 'tabSizeChanged',
|
||
|
|
},
|
||
|
|
snippets: {
|
||
|
|
type: String,
|
||
|
|
notify: true,
|
||
|
|
},
|
||
|
|
autoComplete: {
|
||
|
|
type: Object,
|
||
|
|
notify: true,
|
||
|
|
},
|
||
|
|
minlines: {
|
||
|
|
type: Number,
|
||
|
|
value: 15,
|
||
|
|
},
|
||
|
|
maxlines: {
|
||
|
|
type: Number,
|
||
|
|
value: 30,
|
||
|
|
},
|
||
|
|
enableLiveAutocompletion: {
|
||
|
|
type: Boolean,
|
||
|
|
value: false,
|
||
|
|
},
|
||
|
|
enableSnippets: {
|
||
|
|
type: Boolean,
|
||
|
|
value: false,
|
||
|
|
},
|
||
|
|
initialFocus: {
|
||
|
|
type: Boolean,
|
||
|
|
value: false,
|
||
|
|
},
|
||
|
|
placeholder: {
|
||
|
|
type: String,
|
||
|
|
value: '',
|
||
|
|
},
|
||
|
|
verbose: {
|
||
|
|
type: Boolean,
|
||
|
|
value: false,
|
||
|
|
},
|
||
|
|
baseUrl: {
|
||
|
|
type: String,
|
||
|
|
value: '',
|
||
|
|
}
|
||
|
|
},
|
||
|
|
behaviors: [
|
||
|
|
AceWidgetShadowDom,
|
||
|
|
],
|
||
|
|
ready: function() {
|
||
|
|
|
||
|
|
},
|
||
|
|
attached: function() {
|
||
|
|
// console.debug("[ace-widget] attached")
|
||
|
|
var div = this.$.editor;
|
||
|
|
div.style.width = '100%';
|
||
|
|
div.style.height = '100%';
|
||
|
|
this.editor = ace.edit(div);
|
||
|
|
this.init();
|
||
|
|
|
||
|
|
this.fire('editor-ready', {value: this.editor, oldValue: null});
|
||
|
|
// console.debug("[ace-widget] attached")
|
||
|
|
this.initializeEditor();
|
||
|
|
},
|
||
|
|
|
||
|
|
initializeEditor: function() {
|
||
|
|
var self = this;
|
||
|
|
var editor = this.editor;
|
||
|
|
|
||
|
|
this.head = document.head;
|
||
|
|
if (this.baseUrl)
|
||
|
|
ace.config.set('basePath', this.baseUrl);
|
||
|
|
else
|
||
|
|
ace.config.set('basePath', this.resolveUrl('../ace-builds/src-min-noconflict/'));
|
||
|
|
this.themeChanged();
|
||
|
|
this.editorValue = '';
|
||
|
|
editor.setOption('enableSnippets', this.enableSnippets);
|
||
|
|
editor.setOption('enableBasicAutocompletion', true);
|
||
|
|
editor.setOption('enableLiveAutocompletion', this.enableLiveAutocompletion);
|
||
|
|
|
||
|
|
editor.on('change', this.editorChangeAction.bind(this));
|
||
|
|
editor.on('input', this._updatePlaceholder.bind(this));
|
||
|
|
setTimeout(this._updatePlaceholder.bind(this), 100);
|
||
|
|
this.session = editor.getSession();
|
||
|
|
|
||
|
|
if (this.initialFocus) {
|
||
|
|
editor.focus();
|
||
|
|
}
|
||
|
|
|
||
|
|
editor.$blockScrolling = Infinity;
|
||
|
|
|
||
|
|
editor.setTheme(this.theme);
|
||
|
|
|
||
|
|
// Forcing a xyzChanged() call, because the initial one din't do anything as editor wasn't created yet
|
||
|
|
this.readonlyChanged();
|
||
|
|
this.wrapChanged();
|
||
|
|
this.tabSizeChanged();
|
||
|
|
this.modeChanged();
|
||
|
|
this.softtabsChanged();
|
||
|
|
this.fontSizeChanged();
|
||
|
|
|
||
|
|
// Setting content
|
||
|
|
|
||
|
|
// Trying to get content as HTML content
|
||
|
|
var htmlContent = Polymer.dom(this).innerHTML.trim();
|
||
|
|
// console.debug("[ace-widget] HTML content found", htmlContent);
|
||
|
|
|
||
|
|
// If we have a value in the `value` attribute, we keep it, else we use the HTML content
|
||
|
|
if (this.value === undefined) {
|
||
|
|
this.value = htmlContent;
|
||
|
|
// console.debug("[ace-widget] initializeEditor - using HTML content as value", this.value)
|
||
|
|
} else {
|
||
|
|
// Forcing a valueChanged() call, because the initial one din't do anything as editor wasn't created yet
|
||
|
|
this.valueChanged();
|
||
|
|
}
|
||
|
|
// min and max lines
|
||
|
|
editor.setOptions({
|
||
|
|
minLines: this.minlines,
|
||
|
|
maxLines: this.maxlines,
|
||
|
|
});
|
||
|
|
|
||
|
|
// snippets
|
||
|
|
if (this.enableSnippets) {
|
||
|
|
var snippetManager = ace.require('ace/snippets').snippetManager;
|
||
|
|
snippetManager.register(this.snippets, 'javascript');
|
||
|
|
}
|
||
|
|
// autoComplete
|
||
|
|
var langTools = ace.require('ace/ext/language_tools');
|
||
|
|
var aceWidgetCompleter = {
|
||
|
|
getCompletions: function(editor, session, pos, prefix, callback) {
|
||
|
|
if (prefix.length === 0) {
|
||
|
|
callback(null, []);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
callback(null, self.autoComplete || []);
|
||
|
|
},
|
||
|
|
};
|
||
|
|
langTools.addCompleter(aceWidgetCompleter);
|
||
|
|
|
||
|
|
if (this.verbose) {
|
||
|
|
console.debug('[ace-widget] After initializing: editor.getSession().getValue()',
|
||
|
|
editor.getSession().getValue());
|
||
|
|
}
|
||
|
|
},
|
||
|
|
fontSizeChanged: function() {
|
||
|
|
if (this.editor == undefined) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
this.$.editor.style.fontSize = this.fontSize;
|
||
|
|
},
|
||
|
|
modeChanged: function() {
|
||
|
|
if (!this.editor) return;
|
||
|
|
this.editor.getSession().setMode(this.mode);
|
||
|
|
},
|
||
|
|
softtabsChanged: function() {
|
||
|
|
if (this.editor == undefined) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
this.editor.getSession().setUseSoftTabs(this.softtabs);
|
||
|
|
},
|
||
|
|
themeChanged: function() {
|
||
|
|
if (this.editor == undefined) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
this.editor.setTheme(this.theme);
|
||
|
|
return;
|
||
|
|
},
|
||
|
|
valueChanged: function() {
|
||
|
|
// console.debug("[ace-widget] valueChanged - ",this.value)
|
||
|
|
if (this.editor == undefined) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
if (this.editorValue != this.value) {
|
||
|
|
this.editorValue = this.value;
|
||
|
|
this.editor.clearSelection();
|
||
|
|
this.editor.resize();
|
||
|
|
}
|
||
|
|
},
|
||
|
|
readonlyChanged: function() {
|
||
|
|
if (this.editor == undefined) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
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() {
|
||
|
|
if (this.editor == undefined) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
this.editor.getSession().setUseWrapMode(this.wrap);
|
||
|
|
},
|
||
|
|
tabSizeChanged: function() {
|
||
|
|
if (this.editor == undefined) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
if (this.tabSize) {
|
||
|
|
this.editor.getSession().setTabSize(this.tabSize);
|
||
|
|
}
|
||
|
|
},
|
||
|
|
editorChangeAction: function() {
|
||
|
|
// console.debug("[ace-widget] editorChangeAction", {value: this.editorValue, oldValue: this._value})
|
||
|
|
this.fire('editor-content', {value: this.editorValue, oldValue: this._value});
|
||
|
|
},
|
||
|
|
get editorValue() {
|
||
|
|
return this.editor.getValue();
|
||
|
|
},
|
||
|
|
set editorValue(value) {
|
||
|
|
if (value === undefined) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
this._value = value;
|
||
|
|
this.editor.setValue(value);
|
||
|
|
// console.debug("[ace-widget] set editorValue", this.editorValue)
|
||
|
|
},
|
||
|
|
focus: function() {
|
||
|
|
this.editor.focus();
|
||
|
|
},
|
||
|
|
_updatePlaceholder: function() {
|
||
|
|
var shouldShow = !this.editor.session.getValue().length;
|
||
|
|
var node = this.editor.renderer.emptyMessageNode;
|
||
|
|
if (this.verbose) {
|
||
|
|
console.debug('[ace-widget] _updatePlaceholder', {shouldShow: shouldShow, node: node});
|
||
|
|
}
|
||
|
|
if (!shouldShow && node) {
|
||
|
|
this.editor.renderer.scroller.removeChild(this.editor.renderer.emptyMessageNode);
|
||
|
|
this.editor.renderer.emptyMessageNode = null;
|
||
|
|
} else if (shouldShow && !node) {
|
||
|
|
if (this.verbose) {
|
||
|
|
console.debug('[ace-widget] _updatePlaceholder - shouldShow && !node');
|
||
|
|
}
|
||
|
|
node = this.editor.renderer.emptyMessageNode = document.createElement('div');
|
||
|
|
node.textContent = this.placeholder;
|
||
|
|
node.className = 'ace_comment';
|
||
|
|
node.style.padding = '0 9px';
|
||
|
|
node.style.zIndex = '1';
|
||
|
|
node.style.position = 'absolute';
|
||
|
|
node.style.color = '#aaa';
|
||
|
|
if (this.verbose) {
|
||
|
|
console.debug('[ace-widget] _updatePlaceholder - node', node);
|
||
|
|
}
|
||
|
|
this.editor.renderer.scroller.appendChild(node);
|
||
|
|
}
|
||
|
|
},
|
||
|
|
});
|
||
|
|
</script>
|
||
|
|
|
||
|
|
</dom-module>
|