plugin.min.js 18.7 KB
Newer Older
Five-菜鸟级's avatar
init  
Five-菜鸟级 已提交
1 2 3 4 5 6 7 8 9
/*! 
* Copyright (c) Tiny Technologies, Inc. All rights reserved.
* Licensed under the LGPL or a commercial license.
* For LGPL see License.txt in the project root for license information.
* For commercial licenses see https://www.tiny.cloud/
* 
*  Version: 5.7.0 (2021-02-10)
*  */
!function(t){var r={};function a(n){if(r[n])return r[n].exports;var e=r[n]={i:n,l:!1,exports:{}};return t[n].call(e.exports,e,e.exports,a),e.l=!0,e.exports}a.m=t,a.c=r,a.d=function(n,e,t){a.o(n,e)||Object.defineProperty(n,e,{configurable:!1,enumerable:!0,get:t})},a.r=function(n){Object.defineProperty(n,"__esModule",{value:!0})},a.n=function(n){var e=n&&n.__esModule?function(){return n["default"]}:function(){return n};return a.d(e,"a",e),e},a.o=function(n,e){return Object.prototype.hasOwnProperty.call(n,e)},a.p="",a(a.s="./src/js/plugins/emoticons/plugin.js")}({"./src/js/plugins/emoticons/plugin.js":function(module,exports){eval("/**\n  * Copyright (c) Tiny Technologies, Inc. All rights reserved.\n  * Licensed under the LGPL or a commercial license.\n  * For LGPL see License.txt in the project root for license information.\n  * For commercial licenses see https://www.tiny.cloud/\n  *\n  * Version: 5.7.0 (2021-02-10)\n  */\n (function () {\n    'use strict';\n\n    var global = tinymce.util.Tools.resolve('tinymce.PluginManager');\n\n    var DEFAULT_ID = 'tinymce.plugins.emoticons';\n    var getEmoticonDatabase = function (editor) {\n      return editor.getParam('emoticons_database', 'emojis', 'string');\n    };\n    var getEmoticonDatabaseUrl = function (editor, pluginUrl) {\n      var database = getEmoticonDatabase(editor);\n      return editor.getParam('emoticons_database_url', pluginUrl + '/js/' + database + editor.suffix + '.js', 'string');\n    };\n    var getEmoticonDatabaseId = function (editor) {\n      return editor.getParam('emoticons_database_id', DEFAULT_ID, 'string');\n    };\n    var getAppendedEmoticons = function (editor) {\n      return editor.getParam('emoticons_append', {}, 'object');\n    };\n    var getEmotionsImageUrl = function (editor) {\n      return editor.getParam('emoticons_images_url', 'https://twemoji.maxcdn.com/v/13.0.1/72x72/', 'string');\n    };\n\n    var __assign = function () {\n      __assign = Object.assign || function __assign(t) {\n        for (var s, i = 1, n = arguments.length; i < n; i++) {\n          s = arguments[i];\n          for (var p in s)\n            if (Object.prototype.hasOwnProperty.call(s, p))\n              t[p] = s[p];\n        }\n        return t;\n      };\n      return __assign.apply(this, arguments);\n    };\n\n    var Cell = function (initial) {\n      var value = initial;\n      var get = function () {\n        return value;\n      };\n      var set = function (v) {\n        value = v;\n      };\n      return {\n        get: get,\n        set: set\n      };\n    };\n\n    var hasOwnProperty = Object.prototype.hasOwnProperty;\n    var shallow = function (old, nu) {\n      return nu;\n    };\n    var baseMerge = function (merger) {\n      return function () {\n        var objects = [];\n        for (var _i = 0; _i < arguments.length; _i++) {\n          objects[_i] = arguments[_i];\n        }\n        if (objects.length === 0) {\n          throw new Error('Can\\'t merge zero objects');\n        }\n        var ret = {};\n        for (var j = 0; j < objects.length; j++) {\n          var curObject = objects[j];\n          for (var key in curObject) {\n            if (hasOwnProperty.call(curObject, key)) {\n              ret[key] = merger(ret[key], curObject[key]);\n            }\n          }\n        }\n        return ret;\n      };\n    };\n    var merge = baseMerge(shallow);\n\n    var noop = function () {\n    };\n    var constant = function (value) {\n      return function () {\n        return value;\n      };\n    };\n    var never = constant(false);\n    var always = constant(true);\n\n    var none = function () {\n      return NONE;\n    };\n    var NONE = function () {\n      var eq = function (o) {\n        return o.isNone();\n      };\n      var call = function (thunk) {\n        return thunk();\n      };\n      var id = function (n) {\n        return n;\n      };\n      var me = {\n        fold: function (n, _s) {\n          return n();\n        },\n        is: never,\n        isSome: never,\n        isNone: always,\n        getOr: id,\n        getOrThunk: call,\n        getOrDie: function (msg) {\n          throw new Error(msg || 'error: getOrDie called on none.');\n        },\n        getOrNull: constant(null),\n        getOrUndefined: constant(undefined),\n        or: id,\n        orThunk: call,\n        map: none,\n        each: noop,\n        bind: none,\n        exists: never,\n        forall: always,\n        filter: none,\n        equals: eq,\n        equals_: eq,\n        toArray: function () {\n          return [];\n        },\n        toString: constant('none()')\n      };\n      return me;\n    }();\n    var some = function (a) {\n      var constant_a = constant(a);\n      var self = function () {\n        return me;\n      };\n      var bind = function (f) {\n        return f(a);\n      };\n      var me = {\n        fold: function (n, s) {\n          return s(a);\n        },\n        is: function (v) {\n          return a === v;\n        },\n        isSome: always,\n        isNone: never,\n        getOr: constant_a,\n        getOrThunk: constant_a,\n        getOrDie: constant_a,\n        getOrNull: constant_a,\n        getOrUndefined: constant_a,\n        or: self,\n        orThunk: self,\n        map: function (f) {\n          return some(f(a));\n        },\n        each: function (f) {\n          f(a);\n        },\n        bind: bind,\n        exists: bind,\n        forall: bind,\n        filter: function (f) {\n          return f(a) ? me : NONE;\n        },\n        toArray: function () {\n          return [a];\n        },\n        toString: function () {\n          return 'some(' + a + ')';\n        },\n        equals: function (o) {\n          return o.is(a);\n        },\n        equals_: function (o, elementEq) {\n          return o.fold(never, function (b) {\n            return elementEq(a, b);\n          });\n        }\n      };\n      return me;\n    };\n    var from = function (value) {\n      return value === null || value === undefined ? NONE : some(value);\n    };\n    var Optional = {\n      some: some,\n      none: none,\n      from: from\n    };\n\n    var keys = Object.keys;\n    var hasOwnProperty$1 = Object.hasOwnProperty;\n    var each = function (obj, f) {\n      var props = keys(obj);\n      for (var k = 0, len = props.length; k < len; k++) {\n        var i = props[k];\n        var x = obj[i];\n        f(x, i);\n      }\n    };\n    var map = function (obj, f) {\n      return tupleMap(obj, function (x, i) {\n        return {\n          k: i,\n          v: f(x, i)\n        };\n      });\n    };\n    var tupleMap = function (obj, f) {\n      var r = {};\n      each(obj, function (x, i) {\n        var tuple = f(x, i);\n        r[tuple.k] = tuple.v;\n      });\n      return r;\n    };\n    var has = function (obj, key) {\n      return hasOwnProperty$1.call(obj, key);\n    };\n\n    var checkRange = function (str, substr, start) {\n      return substr === '' || str.length >= substr.length && str.substr(start, start + substr.length) === substr;\n    };\n    var contains = function (str, substr) {\n      return str.indexOf(substr) !== -1;\n    };\n    var startsWith = function (str, prefix) {\n      return checkRange(str, prefix, 0);\n    };\n\n    var global$1 = tinymce.util.Tools.resolve('tinymce.Resource');\n\n    var global$2 = tinymce.util.Tools.resolve('tinymce.util.Delay');\n\n    var global$3 = tinymce.util.Tools.resolve('tinymce.util.Promise');\n\n    var ALL_CATEGORY = 'All';\n    var categoryNameMap = {\n      symbols: 'Symbols',\n      people: 'People',\n      animals_and_nature: 'Animals and Nature',\n      food_and_drink: 'Food and Drink',\n      activity: 'Activity',\n      travel_and_places: 'Travel and Places',\n      objects: 'Objects',\n      flags: 'Flags',\n      user: 'User Defined'\n    };\n    var translateCategory = function (categories, name) {\n      return has(categories, name) ? categories[name] : name;\n    };\n    var getUserDefinedEmoticons = function (editor) {\n      var userDefinedEmoticons = getAppendedEmoticons(editor);\n      return map(userDefinedEmoticons, function (value) {\n        return __assign({\n          keywords: [],\n          category: 'user'\n        }, value);\n      });\n    };\n    var initDatabase = function (editor, databaseUrl, databaseId) {\n      var categories = Cell(Optional.none());\n      var all = Cell(Optional.none());\n      var emojiImagesUrl = getEmotionsImageUrl(editor);\n      var getEmoji = function (lib) {\n        if (startsWith(lib.char, '<img')) {\n          return lib.char.replace(/src=\"([^\"]+)\"/, function (match, url) {\n            return 'src=\"' + emojiImagesUrl + url + '\"';\n          });\n        } else {\n          return lib.char;\n        }\n      };\n      var processEmojis = function (emojis) {\n        var cats = {};\n        var everything = [];\n        each(emojis, function (lib, title) {\n          var entry = {\n            title: title,\n            keywords: lib.keywords,\n            char: getEmoji(lib),\n            category: translateCategory(categoryNameMap, lib.category)\n          };\n          var current = cats[entry.category] !== undefined ? cats[entry.category] : [];\n          cats[entry.category] = current.concat([entry]);\n          everything.push(entry);\n        });\n        categories.set(Optional.some(cats));\n        all.set(Optional.some(everything));\n      };\n      editor.on('init', function () {\n        global$1.load(databaseId, databaseUrl).then(function (emojis) {\n          var userEmojis = getUserDefinedEmoticons(editor);\n          processEmojis(merge(emojis, userEmojis));\n        }, function (err) {\n          console.log('Failed to load emoticons: ' + err);\n          categories.set(Optional.some({}));\n          all.set(Optional.some([]));\n        });\n      });\n      var listCategory = function (category) {\n        if (category === ALL_CATEGORY) {\n          return listAll();\n        }\n        return categories.get().bind(function (cats) {\n          return Optional.from(cats[category]);\n        }).getOr([]);\n      };\n      var listAll = function () {\n        return all.get().getOr([]);\n      };\n      var listCategories = function () {\n        return [ALL_CATEGORY].concat(keys(categories.get().getOr({})));\n      };\n      var waitForLoad = function () {\n        if (hasLoaded()) {\n          return global$3.resolve(true);\n        } else {\n          return new global$3(function (resolve, reject) {\n            var numRetries = 15;\n            var interval = global$2.setInterval(function () {\n              if (hasLoaded()) {\n                global$2.clearInterval(interval);\n                resolve(true);\n              } else {\n                numRetries--;\n                if (numRetries < 0) {\n                  console.log('Could not load emojis from url: ' + databaseUrl);\n                  global$2.clearInterval(interval);\n                  reject(false);\n                }\n              }\n            }, 100);\n          });\n        }\n      };\n      var hasLoaded = function () {\n        return categories.get().isSome() && all.get().isSome();\n      };\n      return {\n        listCategories: listCategories,\n        hasLoaded: hasLoaded,\n        waitForLoad: waitForLoad,\n        listAll: listAll,\n        listCategory: listCategory\n      };\n    };\n\n    var exists = function (xs, pred) {\n      for (var i = 0, len = xs.length; i < len; i++) {\n        var x = xs[i];\n        if (pred(x, i)) {\n          return true;\n        }\n      }\n      return false;\n    };\n    var map$1 = function (xs, f) {\n      var len = xs.length;\n      var r = new Array(len);\n      for (var i = 0; i < len; i++) {\n        var x = xs[i];\n        r[i] = f(x, i);\n      }\n      return r;\n    };\n    var each$1 = function (xs, f) {\n      for (var i = 0, len = xs.length; i < len; i++) {\n        var x = xs[i];\n        f(x, i);\n      }\n    };\n\n    var setup = function (editor) {\n      editor.on('PreInit', function () {\n        editor.parser.addAttributeFilter('data-emoticon', function (nodes) {\n          each$1(nodes, function (node) {\n            node.attr('data-mce-resize', 'false');\n            node.attr('data-mce-placeholder', '1');\n          });\n        });\n      });\n    };\n\n    var emojiMatches = function (emoji, lowerCasePattern) {\n      return contains(emoji.title.toLowerCase(), lowerCasePattern) || exists(emoji.keywords, function (k) {\n        return contains(k.toLowerCase(), lowerCasePattern);\n      });\n    };\n    var emojisFrom = function (list, pattern, maxResults) {\n      var matches = [];\n      var lowerCasePattern = pattern.toLowerCase();\n      var reachedLimit = maxResults.fold(function () {\n        return never;\n      }, function (max) {\n        return function (size) {\n          return size >= max;\n        };\n      });\n      for (var i = 0; i < list.length; i++) {\n        if (pattern.length === 0 || emojiMatches(list[i], lowerCasePattern)) {\n          matches.push({\n            value: list[i].char,\n            text: list[i].title,\n            icon: list[i].char\n          });\n          if (reachedLimit(matches.length)) {\n            break;\n          }\n        }\n      }\n      return matches;\n    };\n\n    var init = function (editor, database) {\n      editor.ui.registry.addAutocompleter('emoticons', {\n        ch: ':',\n        columns: 'auto',\n        minChars: 2,\n        fetch: function (pattern, maxResults) {\n          return database.waitForLoad().then(function () {\n            var candidates = database.listAll();\n            return emojisFrom(candidates, pattern, Optional.some(maxResults));\n          });\n        },\n        onAction: function (autocompleteApi, rng, value) {\n          editor.selection.setRng(rng);\n          editor.insertContent(value);\n          autocompleteApi.hide();\n        }\n      });\n    };\n\n    var last = function (fn, rate) {\n      var timer = null;\n      var cancel = function () {\n        if (timer !== null) {\n          clearTimeout(timer);\n          timer = null;\n        }\n      };\n      var throttle = function () {\n        var args = [];\n        for (var _i = 0; _i < arguments.length; _i++) {\n          args[_i] = arguments[_i];\n        }\n        if (timer !== null) {\n          clearTimeout(timer);\n        }\n        timer = setTimeout(function () {\n          fn.apply(null, args);\n          timer = null;\n        }, rate);\n      };\n      return {\n        cancel: cancel,\n        throttle: throttle\n      };\n    };\n\n    var insertEmoticon = function (editor, ch) {\n      editor.insertContent(ch);\n    };\n\n    var patternName = 'pattern';\n    var open = function (editor, database) {\n      var initialState = {\n        pattern: '',\n        results: emojisFrom(database.listAll(), '', Optional.some(300))\n      };\n      var currentTab = Cell(ALL_CATEGORY);\n      var scan = function (dialogApi) {\n        var dialogData = dialogApi.getData();\n        var category = currentTab.get();\n        var candidates = database.listCategory(category);\n        var results = emojisFrom(candidates, dialogData[patternName], category === ALL_CATEGORY ? Optional.some(300) : Optional.none());\n        dialogApi.setData({ results: results });\n      };\n      var updateFilter = last(function (dialogApi) {\n        scan(dialogApi);\n      }, 200);\n      var searchField = {\n        label: 'Search',\n        type: 'input',\n        name: patternName\n      };\n      var resultsField = {\n        type: 'collection',\n        name: 'results'\n      };\n      var getInitialState = function () {\n        var body = {\n          type: 'tabpanel',\n          tabs: map$1(database.listCategories(), function (cat) {\n            return {\n              title: cat,\n              name: cat,\n              items: [\n                searchField,\n                resultsField\n              ]\n            };\n          })\n        };\n        return {\n          title: 'Emoticons',\n          size: 'normal',\n          body: body,\n          initialData: initialState,\n          onTabChange: function (dialogApi, details) {\n            currentTab.set(details.newTabName);\n            updateFilter.throttle(dialogApi);\n          },\n          onChange: updateFilter.throttle,\n          onAction: function (dialogApi, actionData) {\n            if (actionData.name === 'results') {\n              insertEmoticon(editor, actionData.value);\n              dialogApi.close();\n            }\n          },\n          buttons: [{\n              type: 'cancel',\n              text: 'Close',\n              primary: true\n            }]\n        };\n      };\n      var dialogApi = editor.windowManager.open(getInitialState());\n      dialogApi.focus(patternName);\n      if (!database.hasLoaded()) {\n        dialogApi.block('Loading emoticons...');\n        database.waitForLoad().then(function () {\n          dialogApi.redial(getInitialState());\n          updateFilter.throttle(dialogApi);\n          dialogApi.focus(patternName);\n          dialogApi.unblock();\n        }).catch(function (_err) {\n          dialogApi.redial({\n            title: 'Emoticons',\n            body: {\n              type: 'panel',\n              items: [{\n                  type: 'alertbanner',\n                  level: 'error',\n                  icon: 'warning',\n                  text: '<p>Could not load emoticons</p>'\n                }]\n            },\n            buttons: [{\n                type: 'cancel',\n                text: 'Close',\n                primary: true\n              }],\n            initialData: {\n              pattern: '',\n              results: []\n            }\n          });\n          dialogApi.focus(patternName);\n          dialogApi.unblock();\n        });\n      }\n    };\n\n    var register = function (editor, database) {\n      var onAction = function () {\n        return open(editor, database);\n      };\n      editor.ui.registry.addButton('emoticons', {\n        tooltip: 'Emoticons',\n        icon: 'emoji',\n        onAction: onAction\n      });\n      editor.ui.registry.addMenuItem('emoticons', {\n        text: 'Emoticons...',\n        icon: 'emoji',\n        onAction: onAction\n      });\n    };\n\n    function Plugin () {\n      global.add('emoticons', function (editor, pluginUrl) {\n        var databaseUrl = getEmoticonDatabaseUrl(editor, pluginUrl);\n        var databaseId = getEmoticonDatabaseId(editor);\n        var database = initDatabase(editor, databaseUrl, databaseId);\n        register(editor, database);\n        init(editor, database);\n        setup(editor);\n      });\n    }\n\n    Plugin();\n\n}());\n\n\n//# sourceURL=webpack:///./src/js/plugins/emoticons/plugin.js?")}});