首页 > 其他分享 >TinyMCE——自定义工具栏按钮(基础按钮、下拉框按钮、弹框按钮等)

TinyMCE——自定义工具栏按钮(基础按钮、下拉框按钮、弹框按钮等)

时间:2023-10-09 09:57:10浏览次数:39  
标签:自定义 text date ui editor 按钮 Date 下拉框 registry

详细配置查看官方文档:https://www.tiny.cloud/docs/tinymce/6/custom-toolbarbuttons/

 

 配置方式:

tinymce.init({
  selector: '#editor',
  toolbar: 'myCustomToolbarButton',
  setup: (editor) => {
    editor.ui.registry.addButton('myCustomToolbarButton', {
      text: 'My Custom Button',
      onAction: () => alert('Button clicked!')
    });
  }
})

 

几种按钮使用的方法:

editor.ui.registry.addButton(identifier, configuration)
// 切换按钮
editor.ui.registry.addToggleButton(identifier, configuration)
// 下拉框
editor.ui.registry.addSplitButton(identifier, configuration)
// 菜单
editor.ui.registry.addMenuButton(identifier, configuration)
// 组合按钮
editor.ui.registry.addGroupToolbarButton(identifier, configuration)

  

官网示例:

tinymce.init({
  selector: 'textarea#toolbar-button',
  toolbar: 'basicDateButton selectiveDateButton toggleDateButton splitDateButton menuDateButton',
  setup: (editor) => {

    /* Helper functions */
    const toDateHtml = (date) => `<time datetime="${date.toString()}">${date.toDateString()}</time>`;
    const toGmtHtml = (date) => `<time datetime="${date.toString()}">${date.toGMTString()}</time>`;
    const toIsoHtml = (date) => `<time datetime="${date.toString()}">${date.toISOString()}</time>`;

    /* Basic button that just inserts the date */
    editor.ui.registry.addButton('basicDateButton', {
      text: 'Insert Date',
      tooltip: 'Insert Current Date',
      onAction: (_) => editor.insertContent(toDateHtml(new Date()))
    });

    /* Basic button that inserts the date, but only if the cursor isn't currently in a "time" element */
    editor.ui.registry.addButton('selectiveDateButton', {
      icon: 'insert-time',
      tooltip: 'Insert Current Date',
      enabled: false,
      onAction: (_) => editor.insertContent(toDateHtml(new Date())),
      onSetup: (buttonApi) => {
        const editorEventCallback = (eventApi) => {
          buttonApi.setEnabled(eventApi.element.nodeName.toLowerCase() !== 'time');
        };
        editor.on('NodeChange', editorEventCallback);
        return () => editor.off('NodeChange', editorEventCallback);
      }
    });

    /* Toggle button that inserts the date, but becomes inactive when the cursor is in a "time" element */
    /* so you can't insert a "time" element inside another one. Also gives visual feedback. */
    editor.ui.registry.addToggleButton('toggleDateButton', {
      icon: 'insert-time',
      tooltip: 'Insert Current Date',
      onAction: (_) => editor.insertContent(toDateHtml(new Date())),
      onSetup: (buttonApi) => {
        const editorEventCallback = (eventApi) => {
          buttonApi.setActive(eventApi.element.nodeName.toLowerCase() === 'time');
        };
        editor.on('NodeChange', editorEventCallback);
        return () => editor.off('NodeChange', editorEventCallback);
      }
    });

    /* Split button that lists 3 formats, and inserts the date in the selected format when clicked */
    editor.ui.registry.addSplitButton('splitDateButton', {
      text: 'Insert Date',
      onAction: (_) => editor.insertContent('<p>Its Friday!</p>'),
      onItemAction: (buttonApi, value) => editor.insertContent(value),
      fetch: (callback) => {
        const items = [
          {
            type: 'choiceitem',
            text: 'Insert Date',
            value: toDateHtml(new Date())
          },
          {
            type: 'choiceitem',
            text: 'Insert GMT Date',
            value: toGmtHtml(new Date())
          },
          {
            type: 'choiceitem',
            text: 'Insert ISO Date',
            value: toIsoHtml(new Date())
          }
        ];
        callback(items);
      }
    });

    /* Menu button that has a simple "insert date" menu item, and a submenu containing other formats. */
    /* Clicking the first menu item or one of the submenu items inserts the date in the selected format. */
    editor.ui.registry.addMenuButton('menuDateButton', {
      text: 'Date',
      fetch: (callback) => {
        const items = [
          {
            type: 'menuitem',
            text: 'Insert Date',
            onAction: (_) => editor.insertContent(toDateHtml(new Date()))
          },
          {
            type: 'nestedmenuitem',
            text: 'Other formats',
            getSubmenuItems: () => [
              {
                type: 'menuitem',
                text: 'GMT',
                onAction: (_) => editor.insertContent(toGmtHtml(new Date()))
              },
              {
                type: 'menuitem',
                text: 'ISO',
                onAction: (_) => editor.insertContent(toIsoHtml(new Date()))
              }
            ]
          }
        ];
        callback(items);
      }
    });
  },
  content_style: 'body { font-family:Helvetica,Arial,sans-serif; font-size:16px }'
});

  

出来的效果如下,在官网可以自己进行测试,非常方便

 

标签:自定义,text,date,ui,editor,按钮,Date,下拉框,registry
From: https://www.cnblogs.com/ellen-mylife/p/17750788.html

相关文章

  • PyQt 自定义信号带参数 emit
    PyQt5自定义信号带参数importsysfromPyQt5.QtCoreimportpyqtSignal,QObjectfromPyQt5.QtWidgetsimportQMainWindow,QApplicationclassmysignal(QObject):closeApp=pyqtSignal(list)classExample(QMainWindow):def__init__(self):super().......
  • vue封装搜索组件,自定义elment搜索组件
    组件案例<template><divclass="dialog-search"><el-form:inline="true"ref="ruleForm":model="formInline"class="demo-form-inlinetop-screen"><divclass="to......
  • pytorch 自定义dataset类
    实现模版classour_dataset(Dataset):def__init__(self,···):super(our_dataset,self).__init__()#初始化,可以自定义添加参数def__getitem__(self,index):···returnimg,label#根据索引(0,len(dataset)-1)获取......
  • 视频直播源码,标题居中,底部按钮为三个时居中布局
    视频直播源码,标题居中,底部按钮为三个时居中布局更改底部按钮默认大写的设置 <stylename="CustomAlertDialog"parent="@style/Theme.AppCompat.Light.Dialog.Alert">    <itemname="buttonBarButtonStyle">@style/CustomAlertDialogButton</item><......
  • 非select 下拉框选定ul-li下拉选项, 元素是隐藏的需要用js修改为其属性为可见后,再做元
    遇到问题html中定位的下拉框内容是属性是隐藏的<ulid="reasonselect"style="display:none"fwin="mods"><li>广告/SPAM</li><li>恶意灌水</li><li>违规内容</li><li>文不对题</li><li>重复发帖</li>&......
  • C# Model 自定义检验
    使Model实现IValidatableObject接口并重写Validate方法即可publicclassAuditIPKeepRecordApply_In:IValidatableObject{///<summary>///IP备案申请表主键id///</summary>[Required(ErrorMessage="IP备案申请表主键id不可为空"......
  • .Net7自定义GC垃圾回收器
    1.前言CLR和GC高度耦合,.Net7里面分离CLR和GC,则比较容易实现这件事情。本篇来看下,自定义一个GC垃圾回收器。2.概述这里首先演示下自定义GC垃圾回收后的效果。1.下载Custom.dll2.找到当前.Net目录,比如这里的7.0.10C:\ProgramFiles\dotnet\shared\Microsoft.NETCore.App\7.0.1......
  • qt ui编辑器的按钮相应槽函数为什么没有connect连接
    Qt自动连接槽 AutomaticConnections其原理是由UIC在编译期自动生成连接信号槽的代码,仅限于.ui文件,如果是在.cpp中手写的ui还是要自己connect自动连接可以省去connect信号槽的那句,只需按照规则声明槽函数即可voidon_<objectname>_<signalname>(<signalparameters>);例如......
  • CefSharp自定义滚动条样式
    在WinForm/WPF中使用CefSharp混合开发时,通常需要自定义滚动条样式,以保证应用的整体风格统一。本文将给出一个简单的示例介绍如何自定义CefSharp中滚动条的样式。基本思路在前端开发中,通过CSS来控制滚动条的样式是件寻常的事情。CefSharp也提供了功能强大的API方便开发人员使用c#......
  • Go 复合数据类型之结构体与自定义类型
    Go复合数据类型之结构体与自定义类型目录Go复合数据类型之结构体与自定义类型一、类型别名和自定义类型1.1类型定义(TypeDefinition)简单示例1.2类型别名简单示例1.3类型定义和类型别名的区别二、结构体2.1结构体介绍2.2结构体的定义2.3定义一个空结构体2.3.1空结构体介......