113 lines
2.8 KiB
Vue
113 lines
2.8 KiB
Vue
<template>
|
|
<t-popconfirm
|
|
class="tiptap-iframe-button"
|
|
v-model:visible="visible"
|
|
trigger="click"
|
|
:confirm-btn="{ content: '插入', theme: 'primary' }"
|
|
:cancel-btn="{ content: '取消', theme: 'default' }"
|
|
placement="bottom"
|
|
@visible-change="onVisibleChange"
|
|
@confirm="onConfirm"
|
|
>
|
|
<template #icon><span></span></template>
|
|
<template #content>
|
|
<t-form class="popconfirm-content" label-align="left" label-width="auto">
|
|
<t-form-item label="网页地址">
|
|
<t-input v-model="iframeForm.src" placeholder="请输入网页地址" />
|
|
</t-form-item>
|
|
<t-form-item label="网页标题">
|
|
<t-input v-model="iframeForm.title" placeholder="请输入网页标题,可留空" />
|
|
</t-form-item>
|
|
<t-form-item label="宽度">
|
|
<t-input v-model="iframeForm.width" placeholder="请输入宽度,默认 100%" />
|
|
</t-form-item>
|
|
<t-form-item label="高度">
|
|
<t-input v-model="iframeForm.height" placeholder="请输入高度,默认 28rem" />
|
|
</t-form-item>
|
|
</t-form>
|
|
</template>
|
|
<t-button v-popup="'插入内嵌网页'" variant="outline" :theme="active ? 'primary' : 'default'">
|
|
<t-icon name="web" />
|
|
</t-button>
|
|
</t-popconfirm>
|
|
</template>
|
|
|
|
<script lang="ts" setup>
|
|
import { showPlatformMessage } from "../message";
|
|
import type { TiptapEditorInstance } from "@timi/web/editor";
|
|
|
|
defineOptions({
|
|
name: "TiptapIframeButton"
|
|
});
|
|
|
|
const props = defineProps<{
|
|
editor?: TiptapEditorInstance;
|
|
active: boolean;
|
|
}>();
|
|
|
|
const visible = ref(false);
|
|
const iframeForm = reactive({
|
|
src: "",
|
|
title: "",
|
|
width: "100%",
|
|
height: "28rem"
|
|
});
|
|
|
|
function onVisibleChange(nextVisible: boolean) {
|
|
if (!nextVisible || !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";
|
|
}
|
|
|
|
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>
|
|
.popconfirm-content {
|
|
--td-comp-margin-xxl: .5rem;
|
|
|
|
padding: 0 .5rem;
|
|
}
|
|
</style>
|