api.html 7.2 KB

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