-
Notifications
You must be signed in to change notification settings - Fork 3
Add formatting keyboard shortcuts (Ctrl+B bold, Ctrl+I italic, etc.) #29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Wavesonics
wants to merge
1
commit into
main
Choose a base branch
from
feature/formatting-shortcuts
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -11,12 +11,14 @@ import androidx.compose.ui.input.key.type | |||||
| import androidx.compose.ui.input.key.utf16CodePoint | ||||||
| import androidx.compose.ui.platform.Clipboard | ||||||
| import androidx.compose.ui.text.AnnotatedString | ||||||
| import androidx.compose.ui.text.SpanStyle | ||||||
| import androidx.compose.ui.text.buildAnnotatedString | ||||||
| import com.darkrockstudios.texteditor.CharLineOffset | ||||||
| import com.darkrockstudios.texteditor.TextEditorRange | ||||||
| import com.darkrockstudios.texteditor.clipboard.ClipboardHelper | ||||||
| import com.darkrockstudios.texteditor.input.TextEditorKeyCommandHandler.Companion.TAB_SIZE | ||||||
| import com.darkrockstudios.texteditor.state.TextEditorState | ||||||
| import com.darkrockstudios.texteditor.state.getSpanStylesInRange | ||||||
| import com.darkrockstudios.texteditor.state.moveCursorDown | ||||||
| import com.darkrockstudios.texteditor.state.moveCursorPageDown | ||||||
| import com.darkrockstudios.texteditor.state.moveCursorPageUp | ||||||
|
|
@@ -49,7 +51,8 @@ internal class TextEditorKeyCommandHandler { | |||||
| state: TextEditorState, | ||||||
| clipboard: Clipboard, | ||||||
| scope: CoroutineScope, | ||||||
| enabled: Boolean = true | ||||||
| enabled: Boolean = true, | ||||||
| formattingShortcuts: TextFormattingShortcuts = TextFormattingShortcuts.Default | ||||||
| ): Boolean { | ||||||
| if (keyEvent.type != KeyEventType.KeyDown) return false | ||||||
|
|
||||||
|
|
@@ -108,6 +111,32 @@ internal class TextEditorKeyCommandHandler { | |||||
| // Editing operations require enabled=true | ||||||
| !enabled -> false | ||||||
|
|
||||||
| // Formatting shortcuts. Ctrl+Shift+X (strikethrough) is matched before | ||||||
| // the Ctrl+X cut branch so it isn't swallowed as a cut. | ||||||
| keyEvent.isCtrlPressed && keyEvent.isShiftPressed && keyEvent.key == Key.X && | ||||||
| formattingShortcuts.strikethrough != null -> { | ||||||
| toggleStyleSpan(state, formattingShortcuts.strikethrough) | ||||||
| true | ||||||
| } | ||||||
|
|
||||||
| keyEvent.isCtrlPressed && keyEvent.key == Key.B && | ||||||
| formattingShortcuts.bold != null -> { | ||||||
| toggleStyleSpan(state, formattingShortcuts.bold) | ||||||
| true | ||||||
| } | ||||||
|
|
||||||
| keyEvent.isCtrlPressed && keyEvent.key == Key.I && | ||||||
| formattingShortcuts.italic != null -> { | ||||||
| toggleStyleSpan(state, formattingShortcuts.italic) | ||||||
| true | ||||||
| } | ||||||
|
|
||||||
| keyEvent.isCtrlPressed && keyEvent.key == Key.E && | ||||||
| formattingShortcuts.code != null -> { | ||||||
| toggleStyleSpan(state, formattingShortcuts.code) | ||||||
| true | ||||||
| } | ||||||
|
|
||||||
| keyEvent.isCtrlPressed && keyEvent.key == Key.X -> { | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔴 HIGH RISK To prevent accidental 'Cut' operations when strikethrough shortcuts are disabled or when other Shift+X combinations are used, ensure the Cut handler explicitly checks that Shift is not pressed. This might be a simple fix:
Suggested change
|
||||||
| handleCut(state, clipboard, scope) | ||||||
| true | ||||||
|
|
@@ -210,6 +239,19 @@ internal class TextEditorKeyCommandHandler { | |||||
| return true | ||||||
| } | ||||||
|
|
||||||
| private fun toggleStyleSpan(state: TextEditorState, style: SpanStyle) { | ||||||
| val selection = state.selector.selection | ||||||
| if (selection != null) { | ||||||
| if (state.getSpanStylesInRange(selection).contains(style)) { | ||||||
| state.removeStyleSpan(selection, style) | ||||||
| } else { | ||||||
| state.addStyleSpan(selection, style) | ||||||
| } | ||||||
| } else { | ||||||
| state.cursor.toggleStyle(style) | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| private fun handleCopy(state: TextEditorState, clipboard: Clipboard, scope: CoroutineScope) { | ||||||
| state.selector.selection?.let { selection -> | ||||||
| val selectedText = state.selector.getSelectedText() | ||||||
|
|
||||||
39 changes: 39 additions & 0 deletions
39
...tor/src/commonMain/kotlin/com/darkrockstudios/texteditor/input/TextFormattingShortcuts.kt
This file contains hidden or 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,39 @@ | ||
| package com.darkrockstudios.texteditor.input | ||
|
|
||
| import androidx.compose.ui.text.SpanStyle | ||
| import com.darkrockstudios.texteditor.markdown.MarkdownConfiguration | ||
|
|
||
| /** | ||
| * Maps formatting keyboard shortcuts to the [SpanStyle] each one toggles over the | ||
| * selection (or the cursor for subsequent typing). A `null` style leaves that | ||
| * shortcut unbound. | ||
| * | ||
| * The default styles come from [MarkdownConfiguration.DEFAULT] so toggled spans | ||
| * match the markdown toolbar and round-trip through the markdown extension. If you | ||
| * render with a customized [MarkdownConfiguration], pass the matching styles here. | ||
| * | ||
| * Bindings (matching the editor's Ctrl-based shortcut convention): | ||
| * - [bold]: Ctrl+B | ||
| * - [italic]: Ctrl+I | ||
| * - [strikethrough]: Ctrl+Shift+X | ||
| * - [code]: Ctrl+E | ||
| */ | ||
| data class TextFormattingShortcuts( | ||
| val bold: SpanStyle? = MarkdownConfiguration.DEFAULT.boldStyle, | ||
| val italic: SpanStyle? = MarkdownConfiguration.DEFAULT.italicStyle, | ||
| val strikethrough: SpanStyle? = MarkdownConfiguration.DEFAULT.strikethroughStyle, | ||
| val code: SpanStyle? = MarkdownConfiguration.DEFAULT.codeStyle, | ||
| ) { | ||
| companion object { | ||
| /** Bold, italic, strikethrough, and inline code bound to the markdown defaults. */ | ||
| val Default = TextFormattingShortcuts() | ||
|
|
||
| /** Every formatting shortcut unbound. */ | ||
| val None = TextFormattingShortcuts( | ||
| bold = null, | ||
| italic = null, | ||
| strikethrough = null, | ||
| code = null, | ||
| ) | ||
| } | ||
| } |
137 changes: 137 additions & 0 deletions
137
ComposeTextEditor/src/desktopTest/kotlin/input/FormattingShortcutTest.kt
This file contains hidden or 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,137 @@ | ||
| package input | ||
|
|
||
| import androidx.compose.ui.InternalComposeUiApi | ||
| import androidx.compose.ui.input.key.Key | ||
| import androidx.compose.ui.input.key.KeyEvent | ||
| import androidx.compose.ui.input.key.KeyEventType | ||
| import androidx.compose.ui.platform.Clipboard | ||
| import com.darkrockstudios.texteditor.CharLineOffset | ||
| import com.darkrockstudios.texteditor.TextEditorRange | ||
| import com.darkrockstudios.texteditor.input.TextEditorKeyCommandHandler | ||
| import com.darkrockstudios.texteditor.input.TextFormattingShortcuts | ||
| import com.darkrockstudios.texteditor.markdown.MarkdownConfiguration | ||
| import com.darkrockstudios.texteditor.state.TextEditorState | ||
| import io.mockk.mockk | ||
| import kotlinx.coroutines.test.TestScope | ||
| import kotlin.test.BeforeTest | ||
| import kotlin.test.Test | ||
| import kotlin.test.assertEquals | ||
| import kotlin.test.assertFalse | ||
| import kotlin.test.assertTrue | ||
|
|
||
| class FormattingShortcutTest { | ||
|
|
||
| private val bold = MarkdownConfiguration.DEFAULT.boldStyle | ||
| private val italic = MarkdownConfiguration.DEFAULT.italicStyle | ||
| private val strikethrough = MarkdownConfiguration.DEFAULT.strikethroughStyle | ||
| private val code = MarkdownConfiguration.DEFAULT.codeStyle | ||
|
|
||
| private val handler = TextEditorKeyCommandHandler() | ||
| private val clipboard = mockk<Clipboard>(relaxed = true) | ||
| private val scope = TestScope() | ||
|
|
||
| private lateinit var state: TextEditorState | ||
|
|
||
| @BeforeTest | ||
| fun setup() { | ||
| state = TextEditorState( | ||
| scope = TestScope(), | ||
| measurer = mockk(relaxed = true), | ||
| initialText = null, | ||
| ) | ||
| state.setText("Hello World") | ||
| } | ||
|
|
||
| @OptIn(InternalComposeUiApi::class) | ||
| private fun keyEvent(key: Key, ctrl: Boolean = true, shift: Boolean = false): KeyEvent = | ||
| KeyEvent( | ||
| key = key, | ||
| type = KeyEventType.KeyDown, | ||
| isCtrlPressed = ctrl, | ||
| isShiftPressed = shift, | ||
| ) | ||
|
|
||
| private fun selectWorld() { | ||
| state.selector.updateSelection(CharLineOffset(0, 6), CharLineOffset(0, 11)) | ||
| } | ||
|
|
||
| private fun dispatch(key: Key, shift: Boolean = false, enabled: Boolean = true): Boolean = | ||
| handler.handleKeyEvent(keyEvent(key, shift = shift), state, clipboard, scope, enabled) | ||
|
|
||
| @Test | ||
| fun `Ctrl+B applies bold to the selection`() { | ||
| selectWorld() | ||
| val consumed = dispatch(Key.B) | ||
|
|
||
| assertTrue(consumed) | ||
| val spans = state.textLines[0].spanStyles | ||
| assertEquals(1, spans.size) | ||
| assertEquals(bold, spans.first().item) | ||
| assertEquals(6, spans.first().start) | ||
| assertEquals(11, spans.first().end) | ||
| } | ||
|
|
||
| @Test | ||
| fun `Ctrl+B toggles bold off when already applied`() { | ||
| selectWorld() | ||
| dispatch(Key.B) | ||
| selectWorld() | ||
| dispatch(Key.B) | ||
|
|
||
| assertTrue(state.textLines[0].spanStyles.none { it.item == bold }) | ||
| } | ||
|
|
||
| @Test | ||
| fun `Ctrl+I applies italic to the selection`() { | ||
| selectWorld() | ||
| assertTrue(dispatch(Key.I)) | ||
| assertTrue(state.textLines[0].spanStyles.any { it.item == italic }) | ||
| } | ||
|
|
||
| @Test | ||
| fun `Ctrl+E applies inline code to the selection`() { | ||
| selectWorld() | ||
| assertTrue(dispatch(Key.E)) | ||
| assertTrue(state.textLines[0].spanStyles.any { it.item == code }) | ||
| } | ||
|
|
||
| @Test | ||
| fun `Ctrl+Shift+X applies strikethrough and does not cut`() { | ||
| selectWorld() | ||
| assertTrue(dispatch(Key.X, shift = true)) | ||
| assertEquals("Hello World", state.textLines[0].text) | ||
| assertTrue(state.textLines[0].spanStyles.any { it.item == strikethrough }) | ||
| } | ||
|
|
||
| @Test | ||
| fun `Ctrl+B with no selection sets a pending cursor style`() { | ||
| assertTrue(dispatch(Key.B)) | ||
| assertTrue(state.cursor.styles.contains(bold)) | ||
| assertTrue(state.textLines[0].spanStyles.isEmpty()) | ||
| } | ||
|
|
||
| @Test | ||
| fun `formatting shortcuts do nothing when disabled`() { | ||
| selectWorld() | ||
| val consumed = handler.handleKeyEvent(keyEvent(Key.B), state, clipboard, scope, enabled = false) | ||
|
|
||
| assertFalse(consumed) | ||
| assertTrue(state.textLines[0].spanStyles.isEmpty()) | ||
| } | ||
|
|
||
| @Test | ||
| fun `unbound shortcut is ignored`() { | ||
| selectWorld() | ||
| val consumed = handler.handleKeyEvent( | ||
| keyEvent(Key.B), | ||
| state, | ||
| clipboard, | ||
| scope, | ||
| enabled = true, | ||
| formattingShortcuts = TextFormattingShortcuts.None, | ||
| ) | ||
|
|
||
| assertFalse(consumed) | ||
| assertTrue(state.textLines[0].spanStyles.isEmpty()) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
⚪ LOW RISK
Suggestion: Restrict these shortcuts to only trigger when Shift is not pressed.