| 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 — CKEditor Sample</title> |
|---|
| 10 | <script src="../ckeditor.js"></script> |
|---|
| 11 | <link href="old/sample.css" rel="stylesheet"> |
|---|
| 12 | <script> |
|---|
| 13 | |
|---|
| 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 | |
|---|
| 20 | // Show this sample buttons. |
|---|
| 21 | document.getElementById( 'eButtons' ).style.display = 'block'; |
|---|
| 22 | }); |
|---|
| 23 | |
|---|
| 24 | function InsertHTML() { |
|---|
| 25 | // Get the editor instance that we want to interact with. |
|---|
| 26 | var editor = CKEDITOR.instances.editor1; |
|---|
| 27 | var value = document.getElementById( 'htmlArea' ).value; |
|---|
| 28 | |
|---|
| 29 | // Check the active editing mode. |
|---|
| 30 | if ( editor.mode == 'wysiwyg' ) |
|---|
| 31 | { |
|---|
| 32 | // Insert HTML code. |
|---|
| 33 | // http://docs.ckeditor.com/#!/api/CKEDITOR.editor-method-insertHtml |
|---|
| 34 | editor.insertHtml( value ); |
|---|
| 35 | } |
|---|
| 36 | else |
|---|
| 37 | alert( 'You must be in WYSIWYG mode!' ); |
|---|
| 38 | } |
|---|
| 39 | |
|---|
| 40 | function InsertText() { |
|---|
| 41 | // Get the editor instance that we want to interact with. |
|---|
| 42 | var editor = CKEDITOR.instances.editor1; |
|---|
| 43 | var value = document.getElementById( 'txtArea' ).value; |
|---|
| 44 | |
|---|
| 45 | // Check the active editing mode. |
|---|
| 46 | if ( editor.mode == 'wysiwyg' ) |
|---|
| 47 | { |
|---|
| 48 | // Insert as plain text. |
|---|
| 49 | // http://docs.ckeditor.com/#!/api/CKEDITOR.editor-method-insertText |
|---|
| 50 | editor.insertText( value ); |
|---|
| 51 | } |
|---|
| 52 | else |
|---|
| 53 | alert( 'You must be in WYSIWYG mode!' ); |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | function SetContents() { |
|---|
| 57 | // Get the editor instance that we want to interact with. |
|---|
| 58 | var editor = CKEDITOR.instances.editor1; |
|---|
| 59 | var value = document.getElementById( 'htmlArea' ).value; |
|---|
| 60 | |
|---|
| 61 | // Set editor contents (replace current contents). |
|---|
| 62 | // http://docs.ckeditor.com/#!/api/CKEDITOR.editor-method-setData |
|---|
| 63 | editor.setData( value ); |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | function GetContents() { |
|---|
| 67 | // Get the editor instance that you want to interact with. |
|---|
| 68 | var editor = CKEDITOR.instances.editor1; |
|---|
| 69 | |
|---|
| 70 | // Get editor contents |
|---|
| 71 | // http://docs.ckeditor.com/#!/api/CKEDITOR.editor-method-getData |
|---|
| 72 | alert( editor.getData() ); |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | function ExecuteCommand( commandName ) { |
|---|
| 76 | // Get the editor instance that we want to interact with. |
|---|
| 77 | var editor = CKEDITOR.instances.editor1; |
|---|
| 78 | |
|---|
| 79 | // Check the active editing mode. |
|---|
| 80 | if ( editor.mode == 'wysiwyg' ) |
|---|
| 81 | { |
|---|
| 82 | // Execute the command. |
|---|
| 83 | // http://docs.ckeditor.com/#!/api/CKEDITOR.editor-method-execCommand |
|---|
| 84 | editor.execCommand( commandName ); |
|---|
| 85 | } |
|---|
| 86 | else |
|---|
| 87 | alert( 'You must be in WYSIWYG mode!' ); |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | function printCloneContents(){ |
|---|
| 91 | var editor = CKEDITOR.instances.editor1; |
|---|
| 92 | var range = editor.getSelection().getRanges()[0]; |
|---|
| 93 | var docFragment = range.cloneContents(); |
|---|
| 94 | console.log(docFragment.getHtml()); |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | function printExtractContents(){ |
|---|
| 98 | var editor = CKEDITOR.instances.editor1; |
|---|
| 99 | var range = editor.getSelection().getRanges()[0]; |
|---|
| 100 | var docFragment = range.extractContents(); |
|---|
| 101 | console.log(docFragment.getHtml()); |
|---|
| 102 | console.log(docFragment); |
|---|
| 103 | } |
|---|
| 104 | function CheckDirty() { |
|---|
| 105 | // Get the editor instance that we want to interact with. |
|---|
| 106 | var editor = CKEDITOR.instances.editor1; |
|---|
| 107 | // Checks whether the current editor contents present changes when compared |
|---|
| 108 | // to the contents loaded into the editor at startup |
|---|
| 109 | // http://docs.ckeditor.com/#!/api/CKEDITOR.editor-method-checkDirty |
|---|
| 110 | alert( editor.checkDirty() ); |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | function ResetDirty() { |
|---|
| 114 | // Get the editor instance that we want to interact with. |
|---|
| 115 | var editor = CKEDITOR.instances.editor1; |
|---|
| 116 | // Resets the "dirty state" of the editor (see CheckDirty()) |
|---|
| 117 | // http://docs.ckeditor.com/#!/api/CKEDITOR.editor-method-resetDirty |
|---|
| 118 | editor.resetDirty(); |
|---|
| 119 | alert( 'The "IsDirty" status has been reset' ); |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | function Focus() { |
|---|
| 123 | CKEDITOR.instances.editor1.focus(); |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | function onFocus() { |
|---|
| 127 | document.getElementById( 'eMessage' ).innerHTML = '<b>' + this.name + ' is focused </b>'; |
|---|
| 128 | } |
|---|
| 129 | |
|---|
| 130 | function onBlur() { |
|---|
| 131 | document.getElementById( 'eMessage' ).innerHTML = this.name + ' lost focus'; |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | </script> |
|---|
| 135 | |
|---|
| 136 | </head> |
|---|
| 137 | <body> |
|---|
| 138 | <h1 class="samples"> |
|---|
| 139 | <a href="index.html">CKEditor Samples</a> » Using CKEditor JavaScript API |
|---|
| 140 | </h1> |
|---|
| 141 | <div class="warning deprecated"> |
|---|
| 142 | 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>. |
|---|
| 143 | </div> |
|---|
| 144 | <div class="description"> |
|---|
| 145 | <p> |
|---|
| 146 | This sample shows how to use the |
|---|
| 147 | <a class="samples" href="http://docs.ckeditor.com/#!/api/CKEDITOR.editor">CKEditor JavaScript API</a> |
|---|
| 148 | to interact with the editor at runtime. |
|---|
| 149 | </p> |
|---|
| 150 | <p> |
|---|
| 151 | For details on how to create this setup check the source code of this sample page. |
|---|
| 152 | </p> |
|---|
| 153 | </div> |
|---|
| 154 | |
|---|
| 155 | <!-- This <div> holds alert messages to be display in the sample page. --> |
|---|
| 156 | <div id="alerts"> |
|---|
| 157 | <noscript> |
|---|
| 158 | <p> |
|---|
| 159 | <strong>CKEditor requires JavaScript to run</strong>. In a browser with no JavaScript |
|---|
| 160 | support, like yours, you should still see the contents (HTML data) and you should |
|---|
| 161 | be able to edit it normally, without a rich editor interface. |
|---|
| 162 | </p> |
|---|
| 163 | </noscript> |
|---|
| 164 | </div> |
|---|
| 165 | <form action="../../../samples/sample_posteddata.php" method="post"> |
|---|
| 166 | <textarea cols="100" id="editor1" name="editor1" rows="5"><p>This is some <strong>sample text</strong>. You are using <a href="http://ckeditor.com/">CKEditor</a>.</p></textarea> |
|---|
| 167 | |
|---|
| 168 | <script> |
|---|
| 169 | // Replace the <textarea id="editor1"> with an CKEditor instance. |
|---|
| 170 | CKEDITOR.replace( 'editor1', { |
|---|
| 171 | extraPlugins:'divarea', |
|---|
| 172 | on: { |
|---|
| 173 | focus: onFocus, |
|---|
| 174 | blur: onBlur, |
|---|
| 175 | |
|---|
| 176 | // Check for availability of corresponding plugins. |
|---|
| 177 | pluginsLoaded: function( evt ) { |
|---|
| 178 | var doc = CKEDITOR.document, ed = evt.editor; |
|---|
| 179 | if ( !ed.getCommand( 'bold' ) ) |
|---|
| 180 | doc.getById( 'exec-bold' ).hide(); |
|---|
| 181 | if ( !ed.getCommand( 'link' ) ) |
|---|
| 182 | doc.getById( 'exec-link' ).hide(); |
|---|
| 183 | } |
|---|
| 184 | } |
|---|
| 185 | }); |
|---|
| 186 | </script> |
|---|
| 187 | |
|---|
| 188 | <p id="eMessage"> |
|---|
| 189 | </p> |
|---|
| 190 | |
|---|
| 191 | <div id="eButtons" style="display: none"> |
|---|
| 192 | <input onclick="printExtractContents();" type="button" value="Print Extract Contents"> |
|---|
| 193 | <input id="exec-bold" onclick="ExecuteCommand('bold');" type="button" value="Execute "bold" Command"> |
|---|
| 194 | <input id="exec-link" onclick="ExecuteCommand('link');" type="button" value="Execute "link" Command"> |
|---|
| 195 | <input onclick="Focus();" type="button" value="Focus"> |
|---|
| 196 | <br><br> |
|---|
| 197 | <input onclick="InsertHTML();" type="button" value="Insert HTML"> |
|---|
| 198 | <input onclick="SetContents();" type="button" value="Set Editor Contents"> |
|---|
| 199 | <input onclick="GetContents();" type="button" value="Get Editor Contents (HTML)"> |
|---|
| 200 | <br> |
|---|
| 201 | <textarea cols="100" id="htmlArea" rows="3"><h2>Test</h2><p>This is some <a href="/Test1.html">sample</a> HTML code.</p></textarea> |
|---|
| 202 | <br> |
|---|
| 203 | <br> |
|---|
| 204 | <input onclick="InsertText();" type="button" value="Insert Text"> |
|---|
| 205 | <br> |
|---|
| 206 | <textarea cols="100" id="txtArea" rows="3"> First line with some leading whitespaces. |
|---|
| 207 | |
|---|
| 208 | Second line of text preceded by two line breaks.</textarea> |
|---|
| 209 | <br> |
|---|
| 210 | <br> |
|---|
| 211 | <input onclick="CheckDirty();" type="button" value="checkDirty()"> |
|---|
| 212 | <input onclick="ResetDirty();" type="button" value="resetDirty()"> |
|---|
| 213 | </div> |
|---|
| 214 | </form> |
|---|
| 215 | <div id="footer"> |
|---|
| 216 | <hr> |
|---|
| 217 | <p> |
|---|
| 218 | CKEditor - The text editor for the Internet - <a class="samples" href="http://ckeditor.com/">http://ckeditor.com</a> |
|---|
| 219 | </p> |
|---|
| 220 | <p id="copy"> |
|---|
| 221 | Copyright © 2003-2017, <a class="samples" href="http://cksource.com/">CKSource</a> - Frederico |
|---|
| 222 | Knabben. All rights reserved. |
|---|
| 223 | </p> |
|---|
| 224 | </div> |
|---|
| 225 | </body> |
|---|
| 226 | </html> |
|---|