124 lines
3.0 KiB
Vue
124 lines
3.0 KiB
Vue
<template>
|
|
<t-button v-popup="'插入内嵌网页'" :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
|
|
>
|
|
<div class="form-popup">
|
|
<t-form class="form-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>
|
|
</div>
|
|
</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 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>
|
|
.form-popup {
|
|
width: 100%;
|
|
max-height: 85vh;
|
|
padding: 1rem 1rem calc(1rem + env(safe-area-inset-bottom));
|
|
box-sizing: border-box;
|
|
overflow-y: auto;
|
|
background: var(--td-bg-color-container);
|
|
|
|
.form-content {
|
|
:deep(.t-form__item) {
|
|
margin-bottom: .875rem;
|
|
}
|
|
}
|
|
|
|
}
|
|
</style>
|