Files
timi-tdesign-mobile/src/components/tiptap-editor/TiptapMediaButton.vue
T

264 lines
5.8 KiB
Vue

<template>
<t-button :variant="active ? 'base' : 'text'" :theme="active ? 'light' : 'default'" :loading="uploading" @click="openDialog">
<t-icon :name="iconName" />
</t-button>
<bottom-popup
v-model="visible"
placement="bottom"
:title="`插入${typeLabel}`"
left-button-text="取消"
right-button-text="插入"
@left-button-click="visible = false"
@right-button-click="onConfirm"
destroy-on-close
>
<template #rightButton="{ onClick }">
<t-button
theme="primary"
variant="text"
size="small"
:loading="uploading"
:disabled="uploading"
@click="onClick"
content="插入"
/>
</template>
<t-form class="popup" label-align="left" label-width="auto">
<t-form-item label="文件">
<div class="field">
<t-input
:value="selectedFile?.name || `未选择${typeLabel}文件`"
readonly
borderless
/>
<t-button
size="extra-small"
variant="outline"
@click="pickFile"
>
<span v-text="selectedFile ? '重新选择' : `选择${typeLabel}`" />
</t-button>
</div>
</t-form-item>
<t-form-item label="标题">
<t-input
v-model="mediaForm.title"
:placeholder="`请输入${typeLabel}标题,可留空`"
borderless
/>
</t-form-item>
<t-form-item label="宽度">
<div class="field">
<t-input
v-model="mediaForm.width"
placeholder="请输入宽度,默认 100%"
borderless
/>
<t-button
size="extra-small"
variant="outline"
@click="setWidth('auto')"
content="自动"
/>
<t-button
size="extra-small"
variant="outline"
@click="setWidth('100%')"
content="铺满"
/>
</div>
</t-form-item>
<t-form-item label="高度">
<div class="field">
<t-input
v-model="mediaForm.height"
placeholder="请输入高度,默认 auto"
borderless
/>
<t-button
size="extra-small"
variant="outline"
@click="setHeight"
content="自动"
/>
</div>
</t-form-item>
</t-form>
</bottom-popup>
<input ref="fileInputRef" class="file-input" type="file" :accept="accept" @change="onFileChange" />
</template>
<script lang="ts" setup>
import { showPlatformMessage } from "../message";
import type { TiptapEditorInstance, UploadResult } from "@timi/web/editor";
import BottomPopup from "../bottom-popup/index.vue";
defineOptions({
name: "TiptapMediaButton"
});
const props = defineProps<{
editor?: TiptapEditorInstance;
active: boolean;
type: "image" | "video";
upload?: (file: File) => Promise<UploadResult>;
}>();
const visible = ref(false);
const uploading = ref(false);
const selectedFile = ref<File>();
const fileInputRef = ref<HTMLInputElement>();
const mediaForm = reactive({
title: "",
width: "100%",
height: ""
});
const typeLabel = computed(() => {
return props.type === "image" ? "图片" : "视频";
});
const iconName = computed(() => {
return props.type === "image" ? "image-add" : "video";
});
const accept = computed(() => {
return props.type === "image" ? "image/*" : "video/*";
});
function buildDefaultHeight() {
return "auto";
}
function setWidth(width: "auto" | "100%") {
mediaForm.width = width;
}
function setHeight() {
mediaForm.height = buildDefaultHeight();
}
function pickFile(event?: MouseEvent) {
event?.stopPropagation();
fileInputRef.value?.click();
}
function openDialog() {
if (!props.editor) {
return;
}
selectedFile.value = undefined;
const attributes = props.editor.getAttributes(props.type);
mediaForm.title = (attributes.title as string) || "";
mediaForm.width = (attributes.width as string) || "100%";
mediaForm.height = (attributes.height as string) || buildDefaultHeight();
visible.value = true;
}
function onFileChange(event: Event) {
const target = event.target as HTMLInputElement;
selectedFile.value = target.files?.[0];
target.value = "";
if (!selectedFile.value) {
return;
}
if (!mediaForm.title.trim()) {
mediaForm.title = selectedFile.value.name;
}
}
async function onConfirm() {
if (!props.editor) {
return;
}
if (!props.upload) {
await showPlatformMessage("warning", `${typeLabel.value}上传功能未启用`);
return;
}
if (!selectedFile.value) {
await showPlatformMessage("warning", `请选择${typeLabel.value}文件`);
return;
}
uploading.value = true;
try {
const file = selectedFile.value;
const result = await props.upload(file);
const title = mediaForm.title.trim() || result.name || file.name;
const attrs = {
src: result.url,
title,
width: mediaForm.width.trim() || "100%",
height: mediaForm.height.trim() || buildDefaultHeight(),
attachmentId: result.id,
tempFileId: result.id
};
if (props.type === "image") {
props.editor
.chain()
.focus()
.setImage({
src: attrs.src,
alt: title,
title
})
.updateAttributes("image", {
attachmentId: attrs.attachmentId,
tempFileId: attrs.tempFileId,
width: attrs.width,
height: attrs.height
})
.run();
} else {
props.editor
.chain()
.focus()
.insertContent({
type: "video",
attrs
})
.run();
}
visible.value = false;
} catch (error) {
console.error(`上传${typeLabel.value}失败:`, error);
await showPlatformMessage("error", `上传${typeLabel.value}失败`);
} finally {
uploading.value = false;
}
}
</script>
<style lang="less" scoped>
.popup {
margin: 0;
padding: 0 1rem calc(1rem + var(--tui-safe-bottom, 0px));
overflow-y: auto;
background: var(--td-bg-color-container);
.field {
gap: .5rem;
width: 100%;
display: flex;
align-items: center;
:deep(.t-input) {
min-width: 0;
flex: 1;
}
}
.selector {
--td-button-extra-small-height: 24px;
width: 100%;
display: flex;
align-items: center;
justify-content: space-between;
}
}
.file-input {
display: none;
}
</style>