2013-08-02 127 views
0

我正在写Nano like editor in javascript并与计算供显示的偏移的光标位置和文件有问题:页切换算法

我有两个变量_rowssettings.verticalMoveOffset(其默认纳米设置为9等)时您将光标移动到第20行,并且有19行光标被偏移verticalMoveOffset

我在这几个小时工作,无法弄清楚如何映射文件指针(行)到编辑器光标和文件的偏移量(从它开始视图的行)。

最新尝试是

if (y >= this._rows) { 
    var page = Math.floor(y/(this._rows-this._settings.verticalMoveOffset))-1; 
    var new_offset = (this._rows - this._settings.verticalMoveOffset) * page; 
    if (this._offset !== new_offset) { 
     this._view(new_offset); 
    } 
    cursor_y = y - (this._rows*page) + this._settings.verticalMoveOffset; 
} else { 
    if (y <= this._rows - this._settings.verticalMoveOffset - 1 && 
     this._offset !== 0) { 
     this._pointer.x = x; 
     this._pointer.y = y; 
     this._view(0); 
     cursor_y = y; 
    } else if (this._offset !== 0) { 
     cursor_y = (y - this._settings.verticalMoveOffset) + 1; 
    } else { 
     cursor_y = y; 
    } 
} 

,当我从第1页到SWICH页第2和背,到第3页将其切换为1号线快速的IT工作,在cursor_y为-1。在计算页面时,我尝试在各个地方加上或减去1,但它不起作用。

有人可以帮助我吗?

回答

0

我发现溶液(this._offset由this._view设置)

var offset = this._offset + this._rows - cursor_offset; 
if (y-this._offset >= this._rows) { 
    cursor_y = y - offset; 
    if (this._offset !== offset) { 
     this._pointer.x = x; 
     this._pointer.y = y; 
     this._view(offset); 
    } 
} else if (y-this._offset < 0) { 
    var new_offset = this._offset - this._rows + cursor_offset; 
    this._pointer.x = x; 
    this._pointer.y = y; 
    this._view(new_offset); 
    cursor_y = y - new_offset; 
} else { 
    cursor_y = y - offset + cursor_offset + 1; 
}