1
0
mirror of https://github.com/adobe/brackets.git synced 2024-11-20 18:02:54 +01:00

inital support for switching modes

This commit is contained in:
Chris Bank 2012-01-09 13:46:22 -08:00
parent 775bf63f05
commit daa07bcc32
6 changed files with 174 additions and 20 deletions

3
.gitmodules vendored
View File

@ -1,3 +1,6 @@
[submodule "src/thirdparty/CodeMirror2"]
path = src/thirdparty/CodeMirror2
url = git@github.com:adobe/CodeMirror2.git
[submodule "src/thirdparty/path-utils"]
path = src/thirdparty/path-utils
url = git@github.com:jblas/path-utils.git

125
src/EditorUtils.js Normal file
View File

@ -0,0 +1,125 @@
/*
* Copyright 2011 Adobe Systems Incorporated. All Rights Reserved.
*/
/**
* Set of utilites for working with the code editor
*/
define(function(require, exports, module) {
/**
* Change the current mode of the editor based on file extension
* @param {object} editor An instance of a CodeMirror editor
* @param {string} fileUrl A cannonical file URL to extract the extension from
*/
function setModeFromFileExtension( editor, fileUrl ) {
var mimeForExt = _getMimeTypeFromFileExtensions( fileUrl );
setModeFromFileMimetype( editor, mimeForExt );
}
/**
* Change the current mode of the editor based on mimetype
* @param {object} editor An instance of a CodeMirror editor
* @param {string} mimetype A mimetype that matches one of the available modes
*/
function setModeFromFileMimetype( editor, mimetype ) {
_loadModeForMimeType(mimetype);
editor.setOption("mode", mimetype);
}
/**
* @private
* Given a file URL, determines the most likely mimetype based
* off the files extensions. The mapping is used for the text modes
* @param {string} fileUrl A cannonical file URL to extract the extension from
*/
function _getMimeTypeFromFileExtensions( fileUrl ) {
require("thirdparty/path-utils/path-utils.min");
var ext = PathUtils.filenameExtension( fileUrl );
//incase the arg is just the ext
if( !ext )
ext = fileUrl;
if( ext.charAt(0) == '.' )
ext = ext.substr(1);
switch( ext ) {
case "js":
return "text/javascript";
case "json":
return "application/json";
case "css":
case "less":
return "text/css";
case "html":
case "htm":
case "shtml":
case "shtm":
return "text/html";
case "xml":
return "application/xml";
case "aspx":
return "application/x-aspx";
case "ejs":
return "application/x-ejs";
case "jsp":
case "jst":
return "application/x-jsp";
default:
console.log("Called EditorUtils.js _getMimeTypeFromFileExtensions with an unhandled file extension: " + ext);
return "";
}
}
/**
* @private
* Does as needed loading and simple dependecy management when loading modes
* @param {string} mimetype A mimetype that matches one of the available modes
*/
function _loadModeForMimeType( mimetype ) {
switch( mimetype ) {
case "text/javascript":
case "application/json":
require("thirdparty/CodeMirror2/mode/javascript/javascript");
break;
case "text/css":
require("thirdparty/CodeMirror2/mode/css/css");
break;
case "text/html":
_loadModeForMimeType("application/xml");
_loadModeForMimeType("text/javascript");
_loadModeForMimeType("text/css");
require("thirdparty/CodeMirror2/mode/htmlmixed/htmlmixed");
break;
case "application/xml":
require("thirdparty/CodeMirror2/mode/xml/xml");
break;
case "application/x-aspx":
case "application/x-ejs":
case "application/x-jsp":
_loadModeForMimeType("text/html");
require("thirdparty/CodeMirror2/mode/htmlembedded/htmlembedded");
break;
default:
console.log("Called EditorUtils.js _loadModeForMimeType with an unhandled mimetype: " + mimetype);
break;
}
}
// Define public API
exports.setModeFromFileExtension = setModeFromFileExtension;
exports.setModeFromFileMimetype = setModeFromFileMimetype;
});

View File

@ -151,6 +151,9 @@ define(function(require, exports, module) {
}
}
var EditorUtils = require("EditorUtils");
EditorUtils.setModeFromFileExtension(_editor, _currentFilePath);
// TODO: have a real controller object for the editor
_editor.setValue(event.target.result);

View File

@ -5,7 +5,6 @@
// TODO: break out the definition of brackets into a separate module from the application controller logic
define(function(require, exports, module) {
// Load dependent non-module scripts
require("thirdparty/CodeMirror2/mode/javascript/javascript");
require("widgets/bootstrap-dropdown");
require("widgets/bootstrap-modal");

1
src/thirdparty/path-utils vendored Submodule

@ -0,0 +1 @@
Subproject commit 08dba4f8724edeede95660dbd90d8ad2ad13640d

View File

@ -23,6 +23,29 @@ define(function(require, exports, module) {
// verify editor content
expect(myCodeMirror.getValue()).toEqual(content);
});
describe("Modes", function() {
it("should switch to the HTML mode for files ending in .html", function() {
// verify editor content
var EditorUtils = require("EditorUtils");
EditorUtils.setModeFromFileExtension(myCodeMirror, "file:///only/testing/the/path.html");
expect(myCodeMirror.getOption("mode")).toEqual("text/html");
});
it("should switch modes even if the url has a query string", function() {
// verify editor content
var EditorUtils = require("EditorUtils");
EditorUtils.setModeFromFileExtension(myCodeMirror, "http://only.org/testing/the/path.css?v=2");
expect(myCodeMirror.getOption("mode")).toEqual("text/css");
});
it("should accecpt just a file name too", function() {
// verify editor content
var EditorUtils = require("EditorUtils");
EditorUtils.setModeFromFileExtension(myCodeMirror, "path.js");
expect(myCodeMirror.getOption("mode")).toEqual("text/javascript");
});
});
});
});
});