@@ -6,7 +6,6 @@ import android.text.style.BulletSpan
6
6
import android.text.style.RelativeSizeSpan
7
7
import android.text.style.StyleSpan
8
8
import android.text.style.UnderlineSpan
9
- import android.util.Log
10
9
import androidx.compose.runtime.mutableStateListOf
11
10
import androidx.compose.ui.text.TextRange
12
11
import com.canopas.editor.ui.model.Attributes
@@ -123,7 +122,7 @@ class QuillTextManager(quillSpan: QuillSpan) {
123
122
if (! previousInsert.isNullOrEmpty() && previousInsert.last() == ' ' ) {
124
123
insert + = ' '
125
124
}
126
- val attributes = Attributes (
125
+ var attributes = Attributes (
127
126
header = if (span.style.any { it.isHeaderStyle() }) span.style.find { it.isHeaderStyle() }
128
127
?.headerLevel() else null ,
129
128
bold = if (span.style.contains(TextSpanStyle .BoldStyle )) true else null ,
@@ -132,6 +131,10 @@ class QuillTextManager(quillSpan: QuillSpan) {
132
131
list = if (span.style.contains(TextSpanStyle .BulletStyle )) ListType .bullet else null
133
132
)
134
133
134
+ if (insert == " \n " ) {
135
+ attributes = Attributes ()
136
+ }
137
+
135
138
// Merge consecutive spans with the same attributes into one
136
139
if (groupedSpans.isNotEmpty() && groupedSpans.last().attributes == attributes && attributes.list == null ) {
137
140
groupedSpans.last().insert + = insert
@@ -240,10 +243,91 @@ class QuillTextManager(quillSpan: QuillSpan) {
240
243
val fromIndex = selection.min
241
244
val toIndex = selection.max - 1
242
245
243
- val selectedParts = quillTextSpans.filter { part ->
244
- part.from < toIndex && part.to >= fromIndex && part.style.contains(style)
246
+ val selectedSpan = quillTextSpans.find {
247
+ it.from <= fromIndex && it.to >= toIndex
248
+ }
249
+ if (selectedSpan != null ) {
250
+ if (fromIndex == selectedSpan.from && toIndex == selectedSpan.to) {
251
+ val index = quillTextSpans.indexOf(selectedSpan)
252
+ quillTextSpans[index] =
253
+ selectedSpan.copy(style = selectedSpan.style.filterNot { it == style })
254
+ } else {
255
+ if (fromIndex == selectedSpan.from && toIndex < selectedSpan.to) {
256
+ val index = quillTextSpans.indexOf(selectedSpan)
257
+ quillTextSpans.removeAt(index)
258
+ quillTextSpans.add(
259
+ index,
260
+ QuillTextSpan (
261
+ from = fromIndex,
262
+ to = toIndex,
263
+ style = selectedSpan.style.filterNot { it == style }
264
+ )
265
+ )
266
+ quillTextSpans.add(
267
+ index + 1 ,
268
+ QuillTextSpan (
269
+ from = toIndex + 1 ,
270
+ to = selectedSpan.to,
271
+ style = selectedSpan.style
272
+ )
273
+ )
274
+ } else if (fromIndex > selectedSpan.from && toIndex < selectedSpan.to) {
275
+ val index = quillTextSpans.indexOf(selectedSpan)
276
+ quillTextSpans.removeAt(index)
277
+ quillTextSpans.add(
278
+ index,
279
+ QuillTextSpan (
280
+ from = selectedSpan.from,
281
+ to = fromIndex - 1 ,
282
+ style = selectedSpan.style
283
+ )
284
+ )
285
+ quillTextSpans.add(
286
+ index + 1 ,
287
+ QuillTextSpan (
288
+ from = fromIndex,
289
+ to = toIndex,
290
+ style = selectedSpan.style.filterNot { it == style }
291
+ )
292
+ )
293
+ quillTextSpans.add(
294
+ index + 2 ,
295
+ QuillTextSpan (
296
+ from = toIndex + 1 ,
297
+ to = selectedSpan.to,
298
+ style = selectedSpan.style
299
+ )
300
+ )
301
+ } else if (fromIndex > 0 ) {
302
+ val index = quillTextSpans.indexOf(selectedSpan)
303
+ quillTextSpans.removeAt(index)
304
+ quillTextSpans.add(
305
+ index,
306
+ QuillTextSpan (
307
+ from = selectedSpan.from,
308
+ to = fromIndex - 1 ,
309
+ style = selectedSpan.style
310
+ )
311
+ )
312
+ quillTextSpans.add(
313
+ index + 1 ,
314
+ QuillTextSpan (
315
+ from = fromIndex,
316
+ to = toIndex,
317
+ style = selectedSpan.style.filterNot { it == style }
318
+ )
319
+ )
320
+ quillTextSpans.add(
321
+ index + 2 ,
322
+ QuillTextSpan (
323
+ from = toIndex + 1 ,
324
+ to = selectedSpan.to,
325
+ style = selectedSpan.style
326
+ )
327
+ )
328
+ }
329
+ }
245
330
}
246
- removeStylesFromSelectedPart(selectedParts, fromIndex, toIndex, style)
247
331
updateText()
248
332
}
249
333
}
@@ -310,77 +394,6 @@ class QuillTextManager(quillSpan: QuillSpan) {
310
394
})
311
395
}
312
396
313
- private fun removeStylesFromSelectedPart (
314
- selectedParts : List <QuillTextSpan >,
315
- fromIndex : Int , toIndex : Int , style : TextSpanStyle
316
- ) {
317
-
318
- Log .e(
319
- " XXX" ,
320
- " Selected parts: $selectedParts \n From index: $fromIndex \n To index: $toIndex \n Style: $style "
321
- )
322
- selectedParts.forEach { part ->
323
- val index = quillTextSpans.indexOf(part)
324
- if (index !in quillTextSpans.indices) return @forEach
325
-
326
- if (fromIndex == part.from && toIndex == part.to) {
327
- quillTextSpans[index] = part.copy(style = part.style.filterNot { it == style })
328
- } else if (fromIndex >= part.from && toIndex < part.to && toIndex > 0 ) {
329
- quillTextSpans.removeAt(index)
330
- quillTextSpans.add(
331
- index,
332
- QuillTextSpan (
333
- from = part.from,
334
- to = fromIndex - 1 ,
335
- style = part.style
336
- )
337
- )
338
- quillTextSpans.add(
339
- index + 1 ,
340
- QuillTextSpan (
341
- from = fromIndex,
342
- to = toIndex,
343
- style = part.style.filterNot { it == style }
344
- )
345
- )
346
- quillTextSpans.add(
347
- index + 2 ,
348
- QuillTextSpan (
349
- from = toIndex + 1 ,
350
- to = part.to,
351
- style = part.style
352
- )
353
- )
354
- } else if (fromIndex > 0 ) {
355
- quillTextSpans.removeAt(index)
356
- quillTextSpans.add(
357
- index,
358
- QuillTextSpan (
359
- from = part.from,
360
- to = fromIndex - 1 ,
361
- style = part.style
362
- )
363
- )
364
- quillTextSpans.add(
365
- index + 1 ,
366
- QuillTextSpan (
367
- from = fromIndex,
368
- to = toIndex,
369
- style = part.style.filterNot { it == style }
370
- )
371
- )
372
- quillTextSpans.add(
373
- index + 2 ,
374
- QuillTextSpan (
375
- from = toIndex + 1 ,
376
- to = part.to,
377
- style = part.style
378
- )
379
- )
380
- }
381
- }
382
- }
383
-
384
397
private fun applyStylesToSelectedText (style : TextSpanStyle ) {
385
398
if (selection.collapsed) return
386
399
@@ -618,6 +631,32 @@ class QuillTextManager(quillSpan: QuillSpan) {
618
631
}
619
632
}
620
633
}
634
+ if (currentSpan == null ) {
635
+ val lastSpan = quillTextSpans.lastOrNull()
636
+ if (lastSpan != null ) {
637
+ val lastStyles = lastSpan.style
638
+ if (lastStyles == selectedStyles) {
639
+ quillTextSpans[quillTextSpans.lastIndex] =
640
+ lastSpan.copy(to = lastSpan.to + typedCharsCount)
641
+ } else {
642
+ quillTextSpans.add(
643
+ QuillTextSpan (
644
+ from = startTypeIndex,
645
+ to = startTypeIndex + typedCharsCount - 1 ,
646
+ style = selectedStyles
647
+ )
648
+ )
649
+ }
650
+ } else {
651
+ quillTextSpans.add(
652
+ QuillTextSpan (
653
+ from = startTypeIndex,
654
+ to = startTypeIndex + typedCharsCount - 1 ,
655
+ style = selectedStyles
656
+ )
657
+ )
658
+ }
659
+ }
621
660
}
622
661
623
662
private fun moveSpans (startTypeIndex : Int , by : Int ) {
0 commit comments