Skip to content

Commit cdc193e

Browse files
authored
Merge pull request #2 from kaleidot725/release/0.2.0
Merge 0.2.0
2 parents 64ae8aa + 93854f0 commit cdc193e

File tree

15 files changed

+440
-77
lines changed

15 files changed

+440
-77
lines changed

.idea/deploymentTargetDropDown.xml

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 118 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
- [x] Insert and delete newline
2222
- [x] Get selected line index
2323
- [x] Display line number
24-
- [ ] Select mutilple line
24+
- [x] Copy multiple line
25+
- [x] Delete multiple line
2526
- [ ] Support physical keyboard
2627

2728
## Usage
@@ -43,7 +44,7 @@ allprojects {
4344

4445
```groovy
4546
dependencies {
46-
implementation 'com.github.kaleidot725:text-editor-compose:0.1.0'
47+
implementation 'com.github.kaleidot725:text-editor-compose:0.2.0'
4748
}
4849
```
4950

@@ -67,7 +68,7 @@ class MainActivity : ComponentActivity() {
6768
}
6869
```
6970

70-
### Step 4: Customize what each row displays
71+
### Step 4: Customize row
7172

7273
```kotlin
7374
class MainActivity : ComponentActivity() {
@@ -81,8 +82,8 @@ class MainActivity : ComponentActivity() {
8182
onUpdatedState = { },
8283
modifier = Modifier.fillMaxSize(),
8384
decorationBox = { index, isSelected, innerTextField ->
84-
val backgroundColor = if (isSelected) Color(0x8000ff00) else Color.White
85-
               Row(modifier = Modifier.background(backgroundColor)) {
85+
val backgroundColor = if (isSelected) Color(0x8000ff00) else Color.White
86+
Row(modifier = Modifier.background(backgroundColor)) {
8687
Text(text = (index + 1).toString().padStart(3, '0'))
8788
Spacer(modifier = Modifier.width(4.dp))
8889
innerTextField(modifier = Modifier.fillMaxWidth())
@@ -94,3 +95,115 @@ class MainActivity : ComponentActivity() {
9495
}
9596
}
9697
```
98+
99+
### Step 5: Create multiple selection menu
100+
101+
```kotlin
102+
class MainActivity : ComponentActivity() {
103+
override fun onCreate(savedInstanceState: Bundle?) {
104+
super.onCreate(savedInstanceState)
105+
setContent {
106+
SampleTheme {
107+
val textEditorState by rememberTextEditorState(lines = DemoText.lines())
108+
TextEditor(
109+
textEditorState = textEditorState,
110+
onUpdatedState = { },
111+
modifier = Modifier.fillMaxSize(),
112+
decorationBox = { index, isSelected, innerTextField ->
113+
val backgroundColor = if (isSelected) Color(0x8000ff00) else Color.White
114+
Row(modifier = Modifier.background(backgroundColor)) {
115+
Text(text = (index + 1).toString().padStart(3, '0'))
116+
Spacer(modifier = Modifier.width(4.dp))
117+
innerTextField(modifier = Modifier.fillMaxWidth())
118+
}
119+
}
120+
)
121+
}
122+
}
123+
}
124+
}
125+
126+
@Composable
127+
private fun ColumnScope.TextEditorMenu(textEditorState: TextEditorState) {
128+
val context: Context = LocalContext.current
129+
val clipboardManager: ClipboardManager = LocalClipboardManager.current
130+
131+
Row(modifier = Modifier.padding(8.dp)) {
132+
Text(
133+
text = "Enable multiple selection mode",
134+
modifier = Modifier
135+
.weight(0.9f, true)
136+
.align(Alignment.CenterVertically)
137+
)
138+
Switch(
139+
checked = textEditorState.isMultipleSelectionMode.value,
140+
onCheckedChange = {
141+
textEditorState.enableMultipleSelectionMode(
142+
!textEditorState.isMultipleSelectionMode.value
143+
)
144+
}
145+
)
146+
}
147+
148+
Row(modifier = Modifier.padding(8.dp)) {
149+
Text(
150+
text = "Copy selected lines",
151+
modifier = Modifier
152+
.weight(0.9f, true)
153+
.align(Alignment.CenterVertically)
154+
)
155+
Button(
156+
onClick = {
157+
val text = textEditorState.getSelectedText()
158+
textEditorState.enableMultipleSelectionMode(false)
159+
160+
clipboardManager.setText(AnnotatedString(text))
161+
Toast.makeText(context, "Copy selected text to clipboard", Toast.LENGTH_SHORT).show()
162+
}
163+
) {
164+
Text(text = "EXECUTE")
165+
}
166+
}
167+
168+
Row(modifier = Modifier.padding(8.dp)) {
169+
Text(
170+
text = "Delete selected lines",
171+
modifier = Modifier
172+
.weight(0.9f, true)
173+
.align(Alignment.CenterVertically)
174+
)
175+
Button(onClick = {
176+
textEditorState.deleteSelectedLines()
177+
textEditorState.enableMultipleSelectionMode(false)
178+
}) {
179+
Text(text = "EXECUTE")
180+
}
181+
}
182+
}
183+
```
184+
185+
## Demo
186+
187+
### Edit multiple line text
188+
189+
![Edit multiple line text](./docs/1.gif)
190+
191+
### Insert and delete newline
192+
193+
![Insert and delete newline](./docs/2.gif)
194+
195+
### Get selected line index
196+
197+
![Get selected line index(./docs/3.gif)
198+
199+
### Display line number
200+
201+
![Display line number](./docs/4.gif)
202+
203+
### Copy multiple line
204+
205+
![Copy multiple line](./docs/5.gif)
206+
207+
### Delete multiple line
208+
209+
![Delete multiple line](./docs/6.gif)

app/src/main/java/jp/kaleidot725/sample/MainActivity.kt

Lines changed: 83 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,34 @@
11
package jp.kaleidot725.sample
22

3+
import android.content.Context
34
import android.os.Bundle
5+
import android.widget.Toast
46
import androidx.activity.ComponentActivity
57
import androidx.activity.compose.setContent
68
import androidx.compose.foundation.background
9+
import androidx.compose.foundation.layout.Column
10+
import androidx.compose.foundation.layout.ColumnScope
711
import androidx.compose.foundation.layout.Row
812
import androidx.compose.foundation.layout.Spacer
913
import androidx.compose.foundation.layout.fillMaxSize
10-
import androidx.compose.foundation.layout.fillMaxWidth
14+
import androidx.compose.foundation.layout.padding
1115
import androidx.compose.foundation.layout.width
12-
import androidx.compose.material.Scaffold
16+
import androidx.compose.material.Button
17+
import androidx.compose.material.Switch
1318
import androidx.compose.material.Text
14-
import androidx.compose.material.TopAppBar
1519
import androidx.compose.runtime.Composable
1620
import androidx.compose.runtime.getValue
1721
import androidx.compose.ui.Alignment
1822
import androidx.compose.ui.Modifier
1923
import androidx.compose.ui.graphics.Color
24+
import androidx.compose.ui.platform.ClipboardManager
25+
import androidx.compose.ui.platform.LocalClipboardManager
26+
import androidx.compose.ui.platform.LocalContext
27+
import androidx.compose.ui.text.AnnotatedString
2028
import androidx.compose.ui.unit.dp
2129
import jp.kaleidot725.sample.ui.theme.SampleTheme
2230
import jp.kaleidot725.texteditor.extension.rememberTextEditorState
31+
import jp.kaleidot725.texteditor.state.TextEditorState
2332
import jp.kaleidot725.texteditor.view.TextEditor
2433

2534
class MainActivity : ComponentActivity() {
@@ -28,16 +37,19 @@ class MainActivity : ComponentActivity() {
2837
setContent {
2938
SampleTheme {
3039
val textEditorState by rememberTextEditorState(lines = DemoText.lines())
31-
TextEditor(
32-
textEditorState = textEditorState,
33-
onUpdatedState = { /** Save Action */ },
34-
modifier = Modifier.fillMaxSize()
35-
) { index, isSelected, innerTextField ->
36-
val backgroundColor = if (isSelected) Color(0x8000ff00) else Color.White
37-
Row(modifier = Modifier.background(backgroundColor)) {
38-
Text(text = (index + 1).toString().padStart(3, '0'))
39-
Spacer(modifier = Modifier.width(4.dp))
40-
innerTextField(modifier = Modifier.fillMaxWidth())
40+
Column {
41+
TextEditorMenu(textEditorState = textEditorState)
42+
TextEditor(
43+
textEditorState = textEditorState,
44+
onUpdatedState = { /** Save Action */ },
45+
modifier = Modifier.fillMaxSize()
46+
) { index, isSelected, innerTextField ->
47+
val backgroundColor = if (isSelected) Color(0x8000ff00) else Color.White
48+
Row(modifier = Modifier.background(backgroundColor)) {
49+
Text(text = (index + 1).toString().padStart(3, '0'))
50+
Spacer(modifier = Modifier.width(4.dp))
51+
innerTextField(modifier = Modifier.weight(0.9f, fill = true))
52+
}
4153
}
4254
}
4355
}
@@ -78,3 +90,61 @@ therefore a touchscreen cannot completely replace physical buttons".[34] By 2008
7890
both Nokia and BlackBerry announced touch-based smartphones to rival the iPhone 3G, and Android's focus eventually switched to just touchscreens.
7991
The first commercially available smartphone running Android was the HTC Dream, also known as T-Mobile G1, announced on September 23, 2008.[35][36]
8092
""".trimIndent()
93+
94+
@Composable
95+
private fun ColumnScope.TextEditorMenu(textEditorState: TextEditorState) {
96+
val context: Context = LocalContext.current
97+
val clipboardManager: ClipboardManager = LocalClipboardManager.current
98+
99+
Row(modifier = Modifier.padding(8.dp)) {
100+
Text(
101+
text = "Enable multiple selection mode",
102+
modifier = Modifier
103+
.weight(0.9f, true)
104+
.align(Alignment.CenterVertically)
105+
)
106+
Switch(
107+
checked = textEditorState.isMultipleSelectionMode.value,
108+
onCheckedChange = {
109+
textEditorState.enableMultipleSelectionMode(
110+
!textEditorState.isMultipleSelectionMode.value
111+
)
112+
}
113+
)
114+
}
115+
116+
Row(modifier = Modifier.padding(8.dp)) {
117+
Text(
118+
text = "Copy selected lines",
119+
modifier = Modifier
120+
.weight(0.9f, true)
121+
.align(Alignment.CenterVertically)
122+
)
123+
Button(
124+
onClick = {
125+
val text = textEditorState.getSelectedText()
126+
textEditorState.enableMultipleSelectionMode(false)
127+
128+
clipboardManager.setText(AnnotatedString(text))
129+
Toast.makeText(context, "Copy selected text to clipboard", Toast.LENGTH_SHORT).show()
130+
}
131+
) {
132+
Text(text = "EXECUTE")
133+
}
134+
}
135+
136+
Row(modifier = Modifier.padding(8.dp)) {
137+
Text(
138+
text = "Delete selected lines",
139+
modifier = Modifier
140+
.weight(0.9f, true)
141+
.align(Alignment.CenterVertically)
142+
)
143+
Button(onClick = {
144+
textEditorState.deleteSelectedLines()
145+
textEditorState.enableMultipleSelectionMode(false)
146+
}) {
147+
Text(text = "EXECUTE")
148+
}
149+
}
150+
}

docs/1.gif

116 KB
Loading

docs/2.gif

68.7 KB
Loading

docs/3.gif

619 KB
Loading

docs/4.gif

47.6 KB
Loading

docs/5.gif

58.1 KB
Loading

docs/6.gif

63.1 KB
Loading

texteditor/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ afterEvaluate {
6565
from components.release
6666
groupId = 'com.github.kaleidot725'
6767
artifactId = 'text-editor-compose'
68-
version = '0.1.0'
68+
version = '0.2.0'
6969
}
7070
}
7171
}

0 commit comments

Comments
 (0)