Skip to content
This repository was archived by the owner on May 12, 2026. It is now read-only.

Commit 1ecf1e0

Browse files
authored
Merge pull request #2494 from intercloud/feat/link-to-a-line
feat(editor): add ability to jump to line
2 parents 1ea1482 + e536d20 commit 1ecf1e0

2 files changed

Lines changed: 18 additions & 1 deletion

File tree

browser/components/CodeEditor.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ export default class CodeEditor extends React.Component {
5757
}
5858
this.searchHandler = (e, msg) => this.handleSearch(msg)
5959
this.searchState = null
60+
this.scrollToLineHandeler = this.scrollToLine.bind(this)
6061

6162
this.formatTable = () => this.handleFormatTable()
6263
this.editorActivityHandler = () => this.handleEditorActivity()
@@ -125,6 +126,7 @@ export default class CodeEditor extends React.Component {
125126
componentDidMount () {
126127
const { rulers, enableRulers } = this.props
127128
const expandSnippet = this.expandSnippet.bind(this)
129+
eventEmitter.on('line:jump', this.scrollToLineHandeler)
128130

129131
const defaultSnippet = [
130132
{
@@ -475,7 +477,13 @@ export default class CodeEditor extends React.Component {
475477

476478
moveCursorTo (row, col) {}
477479

478-
scrollToLine (num) {}
480+
scrollToLine (event, num) {
481+
const cursor = {
482+
line: num,
483+
ch: 1
484+
}
485+
this.editor.setCursor(cursor)
486+
}
479487

480488
focus () {
481489
this.editor.focus()

browser/components/MarkdownPreview.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -866,6 +866,15 @@ export default class MarkdownPreview extends React.Component {
866866
return
867867
}
868868

869+
const regexIsLine = /^:line:[0-9]/
870+
if (regexIsLine.test(linkHash)) {
871+
const numberPattern = /\d+/g
872+
873+
const lineNumber = parseInt(linkHash.match(numberPattern)[0])
874+
eventEmitter.emit('line:jump', lineNumber)
875+
return
876+
}
877+
869878
// this will match the old link format storage.key-note.key
870879
// e.g.
871880
// 877f99c3268608328037-1c211eb7dcb463de6490

0 commit comments

Comments
 (0)