添加流程图功能
This commit is contained in:
@@ -65,6 +65,8 @@ import { AddRowBefore } from "./table/AddRowBefore.ts";
|
||||
import { DeleteRow } from "./table/DeleteRow.ts";
|
||||
import { DeleteTable } from "./table/DeleteTable.ts";
|
||||
|
||||
import { Diagrams } from "./tools/Diagrams.ts";
|
||||
|
||||
import { ToggleToc } from "./page/ToggleToc.ts";
|
||||
|
||||
/**
|
||||
@@ -90,6 +92,10 @@ export class Classic extends HTMLElement implements UAIEditorEventListener {
|
||||
classicMenuTableScrollable!: ScrollableDiv;
|
||||
classicMenuTableGroup!: HTMLElement;
|
||||
|
||||
// 工具菜单容器
|
||||
classicMenuToolScrollable!: ScrollableDiv;
|
||||
classicMenuToolGroup!: HTMLElement;
|
||||
|
||||
// 页面菜单容器
|
||||
classicMenuPageScrollable!: ScrollableDiv;
|
||||
classicMenuPageGroup!: HTMLElement;
|
||||
@@ -151,6 +157,9 @@ export class Classic extends HTMLElement implements UAIEditorEventListener {
|
||||
tableMenuDeleteRow!: DeleteRow;
|
||||
tableMenuDeleteTable!: DeleteTable;
|
||||
|
||||
// 工具菜单
|
||||
toolsMenuDiagrams!: Diagrams;
|
||||
|
||||
// 页面菜单
|
||||
pageMenuToggleToc!: ToggleToc;
|
||||
|
||||
@@ -200,6 +209,7 @@ export class Classic extends HTMLElement implements UAIEditorEventListener {
|
||||
this.classicMenuBaseScrollable.style.display = "none";
|
||||
this.classicMenuInsertScrollable.style.display = "none";
|
||||
this.classicMenuTableScrollable.style.display = "none";
|
||||
this.classicMenuToolScrollable.style.display = "none";
|
||||
this.classicMenuPageScrollable.style.display = "none";
|
||||
if (menu === "base") {
|
||||
this.classicMenuBaseScrollable.style.display = "flex";
|
||||
@@ -210,6 +220,9 @@ export class Classic extends HTMLElement implements UAIEditorEventListener {
|
||||
if (menu === "table") {
|
||||
this.classicMenuTableScrollable.style.display = "flex";
|
||||
}
|
||||
if (menu === "tools") {
|
||||
this.classicMenuToolScrollable.style.display = "flex";
|
||||
}
|
||||
if (menu === "page") {
|
||||
this.classicMenuPageScrollable.style.display = "flex";
|
||||
}
|
||||
@@ -218,6 +231,7 @@ export class Classic extends HTMLElement implements UAIEditorEventListener {
|
||||
this.createBaseMenu(event, options);
|
||||
this.createInsertMenu(event, options);
|
||||
this.createTableMenu(event, options);
|
||||
this.createToolMenu(event, options);
|
||||
this.createPageMenu(event, options);
|
||||
this.classicMenuBaseScrollable.style.display = "flex";
|
||||
}
|
||||
@@ -384,6 +398,9 @@ export class Classic extends HTMLElement implements UAIEditorEventListener {
|
||||
this.tableMenuDeleteTable = new DeleteTable({ menuType: "button", enable: true, header: "classic", hideText: false });
|
||||
this.eventComponents.push(this.tableMenuDeleteTable);
|
||||
|
||||
this.toolsMenuDiagrams = new Diagrams({ menuType: "button", enable: true, header: "classic", hideText: false });
|
||||
this.eventComponents.push(this.toolsMenuDiagrams);
|
||||
|
||||
this.pageMenuToggleToc = new ToggleToc({ menuType: "button", enable: true, header: "classic", hideText: false });
|
||||
this.eventComponents.push(this.pageMenuToggleToc);
|
||||
}
|
||||
@@ -516,6 +533,25 @@ export class Classic extends HTMLElement implements UAIEditorEventListener {
|
||||
group3.appendChild(this.tableMenuDeleteTable);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建工具菜单
|
||||
* @param event
|
||||
* @param options
|
||||
*/
|
||||
createToolMenu(event: EditorEvents["create"], options: UAIEditorOptions) {
|
||||
this.classicMenuToolGroup = document.createElement("div");
|
||||
this.classicMenuToolGroup.style.display = "flex";
|
||||
this.classicMenuToolScrollable = new ScrollableDiv(this.classicMenuToolGroup);
|
||||
this.classicMenuToolScrollable.style.display = "none";
|
||||
this.classicMenu.appendChild(this.classicMenuToolScrollable);
|
||||
this.classicMenuToolScrollable.onCreate(event, options);
|
||||
|
||||
const group1 = document.createElement("div");
|
||||
group1.classList.add("uai-classic-virtual-group");
|
||||
this.classicMenuToolGroup.appendChild(group1);
|
||||
group1.appendChild(this.toolsMenuDiagrams);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建页面菜单
|
||||
* @param event
|
||||
|
||||
@@ -67,6 +67,8 @@ import { AddRowBefore } from "./table/AddRowBefore.ts";
|
||||
import { DeleteRow } from "./table/DeleteRow.ts";
|
||||
import { DeleteTable } from "./table/DeleteTable.ts";
|
||||
|
||||
import { Diagrams } from "./tools/Diagrams.ts";
|
||||
|
||||
import { ToggleToc } from "./page/ToggleToc.ts";
|
||||
|
||||
/**
|
||||
@@ -93,6 +95,10 @@ export class Ribbon extends HTMLElement implements UAIEditorEventListener {
|
||||
ribbonMenuTableScrollable!: ScrollableDiv;
|
||||
ribbonMenuTableGroup!: HTMLElement;
|
||||
|
||||
// 工具菜单容器
|
||||
ribbonMenuToolScrollable!: ScrollableDiv;
|
||||
ribbonMenuToolGroup!: HTMLElement;
|
||||
|
||||
// 页面菜单容器
|
||||
ribbonMenuPageScrollable!: ScrollableDiv;
|
||||
ribbonMenuPageGroup!: HTMLElement;
|
||||
@@ -154,6 +160,9 @@ export class Ribbon extends HTMLElement implements UAIEditorEventListener {
|
||||
tableMenuDeleteRow!: DeleteRow;
|
||||
tableMenuDeleteTable!: DeleteTable;
|
||||
|
||||
// 工具菜单
|
||||
toolsMenuDiagrams!: Diagrams;
|
||||
|
||||
// 页面菜单
|
||||
pageMenuToggleToc!: ToggleToc;
|
||||
|
||||
@@ -196,6 +205,7 @@ export class Ribbon extends HTMLElement implements UAIEditorEventListener {
|
||||
this.ribbonMenuBaseScrollable.style.display = "none";
|
||||
this.ribbonMenuInsertScrollable.style.display = "none";
|
||||
this.ribbonMenuTableScrollable.style.display = "none";
|
||||
this.ribbonMenuToolScrollable.style.display = "none";
|
||||
this.ribbonMenuPageScrollable.style.display = "none";
|
||||
if (menu.value === "base") {
|
||||
this.ribbonMenuBaseScrollable.style.display = "flex";
|
||||
@@ -206,6 +216,9 @@ export class Ribbon extends HTMLElement implements UAIEditorEventListener {
|
||||
if (menu.value === "table") {
|
||||
this.ribbonMenuTableScrollable.style.display = "flex";
|
||||
}
|
||||
if (menu.value === "tools") {
|
||||
this.ribbonMenuToolScrollable.style.display = "flex";
|
||||
}
|
||||
if (menu.value === "page") {
|
||||
this.ribbonMenuPageScrollable.style.display = "flex";
|
||||
}
|
||||
@@ -229,6 +242,7 @@ export class Ribbon extends HTMLElement implements UAIEditorEventListener {
|
||||
this.createBaseMenu(event, options);
|
||||
this.createInsertMenu(event, options);
|
||||
this.createTableMenu(event, options);
|
||||
this.createToolMenu(event, options);
|
||||
this.createPageMenu(event, options);
|
||||
this.ribbonMenuBaseScrollable.style.display = "flex";
|
||||
}
|
||||
@@ -407,6 +421,9 @@ export class Ribbon extends HTMLElement implements UAIEditorEventListener {
|
||||
this.tableMenuDeleteTable = new DeleteTable({ menuType: "button", enable: true, huge: true, hideText: false });
|
||||
this.eventComponents.push(this.tableMenuDeleteTable);
|
||||
|
||||
this.toolsMenuDiagrams = new Diagrams({ menuType: "button", enable: true, huge: true, hideText: false });
|
||||
this.eventComponents.push(this.toolsMenuDiagrams);
|
||||
|
||||
this.pageMenuToggleToc = new ToggleToc({ menuType: "button", enable: true, huge: true, hideText: false });
|
||||
this.eventComponents.push(this.pageMenuToggleToc);
|
||||
}
|
||||
@@ -621,6 +638,25 @@ export class Ribbon extends HTMLElement implements UAIEditorEventListener {
|
||||
group3.appendChild(this.tableMenuDeleteTable);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建工具菜单
|
||||
* @param event
|
||||
* @param options
|
||||
*/
|
||||
createToolMenu(_event: EditorEvents["create"], _options: UAIEditorOptions) {
|
||||
this.ribbonMenuToolGroup = document.createElement("div");
|
||||
this.ribbonMenuToolGroup.classList.add("uai-ribbon-container");
|
||||
this.ribbonMenuToolGroup.style.display = "flex";
|
||||
this.ribbonMenuToolScrollable = new ScrollableDiv(this.ribbonMenuToolGroup);
|
||||
this.ribbonMenuToolScrollable.style.display = "none";
|
||||
this.ribbonScrollableContainer.appendChild(this.ribbonMenuToolScrollable);
|
||||
|
||||
const group1 = document.createElement("div");
|
||||
group1.classList.add("uai-ribbon-virtual-group");
|
||||
this.ribbonMenuToolGroup.appendChild(group1);
|
||||
group1.appendChild(this.toolsMenuDiagrams);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建页面菜单
|
||||
* @param event
|
||||
|
||||
@@ -0,0 +1,139 @@
|
||||
// Copyright (c) 2024-present AI-Labs
|
||||
|
||||
// @ ts-nocheck
|
||||
import { MenuButton, MenuButtonOptions } from "../../MenuButton.ts";
|
||||
import icon from "../../../../assets/icons/diagrams.svg";
|
||||
import { t } from "i18next";
|
||||
import { UAIEditorEventListener, UAIEditorOptions, InnerEditor } from "../../../../core/UAIEditor.ts";
|
||||
import { EditorEvents } from "@tiptap/core";
|
||||
import { FullScreenModal } from "../../../modals/FullScreenModal.ts";
|
||||
import DiagramEditor from "../../../../utils/DiagramEditor.ts";
|
||||
import { uuid } from "../../../../utils/UUID.ts";
|
||||
|
||||
/**
|
||||
* 工具菜单:插入流程图
|
||||
*/
|
||||
export class Diagrams extends HTMLElement implements UAIEditorEventListener {
|
||||
// 按钮选项
|
||||
menuButtonOptions: MenuButtonOptions = {
|
||||
menuType: "button",
|
||||
enable: true,
|
||||
icon: icon,
|
||||
hideText: false,
|
||||
text: t('tools.diagrams.text'),
|
||||
tooltip: t('tools.diagrams.text'),
|
||||
}
|
||||
|
||||
// 功能按钮
|
||||
menuButton: MenuButton;
|
||||
// 模态框
|
||||
modal: FullScreenModal;
|
||||
|
||||
constructor(options: MenuButtonOptions) {
|
||||
super();
|
||||
|
||||
// 初始化功能按钮选项
|
||||
this.menuButtonOptions = { ...this.menuButtonOptions, ...options };
|
||||
|
||||
// 创建功能按钮
|
||||
this.menuButton = new MenuButton(this.menuButtonOptions);
|
||||
|
||||
// 创建模态框
|
||||
this.modal = new FullScreenModal(this.menuButtonOptions.text!);
|
||||
}
|
||||
|
||||
/**
|
||||
* 定义创建方法
|
||||
* @param event
|
||||
* @param options
|
||||
*/
|
||||
onCreate(event: EditorEvents["create"], options: UAIEditorOptions) {
|
||||
this.menuButton.onCreate(event, options);
|
||||
this.appendChild(this.menuButton);
|
||||
// 创建模态框容器
|
||||
const container = document.createElement("div");
|
||||
container.classList.add("uai-diagrams-container");
|
||||
container.classList.add("uai-diagrams-container-insert");
|
||||
this.modal.modalBody.appendChild(container);
|
||||
const editor = event.editor;
|
||||
|
||||
// 添加鼠标点击事件,弹出模态框
|
||||
this.addEventListener("click", () => {
|
||||
if (this.menuButtonOptions.enable) {
|
||||
container.innerHTML = "";
|
||||
const diagramEditor = new DiagramEditor({
|
||||
container: `.uai-diagrams-container-insert`,
|
||||
})
|
||||
this.modal.show();
|
||||
diagramEditor.edit('');
|
||||
|
||||
// 添加回调消息监听
|
||||
const messageListener = (evt: MessageEvent) => {
|
||||
if (
|
||||
evt?.type !== 'message' ||
|
||||
typeof evt?.data !== 'string'
|
||||
) {
|
||||
return
|
||||
}
|
||||
|
||||
// 获取回调消息内容
|
||||
const { event, bounds, data } = JSON.parse(evt.data);
|
||||
if (event === 'export') {
|
||||
const { width, height } = bounds
|
||||
const name = `diagrams-${uuid()}.svg`
|
||||
// 将回调消息转换成图片
|
||||
const { size } = new Blob([data], {
|
||||
type: 'image/svg+xml',
|
||||
})
|
||||
const image = {
|
||||
type: 'diagrams',
|
||||
name,
|
||||
size,
|
||||
src: data,
|
||||
width: `${width}`,
|
||||
height,
|
||||
content: data,
|
||||
}
|
||||
this.modal.hide();
|
||||
|
||||
// 在编辑器中插入图片
|
||||
diagramEditor.stopEditing();
|
||||
if (image?.type) {
|
||||
editor.chain().focus().setImage(image, false).run()
|
||||
}
|
||||
window.removeEventListener('message', messageListener);
|
||||
}
|
||||
if (event === 'exit') {
|
||||
// 如果回调消息是关闭,则关闭模态框
|
||||
this.modal.hide();
|
||||
window.removeEventListener('message', messageListener);
|
||||
}
|
||||
}
|
||||
|
||||
// 移除、添加监听
|
||||
window.removeEventListener('message', messageListener);
|
||||
window.addEventListener('message', messageListener);
|
||||
this.modal.registerListener(messageListener);
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 定义Transaction监听方法
|
||||
* @param event
|
||||
* @param options
|
||||
*/
|
||||
onTransaction(event: EditorEvents["transaction"], options: UAIEditorOptions) {
|
||||
this.menuButton.onTransaction(event, options);
|
||||
if (this.menuButton.menuButton) {
|
||||
var disable = event.editor.isEditable;
|
||||
this.onEditableChange(disable);
|
||||
}
|
||||
}
|
||||
|
||||
onEditableChange(editable: boolean) {
|
||||
this.menuButtonOptions.enable = editable;
|
||||
this.menuButton.onEditableChange(editable);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user