Skip to content
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

Allow loading of the editor with non-block content #205

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Open
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
20 changes: 19 additions & 1 deletion src/editor/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,24 @@ function RemoveBlockTypes() {
return null;
}

/**
* Initial content loader. Determine if the textarea contains blocks or raw HTML
*
* @param {string} content Text area content
* @param {*} parser Gutenberg `parse` function
* @param {*} rawHandler Gutenberg `rawHandler` function
*/
function onLoad( content, parser, rawHandler ) {
// Does the content contain blocks?
if ( content.indexOf( '<!-- wp:' ) !== -1 ) {
// Parse the blocks
return parser( content );
}

// Raw HTML - do our best
return rawHandler( { HTML: content } );
}

function createEditorContainer( container, textarea, settings ) {
const root = createRoot( container );

Expand All @@ -75,7 +93,7 @@ function createEditorContainer( container, textarea, settings ) {
<IsolatedBlockEditor
settings={ settings }
onSaveContent={ ( content ) => saveBlocks( textarea, content ) }
onLoad={ ( parser ) => ( textarea && textarea.nodeName === 'TEXTAREA' ? parser( textarea.value ) : [] ) }
onLoad={ ( parser, rawHandler ) => onLoad( ( textarea && textarea.nodeName === 'TEXTAREA' ? textarea.value : '' ), parser, rawHandler ) }
onError={ () => document.location.reload() }
__experimentalOnInput={ ( newBlocks ) => settings?.iso.__experimentalOnInput?.( newBlocks ) }
__experimentalOnChange={ ( newBlocks ) => settings?.iso.__experimentalOnChange?.( newBlocks ) }
Expand Down