Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export function* setupEditor( post, edits, template ) {
// In order to ensure maximum of a single parse during setup, edits are
// included as part of editor setup action. Assume edited content as
// canonical if provided, falling back to post.
let content;
if ( has( edits, [ 'content' ] ) ) {
content = edits.content;
} else {
content = post.content.raw;
}
let blocks = parse( content );
// Apply a template for new posts only, if exists.
const isNewPost = post.status === 'auto-draft';
if ( isNewPost && template ) {
blocks = synchronizeBlocksWithTemplate( blocks, template );
}
yield resetPost( post );
yield {
type: 'SETUP_EDITOR',
post,
edits,
template,
};
yield resetEditorBlocks( blocks, { __unstableShouldCreateUndoLevel: false } );
yield setupEditorState( post );
refresh: ! state.refresh,
};
}
case ActionTypes.BLOCK.CREATE: {
// TODO we need to set focused: true and search for the currently focused block and
// set that one to `focused: false`.
const index = insertBlock( blocks, action.block, action.clientIdAbove );
state.listAnimator.splice( index, 0, action.block );
return {
...state,
blocks: blocks,
refresh: ! state.refresh,
};
}
case ActionTypes.BLOCK.PARSE: {
const parsed = parse( action.html );
return {
...state,
blocks: parsed,
listAnimator: newListAnimator( parsed ),
refresh: ! state.refresh,
};
}
default:
return state;
}
};
constructor( props: PropsType ) {
super( props );
const post = props.post || {
id: 1,
content: {
raw: '',// props.initialHtml,
},
type: 'draft',
};
this.props.setupEditor( post );
this.lastHtml = serialize( parse( props.initialHtml ) );
}
SETUP_EDITOR( action ) {
const { post, settings } = action;
// Parse content as blocks
let blocks;
let isValidTemplate = true;
if ( post.content.raw ) {
blocks = parse( post.content.raw );
// Unlocked templates are considered always valid because they act as default values only.
isValidTemplate = (
! settings.template ||
settings.templateLock !== 'all' ||
doBlocksMatchTemplate( blocks, settings.template )
);
} else if ( settings.template ) {
blocks = synchronizeBlocksWithTemplate( [], settings.template );
} else if ( getDefaultBlockForPostFormat( post.format ) ) {
blocks = [ createBlock( getDefaultBlockForPostFormat( post.format ) ) ];
} else {
blocks = [];
}
// Include auto draft title in edits while not flagging post as dirty
( sharedBlock ) => ( {
sharedBlock,
parsedBlock: parse( sharedBlock.content )[ 0 ],
} )
) ) );
updateHtmlAction( html ) {
const parsed = parse( html );
this.props.resetEditorBlocksWithoutUndoLevel( parsed );
}
updateHtmlAction( html: string = '' ) {
const parsed = parse( html );
this.props.resetEditorBlocksWithoutUndoLevel( parsed );
}
const initialBlocks = useMemo( () => {
const parsedContent = parse( content );
return parsedContent.length ? parsedContent : undefined;
}, [] );
const [ blocks = initialBlocks, setBlocks ] = useEntityProp(
updateHtmlAction( html: string = '' ) {
const parsed = parse( html );
this.props.resetEditorBlocksWithoutUndoLevel( parsed );
}
function onLoadRevision( message ) {
const action = get( message, 'data.action', '' );
const payload = get( message, 'data.payload', null );
if ( action === 'loadRevision' && payload ) {
const blocks = parse( payload.content );
dispatch( 'core/editor' ).editPost( payload );
dispatch( 'core/editor' ).resetBlocks( blocks );
dispatch( 'core/notices' ).removeNotice( 'autosave-exists' );
calypsoPort.removeEventListener( 'message', onLoadRevision, false );
}
}
}