Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scroll to a specific line in codemirror #617

Open
GevinHasmitha opened this issue Jan 16, 2024 · 6 comments
Open

Scroll to a specific line in codemirror #617

GevinHasmitha opened this issue Jan 16, 2024 · 6 comments

Comments

@GevinHasmitha
Copy link

I'm adding a classname to a specific line in the codemirror using classnameExt function provided by uiw codemirror. I have access to the classname and the line number of the specific line. I want to scroll to that specific line on a button click.

<CodeMirror id="codemirrorId" value={value} height={height} theme={darkMode ? aura : xcodeLight} extensions={extensions} onChange={onChange} readOnly={readOnly} color="text.primary" />

This is my codemirror component, how can i scroll to the specific line which has my classname?

@jaywcjlove
Copy link
Member

@GevinHasmitha I don't understand what you mean. Do you want to scroll to the specified line?

@GevinHasmitha
Copy link
Author

GevinHasmitha commented Jan 16, 2024

@jaywcjlove Yes, I want the editor to scroll to the line where my added classname is present when i click on a button. (Im adding the classname using the classnameExt extension)

@jaywcjlove
Copy link
Member

Screenshot 2024-01-17 at 13 24 39

You need to get the dom node of div.cm-scroller to control the scroll bar.

Then get the height of the row and position it.

@jaywcjlove
Copy link
Member

function events<T extends keyof HTMLElementEventMap>(opts: Options<T>) {
const { type = 'scroll', events } = opts;
return ViewPlugin.fromClass(
class {
dom?: HTMLElement;
view: EditorView;
constructor(view: EditorView) {
this.view = view;
if (type === 'dom') {
this.dom = view.dom;
} else if (type === 'content') {
this.dom = view.contentDOM;
} else {
this.dom = view.scrollDOM;
}
(Object.keys(events || {}) as Array<keyof typeof events>).forEach((keyname) => {
if (events && events[keyname] && this.dom) {
this.dom.addEventListener(keyname, events[keyname]);
}
});
}
destroy() {
(Object.keys(events || {}) as Array<keyof typeof events>).forEach((keyname) => {
if (events && events[keyname] && this.dom) {
this.dom.removeEventListener(keyname, events[keyname]);
}
});
}
},
);
}

@GevinHasmitha
Copy link
Author

GevinHasmitha commented Jan 17, 2024

 const scroller = document.getElementsByClassName("cm-scroller")[0];
    if (scroller) {
      const totalLines = 200     /* total number of lines */
      const totalHeight = scroller.scrollHeight;
      const singleLineHeight = totalHeight / totalLines;
  
      scroller.scrollTop = (singleLineHeight * x);   /*x is the line number to scroll to*/
    }

@flatsiedatsie
Copy link

 const scroller = document.getElementsByClassName("cm-scroller")[0];
    if (scroller) {
      const totalLines = 200     /* total number of lines */
      const totalHeight = scroller.scrollHeight;
      const singleLineHeight = totalHeight / totalLines;
  
      scroller.scrollTop = (singleLineHeight * x);   /*x is the line number to scroll to*/
    }

That only works if the lines are of equal height. When using CodeMirror as a text-editor that might not be the case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants