UNPKG

spincycle

Version:

A reactive message router and object manager that lets clients subscribe to object property changes on the server

325 lines (298 loc) 10.6 kB
<!-- @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 An ace-editor in an Polymer element @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="./ace-widget-shadow-dom.html"> <dom-module id="ace-widget"> <style> :host { display: block; width: 100%; } #editor { border: 1px solid #e3e3e3; border-radius: 4px; } </style> <template> <div id="editor"></div> </template> <script> 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: "" }, }, behaviors: [ AceWidgetShadowDom ], ready: function () { // console.debug("[ace-widget] ready") var self = this; 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}); }, attached: function () { // console.debug("[ace-widget] attached") this.initializeEditor(); }, initializeEditor: function () { var self = this; var editor = this.editor; this.head = document.head; this.themeChanged(); ace.config.set('basePath', this.resolveUrl('../ace-builds/src-min-noconflict/')); 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.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 self = this; var aceWidgetCompleter = { getCompletions: function(editor, session, pos, prefix, callback) { if (prefix.length === 0) { callback(null, []); return; } callback(null, self.autoComplete || []); } }; langTools.addCompleter(aceWidgetCompleter); 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; 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.appendChild(cloneStyle(style)); } else { } }, 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) { 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; 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) { 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"; console.debug("[ace-widget] _updatePlaceholder - node", node); this.editor.renderer.scroller.appendChild(node); } }, }) </script> </dom-module>