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

Enable CTRL + B in a word to make it bold

This commit is contained in:
asmsuechan
2017-02-22 03:33:42 +09:00
parent c3e92b3b81
commit 49a4ec5e16

View File

@@ -11,7 +11,9 @@ class MarkdownEditor extends React.Component {
this.escapeFromEditor = ['Control', 'w'] this.escapeFromEditor = ['Control', 'w']
this.supportBold = ['Control', 'b'] this.supportMdBold = ['Control', 'b']
this.supportMdWordBold = ['Control', ':']
this.state = { this.state = {
status: 'PREVIEW', status: 'PREVIEW',
@@ -169,9 +171,12 @@ class MarkdownEditor extends React.Component {
if (!this.state.isLocked && this.state.status === 'CODE' && this.escapeFromEditor.every(isNoteHandlerKey)) { if (!this.state.isLocked && this.state.status === 'CODE' && this.escapeFromEditor.every(isNoteHandlerKey)) {
document.activeElement.blur() document.activeElement.blur()
} }
if (this.supportBold.every(isNoteHandlerKey)) { if (this.supportMdBold.every(isNoteHandlerKey)) {
this.addMdAndMoveCaretToCenter('****') this.addMdAndMoveCaretToCenter('****')
} }
if (this.supportMdWordBold.every(isNoteHandlerKey)) {
this.addMdBetweenWord('**')
}
} }
addMdAndMoveCaretToCenter (md) { addMdAndMoveCaretToCenter (md) {
@@ -181,6 +186,14 @@ class MarkdownEditor extends React.Component {
this.refs.code.editor.setCursor({line: currentCaret.line, ch: currentCaret.ch + md.length/2}) this.refs.code.editor.setCursor({line: currentCaret.line, ch: currentCaret.ch + md.length/2})
} }
addMdBetweenWord (md) {
const currentCaret = this.refs.code.editor.getCursor()
const word = this.refs.code.editor.findWordAt(currentCaret)
const cmDoc = this.refs.code.editor.getDoc()
cmDoc.replaceRange(md, word.anchor)
cmDoc.replaceRange(md, { line: word.head.line, ch: word.head.ch + md.length })
}
handleKeyUp (e) { handleKeyUp (e) {
const keyPressed = Object.assign(this.state.keyPressed, { const keyPressed = Object.assign(this.state.keyPressed, {
[e.key]: false [e.key]: false