0
|
1 /* Demonstration of embedding CodeMirror in a bigger application. The
|
|
2 * interface defined here is a mess of prompts and confirms, and
|
|
3 * should probably not be used in a real project.
|
|
4 */
|
|
5
|
|
6 function MirrorFrame(place, options) {
|
|
7 this.home = document.createElement("div");
|
|
8 if (place.appendChild)
|
|
9 place.appendChild(this.home);
|
|
10 else
|
|
11 place(this.home);
|
|
12
|
|
13 var self = this;
|
|
14 function makeButton(name, action) {
|
|
15 var button = document.createElement("input");
|
|
16 button.type = "button";
|
|
17 button.value = name;
|
|
18 self.home.appendChild(button);
|
|
19 button.onclick = function(){self[action].call(self);};
|
|
20 }
|
|
21
|
|
22 makeButton("Search", "search");
|
|
23 makeButton("Replace", "replace");
|
|
24 makeButton("Current line", "line");
|
|
25 makeButton("Jump to line", "jump");
|
|
26 makeButton("Insert constructor", "macro");
|
|
27 makeButton("Indent all", "reindent");
|
|
28
|
|
29 this.mirror = new CodeMirror(this.home, options);
|
|
30 }
|
|
31
|
|
32 MirrorFrame.prototype = {
|
|
33 search: function() {
|
|
34 var text = prompt("Enter search term:", "");
|
|
35 if (!text) return;
|
|
36
|
|
37 var first = true;
|
|
38 do {
|
|
39 var cursor = this.mirror.getSearchCursor(text, first);
|
|
40 first = false;
|
|
41 while (cursor.findNext()) {
|
|
42 cursor.select();
|
|
43 if (!confirm("Search again?"))
|
|
44 return;
|
|
45 }
|
|
46 } while (confirm("End of document reached. Start over?"));
|
|
47 },
|
|
48
|
|
49 replace: function() {
|
|
50 // This is a replace-all, but it is possible to implement a
|
|
51 // prompting replace.
|
|
52 var from = prompt("Enter search string:", ""), to;
|
|
53 if (from) to = prompt("What should it be replaced with?", "");
|
|
54 if (to == null) return;
|
|
55
|
|
56 var cursor = this.mirror.getSearchCursor(from, false);
|
|
57 while (cursor.findNext())
|
|
58 cursor.replace(to);
|
|
59 },
|
|
60
|
|
61 jump: function() {
|
|
62 var line = prompt("Jump to line:", "");
|
|
63 if (line && !isNaN(Number(line)))
|
|
64 this.mirror.jumpToLine(Number(line));
|
|
65 },
|
|
66
|
|
67 line: function() {
|
|
68 alert("The cursor is currently at line " + this.mirror.currentLine());
|
|
69 this.mirror.focus();
|
|
70 },
|
|
71
|
|
72 macro: function() {
|
|
73 var name = prompt("Name your constructor:", "");
|
|
74 if (name)
|
|
75 this.mirror.replaceSelection("function " + name + "() {\n \n}\n\n" + name + ".prototype = {\n \n};\n");
|
|
76 },
|
|
77
|
|
78 reindent: function() {
|
|
79 this.mirror.reindent();
|
|
80 }
|
|
81 };
|