Skip to content

Commit d4164f1

Browse files
author
Yang Zhen
committed
Add file highlight
1 parent f5f90ab commit d4164f1

File tree

7 files changed

+56
-7
lines changed

7 files changed

+56
-7
lines changed

app/commands/commandBindings/file.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { notify } from '../../components/Notification/actions'
99
import i18n from 'utils/createI18n'
1010
import icons from 'file-icons-js'
1111
import { toJS, when } from 'mobx'
12+
import emitter, { FILE_HIGHLIGHT } from 'utils/emitter'
1213

1314
const nodeToNearestDirPath = (node) => {
1415
if (!node) node = { isDir: true, path: '/' } // fake a root node if !node
@@ -151,6 +152,11 @@ const fileCommands = {
151152
openFile(c.data)
152153
}
153154
},
155+
'file:highlight_line': (c) => {
156+
console.log('file:highlight_line', c)
157+
// const { path, lineNumber } = c.data
158+
emitter.emit(FILE_HIGHLIGHT, c.data)
159+
},
154160
'file:new_file': (c) => {
155161
const node = c.context
156162
const path = nodeToNearestDirPath(node)

app/components/Editor/components/CodeEditor/BaseCodeEditor.jsx

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React, { Component } from 'react'
22
import PropTypes from 'prop-types'
33
import { Editor } from 'components/Editor/state'
4+
import { emitter, E } from 'utils'
45

56
class BaseCodeEditor extends Component {
67
constructor (props) {
@@ -12,6 +13,7 @@ class BaseCodeEditor extends Component {
1213
this.editor = editor
1314
this.cm = this.editor.cm
1415
this.cmDOM = this.cm.getWrapperElement()
16+
this.highlight = this.highlight.bind(this)
1517
}
1618

1719
componentDidMount () {
@@ -21,11 +23,14 @@ class BaseCodeEditor extends Component {
2123
// `setSize()` and `refresh()` are required to correctly render cm
2224
cm.setSize('100%', '100%')
2325
cm.refresh()
24-
const scrollLine = this.editor.scrollLine
25-
setTimeout(() => {
26-
cm.scrollIntoView({ line: scrollLine, ch: 0 })
27-
cm.focus()
28-
}, 0)
26+
// const scrollLine = this.editor.scrollLine
27+
// setTimeout(() => {
28+
// cm.scrollIntoView({ line: scrollLine, ch: 0 })
29+
// cm.setCursor({ line: scrollLine, ch: 0 })
30+
// cm.focus()
31+
// }, 100)
32+
33+
emitter.on(E.FILE_HIGHLIGHT, this.highlight)
2934
}
3035

3136
render () {
@@ -37,6 +42,17 @@ class BaseCodeEditor extends Component {
3742
componentWillUnmount () {
3843
const async = true
3944
this.editor.destroy(async)
45+
emitter.removeListener(E.FILE_HIGHLIGHT, this.highlight)
46+
}
47+
48+
highlight (data) {
49+
const { path, lineNumber } = data
50+
if (this.editor.file && this.editor.file.path === path) {
51+
if (this.breakpointHighlight) {
52+
this.cm.removeLineClass(this.breakpointHighlight, 'background', 'breakpoint-highlight')
53+
}
54+
this.breakpointHighlight = this.cm.addLineClass(lineNumber - 1, 'background', 'breakpoint-highlight')
55+
}
4056
}
4157
}
4258

app/components/Editor/components/CodeEditor/mixins/basicMixin.jsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,10 @@ export default {
6262
scroll: (cm) => {
6363
const { editor } = this.props
6464
const { top, clientHeight } = cm.getScrollInfo()
65-
editor.scrollLine = Math.floor((top + clientHeight) / cm.defaultTextHeight()) - 1
65+
editor.update({
66+
scrollLine: Math.floor((top + clientHeight) / cm.defaultTextHeight()) - 1
67+
})
68+
// editor.scrollLine = Math.floor((top + clientHeight) / cm.defaultTextHeight()) - 1
6669
},
6770
}
6871
},

app/components/Editor/components/CodeEditor/mixins/eslintMixin/eslintMixin.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@ export default {
3434
},
3535
componentDidMount () {
3636
const cm = this.cm
37-
cm.setOption('gutters', ['CodeMirror-lint-markers'])
37+
const gutters = cm.options.gutters
38+
if (gutters.findIndex(item => item === 'CodeMirror-lint-markers') < 0) {
39+
cm.setOption('gutters', ['CodeMirror-lint-markers', ...gutters])
40+
}
41+
// cm.setOption('gutters', ['CodeMirror-lint-markers'])
3842
cm.setOption('lint', lintOption)
3943
}
4044
}

app/components/Editor/state.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,22 @@ class Editor {
8080
if (this.content) {
8181
cm.setValue(this.content)
8282
cm.clearHistory()
83+
const scrollLine = this.scrollLine || 0
84+
if (scrollLine > 0) {
85+
cm.scrollIntoView({ line: scrollLine, ch: 0 })
86+
// cm.setCursor({ line: scrollLine - 1, ch: 0 })
87+
}
88+
cm.focus()
8389
}
90+
91+
autorun(() => {
92+
const cursorLine = this.cursorLine || 0
93+
if (cursorLine > 0) {
94+
cm.scrollIntoView({ line: cursorLine - 1, ch: 0 })
95+
cm.setCursor({ line: cursorLine - 1, ch: 0 })
96+
}
97+
})
98+
8499
if (!this.file) {
85100
cm.setCursor(cm.posFromIndex(this.content.length))
86101
}

app/styles/core-ui/Editor.styl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
line-height: 1.3;
1212
}
1313

14+
.CodeMirror .breakpoint-highlight {
15+
background: red;
16+
}
17+
1418
.status-bar-editor-widgets {
1519
display: flex;
1620
align-items: stretch;

app/utils/emitter/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ export const THEME_CHANGED = 'THEME_CHANGED'
77
export const SOCKET_TRIED_FAILED = 'SOCKET_TRIED_FAILED'
88
export const SOCKET_RETRY = 'SOCKET_RETRY'
99
export const FILE_CHANGE = 'FILE_CHANGE'
10+
export const FILE_HIGHLIGHT = 'FILE_HIGHLIGHT'

0 commit comments

Comments
 (0)