Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: make attachment list item operations extendable #4689

Merged
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
1 change: 1 addition & 0 deletions console/docs/extension-points/entity-listitem-operation.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
- 插件:`"plugin:list-item:operation:create"?: (plugin: Ref<Plugin>) => | OperationItem<Plugin>[] | Promise<OperationItem<Plugin>[]>`
- 备份:`"backup:list-item:operation:create"?: (backup: Ref<Backup>) => | OperationItem<Backup>[] | Promise<OperationItem<Backup>[]>`
- 主题:`"theme:list-item:operation:create"?: (theme: Ref<Theme>) => | OperationItem<Theme>[] | Promise<OperationItem<Theme>[]>`
- 附件:`"attachment:list-item:operation:create"?: (attachment: Ref<Attachment>) => | OperationItem<Attachment>[] | Promise<OperationItem<Attachment>[]>`

示例:

Expand Down
12 changes: 11 additions & 1 deletion console/packages/shared/src/types/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@ import type { PluginInstallationTab } from "@/states/plugin-installation-tabs";
import type { EntityFieldItem } from "@/states/entity";
import type { OperationItem } from "@/states/operation";
import type { ThemeListTab } from "@/states/theme-list-tabs";
import type { Backup, ListedPost, Plugin, Theme } from "@halo-dev/api-client";
import type {
Attachment,
Backup,
ListedPost,
Plugin,
Theme,
} from "@halo-dev/api-client";

export interface RouteRecordAppend {
parentName: RouteRecordName;
Expand Down Expand Up @@ -53,6 +59,10 @@ export interface ExtensionPoint {
backup: Ref<Backup>
) => OperationItem<Backup>[] | Promise<OperationItem<Backup>[]>;

"attachment:list-item:operation:create"?: (
attachment: Ref<Attachment>
) => OperationItem<Attachment>[] | Promise<OperationItem<Attachment>[]>;

"plugin:list-item:field:create"?: (
plugin: Ref<Plugin>
) => EntityFieldItem[] | Promise<EntityFieldItem[]>;
Expand Down
144 changes: 17 additions & 127 deletions console/src/modules/contents/attachments/AttachmentList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ import {
VSpace,
VEmpty,
IconFolder,
VStatusDot,
VEntity,
VEntityField,
VLoading,
Toast,
VDropdown,
Expand All @@ -30,21 +27,20 @@ import AttachmentPoliciesModal from "./components/AttachmentPoliciesModal.vue";
import AttachmentGroupList from "./components/AttachmentGroupList.vue";
import { computed, onMounted, ref, watch } from "vue";
import type { Attachment, Group } from "@halo-dev/api-client";
import { formatDatetime } from "@/utils/date";
import prettyBytes from "pretty-bytes";
import { useFetchAttachmentPolicy } from "./composables/use-attachment-policy";
import { useAttachmentControl } from "./composables/use-attachment";
import { apiClient } from "@/utils/api-client";
import cloneDeep from "lodash.clonedeep";
import { isImage } from "@/utils/image";
import { useRouteQuery } from "@vueuse/router";
import { useFetchAttachmentGroup } from "./composables/use-attachment-group";
import { usePermission } from "@/utils/permission";
import { useI18n } from "vue-i18n";
import { useLocalStorage } from "@vueuse/core";
import UserFilterDropdown from "@/components/filter/UserFilterDropdown.vue";
import { provide } from "vue";
import type { Ref } from "vue";
import AttachmentListItem from "./components/AttachmentListItem.vue";

const { currentUserHasPermission } = usePermission();
const { t } = useI18n();

const policyVisible = ref(false);
Expand Down Expand Up @@ -101,7 +97,6 @@ const {
handleFetchAttachments,
handleSelectNext,
handleSelectPrevious,
handleDelete,
handleDeleteInBatch,
handleCheckAll,
handleSelect,
Expand All @@ -121,6 +116,8 @@ const {
size: size,
});

provide<Ref<Set<Attachment>>>("selectedAttachments", selectedAttachments);

const handleMove = async (group: Group) => {
try {
const promises = Array.from(selectedAttachments.value).map((attachment) => {
Expand Down Expand Up @@ -177,11 +174,6 @@ const onUploadModalClose = () => {
handleFetchAttachments();
};

const getPolicyName = (name: string | undefined) => {
const policy = policies.value?.find((p) => p.metadata.name === name);
return policy?.spec.displayName;
};

// View type
const viewTypes = [
{
Expand Down Expand Up @@ -468,8 +460,8 @@ onMounted(() => {
role="list"
>
<VCard
v-for="(attachment, index) in attachments"
:key="index"
v-for="attachment in attachments"
:key="attachment.metadata.name"
:body-class="['!p-0']"
:class="{
'ring-1 ring-primary': isChecked(attachment),
Expand Down Expand Up @@ -552,118 +544,16 @@ onMounted(() => {
class="box-border h-full w-full divide-y divide-gray-100"
role="list"
>
<li v-for="(attachment, index) in attachments" :key="index">
<VEntity :is-selected="isChecked(attachment)">
<template
v-if="
currentUserHasPermission(['system:attachments:manage'])
"
#checkbox
>
<input
:checked="selectedAttachments.has(attachment)"
class="h-4 w-4 rounded border-gray-300 text-indigo-600"
type="checkbox"
@click="handleSelect(attachment)"
/>
</template>
<template #start>
<VEntityField>
<template #description>
<div
class="h-10 w-10 rounded border bg-white p-1 hover:shadow-sm"
>
<AttachmentFileTypeIcon
:display-ext="false"
:file-name="attachment.spec.displayName"
:width="8"
:height="8"
/>
</div>
</template>
</VEntityField>
<VEntityField
:title="attachment.spec.displayName"
@click="handleClickItem(attachment)"
>
<template #description>
<VSpace>
<span class="text-xs text-gray-500">
{{ attachment.spec.mediaType }}
</span>
<span class="text-xs text-gray-500">
{{ prettyBytes(attachment.spec.size || 0) }}
</span>
</VSpace>
</template>
</VEntityField>
</template>
<template #end>
<VEntityField
:description="getPolicyName(attachment.spec.policyName)"
/>
<VEntityField>
<template #description>
<RouterLink
:to="{
name: 'UserDetail',
params: {
name: attachment.spec.ownerName,
},
}"
class="text-xs text-gray-500"
:class="{
'pointer-events-none': !currentUserHasPermission([
'system:users:view',
]),
}"
>
{{ attachment.spec.ownerName }}
</RouterLink>
</template>
</VEntityField>
<VEntityField
v-if="attachment.metadata.deletionTimestamp"
>
<template #description>
<VStatusDot
v-tooltip="$t('core.common.status.deleting')"
state="warning"
animate
/>
</template>
</VEntityField>
<VEntityField>
<template #description>
<span
class="truncate text-xs tabular-nums text-gray-500"
>
{{
formatDatetime(
attachment.metadata.creationTimestamp
)
}}
</span>
</template>
</VEntityField>
</template>
<template #dropdownItems>
<VDropdownItem @click="handleClickItem(attachment)">
{{ $t("core.common.buttons.detail") }}
</VDropdownItem>
<VDropdownItem
v-if="
currentUserHasPermission([
'system:attachments:manage',
])
"
type="danger"
@click="handleDelete(attachment)"
>
{{ $t("core.common.buttons.delete") }}
</VDropdownItem>
</template>
</VEntity>
<li
v-for="attachment in attachments"
:key="attachment.metadata.name"
>
<AttachmentListItem
:attachment="attachment"
:is-selected="isChecked(attachment)"
@select="handleSelect"
@open-detail="handleClickItem"
/>
</li>
</ul>
</Transition>
Expand Down
Loading