Browse Source

实现简单搜索高亮。

oldj 10 years ago
parent
commit
62e04ebbb9
5 changed files with 70 additions and 40 deletions
  1. 1 1
      app/SH3/MacGap/SwitchHosts!-Info.plist
  2. 0 0
      app/SH3/public/js/main.js
  3. 58 37
      app/src/cm_hl.js
  4. 7 0
      app/src/main.js
  5. 4 2
      app/src/ui.js

+ 1 - 1
app/SH3/MacGap/SwitchHosts!-Info.plist

@@ -21,7 +21,7 @@
 	<key>CFBundleSignature</key>
 	<string>????</string>
 	<key>CFBundleVersion</key>
-	<string>2968</string>
+	<string>3003</string>
 	<key>LSApplicationCategoryType</key>
 	<string>public.app-category.developer-tools</string>
 	<key>LSMinimumSystemVersion</key>

File diff suppressed because it is too large
+ 0 - 0
app/SH3/public/js/main.js


+ 58 - 37
app/src/cm_hl.js

@@ -1,49 +1,70 @@
 // CodeMirror, copyright (c) by Marijn Haverbeke and others
 // Distributed under an MIT license: http://codemirror.net/LICENSE
 
-(function (mod) {
-    mod(require("codemirror"));
-})(function (CodeMirror) {
-    "use strict";
+exports.init = function (app) {
 
-    CodeMirror.defineMode('host', function () {
-        function tokenBase(stream) {
-            if (stream.eatSpace()) return null;
+    (function (mod) {
+        mod(require('codemirror'));
+    })(function (CodeMirror) {
+        'use strict';
 
-            var sol = stream.sol();
-            var ch = stream.next();
+        CodeMirror.defineMode('host', function () {
+            function tokenBase(stream) {
+                if (stream.eatSpace()) return null;
 
-            if (ch === '#') {
-                stream.skipToEnd();
-                return 'comment';
-            }
-            if (!stream.string.match(/^\s*([\d\.]+|[\da-f:\.%lo]+)\s+\w/i)) {
-                return 'error';
+                var sol = stream.sol();
+                var ch = stream.next();
+
+                var s = stream.string;
+
+                if (ch === '#') {
+                    stream.skipToEnd();
+                    return 'comment';
+                }
+                if (!s.match(/^\s*([\d\.]+|[\da-f:\.%lo]+)\s+\w/i)) {
+                    return 'error';
+                }
+
+                if (sol && ch.match(/[\w\.:%]/)) {
+                    stream.eatWhile(/[\w\.:%]/);
+                    return 'ip';
+                }
+
+                return null;
             }
 
-            if (sol && ch.match(/[\w\.:%]/)) {
-                stream.eatWhile(/[\w\.:%]/);
-                return 'ip';
+            function tokenize(stream, state) {
+                return (state.tokens[0] || tokenBase)(stream, state);
             }
 
-            return null;
-        }
-
-        function tokenize(stream, state) {
-            return (state.tokens[0] || tokenBase)(stream, state);
-        }
-
-        return {
-            startState: function () {
-                return {tokens: []};
-            },
-            token: function (stream, state) {
-                return tokenize(stream, state);
-            },
-            lineComment: '#'
-        };
-    });
+            return {
+                startState: function () {
+                    return {tokens: []};
+                },
+                token: function (stream, state) {
+                    return tokenize(stream, state);
+                },
+                lineComment: '#'
+            };
+        });
+
+        CodeMirror.defineMode('hl', function (config, parserConfig) {
+            var searchOverlay = {
+                token: function (stream, state) {
+                    var kw = app.search_keyword;
+                    if (kw && stream.match(kw)) {
+                        return 'hl';
+                    }
 
-    //CodeMirror.defineMIME('text/x-host', 'host');
+                    while (stream.next() != null && !stream.match(kw, false)) {
+                    }
+                    return null;
+                }
+            };
+            return CodeMirror.overlayMode(CodeMirror.getMode(config, parserConfig.backdrop || 'host'), searchOverlay);
+        });
 
-});
+        //CodeMirror.defineMIME('text/x-host', 'host');
+
+    });
+};

+ 7 - 0
app/src/main.js

@@ -70,6 +70,11 @@ var app = new Vue({
             if (host.on) {
                 this.caculateHosts(host);
             }
+        },
+        'search_keyword': function () {
+            this.log('refresh2');
+            //this.codemirror.refresh();
+            this.onCurrentHostChange();
         }
     },
     methods: {
@@ -365,6 +370,7 @@ var app = new Vue({
         },
 
         mySearch: function (item) {
+            this.search_regexp = null;
             var kw = this.search_keyword;
             if (!kw) return true;
 
@@ -393,6 +399,7 @@ var app = new Vue({
                 } catch (e) {}
             }
             if (r && r.test(item.content)) {
+                this.search_regexp = r;
                 return true;
             }
 

+ 4 - 2
app/src/ui.js

@@ -7,7 +7,7 @@
 
 var CodeMirror = require('codemirror');
 require('codemirror/mode/shell/shell');
-require('./cm_hl');
+require('codemirror/addon/mode/overlay');
 
 var my_codemirror;
 
@@ -21,6 +21,8 @@ function resize() {
 
 function init(app) {
 
+    require('./cm_hl').init(app);
+
     $(document).ready(function () {
         var el_textarea = $('#host-code');
         //el_textarea.css('height', window.innerHeight - 8);
@@ -28,7 +30,7 @@ function init(app) {
         my_codemirror = CodeMirror.fromTextArea(el_textarea[0], {
             lineNumbers: true,
             readOnly: true,
-            mode: 'host'
+            mode: 'hl'
         });
         app.codemirror = my_codemirror;
 

Some files were not shown because too many files changed in this diff