@@ -32,6 +32,7 @@ import org.wordpress.aztec.R
3232import org.wordpress.aztec.plugins.IMediaToolbarButton
3333import org.wordpress.aztec.plugins.IToolbarButton
3434import org.wordpress.aztec.source.SourceViewEditText
35+ import java.util.Arrays
3536import java.util.ArrayList
3637import java.util.Locale
3738
@@ -41,6 +42,9 @@ import java.util.Locale
4142 * Supports RTL layout direction on API 19+
4243 */
4344class 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