-
Notifications
You must be signed in to change notification settings - Fork 3
Add native block inserter #162
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: trunk
Are you sure you want to change the base?
Conversation
b2f92e3
to
1dac78c
Compare
@@ -5,7 +5,7 @@ import PackageDescription | |||
|
|||
let package = Package( | |||
name: "GutenbergKit", | |||
platforms: [.iOS(.v15), .macOS(.v14)], | |||
platforms: [.iOS(.v16), .macOS(.v14)], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I used some iOS 16 APIs. The apps require iOS 16, so we are good.
@@ -39,6 +41,11 @@ struct ContentView: View { | |||
} | |||
} | |||
} | |||
.fullScreenCover(isPresented: $isShowingDefaultEditor) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed it to show modally as in the app.
/// Enable native block inserter UI. | ||
public var enableNativeBlockInserter = false | ||
/// Auto-focus the editor when it loads. | ||
public var autoFocusOnLoad = true |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Noting that I enabled it by default. It auto-focuses the editor as you open it.
@@ -43,6 +43,22 @@ $min-touch-target-size: 46px; | |||
color: wordpress.$white; | |||
} | |||
|
|||
// Style the add block button with rounded black background | |||
.gutenberg-kit-editor-toolbar .gutenberg-kit-add-block-button { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I took the library to make it a bit less dramatic. The new button harmonizes better with the rest of the controls. There is no functional reason for it to be black. It's already in a toolbar as the first item – you ca't miss it.
// Continuously update the saved insertion point whenever selection changes | ||
useEffect( () => { | ||
// Only update if we have a selected block OR if we're clearing selection but already have a saved point | ||
if ( selectedBlockClientId !== null ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code was generated. I didn't review it and I need help verifying it. I think the insertion ends up being off by one.
* @param {Array} mediaItems Array of MediaInfo entities with { id, url, type, title, caption, alt, metadata } | ||
* @return {Promise<void>} | ||
*/ | ||
export async function insertMediaFromFiles( mediaItems ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey, @dcalhoun, what you think is the best way to programmatically insert image/video/file blocks and trigger the upload? I got the following working: insert the block at the cursor position, focus it, attach images. I failed to get the uploads working.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From researching quickly, there is a mediaUpload
setting available in the block editor store. It provides a utility for uploading media. It appears most media blocks (image, video, etc) rely upon this utility via the media placeholder component.
I was able to successfully upload an arbitrary image file with this utility. Here is a diff of an arbitrary example in GutenbergKit:
diff --git a/src/components/visual-editor/index.jsx b/src/components/visual-editor/index.jsx
index b4b7f9f..89ca661 100644
--- a/src/components/visual-editor/index.jsx
+++ b/src/components/visual-editor/index.jsx
@@ -13,9 +13,10 @@ import {
store as blockEditorStore,
} from '@wordpress/block-editor';
import { store as editorStore, PostTitle } from '@wordpress/editor';
-import { useSelect } from '@wordpress/data';
+import { useSelect, dispatch, select } from '@wordpress/data';
import { store as editPostStore } from '@wordpress/edit-post';
import '@wordpress/format-library';
+import { store as mediaStore } from '@wordpress/upload-media';
/**
* Internal dependencies
@@ -142,6 +143,19 @@ function VisualEditor( { hideTitle } ) {
) }
</>
) }
+ <form
+ onSubmit={ ( e ) => {
+ e.preventDefault();
+ const formData = new FormData( e.target );
+ const files = formData.getAll( 'files' );
+ select( blockEditorStore )
+ .getSettings()
+ .mediaUpload( { filesList: files } );
+ } }
+ >
+ <input type="file" name="files" multiple />
+ <button type="submit">Upload</button>
+ </form>
{ ! hideTitle && (
<div className={ titleClasses }>
<PostTitle ref={ editorPostTitleRef } />
I'm not sure what the url
value of mediaItems
resembles within the insertMediaFromFiles
, but presumably we could leverage the mediaItems
data to upload the files with mediaUpload
?
What?
Implements a native iOS block inserter UI to replace the web-based block inserter in the editor.
Why?
< see the respective P2 post >
How?
BlockInserterView
) that provides a searchable,categorized view of available blocks with first-class media support and native API integration
SearchEngine
) that supports fuzzy matching and scoring for blockdiscovery
Testing Instructions
Important
enableNativeBlockInserter
(enabled in demo)Integration PR in wpios: wordpress-mobile/WordPress-iOS#24708
enableNativeBlockInserter: true
in the editorconfiguration
Missing
gbk-local
URL scheme to allow the WKWebView to access the uploaded files from disk, and added basic image/gallery/block/insertion. I didn't add the code to trigger the actual upload – couldn't figure it out.core/list-item
– do not show them?Screenshot
Demo
demo.mov