Skip to content

Instantly share code, notes, and snippets.

@geriux
Created May 17, 2024 14:13
Show Gist options
  • Save geriux/1d2ea1a8d0272cbc118a476daa519f2f to your computer and use it in GitHub Desktop.
Save geriux/1d2ea1a8d0272cbc118a476daa519f2f to your computer and use it in GitHub Desktop.
package org.wordpress.android.editor.gutenberg
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import androidx.fragment.app.Fragment
import androidx.core.content.ContextCompat
import androidx.core.util.Pair
import com.facebook.react.ReactRootView
import org.wordpress.mobile.WPAndroidGlue.WPAndroidGlueCode
import org.wordpress.mobile.WPAndroidGlue.GutenbergProps;
import org.wordpress.mobile.WPAndroidGlue.WPAndroidGlueCode.OnGetContentInterrupted;
class GutenComments : Fragment() {
private lateinit var mWPAndroidGlueCode: WPAndroidGlueCode
private lateinit var mReactRootView: ReactRootView
private fun getDefaultGutenbergProps(): GutenbergProps {
// Set default values for GutenbergProps
return GutenbergProps(
isDarkMode = false,
enableContactInfoBlock = false,
enableLayoutGridBlock = false,
enableTiledGalleryBlock = false,
enableVideoPressBlock = false,
enableFacebookEmbed = false,
enableInstagramEmbed = false,
enableLoomEmbed = false,
enableSmartframeEmbed = false,
enableMediaFilesCollectionBlocks = false,
enableMentions = false,
enableXPosts = false,
enableUnsupportedBlockEditor = false,
enableSupportSection = false,
enableOnlyCoreBlocks = true,
canEnableUnsupportedBlockEditor = false,
isAudioBlockMediaUploadEnabled = false,
shouldUseFastImage = false,
enableReusableBlock = false,
localeSlug = "",
postType = "post",
hostAppNamespace = "Jetpack",
featuredImageId = 0,
editorTheme = null,
translations = null,
htmlModeEnabled = false
)
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
mWPAndroidGlueCode = WPAndroidGlueCode()
mWPAndroidGlueCode.onCreate(context)
mReactRootView = ReactRootView(context)
val layout = FrameLayout(requireContext()).apply {
layoutParams = FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
addView(mReactRootView, FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT))
}
val defaultGutenbergProps = getDefaultGutenbergProps()
mWPAndroidGlueCode.onCreateView(
context,
activity?.application,
true, // or true if in debug mode
ContextCompat.getColor(requireContext(), android.R.color.white), // Use ContextCompat for compatibility
null, // Exception logger
null, // Breadcrumb logger
defaultGutenbergProps // Gutenberg props
)
mWPAndroidGlueCode.attachToContainerLight(mReactRootView)
val appProps = GutenbergProps.Companion.initContent(mReactRootView.appProperties, "", "");
mReactRootView.startReactApplication(mWPAndroidGlueCode.reactInstanceManager, "gutenberg", appProps);
return layout
}
override fun onPause() {
super.onPause()
activity?.let { mWPAndroidGlueCode.onPause(it) }
}
override fun onResume() {
super.onResume()
activity?.let { mWPAndroidGlueCode.onResume(null, it) }
}
override fun onDestroyView() {
super.onDestroyView()
activity?.let { mWPAndroidGlueCode.onDestroy(it) }
}
fun setContent(content: String) {
mWPAndroidGlueCode.updateContent("", content);
}
fun getContent(): Pair<CharSequence, CharSequence>? {
return mWPAndroidGlueCode.getTitleAndContent("", object : OnGetContentInterrupted {
override fun onGetContentInterrupted(ie: InterruptedException) {
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment