Skip to content
Open
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
26 changes: 26 additions & 0 deletions .github/workflows/android.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Android CI

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'temurin'
cache: gradle

- name: Grant execute permission for gradlew
run: chmod +x gradlew
- name: Build with Gradle
run: ./gradlew build
5 changes: 3 additions & 2 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/kotlinc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ android {
targetCompatibility JavaVersion.VERSION_17
}
kotlinOptions {
jvmTarget = '11'
jvmTarget = '17'
}
buildFeatures {
compose true
}
composeOptions {
kotlinCompilerExtensionVersion = "1.5.1"
kotlinCompilerExtensionVersion = "1.5.4"
}
packagingOptions {
resources {
Expand Down
11 changes: 10 additions & 1 deletion app/src/main/java/jp/kaleidot725/sample/ui/composable/Demo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import androidx.compose.ui.platform.LocalClipboardManager
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.platform.LocalSoftwareKeyboardController
import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
Expand All @@ -49,7 +50,15 @@ fun Demo(text: String) {
val clipboardManager = LocalClipboardManager.current
val keyboardController = LocalSoftwareKeyboardController.current

var textEditorState by remember { mutableStateOf(TextEditorState.create(text)) }
var textEditorState by remember {
mutableStateOf(
TextEditorState.create(
text,
TextStyle(color = Color.Black),
TextStyle(color = Color.Green)
)
)
}
val bottomPadding = if (textEditorState.isMultipleSelectionMode) 100.dp else 0.dp
val contentBottomPaddingValue =
with(LocalDensity.current) { WindowInsets.ime.getBottom(this).toDp() }
Expand Down
8 changes: 4 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ buildscript {
core_ktx_version = '1.10.1'
lifecycle_ktx_version = '2.6.1'
activity_compose_version = '1.7.2'
compose_ui_version = '1.5.0'
compose_ui_version = '1.5.4'
kotest_version = '5.6.2'
}
}

plugins {
id 'com.android.application' version '7.4.2' apply false
id 'com.android.library' version '7.4.2' apply false
id 'org.jetbrains.kotlin.android' version '1.9.0' apply false
id 'com.android.application' version '8.1.2' apply false
id 'com.android.library' version '8.1.2' apply false
id 'org.jetbrains.kotlin.android' version '1.9.20' apply false
id 'org.jlleitschuh.gradle.ktlint' version '10.3.0'
}

Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.0-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
2 changes: 1 addition & 1 deletion jitpack.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
jdk:
- openjdk11
- openjdk17
12 changes: 9 additions & 3 deletions texteditor/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ android {
}

kotlinOptions {
jvmTarget = '11'
jvmTarget = '17'
}

android.testOptions {
Expand All @@ -45,7 +45,13 @@ android {
}

composeOptions {
kotlinCompilerExtensionVersion '1.5.1'
kotlinCompilerExtensionVersion '1.5.4'
}
publishing{
singleVariant('release') {
withSourcesJar()
withJavadocJar()
}
}
}

Expand All @@ -64,7 +70,7 @@ afterEvaluate {
from components.release
groupId = 'com.github.kaleidot725'
artifactId = 'text-editor-compose'
version = '0.6.0'
version = '0.6.1'
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.text.TextRange
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.input.TextFieldValue
import jp.kaleidot725.texteditor.state.TextEditorState
import jp.kaleidot725.texteditor.state.TextFieldState
Expand All @@ -12,7 +13,7 @@ import java.util.concurrent.locks.ReentrantLock
import kotlin.concurrent.withLock

internal class EditorController(
textEditorState: TextEditorState
val textEditorState: TextEditorState
) {
private var onChanged: (TextEditorState) -> Unit = {}

Expand All @@ -26,7 +27,7 @@ internal class EditorController(
val selectedIndices get() = _selectedIndices.toList()

private val state: TextEditorState
get() = TextEditorState(fields, selectedIndices, isMultipleSelectionMode)
get() = TextEditorState(fields, selectedIndices, isMultipleSelectionMode, textEditorState.textStyle, textEditorState.textSelectedStyle)

private val lock = ReentrantLock()

Expand Down Expand Up @@ -62,7 +63,7 @@ internal class EditorController(

val newSplitFieldValues = splitFieldValues.subList(1, splitFieldValues.count())
val newSplitFieldStates =
newSplitFieldValues.map { TextFieldState(value = it, isSelected = false) }
newSplitFieldValues.map { TextFieldState(value = it, isSelected = false, textStyle = textEditorState.textStyle, textSelectedStyle = textEditorState.textSelectedStyle) }
_fields.addAll(targetIndex + 1, newSplitFieldStates)

val lastNewSplitFieldIndex = targetIndex + newSplitFieldValues.count()
Expand Down Expand Up @@ -95,7 +96,7 @@ internal class EditorController(
_fields[targetIndex] = firstState

val secondValue = TextFieldValue(second, TextRange.Zero)
val secondState = TextFieldState(value = secondValue, isSelected = false)
val secondState = TextFieldState(value = secondValue, isSelected = false, textStyle = textEditorState.textStyle, textSelectedStyle = textEditorState.textSelectedStyle)
_fields.add(targetIndex + 1, secondState)

selectFieldInternal(targetIndex + 1)
Expand Down Expand Up @@ -214,7 +215,7 @@ internal class EditorController(
fun deleteAllLine() {
lock.withLock {
_fields.clear()
_fields.addAll(emptyList<String>().createInitTextFieldStates())
_fields.addAll(emptyList<String>().createInitTextFieldStates(textEditorState.textStyle, textEditorState.textSelectedStyle))
_selectedIndices.clear()
selectFieldInternal(0)
onChanged(state)
Expand Down Expand Up @@ -302,12 +303,14 @@ internal class EditorController(
}

companion object {
fun List<String>.createInitTextFieldStates(): List<TextFieldState> {
if (this.isEmpty()) return listOf(TextFieldState(isSelected = false))
fun List<String>.createInitTextFieldStates(textStyle: TextStyle, textSelectedStyle: TextStyle): List<TextFieldState> {
if (this.isEmpty()) return listOf(TextFieldState(isSelected = false, textStyle = textStyle, textSelectedStyle = textSelectedStyle))
return this.mapIndexed { _, s ->
TextFieldState(
value = TextFieldValue(s, TextRange.Zero),
isSelected = false
isSelected = false,
textStyle = textStyle,
textSelectedStyle = textSelectedStyle
)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package jp.kaleidot725.texteditor.state

import androidx.compose.runtime.Immutable
import androidx.compose.ui.text.TextStyle
import jp.kaleidot725.texteditor.controller.EditorController.Companion.createInitTextFieldStates

@Immutable
data class TextEditorState(
val fields: List<TextFieldState>,
val selectedIndices: List<Int>,
val isMultipleSelectionMode: Boolean,
val textStyle: TextStyle,
val textSelectedStyle: TextStyle
) {
fun getAllText(): String {
return fields.map { it.value.text }.foldIndexed("") { index, acc, s ->
Expand All @@ -24,19 +27,23 @@ data class TextEditorState(
}

companion object {
fun create(text: String): TextEditorState {
fun create(text: String, textStyle: TextStyle = TextStyle(), textSelectedStyle: TextStyle = TextStyle()): TextEditorState {
return TextEditorState(
fields = text.lines().createInitTextFieldStates(),
fields = text.lines().createInitTextFieldStates(textStyle, textSelectedStyle),
selectedIndices = listOf(-1),
isMultipleSelectionMode = false
isMultipleSelectionMode = false,
textStyle = textStyle,
textSelectedStyle = textSelectedStyle
)
}

fun create(lines: List<String>): TextEditorState {
fun create(lines: List<String>, textStyle: TextStyle = TextStyle(), textSelectedStyle: TextStyle = TextStyle()): TextEditorState {
return TextEditorState(
fields = lines.createInitTextFieldStates(),
fields = lines.createInitTextFieldStates(textStyle, textSelectedStyle),
selectedIndices = listOf(-1),
isMultipleSelectionMode = false
isMultipleSelectionMode = false,
textStyle = textStyle,
textSelectedStyle = textSelectedStyle
)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package jp.kaleidot725.texteditor.state

import androidx.compose.runtime.Stable
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.input.TextFieldValue
import java.util.UUID

@Stable
data class TextFieldState(
val id: String = UUID.randomUUID().toString(),
val value: TextFieldValue = TextFieldValue(),
val isSelected: Boolean
val isSelected: Boolean,
val textStyle: TextStyle,
val textSelectedStyle: TextStyle
)
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateMapOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberUpdatedState
Expand Down Expand Up @@ -47,15 +48,16 @@ fun TextEditor(
contentPaddingValues: PaddingValues = PaddingValues(),
decorationBox: DecorationBoxComposable = { _, _, innerTextField -> innerTextField(Modifier) },
) {
val textEditorState by rememberUpdatedState(newValue = textEditorState)
val textEditorStateM by rememberUpdatedState(newValue = textEditorState)
val editableController by rememberTextEditorController(
textEditorState,
onChanged = { onChanged(it) })
textEditorStateM,
onChanged = { onChanged(it) }
)
var lastScrollEvent by remember { mutableStateOf(null as ScrollEvent?) }
val lazyColumnState = rememberLazyListState()
val focusRequesters by remember { mutableStateOf(mutableMapOf<Int, FocusRequester>()) }
val focusRequesters = remember { mutableStateMapOf<Int, FocusRequester>() }

editableController.syncState(textEditorState)
editableController.syncState(textEditorStateM)

LaunchedEffect(lastScrollEvent) {
lastScrollEvent?.consume()
Expand All @@ -77,7 +79,7 @@ fun TextEditor(
contentPadding = contentPaddingValues
) {
itemsIndexed(
items = textEditorState.fields,
items = textEditorStateM.fields,
key = { _, item -> item.id }
) { index, textFieldState ->
val focusRequester by remember { mutableStateOf(FocusRequester()) }
Expand All @@ -99,13 +101,13 @@ fun TextEditor(
interactionSource = remember { MutableInteractionSource() },
indication = null
) {
if (!textEditorState.isMultipleSelectionMode) return@clickable
if (!textEditorStateM.isMultipleSelectionMode) return@clickable
editableController.selectField(targetIndex = index)
}
) {
TextField(
textFieldState = textFieldState,
enabled = !textEditorState.isMultipleSelectionMode,
enabled = !textEditorStateM.isMultipleSelectionMode,
focusRequester = focusRequester,
onUpdateText = { newText ->
editableController.updateField(
Expand Down Expand Up @@ -145,9 +147,9 @@ fun TextEditor(
onDownFocus = {
if (lastScrollEvent != null && lastScrollEvent?.isConsumed != true) return@TextField
editableController.selectNextField()
if (index != textEditorState.fields.lastIndex) lastScrollEvent =
if (index != textEditorStateM.fields.lastIndex) lastScrollEvent =
ScrollEvent(index + 1)
},
}
)
}
}
Expand Down
Loading