Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions internal-plugins/setting/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ const { toastState, confirmState, handleConfirm, handleCancel } = useToast()
<!-- 全局确认对话框 -->
<ConfirmDialog
v-model:visible="confirmState.visible"
v-model:extra-values="confirmState.extraValues"
:title="confirmState.title"
:message="confirmState.message"
:type="confirmState.type"
:confirm-text="confirmState.confirmText"
:cancel-text="confirmState.cancelText"
:extra="confirmState.extra"
@confirm="handleConfirm"
@cancel="handleCancel"
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@

<div class="dialog-content">
<p class="dialog-message">{{ message }}</p>
<div v-if="extraItems.length > 0" class="dialog-extra">
<label v-for="item in extraItems" :key="item.id" class="dialog-extra-item">
<input
type="checkbox"
:checked="currentExtraValues[item.id] ?? false"
@change="handleExtraChange(item.id, $event)"
/>
<span>{{ item.message }}</span>
</label>
</div>
</div>

<div class="dialog-footer">
Expand All @@ -46,6 +56,7 @@

<script setup lang="ts">
import { computed } from 'vue'
import type { ConfirmExtraItem } from '../Toast'

interface Props {
visible: boolean
Expand All @@ -54,6 +65,8 @@ interface Props {
type?: 'info' | 'warning' | 'danger'
confirmText?: string
cancelText?: string
extra?: ConfirmExtraItem[]
extraValues?: Record<string, boolean>
}

const props = withDefaults(defineProps<Props>(), {
Expand All @@ -65,10 +78,14 @@ const props = withDefaults(defineProps<Props>(), {

const emit = defineEmits<{
(e: 'update:visible', value: boolean): void
(e: 'update:extraValues', value: Record<string, boolean>): void
(e: 'confirm'): void
(e: 'cancel'): void
}>()

const extraItems = computed(() => props.extra ?? [])
const currentExtraValues = computed(() => props.extraValues ?? {})

const confirmButtonClass = computed(() => {
if (props.type === 'danger') return 'btn-danger'
if (props.type === 'warning') return 'btn-warning'
Expand All @@ -85,6 +102,15 @@ const handleCancel = (): void => {
emit('update:visible', false)
}

const handleExtraChange = (id: string, event: Event): void => {
const target = event.target
if (!(target instanceof HTMLInputElement)) return
emit('update:extraValues', {
...currentExtraValues.value,
[id]: target.checked
})
}

const handleOverlayClick = (): void => {
handleCancel()
}
Expand Down Expand Up @@ -170,6 +196,30 @@ const handleOverlayClick = (): void => {
white-space: pre-wrap;
}

.dialog-extra {
margin-top: 14px;
display: flex;
flex-direction: column;
gap: 8px;
}

.dialog-extra-item {
display: flex;
align-items: center;
gap: 8px;
font-size: 13px;
line-height: 1.4;
color: var(--text-color);
cursor: pointer;
}

.dialog-extra-item input {
width: 15px;
height: 15px;
margin: 0;
accent-color: var(--primary-color);
}

.dialog-footer {
display: flex;
gap: 10px;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { DetailPanel } from '@/components'
import PluginDetailHeader from './PluginDetailHeader.vue'
import PluginDetailTabs from './PluginDetailTabs.vue'
import PluginDetailToolbar from './PluginDetailToolbar.vue'
import type { PluginDownloadState, PluginItem, TabId } from './types'
import type { PluginDownloadState, PluginItem, PluginUninstallOptions, TabId } from './types'
import { usePluginDetail } from './usePluginDetail'

const props = defineProps<{
Expand All @@ -27,7 +27,7 @@ const emit = defineEmits<{
(e: 'open'): void
(e: 'download'): void
(e: 'upgrade'): void
(e: 'uninstall'): void
(e: 'uninstall', options: PluginUninstallOptions): void
(e: 'kill'): void
(e: 'open-folder'): void
(e: 'toggle-pin'): void
Expand Down Expand Up @@ -105,7 +105,7 @@ function onSwitchTab(tabId: TabId): void {
@open="emit('open')"
@kill="emit('kill')"
@open-folder="emit('open-folder')"
@uninstall="handleUninstall(() => emit('uninstall'))"
@uninstall="handleUninstall((options) => emit('uninstall', options))"
@toggle-pin="emit('toggle-pin')"
@toggle-disabled="emit('toggle-disabled', $event)"
@toggle-settings-dropdown="toggleSettingsDropdown"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
export { default as PluginDetail } from './PluginDetail.vue'
export type { PluginItem, PluginFeature, DocItem, TabId, TabItem } from './types'
export type {
PluginItem,
PluginFeature,
PluginUninstallOptions,
DocItem,
TabId,
TabItem
} from './types'
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,7 @@ export interface PluginDownloadState {
totalBytes?: number
error?: string
}

export interface PluginUninstallOptions {
deleteData: boolean
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { marked } from 'marked'
import { computed, onMounted, onUnmounted, ref, watch, type Ref } from 'vue'
import { useToast } from '@/components'
import type { DocItem, PluginItem, TabId, TabItem } from './types'
import type { DocItem, PluginItem, PluginUninstallOptions, TabId, TabItem } from './types'

// 配置 marked
marked.setOptions({
Expand All @@ -19,7 +19,7 @@ export interface UsePluginDetailOptions {
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
export function usePluginDetail(options: UsePluginDetailOptions) {
const { plugin, isRunning, showComments = false } = options
const { success, error, confirm } = useToast()
const { success, error, confirm, confirmWithExtra } = useToast()

// 插件设置状态
const showSettingsDropdown = ref(false)
Expand Down Expand Up @@ -313,16 +313,23 @@ export function usePluginDetail(options: UsePluginDetailOptions) {
})

// 处理卸载
async function handleUninstall(emitFn: () => void): Promise<void> {
const confirmed = await confirm({
async function handleUninstall(emitFn: (options: PluginUninstallOptions) => void): Promise<void> {
const result = await confirmWithExtra({
title: '删除插件',
message: `确定要删除插件"${plugin.value.name}"吗?\n\n此操作将删除插件文件,无法恢复。`,
type: 'danger',
extra: [
{
id: 'deleteData',
message: '同时删除插件数据',
defaultChecked: false
}
],
confirmText: '删除',
cancelText: '取消'
})
if (confirmed) {
emitFn()
if (result.confirmed) {
emitFn({ deleteData: result.extra.deleteData === true })
}
}

Expand Down
55 changes: 47 additions & 8 deletions internal-plugins/setting/src/components/common/Toast/Toast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,31 @@ interface ConfirmOptions {
cancelText?: string
}

export interface ConfirmExtraItem {
id: string
message: string
defaultChecked?: boolean
}

interface ConfirmWithExtraOptions extends ConfirmOptions {
extra: ConfirmExtraItem[]
}

export interface ConfirmWithExtraResult {
confirmed: boolean
extra: Record<string, boolean>
}

interface ConfirmState {
visible: boolean
title: string
message: string
type: 'info' | 'warning' | 'danger'
confirmText: string
cancelText: string
resolve: ((value: boolean) => void) | null
extra: ConfirmExtraItem[]
extraValues: Record<string, boolean>
resolve: ((value: ConfirmWithExtraResult) => void) | null
}

const toastState = ref<ToastState>({
Expand All @@ -45,6 +62,8 @@ const confirmState = ref<ConfirmState>({
type: 'info',
confirmText: '确定',
cancelText: '取消',
extra: [],
extraValues: {},
resolve: null
Comment thread
guopenghui marked this conversation as resolved.
})

Expand All @@ -58,6 +77,7 @@ export function useToast(): {
info: (message: string, duration?: number) => void
hide: () => void
confirm: (options: ConfirmOptions) => Promise<boolean>
confirmWithExtra: (options: ConfirmWithExtraOptions) => Promise<ConfirmWithExtraResult>
handleConfirm: () => void
handleCancel: () => void
} {
Expand Down Expand Up @@ -91,31 +111,49 @@ export function useToast(): {
}

// 确认对话框
const confirm = (options: ConfirmOptions): Promise<boolean> => {
const confirm = async (options: ConfirmOptions): Promise<boolean> => {
const result = await confirmWithExtra({
...options,
extra: []
})
return result.confirmed
}

/**
* 加勾选框来额外获取一些信息
*/
const confirmWithExtra = (options: ConfirmWithExtraOptions): Promise<ConfirmWithExtraResult> => {
return new Promise((resolve) => {
const extraValues = Object.fromEntries(
options.extra.map((item) => [item.id, item.defaultChecked ?? false])
)
confirmState.value = {
visible: true,
title: options.title || '确认操作',
message: options.message,
type: options.type || 'info',
confirmText: options.confirmText || '确定',
cancelText: options.cancelText || '取消',
extra: options.extra,
extraValues,
resolve
}
})
}

const handleConfirm = (): void => {
if (confirmState.value.resolve) {
confirmState.value.resolve(true)
}
confirmState.value.resolve?.({
confirmed: true,
extra: { ...confirmState.value.extraValues }
})
confirmState.value.visible = false
}

const handleCancel = (): void => {
if (confirmState.value.resolve) {
confirmState.value.resolve(false)
}
confirmState.value.resolve?.({
confirmed: false,
extra: { ...confirmState.value.extraValues }
})
confirmState.value.visible = false
}

Expand All @@ -129,6 +167,7 @@ export function useToast(): {
info,
hide,
confirm,
confirmWithExtra,
handleConfirm,
handleCancel
}
Expand Down
5 changes: 4 additions & 1 deletion internal-plugins/setting/src/env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,10 @@ declare global {
) => Promise<{ success: boolean; error?: string }>
// 打包指定开发项目
packageDevProject: (pluginName: string) => Promise<{ success: boolean; error?: string }>
deletePlugin: (pluginPath: string) => Promise<{ success: boolean; error?: string }>
deletePlugin: (
pluginPath: string,
options?: { deleteData?: boolean }
) => Promise<{ success: boolean; error?: string }>
killPlugin: (pluginPath: string) => Promise<{ success: boolean; error?: string }>
revealInFinder: (filePath: string) => Promise<void>
launch: (options: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<script setup lang="ts">
import { computed, onMounted, onUnmounted, ref } from 'vue'
import { useToast } from '@/components'
import type { PluginUninstallOptions } from '@/components'
import {
compareVersions,
shuffleArray,
Expand Down Expand Up @@ -392,20 +393,23 @@ async function downloadPlugin(plugin: Plugin): Promise<void> {
}
}

async function handleUninstallPlugin(plugin: Plugin): Promise<void> {
async function handleUninstallPlugin(
plugin: Plugin,
options: PluginUninstallOptions
): Promise<void> {
if (!plugin.path) {
error('无法卸载:找不到插件路径')
return
}

try {
const deleteResult = await window.ztools.internal.deletePlugin(plugin.path)
const deleteResult = await window.ztools.internal.deletePlugin(plugin.path, options)
if (!deleteResult.success) {
error(`卸载失败: ${deleteResult.error}`)
return
}

success('插件卸载成功')
success(options.deleteData ? '插件已卸载,插件数据已删除' : '插件已卸载,插件数据已保留')

plugin.installed = false
plugin.localVersion = undefined
Expand Down Expand Up @@ -666,7 +670,7 @@ onUnmounted(() => {
@open="handleOpenPlugin(selectedPlugin)"
@download="downloadPlugin(selectedPlugin)"
@upgrade="handleUpgradePlugin(selectedPlugin)"
@uninstall="handleUninstallPlugin(selectedPlugin)"
@uninstall="handleUninstallPlugin(selectedPlugin, $event)"
/>
</Transition>
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script setup lang="ts">
import { nextTick, ref } from 'vue'
import { PluginDetail as SharedPluginDetail } from '@/components'
import type { TabId } from '@/components'
import type { PluginUninstallOptions, TabId } from '@/components'
import type { PluginDownloadState } from '../types'

const props = defineProps<{
Expand All @@ -16,7 +16,7 @@ defineEmits<{
(e: 'open'): void
(e: 'download'): void
(e: 'upgrade'): void
(e: 'uninstall'): void
(e: 'uninstall', options: PluginUninstallOptions): void
(e: 'kill'): void
(e: 'open-folder'): void
(e: 'package'): void
Expand Down Expand Up @@ -73,7 +73,7 @@ function handleTabSwitch(tabId: TabId): void {
@open="$emit('open')"
@download="$emit('download')"
@upgrade="$emit('upgrade')"
@uninstall="$emit('uninstall')"
@uninstall="$emit('uninstall', $event)"
@kill="$emit('kill')"
@open-folder="$emit('open-folder')"
@package="$emit('package')"
Expand Down
Loading