mirror of https://github.com/halo-dev/halo
pref: refactor default editor video block upload logic (#5302)
#### What type of PR is this? /kind improvement /area console /area editor /milestone 2.13.x #### What this PR does / why we need it: 重构默认编辑器视频组件。使其支持上传、取消上传、从附件库选择、替换等功能。 #### How to test it? 直接拖动、复制或选择文件上传一个视频,查看是否显示上传进度条,取消、重试功能是否正常 #### Which issue(s) this PR fixes: Fixes #5240 #### Does this PR introduce a user-facing change? ```release-note 重构编辑器视频组件的上传逻辑,增加选择文件上传、上传进度条、取消、重试等机制。 ```pull/5409/head
parent
b132597cac
commit
3eb9d165bf
|
@ -96,7 +96,7 @@ const Video = Node.create<ExtensionOptions>({
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
controls: {
|
controls: {
|
||||||
default: null,
|
default: true,
|
||||||
parseHTML: (element) => {
|
parseHTML: (element) => {
|
||||||
return element.getAttribute("controls");
|
return element.getAttribute("controls");
|
||||||
},
|
},
|
||||||
|
|
|
@ -52,7 +52,11 @@ import {
|
||||||
ExtensionSearchAndReplace,
|
ExtensionSearchAndReplace,
|
||||||
} from "@halo-dev/richtext-editor";
|
} from "@halo-dev/richtext-editor";
|
||||||
// ui custom extension
|
// ui custom extension
|
||||||
import { UiExtensionImage, UiExtensionUpload } from "./extensions";
|
import {
|
||||||
|
UiExtensionImage,
|
||||||
|
UiExtensionUpload,
|
||||||
|
UiExtensionVideo,
|
||||||
|
} from "./extensions";
|
||||||
import {
|
import {
|
||||||
IconCalendar,
|
IconCalendar,
|
||||||
IconCharacterRecognition,
|
IconCharacterRecognition,
|
||||||
|
@ -263,7 +267,11 @@ onMounted(() => {
|
||||||
lowlight,
|
lowlight,
|
||||||
}),
|
}),
|
||||||
ExtensionIframe,
|
ExtensionIframe,
|
||||||
ExtensionVideo,
|
currentUserHasPermission(["uc:attachments:manage"])
|
||||||
|
? UiExtensionVideo.configure({
|
||||||
|
uploadVideo: props.uploadImage,
|
||||||
|
})
|
||||||
|
: ExtensionVideo,
|
||||||
ExtensionAudio,
|
ExtensionAudio,
|
||||||
ExtensionCharacterCount,
|
ExtensionCharacterCount,
|
||||||
ExtensionFontSize,
|
ExtensionFontSize,
|
||||||
|
|
|
@ -0,0 +1,224 @@
|
||||||
|
<script setup lang="ts">
|
||||||
|
import { VButton, VSpace, VDropdown } from "@halo-dev/components";
|
||||||
|
import type { Editor } from "@halo-dev/richtext-editor";
|
||||||
|
import { useFileDialog } from "@vueuse/core";
|
||||||
|
import type { AttachmentLike } from "@halo-dev/console-shared";
|
||||||
|
import { getAttachmentUrl, type AttachmentAttr } from "../utils/attachment";
|
||||||
|
import { i18n } from "@/locales";
|
||||||
|
import { onUnmounted, ref } from "vue";
|
||||||
|
import { watch } from "vue";
|
||||||
|
import { uploadFile } from "../utils/upload";
|
||||||
|
import type { Attachment } from "@halo-dev/api-client";
|
||||||
|
import type { AxiosRequestConfig } from "axios";
|
||||||
|
|
||||||
|
const props = withDefaults(
|
||||||
|
defineProps<{
|
||||||
|
editor: Editor;
|
||||||
|
accept: string;
|
||||||
|
uploadedFile: File;
|
||||||
|
uploadToAttachmentFile: (
|
||||||
|
file: File,
|
||||||
|
options?: AxiosRequestConfig
|
||||||
|
) => Promise<Attachment>;
|
||||||
|
}>(),
|
||||||
|
{
|
||||||
|
accept: "*",
|
||||||
|
uploadedFile: undefined,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
const emit = defineEmits<{
|
||||||
|
(event: "setExternalLink", attachment: AttachmentAttr): void;
|
||||||
|
(event: "onUploadReady", file: File): void;
|
||||||
|
(event: "onUploadProgress", progress: number): void;
|
||||||
|
(event: "onUploadFinish"): void;
|
||||||
|
(event: "onUploadError", error: Error): void;
|
||||||
|
(event: "onUploadAbort"): void;
|
||||||
|
}>();
|
||||||
|
|
||||||
|
const externalLink = ref("");
|
||||||
|
|
||||||
|
const handleEnterSetExternalLink = () => {
|
||||||
|
if (!externalLink.value) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
emit("setExternalLink", {
|
||||||
|
url: externalLink.value,
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
const { open, reset, onChange } = useFileDialog({
|
||||||
|
accept: props.accept,
|
||||||
|
multiple: false,
|
||||||
|
});
|
||||||
|
|
||||||
|
const openAttachmentSelector = () => {
|
||||||
|
props.editor.commands.openAttachmentSelector(
|
||||||
|
(attachments: AttachmentLike[]) => {
|
||||||
|
if (attachments.length > 0) {
|
||||||
|
const attachment = attachments[0];
|
||||||
|
const attachmentAttr = getAttachmentUrl(attachment);
|
||||||
|
emit("setExternalLink", attachmentAttr);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
accepts: [props.accept],
|
||||||
|
min: 1,
|
||||||
|
max: 1,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
};
|
||||||
|
const controller = ref<AbortController>();
|
||||||
|
const originalFile = ref<File>();
|
||||||
|
const uploadState = ref<"init" | "uploading" | "error">("init");
|
||||||
|
const uploadProgress = ref<number | undefined>(undefined);
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* Upload files to the attachment library.
|
||||||
|
*
|
||||||
|
* @param file attachments that need to be uploaded to the attachment library
|
||||||
|
*/
|
||||||
|
const handleUploadFile = (file: File) => {
|
||||||
|
controller.value = new AbortController();
|
||||||
|
originalFile.value = file;
|
||||||
|
uploadState.value = "uploading";
|
||||||
|
uploadProgress.value = undefined;
|
||||||
|
emit("onUploadReady", file);
|
||||||
|
uploadFile(file, props.uploadToAttachmentFile, {
|
||||||
|
controller: controller.value,
|
||||||
|
onUploadProgress: (progress) => {
|
||||||
|
uploadProgress.value = progress;
|
||||||
|
emit("onUploadProgress", progress);
|
||||||
|
},
|
||||||
|
|
||||||
|
onFinish: (attachment?: Attachment) => {
|
||||||
|
if (attachment) {
|
||||||
|
emit("setExternalLink", {
|
||||||
|
url: attachment.status?.permalink,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
handleResetUpload();
|
||||||
|
emit("onUploadFinish");
|
||||||
|
},
|
||||||
|
|
||||||
|
onError: (error: Error) => {
|
||||||
|
if (error.name !== "CanceledError") {
|
||||||
|
uploadState.value = "error";
|
||||||
|
}
|
||||||
|
emit("onUploadError", error);
|
||||||
|
},
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleUploadAbort = () => {
|
||||||
|
emit("onUploadAbort");
|
||||||
|
handleResetUpload();
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleUploadRetry = () => {
|
||||||
|
if (!controller.value) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
controller.value.abort();
|
||||||
|
if (!originalFile.value) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
handleUploadFile(originalFile.value);
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleResetUpload = () => {
|
||||||
|
uploadState.value = "init";
|
||||||
|
controller.value?.abort();
|
||||||
|
controller.value = undefined;
|
||||||
|
originalFile.value = undefined;
|
||||||
|
uploadProgress.value = undefined;
|
||||||
|
reset();
|
||||||
|
};
|
||||||
|
|
||||||
|
onChange((files) => {
|
||||||
|
if (!files) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (files.length > 0) {
|
||||||
|
handleUploadFile(files[0]);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
watch(
|
||||||
|
() => props.uploadedFile,
|
||||||
|
async (file) => {
|
||||||
|
if (!file) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
handleUploadFile(file);
|
||||||
|
},
|
||||||
|
{
|
||||||
|
immediate: true,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
onUnmounted(() => {
|
||||||
|
handleUploadAbort();
|
||||||
|
});
|
||||||
|
|
||||||
|
defineExpose({
|
||||||
|
abort: handleUploadAbort,
|
||||||
|
retry: handleUploadRetry,
|
||||||
|
reset: handleResetUpload,
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
<template>
|
||||||
|
<div class="flex h-64 w-full items-center justify-center">
|
||||||
|
<slot
|
||||||
|
v-if="$slots.uploading && uploadState === 'uploading'"
|
||||||
|
name="uploading"
|
||||||
|
:progress="uploadProgress"
|
||||||
|
></slot>
|
||||||
|
<slot
|
||||||
|
v-else-if="$slots.error && uploadState === 'error'"
|
||||||
|
name="error"
|
||||||
|
></slot>
|
||||||
|
<div
|
||||||
|
v-else
|
||||||
|
class="flex h-full w-full cursor-pointer flex-col items-center justify-center border-2 border-dashed border-gray-300 bg-gray-50"
|
||||||
|
>
|
||||||
|
<div
|
||||||
|
class="flex flex-col items-center justify-center space-y-7 pb-6 pt-5"
|
||||||
|
>
|
||||||
|
<slot v-if="$slots.icon" name="icon"></slot>
|
||||||
|
<VSpace>
|
||||||
|
<VButton @click="open()">
|
||||||
|
{{ $t("core.common.buttons.upload") }}
|
||||||
|
</VButton>
|
||||||
|
<VButton @click="openAttachmentSelector">
|
||||||
|
{{
|
||||||
|
$t(
|
||||||
|
"core.components.default_editor.extensions.upload.attachment.title"
|
||||||
|
)
|
||||||
|
}}</VButton
|
||||||
|
>
|
||||||
|
<VDropdown>
|
||||||
|
<VButton>{{
|
||||||
|
$t(
|
||||||
|
"core.components.default_editor.extensions.upload.permalink.title"
|
||||||
|
)
|
||||||
|
}}</VButton>
|
||||||
|
<template #popper>
|
||||||
|
<input
|
||||||
|
v-model="externalLink"
|
||||||
|
class="block w-full rounded-md border border-gray-300 bg-gray-50 px-2 py-1.5 text-sm text-gray-900 hover:bg-gray-100"
|
||||||
|
:placeholder="
|
||||||
|
i18n.global.t(
|
||||||
|
'core.components.default_editor.extensions.upload.permalink.placeholder'
|
||||||
|
)
|
||||||
|
"
|
||||||
|
@keydown.enter="handleEnterSetExternalLink"
|
||||||
|
/>
|
||||||
|
</template>
|
||||||
|
</VDropdown>
|
||||||
|
</VSpace>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</template>
|
|
@ -0,0 +1 @@
|
||||||
|
export { default as EditorLinkObtain } from "./EditorLinkObtain.vue";
|
|
@ -8,20 +8,10 @@ import {
|
||||||
import { NodeViewWrapper } from "@halo-dev/richtext-editor";
|
import { NodeViewWrapper } from "@halo-dev/richtext-editor";
|
||||||
import { computed, onMounted, ref } from "vue";
|
import { computed, onMounted, ref } from "vue";
|
||||||
import Image from "./index";
|
import Image from "./index";
|
||||||
import { watch } from "vue";
|
import { fileToBase64 } from "../../utils/upload";
|
||||||
import { fileToBase64, uploadFile } from "../../utils/upload";
|
import { VButton, IconImageAddLine } from "@halo-dev/components";
|
||||||
import type { Attachment } from "@halo-dev/api-client";
|
import { type AttachmentAttr } from "../../utils/attachment";
|
||||||
import type { AttachmentLike } from "@halo-dev/console-shared";
|
import { EditorLinkObtain } from "../../components";
|
||||||
import { useFileDialog } from "@vueuse/core";
|
|
||||||
import { onUnmounted } from "vue";
|
|
||||||
import {
|
|
||||||
VButton,
|
|
||||||
VSpace,
|
|
||||||
IconImageAddLine,
|
|
||||||
VDropdown,
|
|
||||||
} from "@halo-dev/components";
|
|
||||||
import { getAttachmentUrl } from "../../utils/attachment";
|
|
||||||
import { i18n } from "@/locales";
|
|
||||||
|
|
||||||
const props = defineProps<{
|
const props = defineProps<{
|
||||||
editor: Editor;
|
editor: Editor;
|
||||||
|
@ -63,98 +53,46 @@ const href = computed({
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
const originalFile = ref<File>();
|
|
||||||
const fileBase64 = ref<string>();
|
const fileBase64 = ref<string>();
|
||||||
const uploadProgress = ref<number | undefined>(undefined);
|
const uploadProgress = ref<number | undefined>(undefined);
|
||||||
const retryFlag = ref<boolean>(false);
|
const retryFlag = ref<boolean>(false);
|
||||||
const controller = ref<AbortController>();
|
const editorLinkObtain = ref();
|
||||||
const initSrc = ref<string>();
|
|
||||||
|
const handleUploadAbort = () => {
|
||||||
|
editorLinkObtain.value?.abort();
|
||||||
|
};
|
||||||
|
|
||||||
const initialization = computed(() => {
|
const initialization = computed(() => {
|
||||||
return !src.value && !fileBase64.value;
|
return !src.value && !fileBase64.value;
|
||||||
});
|
});
|
||||||
|
|
||||||
const handleEnterSetSrc = () => {
|
const handleUploadReady = async (file: File) => {
|
||||||
if (!initSrc.value) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
props.updateAttributes({ src: initSrc.value });
|
|
||||||
};
|
|
||||||
|
|
||||||
const openAttachmentSelector = () => {
|
|
||||||
props.editor.commands.openAttachmentSelector(
|
|
||||||
(attachments: AttachmentLike[]) => {
|
|
||||||
if (attachments.length > 0) {
|
|
||||||
const attachment = attachments[0];
|
|
||||||
const attachmentAttr = getAttachmentUrl(attachment);
|
|
||||||
props.updateAttributes({
|
|
||||||
src: attachmentAttr.url,
|
|
||||||
alt: attachmentAttr.name,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
accepts: ["image/*"],
|
|
||||||
min: 1,
|
|
||||||
max: 1,
|
|
||||||
}
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
const { open, reset, onChange } = useFileDialog({
|
|
||||||
accept: "image/*",
|
|
||||||
multiple: false,
|
|
||||||
});
|
|
||||||
|
|
||||||
const handleUploadAbort = () => {
|
|
||||||
if (!controller.value) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
controller.value.abort();
|
|
||||||
resetUpload();
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleUploadRetry = () => {
|
|
||||||
if (!originalFile.value) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
handleUploadImage(originalFile.value);
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleUploadImage = async (file: File) => {
|
|
||||||
originalFile.value = file;
|
|
||||||
fileBase64.value = await fileToBase64(file);
|
fileBase64.value = await fileToBase64(file);
|
||||||
retryFlag.value = false;
|
retryFlag.value = false;
|
||||||
controller.value = new AbortController();
|
};
|
||||||
uploadFile(file, props.extension.options.uploadImage, {
|
|
||||||
controller: controller.value,
|
|
||||||
onUploadProgress: (progress) => {
|
|
||||||
uploadProgress.value = progress;
|
|
||||||
},
|
|
||||||
|
|
||||||
onFinish: (attachment?: Attachment) => {
|
const handleSetExternalLink = (attachment: AttachmentAttr) => {
|
||||||
if (attachment) {
|
props.updateAttributes({
|
||||||
props.updateAttributes({
|
src: attachment.url,
|
||||||
src: attachment.status?.permalink,
|
alt: attachment.name,
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
resetUpload();
|
|
||||||
},
|
|
||||||
|
|
||||||
onError: () => {
|
|
||||||
retryFlag.value = true;
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const handleUploadRetry = () => {
|
||||||
|
editorLinkObtain.value?.retry();
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleUploadProgress = (progress: number) => {
|
||||||
|
uploadProgress.value = progress;
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleUploadError = () => {
|
||||||
|
retryFlag.value = true;
|
||||||
|
};
|
||||||
|
|
||||||
const resetUpload = () => {
|
const resetUpload = () => {
|
||||||
reset();
|
|
||||||
originalFile.value = undefined;
|
|
||||||
fileBase64.value = undefined;
|
fileBase64.value = undefined;
|
||||||
uploadProgress.value = undefined;
|
uploadProgress.value = undefined;
|
||||||
controller.value?.abort();
|
|
||||||
controller.value = undefined;
|
|
||||||
if (props.getPos()) {
|
if (props.getPos()) {
|
||||||
props.updateAttributes({
|
props.updateAttributes({
|
||||||
width: undefined,
|
width: undefined,
|
||||||
|
@ -164,35 +102,13 @@ const resetUpload = () => {
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleResetInit = () => {
|
const handleResetInit = () => {
|
||||||
resetUpload();
|
editorLinkObtain.value?.reset();
|
||||||
props.updateAttributes({
|
props.updateAttributes({
|
||||||
src: "",
|
src: "",
|
||||||
file: undefined,
|
file: undefined,
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
onChange((files) => {
|
|
||||||
if (!files) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (files.length > 0) {
|
|
||||||
handleUploadImage(files[0]);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
watch(
|
|
||||||
() => props.node?.attrs.file,
|
|
||||||
async (file) => {
|
|
||||||
if (!file) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
handleUploadImage(file);
|
|
||||||
},
|
|
||||||
{
|
|
||||||
immediate: true,
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
const aspectRatio = ref<number>(0);
|
const aspectRatio = ref<number>(0);
|
||||||
const resizeRef = ref<HTMLDivElement>();
|
const resizeRef = ref<HTMLDivElement>();
|
||||||
|
|
||||||
|
@ -238,10 +154,6 @@ onMounted(() => {
|
||||||
document.documentElement.removeEventListener("mouseup", stopDrag, false);
|
document.documentElement.removeEventListener("mouseup", stopDrag, false);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
onUnmounted(() => {
|
|
||||||
handleUploadAbort();
|
|
||||||
});
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
|
@ -275,7 +187,7 @@ onUnmounted(() => {
|
||||||
<VButton size="sm" type="secondary" @click="handleResetInit">
|
<VButton size="sm" type="secondary" @click="handleResetInit">
|
||||||
{{
|
{{
|
||||||
$t(
|
$t(
|
||||||
"core.components.default_editor.extensions.image.operations.replace.button"
|
"core.components.default_editor.extensions.upload.operations.replace.button"
|
||||||
)
|
)
|
||||||
}}
|
}}
|
||||||
</VButton>
|
</VButton>
|
||||||
|
@ -297,7 +209,7 @@ onUnmounted(() => {
|
||||||
>
|
>
|
||||||
{{
|
{{
|
||||||
$t(
|
$t(
|
||||||
"core.components.default_editor.extensions.image.upload.error"
|
"core.components.default_editor.extensions.upload.error"
|
||||||
)
|
)
|
||||||
}}
|
}}
|
||||||
</div>
|
</div>
|
||||||
|
@ -309,7 +221,7 @@ onUnmounted(() => {
|
||||||
>
|
>
|
||||||
{{
|
{{
|
||||||
$t(
|
$t(
|
||||||
"core.components.default_editor.extensions.image.upload.click_retry"
|
"core.components.default_editor.extensions.upload.click_retry"
|
||||||
)
|
)
|
||||||
}}
|
}}
|
||||||
</div>
|
</div>
|
||||||
|
@ -332,7 +244,7 @@ onUnmounted(() => {
|
||||||
uploadProgress
|
uploadProgress
|
||||||
? `${uploadProgress}%`
|
? `${uploadProgress}%`
|
||||||
: `${$t(
|
: `${$t(
|
||||||
"core.components.default_editor.extensions.image.upload.loading"
|
"core.components.default_editor.extensions.upload.loading"
|
||||||
)}...`
|
)}...`
|
||||||
}}
|
}}
|
||||||
</div>
|
</div>
|
||||||
|
@ -349,53 +261,28 @@ onUnmounted(() => {
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div v-else>
|
<div v-show="!src && !fileBase64">
|
||||||
<div class="flex w-full items-center justify-center">
|
<EditorLinkObtain
|
||||||
<div
|
ref="editorLinkObtain"
|
||||||
class="flex h-64 w-full cursor-pointer flex-col items-center justify-center border-2 border-dashed border-gray-300 bg-gray-50"
|
:accept="'image/*'"
|
||||||
>
|
:editor="editor"
|
||||||
|
:upload-to-attachment-file="extension.options.uploadImage"
|
||||||
|
:uploaded-file="node?.attrs.file"
|
||||||
|
@set-external-link="handleSetExternalLink"
|
||||||
|
@on-upload-ready="handleUploadReady"
|
||||||
|
@on-upload-progress="handleUploadProgress"
|
||||||
|
@on-upload-finish="resetUpload"
|
||||||
|
@on-upload-error="handleUploadError"
|
||||||
|
@on-upload-abort="resetUpload"
|
||||||
|
>
|
||||||
|
<template #icon>
|
||||||
<div
|
<div
|
||||||
class="flex flex-col items-center justify-center space-y-7 pb-6 pt-5"
|
class="flex h-14 w-14 items-center justify-center rounded-full bg-primary/20"
|
||||||
>
|
>
|
||||||
<div
|
<IconImageAddLine class="text-xl text-primary" />
|
||||||
class="flex h-14 w-14 items-center justify-center rounded-full bg-primary/20"
|
|
||||||
>
|
|
||||||
<IconImageAddLine class="text-xl text-primary" />
|
|
||||||
</div>
|
|
||||||
<VSpace>
|
|
||||||
<VButton @click="open()">
|
|
||||||
{{ $t("core.common.buttons.upload") }}
|
|
||||||
</VButton>
|
|
||||||
<VButton @click="openAttachmentSelector">
|
|
||||||
{{
|
|
||||||
$t(
|
|
||||||
"core.components.default_editor.extensions.image.attachment.title"
|
|
||||||
)
|
|
||||||
}}</VButton
|
|
||||||
>
|
|
||||||
<VDropdown>
|
|
||||||
<VButton>{{
|
|
||||||
$t(
|
|
||||||
"core.components.default_editor.extensions.image.permalink.title"
|
|
||||||
)
|
|
||||||
}}</VButton>
|
|
||||||
<template #popper>
|
|
||||||
<input
|
|
||||||
v-model="initSrc"
|
|
||||||
class="block w-full rounded-md border border-gray-300 bg-gray-50 px-2 py-1.5 text-sm text-gray-900 hover:bg-gray-100"
|
|
||||||
:placeholder="
|
|
||||||
i18n.global.t(
|
|
||||||
'core.components.default_editor.extensions.image.permalink.placeholder'
|
|
||||||
)
|
|
||||||
"
|
|
||||||
@keydown.enter="handleEnterSetSrc"
|
|
||||||
/>
|
|
||||||
</template>
|
|
||||||
</VDropdown>
|
|
||||||
</VSpace>
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</template>
|
||||||
</div>
|
</EditorLinkObtain>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</node-view-wrapper>
|
</node-view-wrapper>
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
import UiExtensionImage from "./image";
|
import UiExtensionImage from "./image";
|
||||||
|
import UiExtensionVideo from "./video";
|
||||||
import UiExtensionUpload from "./upload";
|
import UiExtensionUpload from "./upload";
|
||||||
export { UiExtensionImage, UiExtensionUpload };
|
export { UiExtensionImage, UiExtensionUpload, UiExtensionVideo };
|
||||||
|
|
|
@ -57,6 +57,10 @@ export const Upload = Extension.create({
|
||||||
if (files.length) {
|
if (files.length) {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
files.forEach((file: File) => {
|
files.forEach((file: File) => {
|
||||||
|
// TODO: For drag-and-drop uploaded files,
|
||||||
|
// perhaps it is necessary to determine the
|
||||||
|
// current position of the drag-and-drop
|
||||||
|
// instead of inserting them directly at the cursor.
|
||||||
handleFileEvent({ editor, file });
|
handleFileEvent({ editor, file });
|
||||||
});
|
});
|
||||||
return true;
|
return true;
|
||||||
|
|
|
@ -0,0 +1,209 @@
|
||||||
|
<script lang="ts" setup>
|
||||||
|
import type { PMNode, Decoration } from "@halo-dev/richtext-editor";
|
||||||
|
import type { Editor, Node } from "@halo-dev/richtext-editor";
|
||||||
|
import { NodeViewWrapper } from "@halo-dev/richtext-editor";
|
||||||
|
import { computed, ref } from "vue";
|
||||||
|
import type { AttachmentAttr } from "../../utils/attachment";
|
||||||
|
import RiVideoAddLine from "~icons/ri/video-add-line";
|
||||||
|
import { EditorLinkObtain } from "../../components";
|
||||||
|
import { VButton } from "@halo-dev/components";
|
||||||
|
|
||||||
|
const props = defineProps<{
|
||||||
|
editor: Editor;
|
||||||
|
node: PMNode;
|
||||||
|
decorations: Decoration[];
|
||||||
|
selected: boolean;
|
||||||
|
extension: Node;
|
||||||
|
getPos: () => number;
|
||||||
|
updateAttributes: (attributes: Record<string, unknown>) => void;
|
||||||
|
deleteNode: () => void;
|
||||||
|
}>();
|
||||||
|
|
||||||
|
const src = computed({
|
||||||
|
get: () => {
|
||||||
|
return props.node?.attrs.src;
|
||||||
|
},
|
||||||
|
set: (src: string) => {
|
||||||
|
props.updateAttributes({ src: src });
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const controls = computed(() => {
|
||||||
|
return props.node.attrs.controls;
|
||||||
|
});
|
||||||
|
|
||||||
|
const autoplay = computed(() => {
|
||||||
|
return props.node.attrs.autoplay;
|
||||||
|
});
|
||||||
|
|
||||||
|
const loop = computed(() => {
|
||||||
|
return props.node.attrs.loop;
|
||||||
|
});
|
||||||
|
|
||||||
|
const initialization = computed(() => {
|
||||||
|
return !src.value;
|
||||||
|
});
|
||||||
|
|
||||||
|
const editorLinkObtain = ref();
|
||||||
|
|
||||||
|
const handleSetExternalLink = (attachment: AttachmentAttr) => {
|
||||||
|
props.updateAttributes({
|
||||||
|
src: attachment.url,
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
const resetUpload = () => {
|
||||||
|
if (props.getPos()) {
|
||||||
|
props.updateAttributes({
|
||||||
|
width: undefined,
|
||||||
|
height: undefined,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleUploadRetry = () => {
|
||||||
|
editorLinkObtain.value?.reset();
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleUploadAbort = () => {
|
||||||
|
editorLinkObtain.value?.abort();
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleResetInit = () => {
|
||||||
|
editorLinkObtain.value?.reset();
|
||||||
|
props.updateAttributes({
|
||||||
|
src: "",
|
||||||
|
file: undefined,
|
||||||
|
});
|
||||||
|
};
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<template>
|
||||||
|
<node-view-wrapper as="div" class="inline-block w-full">
|
||||||
|
<div
|
||||||
|
class="relative inline-block h-full max-w-full overflow-hidden rounded-md text-center transition-all"
|
||||||
|
:class="{
|
||||||
|
'rounded ring-2': selected,
|
||||||
|
}"
|
||||||
|
:style="{
|
||||||
|
width: initialization ? '100%' : node.attrs.width,
|
||||||
|
}"
|
||||||
|
>
|
||||||
|
<div v-if="src" class="group relative">
|
||||||
|
<video
|
||||||
|
:src="src"
|
||||||
|
:controls="controls"
|
||||||
|
:autoplay="autoplay"
|
||||||
|
:loop="loop"
|
||||||
|
playsinline
|
||||||
|
preload="metadata"
|
||||||
|
class="m-0 rounded-md"
|
||||||
|
:style="{
|
||||||
|
width: node.attrs.width,
|
||||||
|
height: node.attrs.height,
|
||||||
|
}"
|
||||||
|
></video>
|
||||||
|
<div
|
||||||
|
v-if="src"
|
||||||
|
class="absolute left-0 top-0 hidden h-1/4 w-full cursor-pointer justify-end bg-gradient-to-b from-gray-300 to-transparent p-2 ease-in-out group-hover:flex"
|
||||||
|
>
|
||||||
|
<VButton size="sm" type="secondary" @click="handleResetInit">
|
||||||
|
{{
|
||||||
|
$t(
|
||||||
|
"core.components.default_editor.extensions.upload.operations.replace.button"
|
||||||
|
)
|
||||||
|
}}
|
||||||
|
</VButton>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div v-show="!src" class="relative">
|
||||||
|
<EditorLinkObtain
|
||||||
|
ref="editorLinkObtain"
|
||||||
|
:accept="'video/*'"
|
||||||
|
:editor="editor"
|
||||||
|
:upload-to-attachment-file="extension.options.uploadVideo"
|
||||||
|
:uploaded-file="node?.attrs.file"
|
||||||
|
@set-external-link="handleSetExternalLink"
|
||||||
|
@on-upload-finish="resetUpload"
|
||||||
|
@on-upload-abort="resetUpload"
|
||||||
|
>
|
||||||
|
<template #icon>
|
||||||
|
<div
|
||||||
|
class="flex h-14 w-14 items-center justify-center rounded-full bg-primary/20"
|
||||||
|
>
|
||||||
|
<RiVideoAddLine class="text-xl text-primary" />
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
<template #uploading="{ progress }">
|
||||||
|
<div class="absolute top-0 h-full w-full bg-black bg-opacity-20">
|
||||||
|
<div class="absolute top-[50%] w-full space-y-2 text-white">
|
||||||
|
<div class="px-10">
|
||||||
|
<div
|
||||||
|
class="relative h-4 w-full overflow-hidden rounded-full bg-gray-200"
|
||||||
|
>
|
||||||
|
<div
|
||||||
|
class="h-full bg-primary"
|
||||||
|
:style="{
|
||||||
|
width: `${progress || 0}%`,
|
||||||
|
}"
|
||||||
|
></div>
|
||||||
|
<div
|
||||||
|
class="absolute left-[50%] top-0 -translate-x-[50%] text-xs leading-4 text-white"
|
||||||
|
>
|
||||||
|
{{
|
||||||
|
progress
|
||||||
|
? `${progress}%`
|
||||||
|
: `${$t(
|
||||||
|
"core.components.default_editor.extensions.upload.loading"
|
||||||
|
)}...`
|
||||||
|
}}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div
|
||||||
|
class="inline-block cursor-pointer text-sm hover:opacity-70"
|
||||||
|
@click="handleUploadAbort"
|
||||||
|
>
|
||||||
|
{{ $t("core.common.buttons.cancel") }}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
<template #error>
|
||||||
|
<div class="absolute top-0 h-full w-full bg-black bg-opacity-20">
|
||||||
|
<div class="absolute top-[50%] w-full space-y-2 text-white">
|
||||||
|
<div class="px-10">
|
||||||
|
<div
|
||||||
|
class="relative h-4 w-full overflow-hidden rounded-full bg-gray-200"
|
||||||
|
>
|
||||||
|
<div class="h-full w-full bg-red-600"></div>
|
||||||
|
<div
|
||||||
|
class="absolute left-[50%] top-0 -translate-x-[50%] text-xs leading-4 text-white"
|
||||||
|
>
|
||||||
|
{{
|
||||||
|
$t(
|
||||||
|
"core.components.default_editor.extensions.upload.error"
|
||||||
|
)
|
||||||
|
}}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div
|
||||||
|
class="inline-block cursor-pointer text-sm hover:opacity-70"
|
||||||
|
@click="handleUploadRetry"
|
||||||
|
>
|
||||||
|
{{
|
||||||
|
$t(
|
||||||
|
"core.components.default_editor.extensions.upload.click_retry"
|
||||||
|
)
|
||||||
|
}}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
</EditorLinkObtain>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</node-view-wrapper>
|
||||||
|
</template>
|
|
@ -0,0 +1,36 @@
|
||||||
|
import { ExtensionVideo, VueNodeViewRenderer } from "@halo-dev/richtext-editor";
|
||||||
|
import type { AxiosRequestConfig } from "axios";
|
||||||
|
import type { Attachment } from "@halo-dev/api-client";
|
||||||
|
import VideoView from "./VideoView.vue";
|
||||||
|
|
||||||
|
interface UiVideoOptions {
|
||||||
|
uploadVideo?: (
|
||||||
|
file: File,
|
||||||
|
options?: AxiosRequestConfig
|
||||||
|
) => Promise<Attachment>;
|
||||||
|
}
|
||||||
|
|
||||||
|
const Video = ExtensionVideo.extend<UiVideoOptions>({
|
||||||
|
addOptions() {
|
||||||
|
const { parent } = this;
|
||||||
|
return {
|
||||||
|
...parent?.(),
|
||||||
|
uploadVideo: undefined,
|
||||||
|
};
|
||||||
|
},
|
||||||
|
|
||||||
|
addAttributes() {
|
||||||
|
return {
|
||||||
|
...this.parent?.(),
|
||||||
|
file: {
|
||||||
|
default: null,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
},
|
||||||
|
|
||||||
|
addNodeView() {
|
||||||
|
return VueNodeViewRenderer(VideoView);
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
export default Video;
|
|
@ -2,6 +2,7 @@
|
||||||
import { CoreEditor } from "@halo-dev/richtext-editor";
|
import { CoreEditor } from "@halo-dev/richtext-editor";
|
||||||
import type { Attachment } from "@halo-dev/api-client";
|
import type { Attachment } from "@halo-dev/api-client";
|
||||||
import Image from "../extensions/image";
|
import Image from "../extensions/image";
|
||||||
|
import ExtensionVideo from "../extensions/video";
|
||||||
import type { AxiosRequestConfig } from "axios";
|
import type { AxiosRequestConfig } from "axios";
|
||||||
|
|
||||||
export interface FileProps {
|
export interface FileProps {
|
||||||
|
@ -25,6 +26,11 @@ export const handleFileEvent = ({ file, editor }: FileProps) => {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (file.type.startsWith("video/")) {
|
||||||
|
uploadVideo({ file, editor });
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -41,6 +47,19 @@ export const uploadImage = ({ file, editor }: FileProps) => {
|
||||||
editor.view.dispatch(editor.view.state.tr.replaceSelectionWith(node));
|
editor.view.dispatch(editor.view.state.tr.replaceSelectionWith(node));
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Uploads a video file and inserts it into the editor.
|
||||||
|
*
|
||||||
|
* @param {FileProps} { file, editor } - File to be uploaded and the editor instance
|
||||||
|
*/
|
||||||
|
export const uploadVideo = ({ file, editor }: FileProps) => {
|
||||||
|
const { view } = editor;
|
||||||
|
const node = view.props.state.schema.nodes[ExtensionVideo.name].create({
|
||||||
|
file: file,
|
||||||
|
});
|
||||||
|
editor.view.dispatch(editor.view.state.tr.replaceSelectionWith(node));
|
||||||
|
};
|
||||||
|
|
||||||
export interface UploadFetchResponse {
|
export interface UploadFetchResponse {
|
||||||
controller: AbortController;
|
controller: AbortController;
|
||||||
onUploadProgress: (progress: number) => void;
|
onUploadProgress: (progress: number) => void;
|
||||||
|
|
|
@ -1397,11 +1397,10 @@ core:
|
||||||
placeholder:
|
placeholder:
|
||||||
options:
|
options:
|
||||||
placeholder: Enter / to select input type.
|
placeholder: Enter / to select input type.
|
||||||
image:
|
upload:
|
||||||
upload:
|
error: Upload failed
|
||||||
error: Upload failed
|
click_retry: Click to retry
|
||||||
click_retry: Click to retry
|
loading: Loading
|
||||||
loading: Loading
|
|
||||||
attachment:
|
attachment:
|
||||||
title: Attachment Library
|
title: Attachment Library
|
||||||
permalink:
|
permalink:
|
||||||
|
|
|
@ -1345,11 +1345,10 @@ core:
|
||||||
placeholder:
|
placeholder:
|
||||||
options:
|
options:
|
||||||
placeholder: 输入 / 以选择输入类型
|
placeholder: 输入 / 以选择输入类型
|
||||||
image:
|
upload:
|
||||||
upload:
|
error: 上传失败
|
||||||
error: 上传失败
|
click_retry: 点击重试
|
||||||
click_retry: 点击重试
|
loading: 等待中
|
||||||
loading: 等待中
|
|
||||||
attachment:
|
attachment:
|
||||||
title: 附件库
|
title: 附件库
|
||||||
permalink:
|
permalink:
|
||||||
|
|
|
@ -1311,11 +1311,10 @@ core:
|
||||||
placeholder:
|
placeholder:
|
||||||
options:
|
options:
|
||||||
placeholder: 輸入 / 以選擇輸入類型
|
placeholder: 輸入 / 以選擇輸入類型
|
||||||
image:
|
upload:
|
||||||
upload:
|
error: 上傳失敗
|
||||||
error: 上傳失敗
|
click_retry: 點擊重試
|
||||||
click_retry: 點擊重試
|
loading: 等待中
|
||||||
loading: 等待中
|
|
||||||
attachment:
|
attachment:
|
||||||
title: 附件庫
|
title: 附件庫
|
||||||
permalink:
|
permalink:
|
||||||
|
|
Loading…
Reference in New Issue