refactor editor
This commit is contained in:
@@ -0,0 +1,149 @@
|
||||
<template>
|
||||
<t-button v-popup="'插入内嵌网页'" variant="outline" :theme="active ? 'primary' : 'default'" @click="openDialog">
|
||||
<t-icon name="web" />
|
||||
</t-button>
|
||||
<t-popup
|
||||
v-model="visible"
|
||||
placement="bottom"
|
||||
destroy-on-close
|
||||
>
|
||||
<div class="form-popup">
|
||||
<div class="popup-head">
|
||||
<span class="title">插入内嵌网页</span>
|
||||
<t-button shape="square" size="small" variant="text" title="关闭" @click="visible = false">
|
||||
<t-icon name="close" />
|
||||
</t-button>
|
||||
</div>
|
||||
<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 class="popup-actions">
|
||||
<t-button variant="outline" block @click="visible = false">取消</t-button>
|
||||
<t-button theme="primary" block @click="onConfirm">插入</t-button>
|
||||
</div>
|
||||
</div>
|
||||
</t-popup>
|
||||
</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 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);
|
||||
|
||||
.popup-head {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
margin-bottom: 1rem;
|
||||
|
||||
.title {
|
||||
font-size: 1rem;
|
||||
font-weight: 600;
|
||||
color: var(--td-text-color-primary, #000);
|
||||
}
|
||||
}
|
||||
|
||||
.form-content {
|
||||
:deep(.t-form__item) {
|
||||
margin-bottom: .875rem;
|
||||
}
|
||||
}
|
||||
|
||||
.popup-actions {
|
||||
gap: .75rem;
|
||||
display: flex;
|
||||
margin-top: 1rem;
|
||||
|
||||
:deep(.t-button) {
|
||||
flex: 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
</style>
|
||||
Reference in New Issue
Block a user