plugin.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /***
  2. * Bootstrap 4 Embed Media Dialog for ckEditor 4 based on Fabian Vogelsteller's Embed Media Dialog on https://github.com/frozeman/MediaEmbed
  3. *
  4. * Wrap any embed like an <iframe> in a parent element with .embed-responsive and an aspect ratio.
  5. * The plugin allows you to insert multiple iframes at the same time, managed as separate elements.
  6. * Bootstrap classes for responsive embed are applied only to iframe, video, embed and object tags.
  7. * The other tags are not modified in any way.
  8. *
  9. * Plugin name: bootstrapMediaEmbed
  10. * Menu button name: BootstrapMediaEmbed
  11. *
  12. * Youtube Editor Icon
  13. * http://paulrobertlloyd.com/
  14. *
  15. * @author Domenico Gigante [reloadlab.it]
  16. * @version 1.0
  17. ***/
  18. (function(){
  19. CKEDITOR.plugins.add('bootstrapMediaEmbed',
  20. {
  21. icons: 'bootstrapmediaembed', // %REMOVE_LINE_CORE%
  22. hidpi: true, // %REMOVE_LINE_CORE%
  23. lang: 'en,it',
  24. init: function(editor){
  25. // Bootstrap Embedable element
  26. var embedable = [
  27. 'iframe',
  28. 'embed',
  29. 'video',
  30. 'object'
  31. ];
  32. // Bootstrap embed responsive ratio
  33. var ratio = [
  34. 'embed-responsive-21by9',
  35. 'embed-responsive-16by9',
  36. 'embed-responsive-4by3',
  37. 'embed-responsive-1by1'
  38. ];
  39. var me = this;
  40. CKEDITOR.dialog.add('BootstrapMediaEmbedDialog', function(instance){
  41. return {
  42. title: editor.lang.bootstrapMediaEmbed.dialogTitle,
  43. minWidth: 550,
  44. minHeight: 200,
  45. contents: [
  46. {
  47. id: 'iframe',
  48. expand: true,
  49. elements: [
  50. {
  51. id: 'embedArea',
  52. type: 'textarea',
  53. controlStyle: 'height:100%',
  54. inputStyle: 'height:100%',
  55. rows: 4,
  56. label: editor.lang.bootstrapMediaEmbed.dialogArea,
  57. 'autofocus': 'autofocus',
  58. setup: function(element){
  59. },
  60. commit: function(element){
  61. }
  62. },
  63. {
  64. id: 'selectRatio',
  65. type: 'select',
  66. label: editor.lang.bootstrapMediaEmbed.dialogRatio,
  67. items: [
  68. [editor.lang.bootstrapMediaEmbed.selectRatio[0],'none'],
  69. [editor.lang.bootstrapMediaEmbed.selectRatio[1],0],
  70. [editor.lang.bootstrapMediaEmbed.selectRatio[2],1],
  71. [editor.lang.bootstrapMediaEmbed.selectRatio[3],2],
  72. [editor.lang.bootstrapMediaEmbed.selectRatio[4],3]
  73. ],
  74. 'default': 'none'
  75. }
  76. ]
  77. }
  78. ],
  79. onOk: function(){
  80. // Embed Textarea value
  81. var ea = this.getContentElement('iframe', 'embedArea').getValue();
  82. // Responsive Ratio Select value
  83. var rr = this.getContentElement('iframe', 'selectRatio').getValue();
  84. if(ea && ea.trim() != ''){
  85. // Temporary div container
  86. var tmp = instance.document.createElement('div');
  87. tmp.setHtml(ea);
  88. // Elements to insert in Editor
  89. var ch = tmp.getChildren();
  90. var elem;
  91. while(elem = ch.getItem(0)){
  92. // Node name equal to iframe, video, embed or object
  93. if(embedable.indexOf(elem.getName()) != -1){
  94. // Container div
  95. var div = instance.document.createElement('div');
  96. // Add Bootstrap's css class for responsive design
  97. if(ratio[rr]){
  98. div.setAttribute('class', 'embed-responsive ' + ratio[rr]);
  99. elem.setAttribute('class', 'embed-responsive-item');
  100. }
  101. // Append element to container
  102. div.append(elem);
  103. // Insert into Editor
  104. instance.insertElement(div);
  105. }
  106. // None of embedable elements
  107. else{
  108. // Container div
  109. var div = instance.document.createElement('div');
  110. // Append element to container
  111. div.append(elem);
  112. // Insert into Editor
  113. instance.insertElement(div);
  114. }
  115. }
  116. // Remove temporary div to free memory
  117. tmp.remove();
  118. }
  119. }
  120. };
  121. });
  122. editor.addCommand('BootstrapMediaEmbed',
  123. new CKEDITOR.dialogCommand('BootstrapMediaEmbedDialog',
  124. {
  125. allowedContent: 'iframe[*],video[*],audio[*],source[*],embed[*],object[*]'
  126. }
  127. )
  128. );
  129. editor.ui.addButton('BootstrapMediaEmbed',
  130. {
  131. label: editor.lang.bootstrapMediaEmbed.toolbar,
  132. command: 'BootstrapMediaEmbed',
  133. toolbar: 'bootstrapMediaEmbed'
  134. }
  135. );
  136. }
  137. });
  138. })();