Skip to content

Commit

Permalink
feat(config): support entry update and transfer
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed May 14, 2024
1 parent ae18578 commit af8a19a
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 51 deletions.
15 changes: 7 additions & 8 deletions plugins/config/client/components/forks.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<template>
<el-dialog
:model-value="!!dialogFork"
@update:model-value="dialogFork = null"
@update:model-value="dialogFork = undefined"
class="dialog-config-fork"
destroy-on-close>
<template #header="{ titleId, titleClass }">
Expand All @@ -10,7 +10,7 @@
</span>
</template>
<table>
<tr v-for="id in plugins.forks[dialogFork]" :key="id">
<tr v-for="id in plugins.forks[dialogFork!]" :key="id">
<td class="text-left">
<span class="status-light" :class="ctx.manager.getStatus(plugins.paths[id])"></span>
<span class="path">{{ getFullPath(plugins.paths[id]) }}</span>
Expand All @@ -25,8 +25,8 @@
</table>
<template #footer>
<div class="left">
<template v-if="plugins.forks[dialogFork]?.length">
此插件目前存在 {{ plugins.forks[dialogFork]?.length }} 份配置。
<template v-if="plugins.forks[dialogFork!]?.length">
此插件目前存在 {{ plugins.forks[dialogFork!]?.length }} 份配置。
</template>
<template v-else>
此插件尚未被配置。
Expand All @@ -52,7 +52,7 @@ const dialogFork = computed({
set: (value) => ctx.manager.dialogFork.value = value,
})
const local = computed(() => ctx.manager.data.value.packages[dialogFork.value])
const local = computed(() => ctx.manager.data.value.packages[dialogFork.value!])
function getLabel(tree: Node) {
return `${tree.label ? `${tree.label} ` : ''}[${tree.path}]`
Expand All @@ -64,19 +64,18 @@ function getFullPath(tree: Node) {
tree = tree.parent
path.unshift(getLabel(tree))
}
path.shift()
return path.join(' > ')
}
async function configure(id?: string) {
if (!id) {
id = await send('manager.config.create', {
name: dialogFork.value,
name: dialogFork.value!,
disabled: true,
})
}
await router.push('/plugins/' + id)
dialogFork.value = null
dialogFork.value = undefined
}
</script>
Expand Down
8 changes: 4 additions & 4 deletions plugins/config/client/components/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -164,16 +164,16 @@ async function createGroup(label: string) {
ctx.action('config.tree.clone', {
hidden: ({ config }) => !config.tree || !!config.tree.children,
action: async ({ config }) => {
const children = config.tree.parent.path
? config.tree.parent.children
const children = config.tree.parent!.path
? config.tree.parent!.children
: plugins.value.data.slice(1)
const index = children.findIndex(tree => tree.path === config.tree.path)
const id = await send('manager.config.create', {
name: config.tree.name,
config: config.tree.config,
disabled: true,
parent: config.tree.parent.id,
position: index + 1,
parent: config.tree.parent!.id,
index: index + 1,
})
router.replace(`/plugins/${id}`)
},
Expand Down
7 changes: 2 additions & 5 deletions plugins/config/client/components/plugin.vue
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,7 @@

<!-- config -->
<k-slot-item :order="-1000">
<k-comment v-if="!local.runtime.schema" type="warning">
<p>此插件未声明配置项,这可能并非预期行为。{{ hint }}</p>
</k-comment>
<k-form v-else :schema="local.runtime.schema" :initial="current.config" v-model="config">
<k-form v-if="local.runtime.schema" :schema="local.runtime.schema" :initial="current!.config" v-model="config">
<template #hint>{{ hint }}</template>
</k-form>
</k-slot-item>
Expand Down Expand Up @@ -126,7 +123,7 @@ const hint = computed(() => local.value.workspace ? '请检查插件源代码。
watch(local, (value) => {
if (!value || value.runtime) return
send('manager.package.runtime', value.package.name)
send('manager.package.runtime', { name: value.package.name })
}, { immediate: true })
provide('plugin:name', name)
Expand Down
6 changes: 3 additions & 3 deletions plugins/config/client/components/tree.vue
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,11 @@ function handleCollapse(data: Node, target: TreeNode, instance) {
function handleDrop(source: TreeNode, target: TreeNode, position: 'before' | 'after' | 'inner', event: DragEvent) {
const parent = position === 'inner' ? target : target.parent
let index = parent.childNodes.findIndex(node => node.data.path === source.data.path)
send('manager.config.teleport', {
const index = parent.childNodes.findIndex(node => node.data.path === source.data.path)
send('manager.config.transfer', {
id: source.data.id,
parent: parent.data.path,
position: index,
index,
})
}
Expand Down
2 changes: 1 addition & 1 deletion plugins/config/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ export default class Manager extends Service {
const required = !local.package.peerDependenciesMeta?.[name]?.optional
const active = !!this.data.value.packages[name]?.runtime?.id
result.peer[name] = { required, active }
for (const service of this.data.value.packages[name]?.manifest?.service.implements ?? []) {
for (const service of this.data.value.packages[name]?.manifest?.service?.implements ?? []) {
services.add(service)
}
}
Expand Down
16 changes: 9 additions & 7 deletions plugins/config/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@cordisjs/plugin-config",
"description": "Manage your bots and plugins with console",
"version": "2.8.5",
"version": "2.8.6",
"type": "module",
"exports": {
".": {
Expand All @@ -24,30 +24,32 @@
"license": "MIT",
"repository": {
"type": "git",
"url": "git+https://github.com/cordisjs/webui.git",
"url": "git+https://github.com/cordiverse/webui.git",
"directory": "plugins/config"
},
"bugs": {
"url": "https://github.com/cordisjs/webui/issues"
"url": "https://github.com/cordiverse/webui/issues"
},
"homepage": "https://koishi.chat/plugins/console/config.html",
"keywords": [
"cordis",
"plugin",
"config",
"manager",
"server"
"webui"
],
"cordis": {
"public": [
"dist"
],
"description": {
"en": "Configure your plugins with console",
"en": "Configure your plugins with WebUI",
"zh": "使用控制台查看、配置你的插件"
},
"service": {
"required": [
"implements": [
"manager"
],
"optional": [
"webui"
]
}
Expand Down
1 change: 0 additions & 1 deletion plugins/config/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Context } from 'cordis'
import { LocalScanner } from '@cordisjs/registry'
import {} from 'cordis/worker'
import { Manager } from './shared'
import { pathToFileURL } from 'url'

Expand Down
47 changes: 25 additions & 22 deletions plugins/config/src/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ declare module '@cordisjs/plugin-webui' {
'manager.config.create'(options: Omit<LoaderEntry.Options, 'id'> & EntryLocation): Promise<string>
'manager.config.update'(options: Omit<LoaderEntry.Options, 'name'>): void
'manager.config.remove'(options: { id: string }): void
'manager.config.teleport'(options: { id: string } & EntryLocation): void
'manager.config.transfer'(options: { id: string } & EntryLocation): void
'manager.package.list'(): Promise<LocalObject[]>
'manager.package.runtime'(name: string): Promise<RuntimeData>
'manager.package.runtime'(options: { name: string }): Promise<RuntimeData>
'manager.service.list'(): Dict<string[]>
}
}
Expand Down Expand Up @@ -55,7 +55,7 @@ export interface RuntimeData {

interface EntryLocation {
parent?: string
position?: number
index?: number
}

export abstract class Manager extends Service {
Expand Down Expand Up @@ -128,33 +128,35 @@ export abstract class Manager extends Service {
})

ctx.webui.addListener('manager.config.create', (options) => {
const { parent, position, ...rest } = options
return ctx.loader.create(rest, parent, position)
const { parent, index, ...rest } = options
return ctx.loader.create(rest, parent, index)
})

ctx.webui.addListener('manager.config.update', (options) => {
throw new Error('Not implemented')
const { id, ...rest } = options
return ctx.loader.update(id, rest)
})

ctx.webui.addListener('manager.config.remove', (options) => {
return ctx.loader.remove(options.id)
})

ctx.webui.addListener('manager.config.teleport', (options) => {
throw new Error('Not implemented')
ctx.webui.addListener('manager.config.transfer', (options) => {
const { id, parent, index } = options
return ctx.loader.transfer(id, parent ?? '', index)
})

ctx.webui.addListener('manager.package.list', async () => {
return await this.getPackages()
})

ctx.webui.addListener('manager.package.runtime', async (name) => {
let runtime = this.packages[name]?.runtime
ctx.webui.addListener('manager.package.runtime', async (options) => {
let runtime = this.packages[options.name]?.runtime
if (runtime) return runtime
runtime = await this.parseExports(name)
if (this.packages[name]) {
this.packages[name].runtime = runtime
this.flushPackage(name)
runtime = await this.parseExports(options.name)
if (this.packages[options.name]) {
this.packages[options.name].runtime = runtime
this.flushPackage(options.name)
}
return runtime
})
Expand Down Expand Up @@ -200,13 +202,14 @@ export abstract class Manager extends Service {

async parseExports(name: string) {
try {
const exports = this.ctx.loader.unwrapExports(await this.ctx.loader.import(name))
if (exports) this.plugins.set(exports, name)
const exports = await this.ctx.loader.import(name)
const plugin = this.ctx.loader.unwrapExports(exports)
if (plugin) this.plugins.set(plugin, name)
const result: RuntimeData = { id: null }
result.schema = exports?.Config || exports?.schema
result.usage = exports?.usage
result.filter = exports?.filter
const inject = exports?.using || exports?.inject || []
result.schema = plugin?.Config || plugin?.schema
result.usage = plugin?.usage
result.filter = plugin?.filter
const inject = plugin?.using || plugin?.inject || []
if (Array.isArray(inject)) {
result.required = inject
result.optional = []
Expand All @@ -218,8 +221,8 @@ export abstract class Manager extends Service {
// make sure that result can be serialized into json
JSON.stringify(result)

if (exports) {
const runtime = this.ctx.registry.get(exports)
if (plugin) {
const runtime = this.ctx.registry.get(plugin)
if (runtime) this.parseRuntime(runtime, result)
}
return result
Expand Down

0 comments on commit af8a19a

Please sign in to comment.