Skip to content

Commit f1a58ff

Browse files
committed
Refactor editor structure
1 parent b426344 commit f1a58ff

20 files changed

+935
-688
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
{
2+
"spans": [
3+
{
4+
"insert": "Android Quill Editor",
5+
"attributes": {
6+
"bold": true,
7+
"header": 1
8+
}
9+
},
10+
{
11+
"insert": "\n",
12+
"attributes": {}
13+
},
14+
{
15+
"insert": "\nRich text editor for Android",
16+
"attributes": {
17+
"bold": true,
18+
"header": 2
19+
}
20+
},
21+
{
22+
"insert": "\n",
23+
"attributes": {}
24+
},
25+
{
26+
"insert": "Quill component for Android\n",
27+
"attributes": {
28+
"header": 3,
29+
"italic": true
30+
}
31+
},
32+
{
33+
"insert": "Bullet Journal :\nTrack personal and group journals (ToDo, Note, Ledger) from multiple views with timely reminders",
34+
"attributes": {
35+
"bold": true
36+
}
37+
},
38+
{
39+
"insert": "\nShare your tasks and notes with teammates, and see changes as they happen in real-time, across all devices\n",
40+
"attributes": {}
41+
},
42+
{
43+
"insert": "Splitting bills with friends can never be easier.",
44+
"attributes": {
45+
"list": "bullet"
46+
}
47+
},
48+
{
49+
"insert": "\n",
50+
"attributes": {}
51+
},
52+
{
53+
"insert": "Testing span addition to the editor.",
54+
"attributes": {
55+
"list": "bullet"
56+
}
57+
},
58+
{
59+
"insert": "\n",
60+
"attributes": {}
61+
},
62+
{
63+
"insert": "Start creating a group and invite your friends to join.",
64+
"attributes": {
65+
"list": "bullet"
66+
}
67+
},
68+
{
69+
"insert": "\n",
70+
"attributes": {}
71+
},
72+
{
73+
"insert": "Create a BuJo of Ledger type to see expense or balance summary.",
74+
"attributes": {
75+
"bold": true,
76+
"list": "bullet"
77+
}
78+
},
79+
{
80+
"insert": "\n",
81+
"attributes": {}
82+
}
83+
]
84+
}

app/src/main/assets/sample-data.json

-110
This file was deleted.

app/src/main/java/com/example/texteditor/MainActivity.kt

+17-11
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ import androidx.compose.ui.res.painterResource
3939
import androidx.compose.ui.tooling.preview.Preview
4040
import androidx.compose.ui.unit.dp
4141
import androidx.compose.ui.window.PopupProperties
42-
import com.canopas.editor.ui.data.RichEditorState
42+
import com.canopas.editor.ui.data.QuillEditorState
4343
import com.canopas.editor.ui.ui.RichEditor
4444
import com.canopas.editor.ui.utils.TextSpanStyle
45-
import com.example.texteditor.parser.JsonEditorParser
45+
import com.example.texteditor.parser.QuillJsonEditorParser
4646
import com.example.texteditor.ui.theme.TextEditorTheme
4747

4848
class MainActivity : ComponentActivity() {
@@ -66,21 +66,21 @@ fun Sample() {
6666
TextEditorTheme {
6767
val context = LocalContext.current
6868

69-
val state = remember {
69+
val quillState = remember {
7070
val input =
71-
context.assets.open("sample-data.json").bufferedReader().use { it.readText() }
72-
RichEditorState.Builder()
71+
context.assets.open("android-quill-sample.json").bufferedReader().use { it.readText() }
72+
QuillEditorState.Builder()
7373
.setInput(input)
74-
.adapter(JsonEditorParser())
74+
.adapter(QuillJsonEditorParser())
7575
.build()
7676
}
7777

7878
Column {
7979

80-
StyleContainer(state)
80+
StyleContainer(quillState)
8181

8282
RichEditor(
83-
state = state,
83+
state = quillState,
8484
modifier = Modifier
8585
.fillMaxWidth()
8686
.weight(1f)
@@ -94,7 +94,7 @@ fun Sample() {
9494

9595
@Composable
9696
fun StyleContainer(
97-
state: RichEditorState,
97+
state: QuillEditorState,
9898
) {
9999
Row(
100100
Modifier
@@ -123,6 +123,12 @@ fun StyleContainer(
123123
value = state,
124124
)
125125

126+
StyleButton(
127+
icon = R.drawable.baseline_format_list_bulleted_24,
128+
style = TextSpanStyle.BulletStyle,
129+
value = state,
130+
)
131+
126132
IconButton(
127133
modifier = Modifier
128134
.padding(2.dp)
@@ -144,7 +150,7 @@ fun StyleContainer(
144150

145151
@Composable
146152
fun TitleStyleButton(
147-
value: RichEditorState
153+
value: QuillEditorState
148154
) {
149155
var expanded by remember { mutableStateOf(false) }
150156

@@ -220,7 +226,7 @@ fun DropDownItem(
220226
fun StyleButton(
221227
@DrawableRes icon: Int,
222228
style: TextSpanStyle,
223-
value: RichEditorState,
229+
value: QuillEditorState,
224230
) {
225231
IconButton(
226232
modifier = Modifier

app/src/main/java/com/example/texteditor/parser/JsonEditorParser.kt

-23
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.example.texteditor.parser
2+
3+
import com.canopas.editor.ui.model.QuillSpan
4+
import com.canopas.editor.ui.model.Span
5+
import com.canopas.editor.ui.parser.QuillEditorAdapter
6+
import com.google.gson.Gson
7+
import com.google.gson.GsonBuilder
8+
import com.google.gson.reflect.TypeToken
9+
10+
class QuillJsonEditorParser : QuillEditorAdapter {
11+
12+
private val gson: Gson = GsonBuilder()
13+
.registerTypeAdapter(Span::class.java, QuillRichTextStateAdapter())
14+
.create()
15+
16+
override fun encode(input: String): QuillSpan {
17+
return gson.fromJson(input, object : TypeToken<QuillSpan>() {}.type)
18+
}
19+
20+
override fun decode(editorValue: QuillSpan): String {
21+
return gson.toJson(editorValue)
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.example.texteditor.parser
2+
3+
import android.util.Log
4+
import com.canopas.editor.ui.model.Attributes
5+
import com.canopas.editor.ui.model.Span
6+
import com.google.gson.JsonDeserializationContext
7+
import com.google.gson.JsonDeserializer
8+
import com.google.gson.JsonElement
9+
import com.google.gson.JsonObject
10+
import com.google.gson.JsonParseException
11+
import com.google.gson.JsonSerializationContext
12+
import com.google.gson.JsonSerializer
13+
import java.lang.reflect.Type
14+
15+
class QuillRichTextStateAdapter : JsonSerializer<Span>, JsonDeserializer<Span> {
16+
override fun serialize(
17+
src: Span?,
18+
typeOfSrc: Type?,
19+
context: JsonSerializationContext?
20+
): JsonElement {
21+
val jsonObject = JsonObject()
22+
jsonObject.add("insert", context?.serialize(src?.insert))
23+
jsonObject.add("attributes", context?.serialize(src?.attributes))
24+
return jsonObject
25+
}
26+
27+
override fun deserialize(
28+
json: JsonElement?,
29+
typeOfT: Type?,
30+
context: JsonDeserializationContext?
31+
): Span {
32+
try {
33+
val jsonObject = json?.asJsonObject ?: throw JsonParseException("Invalid JSON")
34+
val insert = jsonObject.get("insert")
35+
val attributes = jsonObject.get("attributes")
36+
return Span(
37+
insert = context?.deserialize<String>(insert, String::class.java),
38+
attributes = context?.deserialize<Attributes>(attributes, Attributes::class.java)
39+
)
40+
} catch (e: Exception) {
41+
Log.e("QuillRichTextStateAdapter", "deserialize: ", e)
42+
throw JsonParseException("Invalid JSON")
43+
}
44+
}
45+
}

0 commit comments

Comments
 (0)