-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor text modifications to UseCases
- Loading branch information
1 parent
930672e
commit 91c9b8c
Showing
3 changed files
with
102 additions
and
43 deletions.
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
app/src/main/java/de/markusressel/mkdocseditor/feature/editor/domain/FormatBoldUseCase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package de.markusressel.mkdocseditor.feature.editor.domain | ||
|
||
import de.markusressel.mkdocseditor.extensions.common.safeSubstring | ||
import javax.inject.Inject | ||
import javax.inject.Singleton | ||
|
||
@Singleton | ||
internal class FormatBoldUseCase @Inject constructor( | ||
) { | ||
operator fun invoke(text: String, selectionStart: Int, selectionEnd: Int): FormatResult { | ||
|
||
// check if the current selection is already bold | ||
val selectionIsBold = run { | ||
val selectedText = | ||
text.safeSubstring((selectionStart - 2), (selectionEnd + 2)) | ||
selectedText.startsWith("**") && selectedText.endsWith("**") | ||
} | ||
|
||
return if (selectionIsBold) { | ||
val newText = text.substring(0, selectionStart - 2) + | ||
text.substring(selectionStart, selectionEnd) + | ||
text.substring(selectionEnd + 2) | ||
|
||
Triple( | ||
newText, | ||
selectionStart - 2, | ||
selectionEnd - 2 | ||
) | ||
} else { | ||
val newText = text.substring(0, selectionStart) + | ||
"**" + | ||
text.substring(selectionStart, selectionEnd) + | ||
"**" + | ||
text.substring(selectionEnd) | ||
|
||
Triple( | ||
newText, | ||
selectionStart + 2, | ||
selectionEnd + 2 | ||
) | ||
} | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
app/src/main/java/de/markusressel/mkdocseditor/feature/editor/domain/FormatItalicUseCase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package de.markusressel.mkdocseditor.feature.editor.domain | ||
|
||
import de.markusressel.mkdocseditor.extensions.common.safeSubstring | ||
import javax.inject.Inject | ||
import javax.inject.Singleton | ||
|
||
@Singleton | ||
internal class FormatItalicUseCase @Inject constructor( | ||
) { | ||
operator fun invoke(text: String, selectionStart: Int, selectionEnd: Int): FormatResult { | ||
// check if the current selection is already italic | ||
val selectionIsItalic = run { | ||
val selectedText = | ||
text.safeSubstring((selectionStart - 1), (selectionEnd + 1)) | ||
selectedText.startsWith("*") && selectedText.endsWith("*") | ||
} | ||
|
||
return if (selectionIsItalic) { | ||
val newText = text.substring(0, selectionStart - 1) + | ||
text.substring(selectionStart, selectionEnd) + | ||
text.substring(selectionEnd + 1) | ||
|
||
Triple( | ||
newText, | ||
selectionStart - 1, | ||
selectionEnd - 1 | ||
) | ||
} else { | ||
val newText = text.substring(0, selectionStart) + | ||
"*" + | ||
text.substring(selectionStart, selectionEnd) + | ||
"*" + | ||
text.substring(selectionEnd) | ||
Triple( | ||
newText, | ||
selectionStart + 1, | ||
selectionEnd + 1 | ||
) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters