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); |
---|
95 | //console.log(docFragment.getHtml()); |
---|
96 | } |
---|
97 | function CheckDirty() { |
---|
98 | // Get the editor instance that we want to interact with. |
---|
99 | var editor = CKEDITOR.instances.editor1; |
---|
100 | // Checks whether the current editor contents present changes when compared |
---|
101 | // to the contents loaded into the editor at startup |
---|
102 | // http://docs.ckeditor.com/#!/api/CKEDITOR.editor-method-checkDirty |
---|
103 | alert( editor.checkDirty() ); |
---|
104 | } |
---|
105 | |
---|
106 | function ResetDirty() { |
---|
107 | // Get the editor instance that we want to interact with. |
---|
108 | var editor = CKEDITOR.instances.editor1; |
---|
109 | // Resets the "dirty state" of the editor (see CheckDirty()) |
---|
110 | // http://docs.ckeditor.com/#!/api/CKEDITOR.editor-method-resetDirty |
---|
111 | editor.resetDirty(); |
---|
112 | alert( 'The "IsDirty" status has been reset' ); |
---|
113 | } |
---|
114 | |
---|
115 | function Focus() { |
---|
116 | CKEDITOR.instances.editor1.focus(); |
---|
117 | } |
---|
118 | |
---|
119 | function onFocus() { |
---|
120 | document.getElementById( 'eMessage' ).innerHTML = '<b>' + this.name + ' is focused </b>'; |
---|
121 | } |
---|
122 | |
---|
123 | function onBlur() { |
---|
124 | document.getElementById( 'eMessage' ).innerHTML = this.name + ' lost focus'; |
---|
125 | } |
---|
126 | |
---|
127 | </script> |
---|
128 | |
---|
129 | </head> |
---|
130 | <body> |
---|
131 | <h1 class="samples"> |
---|
132 | <a href="index.html">CKEditor Samples</a> » Using CKEditor JavaScript API |
---|
133 | </h1> |
---|
134 | <div class="warning deprecated"> |
---|
135 | 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>. |
---|
136 | </div> |
---|
137 | <div class="description"> |
---|
138 | <p> |
---|
139 | This sample shows how to use the |
---|
140 | <a class="samples" href="http://docs.ckeditor.com/#!/api/CKEDITOR.editor">CKEditor JavaScript API</a> |
---|
141 | to interact with the editor at runtime. |
---|
142 | </p> |
---|
143 | <p> |
---|
144 | For details on how to create this setup check the source code of this sample page. |
---|
145 | </p> |
---|
146 | </div> |
---|
147 | |
---|
148 | <!-- This <div> holds alert messages to be display in the sample page. --> |
---|
149 | <div id="alerts"> |
---|
150 | <noscript> |
---|
151 | <p> |
---|
152 | <strong>CKEditor requires JavaScript to run</strong>. In a browser with no JavaScript |
---|
153 | support, like yours, you should still see the contents (HTML data) and you should |
---|
154 | be able to edit it normally, without a rich editor interface. |
---|
155 | </p> |
---|
156 | </noscript> |
---|
157 | </div> |
---|
158 | <form action="../../../samples/sample_posteddata.php" method="post"> |
---|
159 | <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> |
---|
160 | |
---|
161 | <script> |
---|
162 | // Replace the <textarea id="editor1"> with an CKEditor instance. |
---|
163 | CKEDITOR.replace( 'editor1', { |
---|
164 | extraPlugins:'divarea', |
---|
165 | on: { |
---|
166 | focus: onFocus, |
---|
167 | blur: onBlur, |
---|
168 | |
---|
169 | // Check for availability of corresponding plugins. |
---|
170 | pluginsLoaded: function( evt ) { |
---|
171 | var doc = CKEDITOR.document, ed = evt.editor; |
---|
172 | if ( !ed.getCommand( 'bold' ) ) |
---|
173 | doc.getById( 'exec-bold' ).hide(); |
---|
174 | if ( !ed.getCommand( 'link' ) ) |
---|
175 | doc.getById( 'exec-link' ).hide(); |
---|
176 | } |
---|
177 | } |
---|
178 | }); |
---|
179 | </script> |
---|
180 | |
---|
181 | <p id="eMessage"> |
---|
182 | </p> |
---|
183 | |
---|
184 | <div id="eButtons" style="display: none"> |
---|
185 | <input onclick="printCloneContents();" type="button" value="Print Clone Contents"> |
---|
186 | <input id="exec-bold" onclick="ExecuteCommand('bold');" type="button" value="Execute "bold" Command"> |
---|
187 | <input id="exec-link" onclick="ExecuteCommand('link');" type="button" value="Execute "link" Command"> |
---|
188 | <input onclick="Focus();" type="button" value="Focus"> |
---|
189 | <br><br> |
---|
190 | <input onclick="InsertHTML();" type="button" value="Insert HTML"> |
---|
191 | <input onclick="SetContents();" type="button" value="Set Editor Contents"> |
---|
192 | <input onclick="GetContents();" type="button" value="Get Editor Contents (HTML)"> |
---|
193 | <br> |
---|
194 | <textarea cols="100" id="htmlArea" rows="3"><h2>Test</h2><p>This is some <a href="/Test1.html">sample</a> HTML code.</p></textarea> |
---|
195 | <br> |
---|
196 | <br> |
---|
197 | <input onclick="InsertText();" type="button" value="Insert Text"> |
---|
198 | <br> |
---|
199 | <textarea cols="100" id="txtArea" rows="3"> First line with some leading whitespaces. |
---|
200 | |
---|
201 | Second line of text preceded by two line breaks.</textarea> |
---|
202 | <br> |
---|
203 | <br> |
---|
204 | <input onclick="CheckDirty();" type="button" value="checkDirty()"> |
---|
205 | <input onclick="ResetDirty();" type="button" value="resetDirty()"> |
---|
206 | </div> |
---|
207 | </form> |
---|
208 | <div id="footer"> |
---|
209 | <hr> |
---|
210 | <p> |
---|
211 | CKEditor - The text editor for the Internet - <a class="samples" href="http://ckeditor.com/">http://ckeditor.com</a> |
---|
212 | </p> |
---|
213 | <p id="copy"> |
---|
214 | Copyright © 2003-2017, <a class="samples" href="http://cksource.com/">CKSource</a> - Frederico |
---|
215 | Knabben. All rights reserved. |
---|
216 | </p> |
---|
217 | </div> |
---|
218 | </body> |
---|
219 | </html> |
---|