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