plugin.js 3.8 KB
Newer Older
Five-菜鸟级's avatar
init  
Five-菜鸟级 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
/*! 
*  @plugin @tinymce-plugin/tp-lineheight
*  @version 0.0.9 (2022-5-5)
*  @description 行间距
*  @copyright (2022) Five(Li Hailong) https://github.com/tinymce-plugin/tp-lineheight
*/

(function() {
  "use strict";
  var getLineheightVal = function(editor) {
    return editor.getParam("tp_lineheight_value", "1 1.5 1.6 1.75 1.8 2 3 4 5");
  };
  var create = function(editor, value) {
    editor.undoManager.transact(function() {
      editor.focus();
      editor.execCommand("tpLineHeight", false, value);
    });
  };
  var setup = function(editor, opt2) {
    if (!editor.ui.registry.getAll().icons[opt2.registryName]) {
      editor.ui.registry.addIcon(opt2.registryName, opt2.icon);
    }
    var global$1 = tinymce.util.Tools.resolve("tinymce.util.Tools");
    var tp$Tools = editor.tp$.Tools;
    var getDefaultItems = function(_items2) {
      return _items2.length > 0 ? _items2[0] : "";
    };
    var _items = getLineheightVal(editor).split(" ");
    var defaultSelection = tp$Tools.selection.cell(getDefaultItems(_items));
    editor.ui.registry.addSplitButton(opt2.registryName, {
      icon: opt2.registryName,
      tooltip: opt2.title,
      select: function(value) {
        return value === defaultSelection.get();
      },
      fetch: function(done) {
        var _dom = editor.dom;
        var _block = editor.selection.getSelectedBlocks();
        var lhv = -1;
        if (lhv == -1) {
          lhv = _dom.getStyle(_block, "line-height") ? _dom.getStyle(_block, "line-height") : "";
        }
        done(global$1.map(_items, function(item) {
          lhv == item ? defaultSelection.set(item) : "";
          return {
            type: "choiceitem",
            text: item,
            value: item
          };
        }));
      },
      onAction: function() {
        create(editor, defaultSelection.getAction());
      },
      onItemAction: function(_api, value) {
        defaultSelection.set(value, value);
        create(editor, value);
      }
    });
    editor.ui.registry.addNestedMenuItem(opt2.registryName, {
      text: opt2.title,
      icon: opt2.registryName,
      getSubmenuItems: function() {
        var _dom = editor.dom;
        var _block = editor.selection.getSelectedBlocks();
        var lhv = -1;
        if (lhv == -1) {
          lhv = _dom.getStyle(_block, "line-height") ? _dom.getStyle(_block, "line-height") : "";
        }
        return global$1.map(_items, function(item) {
          return {
            type: "togglemenuitem",
            text: item,
            active: lhv === item ? true : false,
            onAction: function() {
              defaultSelection.set(item, item);
              create(editor, item);
            }
          };
        });
      }
    });
  };
  var register = function(editor, opt2) {
    editor.addCommand("mce".concat(opt2.registryName.substring(0, 1).toUpperCase() + opt2.registryName.substring(1)), function(ui, value) {
      create(editor, value);
    });
  };
  var Plugin = function(opt2) {
    tinymce.PluginManager.add(opt2.registryName, function(editor, url) {
      setup(editor, opt2);
      register(editor, opt2);
      return {
        getMetadata: function() {
          return {
            name: opt2.name,
            url: opt2.repo
          };
        }
      };
    });
  };
  var opt = {
    name: "Lineheight",
    registryName: "tpLineheight",
    title: "Line Height",
    repo: "https://github.com/tinymce-plugin/tp-lineheight",
    icon: '<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"><path d="M9.984 12.984v-1.969h12v1.969h-12zM9.984 18.984v-1.969h12v1.969h-12zM9.984 5.016h12v1.969h-12v-1.969zM6 6.984v10.031h2.484l-3.469 3.469-3.516-3.469h2.484v-10.031h-2.484l3.516-3.469 3.469 3.469h-2.484z"></path></svg>'
  };
  Plugin(opt);
  var main = {
    opt
  };
  return main;
}());