Skip to content

Commit 7eeb866

Browse files
authored
Merge pull request #713 from wordpress-mobile/issue/711-update-if-change-zeros-out-original-change
Sync the editors on switch if change zeros out original change
2 parents 9e874ea + a973449 commit 7eeb866

File tree

3 files changed

+61
-7
lines changed

3 files changed

+61
-7
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package org.wordpress.aztec.demo.tests
2+
3+
import android.support.test.rule.ActivityTestRule
4+
import org.junit.Rule
5+
import org.junit.Test
6+
import org.wordpress.aztec.demo.BaseTest
7+
import org.wordpress.aztec.demo.MainActivity
8+
import org.wordpress.aztec.demo.pages.EditorPage
9+
10+
class BasicTextEditingTests : BaseTest() {
11+
12+
@Rule
13+
@JvmField
14+
var mActivityTestRule = ActivityTestRule(MainActivity::class.java)
15+
16+
// Test reproducing the issue described in https://github.com/wordpress-mobile/AztecEditor-Android/issues/711
17+
@Test
18+
fun testEditOutChangesSwitch() {
19+
EditorPage()
20+
.insertText("text")
21+
.toggleHtml()
22+
.toggleHtml()
23+
.clearText()
24+
.toggleHtml()
25+
.verifyHTML("")
26+
}
27+
28+
}

aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
163163
}
164164

165165
@Throws(NoSuchAlgorithmException::class)
166-
private fun calculateSHA256(s: String): ByteArray {
166+
fun calculateSHA256(s: String): ByteArray {
167167
val digest = MessageDigest.getInstance("SHA-256")
168168
digest.update(s.toByteArray())
169169
return digest.digest()

aztec/src/main/kotlin/org/wordpress/aztec/toolbar/AztecToolbar.kt

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import org.wordpress.aztec.R
3232
import org.wordpress.aztec.plugins.IMediaToolbarButton
3333
import org.wordpress.aztec.plugins.IToolbarButton
3434
import org.wordpress.aztec.source.SourceViewEditText
35+
import java.util.Arrays
3536
import java.util.ArrayList
3637
import java.util.Locale
3738

@@ -41,6 +42,9 @@ import java.util.Locale
4142
* Supports RTL layout direction on API 19+
4243
*/
4344
class AztecToolbar : FrameLayout, IAztecToolbar, OnMenuItemClickListener {
45+
val RETAINED_EDITOR_HTML_PARSED_SHA256_KEY = "RETAINED_EDITOR_HTML_PARSED_SHA256_KEY"
46+
val RETAINED_SOURCE_HTML_PARSED_SHA256_KEY = "RETAINED_SOURCE_HTML_PARSED_SHA256_KEY"
47+
4448
private var aztecToolbarListener: IAztecToolbarClickListener? = null
4549
private var editor: AztecText? = null
4650
private var headingMenu: PopupMenu? = null
@@ -53,6 +57,9 @@ class AztecToolbar : FrameLayout, IAztecToolbar, OnMenuItemClickListener {
5357
private var isMediaToolbarVisible: Boolean = false
5458
private var isMediaModeEnabled: Boolean = false
5559

60+
var editorContentParsedSHA256LastSwitch: ByteArray = ByteArray(0)
61+
var sourceContentParsedSHA256LastSwitch: ByteArray = ByteArray(0)
62+
5663
private lateinit var toolbarScrolView: HorizontalScrollView
5764
private lateinit var buttonEllipsisCollapsed: RippleToggleButton
5865
private lateinit var buttonEllipsisExpanded: RippleToggleButton
@@ -346,6 +353,8 @@ class AztecToolbar : FrameLayout, IAztecToolbar, OnMenuItemClickListener {
346353
isMediaToolbarVisible = restoredState.getBoolean("isMediaToolbarVisible")
347354
setAdvancedState()
348355
setupMediaToolbar()
356+
editorContentParsedSHA256LastSwitch = restoredState.getByteArray(RETAINED_EDITOR_HTML_PARSED_SHA256_KEY)
357+
sourceContentParsedSHA256LastSwitch = restoredState.getByteArray(RETAINED_SOURCE_HTML_PARSED_SHA256_KEY)
349358
}
350359

351360
override fun onSaveInstanceState(): Parcelable {
@@ -356,6 +365,8 @@ class AztecToolbar : FrameLayout, IAztecToolbar, OnMenuItemClickListener {
356365
bundle.putBoolean("isMediaMode", isMediaModeEnabled)
357366
bundle.putBoolean("isExpanded", isExpanded)
358367
bundle.putBoolean("isMediaToolbarVisible", isMediaToolbarVisible)
368+
bundle.putByteArray(RETAINED_EDITOR_HTML_PARSED_SHA256_KEY, editorContentParsedSHA256LastSwitch)
369+
bundle.putByteArray(RETAINED_SOURCE_HTML_PARSED_SHA256_KEY, sourceContentParsedSHA256LastSwitch)
359370
savedState.state = bundle
360371
return savedState
361372
}
@@ -561,22 +572,37 @@ class AztecToolbar : FrameLayout, IAztecToolbar, OnMenuItemClickListener {
561572
}
562573
}
563574

575+
private fun syncSourceFromEditor() {
576+
val editorHtml = editor!!.toPlainHtml(true)
577+
val sha256 = AztecText.calculateSHA256(editorHtml)
578+
if (editor!!.hasChanges() != NO_CHANGES || !Arrays.equals(editorContentParsedSHA256LastSwitch, sha256)) {
579+
sourceEditor!!.displayStyledAndFormattedHtml(editorHtml)
580+
}
581+
editorContentParsedSHA256LastSwitch = sha256
582+
}
583+
584+
private fun syncEditorFromSource() {
585+
// temp var of the source html to load it to the editor if needed
586+
val sourceHtml = sourceEditor!!.getPureHtml(true)
587+
val sha256 = AztecText.calculateSHA256(sourceHtml)
588+
if (sourceEditor!!.hasChanges() != NO_CHANGES || !Arrays.equals(sourceContentParsedSHA256LastSwitch, sha256)) {
589+
editor!!.fromHtml(sourceHtml)
590+
}
591+
sourceContentParsedSHA256LastSwitch = sha256
592+
}
593+
564594
override fun toggleEditorMode() {
565595
// only allow toggling if sourceEditor is present
566596
if (sourceEditor == null) return
567597

568598
if (editor!!.visibility == View.VISIBLE) {
569-
if (editor!!.hasChanges() != NO_CHANGES) {
570-
sourceEditor!!.displayStyledAndFormattedHtml(editor!!.toPlainHtml(true))
571-
}
599+
syncSourceFromEditor()
572600
editor!!.visibility = View.GONE
573601
sourceEditor!!.visibility = View.VISIBLE
574602

575603
toggleHtmlMode(true)
576604
} else {
577-
if (sourceEditor!!.hasChanges() != NO_CHANGES) {
578-
editor!!.fromHtml(sourceEditor!!.getPureHtml(true))
579-
}
605+
syncEditorFromSource()
580606
editor!!.visibility = View.VISIBLE
581607
sourceEditor!!.visibility = View.GONE
582608

0 commit comments

Comments
 (0)