api.html 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <!DOCTYPE html>
  2. <!--
  3. Copyright (c) 2003-2021, CKSource - Frederico Knabben. 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>API Usage &mdash; CKEditor Sample</title>
  10. <script src="../../ckeditor.js"></script>
  11. <link href="sample.css" rel="stylesheet">
  12. <meta name="description" content="Try the latest sample of CKEditor 4 and learn more about customizing your WYSIWYG editor with endless possibilities.">
  13. <script>
  14. // The instanceReady event is fired, when an instance of CKEditor has finished
  15. // its initialization.
  16. CKEDITOR.on( 'instanceReady', function( ev ) {
  17. // Show the editor name and description in the browser status bar.
  18. document.getElementById( 'eMessage' ).innerHTML = 'Instance <code>' + ev.editor.name + '<\/code> loaded.';
  19. // Show this sample buttons.
  20. document.getElementById( 'eButtons' ).style.display = 'block';
  21. });
  22. function InsertHTML() {
  23. // Get the editor instance that we want to interact with.
  24. var editor = CKEDITOR.instances.editor1;
  25. var value = document.getElementById( 'htmlArea' ).value;
  26. // Check the active editing mode.
  27. if ( editor.mode == 'wysiwyg' )
  28. {
  29. // Insert HTML code.
  30. // https://ckeditor.com/docs/ckeditor4/latest/api/CKEDITOR_editor.html#method-insertHtml
  31. editor.insertHtml( value );
  32. }
  33. else
  34. alert( 'You must be in WYSIWYG mode!' );
  35. }
  36. function InsertText() {
  37. // Get the editor instance that we want to interact with.
  38. var editor = CKEDITOR.instances.editor1;
  39. var value = document.getElementById( 'txtArea' ).value;
  40. // Check the active editing mode.
  41. if ( editor.mode == 'wysiwyg' )
  42. {
  43. // Insert as plain text.
  44. // https://ckeditor.com/docs/ckeditor4/latest/api/CKEDITOR_editor.html#method-insertText
  45. editor.insertText( value );
  46. }
  47. else
  48. alert( 'You must be in WYSIWYG mode!' );
  49. }
  50. function SetContents() {
  51. // Get the editor instance that we want to interact with.
  52. var editor = CKEDITOR.instances.editor1;
  53. var value = document.getElementById( 'htmlArea' ).value;
  54. // Set editor contents (replace current contents).
  55. // https://ckeditor.com/docs/ckeditor4/latest/api/CKEDITOR_editor.html#method-setData
  56. editor.setData( value );
  57. }
  58. function GetContents() {
  59. // Get the editor instance that you want to interact with.
  60. var editor = CKEDITOR.instances.editor1;
  61. // Get editor contents
  62. // https://ckeditor.com/docs/ckeditor4/latest/api/CKEDITOR_editor.html#method-getData
  63. alert( editor.getData() );
  64. }
  65. function ExecuteCommand( commandName ) {
  66. // Get the editor instance that we want to interact with.
  67. var editor = CKEDITOR.instances.editor1;
  68. // Check the active editing mode.
  69. if ( editor.mode == 'wysiwyg' )
  70. {
  71. // Execute the command.
  72. // https://ckeditor.com/docs/ckeditor4/latest/api/CKEDITOR_editor.html#method-execCommand
  73. editor.execCommand( commandName );
  74. }
  75. else
  76. alert( 'You must be in WYSIWYG mode!' );
  77. }
  78. function CheckDirty() {
  79. // Get the editor instance that we want to interact with.
  80. var editor = CKEDITOR.instances.editor1;
  81. // Checks whether the current editor contents present changes when compared
  82. // to the contents loaded into the editor at startup
  83. // https://ckeditor.com/docs/ckeditor4/latest/api/CKEDITOR_editor.html#method-checkDirty
  84. alert( editor.checkDirty() );
  85. }
  86. function ResetDirty() {
  87. // Get the editor instance that we want to interact with.
  88. var editor = CKEDITOR.instances.editor1;
  89. // Resets the "dirty state" of the editor (see CheckDirty())
  90. // https://ckeditor.com/docs/ckeditor4/latest/api/CKEDITOR_editor.html#method-resetDirty
  91. editor.resetDirty();
  92. alert( 'The "IsDirty" status has been reset' );
  93. }
  94. function Focus() {
  95. CKEDITOR.instances.editor1.focus();
  96. }
  97. function onFocus() {
  98. document.getElementById( 'eMessage' ).innerHTML = '<b>' + this.name + ' is focused </b>';
  99. }
  100. function onBlur() {
  101. document.getElementById( 'eMessage' ).innerHTML = this.name + ' lost focus';
  102. }
  103. </script>
  104. </head>
  105. <body>
  106. <h1 class="samples">
  107. <a href="index.html">CKEditor Samples</a> &raquo; Using CKEditor JavaScript API
  108. </h1>
  109. <div class="warning deprecated">
  110. This sample is not maintained anymore. Check out its <a href="https://ckeditor.com/docs/ckeditor4/latest/examples/api.html">brand new version in CKEditor Examples</a>.
  111. </div>
  112. <div class="description">
  113. <p>
  114. This sample shows how to use the
  115. <a class="samples" href="https://ckeditor.com/docs/ckeditor4/latest/api/CKEDITOR_editor.html">CKEditor JavaScript API</a>
  116. to interact with the editor at runtime.
  117. </p>
  118. <p>
  119. For details on how to create this setup check the source code of this sample page.
  120. </p>
  121. </div>
  122. <!-- This <div> holds alert messages to be display in the sample page. -->
  123. <div id="alerts">
  124. <noscript>
  125. <p>
  126. <strong>CKEditor requires JavaScript to run</strong>. In a browser with no JavaScript
  127. support, like yours, you should still see the contents (HTML data) and you should
  128. be able to edit it normally, without a rich editor interface.
  129. </p>
  130. </noscript>
  131. </div>
  132. <form action="../../../samples/sample_posteddata.php" method="post">
  133. <textarea cols="100" 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>
  134. <script>
  135. // Replace the <textarea id="editor1"> with an CKEditor instance.
  136. CKEDITOR.replace( 'editor1', {
  137. on: {
  138. focus: onFocus,
  139. blur: onBlur,
  140. // Check for availability of corresponding plugins.
  141. pluginsLoaded: function( evt ) {
  142. var doc = CKEDITOR.document, ed = evt.editor;
  143. if ( !ed.getCommand( 'bold' ) )
  144. doc.getById( 'exec-bold' ).hide();
  145. if ( !ed.getCommand( 'link' ) )
  146. doc.getById( 'exec-link' ).hide();
  147. }
  148. }
  149. });
  150. </script>
  151. <p id="eMessage">
  152. </p>
  153. <div id="eButtons" style="display: none">
  154. <input id="exec-bold" onclick="ExecuteCommand('bold');" type="button" value="Execute &quot;bold&quot; Command">
  155. <input id="exec-link" onclick="ExecuteCommand('link');" type="button" value="Execute &quot;link&quot; Command">
  156. <input onclick="Focus();" type="button" value="Focus">
  157. <br><br>
  158. <input onclick="InsertHTML();" type="button" value="Insert HTML">
  159. <input onclick="SetContents();" type="button" value="Set Editor Contents">
  160. <input onclick="GetContents();" type="button" value="Get Editor Contents (HTML)">
  161. <br>
  162. <textarea cols="100" id="htmlArea" rows="3">&lt;h2&gt;Test&lt;/h2&gt;&lt;p&gt;This is some &lt;a href="/Test1.html"&gt;sample&lt;/a&gt; HTML code.&lt;/p&gt;</textarea>
  163. <br>
  164. <br>
  165. <input onclick="InsertText();" type="button" value="Insert Text">
  166. <br>
  167. <textarea cols="100" id="txtArea" rows="3"> First line with some leading whitespaces.
  168. Second line of text preceded by two line breaks.</textarea>
  169. <br>
  170. <br>
  171. <input onclick="CheckDirty();" type="button" value="checkDirty()">
  172. <input onclick="ResetDirty();" type="button" value="resetDirty()">
  173. </div>
  174. </form>
  175. <div id="footer">
  176. <hr>
  177. <p>
  178. CKEditor - The text editor for the Internet - <a class="samples" href="https://ckeditor.com/">https://ckeditor.com</a>
  179. </p>
  180. <p id="copy">
  181. Copyright &copy; 2003-2021, <a class="samples" href="https://cksource.com/">CKSource</a> - Frederico
  182. Knabben. All rights reserved.
  183. </p>
  184. </div>
  185. </body>
  186. </html>