discourse/app/assets/javascripts/admin/components/ace-editor.js.es6

128 lines
3.1 KiB
Plaintext
Raw Normal View History

2018-06-15 11:03:24 -04:00
import loadScript from "discourse/lib/load-script";
import { observes } from "ember-addons/ember-computed-decorators";
const LOAD_ASYNC = !Ember.testing;
export default Ember.Component.extend({
2018-06-15 11:03:24 -04:00
mode: "css",
classNames: ["ace-wrapper"],
_editor: null,
_skipContentChangeEvent: null,
disabled: false,
2018-06-15 11:03:24 -04:00
@observes("editorId")
editorIdChanged() {
2018-06-15 11:03:24 -04:00
if (this.get("autofocus")) {
this.send("focus");
}
},
2018-06-15 11:03:24 -04:00
@observes("content")
contentChanged() {
if (this._editor && !this._skipContentChangeEvent) {
2018-06-15 11:03:24 -04:00
this._editor.getSession().setValue(this.get("content"));
}
},
2018-06-15 11:03:24 -04:00
@observes("mode")
modeChanged() {
if (LOAD_ASYNC && this._editor && !this._skipContentChangeEvent) {
2018-06-15 11:03:24 -04:00
this._editor.getSession().setMode("ace/mode/" + this.get("mode"));
}
},
2018-06-15 11:03:24 -04:00
@observes("disabled")
disabledStateChanged() {
this.changeDisabledState();
},
changeDisabledState() {
const editor = this._editor;
if (editor) {
2018-06-15 11:03:24 -04:00
const disabled = this.get("disabled");
editor.setOptions({
readOnly: disabled,
highlightActiveLine: !disabled,
highlightGutterLine: !disabled
});
editor.container.parentNode.setAttribute("data-disabled", disabled);
}
},
_destroyEditor: function() {
if (this._editor) {
this._editor.destroy();
this._editor = null;
}
if (this.appEvents) {
// xxx: don't run during qunit tests
2018-06-15 11:03:24 -04:00
this.appEvents.off("ace:resize", this, this.resize);
}
2018-06-15 11:03:24 -04:00
$(window).off("ace:resize");
}.on("willDestroyElement"),
resize() {
if (this._editor) {
this._editor.resize();
}
},
didInsertElement() {
this._super();
loadScript("/javascripts/ace/ace.js", { scriptTag: true }).then(() => {
2018-06-15 11:03:24 -04:00
window.ace.require(["ace/ace"], loadedAce => {
if (!this.element || this.isDestroying || this.isDestroyed) {
return;
}
const editor = loadedAce.edit(this.$(".ace")[0]);
if (LOAD_ASYNC) {
editor.setTheme("ace/theme/chrome");
}
editor.setShowPrintMargin(false);
2018-06-15 11:03:24 -04:00
editor.setOptions({ fontSize: "14px" });
if (LOAD_ASYNC) {
2018-06-15 11:03:24 -04:00
editor.getSession().setMode("ace/mode/" + this.get("mode"));
}
2018-06-15 11:03:24 -04:00
editor.on("change", () => {
this._skipContentChangeEvent = true;
2018-06-15 11:03:24 -04:00
this.set("content", editor.getSession().getValue());
this._skipContentChangeEvent = false;
});
editor.$blockScrolling = Infinity;
2018-06-15 11:03:24 -04:00
editor.renderer.setScrollMargin(10, 10);
2018-06-15 11:03:24 -04:00
this.$().data("editor", editor);
this._editor = editor;
this.changeDisabledState();
2018-06-15 11:03:24 -04:00
$(window)
.off("ace:resize")
.on("ace:resize", () => {
this.appEvents.trigger("ace:resize");
});
if (this.appEvents) {
// xxx: don't run during qunit tests
2018-06-15 11:03:24 -04:00
this.appEvents.on("ace:resize", () => this.resize());
}
if (this.get("autofocus")) {
this.send("focus");
}
});
});
},
actions: {
focus() {
if (this._editor) {
this._editor.focus();
this._editor.navigateFileEnd();
}
}
}
});