Skip to content

Commit 77f9e60

Browse files
committed
自動的にスクロールを合わせてくれる
1 parent 35bb792 commit 77f9e60

3 files changed

Lines changed: 60 additions & 11 deletions

File tree

browser/main/HomePage/ArticleDetail.js

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,34 @@ export default class ArticleDetail extends React.Component {
435435

436436
handleTogglePreviewButtonClick (e) {
437437
if (this.state.article.mode === 'markdown') {
438-
this.setState({previewMode: !this.state.previewMode})
438+
if (!this.state.previewMode) {
439+
let cursorPosition = this.refs.code.getCursorPosition()
440+
let firstVisibleRow = this.refs.code.getFirstVisibleRow()
441+
this.setState({
442+
previewMode: true,
443+
cursorPosition,
444+
firstVisibleRow
445+
}, function () {
446+
let previewEl = ReactDOM.findDOMNode(this.refs.preview)
447+
let anchors = previewEl.querySelectorAll('.lineAnchor')
448+
for (let i = 0; i < anchors.length; i++) {
449+
if (parseInt(anchors[i].dataset.key, 10) > cursorPosition.row || i === anchors.length - 1) {
450+
var targetAnchor = anchors[i > 0 ? i - 1 : 0]
451+
previewEl.scrollTop = targetAnchor.offsetTop - 100
452+
break
453+
}
454+
}
455+
})
456+
} else {
457+
this.setState({
458+
previewMode: false
459+
}, function () {
460+
console.log(this.state.cursorPosition)
461+
this.refs.code.moveCursorTo(this.state.cursorPosition.row, this.state.cursorPosition.column)
462+
this.refs.code.scrollToLine(this.state.firstVisibleRow)
463+
this.refs.code.editor.focus()
464+
})
465+
}
439466
}
440467
}
441468

@@ -524,7 +551,7 @@ export default class ArticleDetail extends React.Component {
524551
</div>
525552

526553
{this.state.previewMode
527-
? <MarkdownPreview content={this.state.article.content}/>
554+
? <MarkdownPreview ref='preview' content={this.state.article.content}/>
528555
: (<CodeEditor
529556
ref='code'
530557
onChange={(e, value) => this.handleContentChange(e, value)}

lib/components/CodeEditor.js

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ module.exports = React.createClass({
3030
editor.renderer.setShowGutter(true)
3131
editor.setTheme('ace/theme/xcode')
3232
editor.clearSelection()
33+
editor.moveCursorTo(0, 0)
3334

3435
editor.setReadOnly(!!this.props.readOnly)
3536

@@ -50,23 +51,33 @@ module.exports = React.createClass({
5051
this.props.onChange(e, value)
5152
}
5253
}.bind(this))
53-
54-
this.setState({editor: editor})
5554
},
5655
componentDidUpdate: function (prevProps) {
57-
if (this.state.editor.getValue() !== this.props.code) {
58-
this.state.editor.setValue(this.props.code)
59-
this.state.editor.clearSelection()
56+
if (this.editor.getValue() !== this.props.code) {
57+
this.editor.setValue(this.props.code)
58+
this.editor.clearSelection()
6059
}
6160
if (prevProps.mode !== this.props.mode) {
62-
var session = this.state.editor.getSession()
61+
var session = this.editor.getSession()
6362
let mode = _.findWhere(modes, {name: this.props.mode})
6463
let syntaxMode = mode != null
6564
? mode.mode
6665
: 'text'
6766
session.setMode('ace/mode/' + syntaxMode)
6867
}
6968
},
69+
getFirstVisibleRow: function () {
70+
return this.editor.getFirstVisibleRow()
71+
},
72+
getCursorPosition: function () {
73+
return this.editor.getCursorPosition()
74+
},
75+
moveCursorTo: function (row, col) {
76+
this.editor.moveCursorTo(row, col)
77+
},
78+
scrollToLine: function (num) {
79+
this.editor.scrollToLine(num, false, false)
80+
},
7081
render: function () {
7182
return (
7283
<div ref='target' className={this.props.className == null ? 'CodeEditor' : 'CodeEditor ' + this.props.className}></div>

lib/markdown.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,31 @@ var md = markdownit({
88
highlight: function (str, lang) {
99
if (lang && hljs.getLanguage(lang)) {
1010
try {
11-
return hljs.highlight(lang, str).value;
11+
return hljs.highlight(lang, str).value
1212
} catch (__) {}
1313
}
1414

1515
try {
16-
return hljs.highlightAuto(str).value;
16+
return hljs.highlightAuto(str).value
1717
} catch (__) {}
1818

19-
return ''; // use external default escaping
19+
return ''
2020
}
2121
})
2222
md.use(emoji)
2323

24+
let originalRenderToken = md.renderer.renderToken
25+
md.renderer.renderToken = function renderToken (tokens, idx, options) {
26+
let token = tokens[idx]
27+
let result = originalRenderToken.call(md.renderer, tokens, idx, options)
28+
if (token.map != null) {
29+
return result + '<a class=\'lineAnchor\' data-key=\'' + token.map[0] + '\'></a>'
30+
}
31+
return result
32+
}
33+
2434
export default function markdown (content) {
2535
if (content == null) content = ''
36+
2637
return md.render(content.toString())
2738
}

0 commit comments

Comments
 (0)