Skip to content

Commit 2352c78

Browse files
hikerpigRokt33r
authored andcommitted
Add CodeEditor::setLineContent method to manipulate line contents, related #3163
Avoid changing all CodeMirror instance's contents
1 parent 6ef9c38 commit 2352c78

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
lines changed

browser/components/CodeEditor.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -836,6 +836,17 @@ export default class CodeEditor extends React.Component {
836836
this.editor.setCursor(cursor)
837837
}
838838

839+
/**
840+
* Update content of one line
841+
* @param {Number} lineNumber
842+
* @param {String} content
843+
*/
844+
setLineContent (lineNumber, content) {
845+
const prevContent = this.editor.getLine(lineNumber)
846+
const prevContentLength = prevContent ? prevContent.length : 0
847+
this.editor.replaceRange(content, { line: lineNumber, ch: 0 }, { line: lineNumber, ch: prevContentLength })
848+
}
849+
839850
handleDropImage (dropEvent) {
840851
dropEvent.preventDefault()
841852
const {

browser/components/MarkdownEditor.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,14 +169,15 @@ class MarkdownEditor extends React.Component {
169169
.split('\n')
170170

171171
const targetLine = lines[lineIndex]
172+
let newLine = targetLine
172173

173174
if (targetLine.match(checkedMatch)) {
174-
lines[lineIndex] = targetLine.replace(checkReplace, '[ ]')
175+
newLine = targetLine.replace(checkReplace, '[ ]')
175176
}
176177
if (targetLine.match(uncheckedMatch)) {
177-
lines[lineIndex] = targetLine.replace(uncheckReplace, '[x]')
178+
newLine = targetLine.replace(uncheckReplace, '[x]')
178179
}
179-
this.refs.code.setValue(lines.join('\n'))
180+
this.refs.code.setLineContent(lineIndex, newLine)
180181
}
181182
}
182183

browser/components/MarkdownSplitEditor.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,15 @@ class MarkdownSplitEditor extends React.Component {
8888
.split('\n')
8989

9090
const targetLine = lines[lineIndex]
91+
let newLine = targetLine
9192

9293
if (targetLine.match(checkedMatch)) {
93-
lines[lineIndex] = targetLine.replace(checkReplace, '[ ]')
94+
newLine = targetLine.replace(checkReplace, '[ ]')
9495
}
9596
if (targetLine.match(uncheckedMatch)) {
96-
lines[lineIndex] = targetLine.replace(uncheckReplace, '[x]')
97+
newLine = targetLine.replace(uncheckReplace, '[x]')
9798
}
98-
this.refs.code.setValue(lines.join('\n'))
99+
this.refs.code.setLineContent(lineIndex, newLine)
99100
}
100101
}
101102

0 commit comments

Comments
 (0)