##// END OF EJS Templates
Refactor editor into Editor class
Thomas Kluyver -
Show More
@@ -0,0 +1,80
1 // Copyright (c) IPython Development Team.
2 // Distributed under the terms of the Modified BSD License.
3
4 define([
5 'jquery',
6 'base/js/utils',
7 'codemirror/lib/codemirror',
8 'codemirror/mode/meta',
9 ],
10 function($,
11 utils,
12 CodeMirror
13 ) {
14 var Editor = function(selector, options) {
15 this.selector = selector;
16 this.contents = options.contents;
17 this.events = options.events;
18 this.base_url = options.base_url;
19 this.file_path = options.file_path;
20
21 this.codemirror = CodeMirror($(this.selector)[0]);
22
23 this.save_enabled = true;
24 };
25
26 // TODO: Remove this once the contents API is refactored to just use paths
27 Editor.prototype._split_path = function() {
28 var ix = this.file_path.lastIndexOf("/");
29 if (ix === -1) {
30 return ['', this.file_path];
31 } else {
32 return [
33 this.file_path.substring(0, ix),
34 this.file_path.substring(ix+1)
35 ];
36 }
37 };
38
39 Editor.prototype.load = function() {
40 var split_path = this._split_path();
41 var cm = this.codemirror;
42 this.contents.load(split_path[0], split_path[1], {
43 success: function(model) {
44 if (model.type === "file" && model.format === "text") {
45 cm.setValue(model.content);
46
47 // Find and load the highlighting mode
48 var modeinfo = CodeMirror.findModeByMIME(model.mimetype);
49 if (modeinfo) {
50 utils.requireCodeMirrorMode(modeinfo.mode, function() {
51 cm.setOption('mode', modeinfo.mode);
52 });
53 }
54 } else {
55 this.codemirror.setValue("Error! Not a text file. Saving disabled.");
56 this.save_enabled = false;
57 }
58 }
59 });
60 };
61
62 Editor.prototype.save = function() {
63 var split_path = this._split_path();
64 var model = {
65 path: split_path[0],
66 name: split_path[1],
67 type: 'file',
68 format: 'text',
69 content: this.codemirror.getValue(),
70 };
71 var that = this;
72 this.contents.save(split_path[0], split_path[1], model, {
73 success: function() {
74 that.events.trigger("save_succeeded.TextEditor");
75 }
76 });
77 };
78
79 return {Editor: Editor};
80 });
@@ -3,70 +3,45
3
3
4 require([
4 require([
5 'jquery',
5 'jquery',
6 'base/js/namespace',
6 'base/js/utils',
7 'base/js/utils',
7 'base/js/page',
8 'base/js/page',
8 'base/js/events',
9 'base/js/events',
9 'contents',
10 'contents',
10 'codemirror/lib/codemirror',
11 'texteditor/js/editor',
11 'texteditor/js/menubar',
12 'texteditor/js/menubar',
12 'codemirror/mode/meta',
13 'custom/custom',
13 'custom/custom',
14 ], function(
14 ], function(
15 $,
15 $,
16 IPython,
16 utils,
17 utils,
17 page,
18 page,
18 events,
19 events,
19 contents,
20 contents,
20 CodeMirror,
21 editor,
21 menubar
22 menubar
22 ){
23 ){
23 page = new page.Page();
24 page = new page.Page();
24
25
25 var base_url = utils.get_body_data('baseUrl');
26 var base_url = utils.get_body_data('baseUrl');
26 contents = new contents.Contents({base_url: base_url});
27
28 var file_path = utils.get_body_data('filePath');
27 var file_path = utils.get_body_data('filePath');
29 var ix = file_path.lastIndexOf("/");
28 contents = new contents.Contents({base_url: base_url});
30 var dir_path, basename;
31 if (ix == -1) {
32 dir_path = '';
33 basename = file_path;
34 } else {
35 dir_path = file_path.substring(0, ix);
36 basename = file_path.substring(ix+1);
37 }
38 contents.load(dir_path, basename, {
39 success: function(model) {
40 page.show();
41 if (model.type === "file" && model.format === "text") {
42 console.log(modeinfo);
43 var cm = CodeMirror($('#texteditor-container')[0], {
44 value: model.content,
45 });
46
29
47 var menus = new menubar.MenuBar('#menubar', {
30 var editor = new editor.Editor('#texteditor-container', {
48 base_url: base_url,
31 base_url: base_url,
49 codemirror: cm,
50 contents: contents,
51 events: events,
32 events: events,
52 file_path: file_path
33 contents: contents,
34 file_path: file_path,
53 });
35 });
54
36
55 // Find and load the highlighting mode
37 // Make it available for debugging
56 var modeinfo = CodeMirror.findModeByMIME(model.mimetype);
38 IPython.editor = editor;
57 if (modeinfo) {
58 utils.requireCodeMirrorMode(modeinfo.mode, function() {
59 cm.setOption('mode', modeinfo.mode);
60 });
61 }
62
39
63 // Attach to document for debugging
40 var menus = new menubar.MenuBar('#menubar', {
64 document.cm_instance = cm;
41 base_url: base_url,
65 } else {
42 editor: editor,
66 $('#texteditor-container').append(
67 $('<p/>').text(dir_path + " is not a text file")
68 );
69 }
70 }
71 });
43 });
44
45 editor.load();
46 page.show();
72 });
47 });
@@ -26,10 +26,7 define([
26 options = options || {};
26 options = options || {};
27 this.base_url = options.base_url || utils.get_body_data("baseUrl");
27 this.base_url = options.base_url || utils.get_body_data("baseUrl");
28 this.selector = selector;
28 this.selector = selector;
29 this.codemirror = options.codemirror;
29 this.editor = options.editor;
30 this.contents = options.contents;
31 this.events = options.events;
32 this.file_path = options.file_path;
33
30
34 if (this.selector !== undefined) {
31 if (this.selector !== undefined) {
35 this.element = $(selector);
32 this.element = $(selector);
@@ -41,28 +38,7 define([
41 // File
38 // File
42 var that = this;
39 var that = this;
43 this.element.find('#save_file').click(function () {
40 this.element.find('#save_file').click(function () {
44 var ix = that.file_path.lastIndexOf("/");
41 that.editor.save();
45 var dir_path, basename;
46 if (ix === -1) {
47 dir_path = '';
48 basename = that.file_path;
49 } else {
50 dir_path = that.file_path.substring(0, ix);
51 basename = that.file_path.substring(ix+1);
52 }
53 var model = {
54 path: dir_path,
55 name: basename,
56 type: 'file',
57 format: 'text',
58 content: that.codemirror.getValue(),
59 };
60 console.log(model);
61 that.contents.save(dir_path, basename, model, {
62 success: function() {
63 that.events.trigger("save_succeeded.TextEditor");
64 }
65 });
66 });
42 });
67 };
43 };
68
44
General Comments 0
You need to be logged in to leave comments. Login now