浏览代码

[codemirror] Added selectionStyle option

akosyakov 8 年之前
父节点
当前提交
8cdfc1b6a9
共有 1 个文件被更改,包括 32 次插入9 次删除
  1. 32 9
      src/codemirror/editor.ts

+ 32 - 9
src/codemirror/editor.ts

@@ -42,6 +42,11 @@ class CodeMirrorEditor implements CodeEditor.IEditor {
    */
   readonly uuid: string;
 
+  /**
+   * The selection style of this editor.
+   */
+  readonly selectionStyle?: CodeEditor.ISelectionStyle;
+
   /**
    * Handle keydown events for the editor.
    */
@@ -53,6 +58,8 @@ class CodeMirrorEditor implements CodeEditor.IEditor {
   constructor(host: HTMLElement, options: CodeMirrorEditor.IOptions) {
     host.classList.add(EDITOR_CLASS);
     this.uuid = this.uuid;
+    this.selectionStyle = this.selectionStyle;
+
     this._model = new CodeMirrorModel();
 
     options.theme = (options.theme || DEFAULT_CODEMIRROR_THEME);
@@ -230,8 +237,8 @@ class CodeMirrorEditor implements CodeEditor.IEditor {
    * Set the primary selection. This will remove any secondary cursors.
    */
   setSelection(selection: CodeEditor.IRange): void {
-    const cmSelection = this.toCodeMirrorSelection(selection);
-    this._model.doc.setSelection(cmSelection.anchor, cmSelection.head);
+    const { anchor, head } = this.toCodeMirrorSelection(selection);
+    this._model.doc.setSelection(anchor, head);
   }
 
   /**
@@ -286,11 +293,9 @@ class CodeMirrorEditor implements CodeEditor.IEditor {
   protected markSelections(uuid: string, selections: CodeEditor.ITextSelection[]) {
     const markers: CodeMirror.TextMarker[] = [];
     for (const selection of selections) {
-      const cmSelection = this.toCodeMirrorSelection(selection);
-      this._model.doc.markText(cmSelection.anchor, cmSelection.head, {
-        className: selection.className,
-        title: selection.displayName
-      });
+      const { anchor, head } = this.toCodeMirrorSelection(selection);
+      const markerOptions = this.toTextMarkerOptions(selection);
+      this._model.doc.markText(anchor, head, markerOptions);
     }
     this.selectionMarkers[uuid] = markers;
   }
@@ -315,14 +320,28 @@ class CodeMirrorEditor implements CodeEditor.IEditor {
   /**
    * Converts a code mirror selectio to an editor selection.
    */
-  protected toSelection(selection: CodeMirror.Selection) {
+  protected toSelection(selection: CodeMirror.Selection): CodeEditor.ITextSelection {
     return {
       uuid: this.uuid,
       start: this.toPosition(selection.anchor),
-      end: this.toPosition(selection.head)
+      end: this.toPosition(selection.head),
+      style: this.selectionStyle
     };
   }
 
+  /**
+   * Converts the selection style to a text marker options.
+   */
+  protected toTextMarkerOptions(style: CodeEditor.ISelectionStyle | undefined): CodeMirror.TextMarkerOptions | undefined {
+    if (style) {
+      return {
+        className: style.className,
+        title: style.displayName
+      };
+    }
+    return undefined;
+  }
+
   /**
    * Converts an editor selection to a code mirror selection.
    */
@@ -384,5 +403,9 @@ namespace CodeMirrorEditor {
      * The uuid of an editor.
      */
     readonly uuid: string;
+    /**
+     * A selection style.
+     */
+    readonly selectionStyle?: CodeEditor.ISelectionStyle;
   }
 }