bootstrap.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. require.config({
  2. paths: {
  3. 'simditor': '../addons/simditor/js/simditor',
  4. 'simple-module': '../addons/simditor/js/module',
  5. 'simple-hotkeys': '../addons/simditor/js/hotkeys',
  6. 'simple-uploader': '../addons/simditor/js/uploader',
  7. 'dompurify': '../addons/simditor/js/dompurify',
  8. },
  9. shim: {
  10. 'simditor': [
  11. 'css!../addons/simditor/css/simditor.min.css',
  12. ]
  13. }
  14. });
  15. require(['form'], function (Form) {
  16. var _bindevent = Form.events.bindevent;
  17. Form.events.bindevent = function (form) {
  18. _bindevent.apply(this, [form]);
  19. if ($(Config.simditor.classname || '.editor', form).length > 0) {
  20. //修改上传的接口调用
  21. require(['upload', 'simditor', 'dompurify'], function (Upload, Simditor, DOMPurify) {
  22. var editor, mobileToolbar, toolbar;
  23. Simditor.locale = 'zh-CN';
  24. Simditor.list = {};
  25. toolbar = ['title', 'bold', 'italic', 'underline', 'strikethrough', 'fontScale', 'color', '|', 'ol', 'ul', 'blockquote', 'code', 'table', '|', 'link', 'image', 'hr', '|', 'indent', 'outdent', 'alignment'];
  26. mobileToolbar = ["bold", "underline", "strikethrough", "color", "ul", "ol"];
  27. // 添加 hook 过滤 iframe 来源
  28. DOMPurify.addHook('uponSanitizeElement', function (node, data, config) {
  29. if (data.tagName === 'iframe') {
  30. var allowedIframePrefixes = Config.nkeditor.allowiframeprefixs || [];
  31. var src = node.getAttribute('src');
  32. // 判断是否匹配允许的前缀
  33. var isAllowed = false;
  34. for (var i = 0; i < allowedIframePrefixes.length; i++) {
  35. if (src && src.indexOf(allowedIframePrefixes[i]) === 0) {
  36. isAllowed = true;
  37. break;
  38. }
  39. }
  40. if (!isAllowed) {
  41. // 不符合要求则移除该节点
  42. return node.parentNode.removeChild(node);
  43. }
  44. // 添加安全属性
  45. node.setAttribute('allowfullscreen', '');
  46. node.setAttribute('allow', 'fullscreen');
  47. }
  48. });
  49. var purifyOptions = {
  50. ADD_TAGS: ['iframe'],
  51. FORCE_REJECT_IFRAME: false
  52. };
  53. $(Config.simditor.classname || '.editor', form).each(function () {
  54. var id = $(this).attr("id");
  55. editor = new Simditor({
  56. textarea: this,
  57. height: isNaN(Config.simditor.height) ? null : parseInt(Config.simditor.height),
  58. minHeight: parseInt(Config.simditor.minHeight || 250),
  59. toolbar: Config.simditor.toolbar || [],
  60. mobileToolbar: Config.simditor.mobileToolbar || [],
  61. toolbarFloat: parseInt(Config.simditor.toolbarFloat),
  62. placeholder: Config.simditor.placeholder || '',
  63. dompurify: {
  64. enabled: Config.simditor.isdompurify,
  65. options: purifyOptions
  66. },
  67. pasteImage: true,
  68. defaultImage: Config.__CDN__ + '/assets/addons/simditor/images/image.png',
  69. upload: {url: '/'},
  70. allowedTags: ['div', 'br', 'span', 'a', 'img', 'b', 'strong', 'i', 'strike', 'u', 'font', 'p', 'ul', 'ol', 'li', 'blockquote', 'pre', 'code', 'h1', 'h2', 'h3', 'h4', 'hr'],
  71. allowedAttributes: {
  72. div: ['data-tpl', 'data-source', 'data-id'],
  73. span: ['data-id']
  74. },
  75. allowedStyles: {
  76. div: ['width', 'height', 'padding', 'background', 'color', 'display', 'justify-content', 'border', 'box-sizing', 'max-width', 'min-width', 'position', 'margin-left', 'bottom', 'left', 'margin', 'float'],
  77. p: ['margin', 'color', 'height', 'line-height', 'position', 'width', 'border', 'bottom', 'float'],
  78. span: ['text-decoration', 'color', 'margin-left', 'float', 'background', 'padding', 'margin-right', 'border-radius', 'font-size', 'border', 'float'],
  79. img: ['vertical-align', 'width', 'height', 'object-fit', 'float', 'margin', 'float'],
  80. a: ['text-decoration']
  81. }
  82. });
  83. editor.toolbar.wrapper.find(".menu-item-select-image").on('click', function(){
  84. var selectUrl = typeof Config !== 'undefined' && Config.modulename === 'index' ? 'user/attachment' : 'general/attachment/select';
  85. parent.Fast.api.open(selectUrl + "?element_id=&multiple=true&mimetype=image/", __('Choose'), {
  86. callback: function (data) {
  87. var urlArr = data.url.split(/\,/);
  88. $.each(urlArr, function () {
  89. var url = Fast.api.cdnurl(this, true);
  90. var imgHtml = '<img src="' + url + '" />';
  91. editor.insertHTML(imgHtml);
  92. });
  93. }
  94. });
  95. return false;
  96. });
  97. editor.uploader.on('beforeupload', function (e, file) {
  98. Upload.api.send(file.obj, function (data) {
  99. var url = Fast.api.cdnurl(data.url, true);
  100. editor.uploader.trigger("uploadsuccess", [file, {success: true, file_path: url}]);
  101. });
  102. return false;
  103. });
  104. editor.on("blur", function () {
  105. this.textarea.trigger("blur");
  106. });
  107. if (editor.opts.height) {
  108. editor.body.css({height: editor.opts.height, 'overflow-y': 'auto'});
  109. }
  110. if (editor.opts.minHeight) {
  111. editor.body.css({'min-height': editor.opts.minHeight});
  112. }
  113. Simditor.list[id] = editor;
  114. });
  115. });
  116. }
  117. }
  118. });