代码之家  ›  专栏  ›  技术社区  ›  Andreas

在TinyMCE中添加模板项

  •  1
  • Andreas  · 技术社区  · 15 年前

    我想在tineMCE编辑器中添加“模板项”。 模板项的作用类似于动态插入数据的占位符。

    举个例子:

    而不是写:“嗨{firstname},你已经{years}岁了。” 我想插入一个对象,而不是在服务器上保存时被替换为“{firstname}”的“{firstname}”。在将其加载到编辑器中时,它也应该进行回译。 应该从下拉列表中选择对象(但是一旦其他的事情都解决了,这应该很容易)。

    1 回复  |  直到 15 年前
        1
  •  0
  •   Thariama    15 年前

    在这种情况下,保存时需要替换占位符:

    tinymce.activeEditor.onSaveContent.add(function(ed, o) {
      console.debug(o.element.nodeName);
      // do your replacement here using a regular expression and the saved value from the dropdown selection
    });
    

    或者从你自己插件的下拉框中选择一个名字。

    要在加载时返回它,还需要将替换字符串存储在satabase中,并在启动tinymce时使用正则表达式替换它。

      // Andreas from db should be placed in a custom initialisation paramter like this:
    db_firstname_save: '<?php echo $value_from_db; ?>', // $value_from_db = 'Andreas'
    

    使用正则表达式替换DB中的值

    tinymce.activeEditor.onInit.add(function(ed) {
      console.debug('Editor is done: ' + ed.id);
      // do your replacement here using a regular expression
      ed.setcontent(ed.getContent.replace(ed.getParam('db_firstname_save'),'{firstname}'));
    });
    

    为了从下拉列表中选择要保存到db的名字,您需要创建自己的插件。如何做到这一点是可以找到的 the tinymce documentation wiki .