BetterDiscordApp-rauenzi/src/builtins/customcss.js

155 lines
5.8 KiB
JavaScript
Raw Normal View History

2019-06-10 05:40:35 +02:00
import Builtin from "../structs/builtin";
2019-06-15 04:11:19 +02:00
import {Settings, DataStore, React, Utilities, WebpackModules, Events} from "modules";
2019-06-12 06:28:18 +02:00
import CSSEditor from "../ui/customcss/csseditor";
2019-06-15 04:11:19 +02:00
import FloatingWindowContainer from "../ui/floating/container";
2019-06-11 22:24:57 +02:00
import SettingsTitle from "../ui/settings/title";
2019-06-10 05:40:35 +02:00
2019-06-15 04:11:19 +02:00
const fs = require("fs");
2019-06-10 22:37:50 +02:00
const electron = require("electron");
2019-06-15 04:11:19 +02:00
const UserSettings = WebpackModules.getByProps("updateAccount");
const Dispatcher = WebpackModules.getByProps("dirtyDispatch");
const ActionTypes = WebpackModules.getByProps("ActionTypes").ActionTypes;
2019-06-10 22:37:50 +02:00
2019-06-10 05:40:35 +02:00
export default new class CustomCSS extends Builtin {
get name() {return "Custom CSS";}
get category() {return "customcss";}
get id() {return "customcss";}
2019-06-10 22:37:50 +02:00
get startDetached() {return Settings.get(this.collection, this.category, "startDetached");}
get nativeOpen() {return Settings.get(this.collection, this.category, "nativeOpen");}
constructor() {
super();
2019-06-12 06:28:18 +02:00
this.savedCss = "";
this.insertedCss = "";
2019-06-15 04:11:19 +02:00
this.isDetached = false;
2019-06-10 22:37:50 +02:00
}
2019-06-10 05:40:35 +02:00
2019-06-11 04:33:45 +02:00
async enabled() {
if (!window.ace) {
Utilities.injectJs("https://cdnjs.cloudflare.com/ajax/libs/ace/1.2.9/ace.js").then(() => {
if (window.require.original) window.require = window.require.original;
});
}
2019-06-10 05:40:35 +02:00
Settings.registerPanel(this.id, this.name, {
2019-06-10 22:37:50 +02:00
order: 2,
2019-06-11 22:24:57 +02:00
element: () => [<SettingsTitle text="Custom CSS Editor" />, React.createElement(CSSEditor, {
2019-06-12 06:28:18 +02:00
css: this.savedCss,
2019-06-10 22:37:50 +02:00
save: this.saveCSS.bind(this),
update: this.insertCSS.bind(this),
2019-06-11 22:24:57 +02:00
openNative: this.openNative.bind(this),
2019-06-15 04:11:19 +02:00
openDetached: this.openDetached.bind(this),
onChange: this.onChange.bind(this)
2019-06-11 22:24:57 +02:00
})],
2019-06-10 22:37:50 +02:00
onClick: (thisObject) => {
2019-06-15 04:11:19 +02:00
if (this.isDetached) return;
2019-06-10 22:37:50 +02:00
if (this.nativeOpen) this.openNative();
else if (this.startDetached) this.openDetached();
else thisObject._reactInternalFiber.child.memoizedProps.children.props.onSetSection(this.name);
2019-06-15 04:11:19 +02:00
this.setSection = thisObject._reactInternalFiber.child.memoizedProps.children.props.onSetSection;
2019-06-10 22:37:50 +02:00
}
2019-06-10 05:40:35 +02:00
});
2019-06-10 22:37:50 +02:00
this.loadCSS();
2019-06-12 06:28:18 +02:00
this.insertCSS(this.savedCss);
2019-06-15 04:11:19 +02:00
this.watchContent();
2019-06-10 05:40:35 +02:00
}
disabled() {
Settings.removePanel(this.id);
2019-06-15 04:11:19 +02:00
this.unwatchContent();
}
//* {outline: 1px solid red;}
//DataStore.customCSS
watchContent() {
if (this.watcher) return this.error("Already watching content.");
const timeCache = {};
this.log("Starting to watch content.");
this.watcher = fs.watch(DataStore.customCSS, {persistent: false}, async (eventType, filename) => {
if (!eventType || !filename) return;
await new Promise(r => setTimeout(r, 50));
try {fs.statSync(DataStore.customCSS);}
catch (err) {
if (err.code !== "ENOENT") return;
delete timeCache[filename];
this.saveCSS("");
}
const stats = fs.statSync(DataStore.customCSS);
if (!stats || !stats.mtime || !stats.mtime.getTime()) return;
if (typeof(stats.mtime.getTime()) !== "number") return;
if (timeCache[filename] == stats.mtime.getTime()) return;
timeCache[filename] = stats.mtime.getTime();
if (eventType == "change") {
const newCSS = DataStore.loadCustomCSS();
if (newCSS == this.savedCss) return;
this.savedCss = newCSS;
this.insertCSS(this.savedCss);
Events.emit("customcss-updated", this.savedCss);
}
});
}
unwatchContent() {
if (!this.watcher) return this.error("Was not watching content.");
this.watcher.close();
delete this.watcher;
this.log("No longer watching content.");
}
onChange(value) {
if (!Settings.get("settings", "customcss", "liveUpdate")) return;
this.insertCSS(value);
this.saveCSS(value);
2019-06-10 05:40:35 +02:00
}
2019-06-10 22:37:50 +02:00
loadCSS() {
2019-06-12 06:28:18 +02:00
this.savedCss = DataStore.loadCustomCSS();
2019-06-10 22:37:50 +02:00
}
insertCSS(newCss) {
2019-06-12 06:28:18 +02:00
if (typeof(newCss) === "undefined") newCss = this.insertedCss;
else this.insertedCss = newCss;
2019-06-10 22:37:50 +02:00
if ($("#customcss").length == 0) {
$("head").append("<style id=\"customcss\"></style>");
}
$("#customcss").text(newCss).detach().appendTo(document.head);
}
saveCSS(newCss) {
2019-06-12 06:28:18 +02:00
if (typeof(newCss) !== "undefined") this.savedCss = newCss;
DataStore.saveCustomCSS(this.savedCss);
2019-06-10 22:37:50 +02:00
}
openNative() {
electron.shell.openExternal(`file://${DataStore.customCSS}`);
}
2019-06-15 04:11:19 +02:00
openDetached(currentCSS) {
const editorRef = React.createRef();
const editor = React.createElement(CSSEditor, {
id: "bd-floating-editor",
editorRef: editorRef,
css: currentCSS,
save: this.saveCSS.bind(this),
update: this.insertCSS.bind(this),
openNative: this.openNative.bind(this),
onChange: this.onChange.bind(this)
});
FloatingWindowContainer.open({
onClose: () => {this.isDetached = false;},
onResize: () => {
if (!editorRef || !editorRef.current || !editorRef.current.resize) return;
editorRef.current.resize();
2019-06-11 22:24:57 +02:00
},
2019-06-15 04:11:19 +02:00
title: "Custom CSS Editor",
id: "floating-editor-window",
height: 470,
width: 410,
center: true,
resizable: true,
children: editor
2019-06-11 04:33:45 +02:00
});
2019-06-15 04:11:19 +02:00
this.isDetached = true;
UserSettings.close();
Dispatcher.dirtyDispatch({type: ActionTypes.LAYER_POP});
2019-06-10 22:37:50 +02:00
}
2019-06-15 04:11:19 +02:00
};