Ticket #11843: api2.html

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