]> git.immae.eu Git - perso/Immae/Projets/packagist/ludivine-ckeditor-component.git/blame - sources/plugins/indentlist/plugin.js
Update to 4.7.3
[perso/Immae/Projets/packagist/ludivine-ckeditor-component.git] / sources / plugins / indentlist / plugin.js
CommitLineData
c63493c8
IB
1/**\r
2 * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.\r
3 * For licensing, see LICENSE.md or http://ckeditor.com/license\r
4 */\r
5\r
6/**\r
7 * @fileOverview Handles the indentation of lists.\r
8 */\r
9\r
10( function() {\r
11 'use strict';\r
12\r
13 var isNotWhitespaces = CKEDITOR.dom.walker.whitespaces( true ),\r
14 isNotBookmark = CKEDITOR.dom.walker.bookmark( false, true ),\r
15 TRISTATE_DISABLED = CKEDITOR.TRISTATE_DISABLED,\r
16 TRISTATE_OFF = CKEDITOR.TRISTATE_OFF;\r
17\r
18 CKEDITOR.plugins.add( 'indentlist', {\r
19 requires: 'indent',\r
20 init: function( editor ) {\r
21 var globalHelpers = CKEDITOR.plugins.indent;\r
22\r
23 // Register commands.\r
24 globalHelpers.registerCommands( editor, {\r
25 indentlist: new commandDefinition( editor, 'indentlist', true ),\r
26 outdentlist: new commandDefinition( editor, 'outdentlist' )\r
27 } );\r
28\r
29 function commandDefinition( editor ) {\r
30 globalHelpers.specificDefinition.apply( this, arguments );\r
31\r
32 // Require ul OR ol list.\r
33 this.requiredContent = [ 'ul', 'ol' ];\r
34\r
35 // Indent and outdent lists with TAB/SHIFT+TAB key. Indenting can\r
36 // be done for any list item that isn't the first child of the parent.\r
37 editor.on( 'key', function( evt ) {\r
1794320d
IB
38 var path = editor.elementPath();\r
39\r
c63493c8
IB
40 if ( editor.mode != 'wysiwyg' )\r
41 return;\r
42\r
43 if ( evt.data.keyCode == this.indentKey ) {\r
1794320d
IB
44 // Prevent of getting context of empty path (#424)(https://dev.ckeditor.com/ticket/17028).\r
45 if ( !path ) {\r
46 return;\r
47 }\r
48\r
49 var list = this.getContext( path );\r
c63493c8
IB
50\r
51 if ( list ) {\r
52 // Don't indent if in first list item of the parent.\r
53 // Outdent, however, can always be done to collapse\r
54 // the list into a paragraph (div).\r
1794320d 55 if ( this.isIndent && CKEDITOR.plugins.indentList.firstItemInPath( this.context, path, list ) )\r
c63493c8
IB
56 return;\r
57\r
58 // Exec related global indentation command. Global\r
59 // commands take care of bookmarks and selection,\r
60 // so it's much easier to use them instead of\r
61 // content-specific commands.\r
62 editor.execCommand( this.relatedGlobal );\r
63\r
64 // Cancel the key event so editor doesn't lose focus.\r
65 evt.cancel();\r
66 }\r
67 }\r
68 }, this );\r
69\r
70 // There are two different jobs for this plugin:\r
71 //\r
72 // * Indent job (priority=10), before indentblock.\r
73 //\r
74 // This job is before indentblock because, if this plugin is\r
75 // loaded it has higher priority over indentblock. It means that,\r
76 // if possible, nesting is performed, and then block manipulation,\r
77 // if necessary.\r
78 //\r
79 // * Outdent job (priority=30), after outdentblock.\r
80 //\r
81 // This job got to be after outdentblock because in some cases\r
82 // (margin, config#indentClass on list) outdent must be done on\r
83 // block-level.\r
84\r
85 this.jobs[ this.isIndent ? 10 : 30 ] = {\r
86 refresh: this.isIndent ?\r
87 function( editor, path ) {\r
88 var list = this.getContext( path ),\r
89 inFirstListItem = CKEDITOR.plugins.indentList.firstItemInPath( this.context, path, list );\r
90\r
91 if ( !list || !this.isIndent || inFirstListItem )\r
92 return TRISTATE_DISABLED;\r
93\r
94 return TRISTATE_OFF;\r
95 } : function( editor, path ) {\r
96 var list = this.getContext( path );\r
97\r
98 if ( !list || this.isIndent )\r
99 return TRISTATE_DISABLED;\r
100\r
101 return TRISTATE_OFF;\r
102 },\r
103\r
104 exec: CKEDITOR.tools.bind( indentList, this )\r
105 };\r
106 }\r
107\r
108 CKEDITOR.tools.extend( commandDefinition.prototype, globalHelpers.specificDefinition.prototype, {\r
109 // Elements that, if in an elementpath, will be handled by this\r
110 // command. They restrict the scope of the plugin.\r
111 context: { ol: 1, ul: 1 }\r
112 } );\r
113 }\r
114 } );\r
115\r
116 function indentList( editor ) {\r
117 var that = this,\r
118 database = this.database,\r
1794320d
IB
119 context = this.context,\r
120 range;\r
c63493c8
IB
121\r
122 function indent( listNode ) {\r
123 // Our starting and ending points of the range might be inside some blocks under a list item...\r
124 // So before playing with the iterator, we need to expand the block to include the list items.\r
125 var startContainer = range.startContainer,\r
126 endContainer = range.endContainer;\r
127 while ( startContainer && !startContainer.getParent().equals( listNode ) )\r
128 startContainer = startContainer.getParent();\r
129 while ( endContainer && !endContainer.getParent().equals( listNode ) )\r
130 endContainer = endContainer.getParent();\r
131\r
132 if ( !startContainer || !endContainer )\r
133 return false;\r
134\r
135 // Now we can iterate over the individual items on the same tree depth.\r
136 var block = startContainer,\r
137 itemsToMove = [],\r
138 stopFlag = false;\r
139\r
140 while ( !stopFlag ) {\r
141 if ( block.equals( endContainer ) )\r
142 stopFlag = true;\r
143\r
144 itemsToMove.push( block );\r
145 block = block.getNext();\r
146 }\r
147\r
148 if ( itemsToMove.length < 1 )\r
149 return false;\r
150\r
151 // Do indent or outdent operations on the array model of the list, not the\r
152 // list's DOM tree itself. The array model demands that it knows as much as\r
153 // possible about the surrounding lists, we need to feed it the further\r
154 // ancestor node that is still a list.\r
155 var listParents = listNode.getParents( true );\r
156 for ( var i = 0; i < listParents.length; i++ ) {\r
157 if ( listParents[ i ].getName && context[ listParents[ i ].getName() ] ) {\r
158 listNode = listParents[ i ];\r
159 break;\r
160 }\r
161 }\r
162\r
163 var indentOffset = that.isIndent ? 1 : -1,\r
164 startItem = itemsToMove[ 0 ],\r
165 lastItem = itemsToMove[ itemsToMove.length - 1 ],\r
166\r
167 // Convert the list DOM tree into a one dimensional array.\r
168 listArray = CKEDITOR.plugins.list.listToArray( listNode, database ),\r
169\r
170 // Apply indenting or outdenting on the array.\r
171 baseIndent = listArray[ lastItem.getCustomData( 'listarray_index' ) ].indent;\r
172\r
173 for ( i = startItem.getCustomData( 'listarray_index' ); i <= lastItem.getCustomData( 'listarray_index' ); i++ ) {\r
174 listArray[ i ].indent += indentOffset;\r
1794320d 175 // Make sure the newly created sublist get a brand-new element of the same type. (http://dev.ckeditor.com/ticket/5372)\r
c63493c8
IB
176 if ( indentOffset > 0 ) {\r
177 var listRoot = listArray[ i ].parent;\r
178 listArray[ i ].parent = new CKEDITOR.dom.element( listRoot.getName(), listRoot.getDocument() );\r
179 }\r
180 }\r
181\r
182 for ( i = lastItem.getCustomData( 'listarray_index' ) + 1; i < listArray.length && listArray[ i ].indent > baseIndent; i++ )\r
183 listArray[ i ].indent += indentOffset;\r
184\r
185 // Convert the array back to a DOM forest (yes we might have a few subtrees now).\r
186 // And replace the old list with the new forest.\r
187 var newList = CKEDITOR.plugins.list.arrayToList( listArray, database, null, editor.config.enterMode, listNode.getDirection() );\r
188\r
189 // Avoid nested <li> after outdent even they're visually same,\r
1794320d 190 // recording them for later refactoring.(http://dev.ckeditor.com/ticket/3982)\r
c63493c8
IB
191 if ( !that.isIndent ) {\r
192 var parentLiElement;\r
193 if ( ( parentLiElement = listNode.getParent() ) && parentLiElement.is( 'li' ) ) {\r
194 var children = newList.listNode.getChildren(),\r
195 pendingLis = [],\r
196 count = children.count(),\r
197 child;\r
198\r
199 for ( i = count - 1; i >= 0; i-- ) {\r
200 if ( ( child = children.getItem( i ) ) && child.is && child.is( 'li' ) )\r
201 pendingLis.push( child );\r
202 }\r
203 }\r
204 }\r
205\r
206 if ( newList )\r
207 newList.listNode.replace( listNode );\r
208\r
209 // Move the nested <li> to be appeared after the parent.\r
210 if ( pendingLis && pendingLis.length ) {\r
211 for ( i = 0; i < pendingLis.length; i++ ) {\r
212 var li = pendingLis[ i ],\r
213 followingList = li;\r
214\r
215 // Nest preceding <ul>/<ol> inside current <li> if any.\r
216 while ( ( followingList = followingList.getNext() ) && followingList.is && followingList.getName() in context ) {\r
217 // IE requires a filler NBSP for nested list inside empty list item,\r
1794320d 218 // otherwise the list item will be inaccessiable. (http://dev.ckeditor.com/ticket/4476)\r
c63493c8
IB
219 if ( CKEDITOR.env.needsNbspFiller && !li.getFirst( neitherWhitespacesNorBookmark ) )\r
220 li.append( range.document.createText( '\u00a0' ) );\r
221\r
222 li.append( followingList );\r
223 }\r
224\r
225 li.insertAfter( parentLiElement );\r
226 }\r
227 }\r
228\r
229 if ( newList )\r
230 editor.fire( 'contentDomInvalidated' );\r
231\r
232 return true;\r
233 }\r
234\r
235 var selection = editor.getSelection(),\r
236 ranges = selection && selection.getRanges(),\r
1794320d 237 iterator = ranges.createIterator();\r
c63493c8
IB
238\r
239 while ( ( range = iterator.getNextRange() ) ) {\r
240 var nearestListBlock = range.getCommonAncestor();\r
241\r
242 while ( nearestListBlock && !( nearestListBlock.type == CKEDITOR.NODE_ELEMENT && context[ nearestListBlock.getName() ] ) ) {\r
1794320d 243 // Avoid having plugin propagate to parent of editor in inline mode by canceling the indentation. (http://dev.ckeditor.com/ticket/12796)\r
c63493c8
IB
244 if ( editor.editable().equals( nearestListBlock ) ) {\r
245 nearestListBlock = false;\r
246 break;\r
247 }\r
248 nearestListBlock = nearestListBlock.getParent();\r
249 }\r
250\r
251 // Avoid having selection boundaries out of the list.\r
252 // <ul><li>[...</li></ul><p>...]</p> => <ul><li>[...]</li></ul><p>...</p>\r
253 if ( !nearestListBlock ) {\r
254 if ( ( nearestListBlock = range.startPath().contains( context ) ) )\r
255 range.setEndAt( nearestListBlock, CKEDITOR.POSITION_BEFORE_END );\r
256 }\r
257\r
1794320d 258 // Avoid having selection enclose the entire list. (http://dev.ckeditor.com/ticket/6138)\r
c63493c8
IB
259 // [<ul><li>...</li></ul>] =><ul><li>[...]</li></ul>\r
260 if ( !nearestListBlock ) {\r
261 var selectedNode = range.getEnclosedNode();\r
262 if ( selectedNode && selectedNode.type == CKEDITOR.NODE_ELEMENT && selectedNode.getName() in context ) {\r
263 range.setStartAt( selectedNode, CKEDITOR.POSITION_AFTER_START );\r
264 range.setEndAt( selectedNode, CKEDITOR.POSITION_BEFORE_END );\r
265 nearestListBlock = selectedNode;\r
266 }\r
267 }\r
268\r
269 // Avoid selection anchors under list root.\r
270 // <ul>[<li>...</li>]</ul> => <ul><li>[...]</li></ul>\r
271 if ( nearestListBlock && range.startContainer.type == CKEDITOR.NODE_ELEMENT && range.startContainer.getName() in context ) {\r
272 var walker = new CKEDITOR.dom.walker( range );\r
273 walker.evaluator = listItem;\r
274 range.startContainer = walker.next();\r
275 }\r
276\r
277 if ( nearestListBlock && range.endContainer.type == CKEDITOR.NODE_ELEMENT && range.endContainer.getName() in context ) {\r
278 walker = new CKEDITOR.dom.walker( range );\r
279 walker.evaluator = listItem;\r
280 range.endContainer = walker.previous();\r
281 }\r
282\r
283 if ( nearestListBlock )\r
284 return indent( nearestListBlock );\r
285 }\r
286 return 0;\r
287 }\r
288\r
289 // Determines whether a node is a list <li> element.\r
290 function listItem( node ) {\r
291 return node.type == CKEDITOR.NODE_ELEMENT && node.is( 'li' );\r
292 }\r
293\r
294 function neitherWhitespacesNorBookmark( node ) {\r
295 return isNotWhitespaces( node ) && isNotBookmark( node );\r
296 }\r
297\r
298 /**\r
299 * Global namespace for methods exposed by the Indent List plugin.\r
300 *\r
301 * @singleton\r
302 * @class\r
303 */\r
304 CKEDITOR.plugins.indentList = {};\r
305\r
306 /**\r
307 * Checks whether the first child of the list is in the path.\r
308 * The list can be extracted from the path or given explicitly\r
309 * e.g. for better performance if cached.\r
310 *\r
311 * @since 4.4.6\r
312 * @param {Object} query See the {@link CKEDITOR.dom.elementPath#contains} method arguments.\r
313 * @param {CKEDITOR.dom.elementPath} path\r
314 * @param {CKEDITOR.dom.element} [list]\r
315 * @returns {Boolean}\r
316 * @member CKEDITOR.plugins.indentList\r
317 */\r
318 CKEDITOR.plugins.indentList.firstItemInPath = function( query, path, list ) {\r
319 var firstListItemInPath = path.contains( listItem );\r
320 if ( !list )\r
321 list = path.contains( query );\r
322\r
323 return list && firstListItemInPath && firstListItemInPath.equals( list.getFirst( listItem ) );\r
324 };\r
325} )();\r