6056 |
efrain |
1 |
<!DOCTYPE html>
|
|
|
2 |
<!--
|
|
|
3 |
Copyright (c) 2003-2021, CKSource - Frederico Knabben. All rights reserved.
|
|
|
4 |
For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
|
5 |
-->
|
|
|
6 |
<html lang="en">
|
|
|
7 |
<head>
|
|
|
8 |
<meta charset="utf-8">
|
|
|
9 |
<title>Using API to Customize Dialog Windows — CKEditor Sample</title>
|
|
|
10 |
<script src="../../../ckeditor.js"></script>
|
|
|
11 |
<link rel="stylesheet" href="../../../samples/old/sample.css">
|
|
|
12 |
<meta name="ckeditor-sample-name" content="Using the JavaScript API to customize dialog windows">
|
|
|
13 |
<meta name="ckeditor-sample-group" content="Advanced Samples">
|
|
|
14 |
<meta name="ckeditor-sample-description" content="Using the dialog windows API to customize dialog windows without changing the original editor code.">
|
|
|
15 |
<meta name="description" content="Try the latest sample of CKEditor 4 and learn more about customizing your WYSIWYG editor with endless possibilities.">
|
|
|
16 |
<style>
|
|
|
17 |
|
|
|
18 |
.cke_button__mybutton_icon
|
|
|
19 |
{
|
|
|
20 |
display: none !important;
|
|
|
21 |
}
|
|
|
22 |
|
|
|
23 |
.cke_button__mybutton_label
|
|
|
24 |
{
|
|
|
25 |
display: inline !important;
|
|
|
26 |
}
|
|
|
27 |
|
|
|
28 |
</style>
|
|
|
29 |
<script>
|
|
|
30 |
|
|
|
31 |
CKEDITOR.on( 'instanceCreated', function( ev ){
|
|
|
32 |
var editor = ev.editor;
|
|
|
33 |
|
|
|
34 |
// Listen for the "pluginsLoaded" event, so we are sure that the
|
|
|
35 |
// "dialog" plugin has been loaded and we are able to do our
|
|
|
36 |
// customizations.
|
|
|
37 |
editor.on( 'pluginsLoaded', function() {
|
|
|
38 |
|
|
|
39 |
// If our custom dialog has not been registered, do that now.
|
|
|
40 |
if ( !CKEDITOR.dialog.exists( 'myDialog' ) ) {
|
|
|
41 |
// We need to do the following trick to find out the dialog
|
|
|
42 |
// definition file URL path. In the real world, you would simply
|
|
|
43 |
// point to an absolute path directly, like "/mydir/mydialog.js".
|
|
|
44 |
var href = document.location.href.split( '/' );
|
|
|
45 |
href.pop();
|
|
|
46 |
href.push( 'assets/my_dialog.js' );
|
|
|
47 |
href = href.join( '/' );
|
|
|
48 |
|
|
|
49 |
// Finally, register the dialog.
|
|
|
50 |
CKEDITOR.dialog.add( 'myDialog', href );
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
// Register the command used to open the dialog.
|
|
|
54 |
editor.addCommand( 'myDialogCmd', new CKEDITOR.dialogCommand( 'myDialog' ) );
|
|
|
55 |
|
|
|
56 |
// Add the a custom toolbar buttons, which fires the above
|
|
|
57 |
// command..
|
|
|
58 |
editor.ui.add( 'MyButton', CKEDITOR.UI_BUTTON, {
|
|
|
59 |
label: 'My Dialog',
|
|
|
60 |
command: 'myDialogCmd'
|
|
|
61 |
});
|
|
|
62 |
});
|
|
|
63 |
});
|
|
|
64 |
|
|
|
65 |
// When opening a dialog, its "definition" is created for it, for
|
|
|
66 |
// each editor instance. The "dialogDefinition" event is then
|
|
|
67 |
// fired. We should use this event to make customizations to the
|
|
|
68 |
// definition of existing dialogs.
|
|
|
69 |
CKEDITOR.on( 'dialogDefinition', function( ev ) {
|
|
|
70 |
// Take the dialog name and its definition from the event data.
|
|
|
71 |
var dialogName = ev.data.name;
|
|
|
72 |
var dialogDefinition = ev.data.definition;
|
|
|
73 |
|
|
|
74 |
// Check if the definition is from the dialog we're
|
|
|
75 |
// interested on (the "Link" dialog).
|
|
|
76 |
if ( dialogName == 'myDialog' && ev.editor.name == 'editor2' ) {
|
|
|
77 |
// Get a reference to the "Link Info" tab.
|
|
|
78 |
var infoTab = dialogDefinition.getContents( 'tab1' );
|
|
|
79 |
|
|
|
80 |
// Add a new text field to the "tab1" tab page.
|
|
|
81 |
infoTab.add( {
|
|
|
82 |
type: 'text',
|
|
|
83 |
label: 'My Custom Field',
|
|
|
84 |
id: 'customField',
|
|
|
85 |
'default': 'Sample!',
|
|
|
86 |
validate: function() {
|
|
|
87 |
if ( ( /\d/ ).test( this.getValue() ) )
|
|
|
88 |
return 'My Custom Field must not contain digits';
|
|
|
89 |
}
|
|
|
90 |
});
|
|
|
91 |
|
|
|
92 |
// Remove the "select1" field from the "tab1" tab.
|
|
|
93 |
infoTab.remove( 'select1' );
|
|
|
94 |
|
|
|
95 |
// Set the default value for "input1" field.
|
|
|
96 |
var input1 = infoTab.get( 'input1' );
|
|
|
97 |
input1[ 'default' ] = 'www.example.com';
|
|
|
98 |
|
|
|
99 |
// Remove the "tab2" tab page.
|
|
|
100 |
dialogDefinition.removeContents( 'tab2' );
|
|
|
101 |
|
|
|
102 |
// Add a new tab to the "Link" dialog.
|
|
|
103 |
dialogDefinition.addContents( {
|
|
|
104 |
id: 'customTab',
|
|
|
105 |
label: 'My Tab',
|
|
|
106 |
accessKey: 'M',
|
|
|
107 |
elements: [
|
|
|
108 |
{
|
|
|
109 |
id: 'myField1',
|
|
|
110 |
type: 'text',
|
|
|
111 |
label: 'My Text Field'
|
|
|
112 |
},
|
|
|
113 |
{
|
|
|
114 |
id: 'myField2',
|
|
|
115 |
type: 'text',
|
|
|
116 |
label: 'Another Text Field'
|
|
|
117 |
}
|
|
|
118 |
]
|
|
|
119 |
});
|
|
|
120 |
|
|
|
121 |
// Provide the focus handler to start initial focus in "customField" field.
|
|
|
122 |
dialogDefinition.onFocus = function() {
|
|
|
123 |
var urlField = this.getContentElement( 'tab1', 'customField' );
|
|
|
124 |
urlField.select();
|
|
|
125 |
};
|
|
|
126 |
}
|
|
|
127 |
});
|
|
|
128 |
|
|
|
129 |
var config = {
|
|
|
130 |
extraPlugins: 'dialog',
|
|
|
131 |
toolbar: [ [ 'MyButton' ] ]
|
|
|
132 |
};
|
|
|
133 |
|
|
|
134 |
</script>
|
|
|
135 |
</head>
|
|
|
136 |
<body>
|
|
|
137 |
<h1 class="samples">
|
|
|
138 |
<a href="../../../samples/old/index.html">CKEditor Samples</a> » Using CKEditor Dialog API
|
|
|
139 |
</h1>
|
|
|
140 |
<div class="warning deprecated">
|
|
|
141 |
This sample is not maintained anymore. Check out the <a href="https://ckeditor.com/docs/ckeditor4/latest/examples/index.html">brand new samples in CKEditor Examples</a>.
|
|
|
142 |
</div>
|
|
|
143 |
<div class="description">
|
|
|
144 |
<p>
|
|
|
145 |
This sample shows how to use the
|
|
|
146 |
<a class="samples" href="https://ckeditor.com/docs/ckeditor4/latest/api/CKEDITOR_dialog.html">CKEditor Dialog API</a>
|
|
|
147 |
to customize CKEditor dialog windows without changing the original editor code.
|
|
|
148 |
The following customizations are being done in the example below:
|
|
|
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 |
<p>A custom dialog is added to the editors using the <code>pluginsLoaded</code> event, from an external <a target="_blank" href="assets/my_dialog.js">dialog definition file</a>:</p>
|
|
|
155 |
<ol>
|
|
|
156 |
<li><strong>Creating a custom dialog window</strong> – "My Dialog" dialog window opened with the "My Dialog" toolbar button.</li>
|
|
|
157 |
<li><strong>Creating a custom button</strong> – Add button to open the dialog with "My Dialog" toolbar button.</li>
|
|
|
158 |
</ol>
|
|
|
159 |
<textarea cols="80" id="editor1" name="editor1" rows="10"><p>This is some <strong>sample text</strong>. You are using <a href="https://ckeditor.com/">CKEditor</a>.</p></textarea>
|
|
|
160 |
<script>
|
|
|
161 |
// Replace the <textarea id="editor1"> with an CKEditor instance.
|
|
|
162 |
CKEDITOR.replace( 'editor1', config );
|
|
|
163 |
</script>
|
|
|
164 |
<p>The below editor modify the dialog definition of the above added dialog using the <code>dialogDefinition</code> event:</p>
|
|
|
165 |
<ol>
|
|
|
166 |
<li><strong>Adding dialog tab</strong> – Add new tab "My Tab" to dialog window.</li>
|
|
|
167 |
<li><strong>Removing a dialog window tab</strong> – Remove "Second Tab" page from the dialog window.</li>
|
|
|
168 |
<li><strong>Adding dialog window fields</strong> – Add "My Custom Field" to the dialog window.</li>
|
|
|
169 |
<li><strong>Removing dialog window field</strong> – Remove "Select Field" selection field from the dialog window.</li>
|
|
|
170 |
<li><strong>Setting default values for dialog window fields</strong> – Set default value of "Text Field" text field. </li>
|
|
|
171 |
<li><strong>Setup initial focus for dialog window</strong> – Put initial focus on "My Custom Field" text field. </li>
|
|
|
172 |
</ol>
|
|
|
173 |
<textarea cols="80" id="editor2" name="editor2" rows="10"><p>This is some <strong>sample text</strong>. You are using <a href="https://ckeditor.com/">CKEditor</a>.</p></textarea>
|
|
|
174 |
<script>
|
|
|
175 |
|
|
|
176 |
// Replace the <textarea id="editor1"> with an CKEditor instance.
|
|
|
177 |
CKEDITOR.replace( 'editor2', config );
|
|
|
178 |
|
|
|
179 |
</script>
|
|
|
180 |
<div id="footer">
|
|
|
181 |
<hr>
|
|
|
182 |
<p>
|
|
|
183 |
CKEditor - The text editor for the Internet - <a class="samples" href="https://ckeditor.com/">https://ckeditor.com</a>
|
|
|
184 |
</p>
|
|
|
185 |
<p id="copy">
|
|
|
186 |
Copyright © 2003-2021, <a class="samples" href="https://cksource.com/">CKSource</a> - Frederico
|
|
|
187 |
Knabben. All rights reserved.
|
|
|
188 |
</p>
|
|
|
189 |
</div>
|
|
|
190 |
</body>
|
|
|
191 |
</html>
|