浏览代码

[codeeditor] Replace model.setMimeTypeFromPath by mimeTypeService.getMimeTypeByFilePath

akosyakov 8 年之前
父节点
当前提交
da671c2551

+ 0 - 12
src/codeeditor/editor.ts

@@ -198,11 +198,6 @@ namespace CodeEditor {
      * Clear the undo history.
      * Clear the undo history.
      */
      */
     clearHistory(): void;
     clearHistory(): void;
-
-    /**
-     * Update mime type 
-     */
-    setMimeTypeFromPath(path: string): void;
   }
   }
 
 
   /**
   /**
@@ -453,13 +448,6 @@ namespace CodeEditor {
      */
      */
     clearHistory(): void { /* no-op */ }
     clearHistory(): void { /* no-op */ }
 
 
-    /**
-     * Set mime type for given path.
-     */
-    setMimeTypeFromPath(path: string): void {
-      this.mimeType = '';
-    }
-
     private _mimetype = '';
     private _mimetype = '';
     private _value = '';
     private _value = '';
     private _selections = new ObservableVector<ITextSelection>();
     private _selections = new ObservableVector<ITextSelection>();

+ 2 - 2
src/codeeditor/mimetype.ts

@@ -16,14 +16,14 @@ interface IEditorMimeTypeService {
    * #### Notes
    * #### Notes
    * If a mime type cannot be found returns the defaul mime type `text/plain`, never `null`.  
    * If a mime type cannot be found returns the defaul mime type `text/plain`, never `null`.  
    */
    */
-  getMimeTypeForLanguage(info: nbformat.ILanguageInfoMetadata): string;
+  getMimeTypeByLanguage(info: nbformat.ILanguageInfoMetadata): string;
   /**
   /**
    * Returns a mime type for the given file path.
    * Returns a mime type for the given file path.
    * 
    * 
    * #### Notes
    * #### Notes
    * If a mime type cannot be found returns the defaul mime type `text/plain`, never `null`.
    * If a mime type cannot be found returns the defaul mime type `text/plain`, never `null`.
    */
    */
-  getMimeTypeForPath(path: string): string;
+  getMimeTypeByFilePath(filePath: string): string;
 }
 }
 
 
 /**
 /**

+ 3 - 5
src/codemirror/mimetype.ts

@@ -24,7 +24,7 @@ class CodeMirrorMimeTypeService implements IEditorMimeTypeService {
    * #### Notes
    * #### Notes
    * If a mime type cannot be found returns the defaul mime type `text/plain`, never `null`.  
    * If a mime type cannot be found returns the defaul mime type `text/plain`, never `null`.  
    */
    */
-  getMimeTypeForLanguage(info: nbformat.ILanguageInfoMetadata): string {
+  getMimeTypeByLanguage(info: nbformat.ILanguageInfoMetadata): string {
     if (info.codemirror_mode) {
     if (info.codemirror_mode) {
       return findMode(info.codemirror_mode as any).mime;
       return findMode(info.codemirror_mode as any).mime;
     }
     }
@@ -47,10 +47,8 @@ class CodeMirrorMimeTypeService implements IEditorMimeTypeService {
    * #### Notes
    * #### Notes
    * If a mime type cannot be found returns the defaul mime type `text/plain`, never `null`.  
    * If a mime type cannot be found returns the defaul mime type `text/plain`, never `null`.  
    */
    */
-  getMimeTypeForPath(path: string): string {
-    const index = path.lastIndexOf('.');
-    const extension = index === -1 ? path : path.substring(index);
-    const mode = CodeMirror.findModeByExtension(extension);
+  getMimeTypeByFilePath(path: string): string {
+    const mode = CodeMirror.findModeByFileName(path);
     return mode ? mode.mime : IEditorMimeTypeService.defaultMimeType;
     return mode ? mode.mime : IEditorMimeTypeService.defaultMimeType;
   }
   }
 }
 }

+ 0 - 10
src/codemirror/model.ts

@@ -187,16 +187,6 @@ class CodeMirrorModel implements CodeEditor.IModel {
     this._doc.clearHistory();
     this._doc.clearHistory();
   }
   }
 
 
-  /**
-   * Update mime type from given path.
-   */
-  setMimeTypeFromPath(path: string): void {
-    let mode = CodeMirror.findModeByFileName(path);
-    requireMode(mode).then((modespec) => {
-      this.mimeType = modespec.mime;
-    });
-  }
-
   private _onSelectionChanged(sender: ObservableVector<CodeEditor.ITextSelection>, change: ObservableVector.IChangedArgs<CodeEditor.ITextSelection>): void {
   private _onSelectionChanged(sender: ObservableVector<CodeEditor.ITextSelection>, change: ObservableVector.IChangedArgs<CodeEditor.ITextSelection>): void {
     // TODO
     // TODO
   }
   }

+ 10 - 10
src/editorwidget/widget.ts

@@ -16,11 +16,7 @@ import {
 } from '../docregistry';
 } from '../docregistry';
 
 
 import {
 import {
-  CodeEditor
-} from '../codeeditor/editor';
-
-import {
-  IEditorServices
+  CodeEditor, IEditorServices, IEditorMimeTypeService
 } from '../codeeditor';
 } from '../codeeditor';
 
 
 import {
 import {
@@ -66,7 +62,10 @@ class EditorWidget extends CodeEditorWidget {
   /**
   /**
    * Construct a new editor widget.
    * Construct a new editor widget.
    */
    */
-  constructor(editorFactory: (host: Widget) => CodeEditor.IEditor, context: DocumentRegistry.Context) {
+  constructor(
+    editorFactory: (host: Widget) => CodeEditor.IEditor,
+    context: DocumentRegistry.Context,
+    editorMimeTypeService: IEditorMimeTypeService) {
     super(editorFactory);
     super(editorFactory);
     this.addClass(EDITOR_CLASS);
     this.addClass(EDITOR_CLASS);
     this._context = context;
     this._context = context;
@@ -103,9 +102,9 @@ class EditorWidget extends CodeEditorWidget {
     this.editor.model.valueChanged.connect((sender, args) => {
     this.editor.model.valueChanged.connect((sender, args) => {
       model.fromString(args.newValue);
       model.fromString(args.newValue);
     });
     });
-    editor.model.setMimeTypeFromPath(context.path);
+    editor.model.mimeType = editorMimeTypeService.getMimeTypeByFilePath(context.path);
     context.pathChanged.connect((c, path) => {
     context.pathChanged.connect((c, path) => {
-      editor.model.setMimeTypeFromPath(path);
+      editor.model.mimeType = editorMimeTypeService.getMimeTypeByFilePath(path);
       this.title.label = path.split('/').pop();
       this.title.label = path.split('/').pop();
     });
     });
 
 
@@ -137,14 +136,15 @@ class EditorWidgetFactory extends ABCWidgetFactory<EditorWidget, DocumentRegistr
    * Create a new widget given a context.
    * Create a new widget given a context.
    */
    */
   protected createNewWidget(context: DocumentRegistry.Context): EditorWidget {
   protected createNewWidget(context: DocumentRegistry.Context): EditorWidget {
+    const { factory, mimeTypeService } = this._editorServices;
     return new EditorWidget((host: Widget) => {
     return new EditorWidget((host: Widget) => {
-      let editor = this._editorServices.factory.newDocumentEditor(host.node, {
+      let editor = factory.newDocumentEditor(host.node, {
           lineNumbers: true,
           lineNumbers: true,
           readOnly: false,
           readOnly: false,
           wordWrap: true,
           wordWrap: true,
       });
       });
       return editor;
       return editor;
-    }, context);
+    }, context, mimeTypeService);
   }
   }
 
 
   private _editorServices: IEditorServices;
   private _editorServices: IEditorServices;

+ 1 - 1
src/notebook/notebook/widget.ts

@@ -619,7 +619,7 @@ namespace StaticNotebook {
      * Get the preferred mimetype given language info.
      * Get the preferred mimetype given language info.
      */
      */
     getCodeMimetype(info: nbformat.ILanguageInfoMetadata): string {
     getCodeMimetype(info: nbformat.ILanguageInfoMetadata): string {
-      return this.editorMimeTypeService.getMimeTypeForLanguage(info);
+      return this.editorMimeTypeService.getMimeTypeByLanguage(info);
     }
     }
   }
   }