2013-09-27 28 views
1

我想讓它在光標或選區(兩者或兩者之一)在ace編輯器會話中左右移動時,滾動條會滾動以保持光標或textarea中心的選定區域。如何在Ace編輯器中居中選區

有一個名爲centerSelection()函數

this.centerSelection = function() { 
    var range = this.getSelectionRange(); 
    var pos = { 
     row: Math.floor(range.start.row + (range.end.row - range.start.row)/2), 
     column: Math.floor(range.start.column + (range.end.column - range.start.column)/2) 
    } 
    this.renderer.alignCursor(pos, 0.5); }; 

其中「嘗試居中屏幕上的當前選擇。」

這聽起來像我想要的,但它似乎沒有做任何事情。

還有一個名爲「centerselection」

{ 
name: "centerselection", 
bindKey: bindKey(null, "Ctrl-L"), 
exec: function(editor) { editor.centerSelection(); }, 
readOnly: true} 

命令鍵綁定,但它有窗戶沒有按鍵輸入,只是說空。我沒有一個mac,所以我不能嘗試Ctrl-L,我很好奇它爲什麼沒有窗口的關鍵輸入。

玩過它之後,我意識到它正在工作,它只是居中選擇行而不是選擇列。那麼是否有辦法讓選擇位於中央列和中央行? 我想這是因爲alignCursor功能只對準光標行和列不

this.alignCursor = function(cursor, alignment) { 
    if (typeof cursor == "number") 
     cursor = {row: cursor, column: 0}; 

    var pos = this.$cursorLayer.getPixelPosition(cursor); 
    var h = this.$size.scrollerHeight - this.lineHeight; 
    var offset = pos.top - h * (alignment || 0); 

    this.session.setScrollTop(offset); 
    return offset; 
}; 

回答

2

嗯,我想我想通了,下面這段代碼工作正常。 :)

var cursorLeft = editor.renderer.$cursorLayer.getPixelPosition(0).left; 
//gets distance between cursor and left side of textarea in pixels 
var scrollerWidth = editor.renderer.$size.scrollerWidth; 
//gets the width of the text area (that can be seen) 
editor.renderer.scrollToX(cursorLeft - scrollerWidth/2); 
//moves the scroller so that the left side i at the same point as the cursor minus half the width of the area that can be seen 
+0

標記爲已解決。 :) –