@@ -448,21 +448,39 @@ private void toggleHtmlMode(final ToggleButton toggleButton) {
448448 updateFormatBarEnabledState (true );
449449
450450 if (toggleButton .isChecked ()) {
451- mSourceViewTitle .setText (getTitle ());
451+ Thread thread = new Thread (new Runnable () {
452+ @ Override
453+ public void run () {
454+ // Update mTitle and mContentHtml with the latest state from the ZSSEditor
455+ getTitle ();
456+ getContent ();
457+
458+ getActivity ().runOnUiThread (new Runnable () {
459+ @ Override
460+ public void run () {
461+ // Set HTML mode state
462+ mSourceViewTitle .setText (mTitle );
452463
453- SpannableString spannableContent = new SpannableString (getContent () );
454- HtmlStyleUtils .styleHtmlForDisplay (spannableContent );
455- mSourceViewContent .setText (spannableContent );
464+ SpannableString spannableContent = new SpannableString (mContentHtml );
465+ HtmlStyleUtils .styleHtmlForDisplay (spannableContent );
466+ mSourceViewContent .setText (spannableContent );
456467
457- mWebView .setVisibility (View .GONE );
458- mSourceView .setVisibility (View .VISIBLE );
468+ mWebView .setVisibility (View .GONE );
469+ mSourceView .setVisibility (View .VISIBLE );
459470
460- mSourceViewContent .requestFocus ();
461- mSourceViewContent .setSelection (0 );
471+ mSourceViewContent .requestFocus ();
472+ mSourceViewContent .setSelection (0 );
473+
474+ InputMethodManager imm = ((InputMethodManager ) getActivity ()
475+ .getSystemService (Context .INPUT_METHOD_SERVICE ));
476+ imm .showSoftInput (mSourceViewContent , InputMethodManager .SHOW_IMPLICIT );
477+ }
478+ });
479+ }
480+ });
481+
482+ thread .start ();
462483
463- InputMethodManager imm = ((InputMethodManager ) getActivity ()
464- .getSystemService (Context .INPUT_METHOD_SERVICE ));
465- imm .showSoftInput (mSourceViewContent , InputMethodManager .SHOW_IMPLICIT );
466484 } else {
467485 mWebView .setVisibility (View .VISIBLE );
468486 mSourceView .setVisibility (View .GONE );
@@ -479,6 +497,60 @@ private void toggleHtmlMode(final ToggleButton toggleButton) {
479497 }
480498 }
481499
500+ private void displayLinkDialog () {
501+ final LinkDialogFragment linkDialogFragment = new LinkDialogFragment ();
502+ linkDialogFragment .setTargetFragment (this , LinkDialogFragment .LINK_DIALOG_REQUEST_CODE_ADD );
503+
504+ final Bundle dialogBundle = new Bundle ();
505+
506+ // Pass potential URL from user clipboard
507+ String clipboardUri = Utils .getUrlFromClipboard (getActivity ());
508+ if (clipboardUri != null ) {
509+ dialogBundle .putString (LinkDialogFragment .LINK_DIALOG_ARG_URL , clipboardUri );
510+ }
511+
512+ // Pass selected text to dialog
513+ if (mSourceView .getVisibility () == View .VISIBLE ) {
514+ // HTML mode
515+ mSelectionStart = mSourceViewContent .getSelectionStart ();
516+ mSelectionEnd = mSourceViewContent .getSelectionEnd ();
517+
518+ String selectedText = mSourceViewContent .getText ().toString ().substring (mSelectionStart , mSelectionEnd );
519+ dialogBundle .putString (LinkDialogFragment .LINK_DIALOG_ARG_TEXT , selectedText );
520+
521+ linkDialogFragment .setArguments (dialogBundle );
522+ linkDialogFragment .show (getFragmentManager (), LinkDialogFragment .class .getSimpleName ());
523+ } else {
524+ // Visual mode
525+ Thread thread = new Thread (new Runnable () {
526+ @ Override
527+ public void run () {
528+ mGetSelectedTextCountDownLatch = new CountDownLatch (1 );
529+ getActivity ().runOnUiThread (new Runnable () {
530+ @ Override
531+ public void run () {
532+ mWebView .execJavaScriptFromString (
533+ "ZSSEditor.execFunctionForResult('getSelectedTextToLinkify');" );
534+ }
535+ });
536+
537+ try {
538+ if (mGetSelectedTextCountDownLatch .await (1 , TimeUnit .SECONDS )) {
539+ dialogBundle .putString (LinkDialogFragment .LINK_DIALOG_ARG_TEXT , mJavaScriptResult );
540+ }
541+ } catch (InterruptedException e ) {
542+ AppLog .d (T .EDITOR , "Failed to obtain selected text from JS editor." );
543+ }
544+
545+ linkDialogFragment .setArguments (dialogBundle );
546+ linkDialogFragment .show (getFragmentManager (), LinkDialogFragment .class .getSimpleName ());
547+ }
548+ });
549+
550+ thread .start ();
551+ }
552+ }
553+
482554 @ Override
483555 public void onClick (View v ) {
484556 int id = v .getId ();
@@ -507,40 +579,7 @@ public void onClick(View v) {
507579
508580 ((ToggleButton ) v ).setChecked (false );
509581
510- LinkDialogFragment linkDialogFragment = new LinkDialogFragment ();
511- linkDialogFragment .setTargetFragment (this , LinkDialogFragment .LINK_DIALOG_REQUEST_CODE_ADD );
512-
513- Bundle dialogBundle = new Bundle ();
514-
515- // Pass potential URL from user clipboard
516- String clipboardUri = Utils .getUrlFromClipboard (getActivity ());
517- if (clipboardUri != null ) {
518- dialogBundle .putString (LinkDialogFragment .LINK_DIALOG_ARG_URL , clipboardUri );
519- }
520-
521- // Pass selected text to dialog
522- if (mSourceView .getVisibility () == View .VISIBLE ) {
523- // HTML mode
524- mSelectionStart = mSourceViewContent .getSelectionStart ();
525- mSelectionEnd = mSourceViewContent .getSelectionEnd ();
526-
527- String selectedText = mSourceViewContent .getText ().toString ().substring (mSelectionStart , mSelectionEnd );
528- dialogBundle .putString (LinkDialogFragment .LINK_DIALOG_ARG_TEXT , selectedText );
529- } else {
530- // Visual mode
531- mGetSelectedTextCountDownLatch = new CountDownLatch (1 );
532- mWebView .execJavaScriptFromString ("ZSSEditor.execFunctionForResult('getSelectedTextToLinkify');" );
533- try {
534- if (mGetSelectedTextCountDownLatch .await (1 , TimeUnit .SECONDS )) {
535- dialogBundle .putString (LinkDialogFragment .LINK_DIALOG_ARG_TEXT , mJavaScriptResult );
536- }
537- } catch (InterruptedException e ) {
538- AppLog .d (T .EDITOR , "Failed to obtain selected text from JS editor." );
539- }
540- }
541-
542- linkDialogFragment .setArguments (dialogBundle );
543- linkDialogFragment .show (getFragmentManager (), LinkDialogFragment .class .getSimpleName ());
582+ displayLinkDialog ();
544583 } else {
545584 if (v instanceof ToggleButton ) {
546585 onFormattingButtonClicked ((ToggleButton ) v );
0 commit comments