Skip to content

Conversation

@kean
Copy link
Contributor

@kean kean commented Oct 27, 2025

What?

How?

I used onFilesDrop from use-on-block-drop component as a reference. It uses "transforms" to create blocks and then uses the existing insertion logic introduced in #199 to insert the blocks created by transforms.

This implementation should make sure that we support any media types, any transforms including third-party block ones, and any insertion points including nested blocks.

Testing Instructions

  • Verify that if you select one image, a core/image block is added and the item is uploaded to the remote
  • Verify that if you select multiple images, a gallery is created
  • Verify that if you select a video, a video is uploaded (note: should use videopress on Dotcom sites; untested)
  • Verify that media blocks are inserted at the insertion points, including insertion points in nested blocks

Note: I focused on making the implementation simple and sound and plan to test it and fix any outstanding issues in future iterations.

Screenshots or screencast

Simulator.Screen.Recording.-.iPhone.17.Pro.-.2025-10-28.at.15.48.44.mov

Note: the first frame in the random video I uploaded is black, which is why there is no preview

}

func webView(_ webView: WKWebView, stop urlSchemeTask: WKURLSchemeTask) {
// Nothing to do here for simple file serving
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You probably want to store the task as a member of this class and cancel it from this method?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a nearly-instant operation. It will never be cancelled in flight in practice.

Comment on lines 34 to 35
try? fileManager.createDirectory(at: self.rootURL, withIntermediateDirectories: true)
try? fileManager.createDirectory(at: self.uploadsDirectory, withIntermediateDirectories: true)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If these silently fail saveMediaData won't work right.

Probably best to just run these on every call to saveMediaData if needed – makes the error handling way more straightforward.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea; updated.

@kean kean force-pushed the task/add-photos-kit-and-camera branch 2 times, most recently from 4fbade0 to 611ac1f Compare October 28, 2025 19:52
@kean kean changed the title Native Inserter: Add Photos and Camera integration Native Inserter: Add initial Photos integration Oct 28, 2025
@kean kean changed the base branch from trunk to task/blocks-with-predefined-parameters October 28, 2025 20:06
for scheme in CachedAssetSchemeHandler.supportedURLSchemes {
config.setURLSchemeHandler(schemeHandler, forURLScheme: scheme)
}
config.setURLSchemeHandler(MediaFileSchemeHandler(), forURLScheme: MediaFileSchemeHandler.scheme)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the same approach we use for the assets. It's currently the only way to let WKWebView access the files on disk, but I hope we can simplify it in the future by extracting the bundled editor on disk and pointing WKWebView to that directly and not the readonly bundle.


guard let data = try? JSONEncoder().encode(selection),
let string = String(data: data, encoding: .utf8) else {
debugPrint("Failed to serialize media array to JSON")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should never happen.

onClick={ ( e ) => {
e.preventDefault();

const blocks = serializeBlocksForNative( inserterItems );
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the fix for CMM-903: Performance: Inserter recomputes the list of blocks on every change in the editor. Instead of useEffect, it now serializes blocks only when it's needed.

Ideally, I would also ensure useInsertionPoint isn't called so frequently, but it could be done later.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good. We can revisit other memoization ideas later. Maybe we can further reduce useInsertionPoint invocations.

);

if ( ! transformation ) {
debug( 'insertMedia: No matching transform found', {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I focused only on the happy path and opened CMM-910: Add error handling for media insertion

@kean kean force-pushed the task/add-photos-kit-and-camera branch from 611ac1f to fd21214 Compare October 28, 2025 20:12
@kean kean requested review from dcalhoun and jkmassel October 28, 2025 20:15
@kean kean marked this pull request as ready for review October 28, 2025 20:15
@kean kean force-pushed the task/add-photos-kit-and-camera branch from fd21214 to 78df467 Compare October 28, 2025 20:17
Base automatically changed from task/blocks-with-predefined-parameters to task/insert-block October 29, 2025 19:20
Base automatically changed from task/insert-block to trunk October 29, 2025 19:25
@kean kean force-pushed the task/add-photos-kit-and-camera branch 2 times, most recently from 0ccfd38 to 728c04b Compare October 29, 2025 19:45
@kean kean force-pushed the task/add-photos-kit-and-camera branch from 728c04b to 36b9906 Compare October 29, 2025 19:46
Copy link
Member

@dcalhoun dcalhoun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Selecting media feels very satisfying. Nicely done.

onClick={ ( e ) => {
e.preventDefault();

const blocks = serializeBlocksForNative( inserterItems );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good. We can revisit other memoization ideas later. Maybe we can further reduce useInsertionPoint invocations.

@kean kean enabled auto-merge (squash) October 30, 2025 12:49
@kean kean merged commit e96f2d0 into trunk Oct 30, 2025
11 checks passed
@kean kean deleted the task/add-photos-kit-and-camera branch October 30, 2025 12:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants