dialog.html 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <!DOCTYPE html>
  2. <!--
  3. Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
  4. For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  5. -->
  6. <html lang="en">
  7. <head>
  8. <meta charset="utf-8">
  9. <title>Using API to Customize Dialog Windows &mdash; CKEditor Sample</title>
  10. <script src="../../../ckeditor.js"></script>
  11. <link rel="stylesheet" href="../../../samples/old/sample.css">
  12. <meta name="ckeditor-sample-name" content="Using the JavaScript API to customize dialog windows">
  13. <meta name="ckeditor-sample-group" content="Advanced Samples">
  14. <meta name="ckeditor-sample-description" content="Using the dialog windows API to customize dialog windows without changing the original editor code.">
  15. <meta name="description" content="Try the latest sample of CKEditor 4 and learn more about customizing your WYSIWYG editor with endless possibilities.">
  16. <style>
  17. .cke_button__mybutton_icon
  18. {
  19. display: none !important;
  20. }
  21. .cke_button__mybutton_label
  22. {
  23. display: inline !important;
  24. }
  25. </style>
  26. <script>
  27. CKEDITOR.on( 'instanceCreated', function( ev ){
  28. var editor = ev.editor;
  29. // Listen for the "pluginsLoaded" event, so we are sure that the
  30. // "dialog" plugin has been loaded and we are able to do our
  31. // customizations.
  32. editor.on( 'pluginsLoaded', function() {
  33. // If our custom dialog has not been registered, do that now.
  34. if ( !CKEDITOR.dialog.exists( 'myDialog' ) ) {
  35. // We need to do the following trick to find out the dialog
  36. // definition file URL path. In the real world, you would simply
  37. // point to an absolute path directly, like "/mydir/mydialog.js".
  38. var href = document.location.href.split( '/' );
  39. href.pop();
  40. href.push( 'assets/my_dialog.js' );
  41. href = href.join( '/' );
  42. // Finally, register the dialog.
  43. CKEDITOR.dialog.add( 'myDialog', href );
  44. }
  45. // Register the command used to open the dialog.
  46. editor.addCommand( 'myDialogCmd', new CKEDITOR.dialogCommand( 'myDialog' ) );
  47. // Add the a custom toolbar buttons, which fires the above
  48. // command..
  49. editor.ui.add( 'MyButton', CKEDITOR.UI_BUTTON, {
  50. label: 'My Dialog',
  51. command: 'myDialogCmd'
  52. });
  53. });
  54. });
  55. // When opening a dialog, its "definition" is created for it, for
  56. // each editor instance. The "dialogDefinition" event is then
  57. // fired. We should use this event to make customizations to the
  58. // definition of existing dialogs.
  59. CKEDITOR.on( 'dialogDefinition', function( ev ) {
  60. // Take the dialog name and its definition from the event data.
  61. var dialogName = ev.data.name;
  62. var dialogDefinition = ev.data.definition;
  63. // Check if the definition is from the dialog we're
  64. // interested on (the "Link" dialog).
  65. if ( dialogName == 'myDialog' && ev.editor.name == 'editor2' ) {
  66. // Get a reference to the "Link Info" tab.
  67. var infoTab = dialogDefinition.getContents( 'tab1' );
  68. // Add a new text field to the "tab1" tab page.
  69. infoTab.add( {
  70. type: 'text',
  71. label: 'My Custom Field',
  72. id: 'customField',
  73. 'default': 'Sample!',
  74. validate: function() {
  75. if ( ( /\d/ ).test( this.getValue() ) )
  76. return 'My Custom Field must not contain digits';
  77. }
  78. });
  79. // Remove the "select1" field from the "tab1" tab.
  80. infoTab.remove( 'select1' );
  81. // Set the default value for "input1" field.
  82. var input1 = infoTab.get( 'input1' );
  83. input1[ 'default' ] = 'www.example.com';
  84. // Remove the "tab2" tab page.
  85. dialogDefinition.removeContents( 'tab2' );
  86. // Add a new tab to the "Link" dialog.
  87. dialogDefinition.addContents( {
  88. id: 'customTab',
  89. label: 'My Tab',
  90. accessKey: 'M',
  91. elements: [
  92. {
  93. id: 'myField1',
  94. type: 'text',
  95. label: 'My Text Field'
  96. },
  97. {
  98. id: 'myField2',
  99. type: 'text',
  100. label: 'Another Text Field'
  101. }
  102. ]
  103. });
  104. // Provide the focus handler to start initial focus in "customField" field.
  105. dialogDefinition.onFocus = function() {
  106. var urlField = this.getContentElement( 'tab1', 'customField' );
  107. urlField.select();
  108. };
  109. }
  110. });
  111. var config = {
  112. extraPlugins: 'dialog',
  113. toolbar: [ [ 'MyButton' ] ]
  114. };
  115. </script>
  116. </head>
  117. <body>
  118. <h1 class="samples">
  119. <a href="../../../samples/old/index.html">CKEditor Samples</a> &raquo; Using CKEditor Dialog API
  120. </h1>
  121. <div class="warning deprecated">
  122. This sample is not maintained anymore. Check out the <a href="https://ckeditor.com/docs/ckeditor4/latest/examples/index.html">brand new samples in CKEditor Examples</a>.
  123. </div>
  124. <div class="description">
  125. <p>
  126. This sample shows how to use the
  127. <a class="samples" href="https://ckeditor.com/docs/ckeditor4/latest/api/CKEDITOR_dialog.html">CKEditor Dialog API</a>
  128. to customize CKEditor dialog windows without changing the original editor code.
  129. The following customizations are being done in the example below:
  130. </p>
  131. <p>
  132. For details on how to create this setup check the source code of this sample page.
  133. </p>
  134. </div>
  135. <p>A custom dialog is added to the editors using the <code>pluginsLoaded</code> event, from an external <a target="_blank" href="assets/my_dialog.js">dialog definition file</a>:</p>
  136. <ol>
  137. <li><strong>Creating a custom dialog window</strong> &ndash; "My Dialog" dialog window opened with the "My Dialog" toolbar button.</li>
  138. <li><strong>Creating a custom button</strong> &ndash; Add button to open the dialog with "My Dialog" toolbar button.</li>
  139. </ol>
  140. <textarea cols="80" id="editor1" name="editor1" rows="10">&lt;p&gt;This is some &lt;strong&gt;sample text&lt;/strong&gt;. You are using &lt;a href="https://ckeditor.com/"&gt;CKEditor&lt;/a&gt;.&lt;/p&gt;</textarea>
  141. <script>
  142. // Replace the <textarea id="editor1"> with an CKEditor instance.
  143. CKEDITOR.replace( 'editor1', config );
  144. </script>
  145. <p>The below editor modify the dialog definition of the above added dialog using the <code>dialogDefinition</code> event:</p>
  146. <ol>
  147. <li><strong>Adding dialog tab</strong> &ndash; Add new tab "My Tab" to dialog window.</li>
  148. <li><strong>Removing a dialog window tab</strong> &ndash; Remove "Second Tab" page from the dialog window.</li>
  149. <li><strong>Adding dialog window fields</strong> &ndash; Add "My Custom Field" to the dialog window.</li>
  150. <li><strong>Removing dialog window field</strong> &ndash; Remove "Select Field" selection field from the dialog window.</li>
  151. <li><strong>Setting default values for dialog window fields</strong> &ndash; Set default value of "Text Field" text field. </li>
  152. <li><strong>Setup initial focus for dialog window</strong> &ndash; Put initial focus on "My Custom Field" text field. </li>
  153. </ol>
  154. <textarea cols="80" id="editor2" name="editor2" rows="10">&lt;p&gt;This is some &lt;strong&gt;sample text&lt;/strong&gt;. You are using &lt;a href="https://ckeditor.com/"&gt;CKEditor&lt;/a&gt;.&lt;/p&gt;</textarea>
  155. <script>
  156. // Replace the <textarea id="editor1"> with an CKEditor instance.
  157. CKEDITOR.replace( 'editor2', config );
  158. </script>
  159. <div id="footer">
  160. <hr>
  161. <p>
  162. CKEditor - The text editor for the Internet - <a class="samples" href="https://ckeditor.com/">https://ckeditor.com</a>
  163. </p>
  164. <p id="copy">
  165. Copyright &copy; 2003-2023, <a class="samples" href="https://cksource.com/">CKSource</a> Holding sp. z o.o. All rights reserved.
  166. </p>
  167. </div>
  168. </body>
  169. </html>