Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,8 @@ fun DesktopNoteEditorScreen(
aiFaq = noteEditorViewModel::aiFaq,
aiTags = noteEditorViewModel::aiTags,
selectModel = noteEditorViewModel::selectModel,
changeSideMenuTab = noteEditorViewModel::changeSideMenu
changeSideMenuTab = noteEditorViewModel::changeSideMenu,
titleClick = noteEditorViewModel::titleClick
)

if (showDeleteConfirmation) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
Expand All @@ -57,6 +58,7 @@ import io.writeopia.editor.features.editor.viewmodel.SideMenuTab
import io.writeopia.model.Font
import io.writeopia.resources.WrStrings
import io.writeopia.sdk.models.span.Span
import io.writeopia.sdk.models.story.Tag
import io.writeopia.theme.WriteopiaTheme
import io.writeopia.ui.icons.WrSdkIcons
import io.writeopia.ui.model.SelectionMetadata
Expand Down Expand Up @@ -97,7 +99,8 @@ fun SideEditorOptions(
deleteDocument: () -> Unit,
toggleFavorite: () -> Unit,
selectModel: (String) -> Unit,
changeSideMenuTab: (SideMenuTab) -> Unit
changeSideMenuTab: (SideMenuTab) -> Unit,
titleClick: (Tag) -> Unit
) {
val menuType by sideMenuTabState.collectAsState()

Expand Down Expand Up @@ -158,7 +161,8 @@ fun SideEditorOptions(
codeBlockClick,
highLightBlockClick,
addImage,
addPage
addPage,
titleClick
)
}

Expand Down Expand Up @@ -372,6 +376,57 @@ private fun Title(text: String) {
)
}

@Composable
private fun TitleChanges(
metadata: Set<SelectionMetadata>,
modifier: Modifier = Modifier,
titleClick: (Tag) -> Unit
) {
Row(
modifier = modifier,
verticalAlignment = Alignment.CenterVertically
) {
TextButton(
modifier = Modifier.weight(1F),
text = "Title",
highlight = metadata.contains(
SelectionMetadata.TITLE
),
textStyle = MaterialTheme.typography.bodySmall,
fontWeight = FontWeight.Normal,
onClick = {
titleClick(Tag.H1)
}
)

TextButton(
modifier = Modifier.weight(1F),
text = "Subtitle",
highlight = metadata.contains(
SelectionMetadata.SUBTITLE
),
textStyle = MaterialTheme.typography.bodySmall,
fontWeight = FontWeight.Normal,
onClick = {
titleClick(Tag.H2)
}
)

TextButton(
modifier = Modifier.weight(1F),
text = "Heading",
highlight = metadata.contains(
SelectionMetadata.HEADING
),
textStyle = MaterialTheme.typography.bodySmall,
fontWeight = FontWeight.Normal,
onClick = {
titleClick(Tag.H3)
}
)
}
}

@Composable
private fun TextChanges(spanClick: (Span) -> Unit) {
Row(
Expand Down Expand Up @@ -512,6 +567,8 @@ private fun TextButton(
horizontal = 8.dp,
vertical = 8.dp
),
textStyle: TextStyle = buttonsTextStyle(),
fontWeight: FontWeight = FontWeight.Bold,
onClick: () -> Unit,
) {
val shape = MaterialTheme.shapes.medium
Expand Down Expand Up @@ -541,8 +598,8 @@ private fun TextButton(
.padding(paddingValues),
text = text,
color = MaterialTheme.colorScheme.onBackground,
style = buttonsTextStyle(),
fontWeight = FontWeight.Bold,
style = textStyle,
fontWeight = fontWeight,
textAlign = TextAlign.Center
)
}
Expand Down Expand Up @@ -705,6 +762,7 @@ private fun TextOptions(
highLightBlockClick: () -> Unit,
addImage: (String) -> Unit,
addPage: () -> Unit,
titleClick: (Tag) -> Unit,
modifier: Modifier = Modifier,
) {
Column(
Expand All @@ -716,6 +774,13 @@ private fun TextOptions(
.width(250.dp)
.padding(start = 12.dp, end = 12.dp, top = 8.dp, bottom = 12.dp)
) {
val selectedMetadata by selectedMetadataState.collectAsState()

Title("Title")
Spacer(modifier = Modifier.height(4.dp))
TitleChanges(metadata = selectedMetadata, titleClick = titleClick)
Spacer(modifier = Modifier.height(8.dp))

Title(WrStrings.text())
Spacer(modifier = Modifier.height(4.dp))
TextChanges(spanClick)
Expand All @@ -734,8 +799,6 @@ private fun TextOptions(
Title(WrStrings.decoration())
Spacer(modifier = Modifier.height(4.dp))

val selectedMetadata by selectedMetadataState.collectAsState()

DecorationCommands(
commands = listOf(
DecorationButton(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import io.writeopia.sdk.models.id.GenerateId
import io.writeopia.sdk.models.span.Span
import io.writeopia.sdk.models.span.SpanInfo
import io.writeopia.sdk.models.story.StoryTypes
import io.writeopia.sdk.models.story.Tag
import io.writeopia.sdk.models.utils.ResultData
import io.writeopia.sdk.persistence.core.tracker.OnUpdateDocumentTracker
import io.writeopia.sdk.repository.DocumentRepository
Expand Down Expand Up @@ -724,6 +725,12 @@ class NoteEditorKmpViewModel(
_searchText.value = query
}

override fun titleClick(tag: Tag) {
viewModelScope.launch(Dispatchers.Default) {
writeopiaManager.addTitle(tag)
}
}

private fun documentPrompt(promptFn: (String, String, String) -> Flow<ResultData<String>>) {
if (ollamaRepository == null) return

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import io.writeopia.editor.model.EditState
import io.writeopia.model.Font
import io.writeopia.sdk.models.files.ExternalFile
import io.writeopia.sdk.models.span.Span
import io.writeopia.sdk.models.story.Tag
import io.writeopia.ui.backstack.BackstackHandler
import io.writeopia.ui.backstack.BackstackInform
import io.writeopia.ui.manager.WriteopiaStateManager
Expand Down Expand Up @@ -142,6 +143,8 @@ interface NoteEditorViewModel : BackstackInform, BackstackHandler {
fun setTheme(isDarkTheme: Boolean)

fun selectModel(model: String)

fun titleClick(tag: Tag)
}

data class ShareDocument(val content: String, val title: String, val type: String)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,19 @@ class WriteopiaStateManager(
Tag.HIGH_LIGHT_BLOCK -> {
result.add(SelectionMetadata.BOX)
}

Tag.H1 -> {
result.add(SelectionMetadata.TITLE)
}

Tag.H2 -> {
result.add(SelectionMetadata.SUBTITLE)
}

Tag.H3 -> {
result.add(SelectionMetadata.HEADING)
}

else -> {}
}
}
Expand Down Expand Up @@ -1127,6 +1140,26 @@ class WriteopiaStateManager(
}
}

fun addTitle(tag: Tag) {
val position = currentPosition()

val currentStory = getStory(position)
if (currentStory == null) return

val shouldRemove = currentStory.tags.any { it.tag == tag }

val newTags = currentStory.tags
.filterNotTo(mutableSetOf()) { it.tag.isTitle() }
.apply {
if (!shouldRemove) {
add(TagInfo(tag))
}
}

val newStory = currentStory.copy(tags = newTags)
changeStoryState(Action.StoryStateChange(newStory, position))
}

private suspend fun getUserId(): String =
userRepository?.getUser()?.id ?: WriteopiaUser.disconnectedUser().id

Expand Down Expand Up @@ -1292,8 +1325,8 @@ class WriteopiaStateManager(

_currentStory.value = state.copy(
selection = Selection(
stateChange.selectionStart ?: 0,
stateChange.selectionEnd ?: 0,
stateChange.selectionStart ?: state.selection.start,
stateChange.selectionEnd ?: state.selection.end,
stateChange.position
)
)
Expand All @@ -1304,9 +1337,10 @@ class WriteopiaStateManager(

private fun getStories() = _currentStory.value.stories

private fun currentPosition() = _currentStory.value.focus
private fun currentPosition() =
_currentStory.value.focus ?: _currentStory.value.selection.position

private fun getCurrentStory(): StoryStep? = currentPosition()?.let(::getStory)
private fun getCurrentStory(): StoryStep? = currentPosition().let(::getStory)

private fun selectAll() {
_positionsOnEdit.value = getStories().keys - setOf(0)
Expand Down