Skip to content

Commit cf6a4e4

Browse files
leandroBorgesFerreiraLeandro Ferreira
andauthored
Adding title buttons (#506)
Co-authored-by: Leandro Ferreira <[email protected]>
1 parent 8411c74 commit cf6a4e4

File tree

5 files changed

+119
-11
lines changed

5 files changed

+119
-11
lines changed

application/features/editor/src/commonMain/kotlin/io/writeopia/editor/features/editor/ui/desktop/DesktopNoteEditorScreen.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,8 @@ fun DesktopNoteEditorScreen(
183183
aiFaq = noteEditorViewModel::aiFaq,
184184
aiTags = noteEditorViewModel::aiTags,
185185
selectModel = noteEditorViewModel::selectModel,
186-
changeSideMenuTab = noteEditorViewModel::changeSideMenu
186+
changeSideMenuTab = noteEditorViewModel::changeSideMenu,
187+
titleClick = noteEditorViewModel::titleClick
187188
)
188189

189190
if (showDeleteConfirmation) {

application/features/editor/src/commonMain/kotlin/io/writeopia/editor/features/editor/ui/desktop/edit/menu/SideEditorOptions.kt

Lines changed: 69 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import androidx.compose.ui.Modifier
4141
import androidx.compose.ui.draw.clip
4242
import androidx.compose.ui.graphics.Color
4343
import androidx.compose.ui.graphics.vector.ImageVector
44+
import androidx.compose.ui.text.TextStyle
4445
import androidx.compose.ui.text.font.FontFamily
4546
import androidx.compose.ui.text.font.FontWeight
4647
import androidx.compose.ui.text.style.TextAlign
@@ -57,6 +58,7 @@ import io.writeopia.editor.features.editor.viewmodel.SideMenuTab
5758
import io.writeopia.model.Font
5859
import io.writeopia.resources.WrStrings
5960
import io.writeopia.sdk.models.span.Span
61+
import io.writeopia.sdk.models.story.Tag
6062
import io.writeopia.theme.WriteopiaTheme
6163
import io.writeopia.ui.icons.WrSdkIcons
6264
import io.writeopia.ui.model.SelectionMetadata
@@ -97,7 +99,8 @@ fun SideEditorOptions(
9799
deleteDocument: () -> Unit,
98100
toggleFavorite: () -> Unit,
99101
selectModel: (String) -> Unit,
100-
changeSideMenuTab: (SideMenuTab) -> Unit
102+
changeSideMenuTab: (SideMenuTab) -> Unit,
103+
titleClick: (Tag) -> Unit
101104
) {
102105
val menuType by sideMenuTabState.collectAsState()
103106

@@ -158,7 +161,8 @@ fun SideEditorOptions(
158161
codeBlockClick,
159162
highLightBlockClick,
160163
addImage,
161-
addPage
164+
addPage,
165+
titleClick
162166
)
163167
}
164168

@@ -372,6 +376,57 @@ private fun Title(text: String) {
372376
)
373377
}
374378

379+
@Composable
380+
private fun TitleChanges(
381+
metadata: Set<SelectionMetadata>,
382+
modifier: Modifier = Modifier,
383+
titleClick: (Tag) -> Unit
384+
) {
385+
Row(
386+
modifier = modifier,
387+
verticalAlignment = Alignment.CenterVertically
388+
) {
389+
TextButton(
390+
modifier = Modifier.weight(1F),
391+
text = "Title",
392+
highlight = metadata.contains(
393+
SelectionMetadata.TITLE
394+
),
395+
textStyle = MaterialTheme.typography.bodySmall,
396+
fontWeight = FontWeight.Normal,
397+
onClick = {
398+
titleClick(Tag.H1)
399+
}
400+
)
401+
402+
TextButton(
403+
modifier = Modifier.weight(1F),
404+
text = "Subtitle",
405+
highlight = metadata.contains(
406+
SelectionMetadata.SUBTITLE
407+
),
408+
textStyle = MaterialTheme.typography.bodySmall,
409+
fontWeight = FontWeight.Normal,
410+
onClick = {
411+
titleClick(Tag.H2)
412+
}
413+
)
414+
415+
TextButton(
416+
modifier = Modifier.weight(1F),
417+
text = "Heading",
418+
highlight = metadata.contains(
419+
SelectionMetadata.HEADING
420+
),
421+
textStyle = MaterialTheme.typography.bodySmall,
422+
fontWeight = FontWeight.Normal,
423+
onClick = {
424+
titleClick(Tag.H3)
425+
}
426+
)
427+
}
428+
}
429+
375430
@Composable
376431
private fun TextChanges(spanClick: (Span) -> Unit) {
377432
Row(
@@ -512,6 +567,8 @@ private fun TextButton(
512567
horizontal = 8.dp,
513568
vertical = 8.dp
514569
),
570+
textStyle: TextStyle = buttonsTextStyle(),
571+
fontWeight: FontWeight = FontWeight.Bold,
515572
onClick: () -> Unit,
516573
) {
517574
val shape = MaterialTheme.shapes.medium
@@ -541,8 +598,8 @@ private fun TextButton(
541598
.padding(paddingValues),
542599
text = text,
543600
color = MaterialTheme.colorScheme.onBackground,
544-
style = buttonsTextStyle(),
545-
fontWeight = FontWeight.Bold,
601+
style = textStyle,
602+
fontWeight = fontWeight,
546603
textAlign = TextAlign.Center
547604
)
548605
}
@@ -705,6 +762,7 @@ private fun TextOptions(
705762
highLightBlockClick: () -> Unit,
706763
addImage: (String) -> Unit,
707764
addPage: () -> Unit,
765+
titleClick: (Tag) -> Unit,
708766
modifier: Modifier = Modifier,
709767
) {
710768
Column(
@@ -716,6 +774,13 @@ private fun TextOptions(
716774
.width(250.dp)
717775
.padding(start = 12.dp, end = 12.dp, top = 8.dp, bottom = 12.dp)
718776
) {
777+
val selectedMetadata by selectedMetadataState.collectAsState()
778+
779+
Title("Title")
780+
Spacer(modifier = Modifier.height(4.dp))
781+
TitleChanges(metadata = selectedMetadata, titleClick = titleClick)
782+
Spacer(modifier = Modifier.height(8.dp))
783+
719784
Title(WrStrings.text())
720785
Spacer(modifier = Modifier.height(4.dp))
721786
TextChanges(spanClick)
@@ -734,8 +799,6 @@ private fun TextOptions(
734799
Title(WrStrings.decoration())
735800
Spacer(modifier = Modifier.height(4.dp))
736801

737-
val selectedMetadata by selectedMetadataState.collectAsState()
738-
739802
DecorationCommands(
740803
commands = listOf(
741804
DecorationButton(

application/features/editor/src/commonMain/kotlin/io/writeopia/editor/features/editor/viewmodel/NoteEditorKmpViewModel.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import io.writeopia.sdk.models.id.GenerateId
3131
import io.writeopia.sdk.models.span.Span
3232
import io.writeopia.sdk.models.span.SpanInfo
3333
import io.writeopia.sdk.models.story.StoryTypes
34+
import io.writeopia.sdk.models.story.Tag
3435
import io.writeopia.sdk.models.utils.ResultData
3536
import io.writeopia.sdk.persistence.core.tracker.OnUpdateDocumentTracker
3637
import io.writeopia.sdk.repository.DocumentRepository
@@ -724,6 +725,12 @@ class NoteEditorKmpViewModel(
724725
_searchText.value = query
725726
}
726727

728+
override fun titleClick(tag: Tag) {
729+
viewModelScope.launch(Dispatchers.Default) {
730+
writeopiaManager.addTitle(tag)
731+
}
732+
}
733+
727734
private fun documentPrompt(promptFn: (String, String, String) -> Flow<ResultData<String>>) {
728735
if (ollamaRepository == null) return
729736

application/features/editor/src/commonMain/kotlin/io/writeopia/editor/features/editor/viewmodel/NoteEditorViewModel.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import io.writeopia.editor.model.EditState
55
import io.writeopia.model.Font
66
import io.writeopia.sdk.models.files.ExternalFile
77
import io.writeopia.sdk.models.span.Span
8+
import io.writeopia.sdk.models.story.Tag
89
import io.writeopia.ui.backstack.BackstackHandler
910
import io.writeopia.ui.backstack.BackstackInform
1011
import io.writeopia.ui.manager.WriteopiaStateManager
@@ -142,6 +143,8 @@ interface NoteEditorViewModel : BackstackInform, BackstackHandler {
142143
fun setTheme(isDarkTheme: Boolean)
143144

144145
fun selectModel(model: String)
146+
147+
fun titleClick(tag: Tag)
145148
}
146149

147150
data class ShareDocument(val content: String, val title: String, val type: String)

writeopia_ui/src/commonMain/kotlin/io/writeopia/ui/manager/WriteopiaStateManager.kt

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,19 @@ class WriteopiaStateManager(
268268
Tag.HIGH_LIGHT_BLOCK -> {
269269
result.add(SelectionMetadata.BOX)
270270
}
271+
272+
Tag.H1 -> {
273+
result.add(SelectionMetadata.TITLE)
274+
}
275+
276+
Tag.H2 -> {
277+
result.add(SelectionMetadata.SUBTITLE)
278+
}
279+
280+
Tag.H3 -> {
281+
result.add(SelectionMetadata.HEADING)
282+
}
283+
271284
else -> {}
272285
}
273286
}
@@ -1127,6 +1140,26 @@ class WriteopiaStateManager(
11271140
}
11281141
}
11291142

1143+
fun addTitle(tag: Tag) {
1144+
val position = currentPosition()
1145+
1146+
val currentStory = getStory(position)
1147+
if (currentStory == null) return
1148+
1149+
val shouldRemove = currentStory.tags.any { it.tag == tag }
1150+
1151+
val newTags = currentStory.tags
1152+
.filterNotTo(mutableSetOf()) { it.tag.isTitle() }
1153+
.apply {
1154+
if (!shouldRemove) {
1155+
add(TagInfo(tag))
1156+
}
1157+
}
1158+
1159+
val newStory = currentStory.copy(tags = newTags)
1160+
changeStoryState(Action.StoryStateChange(newStory, position))
1161+
}
1162+
11301163
private suspend fun getUserId(): String =
11311164
userRepository?.getUser()?.id ?: WriteopiaUser.disconnectedUser().id
11321165

@@ -1292,8 +1325,8 @@ class WriteopiaStateManager(
12921325

12931326
_currentStory.value = state.copy(
12941327
selection = Selection(
1295-
stateChange.selectionStart ?: 0,
1296-
stateChange.selectionEnd ?: 0,
1328+
stateChange.selectionStart ?: state.selection.start,
1329+
stateChange.selectionEnd ?: state.selection.end,
12971330
stateChange.position
12981331
)
12991332
)
@@ -1304,9 +1337,10 @@ class WriteopiaStateManager(
13041337

13051338
private fun getStories() = _currentStory.value.stories
13061339

1307-
private fun currentPosition() = _currentStory.value.focus
1340+
private fun currentPosition() =
1341+
_currentStory.value.focus ?: _currentStory.value.selection.position
13081342

1309-
private fun getCurrentStory(): StoryStep? = currentPosition()?.let(::getStory)
1343+
private fun getCurrentStory(): StoryStep? = currentPosition().let(::getStory)
13101344

13111345
private fun selectAll() {
13121346
_positionsOnEdit.value = getStories().keys - setOf(0)

0 commit comments

Comments
 (0)