diff --git a/browser/components/CodeEditor.js b/browser/components/CodeEditor.js index 57705560..166b25bb 100644 --- a/browser/components/CodeEditor.js +++ b/browser/components/CodeEditor.js @@ -148,7 +148,7 @@ export default class CodeEditor extends React.Component { cm.execCommand('insertSoftTab') } cm.execCommand('goLineEnd') - } else if (!emptyChars.test(charBeforeCursor) || cursor.ch !== 0) { + } else if (!emptyChars.test(charBeforeCursor) || cursor.ch > 1) { // text expansion on tab key if the char before is alphabet if (expandSnippet(line, cursor, cm, expandData) === false) { if (tabs) { diff --git a/browser/main/modals/PreferencesModal/SnippetEditor.js b/browser/main/modals/PreferencesModal/SnippetEditor.js index 92da3ae6..15110f34 100644 --- a/browser/main/modals/PreferencesModal/SnippetEditor.js +++ b/browser/main/modals/PreferencesModal/SnippetEditor.js @@ -2,6 +2,8 @@ import CodeMirror from 'codemirror' import PropTypes from 'prop-types' import React from 'react' import _ from 'lodash' +import fs from 'fs' +import { findStorage } from 'browser/lib/findStorage' const defaultEditorFontFamily = ['Monaco', 'Menlo', 'Ubuntu Mono', 'Consolas', 'source-code-pro', 'monospace'] const buildCMRulers = (rulers, enableRulers) => @@ -14,7 +16,7 @@ export default class SnippetEditor extends React.Component { componentDidMount () { const { rulers, enableRulers } = this.props - let cm = CodeMirror(this.refs.root, { + this.cm = CodeMirror(this.refs.root, { rulers: buildCMRulers(rulers, enableRulers), lineNumbers: this.props.displayLineNumbers, lineWrapping: true, @@ -29,11 +31,34 @@ export default class SnippetEditor extends React.Component { gutters: ['CodeMirror-linenumbers', 'CodeMirror-foldgutter'], autoCloseBrackets: true, }) - cm.setValue("asdasd") - cm.setSize("100%", "100%") + this.cm.setSize("100%", "100%") + let snippetId = this.props.snippetId + + const storagePath = findStorage(this.props.storageKey).path + const expandDataFile = path.join(storagePath, 'expandData.json') + if (!fs.existsSync(expandDataFile)) { + const defaultExpandData = [ + { + matches: ['lorem', 'ipsum'], + content: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.' + }, + { match: 'h1', content: '# '}, + { match: 'h2', content: '## '}, + { match: 'h3', content: '### '}, + { match: 'h4', content: '#### '}, + { match: 'h5', content: '##### '}, + { match: 'h6', content: '###### '} + ]; + fs.writeFileSync(expandDataFile, JSON.stringify(defaultExpandData), 'utf8') + } + const expandData = JSON.parse(fs.readFileSync(expandDataFile, 'utf8')) } - render() { + componentWillReceiveProps(newProps) { + this.cm.setValue(newProps.value) + } + + render () { const { fontSize } = this.props let fontFamily = this.props.fontFamily fontFamily = _.isString(fontFamily) && fontFamily.length > 0 diff --git a/browser/main/modals/PreferencesModal/SnippetTab.js b/browser/main/modals/PreferencesModal/SnippetTab.js index 74787943..7d46cc65 100644 --- a/browser/main/modals/PreferencesModal/SnippetTab.js +++ b/browser/main/modals/PreferencesModal/SnippetTab.js @@ -13,16 +13,24 @@ class SnippetTab extends React.Component { this.state = { snippets: [ - { id: 'abcsajisdjiasd', name: 'Hello' } - ] + { id: 'abcsajisdjiasd', name: 'Hello', content: 'asdddddddsaddddddd' }, + { id: 'btbjieejbiebfe', name: 'Hello 2', content: 'asdddddddsaddddddd' } + ], + currentSnippet: null } } + handleSnippetClick(id) { + this.setState({'currentSnippet': id}) + } + renderSnippetList () { let { snippets } = this.state return ( snippets.map((snippet) => ( -