添加插入数学公式菜单

This commit is contained in:
wux_labs
2025-02-18 21:25:48 +08:00
parent 501e6e33eb
commit 8d513d1191
5 changed files with 94 additions and 0 deletions
+6
View File
@@ -53,6 +53,7 @@ import { Audio } from "./insert/Audio.ts";
import { HardBreak } from "./insert/HardBreak.ts";
import { Emoji } from "./insert/Emoji.ts";
import { Symbol } from "./insert/Symbol.ts";
import { Math } from "./insert/Math.ts";
/**
* 传统菜单栏
@@ -117,6 +118,7 @@ export class Classic extends HTMLElement implements UAIEditorEventListener {
insertMenuHardBreak!: HardBreak;
insertMenuEmoji!: Emoji;
insertMenuSymbol!: Symbol;
insertMenuMath!: Math;
constructor(defaultToolbarMenus: Record<string, any>[]) {
super();
@@ -307,6 +309,9 @@ export class Classic extends HTMLElement implements UAIEditorEventListener {
this.insertMenuSymbol = new Symbol({ menuType: "popup", enable: true, header: "classic", hideText: false });
this.eventComponents.push(this.insertMenuSymbol);
this.insertMenuMath = new Math({ menuType: "button", enable: true, header: "classic", hideText: false });
this.eventComponents.push(this.insertMenuMath);
}
/**
* 创建基础菜单
@@ -395,5 +400,6 @@ export class Classic extends HTMLElement implements UAIEditorEventListener {
group2.appendChild(this.insertMenuHardBreak);
group2.appendChild(this.insertMenuEmoji);
group2.appendChild(this.insertMenuSymbol);
group2.appendChild(this.insertMenuMath);
}
}
+6
View File
@@ -55,6 +55,7 @@ import { Audio } from "./insert/Audio.ts";
import { HardBreak } from "./insert/HardBreak.ts";
import { Emoji } from "./insert/Emoji.ts";
import { Symbol } from "./insert/Symbol.ts";
import { Math } from "./insert/Math.ts";
/**
* 经典菜单栏
@@ -120,6 +121,7 @@ export class Ribbon extends HTMLElement implements UAIEditorEventListener {
insertMenuHardBreak!: HardBreak;
insertMenuEmoji!: Emoji;
insertMenuSymbol!: Symbol;
insertMenuMath!: Math;
constructor(defaultToolbarMenus: Record<string, any>[]) {
super();
@@ -330,6 +332,9 @@ export class Ribbon extends HTMLElement implements UAIEditorEventListener {
this.insertMenuSymbol = new Symbol({ menuType: "popup", enable: true, huge: true, hideText: false });
this.eventComponents.push(this.insertMenuSymbol);
this.insertMenuMath = new Math({ menuType: "button", enable: true, huge: true });
this.eventComponents.push(this.insertMenuMath);
}
/**
@@ -492,5 +497,6 @@ export class Ribbon extends HTMLElement implements UAIEditorEventListener {
group2.appendChild(this.insertMenuHardBreak);
group2.appendChild(this.insertMenuEmoji);
group2.appendChild(this.insertMenuSymbol);
group2.appendChild(this.insertMenuMath);
}
}
@@ -0,0 +1,72 @@
// Copyright (c) 2024-present AI-Labs
// @ ts-nocheck
import { MenuButton, MenuButtonOptions } from "../../MenuButton.ts";
import icon from "../../../../assets/icons/math.svg";
import { t } from "i18next";
import { UAIEditorEventListener, UAIEditorOptions } from "../../../../core/UAIEditor.ts";
import { EditorEvents } from "@tiptap/core";
/**
* 插入菜单:插入公式
*/
export class Math extends HTMLElement implements UAIEditorEventListener {
// 按钮选项
menuButtonOptions: MenuButtonOptions = {
menuType: "button",
enable: true,
icon: icon,
hideText: false,
text: t('insert.math'),
tooltip: t('insert.math'),
}
// 功能按钮
menuButton: MenuButton;
constructor(options: MenuButtonOptions) {
super();
// 初始化功能按钮选项
this.menuButtonOptions = { ...this.menuButtonOptions, ...options };
// 创建功能按钮
this.menuButton = new MenuButton(this.menuButtonOptions);
}
/**
* 定义创建方法
* @param event
* @param options
*/
onCreate(event: EditorEvents["create"], options: UAIEditorOptions) {
this.menuButton.onCreate(event, options);
this.appendChild(this.menuButton);
// 定义按钮点击事件,插入公式
this.addEventListener("click", () => {
if (this.menuButtonOptions.enable) {
event.editor.chain().focus().insertContent('$\\LaTeX$').run();
}
})
}
/**
* 定义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);
}
}