remove obsolete title mark when inserting title mark

This commit is contained in:
Le Tan 2017-10-13 19:40:35 +08:00
parent d5e3d47c27
commit ea05c268b3
5 changed files with 84 additions and 32 deletions

View File

@ -689,3 +689,49 @@ bool VEditUtils::needToCancelAutoIndent(int p_autoIndentPos, const QTextCursor &
return false;
}
void VEditUtils::insertTitleMark(QTextCursor &p_cursor,
const QTextBlock &p_block,
int p_level)
{
if (!p_block.isValid()) {
return;
}
Q_ASSERT(p_level >= 1 && p_level <= 6);
bool needInsert = true;
p_cursor.setPosition(p_block.position());
// Test if this block contains title marks.
QRegExp headerReg(VUtils::c_headerRegExp);
QString text = p_block.text();
bool matched = headerReg.exactMatch(text);
if (matched) {
int level = headerReg.cap(1).length();
if (level == p_level) {
needInsert = false;
} else {
// Remove the title mark.
p_cursor.movePosition(QTextCursor::NextCharacter,
QTextCursor::KeepAnchor,
level);
p_cursor.removeSelectedText();
}
}
// Insert titleMark + " " at the front of the block.
if (needInsert) {
// Remove the spaces at front.
// insertText() will remove the selection.
moveCursorFirstNonSpaceCharacter(p_cursor, QTextCursor::KeepAnchor);
// Insert.
const QString titleMark(p_level, '#');
p_cursor.insertText(titleMark + " ");
}
// Go to the end of this block.
p_cursor.movePosition(QTextCursor::EndOfBlock);
}

View File

@ -132,7 +132,15 @@ public:
// Check if we need to cancel auto indent.
// @p_autoIndentPos: the position of the cursor after auto indent.
static bool needToCancelAutoIndent(int p_autoIndentPos, const QTextCursor &p_cursor);
static bool needToCancelAutoIndent(int p_autoIndentPos,
const QTextCursor &p_cursor);
// Insert title Mark at level @p_level in front of block @p_block
// If there already exists title marks, remove it first.
// Move cursor at the end of the block after insertion.
static void insertTitleMark(QTextCursor &p_cursor,
const QTextBlock &p_block,
int p_level);
private:
VEditUtils() {}

View File

@ -626,33 +626,25 @@ void VMdEditOperations::changeListBlockSeqNumber(QTextBlock &p_block, int p_seq)
bool VMdEditOperations::insertTitle(int p_level)
{
Q_ASSERT(p_level > 0 && p_level < 7);
QTextDocument *doc = m_editor->document();
QString titleMark(p_level, '#');
QTextCursor cursor = m_editor->textCursor();
int firstBlock = cursor.block().blockNumber();
int lastBlock = firstBlock;
if (cursor.hasSelection()) {
// Insert title # in front of the selected lines.
// Insert title # in front of the selected blocks.
int start = cursor.selectionStart();
int end = cursor.selectionEnd();
int startBlock = doc->findBlock(start).blockNumber();
int endBlock = doc->findBlock(end).blockNumber();
cursor.beginEditBlock();
cursor.clearSelection();
for (int i = startBlock; i <= endBlock; ++i) {
QTextBlock block = doc->findBlockByNumber(i);
cursor.setPosition(block.position(), QTextCursor::MoveAnchor);
cursor.insertText(titleMark + " ");
firstBlock = doc->findBlock(start).blockNumber();
lastBlock = doc->findBlock(end).blockNumber();
}
cursor.movePosition(QTextCursor::EndOfBlock);
cursor.endEditBlock();
} else {
// Insert title # in front of current block.
cursor.beginEditBlock();
cursor.movePosition(QTextCursor::StartOfBlock);
cursor.insertText(titleMark + " ");
cursor.movePosition(QTextCursor::EndOfBlock);
cursor.endEditBlock();
for (int i = firstBlock; i <= lastBlock; ++i) {
VEditUtils::insertTitleMark(cursor, doc->findBlockByNumber(i), p_level);
}
cursor.endEditBlock();
m_editor->setTextCursor(cursor);
return true;
}

View File

@ -47,6 +47,10 @@ private:
bool handleKeyEsc(QKeyEvent *p_event);
bool handleKeyReturn(QKeyEvent *p_event);
bool handleKeyBracketLeft(QKeyEvent *p_event);
// Insert title of level @p_level.
// Will detect if current block already has some leading #s. If yes,
// will delete it and insert the correct #s.
bool insertTitle(int p_level);
// Change the sequence number of a list block.

View File

@ -286,7 +286,7 @@ bool VMdTab::saveFile()
return true;
}
bool ret;
bool ret = true;
// Make sure the file already exists. Temporary deal with cases when user delete or move
// a file.
QString filePath = m_file->fetchPath();
@ -296,9 +296,8 @@ bool VMdTab::saveFile()
tr("File <span style=\"%1\">%2</span> being written has been removed.")
.arg(g_config->c_dataTextStyle).arg(filePath),
QMessageBox::Ok, QMessageBox::Ok, this);
return false;
}
ret = false;
} else {
m_editor->saveFile();
ret = m_file->save();
if (!ret) {
@ -307,6 +306,9 @@ bool VMdTab::saveFile()
QMessageBox::Ok, QMessageBox::Ok, this);
m_editor->setModified(true);
}
}
updateStatus();
return ret;
}