1
0
mirror of https://github.com/BoostIo/Boostnote synced 2025-12-13 01:36:22 +00:00

Fix: Improved for the app not to need to reload

This commit is contained in:
roottool
2019-05-07 00:42:55 +09:00
parent 79a29c3461
commit a162bab591
3 changed files with 69 additions and 51 deletions

View File

@@ -25,9 +25,9 @@ import TurndownService from 'turndown'
import {languageMaps} from '../lib/CMLanguageList' import {languageMaps} from '../lib/CMLanguageList'
import snippetManager from '../lib/SnippetManager' import snippetManager from '../lib/SnippetManager'
import {generateInEditor, tocExistsInEditor} from 'browser/lib/markdown-toc-generator' import {generateInEditor, tocExistsInEditor} from 'browser/lib/markdown-toc-generator'
import Jsonlint from 'jsonlint-mod'
import markdownlint from 'markdownlint' import markdownlint from 'markdownlint'
import ConfigManager, {DEFAULT_CONFIG} from '../main/lib/ConfigManager' import Jsonlint from 'jsonlint-mod'
import { DEFAULT_CONFIG } from '../main/lib/ConfigManager'
CodeMirror.modeURL = '../node_modules/codemirror/mode/%N/%N.js' CodeMirror.modeURL = '../node_modules/codemirror/mode/%N/%N.js'
@@ -40,47 +40,6 @@ function translateHotkey (hotkey) {
return hotkey.replace(/\s*\+\s*/g, '-').replace(/Command/g, 'Cmd').replace(/Control/g, 'Ctrl') return hotkey.replace(/\s*\+\s*/g, '-').replace(/Command/g, 'Cmd').replace(/Control/g, 'Ctrl')
} }
const validatorOfMarkdown = (text, updateLinting) => {
const config = ConfigManager.get()
let markdownlintRules = config.editor.customMarkdownLintConfig
try {
Jsonlint.parse(markdownlintRules)
} catch (error) {
markdownlintRules = DEFAULT_CONFIG.editor.customMarkdownLintConfig
}
const lintOptions = {
'strings': {
'content': text
},
'config': JSON.parse(markdownlintRules)
}
return markdownlint(lintOptions, (err, result) => {
if (!err) {
const foundIssues = []
result.content.map(item => {
let ruleNames = ''
item.ruleNames.map((ruleName, index) => {
ruleNames += ruleName
if (index === item.ruleNames.length - 1) {
ruleNames += ': '
} else {
ruleNames += '/'
}
})
foundIssues.push({
from: CodeMirror.Pos(item.lineNumber, 0),
to: CodeMirror.Pos(item.lineNumber, 1),
message: ruleNames + item.ruleDescription,
severity: 'warning'
})
})
updateLinting(foundIssues)
}
})
}
export default class CodeEditor extends React.Component { export default class CodeEditor extends React.Component {
constructor (props) { constructor (props) {
super(props) super(props)
@@ -127,6 +86,8 @@ export default class CodeEditor extends React.Component {
this.searchHandler = (e, msg) => this.handleSearch(msg) this.searchHandler = (e, msg) => this.handleSearch(msg)
this.searchState = null this.searchState = null
this.scrollToLineHandeler = this.scrollToLine.bind(this) this.scrollToLineHandeler = this.scrollToLine.bind(this)
this.setCodeEditorLintConfig = this.setCodeEditorLintConfig.bind(this)
this.validatorOfMarkdown = this.validatorOfMarkdown.bind(this)
this.formatTable = () => this.handleFormatTable() this.formatTable = () => this.handleFormatTable()
@@ -300,7 +261,6 @@ export default class CodeEditor extends React.Component {
snippetManager.init() snippetManager.init()
this.updateDefaultKeyMap() this.updateDefaultKeyMap()
const checkMarkdownNoteIsOpening = this.props.mode === 'Boost Flavored Markdown'
this.value = this.props.value this.value = this.props.value
this.editor = CodeMirror(this.refs.root, { this.editor = CodeMirror(this.refs.root, {
rulers: buildCMRulers(rulers, enableRulers), rulers: buildCMRulers(rulers, enableRulers),
@@ -317,10 +277,7 @@ export default class CodeEditor extends React.Component {
inputStyle: 'textarea', inputStyle: 'textarea',
dragDrop: false, dragDrop: false,
foldGutter: true, foldGutter: true,
lint: checkMarkdownNoteIsOpening ? { lint: this.setCodeEditorLintConfig(),
'getAnnotations': validatorOfMarkdown,
'async': true
} : false,
gutters: ['CodeMirror-linenumbers', 'CodeMirror-foldgutter', 'CodeMirror-lint-markers'], gutters: ['CodeMirror-linenumbers', 'CodeMirror-foldgutter', 'CodeMirror-lint-markers'],
autoCloseBrackets: { autoCloseBrackets: {
pairs: this.props.matchingPairs, pairs: this.props.matchingPairs,
@@ -557,7 +514,8 @@ export default class CodeEditor extends React.Component {
let needRefresh = false let needRefresh = false
const { const {
rulers, rulers,
enableRulers enableRulers,
customMarkdownLintConfig
} = this.props } = this.props
if (prevProps.mode !== this.props.mode) { if (prevProps.mode !== this.props.mode) {
this.setMode(this.props.mode) this.setMode(this.props.mode)
@@ -575,6 +533,11 @@ export default class CodeEditor extends React.Component {
if (prevProps.keyMap !== this.props.keyMap) { if (prevProps.keyMap !== this.props.keyMap) {
needRefresh = true needRefresh = true
} }
if (!needRefresh && prevProps.customMarkdownLintConfig !== customMarkdownLintConfig) {
this.setCodeEditorLintConfig()
needRefresh = true
}
if ( if (
prevProps.enableRulers !== enableRulers || prevProps.enableRulers !== enableRulers ||
@@ -655,6 +618,57 @@ export default class CodeEditor extends React.Component {
} }
} }
setCodeEditorLintConfig () {
const { mode } = this.props
const checkMarkdownNoteIsOpening = mode === 'Boost Flavored Markdown'
return checkMarkdownNoteIsOpening ? {
'getAnnotations': this.validatorOfMarkdown,
'async': true
} : false
}
validatorOfMarkdown (text, updateLinting) {
const { customMarkdownLintConfig } = this.props
let lintConfigJson
try {
Jsonlint.parse(customMarkdownLintConfig)
lintConfigJson = JSON.parse(customMarkdownLintConfig)
} catch (err) {
throw err
}
const lintOptions = {
'strings': {
'content': text
},
'config': lintConfigJson
}
return markdownlint(lintOptions, (err, result) => {
if (!err) {
const foundIssues = []
result.content.map(item => {
let ruleNames = ''
item.ruleNames.map((ruleName, index) => {
ruleNames += ruleName
if (index === item.ruleNames.length - 1) {
ruleNames += ': '
} else {
ruleNames += '/'
}
})
foundIssues.push({
from: CodeMirror.Pos(item.lineNumber, 0),
to: CodeMirror.Pos(item.lineNumber, 1),
message: ruleNames + item.ruleDescription,
severity: 'warning'
})
})
updateLinting(foundIssues)
}
})
}
setMode (mode) { setMode (mode) {
let syntax = CodeMirror.findModeByName(convertModeName(mode || 'text')) let syntax = CodeMirror.findModeByName(convertModeName(mode || 'text'))
if (syntax == null) syntax = CodeMirror.findModeByName('Plain Text') if (syntax == null) syntax = CodeMirror.findModeByName('Plain Text')
@@ -1160,7 +1174,8 @@ CodeEditor.propTypes = {
onChange: PropTypes.func, onChange: PropTypes.func,
readOnly: PropTypes.bool, readOnly: PropTypes.bool,
autoDetect: PropTypes.bool, autoDetect: PropTypes.bool,
spellCheck: PropTypes.bool spellCheck: PropTypes.bool,
customMarkdownLintConfig: PropTypes.string
} }
CodeEditor.defaultProps = { CodeEditor.defaultProps = {
@@ -1172,5 +1187,6 @@ CodeEditor.defaultProps = {
indentSize: 4, indentSize: 4,
indentType: 'space', indentType: 'space',
autoDetect: false, autoDetect: false,
spellCheck: false spellCheck: false,
customMarkdownLintConfig: DEFAULT_CONFIG.editor.customMarkdownLintConfig
} }

View File

@@ -319,6 +319,7 @@ class MarkdownEditor extends React.Component {
enableSmartPaste={config.editor.enableSmartPaste} enableSmartPaste={config.editor.enableSmartPaste}
hotkey={config.hotkey} hotkey={config.hotkey}
switchPreview={config.editor.switchPreview} switchPreview={config.editor.switchPreview}
customMarkdownLintConfig={config.editor.customMarkdownLintConfig}
/> />
<MarkdownPreview styleName={this.state.status === 'PREVIEW' <MarkdownPreview styleName={this.state.status === 'PREVIEW'
? 'preview' ? 'preview'

View File

@@ -179,6 +179,7 @@ class MarkdownSplitEditor extends React.Component {
enableSmartPaste={config.editor.enableSmartPaste} enableSmartPaste={config.editor.enableSmartPaste}
hotkey={config.hotkey} hotkey={config.hotkey}
switchPreview={config.editor.switchPreview} switchPreview={config.editor.switchPreview}
customMarkdownLintConfig={config.editor.customMarkdownLintConfig}
/> />
<div styleName='slider' style={{left: this.state.codeEditorWidthInPercent + '%'}} onMouseDown={e => this.handleMouseDown(e)} > <div styleName='slider' style={{left: this.state.codeEditorWidthInPercent + '%'}} onMouseDown={e => this.handleMouseDown(e)} >
<div styleName='slider-hitbox' /> <div styleName='slider-hitbox' />