fix: app upgrade should not overwrite the activation status of plugins (#1610)

Co-authored-by: sealday <sealday@gmail.com>
Reviewed-on: daoyoucloud/tachybase#1610
This commit is contained in:
sealday 2024-10-16 12:12:03 +08:00 committed by ruiling
parent 388172e098
commit 278da91d16
22 changed files with 464 additions and 2 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 69 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 71 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 73 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 74 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 71 KiB

View File

@ -0,0 +1,2 @@
/node_modules
/src

Binary file not shown.

After

Width:  |  Height:  |  Size: 89 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 82 KiB

View File

@ -0,0 +1,123 @@
## Todo 应用插件说明
@tachybase/plugin-todo-page
### 简介
该项目实现了一个 Todo 应用,用户可以通过浏览器在页面中管理待办事项。通过使用 React 进行开发,待办事项列表支持增、删、改、查操作。该插件通过 `@tachybase/client` 扩展系统功能,加载并展示 Todo 页面。
### 功能特性
- **添加待办事项**:用户可以输入新的任务并将其添加到待办事项列表中。
- **删除待办事项**:用户可以删除已完成或不再需要的任务。
- **编辑待办事项**:用户可以对现有的任务进行修改。
- **标记完成状态**:用户可以通过复选框标记任务是否完成。
- **页面加载插件**:通过 `Plugin` 类将 Todo 页面的加载集成到主应用中。
### 环境要求
- Node.js
- React
- `@tachybase/client` 插件系统支持
### 安装步骤
1. **克隆项目**
```bash
# git clone 项目
git clone https://git.daoyoucloud.com:8443/daoyoucloud/tachybase.git
# 安装依赖(必须使用 pnpm
cd tachybase
pnpm i
# 初始化环境
pnpm tachybase install -f
# 然后初始化一个插件,并添加到系统中:
pnpm pm create @samples/plugin-replace-page
pnpm pm enable @samples/plugin-replace-page
pnpm pm create @tachybase/plugin-todo-page
pnpm pm enable @tachybase/plugin-todo-page
# 然后启动项目即可:
pnpm dev
```
2. **安装依赖**
```bash
{
"name": "@tachybase/plugin-todo-page",
"version": "0.21.61",
"main": "dist/server/index.js",
"dependencies": {
"react-router-dom": "^6.11.2",
"antd": "^5.18.3"
},
"peerDependencies": {
"@tachybase/client": "workspace:*",
"@tachybase/server": "workspace:*",
"@tachybase/plugin-auth": "workspace:*",
"@tachybase/test": "workspace:*"
}
}
```
3. **运行项目**
```bash
pnpm dev
```
### API 集成说明
该项目通过 `PluginTodoPageClient` 类集成了 Todo 页面,主要实现了 `afterAdd`、`beforeLoad` 和 `load` 方法,具体说明如下:
#### 1. `afterAdd()`
该方法将在插件添加之后执行,目前预留了钩子(注释部分可实现自定义操作)。
#### 2. `beforeLoad()`
在插件页面加载之前会调用此方法,允许在加载前执行相关初始化工作。
#### 3. `load()`
该方法是插件的核心部分,负责将 Todo 页面的路由和组件添加到应用中:
- `this.router.add()`:将路径 `/todo``todoList` 组件添加到路由中,确保访问该路径时显示待办事项页面。
- `this.app.pluginSettingsManager.add()`:向应用的插件管理器中添加设置,确保在插件管理界面显示 Todo 页面的入口。
### 文件说明
#### 1. `todoList.js`
- **作用**:定义了 `todoList` 组件,用于渲染待办事项页面。
- **功能**
- 通过 API 接口获取待办事项列表。
- 支持增、删、改、查、标记完成的功能。
- 使用 React 状态管理,用户可以在页面上实时操作待办事项。
#### 2. `PluginTodoPageClient.js`
- **作用**:作为插件的入口文件,负责将 Todo 页面的路由、组件注册到系统的插件管理中。
- **功能**
- 扩展系统的路由和页面展示功能。
- 加载 `todoList` 组件并集成到主应用中。
### API 说明
插件通过 HTTP API 与后端进行交互,实现对待办事项的增删改查功能:
- **GET `/api/todo:list`**:获取所有待办事项列表。
- **POST `/api/todo:create`**:添加新待办事项。
- **DELETE `/api/todo:destroy?filterByTk={id}`**:删除指定 ID 的待办事项。
- **PUT `/api/todo:update?filterByTk={id}`**:更新指定 ID 的待办事项。
### 项目结构
```
├── src
│ └── client
│ ├── index.js # 插件入口
│ ├── index.css # 样式文件
│ └── todoList.js # 定义 TodoList 组件
├── package.json # 项目依赖和配置信息
└── README.md # 项目说明文档
```
### 使用说明
1. 运行项目后,访问 `/todo` 路径可以看到待办事项页面。
2. 通过页面可以添加、删除、编辑和标记完成状态。
3. 插件会在应用中自动注册并集成,无需额外配置。
### 未来计划
- 增加任务优先级和截止日期功能。
- 增强界面交互,支持拖拽排序。
- 支持本地持久化和同步到云端功能。
### 贡献指南
如果您发现

View File

@ -0,0 +1,2 @@
export * from './dist/client';
export { default } from './dist/client';

View File

@ -0,0 +1 @@
module.exports = require('./dist/client/index.js');

View File

@ -0,0 +1,15 @@
{
"name": "@tachybase/plugin-todo-page",
"version": "0.21.61",
"main": "dist/server/index.js",
"dependencies": {
"antd": "^5.18.3",
"react-router-dom": "^6.11.2"
},
"peerDependencies": {
"@tachybase/client": "workspace:*",
"@tachybase/plugin-auth": "workspace:*",
"@tachybase/server": "workspace:*",
"@tachybase/test": "workspace:*"
}
}

View File

@ -0,0 +1,2 @@
export * from './dist/server';
export { default } from './dist/server';

View File

@ -0,0 +1 @@
module.exports = require('./dist/server/index.js');

View File

@ -0,0 +1,124 @@
import React, { useEffect, useState } from 'react';
import './index.css';
export function TodoList() {
const [todos, setTodos] = useState([]);
const [newTodo, setNewTodo] = useState('');
const [editingId, setEditingId] = useState(null);
const [editingText, setEditingText] = useState('');
useEffect(() => {
const fetchTodos = async () => {
const response = await fetch('http://localhost:3000/api/todo:list');
const { data } = await response.json();
if (Array.isArray(data)) setTodos(data);
};
fetchTodos();
}, []);
const handleAdd = async (e) => {
e.preventDefault();
if (newTodo.trim()) {
const response = await fetch('http://localhost:3000/api/todo:create', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ title: newTodo, completed: false }),
});
const newTodoItem = await response.json();
setTodos([...todos, newTodoItem]);
setNewTodo('');
}
};
const handleDelete = async (id) => {
await fetch(`http://localhost:3000/api/todo:destroy?filterByTk=${id}`, { method: 'DELETE' });
setTodos(todos.filter((todo) => todo.id !== id));
};
const handleEdit = (id, title) => {
setEditingId(id);
setEditingText(title);
};
const handleUpdate = async (id) => {
await fetch(`http://localhost:3000/api/todo:update?filterByTk=${id}`, {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ title: editingText }),
});
setTodos(todos.map((todo) => (todo.id === id ? { ...todo, title: editingText } : todo)));
setEditingId(null);
setEditingText('');
};
const handleToggleComplete = async (id, currentStatus) => {
const updatedStatus = !currentStatus;
await fetch(`http://localhost:3000/api/todo:update?filterByTk=${id}`, {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ completed: updatedStatus }),
});
setTodos(todos.map((todo) => (todo.id === id ? { ...todo, completed: updatedStatus } : todo)));
};
return (
<div className="todoapp stack-large">
<h1>TodoMatic</h1>
<form onSubmit={handleAdd}>
<h2 className="label-wrapper">
<label htmlFor="new-todo-input" className="label__lg">
What needs to be done?
</label>
</h2>
<input
type="text"
id="new-todo-input"
className="input input__lg"
autoComplete="off"
value={newTodo}
onChange={(e) => setNewTodo(e.target.value)}
/>
<button type="submit" className="btn btn__primary btn__lg">
Add
</button>
</form>
<h2 id="list-heading">{todos.length} tasks remaining</h2>
<ul role="list" className="todo-list stack-large stack-exception" aria-labelledby="list-heading">
{todos.map((todo) => (
<li key={todo.id} className="todo stack-small">
<div className="c-cb">
<input
id={`todo-${todo.id}`}
type="checkbox"
checked={todo.completed}
onChange={() => handleToggleComplete(todo.id, todo.completed)}
/>
{editingId === todo.id ? (
<input type="text" value={editingText} onChange={(e) => setEditingText(e.target.value)} />
) : (
<label className="todo-label" htmlFor={`todo-${todo.id}`}>
{todo.title}
</label>
)}
</div>
<div className="btn-group">
{editingId === todo.id ? (
<button type="button" className="btn" onClick={() => handleUpdate(todo.id)}>
Save
</button>
) : (
<button type="button" className="btn" onClick={() => handleEdit(todo.id, todo.title)}>
Edit
</button>
)}
<button type="button" className="btn btn__danger" onClick={() => handleDelete(todo.id)}>
Delete
</button>
</div>
</li>
))}
</ul>
</div>
);
}

View File

@ -0,0 +1,146 @@
/* RESETS */
*
*::before,
*::after {
box-sizing: border-box;
}
*:focus {
outline: 3px dashed #228bec;
outline-offset: 0;
}
html {
font: 62.5% / 1.15 sans-serif;
}
h1,
h2 {
margin-bottom: 0;
}
ul {
list-style: none;
padding: 0;
}
button {
border: none;
margin: 0;
padding: 0;
width: auto;
overflow: visible;
background: transparent;
color: inherit;
font: inherit;
line-height: normal;
-webkit-font-smoothing: inherit;
-moz-osx-font-smoothing: inherit;
-webkit-appearance: none;
}
button::-moz-focus-inner {
border: 0;
}
button,
input,
optgroup,
select,
textarea {
font-family: inherit;
font-size: 100%;
line-height: 1.15;
margin: 0;
}
button,
input {
overflow: visible;
}
input[type="text"] {
border-radius: 0;
}
body {
width: 100%;
max-width: 68rem;
margin: 0 auto;
font:
1.6rem/1.25 Arial,
sans-serif;
background-color: #f5f5f5;
color: #4d4d4d;
}
@media screen and (min-width: 620px) {
body {
font-size: 1.9rem;
line-height: 1.31579;
}
}
/*END RESETS*/
/* GLOBAL STYLES */
.form-group>input[type="text"] {
display: inline-block;
margin-top: 0.4rem;
}
.btn {
padding: 0.8rem 1rem 0.7rem;
border: 0.2rem solid #4d4d4d;
cursor: pointer;
text-transform: capitalize;
}
.btn.toggle-btn {
border-width: 1px;
border-color: #d3d3d3;
}
.btn.toggle-btn[aria-pressed="true"] {
text-decoration: underline;
border-color: #4d4d4d;
}
.btn__danger {
color: #fff;
background-color: #ca3c3c;
border-color: #bd2130;
}
.btn__filter {
border-color: lightgrey;
}
.btn__primary {
color: #fff;
background-color: #000;
}
.btn-group {
display: flex;
justify-content: space-between;
}
.btn-group>* {
flex: 1 1 49%;
}
.btn-group>*+* {
margin-left: 0.8rem;
}
.label-wrapper {
margin: 0;
flex: 0 0 100%;
text-align: center;
}
.visually-hidden {
position: absolute !important;
height: 1px;
width: 1px;
overflow

View File

@ -0,0 +1,25 @@
import { Plugin } from '@tachybase/client';
import { TodoList } from './TodoList';
export class PluginTodoPageClient extends Plugin {
async afterAdd() {
// await this.app.pm.add()
}
async beforeLoad() {} // You can get and modify the app instance here
async load() {
this.router.add('todo', {
path: '/todo',
Component: TodoList,
});
this.app.pluginSettingsManager.add('todo', {
title: 'todo',
icon: 'ApiOutlined',
Component: TodoList,
});
}
}
export default PluginTodoPageClient;

View File

@ -0,0 +1,2 @@
export * from './server';
export { default } from './server';

View File

@ -0,0 +1,20 @@
import { Plugin } from '@tachybase/server';
export class PluginTodoServer extends Plugin {
async load() {
// 定义一个 todo 数据表,包含 title 和 completed 字段
this.app.db.collection({
name: 'todos',
fields: [
{ type: 'string', name: 'title' },
{ type: 'boolean', name: 'completed', default: false },
],
});
await this.app.db.sync();
// 设置 todos 数据表的访问权限为公开
this.app.acl.allow('todos', '*', 'public');
}
}
export default PluginTodoServer;

View File

@ -191,9 +191,8 @@ export class PresetTachyBase extends Plugin {
}
}
const name = plugin[0];
const enabled = plugin[2];
const packageJson = await this.getPackageJson(name);
plugins.push({ name, packageName: packageJson.name, version: packageJson.version, enabled });
plugins.push({ name, packageName: packageJson.name, version: packageJson.version });
}
return plugins;
}