mirror of
https://gitee.com/vnotex/vnote.git
synced 2025-07-05 05:49:53 +08:00
add shortcut Ctrl+O to insert backquotes
Signed-off-by: Le Tan <tamlokveer@gmail.com>
This commit is contained in:
parent
f533dd6f95
commit
336d9ad109
@ -249,6 +249,14 @@ bool VMdEditOperations::handleKeyPressEvent(QKeyEvent *p_event)
|
||||
break;
|
||||
}
|
||||
|
||||
case Qt::Key_O:
|
||||
{
|
||||
if (handleKeyO(p_event)) {
|
||||
return true;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case Qt::Key_U:
|
||||
{
|
||||
if (handleKeyU(p_event)) {
|
||||
@ -515,6 +523,50 @@ bool VMdEditOperations::handleKeyI(QKeyEvent *p_event)
|
||||
return false;
|
||||
}
|
||||
|
||||
bool VMdEditOperations::handleKeyO(QKeyEvent *p_event)
|
||||
{
|
||||
if (p_event->modifiers() == Qt::ControlModifier) {
|
||||
// Ctrl+O, inline codeblock.
|
||||
QTextCursor cursor = m_editor->textCursor();
|
||||
if (cursor.hasSelection()) {
|
||||
// Insert ` around the selected text.
|
||||
int start = cursor.selectionStart();
|
||||
int end = cursor.selectionEnd();
|
||||
cursor.beginEditBlock();
|
||||
cursor.clearSelection();
|
||||
cursor.setPosition(start, QTextCursor::MoveAnchor);
|
||||
cursor.insertText("`");
|
||||
cursor.setPosition(end + 1, QTextCursor::MoveAnchor);
|
||||
cursor.insertText("`");
|
||||
cursor.endEditBlock();
|
||||
m_editor->setTextCursor(cursor);
|
||||
} else {
|
||||
// Insert `` and place cursor in the middle.
|
||||
// Or if there are one ` after current cursor, just skip it.
|
||||
cursor.beginEditBlock();
|
||||
int pos = cursor.positionInBlock();
|
||||
bool hasBackquote = false;
|
||||
QString text = cursor.block().text();
|
||||
if (pos <= text.size() - 1) {
|
||||
if (text[pos] == '`') {
|
||||
hasBackquote = true;
|
||||
}
|
||||
}
|
||||
if (hasBackquote) {
|
||||
cursor.movePosition(QTextCursor::Right, QTextCursor::MoveAnchor, 1);
|
||||
} else {
|
||||
cursor.insertText("``");
|
||||
cursor.movePosition(QTextCursor::Left, QTextCursor::MoveAnchor, 1);
|
||||
}
|
||||
cursor.endEditBlock();
|
||||
m_editor->setTextCursor(cursor);
|
||||
}
|
||||
p_event->accept();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool VMdEditOperations::handleKeyU(QKeyEvent *p_event)
|
||||
{
|
||||
if (p_event->modifiers() == Qt::ControlModifier) {
|
||||
|
@ -36,6 +36,7 @@ private:
|
||||
bool handleKeyD(QKeyEvent *p_event);
|
||||
bool handleKeyH(QKeyEvent *p_event);
|
||||
bool handleKeyI(QKeyEvent *p_event);
|
||||
bool handleKeyO(QKeyEvent *p_event);
|
||||
bool handleKeyU(QKeyEvent *p_event);
|
||||
bool handleKeyW(QKeyEvent *p_event);
|
||||
bool handleKeyEsc(QKeyEvent *p_event);
|
||||
|
Loading…
x
Reference in New Issue
Block a user