tinymceInit.js 3.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. (function () {
  2. wysiwyg = function (selector, handleOnChange) {
  3. function base64EncodeAndTinyMceInsert (files) { // @todo move this function out of wysiwyg = { ... }
  4. for (var i = 0; i < files.length; i++) {
  5. if (files[i].size > 1000000)
  6. files[i].allowed = confirm(files[i].name + " fait plus de 1mo et peut prendre du temps à insérer, voulez-vous continuer ?")
  7. }
  8. for (var i = 0; i < files.length; i++) {
  9. if (files[i].allowed !== false && files[i].type.match('image.*')) {
  10. var img = document.createElement('img')
  11. var fr = new FileReader()
  12. fr.readAsDataURL(files[i])
  13. fr.onloadend = function (e) {
  14. img.src = e.target.result
  15. tinymce.activeEditor.execCommand('mceInsertContent', false, img.outerHTML)
  16. }
  17. }
  18. }
  19. }
  20. // HACK: The tiny mce source code modal contain a textarea, but we
  21. // can't edit it (like it's readonly). The following solution
  22. // solve the bug: https://stackoverflow.com/questions/36952148/tinymce-code-editor-is-readonly-in-jtable-grid
  23. $(document).on('focusin', function(e) {
  24. if ($(e.target).closest(".mce-window").length) {
  25. e.stopImmediatePropagation();
  26. }
  27. });
  28. tinymce.init({
  29. selector: selector,
  30. menubar: false,
  31. resize: false,
  32. skin: "lightgray",
  33. plugins:'advlist autolink lists link image charmap print preview anchor textcolor searchreplace visualblocks code fullscreen insertdatetime media table contextmenu paste code help',
  34. toolbar: 'insert | formatselect | bold italic underline strikethrough forecolor backcolor | link | alignleft aligncenter alignright alignjustify | numlist bullist outdent indent | table | code ',
  35. content_style: "div {height: 100%;}",
  36. setup: function ($editor) {
  37. $editor.on('change', function(e) {
  38. handleOnChange({target: {value: $editor.getContent()}}) // target.value to emulate a js event so the react handler can expect one
  39. })
  40. //////////////////////////////////////////////
  41. // add custom btn to handle image by selecting them with system explorer
  42. $editor.addButton('customInsertImage', {
  43. icon: 'mce-ico mce-i-image',
  44. title: 'Image',
  45. onclick: function () {
  46. if ($('#hidden_tinymce_fileinput').length > 0) $('#hidden_tinymce_fileinput').remove()
  47. fileTag = document.createElement('input')
  48. fileTag.id = 'hidden_tinymce_fileinput'
  49. fileTag.type = 'file'
  50. $('body').append(fileTag)
  51. $('#hidden_tinymce_fileinput').on('change', function () {
  52. base64EncodeAndTinyMceInsert($(this)[0].files)
  53. })
  54. $('#hidden_tinymce_fileinput').click()
  55. }
  56. })
  57. //////////////////////////////////////////////
  58. // Handle drag & drop image into TinyMce by encoding them in base64 (to avoid uploading them somewhere and keep saving comment in string format)
  59. $editor
  60. .on('drag dragstart dragend dragover dragenter dragleave drop', function (e) {
  61. e.preventDefault()
  62. e.stopPropagation()
  63. })
  64. .on('drop', function(e) {
  65. base64EncodeAndTinyMceInsert(e.dataTransfer.files)
  66. })
  67. }
  68. })
  69. }
  70. })()