Annotation of loncom/html/adm/ckeditor/plugins/chem/plugin.js, revision 1.1

1.1     ! damieng     1: /**
        !             2:  * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
        !             3:  * For licensing, see LICENSE.md or http://ckeditor.com/license
        !             4:  */
        !             5: 
        !             6: /**
        !             7:  * @fileOverview [Chemistry] LON-CAPA ckeditor plugin for the <chem> element.
        !             8:  */
        !             9: 
        !            10: 'use strict';
        !            11: 
        !            12: CKEDITOR.plugins.add( 'chem', {
        !            13:     lang: 'de,en,es,fr,ja,pt,pt-br,ru,zh,zh-cn',
        !            14:     requires: 'widget',
        !            15:     icons: 'chem',
        !            16:     hidpi: true,
        !            17: 
        !            18:     init: function( editor ) {
        !            19:         editor.widgets.add( 'chem', {
        !            20: 
        !            21:             dialog: 'chem',
        !            22:             
        !            23:             button: editor.lang.chem.button,
        !            24: 
        !            25:             template:
        !            26:                 '<span class="chem"></span>',
        !            27: 
        !            28:             data: function() {
        !            29:                 var span = this.element;
        !            30:                 if (this.data.reaction)
        !            31:                     CKEDITOR.plugins.chem.add_html(this.data.reaction, span);
        !            32:                 else
        !            33:                     span.setHtml('?');
        !            34:             },
        !            35: 
        !            36:             upcast: function( el, data ) {
        !            37:                 if ( !( el.name == 'chem' ) )
        !            38:                     return;
        !            39: 
        !            40:                 if ( el.children.length > 1 || el.children[ 0 ].type != CKEDITOR.NODE_TEXT )
        !            41:                     return;
        !            42: 
        !            43:                 data.reaction = CKEDITOR.tools.htmlDecode( el.children[ 0 ].value );
        !            44:                 
        !            45:                 var span = new CKEDITOR.htmlParser.element('span');
        !            46:                 span.addClass('chem');
        !            47:                 
        !            48:                 CKEDITOR.plugins.chem.add_html(data.reaction, span);
        !            49:                 
        !            50:                 // Add attribute to prevent deleting empty span in data processing.
        !            51:                 var attrs = span.attributes;
        !            52:                 attrs[ 'data-cke-survive' ] = 1;
        !            53:                 if (el.attributes.eval)
        !            54:                     attrs['data-eval'] = el.attributes.eval;
        !            55:                 el.replaceWith(span);
        !            56:                 return span;
        !            57:             },
        !            58: 
        !            59:             downcast: function( el ) {
        !            60:                 var chem = new CKEDITOR.htmlParser.element('chem');
        !            61:                 if (el.attributes['data-eval'])
        !            62:                     chem.attributes.eval = el.attributes['data-eval'];
        !            63:                 chem.add(new CKEDITOR.htmlParser.text(CKEDITOR.tools.htmlEncode(this.data.reaction)));
        !            64:                 return chem;
        !            65:             }
        !            66:             
        !            67:         } );
        !            68:         // Add dialog.
        !            69:         CKEDITOR.dialog.add( 'chem', this.path + 'dialogs/chem.js' );
        !            70:     }
        !            71: } );
        !            72: 
        !            73: 
        !            74: /**
        !            75:  * @private
        !            76:  * @singleton
        !            77:  * @class CKEDITOR.plugins.chem
        !            78:  */
        !            79: CKEDITOR.plugins.chem = {};
        !            80: 
        !            81: /**
        !            82:  * Adds HTML to the span for the given reaction, using UNICODE characters for arrows.
        !            83:  *
        !            84:  * @private
        !            85:  * @param {string} reaction
        !            86:  * @param {CKEDITOR.dom.element} span - element the HTML will be constructed inside
        !            87:  */
        !            88: CKEDITOR.plugins.chem.add_html = function( reaction, span ) {
        !            89:     // this is doing the same thing as chemparse, except it uses UNICODE characters instead of LaTeX
        !            90:     var tokens = reaction.split(/(\s\+|\->|<=>|<\-|\.)/);
        !            91:     var formula = '';
        !            92:     for (var i=0; i<tokens.length; i++) {
        !            93:         var token = tokens[i];
        !            94:         if (token == '->' ) {
        !            95:             formula += '&#8594; ';
        !            96:             continue;
        !            97:         }
        !            98:         if (token == '<-' ) {
        !            99:             formula += '&#8592; ';
        !           100:             continue;
        !           101:         }  
        !           102:         if (token == '<=>') {
        !           103:             formula += '&#8652; ';
        !           104:             continue;
        !           105:         }
        !           106:         if (token == '.') {
        !           107:           formula = formula.replace(/&nbsp;| /, '');
        !           108:           formula += '&middot;';
        !           109:           continue;
        !           110:         }
        !           111:         var found = token.match(/^\s*([\d|\/]*(?:&frac\d\d)?)(.*)/);
        !           112:         if (found != null && found.length > 1 && found[1] != '1')
        !           113:             formula += found[1]; // stoichiometric coefficient
        !           114:         
        !           115:         var molecule;
        !           116:         if (found != null && found.length > 2)
        !           117:             molecule = found[2];
        !           118:         else
        !           119:             molecule = '';
        !           120:         // subscripts
        !           121:         // $molecule =~ s|(?<=[a-zA-Z\)\]\s])(\d+)|<sub>$1</sub>|g;
        !           122:         // Javascript does not support look-behind like Perl
        !           123:         molecule = molecule.replace(/([a-zA-Z\)\]\s])(\d+)/g, '$1<sub>$2</sub>');
        !           124:         // superscripts
        !           125:         molecule = molecule.replace(/\^(\d*[+\-]*)/g, '<sup>$1</sup>');
        !           126:         // strip whitespace
        !           127:         molecule = molecule.replace(/\s*/g, '');
        !           128:         // forced space
        !           129:         molecule = molecule.replace(/_/g, ' ');
        !           130:         molecule = molecule.replace(/-/g, '&minus;');
        !           131:         formula += molecule + '&nbsp;';
        !           132:     }
        !           133:     // get rid of trailing space
        !           134:     formula = formula.replace(/(&nbsp;| )$/, '');
        !           135:     // we need to add CSS here, because ckeditor changes CSS for sup and sub, and ckeditor css for plugins is not used inside dialogs
        !           136:     // (whether we define it with CKEDITOR.editor.addContentsCss or CKEDITOR.addCss)
        !           137:     formula = formula.replace(/<sub>/g,'<sub style="vertical-align:sub; font-size:smaller">');
        !           138:     formula = formula.replace(/<sup>/g,'<sup style="vertical-align:super; font-size:smaller">');
        !           139:     span.setHtml(formula);
        !           140: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>