-
Notifications
You must be signed in to change notification settings - Fork 949
Expand file tree
/
Copy paththemeSetting.js
More file actions
264 lines (235 loc) · 6.08 KB
/
themeSetting.js
File metadata and controls
264 lines (235 loc) · 6.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
import "./themeSetting.scss";
import Page from "components/page";
import searchBar from "components/searchbar";
import TabView from "components/tabView";
import alert from "dialogs/alert";
import Ref from "html-tag-js/ref";
import actionStack from "lib/actionStack";
import removeAds from "lib/removeAds";
import appSettings from "lib/settings";
import CustomTheme from "pages/customTheme";
import ThemeBuilder from "theme/builder";
import themes from "theme/list";
import helpers from "utils/helpers";
export default function () {
const $page = Page(strings.theme.capitalize());
const $search = <span attr-action="search" className="icon search"></span>;
const $themePreview = <div id="theme-preview"></div>;
const list = new Ref();
const editor = ace.edit($themePreview);
const session = ace.createEditSession("");
const activeFile = editorManager.activeFile;
if (activeFile && activeFile.type === "editor") {
const currentSession = activeFile.session;
session.setMode(currentSession.getMode());
session.setValue(currentSession.getValue());
} else {
// Fallback content for preview
session.setMode("ace/mode/javascript");
session.setValue(`// Acode is awesome!
const message = "Welcome to Acode";
console.log(message);`);
}
editor.setReadOnly(true);
editor.setSession(session);
editor.renderer.setMargin(0, 0, -16, 0);
actionStack.push({
id: "appTheme",
action: () => {
editor.destroy();
$page.hide();
$page.removeEventListener("click", clickHandler);
},
});
$page.onhide = () => {
helpers.hideAd();
actionStack.remove("appTheme");
};
$page.body = (
<TabView id="theme-setting">
<div className="options">
<span className="active" onclick={renderAppThemes} tabindex={0}>
App
</span>
<span onclick={renderEditorThemes} tabindex={0}>
Editor
</span>
</div>
<div ref={list} id="theme-list" className="list scroll"></div>
</TabView>
);
$page.querySelector("header").append($search);
app.append($page);
renderAppThemes();
helpers.showAd();
$page.addEventListener("click", clickHandler);
function renderAppThemes() {
$themePreview.remove();
const content = [];
if (!DOES_SUPPORT_THEME) {
content.push(
<div className="list-item">
<span className="icon warningreport_problem"></span>
<div className="container">
<span className="text">{strings["unsupported device"]}</span>
</div>
</div>,
);
}
const currentTheme = appSettings.value.appTheme;
let $currentItem;
themes.list().forEach((theme) => {
const isCurrentTheme = theme.id === currentTheme;
const isPremium = theme.version === "paid" && IS_FREE_VERSION;
const $item = (
<Item
name={theme.name}
isPremium={isPremium}
isCurrent={isCurrentTheme}
color={theme.primaryColor}
onclick={() => setAppTheme(theme, isPremium)}
/>
);
content.push($item);
if (isCurrentTheme) $currentItem = $item;
});
list.el.content = content;
$currentItem?.scrollIntoView();
}
function renderEditorThemes() {
const currentTheme = appSettings.value.editorTheme;
const themePrefix = "ace/theme/";
const fullThemePath = currentTheme.startsWith(themePrefix)
? currentTheme
: themePrefix + currentTheme;
editor.setTheme(fullThemePath);
if (innerHeight * 0.3 >= 120) {
$page.body.append($themePreview);
editor.resize();
} else {
$themePreview.remove();
}
const themeList = ace.require("ace/ext/themelist");
let $currentItem;
list.el.content = themeList.themes.map((theme) => {
const isCurrent = theme.theme === fullThemePath;
const $item = (
<Item
name={theme.caption}
isCurrent={isCurrent}
isDark={theme.isDark}
onclick={() => setEditorTheme(theme)}
/>
);
if (isCurrent) $currentItem = $item;
return $item;
});
$currentItem?.scrollIntoView();
}
/**
*
* @param {MouseEvent} e
*/
function clickHandler(e) {
const $target = e.target;
if (!($target instanceof HTMLElement)) return;
const action = $target.getAttribute("action");
if (!action) return;
switch (action) {
case "search":
searchBar(list.el);
break;
default:
break;
}
}
/**
* Sets the selected theme
* @param {ThemeBuilder} theme
*/
async function setAppTheme(theme, buy) {
if (!DOES_SUPPORT_THEME) return;
if (buy) {
try {
await removeAds();
renderAppThemes();
} catch (e) {
return;
}
}
if (theme.id === "custom") {
CustomTheme();
return;
}
themes.apply(theme.id, true);
updateCheckedItem(theme.name);
}
/**
* Sets the selected editor theme
* @param {object} param0
* @param {string} param0.theme
*/
function setEditorTheme({ caption, theme }) {
if (appSettings.value.appTheme.toLowerCase() === "system") {
alert(
"Info",
"App theme is set to 'System'. Changing the editor theme will not affect the editor appearance.",
);
return;
}
editorManager.editor.setTheme(theme);
editor.setTheme(theme); // preview
appSettings.update(
{
editorTheme: theme,
},
false,
);
updateCheckedItem(caption);
}
/**
* Updates the checked item
* @param {string} theme
*/
function updateCheckedItem(theme) {
list.get('[checked="true"]')?.uncheck();
list.get(`[theme="${theme}"]`)?.check();
}
function Item({ name, color, isDark, onclick, isCurrent, isPremium }) {
const check = <span ref={check} className="icon check"></span>;
const star = <span ref={star} className="icon stars"></span>;
let style = {};
let className = "icon color";
if (color) {
style = { color };
} else if (isDark) {
className += " dark";
} else {
className += " light";
}
const $el = (
<div
attr-checked={isCurrent}
attr-theme={name}
className="list-item"
onclick={onclick}
>
<span style={style} className={className}></span>
<div className="container">
<span className="text">{name}</span>
</div>
{isCurrent ? check : <></>}
{isPremium ? star : <></>}
</div>
);
$el.uncheck = () => {
check.remove();
$el.removeAttribute("checked");
};
$el.check = () => {
$el.append(check);
$el.setAttribute("checked", true);
};
return $el;
}
}