custom.html 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0" />
  6. <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.4.0/css/font-awesome.min.css">
  7. <link rel="stylesheet" href="../../css/froala_editor.css">
  8. <link rel="stylesheet" href="../../css/froala_style.css">
  9. <style>
  10. body {
  11. text-align: center;
  12. }
  13. section {
  14. width: 81%;
  15. margin: auto;
  16. text-align: left;
  17. }
  18. .custom-layer {
  19. text-align: center;
  20. padding: 10px;
  21. }
  22. </style>
  23. </head>
  24. <body>
  25. <section id="editor">
  26. <div id='edit' style="margin-top: 30px;">
  27. <h1>Custom Popup example</h1>
  28. <p>This is an example of how to create your own popup.</p>
  29. </div>
  30. </section>
  31. <script type="text/javascript" src="../../js/froala_editor.min.js"></script>
  32. <script>
  33. // Define popup template.
  34. Object.assign(FroalaEditor.POPUP_TEMPLATES, {
  35. 'customPlugin.popup': '[_BUTTONS_][_CUSTOM_LAYER_]'
  36. })
  37. // Define popup buttons.
  38. Object.assign(FroalaEditor.DEFAULTS, {
  39. popupButtons: ['popupClose', '|', 'popupButton1', 'popupButton2'],
  40. })
  41. // The custom popup is defined inside a plugin (new or existing).
  42. FroalaEditor.PLUGINS.customPlugin = function (editor) {
  43. // Create custom popup.
  44. function initPopup() {
  45. // Load popup template.
  46. var template = FroalaEditor.POPUP_TEMPLATES.customPopup
  47. if (typeof template == 'function') template = template.apply(editor)
  48. // Popup buttons.
  49. var popup_buttons = ''
  50. // Create the list of buttons.
  51. if (editor.opts.popupButtons.length > 1) {
  52. popup_buttons += '<div class="fr-buttons fr-tabs">'
  53. popup_buttons += editor.button.buildList(editor.opts.popupButtons)
  54. popup_buttons += '</div>'
  55. }
  56. // Load popup template.
  57. var template = {
  58. buttons: popup_buttons,
  59. custom_layer: '<div class="custom-layer">Hello World!</div>'
  60. }
  61. // Create popup.
  62. var $popup = editor.popups.create('customPlugin.popup', template)
  63. return $popup
  64. }
  65. // Show the popup
  66. function showPopup() {
  67. // Get the popup object defined above.
  68. var $popup = editor.popups.get('customPlugin.popup')
  69. // If popup doesn't exist then create it.
  70. // To improve performance it is best to create the popup when it is first needed
  71. // and not when the editor is initialized.
  72. if (!$popup) $popup = initPopup()
  73. // Set the editor toolbar as the popup's container.
  74. editor.popups.setContainer('customPlugin.popup', editor.$tb)
  75. // If the editor is not displayed when a toolbar button is pressed, then set BODY as the popup's container.
  76. // editor.popups.setContainer('customPlugin.popup', $('body'));
  77. // Trigger refresh for the popup.
  78. // editor.popups.refresh('customPlugin.popup');
  79. // This custom popup is opened by pressing a button from the editor's toolbar.
  80. // Get the button's object in order to place the popup relative to it.
  81. var $btn = editor.$tb.find('.fr-command[data-cmd="myButton"]')
  82. // Compute the popup's position.
  83. var left = $btn.offset().left
  84. var top = $btn.offset().top + (editor.opts.toolbarBottom ? 10 : $btn.outerHeight() - 10)
  85. // Show the custom popup.
  86. // The button's outerHeight is required in case the popup needs to be displayed above it.
  87. editor.popups.show('customPlugin.popup', left, top, $btn.outerHeight())
  88. }
  89. // Hide the custom popup.
  90. function hidePopup() {
  91. editor.popups.hide('customPlugin.popup')
  92. }
  93. // Methods visible outside the plugin.
  94. return {
  95. showPopup: showPopup,
  96. hidePopup: hidePopup
  97. }
  98. }
  99. // Define an icon and command for the button that opens the custom popup.
  100. FroalaEditor.DefineIcon('buttonIcon', { NAME: 'star', SVG_KEY: 'star' })
  101. FroalaEditor.RegisterCommand('myButton', {
  102. title: 'Show Popup',
  103. icon: 'buttonIcon',
  104. undo: false,
  105. focus: false,
  106. popup: true,
  107. // Buttons which are included in the editor toolbar should have the plugin property set.
  108. plugin: 'customPlugin',
  109. callback: function () {
  110. if (!this.popups.isVisible('customPlugin.popup')) {
  111. this.customPlugin.showPopup();
  112. }
  113. else {
  114. if (this.$el.find('.fr-marker')) {
  115. this.events.disableBlur()
  116. this.selection.restore()
  117. }
  118. this.popups.hide('customPlugin.popup')
  119. }
  120. }
  121. })
  122. // Define custom popup close button icon and command.
  123. FroalaEditor.DefineIcon('popupClose', { NAME: 'times', SVG_KEY: 'close' });
  124. FroalaEditor.RegisterCommand('popupClose', {
  125. title: 'Close',
  126. undo: false,
  127. focus: false,
  128. callback: function () {
  129. this.customPlugin.hidePopup();
  130. }
  131. })
  132. // Define custom popup 1.
  133. FroalaEditor.DefineIcon('popupButton1', { NAME: 'bell-o', SVG_KEY: 'anchors' });
  134. FroalaEditor.RegisterCommand('popupButton1', {
  135. title: 'Button 1',
  136. undo: false,
  137. focus: false,
  138. callback: function () {
  139. alert("popupButton1 was pressed");
  140. }
  141. })
  142. // Define custom popup 2.
  143. FroalaEditor.DefineIcon('popupButton2', { NAME: 'bullhorn', SVG_KEY: 'tags' });
  144. FroalaEditor.RegisterCommand('popupButton2', {
  145. title: 'Button 2',
  146. undo: false,
  147. focus: false,
  148. callback: function () {
  149. alert("popupButton2")
  150. }
  151. })
  152. new FroalaEditor("#edit", {
  153. toolbarButtons: [ ['bold', 'italic', 'underline', 'strikeThrough', 'myButton'], ['undo', 'redo'] ],
  154. pluginsEnabled: ['customPlugin']
  155. })
  156. </script>
  157. </body>
  158. </html>