1
0
mirror of https://github.com/BoostIo/Boostnote synced 2025-12-13 17:56:25 +00:00

added fetch snippet

refactored some code

fixed snippet content empty on changed from list

fixed snippet name not updating on list when changed
This commit is contained in:
Nguyễn Việt Hưng
2018-04-27 08:22:54 +07:00
parent a7d0a4bdac
commit e88694b049
6 changed files with 95 additions and 65 deletions

View File

@@ -1,19 +1,22 @@
import fs from 'fs' import fs from 'fs'
import crypto from 'crypto' import crypto from 'crypto'
import consts from 'browser/lib/consts' import consts from 'browser/lib/consts'
import fetchSnippet from 'browser/main/lib/dataApi/fetchSnippet'
function createSnippet (snippets) { function createSnippet () {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const newSnippet = { fetchSnippet().then((snippets) => {
id: crypto.randomBytes(16).toString('hex'), const newSnippet = {
name: 'Unnamed snippet', id: crypto.randomBytes(16).toString('hex'),
prefix: [], name: 'Unnamed snippet',
content: '' prefix: [],
} content: ''
snippets.push(newSnippet) }
fs.writeFile(consts.SNIPPET_FILE, JSON.stringify(snippets, null, 4), (err) => { snippets.push(newSnippet)
if (err) reject(err) fs.writeFile(consts.SNIPPET_FILE, JSON.stringify(snippets, null, 4), (err) => {
resolve(snippets) if (err) reject(err)
resolve(newSnippet)
})
}) })
}) })
} }

View File

@@ -1,17 +1,16 @@
import fs from 'fs' import fs from 'fs'
import consts from 'browser/lib/consts' import consts from 'browser/lib/consts'
import fetchSnippet from 'browser/main/lib/dataApi/fetchSnippet'
function deleteSnippet (snippets, snippetId) { function deleteSnippet (snippet) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
for (let i = 0; i < snippets.length; i++) { fetchSnippet().then((snippets) => {
if (snippets[i].id === snippetId) { snippets = snippets.filter(currentSnippet => currentSnippet.id !== snippet.id)
snippets.splice(i, 1) fs.writeFile(consts.SNIPPET_FILE, JSON.stringify(snippets, null, 4), (err) => {
fs.writeFile(consts.SNIPPET_FILE, JSON.stringify(snippets, null, 4), (err) => { if (err) reject(err)
if (err) reject(err) resolve(snippet)
resolve(snippets) })
}) })
}
}
}) })
} }

View File

@@ -0,0 +1,21 @@
import fs from 'fs'
import crypto from 'crypto'
import consts from 'browser/lib/consts'
function fetchSnippet (id) {
return new Promise((resolve, reject) => {
fs.readFile(consts.SNIPPET_FILE, 'utf8', (err, data) => {
if (err) {
reject(err)
}
const snippets = JSON.parse(data)
if (id) {
const snippet = snippets.find(snippet => { return snippet.id === id })
resolve(snippet)
}
resolve(snippets)
})
})
}
module.exports = fetchSnippet

View File

@@ -16,6 +16,7 @@ const dataApi = {
createSnippet: require('./createSnippet'), createSnippet: require('./createSnippet'),
deleteSnippet: require('./deleteSnippet'), deleteSnippet: require('./deleteSnippet'),
updateSnippet: require('./updateSnippet'), updateSnippet: require('./updateSnippet'),
fetchSnippet: require('./fetchSnippet'),
_migrateFromV6Storage: require('./migrateFromV6Storage'), _migrateFromV6Storage: require('./migrateFromV6Storage'),
_resolveStorageData: require('./resolveStorageData'), _resolveStorageData: require('./resolveStorageData'),

View File

@@ -12,6 +12,7 @@ const buildCMRulers = (rulers, enableRulers) =>
export default class SnippetEditor extends React.Component { export default class SnippetEditor extends React.Component {
componentDidMount () { componentDidMount () {
this.props.onRef(this)
const { rulers, enableRulers } = this.props const { rulers, enableRulers } = this.props
this.cm = CodeMirror(this.refs.root, { this.cm = CodeMirror(this.refs.root, {
rulers: buildCMRulers(rulers, enableRulers), rulers: buildCMRulers(rulers, enableRulers),
@@ -29,9 +30,6 @@ export default class SnippetEditor extends React.Component {
autoCloseBrackets: true autoCloseBrackets: true
}) })
this.cm.setSize('100%', '100%') this.cm.setSize('100%', '100%')
this.snippet = this.props.snippet
const snippetId = this.snippet.id
this.loadSnippet(snippetId)
let changeDelay = null let changeDelay = null
this.cm.on('change', () => { this.cm.on('change', () => {
@@ -44,32 +42,25 @@ export default class SnippetEditor extends React.Component {
}) })
} }
componentWillUnmount () {
this.props.onRef(undefined)
}
onSnippetChanged (newSnippet) {
this.snippet = newSnippet
this.cm.setValue(this.snippet.content)
}
onSnippetNameOrPrefixChanged (newSnippet) {
this.snippet.name = newSnippet.name
this.snippet.prefix = newSnippet.prefix.toString().replace(/\s/g, '').split(',')
this.saveSnippet()
}
saveSnippet () { saveSnippet () {
dataApi.updateSnippet(this.snippet).catch((err) => { throw err }) dataApi.updateSnippet(this.snippet).catch((err) => { throw err })
} }
loadSnippet (snippetId) {
const snippets = JSON.parse(fs.readFileSync(consts.SNIPPET_FILE, 'utf8'))
for (let i = 0; i < snippets.length; i++) {
if (snippets[i].id === snippetId) {
this.cm.setValue(snippets[i].content)
}
}
}
componentWillReceiveProps (newProps) {
if (this.snippet.id !== newProps.snippet.id) {
// when user changed to a new snippet on the snippetList.js
this.loadSnippet(newProps.snippet.id)
} else {
// when snippet name or prefix being changed from snippetTab.js
this.snippet.name = newProps.snippet.name
this.snippet.prefix = newProps.snippet.prefix.replace(/\s/g, '').split(/\,/).filter(val => val)
this.saveSnippet()
}
}
render () { render () {
const { fontSize } = this.props const { fontSize } = this.props
let fontFamily = this.props.fontFamily let fontFamily = this.props.fontFamily

View File

@@ -19,18 +19,30 @@ class SnippetTab extends React.Component {
snippets: [], snippets: [],
currentSnippet: null currentSnippet: null
} }
this.changeDelay = null
} }
componentDidMount () { componentDidMount () {
this.snippets = JSON.parse(fs.readFileSync(consts.SNIPPET_FILE, 'utf8')) this.reloadSnippetList()
this.setState({snippets: this.snippets})
} }
handleSnippetClick (snippet) { handleSnippetClick (snippet) {
const currentSnippet = Object.assign({}, snippet) if (this.state.currentSnippet === null || this.state.currentSnippet.id !== snippet.id) {
currentSnippet.prefix = currentSnippet.prefix.join(', ') dataApi.fetchSnippet(snippet.id).then(changedSnippet => {
this.setState({currentSnippet}) // notify the snippet editor to load the content of the new snippet
this.snippetEditor.onSnippetChanged(changedSnippet)
this.setState({currentSnippet: changedSnippet})
})
}
}
handleSnippetNameOrPrefixChange () {
clearTimeout(this.changeDelay)
this.changeDelay = setTimeout(() => {
// notify the snippet editor that the name or prefix of snippet has been changed
this.snippetEditor.onSnippetNameOrPrefixChanged(this.state.currentSnippet)
this.reloadSnippetList()
}, 500)
} }
handleSnippetContextMenu (snippet) { handleSnippetContextMenu (snippet) {
@@ -38,23 +50,25 @@ class SnippetTab extends React.Component {
menu.append(new MenuItem({ menu.append(new MenuItem({
label: i18n.__('Delete snippet'), label: i18n.__('Delete snippet'),
click: () => { click: () => {
this.deleteSnippet(snippet.id) this.deleteSnippet(snippet)
} }
})) }))
menu.popup() menu.popup()
} }
deleteSnippet (id) { reloadSnippetList () {
dataApi.deleteSnippet(this.snippets, id).then((snippets) => { dataApi.fetchSnippet().then(snippets => this.setState({snippets}))
this.snippets = snippets }
this.setState(this.snippets)
deleteSnippet (snippet) {
dataApi.deleteSnippet(snippet).then(() => {
this.reloadSnippetList()
}).catch(err => { throw err }) }).catch(err => { throw err })
} }
createSnippet () { createSnippet () {
dataApi.createSnippet(this.snippets).then((snippets) => { dataApi.createSnippet().then(() => {
this.snippets = snippets this.reloadSnippetList()
this.setState(this.snippets)
// scroll to end of list when added new snippet // scroll to end of list when added new snippet
const snippetList = document.getElementById('snippets') const snippetList = document.getElementById('snippets')
snippetList.scrollTop = snippetList.scrollHeight snippetList.scrollTop = snippetList.scrollHeight
@@ -100,17 +114,18 @@ class SnippetTab extends React.Component {
</div> </div>
{this.renderSnippetList()} {this.renderSnippetList()}
</div> </div>
{this.state.currentSnippet ? <div styleName='snippet-detail'> <div styleName='snippet-detail' style={{visibility: this.state.currentSnippet ? 'visible' : 'hidden'}}>
<div styleName='group-section'> <div styleName='group-section'>
<div styleName='group-section-label'>{i18n.__('Snippet name')}</div> <div styleName='group-section-label'>{i18n.__('Snippet name')}</div>
<div styleName='group-section-control'> <div styleName='group-section-control'>
<input <input
styleName='group-section-control-input' styleName='group-section-control-input'
value={this.state.currentSnippet.name} value={this.state.currentSnippet ? this.state.currentSnippet.name : ''}
onChange={e => { onChange={e => {
const newSnippet = Object.assign({}, this.state.currentSnippet) const newSnippet = Object.assign({}, this.state.currentSnippet)
newSnippet.name = e.target.value newSnippet.name = e.target.value
this.setState({ currentSnippet: newSnippet }) this.setState({ currentSnippet: newSnippet })
this.handleSnippetNameOrPrefixChange()
}} }}
type='text' /> type='text' />
</div> </div>
@@ -120,11 +135,12 @@ class SnippetTab extends React.Component {
<div styleName='group-section-control'> <div styleName='group-section-control'>
<input <input
styleName='group-section-control-input' styleName='group-section-control-input'
value={this.state.currentSnippet.prefix} value={this.state.currentSnippet ? this.state.currentSnippet.prefix : ''}
onChange={e => { onChange={e => {
const newSnippet = Object.assign({}, this.state.currentSnippet) const newSnippet = Object.assign({}, this.state.currentSnippet)
newSnippet.prefix = e.target.value newSnippet.prefix = e.target.value
this.setState({ currentSnippet: newSnippet }) this.setState({ currentSnippet: newSnippet })
this.handleSnippetNameOrPrefixChange()
}} }}
type='text' /> type='text' />
</div> </div>
@@ -142,10 +158,9 @@ class SnippetTab extends React.Component {
rulers={config.editor.rulers} rulers={config.editor.rulers}
displayLineNumbers={config.editor.displayLineNumbers} displayLineNumbers={config.editor.displayLineNumbers}
scrollPastEnd={config.editor.scrollPastEnd} scrollPastEnd={config.editor.scrollPastEnd}
snippet={this.state.currentSnippet} /> onRef={ref => this.snippetEditor = ref} />
</div> </div>
</div> </div>
: ''}
</div> </div>
) )
} }