Skip to content

Commit

Permalink
Static Editor Methods
Browse files Browse the repository at this point in the history
Now you can call the Open, Close, and Rename methods for an Editor based on it's identifier, meaning you don't need a specific reference to it to access it! This is the initial last part of moving the Editor functions into a single component, which encapulates all of the state for the "Editor thing".

This is my first commit from my new machine! It's awesome!!! Already done so much on here, it's super sweet. Have Homebrew all set up, my music library (listening to The Drapery Falls as I type this 'time - 9:59'), and everything brought over from my iMac using Time Machine and Migration Assistant.

I'm so glad I did that instead of moving everything manually, and adding my own settings again. There's so much I forgot that vanilla macOS does different than to my custom config, that would've been even more of a headache to get set up than it already was. It's mostly just overwhelming making sure that you definitely have everything copied over, and ready to go just the way you like it.

Got the Bedrock-LevelDB project, ffmpeg, and Baobab all running on here! Baobab was really suprising, was curious if that was available outside of Linux too, super awesome. GTK looks great on this screen resolution.

Ooooh yeah!!! Gonna have to install UTM, and likely eventually try out Asahi Linux too. Super stoked for that. Minecraft runs great on here too! That's a bonus.

Alright! Noice :)
  • Loading branch information
Offroaders123 committed Feb 25, 2023
1 parent 7b88518 commit e57c949
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 10 deletions.
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@
<li onclick="new Editor({ autoReplace: false });" data-shortcuts='{ "default": "Ctrl+Shift+X", "macOS": "Shift+Cmd+X" }'>New Editor</li>
<li part="create-window-option" onclick="createWindow();" data-shortcuts='{ "default": "Ctrl+Shift+C", "macOS": "Shift+Cmd+C" }'>New Window</li>
<li onclick="openFiles();" data-shortcuts='{ "default": "Ctrl+O", "macOS": "Cmd+O" }'>Open</li>
<li onclick="window.setTimeout(renameEditor,10);" data-shortcuts='{ "default": "Ctrl+Shift+R", "macOS": "Shift+Cmd+R" }'>Rename</li>
<li onclick="window.setTimeout(Editor.rename,10);" data-shortcuts='{ "default": "Ctrl+Shift+R", "macOS": "Shift+Cmd+R" }'>Rename</li>
<li onclick="saveFile();" data-shortcuts='{ "default": "Ctrl+S", "macOS": "Cmd+S" }'>Save</li>
<li>Save As...
<ul>
Expand Down
61 changes: 57 additions & 4 deletions scripts/Editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,51 @@ import { setPreviewSource, refreshPreview } from "./Workspace.js";
* Creates a new Editor within the Workspace.
*/
export class Editor {
/**
* @type { { [identifier: string]: Editor; } }
*/
static #editors = {};

/**
* Opens an Editor from a given identifier.
*
* @param { string } identifier
* @param { EditorOpenOptions } options
*/
static open(identifier,options = {}) {
const editor = this.#editors[identifier];
editor.open(options);
}

/**
* Closes an Editor from a given identifier.
*
* @param { string } identifier
*/
static async close(identifier) {
const editor = this.#editors[identifier];
await editor.close();
}

/**
* Renames an Editor from a given identifier.
*
* @param { string } identifier
* @param { string } [rename] - If a new name isn't provided, the user is prompted to provide one.
*/
static rename(identifier,rename) {
const editor = this.#editors[identifier];
const currentName = editor.#name;

if (!rename){
const result = prompt(`Enter a new file name for "${currentName}".`,currentName);
if (result === null) return;
rename = result;
}

editor.name = rename;
}

#name;

/**
Expand Down Expand Up @@ -196,6 +241,8 @@ export class Editor {
workspace_editors.append(this.container);
preview_menu.main.append(this.previewOption);

Editor.#editors[this.identifier] = this;

applyEditingBehavior({ element: this.container });

this.previewOption.addEventListener("click",() => {
Expand Down Expand Up @@ -244,6 +291,8 @@ export class Editor {
}

/**
* Opens the editor in the workspace.
*
* @param { EditorOpenOptions } options
*/
open({ autoCreated = false, focusedOverride = false } = {}) {
Expand Down Expand Up @@ -275,6 +324,9 @@ export class Editor {
}
}

/**
* Closes the editor in the workspace
*/
async close() {
if (this.tab.hasAttribute("data-editor-unsaved")){
if (!confirm(`Are you sure you would like to close "${this.#name}"?\nRecent changes have not yet been saved.`)) return;
Expand Down Expand Up @@ -305,15 +357,15 @@ export class Editor {

if (this.tab === editorTabs[0] && editorTabs[1] && this.tab.classList.contains("active")){
const identifier = /** @type { string } */ (editorTabs[1].getAttribute("data-editor-identifier"));
openEditor({ identifier });
Editor.open(identifier);
}
if (this.tab === editorTabs[editorTabs.length - 1] && this.tab !== editorTabs[0] && this.tab.classList.contains("active")){
const identifier = /** @type { string } */ (editorTabs[editorTabs.length - 2].getAttribute("data-editor-identifier"));
openEditor({ identifier });
Editor.open(identifier);
}
if (this.tab !== editorTabs[0] && this.tab.classList.contains("active")){
const identifier = /** @type { string } */ (editorTabs[editorTabs.indexOf(this.tab) + 1].getAttribute("data-editor-identifier"));
openEditor({ identifier });
Editor.open(identifier);
}

if (focused && STE.query().textarea !== null){
Expand All @@ -330,6 +382,8 @@ export class Editor {
workspace_editors.removeChild(this.container);
preview_menu.main.removeChild(this.previewOption);

delete Editor.#editors[this.identifier];

if (STE.fileHandles[this.identifier]){
delete STE.fileHandles[this.identifier];
}
Expand All @@ -351,7 +405,6 @@ export class Editor {

set name(rename) {
const { getName } = STE.query(this.identifier);
const currentName = this.#name;
const base = getName("base");
const extension = getName("extension");

Expand Down
2 changes: 1 addition & 1 deletion scripts/Workspace.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ export async function saveFile(extension){
await stream.write(STE.query().textarea?.value ?? "");
await stream.close();
var currentName = STE.query().getName(), file = await handle.getFile(), rename = file.name;
if (currentName != rename) renameEditor({ name: rename });
if (currentName != rename) Editor.rename(identifier,rename);
}
if (STE.query().tab.hasAttribute("data-editor-auto-created")) STE.query().tab.removeAttribute("data-editor-auto-created");
if (STE.query().tab.hasAttribute("data-editor-unsaved")) STE.query().tab.removeAttribute("data-editor-unsaved");
Expand Down
12 changes: 8 additions & 4 deletions scripts/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,20 +106,22 @@ document.body.addEventListener("keydown",event => {
event.preventDefault();
if (event.repeat) return;
/* Future feature: If an editor tab is focused, close that editor instead of only the active editor */
closeEditor();
if (STE.activeEditor !== null){
Editor.close(STE.activeEditor);
}
}
if (((controlShift || (event.ctrlKey && shift && !command && STE.environment.appleDevice)) && pressed("Tab")) || ((controlShift || controlCommand) && (pressed("[") || pressed("{")))){
event.preventDefault();
if (event.repeat) return;
// For both of these expected errors, I need to add handling for when there aren't any Editors opened, since it will throw an error trying to open the next editor, when there isn't one there. There's not even one to start from in that case, either!
// @ts-expect-error
openEditor({ identifier: getPreviousEditor() });
Editor.open(getPreviousEditor);
}
if (((control || (event.ctrlKey && !command && STE.environment.appleDevice)) && !shift && pressed("Tab")) || ((controlShift || controlCommand) && (pressed("]") || pressed("}")))){
event.preventDefault();
if (event.repeat) return;
// @ts-expect-error
openEditor({ identifier: getNextEditor() });
Editor.open(getNextEditor);
}
if (((controlShift || shiftCommand) && pressed("n")) || ((controlShift || shiftCommand) && pressed("c"))){
event.preventDefault();
Expand All @@ -134,7 +136,9 @@ document.body.addEventListener("keydown",event => {
if ((controlShift || shiftCommand) && pressed("r")){
event.preventDefault();
if (event.repeat) return;
renameEditor();
if (STE.activeEditor !== null){
Editor.rename(STE.activeEditor);
}
}
if ((control || command) && !shift && pressed("s")){
event.preventDefault();
Expand Down

0 comments on commit e57c949

Please sign in to comment.