You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

210 lines
6.8 KiB

7 years ago
  1. <!DOCTYPE html>
  2. <!--
  3. Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  4. For licensing, see LICENSE.md or http://ckeditor.com/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. // http://docs.ckeditor.com/#!/api/CKEDITOR.editor-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. // http://docs.ckeditor.com/#!/api/CKEDITOR.editor-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. // http://docs.ckeditor.com/#!/api/CKEDITOR.editor-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. // http://docs.ckeditor.com/#!/api/CKEDITOR.editor-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. // http://docs.ckeditor.com/#!/api/CKEDITOR.editor-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. // http://docs.ckeditor.com/#!/api/CKEDITOR.editor-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. // http://docs.ckeditor.com/#!/api/CKEDITOR.editor-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="http://sdk.ckeditor.com/samples/api.html">brand new version in CKEditor SDK</a>.
  110. </div>
  111. <div class="description">
  112. <p>
  113. This sample shows how to use the
  114. <a class="samples" href="http://docs.ckeditor.com/#!/api/CKEDITOR.editor">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="http://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="http://ckeditor.com/">http://ckeditor.com</a>
  178. </p>
  179. <p id="copy">
  180. Copyright &copy; 2003-2017, <a class="samples" href="http://cksource.com/">CKSource</a> - Frederico
  181. Knabben. All rights reserved.
  182. </p>
  183. </div>
  184. </body>
  185. </html>