Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ let package = Package(
targets: [
.target(
name: "GutenbergKit",
dependencies: ["SwiftSoup", "SVGView", "GutenbergKitResources"],
dependencies: ["SwiftSoup", "SVGView", "GutenbergKitResources", "GutenbergKitHTTP"],
path: "ios/Sources/GutenbergKit",
exclude: ["Gutenberg"],
packageAccess: false
Expand Down
1 change: 1 addition & 0 deletions android/Gutenberg/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ dependencies {
implementation(libs.jsoup)
implementation(libs.okhttp)

testImplementation(libs.json)
testImplementation(libs.junit)
testImplementation(kotlin("test"))
testImplementation(libs.kotlinx.coroutines.test)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,25 @@ class GutenbergView : WebView {

var requestInterceptor: GutenbergRequestInterceptor = DefaultGutenbergRequestInterceptor()

/** Optional delegate for customizing media upload behavior (resize, transcode, custom upload). */
var mediaUploadDelegate: MediaUploadDelegate? = null
set(value) {
if (field === value) return
field = value
// Stop any previously running server before starting a new one.
uploadServer?.stop()
uploadServer = null
// (Re)start the upload server so it captures the delegate.
// This handles the common case where the delegate is set after
// construction but before the editor finishes loading.
if (value != null) {
startUploadServer()
}
}

private var uploadServer: MediaUploadServer? = null
private val uploadHttpClient: okhttp3.OkHttpClient by lazy { okhttp3.OkHttpClient() }

private var onFileChooserRequested: ((Intent, Int) -> Unit)? = null
private var contentChangeListener: ContentChangeListener? = null
private var historyChangeListener: HistoryChangeListener? = null
Expand Down Expand Up @@ -441,7 +460,12 @@ class GutenbergView : WebView {
}

private fun setGlobalJavaScriptVariables() {
val gbKit = GBKitGlobal.fromConfiguration(configuration, dependencies)
val gbKit = GBKitGlobal.fromConfiguration(
configuration,
dependencies,
nativeUploadPort = uploadServer?.port,
nativeUploadToken = uploadServer?.token
)
val gbKitJson = gbKit.toJsonString()
val gbKitConfig = """
window.GBKit = $gbKitJson;
Expand All @@ -452,6 +476,26 @@ class GutenbergView : WebView {
}


private fun startUploadServer() {
if (configuration.siteApiRoot.isEmpty() || configuration.authHeader.isEmpty()) return

try {
val defaultUploader = DefaultMediaUploader(
httpClient = uploadHttpClient,
siteApiRoot = configuration.siteApiRoot,
authHeader = configuration.authHeader,
siteApiNamespace = configuration.siteApiNamespace.toList()
)
uploadServer = MediaUploadServer(
uploadDelegate = mediaUploadDelegate,
defaultUploader = defaultUploader,
cacheDir = context.cacheDir
)
} catch (e: Exception) {
Log.w(TAG, "Failed to start upload server", e)
}
}

fun clearConfig() {
val jsCode = """
delete window.GBKit;
Expand Down Expand Up @@ -879,6 +923,8 @@ class GutenbergView : WebView {
override fun onDetachedFromWindow() {
super.onDetachedFromWindow()
stopNetworkMonitoring()
uploadServer?.stop()
uploadServer = null
clearConfig()
this.stopLoading()
FileCache.clearCache(context)
Expand Down Expand Up @@ -944,6 +990,8 @@ class GutenbergView : WebView {
}

companion object {
private const val TAG = "GutenbergView"

/** Hosts that are safe to serve assets over HTTP (local development only). */
private val LOCAL_HOSTS = setOf("localhost", "127.0.0.1", "10.0.2.2")

Expand Down
Loading
Loading