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 drag to different page #3312

Merged
merged 37 commits into from
Oct 4, 2019
Merged
Changes from 1 commit
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
6e37b45
Allow drag to different page
barklund Sep 20, 2019
ba20251
Refactor duplicated logic
barklund Sep 20, 2019
354443b
Remove unnecessary hook
barklund Sep 20, 2019
40259f8
More performant structure
barklund Sep 23, 2019
9fd43c3
Better separation of concerns
barklund Sep 23, 2019
305b692
Handle CTA blocks correctly when dragging between pages
barklund Sep 23, 2019
f7d0c19
Fix proptypes and add inline comment
barklund Sep 23, 2019
83b2e4f
Fix offset bug when dragging 2+ pages left
barklund Sep 23, 2019
3928f60
Adding documentation for functions
barklund Sep 24, 2019
efd22fd
Fix disallow duplicate CTA
barklund Sep 24, 2019
95ca6c8
Generalised helper for checking if block is allowed on given page
barklund Sep 26, 2019
6109bec
Refactored `isCTABlock` to helpers
barklund Sep 26, 2019
b3be9f5
Rename variable and move to constants
barklund Sep 27, 2019
0395f30
Merge branch 'develop' into feature/3311-allow-drag-to-page
barklund Sep 27, 2019
04fffea
Make props required where applicable
barklund Sep 27, 2019
3c7afcf
Reorganised helper to aid in testing
barklund Sep 27, 2019
aee7550
Fix typos
barklund Sep 27, 2019
64f341d
Reuse `STORY_PAGE_MARGIN` where applicable
barklund Sep 27, 2019
5160c2f
Missing rewrite to `isCTABlock` fixed
barklund Sep 27, 2019
9bcb884
Fix minor errors in rewrites
barklund Sep 27, 2019
7ee41f3
Add `aria-grabbed` to the block mover
barklund Sep 27, 2019
3b8a139
Revert "Add `aria-grabbed` to the block mover"
barklund Sep 27, 2019
4f4f619
Fix test wording
barklund Sep 30, 2019
d1813e1
Updated component documentation
barklund Oct 1, 2019
c3e51f4
Updated logic to display non-allowed drop zones
barklund Oct 1, 2019
4041030
Add border on allowed regions instead (and fix css lint)
barklund Oct 1, 2019
1cde15c
Major rewrite of entire implementation
barklund Oct 2, 2019
3f13272
Merge branch 'develop' into feature/3311-allow-drag-to-page
barklund Oct 3, 2019
e963844
Convert helper function to hook
barklund Oct 3, 2019
3241e3b
Added dependencies for `useSelect`
barklund Oct 3, 2019
005bac0
Created new constant for the height of the drop zone for CTAs
barklund Oct 3, 2019
354ee52
Updated `useMoveBlockToPage` and added tests
barklund Oct 3, 2019
8f068be
Fix proptype missed in refactor
barklund Oct 3, 2019
d5eb6a8
Merge branch 'develop' into feature/3311-allow-drag-to-page
swissspidy Oct 4, 2019
144c430
Refactor MediaInserter to use hooks
swissspidy Oct 4, 2019
60f0198
Move isBlockAllowedOnPage function inline
swissspidy Oct 4, 2019
9bd9ab3
Remove ifCondition HOC usage from stories editor
swissspidy Oct 4, 2019
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
119 changes: 54 additions & 65 deletions assets/src/stories-editor/components/media-inserter/index.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,71 @@
/**
spacedmonkey marked this conversation as resolved.
Show resolved Hide resolved
* External dependencies
*/
import PropTypes from 'prop-types';

/**
* WordPress dependencies
*/
import { getBlockType, createBlock } from '@wordpress/blocks';
import { BlockIcon } from '@wordpress/block-editor';
import { withDispatch, withSelect } from '@wordpress/data';
import { withSelect, useSelect, useDispatch } from '@wordpress/data';
import { useCallback } from '@wordpress/element';
import { DropdownMenu } from '@wordpress/components';
import { compose, ifCondition } from '@wordpress/compose';
import { __, sprintf } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import { processMedia, isBlockAllowedOnPage } from '../../helpers';
import {
IMAGE_BACKGROUND_TYPE,
} from '../../constants';
import { processMedia, useIsBlockAllowedOnPage } from '../../helpers';
import { IMAGE_BACKGROUND_TYPE } from '../../constants';

const POPOVER_PROPS = {
position: 'bottom right',
};

const MediaInserter = ( { insertBlock, updateBlock, canInsertBlockType, showInserter, mediaType, allowedVideoMimeTypes } ) => {
const MediaInserter = () => {
const {
currentPage,
blockOrder,
showInserter,
mediaType,
allowedVideoMimeTypes,
} = useSelect( ( select ) => {
const { getCurrentPage } = select( 'amp/story' );
const { getBlock, getBlockOrder } = select( 'core/block-editor' );
const { getSettings } = select( 'amp/story' );

const _currentPage = getCurrentPage();
const block = getBlock( _currentPage );

return {
currentPage: _currentPage,
blockOrder: getBlockOrder( _currentPage ),
// As used in <HeaderToolbar> component
showInserter: select( 'core/edit-post' ).getEditorMode() === 'visual' && select( 'core/editor' ).getEditorSettings().richEditingEnabled,
mediaType: block && block.attributes.mediaType ? block.attributes.mediaType : '',
allowedVideoMimeTypes: getSettings().allowedVideoMimeTypes,
};
}, [] );

const isBlockAllowedOnPage = useIsBlockAllowedOnPage();

const { insertBlock, updateBlockAttributes, selectBlock } = useDispatch( 'core/block-editor' );

const insertBlockOnPage = useCallback( ( name ) => {
const index = blockOrder.length;

const insertedBlock = createBlock( name, {} );

insertBlock( insertedBlock, index, currentPage );
}, [ blockOrder, currentPage, insertBlock ] );

const updateBlock = useCallback( ( media ) => {
if ( ! currentPage ) {
return;
}

const processed = processMedia( media );
updateBlockAttributes( currentPage, processed );
selectBlock( currentPage );
}, [ currentPage, selectBlock, updateBlockAttributes ] );

const blocks = [
'core/video',
'core/image',
Expand All @@ -50,14 +90,14 @@ const MediaInserter = ( { insertBlock, updateBlock, canInsertBlockType, showInse
];

for ( const block of blocks ) {
if ( ! canInsertBlockType( block ) ) {
if ( ! isBlockAllowedOnPage( block, currentPage ) ) {
continue;
}

const blockType = getBlockType( block );
const item = {
title: sprintf( __( 'Insert %s', 'amp' ), blockType.title ),
onClick: () => insertBlock( block ),
onClick: () => insertBlockOnPage( block ),
disabled: ! showInserter,
icon: <BlockIcon icon={ blockType.icon } />,
};
Expand All @@ -80,15 +120,6 @@ const MediaInserter = ( { insertBlock, updateBlock, canInsertBlockType, showInse
);
};

MediaInserter.propTypes = {
insertBlock: PropTypes.func.isRequired,
updateBlock: PropTypes.func.isRequired,
canInsertBlockType: PropTypes.func.isRequired,
showInserter: PropTypes.bool.isRequired,
mediaType: PropTypes.string.isRequired,
allowedVideoMimeTypes: PropTypes.arrayOf( PropTypes.string ).isRequired,
};

const mediaPicker = ( dialogTitle, mediaType, updateBlock ) => {
// Create the media frame.
const fileFrame = wp.media( {
Expand All @@ -114,56 +145,14 @@ const mediaPicker = ( dialogTitle, mediaType, updateBlock ) => {
};

const applyWithSelect = withSelect( ( select ) => {
const { getCurrentPage } = select( 'amp/story' );
const { getBlock } = select( 'core/block-editor' );
const { isReordering, getSettings } = select( 'amp/story' );

const currentPage = getCurrentPage();
const block = getBlock( currentPage );
const mediaType = block && block.attributes.mediaType ? block.attributes.mediaType : '';
const { allowedVideoMimeTypes } = getSettings();
const { isReordering } = select( 'amp/story' );

return {
isReordering: isReordering(),
canInsertBlockType: ( name ) => isBlockAllowedOnPage( name, currentPage ),
// As used in <HeaderToolbar> component
showInserter: select( 'core/edit-post' ).getEditorMode() === 'visual' && select( 'core/editor' ).getEditorSettings().richEditingEnabled,
mediaType,
allowedVideoMimeTypes,
};
} );

const applyWithDispatch = withDispatch( ( dispatch, props, { select } ) => {
const { getCurrentPage } = select( 'amp/story' );
const { getBlockOrder } = select( 'core/block-editor' );
const { insertBlock } = dispatch( 'core/block-editor' );

return {
insertBlock: ( name ) => {
const currentPage = getCurrentPage();
const index = getBlockOrder( currentPage ).length;

const insertedBlock = createBlock( name, {} );

insertBlock( insertedBlock, index, currentPage );
},
updateBlock: ( media ) => {
const clientId = getCurrentPage();
const { updateBlockAttributes, selectBlock } = dispatch( 'core/block-editor' );

if ( ! clientId ) {
return;
}

const processed = processMedia( media );
updateBlockAttributes( clientId, processed );
selectBlock( clientId );
},
};
} );

export default compose(
applyWithSelect,
applyWithDispatch,
ifCondition( ( { isReordering } ) => ! isReordering ),
swissspidy marked this conversation as resolved.
Show resolved Hide resolved
)( MediaInserter );