2019-10-23 12:30:52 -04:00
|
|
|
import Component from "@ember/component";
|
2020-08-14 13:15:30 -04:00
|
|
|
import getURL from "discourse-common/lib/get-url";
|
2015-03-09 12:49:11 -04:00
|
|
|
import loadScript from "discourse/lib/load-script";
|
2019-11-07 16:38:28 -05:00
|
|
|
import { observes } from "discourse-common/utils/decorators";
|
2019-10-30 18:05:27 -04:00
|
|
|
import { on } from "@ember/object/evented";
|
2015-03-09 12:49:11 -04:00
|
|
|
|
2019-10-23 12:30:52 -04:00
|
|
|
export default Component.extend({
|
2015-03-09 12:49:11 -04:00
|
|
|
mode: "css",
|
|
|
|
classNames: ["ace-wrapper"],
|
|
|
|
_editor: null,
|
|
|
|
_skipContentChangeEvent: null,
|
2017-10-30 03:07:49 -04:00
|
|
|
disabled: false,
|
2021-01-22 10:03:43 -05:00
|
|
|
htmlPlaceholder: false,
|
2015-03-09 12:49:11 -04:00
|
|
|
|
2017-04-13 16:21:46 -04:00
|
|
|
@observes("editorId")
|
|
|
|
editorIdChanged() {
|
2019-05-27 04:15:39 -04:00
|
|
|
if (this.autofocus) {
|
2017-04-13 16:21:46 -04:00
|
|
|
this.send("focus");
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-11-28 11:29:56 -05:00
|
|
|
@observes("content")
|
|
|
|
contentChanged() {
|
2019-07-08 20:06:41 -04:00
|
|
|
const content = this.content || "";
|
|
|
|
if (this._editor && !this._skipContentChangeEvent) {
|
|
|
|
this._editor.getSession().setValue(content);
|
2015-03-09 12:49:11 -04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-04-12 10:52:52 -04:00
|
|
|
@observes("mode")
|
|
|
|
modeChanged() {
|
2018-09-30 22:06:01 -04:00
|
|
|
if (this._editor && !this._skipContentChangeEvent) {
|
2019-05-27 04:15:39 -04:00
|
|
|
this._editor.getSession().setMode("ace/mode/" + this.mode);
|
2017-04-12 10:52:52 -04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2020-08-18 13:02:13 -04:00
|
|
|
@observes("placeholder")
|
|
|
|
placeholderChanged() {
|
|
|
|
if (this._editor) {
|
|
|
|
this._editor.setOptions({
|
|
|
|
placeholder: this.placeholder,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-10-30 03:07:49 -04:00
|
|
|
@observes("disabled")
|
|
|
|
disabledStateChanged() {
|
|
|
|
this.changeDisabledState();
|
|
|
|
},
|
|
|
|
|
|
|
|
changeDisabledState() {
|
|
|
|
const editor = this._editor;
|
|
|
|
if (editor) {
|
2019-05-27 04:15:39 -04:00
|
|
|
const disabled = this.disabled;
|
2017-10-30 03:07:49 -04:00
|
|
|
editor.setOptions({
|
|
|
|
readOnly: disabled,
|
|
|
|
highlightActiveLine: !disabled,
|
|
|
|
highlightGutterLine: !disabled,
|
|
|
|
});
|
|
|
|
editor.container.parentNode.setAttribute("data-disabled", disabled);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2019-10-30 18:05:27 -04:00
|
|
|
_destroyEditor: on("willDestroyElement", function () {
|
2015-03-09 12:49:11 -04:00
|
|
|
if (this._editor) {
|
|
|
|
this._editor.destroy();
|
|
|
|
this._editor = null;
|
|
|
|
}
|
2015-07-15 13:15:05 -04:00
|
|
|
if (this.appEvents) {
|
|
|
|
// xxx: don't run during qunit tests
|
2020-03-30 12:55:15 -04:00
|
|
|
this.appEvents.off("ace:resize", this, "resize");
|
2015-07-15 13:15:05 -04:00
|
|
|
}
|
2017-05-15 16:10:07 -04:00
|
|
|
|
|
|
|
$(window).off("ace:resize");
|
2019-10-17 19:49:41 -04:00
|
|
|
}),
|
2015-03-09 12:49:11 -04:00
|
|
|
|
2015-07-09 16:25:33 -04:00
|
|
|
resize() {
|
|
|
|
if (this._editor) {
|
|
|
|
this._editor.resize();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-11-28 11:29:56 -05:00
|
|
|
didInsertElement() {
|
2019-01-19 04:05:51 -05:00
|
|
|
this._super(...arguments);
|
2020-09-11 13:53:56 -04:00
|
|
|
loadScript("/javascripts/ace/ace.js").then(() => {
|
2016-11-28 11:29:56 -05:00
|
|
|
window.ace.require(["ace/ace"], (loadedAce) => {
|
2020-08-14 13:15:30 -04:00
|
|
|
loadedAce.config.set("loadWorkerFromBlob", false);
|
|
|
|
loadedAce.config.set("workerPath", getURL("/javascripts/ace")); // Do not use CDN for workers
|
|
|
|
|
2021-01-22 10:03:43 -05:00
|
|
|
if (this.htmlPlaceholder) {
|
|
|
|
this._overridePlaceholder(loadedAce);
|
|
|
|
}
|
|
|
|
|
2016-12-19 11:19:10 -05:00
|
|
|
if (!this.element || this.isDestroying || this.isDestroyed) {
|
|
|
|
return;
|
|
|
|
}
|
2019-07-16 06:45:15 -04:00
|
|
|
const editor = loadedAce.edit(this.element.querySelector(".ace"));
|
2015-05-13 16:24:49 -04:00
|
|
|
|
2018-09-30 22:06:01 -04:00
|
|
|
editor.setTheme("ace/theme/chrome");
|
2015-05-13 16:24:49 -04:00
|
|
|
editor.setShowPrintMargin(false);
|
2020-08-18 13:02:13 -04:00
|
|
|
editor.setOptions({ fontSize: "14px", placeholder: this.placeholder });
|
2019-05-27 04:15:39 -04:00
|
|
|
editor.getSession().setMode("ace/mode/" + this.mode);
|
2016-11-28 11:29:56 -05:00
|
|
|
editor.on("change", () => {
|
|
|
|
this._skipContentChangeEvent = true;
|
|
|
|
this.set("content", editor.getSession().getValue());
|
|
|
|
this._skipContentChangeEvent = false;
|
2015-05-13 16:24:49 -04:00
|
|
|
});
|
2015-07-09 16:25:33 -04:00
|
|
|
editor.$blockScrolling = Infinity;
|
2017-04-18 15:43:22 -04:00
|
|
|
editor.renderer.setScrollMargin(10, 10);
|
2015-05-13 16:24:49 -04:00
|
|
|
|
2019-07-16 06:45:15 -04:00
|
|
|
this.element.setAttribute("data-editor", editor);
|
2016-11-28 11:29:56 -05:00
|
|
|
this._editor = editor;
|
2017-10-30 03:07:49 -04:00
|
|
|
this.changeDisabledState();
|
2017-05-15 16:10:07 -04:00
|
|
|
|
|
|
|
$(window)
|
|
|
|
.off("ace:resize")
|
2020-03-30 12:55:15 -04:00
|
|
|
.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 13:15:05 -04:00
|
|
|
// xxx: don't run during qunit tests
|
2019-03-20 07:50:13 -04:00
|
|
|
this.appEvents.on("ace:resize", this, "resize");
|
2015-07-15 13:15:05 -04:00
|
|
|
}
|
2017-04-13 16:21:46 -04:00
|
|
|
|
2019-05-27 04:15:39 -04:00
|
|
|
if (this.autofocus) {
|
2017-04-13 16:21:46 -04:00
|
|
|
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
|
|
|
},
|
2021-01-22 10:03:43 -05:00
|
|
|
|
|
|
|
_overridePlaceholder(loadedAce) {
|
|
|
|
const originalPlaceholderSetter =
|
|
|
|
loadedAce.config.$defaultOptions.editor.placeholder.set;
|
|
|
|
|
|
|
|
loadedAce.config.$defaultOptions.editor.placeholder.set = function () {
|
|
|
|
if (!this.$updatePlaceholder) {
|
|
|
|
const originalRendererOn = this.renderer.on;
|
|
|
|
this.renderer.on = function () {};
|
|
|
|
originalPlaceholderSetter.call(this, ...arguments);
|
|
|
|
this.renderer.on = originalRendererOn;
|
|
|
|
|
|
|
|
const originalUpdatePlaceholder = this.$updatePlaceholder;
|
|
|
|
|
|
|
|
this.$updatePlaceholder = function () {
|
|
|
|
originalUpdatePlaceholder.call(this, ...arguments);
|
|
|
|
|
|
|
|
if (this.renderer.placeholderNode) {
|
|
|
|
this.renderer.placeholderNode.innerHTML = this.$placeholder || "";
|
|
|
|
}
|
|
|
|
}.bind(this);
|
|
|
|
|
|
|
|
this.on("input", this.$updatePlaceholder);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.$updatePlaceholder();
|
|
|
|
};
|
|
|
|
},
|
2016-11-28 11:29:56 -05:00
|
|
|
});
|