]> git.immae.eu Git - perso/Immae/Projets/packagist/ludivine-ckeditor-component.git/blobdiff - sources/plugins/font/plugin.js
Add audio, color and fonts
[perso/Immae/Projets/packagist/ludivine-ckeditor-component.git] / sources / plugins / font / plugin.js
diff --git a/sources/plugins/font/plugin.js b/sources/plugins/font/plugin.js
new file mode 100644 (file)
index 0000000..8794d89
--- /dev/null
@@ -0,0 +1,353 @@
+/**\r
+ * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.\r
+ * For licensing, see LICENSE.md or http://ckeditor.com/license\r
+ */\r
+\r
+( function() {\r
+       function addCombo( editor, comboName, styleType, lang, entries, defaultLabel, styleDefinition, order ) {\r
+               var config = editor.config,\r
+                       style = new CKEDITOR.style( styleDefinition );\r
+\r
+               // Gets the list of fonts from the settings.\r
+               var names = entries.split( ';' ),\r
+                       values = [];\r
+\r
+               // Create style objects for all fonts.\r
+               var styles = {};\r
+               for ( var i = 0; i < names.length; i++ ) {\r
+                       var parts = names[ i ];\r
+\r
+                       if ( parts ) {\r
+                               parts = parts.split( '/' );\r
+\r
+                               var vars = {},\r
+                                       name = names[ i ] = parts[ 0 ];\r
+\r
+                               vars[ styleType ] = values[ i ] = parts[ 1 ] || name;\r
+\r
+                               styles[ name ] = new CKEDITOR.style( styleDefinition, vars );\r
+                               styles[ name ]._.definition.name = name;\r
+                       } else {\r
+                               names.splice( i--, 1 );\r
+                       }\r
+               }\r
+\r
+               editor.ui.addRichCombo( comboName, {\r
+                       label: lang.label,\r
+                       title: lang.panelTitle,\r
+                       toolbar: 'styles,' + order,\r
+                       allowedContent: style,\r
+                       requiredContent: style,\r
+                       contentTransformations: [\r
+                               [\r
+                                       {\r
+                                               element: 'font',\r
+                                               check: 'span',\r
+                                               left: function( element ) {\r
+                                                       return !!element.attributes.size ||\r
+                                                               !!element.attributes.align ||\r
+                                                               !!element.attributes.face;\r
+                                               },\r
+                                               right: function( element ) {\r
+                                                       var sizes = [\r
+                                                               '', // Non-existent size "0"\r
+                                                               'x-small',\r
+                                                               'small',\r
+                                                               'medium',\r
+                                                               'large',\r
+                                                               'x-large',\r
+                                                               'xx-large',\r
+                                                               '48px' // Closest value to what size="7" might mean.\r
+                                                       ];\r
+\r
+                                                       element.name = 'span';\r
+\r
+                                                       if ( element.attributes.size ) {\r
+                                                               element.styles[ 'font-size' ] = sizes[ element.attributes.size ];\r
+                                                               delete element.attributes.size;\r
+                                                       }\r
+\r
+                                                       if ( element.attributes.align ) {\r
+                                                               element.styles[ 'text-align' ] = element.attributes.align;\r
+                                                               delete element.attributes.align;\r
+                                                       }\r
+\r
+                                                       if ( element.attributes.face ) {\r
+                                                               element.styles[ 'font-family' ] = element.attributes.face;\r
+                                                               delete element.attributes.face;\r
+                                                       }\r
+                                               }\r
+                                       }\r
+                               ]\r
+                       ],\r
+                       panel: {\r
+                               css: [ CKEDITOR.skin.getPath( 'editor' ) ].concat( config.contentsCss ),\r
+                               multiSelect: false,\r
+                               attributes: { 'aria-label': lang.panelTitle }\r
+                       },\r
+\r
+                       init: function() {\r
+                               this.startGroup( lang.panelTitle );\r
+\r
+                               for ( var i = 0; i < names.length; i++ ) {\r
+                                       var name = names[ i ];\r
+\r
+                                       // Add the tag entry to the panel list.\r
+                                       this.add( name, styles[ name ].buildPreview(), name );\r
+                               }\r
+                       },\r
+\r
+                       onClick: function( value ) {\r
+                               editor.focus();\r
+                               editor.fire( 'saveSnapshot' );\r
+\r
+                               var previousValue = this.getValue(),\r
+                                       style = styles[ value ];\r
+\r
+                               // When applying one style over another, first remove the previous one (#12403).\r
+                               // NOTE: This is only a temporary fix. It will be moved to the styles system (#12687).\r
+                               if ( previousValue && value != previousValue ) {\r
+                                       var previousStyle = styles[ previousValue ],\r
+                                               range = editor.getSelection().getRanges()[ 0 ];\r
+\r
+                                       // If the range is collapsed we can't simply use the editor.removeStyle method\r
+                                       // because it will remove the entire element and we want to split it instead.\r
+                                       if ( range.collapsed ) {\r
+                                               var path = editor.elementPath(),\r
+                                                       // Find the style element.\r
+                                                       matching = path.contains( function( el ) {\r
+                                                               return previousStyle.checkElementRemovable( el );\r
+                                                       } );\r
+\r
+                                               if ( matching ) {\r
+                                                       var startBoundary = range.checkBoundaryOfElement( matching, CKEDITOR.START ),\r
+                                                               endBoundary = range.checkBoundaryOfElement( matching, CKEDITOR.END ),\r
+                                                               node, bm;\r
+\r
+                                                       // If we are at both boundaries it means that the element is empty.\r
+                                                       // Remove it but in a way that we won't lose other empty inline elements inside it.\r
+                                                       // Example: <p>x<span style="font-size:48px"><em>[]</em></span>x</p>\r
+                                                       // Result: <p>x<em>[]</em>x</p>\r
+                                                       if ( startBoundary && endBoundary ) {\r
+                                                               bm = range.createBookmark();\r
+                                                               // Replace the element with its children (TODO element.replaceWithChildren).\r
+                                                               while ( ( node = matching.getFirst() ) ) {\r
+                                                                       node.insertBefore( matching );\r
+                                                               }\r
+                                                               matching.remove();\r
+                                                               range.moveToBookmark( bm );\r
+\r
+                                                       // If we are at the boundary of the style element, move out and copy nested styles/elements.\r
+                                                       } else if ( startBoundary || endBoundary ) {\r
+                                                               range.moveToPosition( matching, startBoundary ? CKEDITOR.POSITION_BEFORE_START : CKEDITOR.POSITION_AFTER_END );\r
+                                                               cloneSubtreeIntoRange( range, path.elements.slice(), matching );\r
+                                                       } else {\r
+                                                               // Split the element and clone the elements that were in the path\r
+                                                               // (between the startContainer and the matching element)\r
+                                                               // into the new place.\r
+                                                               range.splitElement( matching );\r
+                                                               range.moveToPosition( matching, CKEDITOR.POSITION_AFTER_END );\r
+                                                               cloneSubtreeIntoRange( range, path.elements.slice(), matching );\r
+                                                       }\r
+\r
+                                                       editor.getSelection().selectRanges( [ range ] );\r
+                                               }\r
+                                       } else {\r
+                                               editor.removeStyle( previousStyle );\r
+                                       }\r
+                               }\r
+\r
+                               editor[ previousValue == value ? 'removeStyle' : 'applyStyle' ]( style );\r
+\r
+                               editor.fire( 'saveSnapshot' );\r
+                       },\r
+\r
+                       onRender: function() {\r
+                               editor.on( 'selectionChange', function( ev ) {\r
+                                       var currentValue = this.getValue();\r
+\r
+                                       var elementPath = ev.data.path,\r
+                                               elements = elementPath.elements;\r
+\r
+                                       // For each element into the elements path.\r
+                                       for ( var i = 0, element; i < elements.length; i++ ) {\r
+                                               element = elements[ i ];\r
+\r
+                                               // Check if the element is removable by any of\r
+                                               // the styles.\r
+                                               for ( var value in styles ) {\r
+                                                       if ( styles[ value ].checkElementMatch( element, true, editor ) ) {\r
+                                                               if ( value != currentValue )\r
+                                                                       this.setValue( value );\r
+                                                               return;\r
+                                                       }\r
+                                               }\r
+                                       }\r
+\r
+                                       // If no styles match, just empty it.\r
+                                       this.setValue( '', defaultLabel );\r
+                               }, this );\r
+                       },\r
+\r
+                       refresh: function() {\r
+                               if ( !editor.activeFilter.check( style ) )\r
+                                       this.setState( CKEDITOR.TRISTATE_DISABLED );\r
+                       }\r
+               } );\r
+       }\r
+\r
+       // Clones the subtree between subtreeStart (exclusive) and the\r
+       // leaf (inclusive) and inserts it into the range.\r
+       //\r
+       // @param range\r
+       // @param {CKEDITOR.dom.element[]} elements Elements path in the standard order: leaf -> root.\r
+       // @param {CKEDITOR.dom.element/null} substreeStart The start of the subtree.\r
+       // If null, then the leaf belongs to the subtree.\r
+       function cloneSubtreeIntoRange( range, elements, subtreeStart ) {\r
+               var current = elements.pop();\r
+               if ( !current ) {\r
+                       return;\r
+               }\r
+               // Rewind the elements array up to the subtreeStart and then start the real cloning.\r
+               if ( subtreeStart ) {\r
+                       return cloneSubtreeIntoRange( range, elements, current.equals( subtreeStart ) ? null : subtreeStart );\r
+               }\r
+\r
+               var clone = current.clone();\r
+               range.insertNode( clone );\r
+               range.moveToPosition( clone, CKEDITOR.POSITION_AFTER_START );\r
+\r
+               cloneSubtreeIntoRange( range, elements );\r
+       }\r
+\r
+       CKEDITOR.plugins.add( 'font', {\r
+               requires: 'richcombo',\r
+               // jscs:disable maximumLineLength\r
+               lang: 'af,ar,az,bg,bn,bs,ca,cs,cy,da,de,de-ch,el,en,en-au,en-ca,en-gb,eo,es,et,eu,fa,fi,fo,fr,fr-ca,gl,gu,he,hi,hr,hu,id,is,it,ja,ka,km,ko,ku,lt,lv,mk,mn,ms,nb,nl,no,oc,pl,pt,pt-br,ro,ru,si,sk,sl,sq,sr,sr-latn,sv,th,tr,tt,ug,uk,vi,zh,zh-cn', // %REMOVE_LINE_CORE%\r
+               // jscs:enable maximumLineLength\r
+               init: function( editor ) {\r
+                       var config = editor.config;\r
+\r
+                       addCombo( editor, 'Font', 'family', editor.lang.font, config.font_names, config.font_defaultLabel, config.font_style, 30 );\r
+                       addCombo( editor, 'FontSize', 'size', editor.lang.font.fontSize, config.fontSize_sizes, config.fontSize_defaultLabel, config.fontSize_style, 40 );\r
+               }\r
+       } );\r
+} )();\r
+\r
+/**\r
+ * The list of fonts names to be displayed in the Font combo in the toolbar.\r
+ * Entries are separated by semi-colons (`';'`), while it's possible to have more\r
+ * than one font for each entry, in the HTML way (separated by comma).\r
+ *\r
+ * A display name may be optionally defined by prefixing the entries with the\r
+ * name and the slash character. For example, `'Arial/Arial, Helvetica, sans-serif'`\r
+ * will be displayed as `'Arial'` in the list, but will be outputted as\r
+ * `'Arial, Helvetica, sans-serif'`.\r
+ *\r
+ *             config.font_names =\r
+ *                     'Arial/Arial, Helvetica, sans-serif;' +\r
+ *                     'Times New Roman/Times New Roman, Times, serif;' +\r
+ *                     'Verdana';\r
+ *\r
+ *             config.font_names = 'Arial;Times New Roman;Verdana';\r
+ *\r
+ * @cfg {String} [font_names=see source]\r
+ * @member CKEDITOR.config\r
+ */\r
+CKEDITOR.config.font_names = 'Arial/Arial, Helvetica, sans-serif;' +\r
+       'Comic Sans MS/Comic Sans MS, cursive;' +\r
+       'Courier New/Courier New, Courier, monospace;' +\r
+       'Georgia/Georgia, serif;' +\r
+       'Lucida Sans Unicode/Lucida Sans Unicode, Lucida Grande, sans-serif;' +\r
+       'Tahoma/Tahoma, Geneva, sans-serif;' +\r
+       'Times New Roman/Times New Roman, Times, serif;' +\r
+       'Trebuchet MS/Trebuchet MS, Helvetica, sans-serif;' +\r
+       'Verdana/Verdana, Geneva, sans-serif';\r
+\r
+/**\r
+ * The text to be displayed in the Font combo is none of the available values\r
+ * matches the current cursor position or text selection.\r
+ *\r
+ *             // If the default site font is Arial, we may making it more explicit to the end user.\r
+ *             config.font_defaultLabel = 'Arial';\r
+ *\r
+ * @cfg {String} [font_defaultLabel='']\r
+ * @member CKEDITOR.config\r
+ */\r
+CKEDITOR.config.font_defaultLabel = '';\r
+\r
+/**\r
+ * The style definition to be used to apply the font in the text.\r
+ *\r
+ *             // This is actually the default value for it.\r
+ *             config.font_style = {\r
+ *                     element:                'span',\r
+ *                     styles:                 { 'font-family': '#(family)' },\r
+ *                     overrides:              [ { element: 'font', attributes: { 'face': null } } ]\r
+ *     };\r
+ *\r
+ * @cfg {Object} [font_style=see example]\r
+ * @member CKEDITOR.config\r
+ */\r
+CKEDITOR.config.font_style = {\r
+       element: 'span',\r
+       styles: { 'font-family': '#(family)' },\r
+       overrides: [ {\r
+               element: 'font', attributes: { 'face': null }\r
+       } ]\r
+};\r
+\r
+/**\r
+ * The list of fonts size to be displayed in the Font Size combo in the\r
+ * toolbar. Entries are separated by semi-colons (`';'`).\r
+ *\r
+ * Any kind of "CSS like" size can be used, like `'12px'`, `'2.3em'`, `'130%'`,\r
+ * `'larger'` or `'x-small'`.\r
+ *\r
+ * A display name may be optionally defined by prefixing the entries with the\r
+ * name and the slash character. For example, `'Bigger Font/14px'` will be\r
+ * displayed as `'Bigger Font'` in the list, but will be outputted as `'14px'`.\r
+ *\r
+ *             config.fontSize_sizes = '16/16px;24/24px;48/48px;';\r
+ *\r
+ *             config.fontSize_sizes = '12px;2.3em;130%;larger;x-small';\r
+ *\r
+ *             config.fontSize_sizes = '12 Pixels/12px;Big/2.3em;30 Percent More/130%;Bigger/larger;Very Small/x-small';\r
+ *\r
+ * @cfg {String} [fontSize_sizes=see source]\r
+ * @member CKEDITOR.config\r
+ */\r
+CKEDITOR.config.fontSize_sizes = '8/8px;9/9px;10/10px;11/11px;12/12px;14/14px;16/16px;18/18px;20/20px;22/22px;24/24px;26/26px;28/28px;36/36px;48/48px;72/72px';\r
+\r
+/**\r
+ * The text to be displayed in the Font Size combo is none of the available\r
+ * values matches the current cursor position or text selection.\r
+ *\r
+ *             // If the default site font size is 12px, we may making it more explicit to the end user.\r
+ *             config.fontSize_defaultLabel = '12px';\r
+ *\r
+ * @cfg {String} [fontSize_defaultLabel='']\r
+ * @member CKEDITOR.config\r
+ */\r
+CKEDITOR.config.fontSize_defaultLabel = '';\r
+\r
+/**\r
+ * The style definition to be used to apply the font size in the text.\r
+ *\r
+ *             // This is actually the default value for it.\r
+ *             config.fontSize_style = {\r
+ *                     element:                'span',\r
+ *                     styles:                 { 'font-size': '#(size)' },\r
+ *                     overrides:              [ { element: 'font', attributes: { 'size': null } } ]\r
+ *             };\r
+ *\r
+ * @cfg {Object} [fontSize_style=see example]\r
+ * @member CKEDITOR.config\r
+ */\r
+CKEDITOR.config.fontSize_style = {\r
+       element: 'span',\r
+       styles: { 'font-size': '#(size)' },\r
+       overrides: [ {\r
+               element: 'font', attributes: { 'size': null }\r
+       } ]\r
+};\r