Changeset 51704 in spip-zone


Ignore:
Timestamp:
Sep 22, 2011, 4:38:12 PM (8 years ago)
Author:
cedric@…
Message:

externaliser le js d'init dans un fichier

Location:
_plugins_/skeleditor/trunk
Files:
2 added
1 edited

Legend:

Unmodified
Added
Removed
  • _plugins_/skeleditor/trunk/inc/skeleditor_codemiror.php

    r51702 r51704  
    6060        $css .= "<link rel='stylesheet' href='".$dir."codemirror/lib/codemirror.css' type='text/css' />\n"
    6161          . "<link rel='stylesheet' href='".$dir."codemirror/theme/default.css' type='text/css' />\n"
    62           . "<link rel='stylesheet' href='".$dir."css/skeleditor.css' type='text/css' />";
     62          . "<link rel='stylesheet' href='".$dir."css/skeleditor.css' type='text/css' />\n";
    6363
    64         $js .= "<script src='".$dir."codemirror/lib/codemirror.js' type='text/javascript'></script>";
     64        $js .= "<script src='".$dir."codemirror/lib/codemirror.js' type='text/javascript'></script>\n";
    6565
    6666        foreach($mode as $m) {
    6767                $test = $dir."codemirror/mode/$m/$m";
    6868                if (file_exists($f=$test.".css"))
    69                         $css .= "<link rel='stylesheet' href='$f' type='text/css' />";
     69                        $css .= "<link rel='stylesheet' href='$f' type='text/css' />\n";
    7070                if (file_exists($f=$test.".js"))
    71                         $js .= "<script src='$f' type='text/javascript'></script>";
     71                        $js .= "<script src='$f' type='text/javascript'></script>\n";
    7272        }
    7373
     
    8181 * @return string
    8282 */
    83 function determine_mime_type($extension) {
     83function skeleditor_codemirror_determine_mode($extension) {
    8484        $mode = "";
    85   $mime_types = array(
     85  $modes = array(
    8686                'txt' => 'text/plain',
    8787                'htm' => 'text/html',
     
    8989                'php' => 'application/x-httpd-php',
    9090                'css' => 'text/css',
    91                 //'js' => 'application/javascript', codemirror2 ne doit pas avoir de mode définit pour les js
     91                'js' => 'javascript', //codemirror2 ne doit pas avoir de mode définit pour les js
    9292                'json' => 'application/json',
    9393                'xml' => 'application/xml',
    9494        );
    95         if (array_key_exists($extension, $mime_types)) {
    96                 $mode = 'mode:"'.$mime_types[$extension].'",';
     95        if (array_key_exists($extension, $modes)) {
     96                $mode = $modes[$extension];
    9797        }
    9898        return $mode;
     
    114114
    115115        // readOnly: jQuery("#code").attr("readonly"),
    116         $mode = determine_mime_type($infos['extension']);
     116        $mode = skeleditor_codemirror_determine_mode($infos['extension']);
     117        $script =
     118    '<script type="text/javascript">var cm_mode="'.$mode.'";</script>'
     119                . $css
     120                . $js
     121                . "<script src='".find_in_path("javascript/codemirror_init.js")."' type='text/javascript'></script>\n";
    117122
    118 $script =
    119         $css
    120         . $js
    121         . '<script type="text/javascript">
    122 var editor = CodeMirror.fromTextArea(document.getElementById(\'code\'), {
    123         lineNumbers: true,
    124         matchBrackets: true,
    125         indentUnit: 6,
    126         indentWithTabs: true,
    127         enterMode: "keep",
    128                                 '.$mode.'
    129         tabMode: "shift",
    130                 });
    131 var lastPos = null, lastQuery = null, marked = [];
    132 
    133 function unmark() {
    134   for (var i = 0; i < marked.length; ++i) marked[i]();
    135   marked.length = 0;
    136 }
    137 
    138 function search() {
    139   unmark();
    140   var text = document.getElementById("query").value;
    141   if (!text) return;
    142   for (var cursor = editor.getSearchCursor(text); cursor.findNext();)
    143     marked.push(editor.markText(cursor.from(), cursor.to(), "searched"));
    144 
    145   if (lastQuery != text) lastPos = null;
    146   var cursor = editor.getSearchCursor(text, lastPos || editor.getCursor());
    147   if (!cursor.findNext()) {
    148     cursor = editor.getSearchCursor(text);
    149     if (!cursor.findNext()) return;
    150   }
    151   editor.setSelection(cursor.from(), cursor.to());
    152   lastQuery = text; lastPos = cursor.to();
    153 }
    154 
    155 function replace() {
    156   unmark();
    157   var text = document.getElementById("query").value,
    158       replace = document.getElementById("replace").value;
    159   if (!text) return;
    160   for (var cursor = editor.getSearchCursor(text); cursor.findNext();)
    161     editor.replaceRange(replace, cursor.from(), cursor.to());
    162 }
    163 </script>';
    164123        return $script;
    165124}
Note: See TracChangeset for help on using the changeset viewer.