Files
timi-tdesign-mobile/src/components/tiptap-editor/TiptapIframeButton.vue
T
2026-11-16 17:46:25 +08:00

161 lines
3.5 KiB
Vue

<template>
<t-button :variant="active ? 'base' : 'text'" :theme="active ? 'light' : 'default'" @click="openDialog">
<t-icon name="web" />
</t-button>
<bottom-popup
v-model="visible"
placement="bottom"
title="插入内嵌网页"
left-button-text="取消"
right-button-text="插入"
@left-button-click="visible = false"
@right-button-click="onConfirm"
destroy-on-close
>
<t-form class="popup" label-align="left" label-width="auto">
<t-form-item label="网页地址">
<t-input
v-model="iframeForm.src"
placeholder="请输入网页地址"
borderless
/>
</t-form-item>
<t-form-item label="网页标题">
<t-input
v-model="iframeForm.title"
placeholder="请输入网页标题,可留空"
borderless
/>
</t-form-item>
<t-form-item label="宽度">
<div class="field">
<t-input
v-model="iframeForm.width"
placeholder="请输入宽度,默认 100%"
borderless
/>
<t-button size="extra-small" variant="outline" @click="setWidth('auto')">
自动
</t-button>
<t-button size="extra-small" variant="outline" @click="setWidth('100%')">
铺满
</t-button>
</div>
</t-form-item>
<t-form-item label="高度">
<div class="field">
<t-input
v-model="iframeForm.height"
placeholder="请输入高度,默认 28rem"
borderless
/>
<t-button size="extra-small" variant="outline" @click="setHeight">
自动
</t-button>
</div>
</t-form-item>
</t-form>
</bottom-popup>
</template>
<script lang="ts" setup>
import { showPlatformMessage } from "../message";
import type { TiptapEditorInstance } from "timi-web/editor";
import BottomPopup from "../bottom-popup/index.vue";
defineOptions({
name: "TiptapIframeButton"
});
const props = defineProps<{
editor?: TiptapEditorInstance;
active: boolean;
}>();
const visible = ref(false);
const iframeForm = reactive({
src: "",
title: "",
width: "100%",
height: "28rem"
});
function setWidth(width: "auto" | "100%") {
iframeForm.width = width;
}
function setHeight() {
iframeForm.height = "auto";
}
function openDialog() {
if (!props.editor) {
return;
}
const attributes = props.editor.getAttributes("iframe");
iframeForm.src = (attributes.src as string) || "https://";
iframeForm.title = (attributes.title as string) || "";
iframeForm.width = (attributes.width as string) || "100%";
iframeForm.height = (attributes.height as string) || "28rem";
visible.value = true;
}
function isValidHttpUrl(value: string) {
try {
const url = new URL(value);
return url.protocol === "http:" || url.protocol === "https:";
} catch {
return false;
}
}
async function onConfirm() {
if (!props.editor) {
return;
}
const src = iframeForm.src.trim();
if (!src) {
visible.value = false;
return;
}
if (!isValidHttpUrl(src)) {
await showPlatformMessage("warning", "请输入有效的 http 或 https 地址");
return;
}
props.editor
.chain()
.focus()
.insertContent({
type: "iframe",
attrs: {
src,
title: iframeForm.title.trim() || "内嵌网页",
width: iframeForm.width.trim() || "100%",
height: iframeForm.height.trim() || "28rem"
}
})
.run();
visible.value = false;
}
</script>
<style lang="less" scoped>
.popup {
margin: 0;
padding: 0 1rem calc(1rem + var(--tui-safe-bottom, 0px));
background: var(--td-bg-color-container);
.field {
gap: .5rem;
width: 100%;
display: flex;
align-items: center;
:deep(.t-input) {
min-width: 0;
flex: 1;
}
}
}
</style>