浏览代码

[notebook] Support for switching snippet type with %type on the first line

Enrico Berti 10 年之前
父节点
当前提交
6ea0094df5

+ 16 - 0
desktop/core/src/desktop/static/desktop/js/ko.hue-bindings.js

@@ -1985,6 +1985,22 @@
           editor.previousSize = editor.session.getLength();
           $(document).trigger("editorSizeChanged");
         }
+        // automagically change snippet type
+        var firstLine = editor.session.getLine(0);
+        if (firstLine.indexOf("%") == 0 && firstLine.charAt(firstLine.length - 1) == " ") {
+          var availableSnippets = snippet.availableSnippets;
+          var removeFirstLine = false;
+          for (var i = 0; i < availableSnippets.length; i++) {
+            if ($.trim(firstLine.substr(1)) == availableSnippets[i].type()) {
+              snippet.type(availableSnippets[i].type());
+              removeFirstLine = true;
+              break;
+            }
+          }
+          if (removeFirstLine) {
+            editor.session.remove(new AceRange(0, 0, 0, 200));
+          }
+        }
       });
 
       editor.commands.addCommand({

+ 2 - 0
desktop/libs/notebook/src/notebook/static/notebook/js/notebook.ko.js

@@ -151,6 +151,8 @@
     self.ace = ko.observable(null);
     self.errors = ko.observableArray([]);
 
+    self.availableSnippets = vm.availableSnippets();
+
     self.getAceMode = function() {
       return vm.getSnippetViewSettings(self.type()).aceMode;
     };