tinymceInit.js 3.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. forced_root_block : "",
  30. selector: selector,
  31. menubar: false,
  32. resize: false,
  33. skin: "lightgray",
  34. plugins:'advlist autolink lists link image charmap print preview anchor textcolor searchreplace visualblocks code fullscreen insertdatetime media table contextmenu paste code help',
  35. toolbar: 'insert | formatselect | bold italic underline strikethrough forecolor backcolor | link | alignleft aligncenter alignright alignjustify | numlist bullist outdent indent | table | code ',
  36. content_style: "div {height: 100%;}",
  37. setup: function ($editor) {
  38. $editor.on('change', function(e) {
  39. handleOnChange({target: {value: $editor.getContent()}}) // target.value to emulate a js event so the react handler can expect one
  40. })
  41. //////////////////////////////////////////////
  42. // add custom btn to handle image by selecting them with system explorer
  43. $editor.addButton('customInsertImage', {
  44. icon: 'mce-ico mce-i-image',
  45. title: 'Image',
  46. onclick: function () {
  47. if ($('#hidden_tinymce_fileinput').length > 0) $('#hidden_tinymce_fileinput').remove()
  48. fileTag = document.createElement('input')
  49. fileTag.id = 'hidden_tinymce_fileinput'
  50. fileTag.type = 'file'
  51. $('body').append(fileTag)
  52. $('#hidden_tinymce_fileinput').on('change', function () {
  53. base64EncodeAndTinyMceInsert($(this)[0].files)
  54. })
  55. $('#hidden_tinymce_fileinput').click()
  56. }
  57. })
  58. //////////////////////////////////////////////
  59. // Handle drag & drop image into TinyMce by encoding them in base64 (to avoid uploading them somewhere and keep saving comment in string format)
  60. $editor
  61. .on('drag dragstart dragend dragover dragenter dragleave drop', function (e) {
  62. e.preventDefault()
  63. e.stopPropagation()
  64. })
  65. .on('drop', function(e) {
  66. base64EncodeAndTinyMceInsert(e.dataTransfer.files)
  67. })
  68. }
  69. })
  70. }
  71. })()