如何为 TinyMCE 编辑器新增一个自定义按钮,点击后插入当前日期?

解读

在国内企业级 Joomla 项目中,编辑体验往往决定内容更新频率。面试官抛出“给 TinyMCE 加按钮插日期”这一题,表面看是“改编辑器”,实则在考察候选人对 Joomla 插件机制、MVC 分层、JavaScript 模块化以及多语言规范的系统掌握。答题时若只给出前端代码,会被追问“如何打包、如何更新、如何兼容多版本、如何权限控制”,因此必须给出一条从“插件创建→按钮注册→事件处理→国际化→打包安装”的完整闭环,并体现可维护、可移植、可回滚的工程思维。

知识点

  1. Joomla 插件类型:tinymce 属于“editor-xtd”与“editors”双插件体系,新增按钮需新建一个 editor-xtd 插件,而非直接改核心。
  2. 插件骨架:manifest.xml(声明版本、依赖、语言文件)、php 入口(继承 JPlugin,响应 onDisplay 事件)、tmpl 模板(输出按钮 HTML)。
  3. TinyMCE 5 API:editor.ui.registry.addButton(id, {text, icon, onAction}),通过 Joomla.getOptions(‘plg_editor_tinymce’) 获取实例。
  4. 日期本地化:JHtml::('date', 'now', JText::('PLG_TINYMCE_DATE_FORMAT')),格式串写入语言文件,支持 zh-CN 的“YYYY年MM月DD日”。
  5. 安全与权限:按钮输出前做 $user->authorise('core.edit', 'com_content') 检查,避免前端暴露。
  6. 打包与更新:插件目录 plg_editors-xtd_insertdate,打成 zip,通过“系统→安装→从本地包上传”,支持 Joomla 4/5 的 namespace 自动加载。
  7. 持续交付:在 CI 中执行 php cli/joomla.php extension:discover 自动注册,避免手工启用。

答案

步骤一:创建插件目录结构
plugins/editors-xtd/insertdate/
├─ insertdate.xml
├─ insertdate.php
├─ tmpl/button.php
├─ language/zh-CN/plg_editors-xtd_insertdate.ini

步骤二:manifest 文件 insertdate.xml

<extension type="plugin" group="editors-xtd" method="upgrade">
    <name>PLG_EDITORS-XTD_INSERTDATE</name>
    <version>1.0.0</version>
    <description>PLG_EDITORS-XTD_INSERTDATE_XML_DESCRIPTION</description>
    <files>
        <filename plugin="insertdate">insertdate.php</filename>
        <folder>tmpl</folder>
    </files>
    <languages>
        <language tag="zh-CN">language/zh-CN/plg_editors-xtd_insertdate.ini</language>
    </languages>
</extension>

步骤三:入口文件 insertdate.php

defined('_JEXEC') or die;
use Joomla\CMS\Plugin\CMSPlugin;
use Joomla\CMS\Factory;
use Joomla\CMS\Language\Text;

class PlgEditorsXtdInsertdate extends CMSPlugin
{
    public function onDisplay($name)
    {
        $user = Factory::getApplication()->getIdentity();
        if (!$user->authorise('core.edit', 'com_content')) {
            return;
        }

        $button = new \Joomla\CMS\Object\CMSObject;
        $button->modal   = false;
        $button->text    = Text::_('PLG_INSERTDATE_BUTTON_TEXT');
        $button->name    = 'calendar';
        $button->onclick = 'insertDate' . $name . '()';
        $button->iconSVG = '<svg viewBox="0 0 24 24"><path d="M19 3h-1V1h-2v2H8V1H6v2H5c-1.11 0-1.99.9-1.99 2L3 19a2 2 0 002 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zm0 16H5V8h14v11z"/></svg>';
        return $button;
    }
}

步骤四:按钮模板 tmpl/button.php

defined('_JEXEC') or die;
$editorName = $displayData->name;
$js = "
function insertDate{$editorName}() {
  const ed = Joomla.editors.instances['{$editorName}'];
  if (ed && ed.getDoc) {  // TinyMCE 4/5 兼容
    const date = new Date().toLocaleDateString('zh-CN');
    ed.insertContent(date);
  }
}";
Factory::getDocument()->addScriptDeclaration($js);

步骤五:语言文件 zh-CN/plg_editors-xtd_insertdate.ini

PLG_EDITORS-XTD_INSERTDATE="编辑器按钮 - 插入日期"
PLG_EDITORS-XTD_INSERTDATE_XML_DESCRIPTION="在 TinyMCE 工具栏增加一个插入当天日期的按钮"
PLG_INSERTDATE_BUTTON_TEXT="插入日期"

步骤六:打包与安装

cd plugins/editors-xtd
zip -r insertdate-1.0.0.zip insertdate/

后台“扩展→安装→上传”,成功后“扩展→插件”启用“编辑器按钮 - 插入日期”。再进入文章编辑,TinyMCE 工具栏即刻出现日历图标,点击即可插入形如“2025年6月25日”的日期文本,格式随语言包自动切换。

拓展思考

  1. 动态格式:把日期格式做成插件参数,用户在“插件配置”里可选“YYYY-MM-DD”或“DD/MM/YYYY”,通过 $this->params->get('date_format', 'Y年m月d日') 读取。
  2. 前端缓存:若页面开启 CDN,按钮 JS 会被缓存,可在文件名后追加 ?v=<?php echo $this->params->get('version', '1.0.0'); ?> 强制刷新。
  3. 多编辑器共存:同一页面若有多个 TinyMCE 实例,按钮 ID 需动态后缀,避免全局变量污染。
  4. 升级 Joomla 5:Joomla 5 默认用 TinyMCE 6,API 改为 editor.insertContent(),需在插件中加版本嗅探,保证 4/5/6 三版本兼容。
  5. 单元测试:编写 Cypress 用例,断言点击按钮后 textarea 值包含当天年份,纳入 GitHub Actions,每次 PR 自动跑通,体现国内企业对“质量门禁”的硬性要求。