15

CodeMirrorでHTMLコードの行を強調表示しており、CodeMirrorエディターを特定の行にスクロールするアンカーを追加したいと思います。

setCursorメソッドを使用してX行までスクロールできます。ただし、CodeMirrorウィンドウの中央にX行を配置したいと思います。それをしてもいいですか?APIとデモを勉強しましたが、運がありませんでした。

ありがとう!

4

4 に答える 4

14

これは機能するはずです:

var editor = CodeMirror.fromTextArea(...);

function jumpToLine(i) { 
    var t = editor.charCoords({line: i, ch: 0}, "local").top; 
    var middleHeight = editor.getScrollerElement().offsetHeight / 2; 
    editor.scrollTo(null, t - middleHeight - 5); 
} 
于 2014-05-09T12:14:06.023 に答える
6

とても簡単です。

初期化:

var editor = CodeMirror.fromTextArea(...);

現在の位置を後で設定する場合は、次を使用できます。

editor.getScrollInfo();

JavaScriptオブジェクトを返します。

{
  "left": 0,
  "top": 0,
  "height": 500,
  "width": 500,
  "clientHeight": 300,
  "clientWidth": 200
} 

これで、次を使用してエディターのスクロール位置を設定できます。

editor.scrollTo(left,top);
于 2013-12-13T05:15:43.237 に答える
3

https://codemirror.net/doc/manual.html#scrollIntoViewが必要です

あなたが望むことをするはずのオプションのマージンパラメータに注意してください:

cm.scrollIntoView(what: {line, ch}|{left, top, right, bottom}|{from, to}|null, ?margin: number)

コードは次のようになります。

cm.scrollIntoView({line:50, char:5}, 200)
于 2017-11-24T12:29:40.097 に答える
2

初期化:

var editor = CodeMirror.fromTextArea(...);

エディターの中央に行を表示する関数:

function jumpToLine(i) {

    // editor.getLineHandle does not help as it does not return the reference of line.
    editor.setCursor(i);
    window.setTimeout(function() {
       editor.setLineClass(i, null, "center-me");
       var line = $('.CodeMirror-lines .center-me');
       var h = line.parent();

       $('.CodeMirror-scroll').scrollTop(0).scrollTop(line.offset().top - $('.CodeMirror-scroll').offset().top - Math.round($('.CodeMirror-scroll').height()/2));
   }, 200);
}
于 2012-05-23T18:29:47.527 に答える