tona 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,116 @@
1
+ # Tona
2
+
3
+ API for creating cnblog theme.
4
+
5
+ ## Install
6
+
7
+ ```shell
8
+ npm i tona
9
+ ```
10
+
11
+ ## API
12
+
13
+ ### createTheme
14
+
15
+ Returns a theme instance that provides a theme context.
16
+
17
+ ```js
18
+ import { createTheme } from 'tona'
19
+
20
+ const theme = createTheme()
21
+ ```
22
+
23
+ ### defineOptions
24
+
25
+ Returns a generic configuration object.
26
+
27
+ ```js
28
+ import { defineOptions } from 'tona'
29
+
30
+ const getBackgroundOptions = defineOptions('bodyBackground', {
31
+ enable: false,
32
+ value: '',
33
+ opacity: 0.85,
34
+ repeat: false,
35
+ })
36
+ ```
37
+
38
+ Cnblog theme users can add the following configuration:
39
+
40
+ ```js
41
+ const opts = {
42
+ bodyBackground: {
43
+ enable: false,
44
+ value: '',
45
+ opacity: 0.85,
46
+ repeat: false,
47
+ },
48
+ }
49
+ ```
50
+
51
+ Using configuration aliases:
52
+
53
+ ```javascript
54
+ import { defineOptions } from 'tona'
55
+
56
+ const getBackgroundOptions = defineOptions(['bodyBackground', 'background'], {
57
+ enable: false,
58
+ value: '',
59
+ opacity: 0.85,
60
+ repeat: false,
61
+ })
62
+ ```
63
+
64
+ ## Plugin System
65
+
66
+ ```js
67
+ // plugin.js
68
+ import { defineOptions } from 'tona'
69
+
70
+ export function backgroundPlugin(theme, devOptions, pluginOptions) {
71
+ const getBackgroundOptions = defineOptions('bodyBackground', {
72
+ enable: false,
73
+ value: '',
74
+ opacity: 0.85,
75
+ repeat: false,
76
+ })
77
+
78
+ const { enable, value, opacity, repeat } = getBackgroundOptions(devOptions)
79
+
80
+ if (!enable)
81
+ return
82
+
83
+ const { opacitySelector } = Object.assign(
84
+ {},
85
+ {
86
+ opacitySelector: '#main,#navigator',
87
+ },
88
+ pluginOptions
89
+ )
90
+
91
+ setBackground(value, repeat)
92
+ setOpacity(opacity, opacitySelector)
93
+ }
94
+ ```
95
+
96
+ ```js
97
+ // theme/index.js
98
+ import { createTheme } from 'tona'
99
+ import { backgroundPlugin } from './plugin'
100
+
101
+ const theme = createTheme()
102
+
103
+ theme.use(
104
+ backgroundPlugin,
105
+ {
106
+ // Set the default configuration of the theme
107
+ enable: true,
108
+ value: '#ffb3cc',
109
+ opacity: 0.85,
110
+ },
111
+ {
112
+ // Set the default configuration of the plugin
113
+ opacitySelector: '#main',
114
+ }
115
+ )
116
+ ```
@@ -0,0 +1,30 @@
1
+ //#region src/createThemeApi.d.ts
2
+ interface Theme {
3
+ version: string;
4
+ _context: ThemeContext;
5
+ config: ThemeConfig;
6
+ use: (plugin: Plugin, ...options: any[]) => this;
7
+ }
8
+ interface ThemeConfig {
9
+ globalProperties: Record<string, any>;
10
+ }
11
+ interface ThemeContext {
12
+ theme: Theme;
13
+ config: ThemeConfig;
14
+ }
15
+ type PluginInstallFunction = (theme: Theme, ...options: any[]) => any;
16
+ type Plugin = (PluginInstallFunction & {
17
+ install?: PluginInstallFunction;
18
+ }) | {
19
+ install: PluginInstallFunction;
20
+ };
21
+ interface CreateThemeConfig {
22
+ log: boolean;
23
+ }
24
+ declare function createTheme(options?: CreateThemeConfig): Theme;
25
+ //#endregion
26
+ //#region src/defineOptionsApi.d.ts
27
+ type DefineOptionsFn = <F extends object, D extends object, U extends object>(userOptionName: string | Array<string>, defaultOptions: F) => (devOptions?: D) => F & U & D;
28
+ declare const defineOptions: DefineOptionsFn;
29
+ //#endregion
30
+ export { CreateThemeConfig, Plugin, PluginInstallFunction, Theme, ThemeConfig, ThemeContext, createTheme, defineOptions };
@@ -0,0 +1,30 @@
1
+ //#region src/createThemeApi.d.ts
2
+ interface Theme {
3
+ version: string;
4
+ _context: ThemeContext;
5
+ config: ThemeConfig;
6
+ use: (plugin: Plugin, ...options: any[]) => this;
7
+ }
8
+ interface ThemeConfig {
9
+ globalProperties: Record<string, any>;
10
+ }
11
+ interface ThemeContext {
12
+ theme: Theme;
13
+ config: ThemeConfig;
14
+ }
15
+ type PluginInstallFunction = (theme: Theme, ...options: any[]) => any;
16
+ type Plugin = (PluginInstallFunction & {
17
+ install?: PluginInstallFunction;
18
+ }) | {
19
+ install: PluginInstallFunction;
20
+ };
21
+ interface CreateThemeConfig {
22
+ log: boolean;
23
+ }
24
+ declare function createTheme(options?: CreateThemeConfig): Theme;
25
+ //#endregion
26
+ //#region src/defineOptionsApi.d.ts
27
+ type DefineOptionsFn = <F extends object, D extends object, U extends object>(userOptionName: string | Array<string>, defaultOptions: F) => (devOptions?: D) => F & U & D;
28
+ declare const defineOptions: DefineOptionsFn;
29
+ //#endregion
30
+ export { CreateThemeConfig, Plugin, PluginInstallFunction, Theme, ThemeConfig, ThemeContext, createTheme, defineOptions };
package/dist/index.js ADDED
@@ -0,0 +1,95 @@
1
+
2
+ //#region src/constants/env.ts
3
+ const __DEV__ = false;
4
+
5
+ //#endregion
6
+ //#region src/init.ts
7
+ function setDevOptions() {
8
+ if (__DEV__) window.opts = {};
9
+ }
10
+ function hideLoading() {
11
+ const loading = $("#loading,.loading");
12
+ if (loading.length) loading.fadeOut(300);
13
+ }
14
+ var init_default = (_) => {
15
+ setDevOptions();
16
+ hideLoading();
17
+ };
18
+
19
+ //#endregion
20
+ //#region src/utils/shared.ts
21
+ const extend = Object.assign;
22
+ const isArray = Array.isArray;
23
+ function isFunction(x) {
24
+ return typeof x === "function";
25
+ }
26
+
27
+ //#endregion
28
+ //#region src/createThemeApi.ts
29
+ function createThemeContext() {
30
+ return {
31
+ theme: null,
32
+ config: { globalProperties: {} }
33
+ };
34
+ }
35
+ function createThemeAPI() {
36
+ return function createTheme$1() {
37
+ const context = createThemeContext();
38
+ const installedPlugins = /* @__PURE__ */ new Set();
39
+ const themeObject = {
40
+ _context: context,
41
+ version: "3.0",
42
+ get config() {
43
+ return context.config;
44
+ },
45
+ set config(_) {
46
+ if (__DEV__) console.warn(`theme.config cannot be replaced. Modify individual options instead.`);
47
+ },
48
+ use(plugin, ...options) {
49
+ if (installedPlugins.has(plugin)) __DEV__ && console.warn(`Plugin has already been applied to target theme.`);
50
+ else if (plugin && isFunction(plugin.install)) {
51
+ installedPlugins.add(plugin);
52
+ plugin.install(themeObject, ...options);
53
+ } else if (isFunction(plugin)) {
54
+ installedPlugins.add(plugin);
55
+ plugin(themeObject, ...options);
56
+ } else if (__DEV__) console.warn("A plugin must either be a function or an object with an \"install\" function.");
57
+ return themeObject;
58
+ }
59
+ };
60
+ const theme = themeObject;
61
+ context.theme = themeObject;
62
+ return theme;
63
+ };
64
+ }
65
+ function baseCreateTheme(options) {
66
+ init_default(options);
67
+ return { createTheme: createThemeAPI() };
68
+ }
69
+ function createTheme(options) {
70
+ return baseCreateTheme(options).createTheme();
71
+ }
72
+
73
+ //#endregion
74
+ //#region src/defineOptionsApi.ts
75
+ const mergeOptions = (def = {}, user = {}, dev = {}) => {
76
+ return extend({}, extend({}, def, dev), user);
77
+ };
78
+ function getValue(valueKeys = [], obj = {}) {
79
+ const key = Object.keys(obj).find((key$1) => valueKeys.includes(key$1));
80
+ return key ? obj[key] : null;
81
+ }
82
+ function ensureUserOptions(userOptionName) {
83
+ const userConfig = window.opts || {};
84
+ if (typeof userOptionName === "string") return userConfig[userOptionName];
85
+ else if (isArray(userOptionName)) return getValue(userOptionName, userConfig);
86
+ }
87
+ const defineOptions = (userOptionName, defaultOptions) => {
88
+ return (devOptions) => {
89
+ return mergeOptions(defaultOptions, ensureUserOptions(userOptionName), devOptions);
90
+ };
91
+ };
92
+
93
+ //#endregion
94
+ exports.createTheme = createTheme;
95
+ exports.defineOptions = defineOptions;
package/dist/index.mjs ADDED
@@ -0,0 +1,93 @@
1
+ //#region src/constants/env.ts
2
+ const __DEV__ = false;
3
+
4
+ //#endregion
5
+ //#region src/init.ts
6
+ function setDevOptions() {
7
+ if (__DEV__) window.opts = {};
8
+ }
9
+ function hideLoading() {
10
+ const loading = $("#loading,.loading");
11
+ if (loading.length) loading.fadeOut(300);
12
+ }
13
+ var init_default = (_) => {
14
+ setDevOptions();
15
+ hideLoading();
16
+ };
17
+
18
+ //#endregion
19
+ //#region src/utils/shared.ts
20
+ const extend = Object.assign;
21
+ const isArray = Array.isArray;
22
+ function isFunction(x) {
23
+ return typeof x === "function";
24
+ }
25
+
26
+ //#endregion
27
+ //#region src/createThemeApi.ts
28
+ function createThemeContext() {
29
+ return {
30
+ theme: null,
31
+ config: { globalProperties: {} }
32
+ };
33
+ }
34
+ function createThemeAPI() {
35
+ return function createTheme$1() {
36
+ const context = createThemeContext();
37
+ const installedPlugins = /* @__PURE__ */ new Set();
38
+ const themeObject = {
39
+ _context: context,
40
+ version: "3.0",
41
+ get config() {
42
+ return context.config;
43
+ },
44
+ set config(_) {
45
+ if (__DEV__) console.warn(`theme.config cannot be replaced. Modify individual options instead.`);
46
+ },
47
+ use(plugin, ...options) {
48
+ if (installedPlugins.has(plugin)) __DEV__ && console.warn(`Plugin has already been applied to target theme.`);
49
+ else if (plugin && isFunction(plugin.install)) {
50
+ installedPlugins.add(plugin);
51
+ plugin.install(themeObject, ...options);
52
+ } else if (isFunction(plugin)) {
53
+ installedPlugins.add(plugin);
54
+ plugin(themeObject, ...options);
55
+ } else if (__DEV__) console.warn("A plugin must either be a function or an object with an \"install\" function.");
56
+ return themeObject;
57
+ }
58
+ };
59
+ const theme = themeObject;
60
+ context.theme = themeObject;
61
+ return theme;
62
+ };
63
+ }
64
+ function baseCreateTheme(options) {
65
+ init_default(options);
66
+ return { createTheme: createThemeAPI() };
67
+ }
68
+ function createTheme(options) {
69
+ return baseCreateTheme(options).createTheme();
70
+ }
71
+
72
+ //#endregion
73
+ //#region src/defineOptionsApi.ts
74
+ const mergeOptions = (def = {}, user = {}, dev = {}) => {
75
+ return extend({}, extend({}, def, dev), user);
76
+ };
77
+ function getValue(valueKeys = [], obj = {}) {
78
+ const key = Object.keys(obj).find((key$1) => valueKeys.includes(key$1));
79
+ return key ? obj[key] : null;
80
+ }
81
+ function ensureUserOptions(userOptionName) {
82
+ const userConfig = window.opts || {};
83
+ if (typeof userOptionName === "string") return userConfig[userOptionName];
84
+ else if (isArray(userOptionName)) return getValue(userOptionName, userConfig);
85
+ }
86
+ const defineOptions = (userOptionName, defaultOptions) => {
87
+ return (devOptions) => {
88
+ return mergeOptions(defaultOptions, ensureUserOptions(userOptionName), devOptions);
89
+ };
90
+ };
91
+
92
+ //#endregion
93
+ export { createTheme, defineOptions };
package/package.json ADDED
@@ -0,0 +1,48 @@
1
+ {
2
+ "name": "tona",
3
+ "version": "0.0.1",
4
+ "description": "",
5
+ "author": {
6
+ "name": "guangzan",
7
+ "url": "https://www.cnblogs.com/guangzan",
8
+ "email": "guangzan1999@outlook.com"
9
+ },
10
+ "license": "MIT",
11
+ "homepage": "https://github.com/acnblogs/core#readme",
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "git+https://github.com/acnblogs/core.git"
15
+ },
16
+ "bugs": {
17
+ "url": "https://github.com/acnblogs/core/issues"
18
+ },
19
+ "keywords": [
20
+ "博客园"
21
+ ],
22
+ "exports": {
23
+ ".": {
24
+ "types": "./dist/index.d.ts",
25
+ "import": "./dist/index.mjs",
26
+ "require": "./dist/index.js"
27
+ }
28
+ },
29
+ "main": "./dist/index.mjs",
30
+ "module": "./dist/index.js",
31
+ "types": "./dist/index.d.ts",
32
+ "files": [
33
+ "dist"
34
+ ],
35
+ "scripts": {
36
+ "dev": "tsdown --watch",
37
+ "build": "tsdown",
38
+ "test": "vitest",
39
+ "test:run": "vitest run"
40
+ },
41
+ "devDependencies": {
42
+ "@types/jquery": "catalog:",
43
+ "@vitest/ui": "catalog:",
44
+ "happy-dom": "catalog:",
45
+ "tsdown": "catalog:",
46
+ "vitest": "catalog:"
47
+ }
48
+ }