Skip to content

Commit

Permalink
clear the history of editor when switch between docs. oldj#348
Browse files Browse the repository at this point in the history
  • Loading branch information
oldj committed Jul 26, 2019
1 parent cccd2fe commit d63f042
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 9 deletions.
51 changes: 44 additions & 7 deletions app-ui/content/Editor.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ export default class Editor extends React.Component {
this.state = {
marks: [],
pos: [],
search_kw: ''
search_kw: '',
id: null,
code: '',
readonly: false
}

}
Expand Down Expand Up @@ -186,17 +189,51 @@ export default class Editor extends React.Component {
})
}

componentWillReceiveProps (next_props) { // todo ...
//console.log(next_props);
//componentWillReceiveProps (next_props) { // todo ...
// //console.log(next_props);
// let cm = this.codemirror
// let doc = cm.getDoc()
// let v = doc.getValue()
// if (v !== next_props.code) {
// let cursor_pos = doc.getCursor()
// doc.setValue(next_props.code)
// doc.setCursor(cursor_pos)
// }
// cm.setOption('readOnly', next_props.readonly)
// setTimeout(() => {
// //this.highlightKeyword()
// this.doSearch()
// }, 100)
//}

static getDerivedStateFromProps (nextProps, prevState) {
let state = {}

;(['code', 'readonly', 'id']).map(k => {
if (prevState[k] !== nextProps[k]) {
state[k] = nextProps[k]
}
})

return state
}

componentDidUpdate (prevProps, prevState) {
let cm = this.codemirror
let doc = cm.getDoc()
let v = doc.getValue()
if (v !== next_props.code) {

let {code, readonly, id} = this.state
if (doc.getValue() !== code) {
let cursor_pos = doc.getCursor()
doc.setValue(next_props.code)
doc.setValue(code)
doc.setCursor(cursor_pos)
}
cm.setOption('readOnly', next_props.readonly)

if (id !== prevState.id) {
setTimeout(() => doc.clearHistory(), 300)
}

cm.setOption('readOnly', readonly)
setTimeout(() => {
//this.highlightKeyword()
this.doSearch()
Expand Down
2 changes: 1 addition & 1 deletion app/ui/app.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion app/version.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
module.exports = [3, 4, 0, 5439]
module.exports = [3, 4, 0, 5440]

0 comments on commit d63f042

Please sign in to comment.