docs: add tutorial for create theme plugin and init environment

pull/90/head
moonrailgun 2 years ago
parent 1d0427b71f
commit ab2ed82d3f

@ -0,0 +1,4 @@
{
"label": "Guide",
"position": 5
}

@ -0,0 +1,213 @@
---
sidebar_position: 1
title: Create Theme Plugin
---
It is very convenient to develop plugin in `Tailchat`. We start from developing a theme plugin
## Final Effect
![](/img/tutorial/plugin/2.png)
![](/img/tutorial/plugin/3.png)
![](/img/tutorial/plugin/4.png)
## Create Plugin
> If it is the first development, it is necessary to ensure that the plugin development environment has been initialized. [Learn more](./init-env.md)
```bash
tailchat create --template client-plugin
```
Since the theme style is a pure front-end implementation, we choose the `client-plugin` template
Complete creation via interactive command line prompts
![](/img/tutorial/plugin/5.png)
At this point the directory structure should look like this:
```
.
├── package-lock.json
├── package.json
├── node_modules
└── plugins
└── com.test.hutao
├── manifest.json
├── package.json
├── src
│   ├── index.tsx
│   └── translate.ts
├── tsconfig.json
└── types
└── tailchat.d.ts
```
At this time we can start to compile the plugin immediately
```bash
npm run plugins:all
```
The compiled product will appear in `/dist/plugins/com.test.hutao`. By modifying the configuration of the `mini-star", it can allow it to output in other directory *(this will be used in the service side development plugin)*.
## Install plugin in Tailchat
For the frontend plugin, we need to provide the product with an HTTP static service, such as in local development, we can do this:
```bash
npx http-server .
```
![](/img/tutorial/plugin/6.png)
At this time we can access the results of our compilation through the address, such as [http://127.0.0.1:8080/dist/plugins/com.test.hutao/index.js](http://127.0.0.1:8080/dist/plugins/com.test.hutao/index.js)
A manual installation plugin is provided in Tailchat. We can install the plugin by manual install.
Copy the contents of `plugins/com.test.hutao/manifest.json` in the plugin directory, paste to the manual install plugin part.
Modify the URL as the real address that can be accessed. After clicking the Submit button, the installation command is automatically executed:
![](/img/tutorial/plugin/7.png)
If the installation is prompted successfully, you can see the corresponding output when opening the console:
![](/img/tutorial/plugin/8.png)
Here is the logic generated by the default in the plugin. The prompts that the plugin after the plugin is loaded, means that our plugin has been successfully installed.
## Write style files and apply
Because we need to modify the theme style, we involve the processing of style files and static resources. Next, let's create the style of our theme first
Write in `plugins/com.test.hutao/src/theme.less`:
```less
#tailchat-app.theme-genshin-hutao {
@primary-color: #dd5545;
--tc-primary-color: @primary-color;
--tc-background-image: url(./bg.jpg);
--tc-content-background-image: url(./avatar.png);
--tc-content-background-image-opacity: 0.15;
.bg-navbar-light {
background-color: @primary-color;
.bg-gray-400 {
background-color: darken(@primary-color, 10%);
}
}
.bg-sidebar-light {
background-color: lighten(@primary-color, 20%);
}
.bg-content-light {
background-color: lighten(@primary-color, 40%);
}
&.dark {
--tc-primary-color: darken(@primary-color, 10%);
.dark\:bg-navbar-dark {
background-color: darken(@primary-color, 40%);
}
.dark\:bg-sidebar-dark {
background-color: darken(@primary-color, 20%);
}
.dark\:bg-content-dark {
background-color: @primary-color;
}
}
}
```
This style file has done three things:
- In the method of specifying the main color by the method of the `less variable`, which greatly reduces the redundant code
- In the background map of the `Tailchat` by `css variable`, mainly the background map and avatar of the login page
- Stop at the root node `#tailchat-app.theme-genshin-hutao` selector to ensure that it will not pollute the global style.
**Correspondingly, you need to put the `bg.jpg` and` avatar.png` as a static resource at the root node**
In order for `Tailchat` to know that there is such a topic, we need to register the information related to this topic into Tailchat.
```js
// plugins/com.test.hutao/src/index.tsx
import { regPluginColorScheme, sharedEvent } from '@capital/common';
regPluginColorScheme({
label: '原神-胡桃测试主题',
name: 'light+genshin-hutao',
});
sharedEvent.on('loadColorScheme', (colorSchemeName) => {
if (colorSchemeName === 'light+genshin-hutao') {
console.log('正在加载胡桃主题...');
import('./theme.less');
}
});
```
This plugin does two things:
- Call `regPluginColorScheme` to register the theme life into `Tailchat`, so that `Tailchat` will display the theme in the system settings.
- It should be noted that the `name` of the theme is composed of `<color>+<theme-name>`, the default Tailchat will provide `auto`/`light`/`dark` three color schemes, here it means to add a style override based on the bright color mode.
- Listen to the theme loading event through `sharedEvent`, if the color theme is our theme, then load the theme asynchronously (the purpose of asynchronous loading is to reduce meaningless network requests)
At this time, our compilation cannot pass, because the file of less type has not been processed yet
`mini-star` has already processed the less file by default, we only need to install the `less` package, and `mini-star` will automatically call the installed less to compile.
```bash
npm install less
```
At this time, execute `npm run plugins:all` to compile, and the final directory should be as follows:
```
.
├── dist
│   └── plugins
│   └── com.test.hutao
│   ├── index.js
│   ├── index.js.map
│   ├── theme-475203da.js
│   └── theme-475203da.js.map
├── package-lock.json
├── package.json
└── plugins
└── com.test.hutao
├── manifest.json
├── package.json
├── src
│   ├── avatar.png
│   ├── bg.jpg
│   ├── index.tsx
│   ├── theme.less
│   └── translate.ts
├── tsconfig.json
└── types
└── tailchat.d.ts
```
For ease of management, `mini-star` will directly package images in the format of `base64` into the style file when processing the static resources referenced by less. Therefore, the theme file will be very large, which is why we need to load it asynchronously on demand.
![](/img/tutorial/plugin/9.png)
Because we have installed the plugin we are developing in Tailchat before, so just refresh it directly.
Click on the theme page of system settings in the setting menu in the lower left corner, and we can see our theme. Switch to the past and immediately the interface will become the theme style we want.
![](/img/tutorial/plugin/10.png)
## Reference
The official implementation of this theme can be accessed at [https://github.com/msgbyte/tailchat/tree/master/client/web/plugins/com.msgbyte.theme.genshin](https://github.com/msgbyte/tailchat/tree/master/client/web/plugins/com.msgbyte.theme.genshin) View

@ -0,0 +1,62 @@
---
sidebar_position: 1
title: Init Plugin Develop Env
---
Before developing a plugin, we need to create a plugin development environment. This environment can be the environment where the official source code of Tailchat is directly reused (https://github.com/msgbyte/tailchat/tree/master/client/web/plugins), It can also be an independent project
Here we mainly teach you how to create an independent plugin development environment
## frontend plugin development environment
It is very simple to create a plugin. Before that, if we did not initialize the plugin environment, we need to initialize the development environment first
Let's just find a place to build a project folder:
```bash
mkdir tailchat-plugin-test && cd tailchat-plugin-test
```
Execute in the root directory:
```bash
npm init -y
npm install mini-star
```
Create the configuration file of the `mini-star` in the root directory which named `.ministarrc.js`, the content is as follows:
```js
// .ministarrc.js
const path = require('path');
module.exports = {
externalDeps: [
'react',
'react-router',
'axios',
'styled-components',
'zustand',
'zustand/middleware/immer',
],
};
```
Write a compilation script in `package.json`
```json
{
//...
"scripts": {
// ...
"plugins:all": "ministar buildPlugin all",
"plugins:watch": "ministar watchPlugin all",
// ...
}
//...
}
```
## backend plugin development environment
TODO

@ -1,5 +1,5 @@
---
sidebar_position: 2
sidebar_position: 10
title: Icon
---

@ -5,12 +5,33 @@ title: Start developing plugins
## Learn MiniStar
`MiniStar` is a complete microkernel architecture development toolchain, and the plugin architecture of `tailchat` is developed based on `MiniStar`.
`MiniStar` is a complete microkernel architecture development toolchain, and the plugin architecture of `Tailchat` is developed based on `MiniStar`.
Learn more about `MiniStar`, you can check the official documentation of `MiniStar`: [https://ministar.moonrailgun.com/](https://ministar.moonrailgun.com/)
## Create a base project
import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';
<Tabs groupId="start">
<TabItem value="cli" label="Use the cli to quick start" default>
Move to the project directory (can be a custom project):
```bash
tailchat create
```
Choose the template according to the actual situation
![](/img/tutorial/plugin/1.png)
</TabItem>
<TabItem value="ministar" label="Use the native ministar to create a plugin project" default>
First, create a basic npm project and install `MiniStar` globally
```bash
@ -29,6 +50,9 @@ Execute: `ministar buildPlugin` in the project to compile the plugin
>
> Registration content: `com.msgbyte.webview/grouppanel`
</TabItem>
</Tabs>
## Install Plugin
### Manually install plugin

@ -0,0 +1,212 @@
---
sidebar_position: 2
title: 开发一个主题插件
---
`Tailchat` 中开发插件是非常方便的,我们从开发一个主题插件来开始
## 最终效果
![](/img/tutorial/plugin/2.png)
![](/img/tutorial/plugin/3.png)
![](/img/tutorial/plugin/4.png)
## 创建插件
> 如果是第一次开发需要确保已经初始化了插件开发环境。 [了解更多](./init-env.md)
```bash
tailchat create --template client-plugin
```
因为主题样式是一个纯前端的实现,因此我们选择`client-plugin`模板
通过交互式命令行提示完成创建工作
![](/img/tutorial/plugin/5.png)
此时的目录结构应该是这样的:
```
.
├── package-lock.json
├── package.json
├── node_modules
└── plugins
└── com.test.hutao
├── manifest.json
├── package.json
├── src
│   ├── index.tsx
│   └── translate.ts
├── tsconfig.json
└── types
└── tailchat.d.ts
```
这时候我们就可以立即开始编译插件了
```bash
npm run plugins:all
```
编译的产物会出现在 `/dist/plugins/com.test.hutao`。通过修改 `mini-star` 的配置可以让其输出在其他目录 *(这点会在服务端开发插件中会用到)*
## 在 Tailchat 中安装插件
对于前端插件我们需要把产物用一个http静态服务提供出来比如在本地开发中我们可以这样:
```bash
npx http-server .
```
![](/img/tutorial/plugin/6.png)
此时我们可以通过地址访问到我们编译出来的结果了,如 [http://127.0.0.1:8080/dist/plugins/com.test.hutao/index.js](http://127.0.0.1:8080/dist/plugins/com.test.hutao/index.js)
在 Tailchat 中提供了手动安装插件的方式,我们可以通过手动安装的方式安装插件。
复制插件目录下 `plugins/com.test.hutao/manifest.json` 的内容,粘贴到手动安装插件部分。
修改url为可以访问到的真实地址。点击确定按钮后自动执行安装命令:
![](/img/tutorial/plugin/7.png)
如果提示安装成功,打开控制台则可以看到对应输出:
![](/img/tutorial/plugin/8.png)
这里是插件里默认生成的逻辑,当日志输出了插件加载完毕的提示说明我们的插件已经被安装成功了。
## 编写样式文件并应用
因为我们需要修改主题样式,因此涉及到样式文件和静态资源的处理。接下来我们先来创建我们主题的样式
在`plugins/com.test.hutao/src/theme.less`中写入样式:
```less
#tailchat-app.theme-genshin-hutao {
@primary-color: #dd5545;
--tc-primary-color: @primary-color;
--tc-background-image: url(./bg.jpg);
--tc-content-background-image: url(./avatar.png);
--tc-content-background-image-opacity: 0.15;
.bg-navbar-light {
background-color: @primary-color;
.bg-gray-400 {
background-color: darken(@primary-color, 10%);
}
}
.bg-sidebar-light {
background-color: lighten(@primary-color, 20%);
}
.bg-content-light {
background-color: lighten(@primary-color, 40%);
}
&.dark {
--tc-primary-color: darken(@primary-color, 10%);
.dark\:bg-navbar-dark {
background-color: darken(@primary-color, 40%);
}
.dark\:bg-sidebar-dark {
background-color: darken(@primary-color, 20%);
}
.dark\:bg-content-dark {
background-color: @primary-color;
}
}
}
```
这个样式文件做了三件事情:
- 通过 `less变量` 的方式指定主色调, 大幅度减少冗余代码
- 通过 `css变量` 的方式指定 `Tailchat` 的各处背景图, 主要是登录页的背景图与头像
- 在根节点使用`#tailchat-app.theme-genshin-hutao`选择器确保不会污染全局样式。
**对应的,需要在根节点下放入`bg.jpg`和`avatar.png`作为静态资源**
为了让 `Tailchat` 能够知道有这么一个主题存在,我们需要将这个主题相关的信息注册到 `Tailchat` 中.
```js
// plugins/com.test.hutao/src/index.tsx
import { regPluginColorScheme, sharedEvent } from '@capital/common';
regPluginColorScheme({
label: '原神-胡桃测试主题',
name: 'light+genshin-hutao',
});
sharedEvent.on('loadColorScheme', (colorSchemeName) => {
if (colorSchemeName === 'light+genshin-hutao') {
console.log('正在加载胡桃主题...');
import('./theme.less');
}
});
```
这个插件做了两件事情:
- 调用 `regPluginColorScheme` 将主题生命注册到 `Tailchat` 中,这样 `Tailchat` 就会在系统设置中显示该主题。
- 需要注意的是主题的 `name` 是以 `<color>+<theme-name>`的形式组成的,默认的 `Tailchat` 会提供 `auto`/`light`/`dark`三种配色方案,这里是指基于亮色模式追加样式覆盖的意思。
- 通过 `sharedEvent` 监听加载主题样式事件,如果配色主题是我们的主题,则异步加载主题(异步加载的目的是减少无意义的网络请求)
此时我们的编译是无法通过的因为对于less类型的文件还没有处理
`mini-star` 已经默认对less文件进行处理我们只需要安装一下 `less` 包以后 `mini-star` 会自动调用安装的less进行编译
```bash
npm install less
```
此时执行`npm run plugins:all`进行编译操作,最后目录应当如下:
```
.
├── dist
│   └── plugins
│   └── com.test.hutao
│   ├── index.js
│   ├── index.js.map
│   ├── theme-475203da.js
│   └── theme-475203da.js.map
├── package-lock.json
├── package.json
└── plugins
└── com.test.hutao
├── manifest.json
├── package.json
├── src
│   ├── avatar.png
│   ├── bg.jpg
│   ├── index.tsx
│   ├── theme.less
│   └── translate.ts
├── tsconfig.json
└── types
└── tailchat.d.ts
```
为了便于管理,`mini-star` 在处理less引用的静态资源时会直接以 `base64` 的格式把图片打包到样式文件中。因此主题文件会非常大,这也是为什么我们需要按需异步加载的原因
![](/img/tutorial/plugin/9.png)
因为我们之前已经在 Tailchat 中安装过我们开发中的插件了,因此直接刷新即可。
在左下角设置菜单中点开系统设置的主题页面,我们就可以看到我们的主题了。切换过去立即界面会变成我们想要的主题风格。
![](/img/tutorial/plugin/10.png)
## 源码参考
官方的该主题实现可以访问 [https://github.com/msgbyte/tailchat/tree/master/client/web/plugins/com.msgbyte.theme.genshin](https://github.com/msgbyte/tailchat/tree/master/client/web/plugins/com.msgbyte.theme.genshin) 查看

@ -0,0 +1,62 @@
---
sidebar_position: 1
title: 初始化插件开发环境
---
在开发一个插件之前,我们需要创建一个插件开发环境,这个环境可以是直接复用 Tailchat 官方源码的环境(https://github.com/msgbyte/tailchat/tree/master/client/web/plugins),也可以是一个独立的项目
这里主要教大家怎么创建一个独立的插件开发环境
## 前端插件开发环境
创建一个插件非常简单, 在此之前如果我们没有初始化插件环境的话需要先初始化一下开发环境
我们先随便找个地方建一个项目文件夹:
```bash
mkdir tailchat-plugin-test && cd tailchat-plugin-test
```
在根目录下执行:
```bash
npm init -y
npm install mini-star
```
在根目录创建 `mini-star` 的配置文件 `.ministarrc.js`,内容如下:
```js
// .ministarrc.js
const path = require('path');
module.exports = {
externalDeps: [
'react',
'react-router',
'axios',
'styled-components',
'zustand',
'zustand/middleware/immer',
],
};
```
`package.json` 中写入编译脚本
```json
{
//...
"scripts": {
// ...
"plugins:all": "ministar buildPlugin all",
"plugins:watch": "ministar watchPlugin all",
// ...
}
//...
}
```
## 后端插件开发环境
TODO

@ -1,5 +1,5 @@
---
sidebar_position: 2
sidebar_position: 10
title: Icon 图标
---

@ -5,13 +5,33 @@ title: 开始开发插件
## 认识 MiniStar
`MiniStar` 是一套完整的微内核架构开发工具链,`tailchat`的插件架构就是基于 `MiniStar` 进行开发。
`MiniStar` 是一套完整的微内核架构开发工具链,`Tailchat`的插件架构就是基于 `MiniStar` 进行开发。
关于更多的 `MiniStar` 相关问题可以查看 `MiniStar` 的官方文档: [https://ministar.moonrailgun.com/](https://ministar.moonrailgun.com/)
## 创建一个基项目
## 创建一个基项目
首先创建一个基本的 npm 项目, 并全局安装 `MiniStar`
import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';
<Tabs groupId="start">
<TabItem value="cli" label="使用命令行工具快速创建" default>
移动到项目目录下(可以是自定义项目):
```bash
tailchat create
```
根据实际情况选择模板
![](/img/tutorial/plugin/1.png)
</TabItem>
<TabItem value="ministar" label="使用原生ministar创建插件项目" default>
创建一个基本的 npm 项目, 并全局安装 `MiniStar`
```bash
npm install --global mini-star
@ -28,6 +48,12 @@ npm install --global mini-star
>
> 注册内容: `com.msgbyte.webview/grouppanel`
</TabItem>
</Tabs>
## 安装插件
### 手动安装插件

Binary file not shown.

After

Width:  |  Height:  |  Size: 54 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 85 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 87 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.0 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 88 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 87 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 104 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 47 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

Loading…
Cancel
Save