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
16 changes: 16 additions & 0 deletions src/ace/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,22 @@ const commands = [
},
readOnly: true,
},
{
name: "changeAppTheme",
description: "Change App Theme",
exec() {
acode.exec("change-app-theme");
},
readOnly: true,
},
{
name: "changeEditorTheme",
description: "Change Editor Theme",
exec() {
acode.exec("change-editor-theme");
},
readOnly: true,
},
];

export function setCommands(editor) {
Expand Down
47 changes: 35 additions & 12 deletions src/components/palette/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,18 @@ This shows that using keyboardHideStart event is faster than not using it.
* @param {()=>string} onsSelectCb Callback to call when a hint is selected
* @param {string} placeholder Placeholder for input
* @param {function} onremove Callback to call when palette is removed
* @param {function} onHighlight Callback to call when hint is highlighted
* @returns {void}
*/
export default function palette(getList, onsSelectCb, placeholder, onremove) {
export default function palette(
getList,
onsSelectCb,
placeholder,
onremove,
onHighlight,
) {
let isRemoving = false;

/**@type {HTMLInputElement} */
const $input = (
<input
Expand All @@ -63,16 +72,16 @@ export default function palette(getList, onsSelectCb, placeholder, onremove) {
const $palette = <div id="palette">{$input}</div>;

// Create a palette with input and hints
inputhints($input, generateHints, onSelect);
inputhints($input, generateHints, onSelect, onHighlight);

// Removes the darkened color from status bar and navigation bar
restoreTheme(true);

// Remove palette when input is blurred
$input.addEventListener("blur", remove);
$input.addEventListener("blur", handleBlur);
// Don't wait for input to blur when keyboard hides, remove is
// as soon as keyboard starts to hide
keyboardHandler.on("keyboardHideStart", remove);
keyboardHandler.on("keyboardHideStart", handleKeyboardHide);

// Add to DOM
app.append($palette, $mask);
Expand All @@ -91,8 +100,23 @@ export default function palette(getList, onsSelectCb, placeholder, onremove) {
* @param {string} value
*/
function onSelect(value) {
onsSelectCb(value);
isRemoving = true;
remove();
setTimeout(() => {
onsSelectCb(value);
}, 0);
}

function handleBlur() {
if (!isRemoving) {
remove();
}
}

function handleKeyboardHide() {
if (!isRemoving) {
remove();
}
}

/**
Expand All @@ -119,9 +143,12 @@ export default function palette(getList, onsSelectCb, placeholder, onremove) {
* Removes the palette
*/
function remove() {
if (isRemoving) return;
isRemoving = true;

actionStack.remove("palette");
keyboardHandler.off("keyboardHideStart", remove);
$input.removeEventListener("blur", remove);
keyboardHandler.off("keyboardHideStart", handleKeyboardHide);
$input.removeEventListener("blur", handleBlur);

restoreTheme();
$palette.remove();
Expand All @@ -133,12 +160,8 @@ export default function palette(getList, onsSelectCb, placeholder, onremove) {
}

const { activeFile, editor } = editorManager;
if (activeFile.wasFocused) {
if (activeFile?.wasFocused) {
editor.focus();
}

remove = () => {
window.log("warn", "Palette already removed.");
};
}
}
7 changes: 7 additions & 0 deletions src/lib/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import plugins from "pages/plugins";
import Problems from "pages/problems/problems";
import changeEncoding from "palettes/changeEncoding";
import changeMode from "palettes/changeMode";
import changeTheme from "palettes/changeTheme";
import commandPalette from "palettes/commandPalette";
import findFile from "palettes/findFile";
import browser from "plugins/browser";
Expand Down Expand Up @@ -281,6 +282,12 @@ export default {
syntax() {
changeMode();
},
"change-app-theme"() {
changeTheme("app");
},
"change-editor-theme"() {
changeTheme("editor");
},
"toggle-fullscreen"() {
app.classList.toggle("fullscreen-mode");
this["resize-editor"]();
Expand Down
81 changes: 81 additions & 0 deletions src/palettes/changeTheme/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import "./style.scss";
import palette from "components/palette";
import appSettings from "lib/settings";
import themes from "theme/list";

export default function changeTheme(type = "editor") {
palette(
() => generateHints(type),
(value) => onselect(value),
strings[type === "editor" ? "editor theme" : "app theme"],
undefined,
(value) => onselect(value),
);
}

function generateHints(type) {
if (type === "editor") {
const themeList = ace.require("ace/ext/themelist");
const currentTheme = appSettings.value.editorTheme;
const themePrefix = "ace/theme/";

return themeList.themes.map((theme) => {
const isCurrent =
theme.theme ===
(currentTheme.startsWith(themePrefix)
? currentTheme
: themePrefix + currentTheme);

return {
value: JSON.stringify({ type: "editor", theme: theme.theme }),
text: `<div class="theme-item">
<span>${theme.caption}</span>
${isCurrent ? '<span class="current">current</span>' : ""}
</div>`,
};
});
}

// App themes
const currentTheme = appSettings.value.appTheme;
const availableThemes = themes
.list()
.filter((theme) => !(theme.version === "paid" && IS_FREE_VERSION));

return availableThemes.map((theme) => {
const isCurrent = theme.id === currentTheme;

return {
value: JSON.stringify({
type: "app",
theme: theme.id,
}),
text: `<div class="theme-item">
<span>${theme.name}</span>
${isCurrent ? '<span class="current">current</span>' : ""}
</div>`,
};
});
}

function onselect(value) {
if (!value) return;

const selection = JSON.parse(value);

if (selection.type === "editor") {
editorManager.editor.setTheme(selection.theme);
appSettings.update(
{
editorTheme: selection.theme,
},
false,
);
} else {
if (selection.theme === "custom") {
CustomTheme();
return;
}
themes.apply(selection.theme, true);
}
}
20 changes: 20 additions & 0 deletions src/palettes/changeTheme/style.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
.theme-item {
display: flex;
align-items: center;
justify-content: space-between;
padding: 4px;
width: 100%;

span {
font-size: 1rem;
}

.current {
color: var(--error-text-color);
background-color: var(--primary-color);
border-radius: 5px;
padding: 2px 6px;
font-size: 0.8rem;
margin-left: 8px;
}
}