Skip to content

Commit

Permalink
STE Object
Browse files Browse the repository at this point in the history
Renamed the Editor object to instead be the STE object. This will free up the Editor name to be available so I can use it to represent file editors in the workspace! Using STE also makes more sense to me for the name of the global app object, rather than Editor.

I also changed the formatting for the object properties from snake_case to camelCase. I don't really use snake_case anywhere else in my code (STE, or other projects), so I thought it would be nicer to make it more consistent with how I have usually been doing things. I think I probably got this from Minecraft's implementation, not totally sure. It's not too common to see in Web Dev.
  • Loading branch information
Offroaders123 committed Jan 15, 2023
1 parent d22a3a0 commit 70a8a28
Show file tree
Hide file tree
Showing 3 changed files with 212 additions and 212 deletions.
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.23.0/prism.min.js" data-manual></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.23.0/components/prism-json.min.js"></script>

<script src="./scripts/Editor.js"></script>
<script src="./scripts/STE.js"></script>

<link rel="stylesheet" href="./styles/styles.css">

Expand Down
126 changes: 63 additions & 63 deletions scripts/Editor.js → scripts/STE.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
class Editor {
class STE {
static appearance = {
get parent_window() {
get parentWindow() {
return (window.self == window.top);
},

get standalone() {
return (window.matchMedia("(display-mode: standalone)").matches || navigator.standalone || !window.menubar.visible);
},

get apple_home_screen() {
get appleHomeScreen() {
// @ts-expect-error
return (/(macOS|Mac|iPhone|iPad|iPod)/i.test(("userAgentData" in navigator) ? navigator.userAgentData.platform : navigator.platform) && "standalone" in navigator && navigator.standalone);
},

get hidden_chrome() {
get hiddenChrome() {
return (window.outerWidth == window.innerWidth && window.outerHeight == window.innerHeight);
},

get window_controls_overlay() {
return ((("windowControlsOverlay" in navigator) ? navigator.windowControlsOverlay.visible : false) && Editor.appearance.standalone);
get windowControlsOverlay() {
return ((("windowControlsOverlay" in navigator) ? navigator.windowControlsOverlay.visible : false) && STE.appearance.standalone);
},

refresh_window_controls_overlay() {
var visibility = Editor.appearance.window_controls_overlay, styling = document.documentElement.classList.contains("window-controls-overlay");
refreshWindowControlsOverlay() {
var visibility = STE.appearance.windowControlsOverlay, styling = document.documentElement.classList.contains("window-controls-overlay");
if (visibility != styling) (visibility) ? document.documentElement.classList.add("window-controls-overlay") : document.documentElement.classList.remove("window-controls-overlay");
},

get fullscreen() {
return (window.matchMedia("(display-mode: fullscreen)").matches || (!window.screenY && !window.screenTop && Editor.appearance.hidden_chrome) || (!window.screenY && !window.screenTop && Editor.appearance.standalone));
return (window.matchMedia("(display-mode: fullscreen)").matches || (!window.screenY && !window.screenTop && STE.appearance.hiddenChrome) || (!window.screenY && !window.screenTop && STE.appearance.standalone));
},

get safe_area_insets() {
get safeAreaInsets() {
return {
left: this._getSafeAreaInset("left"),
right: this._getSafeAreaInset("right"),
Expand All @@ -39,7 +39,7 @@ class Editor {
};
},

get titlebar_area_insets() {
get titleBarAreaInsets() {
return {
top: this._getTitlebarAreaInset("top"),
width_left: this._getTitlebarAreaInset("width-left"),
Expand All @@ -48,12 +48,12 @@ class Editor {
};
},

get device_pixel_ratio() {
get devicePixelRatio() {
return window.devicePixelRatio.toFixed(2);
},

refresh_device_pixel_ratio() {
var ratio = Editor.appearance.device_pixel_ratio, styling = this._getRootStyleProperty("--device-pixel-ratio");
refreshDevicePixelRatio() {
var ratio = STE.appearance.devicePixelRatio, styling = this._getRootStyleProperty("--device-pixel-ratio");
if (ratio != styling) document.documentElement.style.setProperty("--device-pixel-ratio",ratio);
},

Expand Down Expand Up @@ -82,59 +82,59 @@ class Editor {
}

static environment = {
get file_protocol() {
get fileProtocol() {
return (window.location.protocol == "file:");
},

get touch_device() {
get touchDevice() {
return ("ontouchstart" in window);
},

get apple_device() {
get appleDevice() {
// @ts-expect-error
return (/(macOS|Mac|iPhone|iPad|iPod)/i.test(("userAgentData" in navigator) ? navigator.userAgentData.platform : navigator.platform));
},

get macOS_device() {
get macOSDevice() {
// @ts-expect-error
return (/(macOS|Mac)/i.test(("userAgentData" in navigator) ? navigator.userAgentData.platform : navigator.platform) && navigator.standalone == undefined);
},

get mozilla_browser() {
get mozillaBrowser() {
return (CSS.supports("-moz-appearance: none"));
}
}

static support = {
get local_storage() {
get localStorage() {
return (window.location.protocol != "blob:") ? window.localStorage : null;
},

get file_system() {
get fileSystem() {
return ("showOpenFilePicker" in window);
},

get file_handling() {
get fileHandling() {
return ("launchQueue" in window && "LaunchParams" in window);
},

get window_controls_overlay() {
get windowControlsOverlay() {
return ("windowControlsOverlay" in navigator);
},

get editing_commands() {
return (!Editor.environment.mozilla_browser);
get editingCommands() {
return (!STE.environment.mozillaBrowser);
},

get web_sharing() {
get webSharing() {
return ("share" in navigator);
}
}

/**
* @param { string | null } identifier
*/
static query(identifier = Editor.active_editor) {
static query(identifier = STE.activeEditor) {
const tab = /** @type { HTMLButtonElement } */ (workspace_tabs.querySelector(`.tab[data-editor-identifier="${identifier}"]`));
const container = /** @type { NumTextElement } */ (workspace_editors.querySelector(`.editor[data-editor-identifier="${identifier}"]`));
const textarea = (container) ? container.editor : null;
Expand All @@ -143,7 +143,7 @@ class Editor {
* @param { "base" | "extension" } [section]
*/
function getName(section){
if ((document.querySelectorAll(`[data-editor-identifier="${identifier}"]:not([data-editor-change])`).length == 0) && (identifier != Editor.active_editor)) return null;
if ((document.querySelectorAll(`[data-editor-identifier="${identifier}"]:not([data-editor-change])`).length == 0) && (identifier != STE.activeEditor)) return null;
/** @type { string | string[] } */
let name = /** @type { HTMLSpanElement } */ (workspace_tabs.querySelector(`.tab[data-editor-identifier="${identifier}"] [data-editor-name]`)).innerText;
if (!section || (!name.includes(".") && section == "base")) return name;
Expand All @@ -167,40 +167,40 @@ class Editor {
return document.body.getAttribute("data-view") ?? "code";
}

static get view_change() {
static get viewChange() {
return (document.body.hasAttribute("data-view-change"));
}

static get orientation() {
return document.body.getAttribute("data-orientation") ?? "horizontal";
}

static get orientation_change() {
static get orientationChange() {
return (document.body.hasAttribute("data-orientation-change"));
}

static get scaling_change() {
static get scalingChange() {
return (document.body.hasAttribute("data-scaling-change"));
}

static get unsaved_work() {
return (!Editor.appearance.parent_window || (workspace_tabs.querySelectorAll(".tab:not([data-editor-change])[data-editor-unsaved]").length == 0));
static get unsavedWork() {
return (!STE.appearance.parentWindow || (workspace_tabs.querySelectorAll(".tab:not([data-editor-change])[data-editor-unsaved]").length == 0));
}

static preapproved_extensions = ["txt","html","css","js","php","json","webmanifest","bbmodel","xml","yaml","yml","dist","config","ini","md","markdown","mcmeta","lang","properties","uidx","material","h","fragment","vertex","fxh","hlsl","ihlsl","svg"];
static preapprovedExtensions = ["txt","html","css","js","php","json","webmanifest","bbmodel","xml","yaml","yml","dist","config","ini","md","markdown","mcmeta","lang","properties","uidx","material","h","fragment","vertex","fxh","hlsl","ihlsl","svg"];
/**
* @type { string | null }
*/
static active_editor = null;
static preview_editor = "active-editor";
static activeEditor = null;
static previewEditor = "active-editor";
/**
* @type { { [identifier: string]: FileSystemFileHandle } }
*/
static file_handles = {};
static fileHandles = {};
/**
* @type { Window[] }
*/
static child_windows = [];
static childWindows = [];

static settings = {
entries: JSON.parse(window.localStorage.getItem("settings") ?? "null") || {},
Expand All @@ -210,40 +210,40 @@ class Editor {
* @param { string } value
*/
set(key,value) {
if (!Editor.support.local_storage) return;
Editor.settings.entries[key] = value;
window.localStorage.setItem("settings",JSON.stringify(Editor.settings.entries,null," "));
if (!STE.support.localStorage) return;
STE.settings.entries[key] = value;
window.localStorage.setItem("settings",JSON.stringify(STE.settings.entries,null," "));
return value;
},

/**
* @param { string } key
*/
remove(key) {
if (!Editor.support.local_storage) return;
delete Editor.settings.entries[key];
window.localStorage.setItem("settings",JSON.stringify(Editor.settings.entries,null," "));
if (!STE.support.localStorage) return;
delete STE.settings.entries[key];
window.localStorage.setItem("settings",JSON.stringify(STE.settings.entries,null," "));
return true;
},

/**
* @param { string } key
*/
has(key) {
return (key in Editor.settings.entries);
return (key in STE.settings.entries);
},

/**
* @param { string } key
*/
get(key) {
if (!Editor.support.local_storage) return;
if (!Editor.settings.has(key)) return;
return Editor.settings.entries[key];
if (!STE.support.localStorage) return;
if (!STE.settings.has(key)) return;
return STE.settings.entries[key];
},

reset({ confirm: showPrompt = false } = {}) {
if (!Editor.support.local_storage) return false;
if (!STE.support.localStorage) return false;
if (showPrompt){
if (!confirm("Are you sure you would like to reset all settings?")) return false;
}
Expand All @@ -252,7 +252,7 @@ class Editor {
syntax_highlighting_setting.checked = false;
automatic_refresh_setting.checked = true;
preview_base_input.reset();
Editor.settings.entries = {};
STE.settings.entries = {};
window.localStorage.removeItem("settings");
if (showPrompt) reset_settings_card.open();
return true;
Expand All @@ -262,28 +262,28 @@ class Editor {
/**
* @type { STECardElement | null }
*/
static active_dialog = null;
static activeDialog = null;
/**
* @type { STECardElement | null }
*/
static dialog_previous = null;
static dialogPrevious = null;
/**
* @type { STECardElement | null }
*/
static active_widget = null;
static picker_color = null;
static activeWidget = null;
static pickerColor = null;
/**
* @type { BeforeInstallPromptEvent | null }
*/
static install_prompt = null;
static installPrompt = null;
}

if (Editor.appearance.parent_window) document.documentElement.classList.add("startup-fade");
if (Editor.appearance.apple_home_screen) document.documentElement.classList.add("apple-home-screen");
if (Editor.environment.touch_device) document.documentElement.classList.add("touch-device");
if (Editor.environment.apple_device) document.documentElement.classList.add("apple-device");
if (Editor.environment.macOS_device) document.documentElement.classList.add("macOS-device");
if (Editor.support.web_sharing) document.documentElement.classList.add("web-sharing");
if (STE.appearance.parentWindow) document.documentElement.classList.add("startup-fade");
if (STE.appearance.appleHomeScreen) document.documentElement.classList.add("apple-home-screen");
if (STE.environment.touchDevice) document.documentElement.classList.add("touch-device");
if (STE.environment.appleDevice) document.documentElement.classList.add("apple-device");
if (STE.environment.macOSDevice) document.documentElement.classList.add("macOS-device");
if (STE.support.webSharing) document.documentElement.classList.add("web-sharing");

Editor.appearance.refresh_window_controls_overlay();
Editor.appearance.refresh_device_pixel_ratio();
STE.appearance.refreshWindowControlsOverlay();
STE.appearance.refreshDevicePixelRatio();
Loading

0 comments on commit 70a8a28

Please sign in to comment.