2023-10-11 02:38:59 +08:00
|
|
|
import Component from "@ember/component";
|
2023-02-23 23:32:53 +08:00
|
|
|
import { action } from "@ember/object";
|
|
|
|
import { classNames } from "@ember-decorators/component";
|
2023-02-24 17:50:52 +08:00
|
|
|
import { observes, on } from "@ember-decorators/object";
|
2023-10-17 20:56:59 +08:00
|
|
|
import $ from "jquery";
|
2015-03-10 00:49:11 +08:00
|
|
|
import loadScript from "discourse/lib/load-script";
|
2023-10-11 02:38:59 +08:00
|
|
|
import getURL from "discourse-common/lib/get-url";
|
2023-02-24 17:50:52 +08:00
|
|
|
import { bind } from "discourse-common/utils/decorators";
|
2023-10-11 02:38:59 +08:00
|
|
|
import I18n from "I18n";
|
2015-03-10 00:49:11 +08:00
|
|
|
|
2022-07-06 16:37:54 +08:00
|
|
|
const COLOR_VARS_REGEX =
|
|
|
|
/\$(primary|secondary|tertiary|quaternary|header_background|header_primary|highlight|danger|success|love)(\s|;|-(low|medium|high))/g;
|
2021-05-11 21:01:06 +08:00
|
|
|
|
2023-02-23 23:32:53 +08:00
|
|
|
@classNames("ace-wrapper")
|
|
|
|
export default class AceEditor extends Component {
|
|
|
|
mode = "css";
|
|
|
|
disabled = false;
|
|
|
|
htmlPlaceholder = false;
|
|
|
|
_editor = null;
|
|
|
|
_skipContentChangeEvent = null;
|
2015-03-10 00:49:11 +08:00
|
|
|
|
2017-04-14 04:21:46 +08:00
|
|
|
@observes("editorId")
|
|
|
|
editorIdChanged() {
|
2019-05-27 16:15:39 +08:00
|
|
|
if (this.autofocus) {
|
2017-04-14 04:21:46 +08:00
|
|
|
this.send("focus");
|
|
|
|
}
|
2023-02-23 23:32:53 +08:00
|
|
|
}
|
2017-04-14 04:21:46 +08:00
|
|
|
|
2021-05-06 01:45:52 +08:00
|
|
|
didRender() {
|
|
|
|
this._skipContentChangeEvent = false;
|
2023-02-23 23:32:53 +08:00
|
|
|
}
|
2021-05-06 01:45:52 +08:00
|
|
|
|
2016-11-29 00:29:56 +08:00
|
|
|
@observes("content")
|
|
|
|
contentChanged() {
|
2019-07-09 08:06:41 +08:00
|
|
|
const content = this.content || "";
|
|
|
|
if (this._editor && !this._skipContentChangeEvent) {
|
|
|
|
this._editor.getSession().setValue(content);
|
2015-03-10 00:49:11 +08:00
|
|
|
}
|
2023-02-23 23:32:53 +08:00
|
|
|
}
|
2015-03-10 00:49:11 +08:00
|
|
|
|
2017-04-12 22:52:52 +08:00
|
|
|
@observes("mode")
|
|
|
|
modeChanged() {
|
2018-10-01 10:06:01 +08:00
|
|
|
if (this._editor && !this._skipContentChangeEvent) {
|
2019-05-27 16:15:39 +08:00
|
|
|
this._editor.getSession().setMode("ace/mode/" + this.mode);
|
2017-04-12 22:52:52 +08:00
|
|
|
}
|
2023-02-23 23:32:53 +08:00
|
|
|
}
|
2017-04-12 22:52:52 +08:00
|
|
|
|
2020-08-19 01:02:13 +08:00
|
|
|
@observes("placeholder")
|
|
|
|
placeholderChanged() {
|
|
|
|
if (this._editor) {
|
|
|
|
this._editor.setOptions({
|
|
|
|
placeholder: this.placeholder,
|
|
|
|
});
|
|
|
|
}
|
2023-02-23 23:32:53 +08:00
|
|
|
}
|
2020-08-19 01:02:13 +08:00
|
|
|
|
2017-10-30 15:07:49 +08:00
|
|
|
@observes("disabled")
|
|
|
|
disabledStateChanged() {
|
|
|
|
this.changeDisabledState();
|
2023-02-23 23:32:53 +08:00
|
|
|
}
|
2017-10-30 15:07:49 +08:00
|
|
|
|
|
|
|
changeDisabledState() {
|
|
|
|
const editor = this._editor;
|
|
|
|
if (editor) {
|
2019-05-27 16:15:39 +08:00
|
|
|
const disabled = this.disabled;
|
2017-10-30 15:07:49 +08:00
|
|
|
editor.setOptions({
|
|
|
|
readOnly: disabled,
|
|
|
|
highlightActiveLine: !disabled,
|
|
|
|
highlightGutterLine: !disabled,
|
|
|
|
});
|
|
|
|
editor.container.parentNode.setAttribute("data-disabled", disabled);
|
|
|
|
}
|
2023-02-23 23:32:53 +08:00
|
|
|
}
|
2017-10-30 15:07:49 +08:00
|
|
|
|
2023-02-23 23:32:53 +08:00
|
|
|
@on("willDestroyElement")
|
|
|
|
_destroyEditor() {
|
2015-03-10 00:49:11 +08:00
|
|
|
if (this._editor) {
|
|
|
|
this._editor.destroy();
|
|
|
|
this._editor = null;
|
|
|
|
}
|
2015-07-16 01:15:05 +08:00
|
|
|
if (this.appEvents) {
|
|
|
|
// xxx: don't run during qunit tests
|
2020-03-31 00:55:15 +08:00
|
|
|
this.appEvents.off("ace:resize", this, "resize");
|
2015-07-16 01:15:05 +08:00
|
|
|
}
|
2017-05-16 04:10:07 +08:00
|
|
|
|
|
|
|
$(window).off("ace:resize");
|
2023-02-23 23:32:53 +08:00
|
|
|
}
|
2015-03-10 00:49:11 +08:00
|
|
|
|
2015-07-10 04:25:33 +08:00
|
|
|
resize() {
|
|
|
|
if (this._editor) {
|
|
|
|
this._editor.resize();
|
|
|
|
}
|
2023-02-23 23:32:53 +08:00
|
|
|
}
|
2015-07-10 04:25:33 +08:00
|
|
|
|
2016-11-29 00:29:56 +08:00
|
|
|
didInsertElement() {
|
2023-02-23 23:32:53 +08:00
|
|
|
super.didInsertElement(...arguments);
|
2020-09-12 01:53:56 +08:00
|
|
|
loadScript("/javascripts/ace/ace.js").then(() => {
|
2016-11-29 00:29:56 +08:00
|
|
|
window.ace.require(["ace/ace"], (loadedAce) => {
|
2020-08-15 01:15:30 +08:00
|
|
|
loadedAce.config.set("loadWorkerFromBlob", false);
|
|
|
|
loadedAce.config.set("workerPath", getURL("/javascripts/ace")); // Do not use CDN for workers
|
|
|
|
|
2021-01-22 23:03:43 +08:00
|
|
|
if (this.htmlPlaceholder) {
|
|
|
|
this._overridePlaceholder(loadedAce);
|
|
|
|
}
|
|
|
|
|
2016-12-20 00:19:10 +08:00
|
|
|
if (!this.element || this.isDestroying || this.isDestroyed) {
|
|
|
|
return;
|
|
|
|
}
|
2019-07-16 18:45:15 +08:00
|
|
|
const editor = loadedAce.edit(this.element.querySelector(".ace"));
|
2015-05-14 04:24:49 +08:00
|
|
|
|
|
|
|
editor.setShowPrintMargin(false);
|
2020-08-19 01:02:13 +08:00
|
|
|
editor.setOptions({ fontSize: "14px", placeholder: this.placeholder });
|
2019-05-27 16:15:39 +08:00
|
|
|
editor.getSession().setMode("ace/mode/" + this.mode);
|
2016-11-29 00:29:56 +08:00
|
|
|
editor.on("change", () => {
|
|
|
|
this._skipContentChangeEvent = true;
|
|
|
|
this.set("content", editor.getSession().getValue());
|
2015-05-14 04:24:49 +08:00
|
|
|
});
|
2021-05-04 19:56:10 +08:00
|
|
|
if (this.attrs.save) {
|
|
|
|
editor.commands.addCommand({
|
|
|
|
name: "save",
|
|
|
|
exec: () => {
|
|
|
|
this.attrs.save();
|
|
|
|
},
|
|
|
|
bindKey: { mac: "cmd-s", win: "ctrl-s" },
|
|
|
|
});
|
|
|
|
}
|
2021-05-11 21:01:06 +08:00
|
|
|
|
|
|
|
editor.on("blur", () => {
|
|
|
|
this.warnSCSSDeprecations();
|
|
|
|
});
|
|
|
|
|
2015-07-10 04:25:33 +08:00
|
|
|
editor.$blockScrolling = Infinity;
|
2017-04-19 03:43:22 +08:00
|
|
|
editor.renderer.setScrollMargin(10, 10);
|
2015-05-14 04:24:49 +08:00
|
|
|
|
2019-07-16 18:45:15 +08:00
|
|
|
this.element.setAttribute("data-editor", editor);
|
2016-11-29 00:29:56 +08:00
|
|
|
this._editor = editor;
|
2017-10-30 15:07:49 +08:00
|
|
|
this.changeDisabledState();
|
2021-05-11 21:01:06 +08:00
|
|
|
this.warnSCSSDeprecations();
|
2017-05-16 04:10:07 +08:00
|
|
|
|
|
|
|
$(window)
|
|
|
|
.off("ace:resize")
|
2020-03-31 00:55:15 +08:00
|
|
|
.on("ace:resize", () => this.appEvents.trigger("ace:resize"));
|
2017-05-16 04:10:07 +08:00
|
|
|
|
2016-11-29 00:29:56 +08:00
|
|
|
if (this.appEvents) {
|
2015-07-16 01:15:05 +08:00
|
|
|
// xxx: don't run during qunit tests
|
2019-03-20 19:50:13 +08:00
|
|
|
this.appEvents.on("ace:resize", this, "resize");
|
2015-07-16 01:15:05 +08:00
|
|
|
}
|
2017-04-14 04:21:46 +08:00
|
|
|
|
2019-05-27 16:15:39 +08:00
|
|
|
if (this.autofocus) {
|
2017-04-14 04:21:46 +08:00
|
|
|
this.send("focus");
|
|
|
|
}
|
2021-12-30 00:02:37 +08:00
|
|
|
|
|
|
|
this.setAceTheme();
|
|
|
|
this._darkModeListener = window.matchMedia(
|
|
|
|
"(prefers-color-scheme: dark)"
|
|
|
|
);
|
|
|
|
this._darkModeListener.addListener(this.setAceTheme);
|
2015-03-10 00:49:11 +08:00
|
|
|
});
|
|
|
|
});
|
2023-02-23 23:32:53 +08:00
|
|
|
}
|
2017-04-14 04:21:46 +08:00
|
|
|
|
2021-12-30 00:02:37 +08:00
|
|
|
willDestroyElement() {
|
2022-07-06 21:20:00 +08:00
|
|
|
if (this._darkModeListener) {
|
|
|
|
this._darkModeListener.removeListener(this.setAceTheme);
|
|
|
|
}
|
2023-02-23 23:32:53 +08:00
|
|
|
}
|
2021-12-30 00:02:37 +08:00
|
|
|
|
|
|
|
@bind
|
|
|
|
setAceTheme() {
|
|
|
|
const schemeType = getComputedStyle(document.body)
|
|
|
|
.getPropertyValue("--scheme-type")
|
|
|
|
.trim();
|
|
|
|
|
|
|
|
this._editor.setTheme(
|
|
|
|
`ace/theme/${schemeType === "dark" ? "chaos" : "chrome"}`
|
|
|
|
);
|
2023-02-23 23:32:53 +08:00
|
|
|
}
|
2021-12-30 00:02:37 +08:00
|
|
|
|
2021-05-11 21:01:06 +08:00
|
|
|
warnSCSSDeprecations() {
|
|
|
|
if (
|
|
|
|
this.mode !== "scss" ||
|
|
|
|
this.editorId.startsWith("color_definitions") ||
|
|
|
|
!this._editor
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let warnings = this.content
|
|
|
|
.split("\n")
|
|
|
|
.map((line, row) => {
|
|
|
|
if (line.match(COLOR_VARS_REGEX)) {
|
|
|
|
return {
|
|
|
|
row,
|
|
|
|
column: 0,
|
|
|
|
text: I18n.t("admin.customize.theme.scss_warning_inline"),
|
|
|
|
type: "warning",
|
|
|
|
};
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.filter(Boolean);
|
|
|
|
|
|
|
|
this._editor.getSession().setAnnotations(warnings);
|
|
|
|
|
2023-02-24 17:34:56 +08:00
|
|
|
this.setWarning?.(
|
2021-05-11 21:01:06 +08:00
|
|
|
warnings.length
|
|
|
|
? I18n.t("admin.customize.theme.scss_color_variables_warning")
|
|
|
|
: false
|
|
|
|
);
|
2023-02-23 23:32:53 +08:00
|
|
|
}
|
2021-05-11 21:01:06 +08:00
|
|
|
|
2023-02-23 23:32:53 +08:00
|
|
|
@action
|
|
|
|
focus() {
|
|
|
|
if (this._editor) {
|
|
|
|
this._editor.focus();
|
|
|
|
this._editor.navigateFileEnd();
|
|
|
|
}
|
|
|
|
}
|
2021-01-22 23:03:43 +08: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();
|
|
|
|
};
|
2023-02-23 23:32:53 +08:00
|
|
|
}
|
|
|
|
}
|