-
Notifications
You must be signed in to change notification settings - Fork 114
/
AztecText.kt
879 lines (697 loc) · 33.3 KB
/
AztecText.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
/*
* Copyright (C) 2016 Automattic
* Copyright (C) 2015 Matthew Lee
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.wordpress.aztec
import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.graphics.drawable.Drawable
import android.os.Bundle
import android.os.Parcel
import android.os.Parcelable
import android.support.v4.content.ContextCompat
import android.support.v7.app.AlertDialog
import android.text.*
import android.text.style.ImageSpan
import android.text.style.LeadingMarginSpan
import android.text.style.ParagraphStyle
import android.text.style.SuggestionSpan
import android.util.AttributeSet
import android.view.KeyEvent
import android.view.LayoutInflater
import android.view.View
import android.view.inputmethod.BaseInputConnection
import android.view.inputmethod.EditorInfo
import android.view.inputmethod.InputConnection
import android.view.inputmethod.InputConnectionWrapper
import android.widget.EditText
import org.wordpress.android.util.DisplayUtils
import org.wordpress.aztec.formatting.BlockFormatter
import org.wordpress.aztec.formatting.InlineFormatter
import org.wordpress.aztec.formatting.LineBlockFormatter
import org.wordpress.aztec.formatting.LinkFormatter
import org.wordpress.aztec.source.Format
import org.wordpress.aztec.spans.*
import org.wordpress.aztec.util.TypefaceCache
import java.util.*
class AztecText : EditText, TextWatcher {
private var historyEnable = resources.getBoolean(R.bool.history_enable)
private var historySize = resources.getInteger(R.integer.history_size)
private var addLinkDialog: AlertDialog? = null
private var consumeEditEvent: Boolean = false
private var textChangedEventDetails = TextChangedEvent("", 0, 0, 0)
private var onSelectionChangedListener: OnSelectionChangedListener? = null
private var onImeBackListener: OnImeBackListener? = null
private var isViewInitialized = false
private var previousCursorPosition = 0
val selectedStyles = ArrayList<TextFormat>()
private var isNewStyleSelected = false
var isMediaAdded = false
lateinit var history: History
lateinit var inlineFormatter: InlineFormatter
lateinit var blockFormatter: BlockFormatter
val lineBlockFormatter: LineBlockFormatter
lateinit var linkFormatter: LinkFormatter
var imageGetter: Html.ImageGetter? = null
interface OnSelectionChangedListener {
fun onSelectionChanged(selStart: Int, selEnd: Int)
}
interface OnImeBackListener {
fun onImeBack()
}
init {
lineBlockFormatter = LineBlockFormatter(this)
}
constructor(context: Context) : super(context) {
init(null)
}
constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
init(attrs)
}
constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
init(attrs)
}
private fun init(attrs: AttributeSet?) {
TypefaceCache.setCustomTypeface(context, this, TypefaceCache.TYPEFACE_MERRIWEATHER_REGULAR)
val array = context.obtainStyledAttributes(attrs, R.styleable.AztecText, 0, R.style.AztecTextStyle)
setLineSpacing(
array.getDimension(
R.styleable.AztecText_lineSpacingExtra,
resources.getDimension(R.dimen.spacing_extra)
),
array.getFloat(
R.styleable.AztecText_lineSpacingMultiplier,
resources.getString(R.dimen.spacing_multiplier).toFloat()
)
)
setBackgroundColor(array.getColor(R.styleable.AztecText_backgroundColor, ContextCompat.getColor(context, R.color.background)))
setTextColor(array.getColor(R.styleable.AztecText_textColor, ContextCompat.getColor(context, R.color.text)))
setHintTextColor(array.getColor(R.styleable.AztecText_textColorHint, ContextCompat.getColor(context, R.color.text_hint)))
historyEnable = array.getBoolean(R.styleable.AztecText_historyEnable, historyEnable)
historySize = array.getInt(R.styleable.AztecText_historySize, historySize)
inlineFormatter = InlineFormatter(this,
InlineFormatter.CodeStyle(
array.getColor(R.styleable.AztecText_codeBackground, 0),
array.getColor(R.styleable.AztecText_codeColor, 0)))
blockFormatter = BlockFormatter(this,
BlockFormatter.ListStyle(
array.getColor(R.styleable.AztecText_bulletColor, 0),
array.getDimensionPixelSize(R.styleable.AztecText_bulletMargin, 0),
array.getDimensionPixelSize(R.styleable.AztecText_bulletPadding, 0),
array.getDimensionPixelSize(R.styleable.AztecText_bulletWidth, 0)),
BlockFormatter.QuoteStyle(
array.getColor(R.styleable.AztecText_quoteBackground, 0),
array.getColor(R.styleable.AztecText_quoteColor, 0),
array.getDimensionPixelSize(R.styleable.AztecText_quoteMargin, 0),
array.getDimensionPixelSize(R.styleable.AztecText_quotePadding, 0),
array.getDimensionPixelSize(R.styleable.AztecText_quoteWidth, 0)
))
linkFormatter = LinkFormatter(this, LinkFormatter.LinkStyle(array.getColor(
R.styleable.AztecText_linkColor, 0),
array.getBoolean(R.styleable.AztecText_linkUnderline, true)))
array.recycle()
if (historyEnable && historySize <= 0) {
throw IllegalArgumentException("historySize must > 0")
}
history = History(historyEnable, historySize)
// triggers ClickableSpan onClick() events
movementMethod = EnhancedMovementMethod
//Detect the press of backspace from hardware keyboard when no characters are deleted (eg. at 0 index of EditText)
setOnKeyListener { v, keyCode, event ->
var consumeKeyEvent = false
history.beforeTextChanged(toFormattedHtml())
if (keyCode == KeyEvent.KEYCODE_DEL && event.action == KeyEvent.ACTION_DOWN) {
consumeKeyEvent = blockFormatter.tryRemoveBlockStyleFromFirstLine()
}
if (consumeKeyEvent) {
history.handleHistory(this@AztecText)
}
consumeKeyEvent
}
isViewInitialized = true
}
override fun onAttachedToWindow() {
super.onAttachedToWindow()
addTextChangedListener(this)
}
override fun onDetachedFromWindow() {
super.onDetachedFromWindow()
removeTextChangedListener(this)
if (addLinkDialog != null && addLinkDialog!!.isShowing) {
addLinkDialog!!.dismiss()
}
}
override fun onRestoreInstanceState(state: Parcelable?) {
val savedState = state as SavedState
super.onRestoreInstanceState(savedState.superState)
val customState = savedState.state
val array = ArrayList(customState.getStringArrayList("historyList"))
val list = LinkedList<String>()
list += array
history.historyList = list
history.historyCursor = customState.getInt("historyCursor")
history.inputLast = customState.getString("inputLast")
visibility = customState.getInt("visibility")
val retainedHtml = customState.getString("retained_html")
fromHtml(retainedHtml)
val retainedSelectionStart = customState.getInt("selection_start")
val retainedSelectionEnd = customState.getInt("selection_end")
setSelection(retainedSelectionStart, retainedSelectionEnd)
val isDialogVisible = customState.getBoolean("isUrlDialogVisible", false)
if (isDialogVisible) {
val retainedUrl = customState.getString("retainedUrl", "")
val retainedAnchor = customState.getString("retainedAnchor", "")
showLinkDialog(retainedUrl, retainedAnchor)
}
isMediaAdded = customState.getBoolean("isMediaAdded")
}
override fun onSaveInstanceState(): Parcelable {
val superState = super.onSaveInstanceState()
val savedState = SavedState(superState)
val bundle = Bundle()
bundle.putStringArrayList("historyList", ArrayList<String>(history.historyList))
bundle.putInt("historyCursor", history.historyCursor)
bundle.putString("inputLast", history.inputLast)
bundle.putInt("visibility", visibility)
bundle.putString("retained_html", toHtml(false))
bundle.putInt("selection_start", selectionStart)
bundle.putInt("selection_end", selectionEnd)
if (addLinkDialog != null && addLinkDialog!!.isShowing) {
bundle.putBoolean("isUrlDialogVisible", true)
val urlInput = addLinkDialog!!.findViewById(R.id.linkURL) as EditText
val anchorInput = addLinkDialog!!.findViewById(R.id.linkText) as EditText
bundle.putString("retainedUrl", urlInput.text.toString())
bundle.putString("retainedAnchor", anchorInput.text.toString())
}
bundle.putBoolean("isMediaAdded", isMediaAdded)
savedState.state = bundle
return savedState
}
internal class SavedState : BaseSavedState {
var state: Bundle = Bundle()
constructor(superState: Parcelable) : super(superState) {
}
constructor(parcel: Parcel) : super(parcel) {
state = parcel.readBundle(javaClass.classLoader)
}
override fun writeToParcel(out: Parcel, flags: Int) {
super.writeToParcel(out, flags)
out.writeBundle(state)
}
companion object {
@JvmField val CREATOR: Parcelable.Creator<SavedState> = object : Parcelable.Creator<SavedState> {
override fun createFromParcel(source: Parcel): SavedState {
return SavedState(source)
}
override fun newArray(size: Int): Array<SavedState?> {
return arrayOfNulls(size)
}
}
}
}
fun setSelectedStyles(styles: ArrayList<TextFormat>) {
isNewStyleSelected = true
selectedStyles.clear()
selectedStyles.addAll(styles)
}
fun setOnSelectionChangedListener(onSelectionChangedListener: OnSelectionChangedListener) {
this.onSelectionChangedListener = onSelectionChangedListener
}
fun setOnImeBackListener(listener: OnImeBackListener) {
this.onImeBackListener = listener
}
override fun onKeyPreIme(keyCode: Int, event: KeyEvent): Boolean {
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
onImeBackListener?.onImeBack()
}
return super.onKeyPreIme(keyCode, event)
}
public override fun onSelectionChanged(selStart: Int, selEnd: Int) {
super.onSelectionChanged(selStart, selEnd)
if (!isViewInitialized) return
if (selStart == selEnd && movedCursorIfBeforeZwjChar(selEnd)) return
previousCursorPosition = selEnd
onSelectionChangedListener?.onSelectionChanged(selStart, selEnd)
setSelectedStyles(getAppliedStyles(selStart, selEnd))
}
private fun movedCursorIfBeforeZwjChar(selEnd: Int): Boolean {
if (selEnd < text.length && text[selEnd] == Constants.ZWJ_CHAR) {
if (selEnd == previousCursorPosition - 1 && selEnd > 0) {
// moved left
setSelection(selEnd - 1)
} else {
// moved right or dropped to right to the left of ZWJ
setSelection(selEnd + 1)
}
return true
}
return false
}
fun getSelectedText(): String {
if (selectionStart == -1 || selectionEnd == -1) return ""
return editableText.substring(selectionStart, selectionEnd)
}
fun getAppliedStyles(selectionStart: Int, selectionEnd: Int): ArrayList<TextFormat> {
val styles = ArrayList<TextFormat>()
var newSelStart = if (selectionStart > selectionEnd) selectionEnd else selectionStart
var newSelEnd = selectionEnd
if (editableText.isEmpty()) {
return styles
}
if (newSelStart == 0 && newSelEnd == 0) {
newSelEnd++
} else if (newSelStart == newSelEnd && editableText.length > selectionStart && editableText[selectionStart - 1] == '\n') {
newSelEnd++
} else if (newSelStart > 0 && !isTextSelected()) {
newSelStart--
}
TextFormat.values().forEach {
if (contains(it, newSelStart, newSelEnd)) {
styles.add(it)
}
}
return styles
}
fun isEmpty(): Boolean {
return text.isEmpty()
}
fun formattingIsApplied(): Boolean {
return !selectedStyles.isEmpty()
}
fun formattingHasChanged(): Boolean {
return isNewStyleSelected
}
fun setFormattingChangesApplied() {
isNewStyleSelected = false
}
fun isTextSelected(): Boolean {
return selectionStart != selectionEnd
}
fun toggleFormatting(textFormat: TextFormat) {
history.beforeTextChanged(toFormattedHtml())
when (textFormat) {
TextFormat.FORMAT_PARAGRAPH,
TextFormat.FORMAT_HEADING_1,
TextFormat.FORMAT_HEADING_2,
TextFormat.FORMAT_HEADING_3,
TextFormat.FORMAT_HEADING_4,
TextFormat.FORMAT_HEADING_5,
TextFormat.FORMAT_HEADING_6 -> lineBlockFormatter.applyHeading(textFormat)
TextFormat.FORMAT_BOLD -> inlineFormatter.toggleBold()
TextFormat.FORMAT_ITALIC -> inlineFormatter.toggleItalic()
TextFormat.FORMAT_STRIKETHROUGH -> inlineFormatter.toggleStrikethrough()
TextFormat.FORMAT_UNORDERED_LIST -> blockFormatter.toggleUnorderedList()
TextFormat.FORMAT_ORDERED_LIST -> blockFormatter.toggleOrderedList()
TextFormat.FORMAT_QUOTE -> blockFormatter.toggleQuote()
TextFormat.FORMAT_MORE -> lineBlockFormatter.applyMoreComment()
TextFormat.FORMAT_PAGE -> lineBlockFormatter.applyPageComment()
TextFormat.FORMAT_CODE -> inlineFormatter.toggleCode()
else -> {
}
}
history.handleHistory(this)
}
fun contains(format: TextFormat, selStart: Int = selectionStart, selEnd: Int = selectionEnd): Boolean {
when (format) {
TextFormat.FORMAT_HEADING_1,
TextFormat.FORMAT_HEADING_2,
TextFormat.FORMAT_HEADING_3,
TextFormat.FORMAT_HEADING_4,
TextFormat.FORMAT_HEADING_5,
TextFormat.FORMAT_HEADING_6 -> return lineBlockFormatter.containsHeading(format, selStart, selEnd)
TextFormat.FORMAT_BOLD -> return inlineFormatter.containsInlineStyle(TextFormat.FORMAT_BOLD, selStart, selEnd)
TextFormat.FORMAT_ITALIC -> return inlineFormatter.containsInlineStyle(TextFormat.FORMAT_ITALIC, selStart, selEnd)
TextFormat.FORMAT_UNDERLINED -> return inlineFormatter.containsInlineStyle(TextFormat.FORMAT_UNDERLINED, selStart, selEnd)
TextFormat.FORMAT_STRIKETHROUGH -> return inlineFormatter.containsInlineStyle(TextFormat.FORMAT_STRIKETHROUGH, selStart, selEnd)
TextFormat.FORMAT_UNORDERED_LIST -> return blockFormatter.containsList(TextFormat.FORMAT_UNORDERED_LIST, selStart, selEnd)
TextFormat.FORMAT_ORDERED_LIST -> return blockFormatter.containsList(TextFormat.FORMAT_ORDERED_LIST, selStart, selEnd)
TextFormat.FORMAT_QUOTE -> return blockFormatter.containQuote(selectionStart, selectionEnd)
TextFormat.FORMAT_LINK -> return linkFormatter.containLink(selStart, selEnd)
TextFormat.FORMAT_CODE -> return inlineFormatter.containsInlineStyle(TextFormat.FORMAT_CODE, selStart, selEnd)
else -> return false
}
}
override fun beforeTextChanged(text: CharSequence, start: Int, count: Int, after: Int) {
if (!isViewInitialized) return
if (selectionEnd < text.length && text[selectionEnd] == Constants.ZWJ_CHAR) {
setSelection(selectionEnd + 1)
}
prepareTextChangeEventDetails(after, count, start, text)
blockFormatter.carryOverDeletedListItemAttributes(count, start, text, this.text)
inlineFormatter.carryOverInlineSpans(start, count, after)
if (!isTextChangedListenerDisabled()) {
history.beforeTextChanged(toFormattedHtml())
}
}
private fun prepareTextChangeEventDetails(after: Int, count: Int, start: Int, text: CharSequence) {
var deletedFromBlock = false
var blockStart = -1
var blockEnd = -1
if (count > 0 && after < count && !isTextChangedListenerDisabled()) {
val block = this.text.getSpans(start, start + 1, AztecBlockSpan::class.java).firstOrNull()
if (block != null) {
blockStart = this.text.getSpanStart(block)
blockEnd = this.text.getSpanEnd(block)
deletedFromBlock = start < blockEnd && this.text[start] != '\n' &&
(start + count >= blockEnd || (start + count + 1 == blockEnd && text[start + count] == '\n')) &&
(start == 0 || text[start - 1] == '\n')
// if we are removing all characters from the span, we must change the flag to SPAN_EXCLUSIVE_INCLUSIVE
// because we want to allow a block span with empty text (such as list with a single empty first item)
if (deletedFromBlock && after == 0 && blockEnd - blockStart == count) {
this.text.setSpan(block, blockStart, blockEnd, Spanned.SPAN_EXCLUSIVE_INCLUSIVE)
}
}
}
if (deletedFromBlock) {
textChangedEventDetails = TextChangedEvent(this.text.toString(), deletedFromBlock, blockStart)
} else {
textChangedEventDetails = TextChangedEvent(this.text.toString())
}
}
override fun onTextChanged(text: CharSequence, start: Int, before: Int, count: Int) {
if (!isViewInitialized) return
inlineFormatter.reapplyCarriedOverInlineSpans()
textChangedEventDetails.before = before
textChangedEventDetails.text = text
textChangedEventDetails.countOfCharacters = count
textChangedEventDetails.start = start
textChangedEventDetails.initialize()
}
override fun afterTextChanged(text: Editable) {
if (isTextChangedListenerDisabled()) {
enableTextChangedListener()
return
}
if (textChangedEventDetails.inputStart == 0 && textChangedEventDetails.count == 0) {
removeLeadingStyle(text, AztecInlineSpan::class.java)
removeLeadingStyle(text, LeadingMarginSpan::class.java)
}
val newLine = textChangedEventDetails.isNewLine()
blockFormatter.handleBlockStyling(text, textChangedEventDetails)
inlineFormatter.handleInlineStyling(textChangedEventDetails)
lineBlockFormatter.handleLineBlockStyling(textChangedEventDetails)
isMediaAdded = text.getSpans(0, text.length, AztecMediaSpan::class.java).isNotEmpty()
if (textChangedEventDetails.count > 0 && text.isEmpty()) {
onSelectionChanged(0, 0)
}
// preserve the attributes on the previous list item when adding a new one
blockFormatter.realignAttributesWhenAddingItem(text, textChangedEventDetails, newLine)
history.handleHistory(this)
}
fun removeLeadingStyle(text: Editable, spanClass: Class<*>) {
text.getSpans(0, 0, spanClass).forEach {
if (text.isNotEmpty()) {
text.setSpan(it, 0, text.getSpanEnd(it), text.getSpanFlags(it))
} else {
text.removeSpan(it)
}
}
}
fun redo() {
history.redo(this)
}
fun undo() {
history.undo(this)
}
// Helper ======================================================================================
fun consumeCursorPosition(text: SpannableStringBuilder): Int {
var cursorPosition = 0
text.getSpans(0, text.length, AztecCursorSpan::class.java).forEach {
cursorPosition = text.getSpanStart(it)
text.removeSpan(it)
}
return cursorPosition
}
fun fromHtml(source: String) {
disableTextChangedListener()
editableText.clear()
val builder = SpannableStringBuilder()
val parser = AztecParser()
builder.append(parser.fromHtml(Format.clearFormatting(source), context))
switchToAztecStyle(builder, 0, builder.length)
disableTextChangedListener()
val cursorPosition = consumeCursorPosition(builder)
setTextKeepState(builder)
enableTextChangedListener()
setSelection(cursorPosition)
loadImages()
}
private fun loadImages() {
val spans = this.text.getSpans(0, text.length, ImageSpan::class.java)
spans.forEach {
if (it !is AztecMediaSpan && it !is UnknownHtmlSpan && it !is AztecCommentSpan) {
val callbacks = object : Html.ImageGetter.Callbacks {
override fun onImageLoaded(drawable: Drawable?) {
replaceImage(drawable)
}
override fun onImageLoadingFailed() {
val drawable = ContextCompat.getDrawable(context, R.drawable.ic_image_failed)
replaceImage(drawable)
}
private fun replaceImage(drawable: Drawable?) {
val start = text.getSpanStart(it)
val end = text.getSpanEnd(it)
if (start == -1 || end == -1) return
text.removeSpan(it)
val newImageSpan = ImageSpan(drawable, it.source)
drawable?.setBounds(0, 0, drawable.intrinsicWidth, drawable.intrinsicHeight)
text.setSpan(newImageSpan, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
}
}
/*
* Following Android guidelines for keylines and spacing, screen edge margins should
* be 16dp. Therefore, the width of images should be the width of the screen minus
* 16dp on both sides (i.e. 16 * 2 = 32).
*
* https://material.io/guidelines/layout/metrics-keylines.html#metrics-keylines-baseline-grids
*/
val width = context.resources.displayMetrics.widthPixels - DisplayUtils.dpToPx(context, 32)
imageGetter?.loadImage(it.source, callbacks, width)
}
}
}
fun toHtml(withCursorTag: Boolean = false): String {
val parser = AztecParser()
val output = SpannableStringBuilder(text)
clearMetaSpans(output)
if (withCursorTag) {
output.setSpan(AztecCursorSpan(), selectionEnd, selectionEnd, Spanned.SPAN_MARK_MARK)
}
return Format.clearFormatting(parser.toHtml(output, withCursorTag))
}
fun toFormattedHtml(): String {
val parser = AztecParser()
val output = SpannableStringBuilder(text)
clearMetaSpans(output)
return Format.addFormatting(parser.toHtml(output))
}
private fun switchToAztecStyle(editable: Editable, start: Int, end: Int) {
val blockSpans = editable.getSpans(start, end, AztecBlockSpan::class.java)
blockSpans.forEach {
val spanStart = editable.getSpanStart(it)
val spanEnd = editable.getSpanEnd(it)
editable.removeSpan(it)
if (it is AztecListSpan) {
editable.setSpan(blockFormatter.makeBlockSpan(it.javaClass as Class<AztecBlockSpan>, it.attributes, it.lastItem), spanStart, spanEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
} else {
editable.setSpan(blockFormatter.makeBlockSpan(it.javaClass, it.attributes), spanStart, spanEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
}
}
val paragraphSpans = editable.getSpans(start, end, ParagraphSpan::class.java)
for (span in paragraphSpans) {
val spanStart = editable.getSpanStart(span)
var spanEnd = editable.getSpanEnd(span)
spanEnd = if (0 < spanEnd && spanEnd < editable.length && editable[spanEnd] == '\n') spanEnd - 1 else spanEnd
editable.removeSpan(span)
editable.setSpan(ParagraphSpan(span.attributes), spanStart, spanEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
}
val urlSpans = editable.getSpans(start, end, AztecURLSpan::class.java)
for (span in urlSpans) {
val spanStart = editable.getSpanStart(span)
val spanEnd = editable.getSpanEnd(span)
editable.removeSpan(span)
editable.setSpan(linkFormatter.makeUrlSpan(span.url, span.attributes), spanStart, spanEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
}
val codeSpans = editable.getSpans(start, end, AztecCodeSpan::class.java)
codeSpans.forEach {
val spanStart = editable.getSpanStart(it)
val spanEnd = editable.getSpanEnd(it)
editable.removeSpan(it)
editable.setSpan(inlineFormatter.makeInlineSpan(it.javaClass, it.attributes), spanStart, spanEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
}
}
fun disableTextChangedListener() {
consumeEditEvent = true
}
fun enableTextChangedListener() {
consumeEditEvent = false
}
fun isTextChangedListenerDisabled(): Boolean {
return consumeEditEvent
}
fun refreshText() {
disableTextChangedListener()
val selStart = selectionStart
val selEnd = selectionEnd
text = editableText
setSelection(selStart, selEnd)
enableTextChangedListener()
}
fun removeHeadingStylesFromRange(start: Int, end: Int) {
val spans = editableText.getSpans(start, end, AztecHeadingSpan::class.java)
for (span in spans) {
editableText.removeSpan(span)
}
}
fun removeInlineStylesFromRange(start: Int, end: Int) {
inlineFormatter.removeInlineStyle(TextFormat.FORMAT_BOLD, start, end)
inlineFormatter.removeInlineStyle(TextFormat.FORMAT_ITALIC, start, end)
inlineFormatter.removeInlineStyle(TextFormat.FORMAT_STRIKETHROUGH, start, end)
inlineFormatter.removeInlineStyle(TextFormat.FORMAT_UNDERLINED, start, end)
inlineFormatter.removeInlineStyle(TextFormat.FORMAT_CODE, start, end)
}
fun removeBlockStylesFromRange(start: Int, end: Int, ignoreLineBounds: Boolean = false) {
blockFormatter.removeBlockStyle(start, end, AztecBlockSpan::class.java, ignoreLineBounds)
}
//logic party copied from TextView
override fun onTextContextMenuItem(id: Int): Boolean {
var min = 0
var max = text.length
if (isFocused) {
min = Math.max(0, Math.min(selectionStart, selectionEnd))
max = Math.max(0, Math.max(selectionStart, selectionEnd))
}
when (id) {
android.R.id.paste -> paste(text, min, max)
android.R.id.copy -> {
copy(text, min, max)
clearFocus() //hide text action menu
}
android.R.id.cut -> {
copy(text, min, max)
text.delete(min, max) //this will hide text action menu
}
else -> return super.onTextContextMenuItem(id)
}
return true
}
//Convert selected text to html and add it to clipboard
fun copy(editable: Editable, start: Int, end: Int) {
val selectedText = editable.subSequence(start, end)
val parser = AztecParser()
val output = SpannableStringBuilder(selectedText)
//Strip block elements untill we figure out copy paste completely
output.getSpans(0, output.length, ParagraphStyle::class.java).forEach { output.removeSpan(it) }
clearMetaSpans(output)
val html = Format.clearFormatting(parser.toHtml(output))
val clipboard = context.getSystemService(Context.CLIPBOARD_SERVICE) as android.content.ClipboardManager
clipboard.primaryClip = ClipData.newPlainText(null, html)
}
//copied from TextView with some changes
private fun paste(editable: Editable, min: Int, max: Int) {
val clipboard = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
val clip = clipboard.primaryClip
if (clip != null) {
val parser = AztecParser()
for (i in 0..clip.itemCount - 1) {
val textToPaste = clip.getItemAt(i).coerceToText(context)
val builder = SpannableStringBuilder()
builder.append(parser.fromHtml(Format.clearFormatting(textToPaste.toString()), context).trim())
Selection.setSelection(editable, max)
disableTextChangedListener()
editable.replace(min, max, builder)
enableTextChangedListener()
inlineFormatter.joinStyleSpans(0, editable.length) //TODO: see how this affects performance
}
}
}
fun clearMetaSpans(text: Spannable) {
BaseInputConnection.removeComposingSpans(text)
text.getSpans(0, text.length, SuggestionSpan::class.java).forEach { text.removeSpan(it) }
}
override fun setVisibility(visibility: Int) {
super.setVisibility(visibility)
if (visibility == View.VISIBLE) {
requestFocus()
}
}
fun link(url: String, anchor: String) {
if (TextUtils.isEmpty(url) && linkFormatter.isUrlSelected()) {
removeLink()
} else if (linkFormatter.isUrlSelected()) {
linkFormatter.editLink(url, anchor, linkFormatter.getUrlSpanBounds().first, linkFormatter.getUrlSpanBounds().second)
} else {
linkFormatter.addLink(url, anchor, selectionStart, selectionEnd)
}
}
fun removeLink() {
val urlSpanBounds = linkFormatter.getUrlSpanBounds()
linkFormatter.linkInvalid(urlSpanBounds.first, urlSpanBounds.second)
onSelectionChanged(urlSpanBounds.first, urlSpanBounds.second)
}
fun showLinkDialog(presetUrl: String = "", presetAnchor: String = "") {
val urlAndAnchor = linkFormatter.getSelectedUrlWithAnchor()
val url = if (TextUtils.isEmpty(presetUrl)) urlAndAnchor.first else presetUrl
val anchor = if (TextUtils.isEmpty(presetAnchor)) urlAndAnchor.second else presetAnchor
val builder = AlertDialog.Builder(context)
val dialogView = LayoutInflater.from(context).inflate(R.layout.dialog_link, null)
val urlInput = dialogView.findViewById(R.id.linkURL) as EditText
val anchorInput = dialogView.findViewById(R.id.linkText) as EditText
urlInput.setText(url)
anchorInput.setText(anchor)
builder.setView(dialogView)
builder.setTitle(R.string.dialog_title)
builder.setPositiveButton(R.string.dialog_button_ok, { dialog, which ->
val linkText = urlInput.text.toString().trim { it <= ' ' }
val anchorText = anchorInput.text.toString().trim { it <= ' ' }
link(linkText, anchorText)
})
if (linkFormatter.isUrlSelected()) {
builder.setNeutralButton(R.string.dialog_button_remove_link, { dialogInterface, i ->
removeLink()
})
}
builder.setNegativeButton(R.string.dialog_button_cancel, { dialogInterface, i ->
dialogInterface.dismiss()
})
addLinkDialog = builder.create()
addLinkDialog!!.show()
}
//Custom input connection is used to detect the press of backspace when no characters are deleted
//(eg. at 0 index of EditText)
override fun onCreateInputConnection(outAttrs: EditorInfo): InputConnection {
return AztecInputConnection(super.onCreateInputConnection(outAttrs), true)
}
private inner class AztecInputConnection(target: InputConnection, mutable: Boolean) : InputConnectionWrapper(target, mutable) {
override fun sendKeyEvent(event: KeyEvent): Boolean {
if (event.action === KeyEvent.ACTION_DOWN && event.keyCode === KeyEvent.KEYCODE_DEL) {
history.beforeTextChanged(toFormattedHtml())
val isStyleRemoved = blockFormatter.tryRemoveBlockStyleFromFirstLine()
if (isStyleRemoved) {
history.handleHistory(this@AztecText)
return false
}
}
return super.sendKeyEvent(event)
}
override fun deleteSurroundingText(beforeLength: Int, afterLength: Int): Boolean {
if (beforeLength == 1 && afterLength == 0) {
return sendKeyEvent(KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL)) && sendKeyEvent(KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DEL))
}
return super.deleteSurroundingText(beforeLength, afterLength)
}
}
}