Nano编辑器的页面切换算法

时间:2013-08-02 19:13:02

标签: javascript algorithm nano

我正在编写Nano like editor in javascript并且在计算光标位置和文件偏移量时遇到问题:

我有两个变量_rowssettings.verticalMoveOffset(默认设置为9,就像在Nano中一样)当你在第20行移动光标时,有19行光标偏移{{1} }。

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

最后一次尝试是

verticalMoveOffset

当我从第1页切换到第2页并返回时,它工作到第3页它切换1行到快,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。我在计算页面时尝试在各个地方加上或减去1,但它不起作用。

任何人都可以帮我这个吗?

1 个答案:

答案 0 :(得分: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;
}