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.

207 lines
6.8 KiB

  1. <!DOCTYPE html>
  2. <!--
  3. Copyright (c) 2003-2014, 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="description">
  109. <p>
  110. This sample shows how to use the
  111. <a class="samples" href="http://docs.ckeditor.com/#!/api/CKEDITOR.editor">CKEditor JavaScript API</a>
  112. to interact with the editor at runtime.
  113. </p>
  114. <p>
  115. For details on how to create this setup check the source code of this sample page.
  116. </p>
  117. </div>
  118. <!-- This <div> holds alert messages to be display in the sample page. -->
  119. <div id="alerts">
  120. <noscript>
  121. <p>
  122. <strong>CKEditor requires JavaScript to run</strong>. In a browser with no JavaScript
  123. support, like yours, you should still see the contents (HTML data) and you should
  124. be able to edit it normally, without a rich editor interface.
  125. </p>
  126. </noscript>
  127. </div>
  128. <form action="../../../samples/sample_posteddata.php" method="post">
  129. <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>
  130. <script>
  131. // Replace the <textarea id="editor1"> with an CKEditor instance.
  132. CKEDITOR.replace( 'editor1', {
  133. on: {
  134. focus: onFocus,
  135. blur: onBlur,
  136. // Check for availability of corresponding plugins.
  137. pluginsLoaded: function( evt ) {
  138. var doc = CKEDITOR.document, ed = evt.editor;
  139. if ( !ed.getCommand( 'bold' ) )
  140. doc.getById( 'exec-bold' ).hide();
  141. if ( !ed.getCommand( 'link' ) )
  142. doc.getById( 'exec-link' ).hide();
  143. }
  144. }
  145. });
  146. </script>
  147. <p id="eMessage">
  148. </p>
  149. <div id="eButtons" style="display: none">
  150. <input id="exec-bold" onclick="ExecuteCommand('bold');" type="button" value="Execute &quot;bold&quot; Command">
  151. <input id="exec-link" onclick="ExecuteCommand('link');" type="button" value="Execute &quot;link&quot; Command">
  152. <input onclick="Focus();" type="button" value="Focus">
  153. <br><br>
  154. <input onclick="InsertHTML();" type="button" value="Insert HTML">
  155. <input onclick="SetContents();" type="button" value="Set Editor Contents">
  156. <input onclick="GetContents();" type="button" value="Get Editor Contents (HTML)">
  157. <br>
  158. <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>
  159. <br>
  160. <br>
  161. <input onclick="InsertText();" type="button" value="Insert Text">
  162. <br>
  163. <textarea cols="100" id="txtArea" rows="3"> First line with some leading whitespaces.
  164. Second line of text preceded by two line breaks.</textarea>
  165. <br>
  166. <br>
  167. <input onclick="CheckDirty();" type="button" value="checkDirty()">
  168. <input onclick="ResetDirty();" type="button" value="resetDirty()">
  169. </div>
  170. </form>
  171. <div id="footer">
  172. <hr>
  173. <p>
  174. CKEditor - The text editor for the Internet - <a class="samples" href="http://ckeditor.com/">http://ckeditor.com</a>
  175. </p>
  176. <p id="copy">
  177. Copyright &copy; 2003-2014, <a class="samples" href="http://cksource.com/">CKSource</a> - Frederico
  178. Knabben. All rights reserved.
  179. </p>
  180. </div>
  181. </body>
  182. </html>