tinymce.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. layui.define(['jquery'],function (exports) {
  2. var $ = layui.$
  3. var modFile = layui.cache.modules['tinymce'];
  4. var modPath = modFile.substr(0, modFile.lastIndexOf('.'))
  5. var setter = layui.setter || {}
  6. var response = setter.response || {}
  7. var plugin_filename = 'tinymce.min.js'//插件路径,不包含base_url部分
  8. var settings = {
  9. base_url: modPath
  10. , images_upload_url: ''//图片上传接口,可在option传入,也可在这里修改,option的值优先
  11. , language: 'zh_CN'//语言,可在option传入,也可在这里修改,option的值优先
  12. , response: {//后台返回数据格式设置
  13. statusName: response.statusName || 'code'//返回状态字段
  14. , msgName: response.msgName || 'msg'//返回消息字段
  15. , dataName: response.dataName || 'data'//返回的数据
  16. , statusCode: response.statusCode || {
  17. ok: 0//数据正常
  18. }
  19. }
  20. , success: function (res, succFun, failFun) {//图片上传完成回调 根据自己需要修改
  21. if (res[this.response.statusName] == this.response.statusCode.ok) {
  22. succFun(res[this.response.dataName]);
  23. } else {
  24. failFun(res[this.response.msgName]);
  25. }
  26. }
  27. };
  28. // ---------------- 以下代码无需修改 ----------------
  29. var t = {};
  30. //初始化
  31. t.render = function (options,callback) {
  32. initTinymce();
  33. var option = initOptions(options,callback)
  34. ,edit = t.get(option.elem);
  35. if (edit) {
  36. edit.destroy();
  37. }
  38. tinymce.init(option);
  39. return t.get(option.elem);
  40. };
  41. t.init = t.render
  42. // 获取ID对应的编辑器对象
  43. t.get = function (elem) {
  44. initTinymce();
  45. if (elem && /^#|\./.test(elem)) {
  46. var id = elem.substr(1);
  47. var edit = tinymce.editors[id];
  48. return edit
  49. } else {
  50. return false;
  51. }
  52. }
  53. //重载
  54. t.reload = function (elem, option, callback) {
  55. var options = {}
  56. if(typeof elem == 'string'){
  57. option.elem = elem
  58. options = $.extend({}, option)
  59. } else if (typeof elem == 'object' && typeof elem.elem == 'string'){
  60. options = $.extend({}, elem)
  61. callback = option
  62. }
  63. var optionCache = layui.sessionData('layui-tinymce')[options.elem]
  64. delete optionCache.init_instance_callback
  65. $.extend(optionCache,options)
  66. return t.render(optionCache,callback)
  67. }
  68. function initOptions(option,callback) {
  69. var admin = layui.admin || {}
  70. var form = option.form || {}
  71. var file_field = form.name || 'edit' //文件字段名
  72. var form_data = form.data || {} //其他表单数据 {key:value, ...}
  73. option.suffix= isset(option.suffix) ? option.suffix : (plugin_filename.indexOf('.min')>-1 ? '.min' : '')
  74. option.base_url = isset(option.base_url) ? option.base_url : settings.base_url
  75. option.language = isset(option.language) ? option.language : settings.language
  76. option.selector = isset(option.selector) ? option.selector : option.elem
  77. option.quickbars_selection_toolbar = isset(option.quickbars_selection_toolbar) ? option.quickbars_selection_toolbar : 'cut copy | bold italic underline strikethrough '
  78. option.plugins = isset(option.plugins) ? option.plugins : 'code kityformula-editor quickbars print preview searchreplace autolink fullscreen image link media codesample table charmap hr advlist lists wordcount imagetools indent2em';
  79. option.toolbar = isset(option.toolbar) ? option.toolbar : 'code undo redo | kityformula-editor forecolor backcolor bold italic underline strikethrough | indent2em alignleft aligncenter alignright alignjustify outdent indent | link bullist numlist image table codesample | formatselect fontselect fontsizeselect';
  80. option.resize = isset(option.resize) ? option.resize : false;
  81. option.elementpath = isset(option.elementpath) ? option.elementpath : false;
  82. option.branding = isset(option.branding) ? option.branding : false;
  83. option.contextmenu_never_use_native = isset(option.contextmenu_never_use_native) ? option.contextmenu_never_use_native : true;
  84. option.menubar = isset(option.menubar) ? option.menubar : 'file edit insert format table';
  85. option.menu = isset(option.menu) ? option.menu : {
  86. file: {title: '文件', items: 'newdocument | print preview fullscreen | wordcount'},
  87. edit: {title: '编辑', items: 'undo redo | cut copy paste pastetext selectall | searchreplace'},
  88. format: {
  89. title: '格式',
  90. items: 'bold italic underline strikethrough superscript subscript | formats | forecolor backcolor | removeformat'
  91. },
  92. table: {title: '表格', items: 'inserttable tableprops deletetable | cell row column'},
  93. };
  94. option.init_instance_callback =isset(option.init_instance_callback) ? option.init_instance_callback : function(inst) {
  95. if(typeof callback == 'function') callback(option,inst)
  96. };
  97. option.images_upload_url = isset(option.images_upload_url) ? option.images_upload_url : settings.images_upload_url;
  98. option.images_upload_handler = isset(option.images_upload_handler) ? option.images_upload_handler : function(blobInfo, succFun, failFun) {
  99. if(isEmpty(option.images_upload_url)){
  100. failFun("上传接口未配置");
  101. return console.error('images_upload_url未配置');
  102. }
  103. var formData = new FormData();
  104. formData.append(file_field, blobInfo.blob());
  105. if(typeof form_data == 'object'){
  106. for(var key in form_data){
  107. formData.append(key, form_data[key]);
  108. }
  109. }
  110. var ajaxOpt = {
  111. url: option.images_upload_url,
  112. dataType: 'json',
  113. type: 'POST',
  114. data: formData,
  115. processData: false,
  116. contentType: false,
  117. success: function (res) {
  118. settings.success(res, succFun, failFun)
  119. },
  120. error: function (res) {
  121. failFun("网络错误:" + res.status);
  122. }
  123. };
  124. if (typeof admin.req == 'function') {
  125. admin.req(ajaxOpt);
  126. } else {
  127. $.ajax(ajaxOpt);
  128. }
  129. }
  130. layui.sessionData('layui-tinymce',{
  131. key:option.selector,
  132. value:option
  133. })
  134. return option
  135. }
  136. function initTinymce() {
  137. if (typeof tinymce == 'undefined') {
  138. $.ajax({//获取插件
  139. url: settings.base_url + '/' + plugin_filename,
  140. dataType: 'script',
  141. cache: true,
  142. async: false,
  143. });
  144. }
  145. }
  146. function isset(value) {
  147. return typeof value !== 'undefined' && value !== null
  148. }
  149. function isEmpty(value) {
  150. if(typeof value === 'undefined' || value === null|| value === ''){
  151. return true
  152. } else if (value instanceof Array && value.length === 0){
  153. return true
  154. } else if (typeof value === 'object' && Object.keys(value).length === 0){
  155. return true
  156. }
  157. return false
  158. }
  159. exports('tinymce', t);
  160. });