Skip to content

Commit 2296394

Browse files
committed
Add a convenient way to check enabledMode
1 parent f661fe8 commit 2296394

8 files changed

Lines changed: 129 additions & 46 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Add `markedit-theming` to your (TypeScript) project's dependencies:
1111
```json
1212
{
1313
"dependencies": {
14-
"markedit-theming": "https://github.com/MarkEdit-app/MarkEdit-theming#v0.10.0"
14+
"markedit-theming": "https://github.com/MarkEdit-app/MarkEdit-theming#v0.11.0"
1515
}
1616
}
1717
```

dist/index.cjs

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,22 @@ const view = require("@codemirror/view");
77
const language = require("@codemirror/language");
88
const toObject = (value, fallback = {}) => value ?? fallback;
99
const userSettings = toObject(markeditApi.MarkEdit.userSettings);
10-
const rootValue = toObject(userSettings["extension.markeditTheming"]);
11-
const enabledMode = rootValue.enabledMode ?? "both";
12-
const lightColors = toObject(rootValue.lightTheme);
13-
const darkColors = toObject(rootValue.darkTheme);
14-
const isModeCustomized = (isDark) => ["both", isDark ? "dark" : "light"].includes(enabledMode);
10+
const rootValue = settingsForKey("extension.markeditTheming");
11+
const lightColors = isModeEnabled(false) ? toObject(rootValue.lightTheme) : void 0;
12+
const darkColors = isModeEnabled(true) ? toObject(rootValue.darkTheme) : void 0;
13+
function settingsForKey(key) {
14+
return key === void 0 ? {} : toObject(userSettings[key]);
15+
}
16+
function enabledMode(settings) {
17+
return settings.enabledMode ?? "both";
18+
}
19+
function isModeEnabled(isDark, mode = enabledMode(rootValue)) {
20+
return ["both", isDark ? "dark" : "light"].includes(mode);
21+
}
1522
function buildBlendedTheme(isDark, extension, colors) {
1623
const mergedColors = mergeColors({
1724
lhs: colors,
18-
rhs: isModeCustomized(isDark) ? isDark ? darkColors : lightColors : void 0
25+
rhs: isDark ? darkColors : lightColors
1926
});
2027
const custom = isDark ? createTheme(mergedColors, { dark: true }) : createTheme(mergedColors);
2128
return {
@@ -290,12 +297,14 @@ function parseCssRules(cssText2) {
290297
}
291298
return result;
292299
}
293-
function overrideThemes(themes) {
294-
if (themes.light !== void 0) {
295-
$context().customThemes.light = themes.light;
296-
}
297-
if (themes.dark !== void 0) {
298-
$context().customThemes.dark = themes.dark;
300+
function overrideThemes(config) {
301+
const key = config.options?.settingsKey;
302+
const mode = enabledMode(settingsForKey(key));
303+
if (config.light !== void 0 && isModeEnabled(false, mode)) {
304+
$context().customThemes.light = config.light;
305+
}
306+
if (config.dark !== void 0 && isModeEnabled(true, mode)) {
307+
$context().customThemes.dark = config.dark;
299308
}
300309
if (typeof markeditApi.MarkEdit.editorView === "object") {
301310
updateTheme(markeditApi.MarkEdit.editorView);

dist/index.d.ts

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,40 @@ import type { Colors } from './colors';
44
/**
55
* @public
66
*
7-
* Override themes in MarkEdit.
8-
*
9-
* It is recommended to call this as soon as your script runs.
7+
* Configuration for overriding themes.
108
*
11-
* @param themes Themes for light mode and/or dark mode, both are optional.
9+
* All properties are optional.
1210
*/
13-
export declare function overrideThemes(themes: {
11+
export interface Config {
12+
/**
13+
* Theme for the light mode.
14+
*/
1415
light?: CustomTheme;
16+
/**
17+
* Theme for the dark mode.
18+
*/
1519
dark?: CustomTheme;
16-
}): void;
20+
/**
21+
* Additional options to control the behavior.
22+
*/
23+
options?: {
24+
/**
25+
* The key of the extension settings in the [settings.json](https://github.com/MarkEdit-app/MarkEdit/wiki/Customization#advanced-settings) file.
26+
*
27+
* When specified, MarkEdit-theming automatically determines the available themes based on the value of `enabledMode`.
28+
*/
29+
settingsKey?: string;
30+
};
31+
}
32+
33+
/**
34+
* @public
35+
*
36+
* Override themes in MarkEdit.
37+
*
38+
* It is recommended to call this as soon as your script runs.
39+
*/
40+
export declare function overrideThemes(config: Config): void;
1741

1842
/**
1943
* @public

dist/index.js

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,22 @@ import { EditorView } from "@codemirror/view";
55
import { syntaxHighlighting, HighlightStyle } from "@codemirror/language";
66
const toObject = (value, fallback = {}) => value ?? fallback;
77
const userSettings = toObject(MarkEdit.userSettings);
8-
const rootValue = toObject(userSettings["extension.markeditTheming"]);
9-
const enabledMode = rootValue.enabledMode ?? "both";
10-
const lightColors = toObject(rootValue.lightTheme);
11-
const darkColors = toObject(rootValue.darkTheme);
12-
const isModeCustomized = (isDark) => ["both", isDark ? "dark" : "light"].includes(enabledMode);
8+
const rootValue = settingsForKey("extension.markeditTheming");
9+
const lightColors = isModeEnabled(false) ? toObject(rootValue.lightTheme) : void 0;
10+
const darkColors = isModeEnabled(true) ? toObject(rootValue.darkTheme) : void 0;
11+
function settingsForKey(key) {
12+
return key === void 0 ? {} : toObject(userSettings[key]);
13+
}
14+
function enabledMode(settings) {
15+
return settings.enabledMode ?? "both";
16+
}
17+
function isModeEnabled(isDark, mode = enabledMode(rootValue)) {
18+
return ["both", isDark ? "dark" : "light"].includes(mode);
19+
}
1320
function buildBlendedTheme(isDark, extension, colors) {
1421
const mergedColors = mergeColors({
1522
lhs: colors,
16-
rhs: isModeCustomized(isDark) ? isDark ? darkColors : lightColors : void 0
23+
rhs: isDark ? darkColors : lightColors
1724
});
1825
const custom = isDark ? createTheme(mergedColors, { dark: true }) : createTheme(mergedColors);
1926
return {
@@ -288,12 +295,14 @@ function parseCssRules(cssText2) {
288295
}
289296
return result;
290297
}
291-
function overrideThemes(themes) {
292-
if (themes.light !== void 0) {
293-
$context().customThemes.light = themes.light;
294-
}
295-
if (themes.dark !== void 0) {
296-
$context().customThemes.dark = themes.dark;
298+
function overrideThemes(config) {
299+
const key = config.options?.settingsKey;
300+
const mode = enabledMode(settingsForKey(key));
301+
if (config.light !== void 0 && isModeEnabled(false, mode)) {
302+
$context().customThemes.light = config.light;
303+
}
304+
if (config.dark !== void 0 && isModeEnabled(true, mode)) {
305+
$context().customThemes.dark = config.dark;
297306
}
298307
if (typeof MarkEdit.editorView === "object") {
299308
updateTheme(MarkEdit.editorView);

index.ts

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,59 @@ import { MarkEdit } from 'markedit-api';
44
import { buildBlendedTheme } from './src/builder';
55
import { selectors, cssText } from './src/const';
66
import { injectStyles, extractTheme, extractTaggedColor, findBackground, lighterColor, isEmptyObject } from './src/utils';
7+
import { settingsForKey, enabledMode, isModeEnabled } from './src/settings';
78

89
import type { EditorView } from '@codemirror/view';
910
import type { TagStyle } from '@codemirror/language';
1011
import type { Colors } from './colors';
1112
import type { OriginalRules } from './src/types';
1213

14+
/**
15+
* @public
16+
*
17+
* Configuration for overriding themes.
18+
*
19+
* All properties are optional.
20+
*/
21+
export interface Config {
22+
/**
23+
* Theme for the light mode.
24+
*/
25+
light?: CustomTheme;
26+
/**
27+
* Theme for the dark mode.
28+
*/
29+
dark?: CustomTheme;
30+
/**
31+
* Additional options to control the behavior.
32+
*/
33+
options?: {
34+
/**
35+
* The key of the extension settings in the [settings.json](https://github.com/MarkEdit-app/MarkEdit/wiki/Customization#advanced-settings) file.
36+
*
37+
* When specified, MarkEdit-theming automatically determines the available themes based on the value of `enabledMode`.
38+
*/
39+
settingsKey?: string;
40+
};
41+
}
42+
1343
/**
1444
* @public
1545
*
1646
* Override themes in MarkEdit.
1747
*
1848
* It is recommended to call this as soon as your script runs.
19-
*
20-
* @param themes Themes for light mode and/or dark mode, both are optional.
2149
*/
22-
export function overrideThemes(themes: { light?: CustomTheme; dark?: CustomTheme }) {
23-
if (themes.light !== undefined) {
24-
$context().customThemes.light = themes.light;
50+
export function overrideThemes(config: Config) {
51+
const key = config.options?.settingsKey;
52+
const mode = enabledMode(settingsForKey(key));
53+
54+
if (config.light !== undefined && isModeEnabled(false, mode)) {
55+
$context().customThemes.light = config.light;
2556
}
2657

27-
if (themes.dark !== undefined) {
28-
$context().customThemes.dark = themes.dark;
58+
if (config.dark !== undefined && isModeEnabled(true, mode)) {
59+
$context().customThemes.dark = config.dark;
2960
}
3061

3162
if (typeof MarkEdit.editorView === 'object') {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "markedit-theming",
3-
"version": "0.10.0",
3+
"version": "0.11.0",
44
"description": "Create custom MarkEdit themes easily.",
55
"scripts": {
66
"lint": "eslint . --config eslint.config.mjs",

src/builder.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { EditorView } from '@codemirror/view';
22
import { HighlightStyle, syntaxHighlighting, type TagStyle } from '@codemirror/language';
33
import { tags } from '@lezer/highlight';
4-
import { lightColors, darkColors, isModeCustomized } from './settings';
4+
import { lightColors, darkColors } from './settings';
55

66
import type { Extension } from '@codemirror/state';
77
import type { StyleSpec } from 'style-mod';
@@ -11,7 +11,7 @@ export function buildBlendedTheme(isDark: boolean, extension?: Extension, colors
1111
// In a JS object, values that are spread later have higher priority
1212
const mergedColors = mergeColors({
1313
lhs: colors,
14-
rhs: isModeCustomized(isDark) ? (isDark ? darkColors : lightColors) : undefined,
14+
rhs: isDark ? darkColors : lightColors,
1515
});
1616

1717
const custom = isDark ? createTheme(mergedColors, { dark: true }) : createTheme(mergedColors);

src/settings.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,19 @@ import type { Colors } from '../colors';
33

44
const toObject = (value: JSONValue, fallback = {}) => (value ?? fallback) as JSONObject;
55
const userSettings = toObject(MarkEdit.userSettings);
6-
const rootValue = toObject(userSettings['extension.markeditTheming']);
7-
const enabledMode = (rootValue.enabledMode ?? 'both') as string;
6+
const rootValue = settingsForKey('extension.markeditTheming');
87

9-
export const lightColors = toObject(rootValue.lightTheme) as Colors | undefined;
10-
export const darkColors = toObject(rootValue.darkTheme) as Colors | undefined;
11-
export const isModeCustomized = (isDark: boolean) => (['both', isDark ? 'dark' : 'light']).includes(enabledMode);
8+
export const lightColors = isModeEnabled(false) ? (toObject(rootValue.lightTheme) as Colors) : undefined;
9+
export const darkColors = isModeEnabled(true) ? (toObject(rootValue.darkTheme) as Colors) : undefined;
10+
11+
export function settingsForKey(key?: string) {
12+
return key === undefined ? {} : toObject(userSettings[key]);
13+
}
14+
15+
export function enabledMode(settings: JSONObject) {
16+
return (settings.enabledMode ?? 'both') as string;
17+
}
18+
19+
export function isModeEnabled(isDark: boolean, mode: string = enabledMode(rootValue)) {
20+
return ['both', isDark ? 'dark' : 'light'].includes(mode);
21+
}

0 commit comments

Comments
 (0)