Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions src/lib/acode.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ class Acode {
// Registered formatter implementations (populated by plugins)
#formatter = [];
#pluginWatchers = {};
/** @type {Map<string, {order: number, $el: HTMLElement}>} */
#headerButtons = new Map();
/** Set by main.js; called whenever the header button list changes. */
onHeaderButtonsChange = null;

/**
* Clear a plugin's broken mark (so it can be retried)
Expand Down Expand Up @@ -438,6 +442,10 @@ class Acode {
this.define("themeBuilder", ThemeBuilder);
this.define("selectionMenu", selectionMenu);
this.define("sidebarApps", sidebarAppsModule);
this.define("headerButtons", {
add: (options) => this.addHeaderButton(options),
remove: (id) => this.removeHeaderButton(id),
});
this.define("terminal", terminalModule);
this.define("webview", webview);
this.define("orientation", orientation);
Expand Down Expand Up @@ -933,6 +941,55 @@ class Acode {
return Url.join(...args);
}

/**
* Add a button to the main editor header, next to the edit/run buttons.
* Register an icon first with `acode.addIcon` and pass its class name.
* @param {object} options
* @param {string} options.id Unique id (re-adding the same id replaces it)
* @param {string} [options.icon] Icon class name (`.icon.<icon>`)
* @param {string} [options.label] Accessible label
* @param {() => void} options.onclick Click handler
* @param {number} [options.order=0] Lower values appear further left
* @returns {() => void} Function that removes the button
*/
addHeaderButton({ id, icon, label, onclick, order = 0 } = {}) {
if (!id || typeof onclick !== "function") {
throw new Error("addHeaderButton requires an `id` and an `onclick`.");
}
this.removeHeaderButton(id);
const $el = (
<span
className={`icon ${icon || "extension"}`}
data-header-button={id}
aria-label={label || id}
style={{ fontSize: "1.2em" }}
onclick={onclick}
/>
);
this.#headerButtons.set(id, { order, $el });
this.onHeaderButtonsChange?.();
return () => this.removeHeaderButton(id);
}

/**
* Remove a header button added with `addHeaderButton`
* @param {string} id
*/
removeHeaderButton(id) {
const entry = this.#headerButtons.get(id);
if (!entry) return;
entry.$el.remove();
this.#headerButtons.delete(id);
this.onHeaderButtonsChange?.();
}

/** Header button elements, sorted by `order`. */
get headerButtons() {
return [...this.#headerButtons.values()]
.sort((a, b) => a.order - b.order)
.map(({ $el }) => $el);
}

/**
* Adds a custom icon class that can be used with the .icon element
* @param {string} className - The class name for the icon (used as .icon.className)
Expand Down
14 changes: 14 additions & 0 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,20 @@ async function loadApp() {
};

acode.$headerToggler = $headerToggler;

// Plugin header buttons sit just left of the edit/run buttons. Those are
// re-inserted before the menu button on file switch, so they land after.
const placeHeaderButtons = () => {
const $anchor =
[$editMenuToggler, $runBtn].find(($el) => $el.isConnected) ||
$header.lastChild;
for (const $btn of acode.headerButtons) {
if ($btn.isConnected) continue;
$header.insertBefore($btn, $anchor);
}
};
acode.onHeaderButtonsChange = placeHeaderButtons;
placeHeaderButtons();
window.actionStack = actionStack.windowCopy();
window.editorManager = editorManager;
setMainMenu(settings.value.openFileListPos);
Expand Down