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

TinyMCE per block: Adding a simple embed block #181

Merged
merged 1 commit into from
Mar 6, 2017
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions shared/post-content.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ window.content = [
'<p>By shipping early and often you have the unique competitive advantage of hearing from real people what they think of your work, which in best case helps you anticipate market direction, and in worst case gives you a few people rooting for you that you can email when your team pivots to a new idea. Nothing can recreate the crucible of real usage.</p>',
'<!-- /wp -->',

'<!-- wp:embed -->',
'https://www.youtube.com/watch?v=Nl6U7UotA-M',
'<!-- wp:embed url:https://www.youtube.com/watch?v=Nl6U7UotA-M -->',
'<iframe width="560" height="315" src="//www.youtube.com/embed/Nl6U7UotA-M" frameborder="0" allowfullscreen></iframe>',
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@aduth I updated the markup because in static blocks, the markup should be the same as the output markup

Copy link
Member

Choose a reason for hiding this comment

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

@aduth I updated the markup because in static blocks, the markup should be the same as the output markup

Hmm, this could have an interesting impact on oEmbeds in core though. Currently a post will store a link like this in its plain form (just the URL, not the frame) and fetch+cache the resulting embed at the time of content being rendered. This can be desirable in some cases to allow the embed provider to change their markup if need be and have it be reflected after a site's embed cache expires.

Copy link
Contributor Author

@youknowriad youknowriad Mar 6, 2017

Choose a reason for hiding this comment

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

Well! I assume this is the behavior of "dynamic" blocks, so I'm not agains considering the embed as a dynamic block. Which will be our first example of dynamic blocks

'<!-- /wp -->'
].join( '' );
26 changes: 13 additions & 13 deletions tinymce-per-block/build/app.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions tinymce-per-block/src/assets/stylesheets/main.scss
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
@import '~blocks/heading-block/style';
@import '~blocks/text-block/style';
@import '~blocks/quote-block/style';
@import '~blocks/embed-block/style';
@import '~external/dashicons/style';
@import '~renderers/block/block-list/style';
@import '~renderers/html/html-editor/style';
Expand Down
19 changes: 19 additions & 0 deletions tinymce-per-block/src/blocks/embed-block/_style.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
.embed-block__form textarea, .embed-block__form .textarea-mirror {
margin-top: 10px;
width: 100%;
border: none;
font: inherit;
font-family: "Merriweather", serif;
font-weight: 300;
font-size: 14px;
color: $gray-dark-300;
resize: none;

&:focus {
outline: 0;
}
}

.embed-block__content iframe {
width: 100%;
}
51 changes: 51 additions & 0 deletions tinymce-per-block/src/blocks/embed-block/form.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* External dependencies
*/
import { createElement, Component } from 'wp-elements';

import { EnhancedInputComponent } from 'wp-blocks';
import BlockArrangement from 'controls/block-arrangement';
import { getEmbedHtmlFromUrl } from '../../utils/embed';

export default class EmbedBlockForm extends Component {
merge = () => {
this.props.remove();
};

bindInput = ( ref ) => {
this.input = ref;
};

render() {
const { block, isFocused, change, moveUp, moveDown, remove, focusConfig, focus } = this.props;

const removePrevious = () => {
if ( ! block.url ) {
remove();
}
};

const html = getEmbedHtmlFromUrl( block.url );
console.log( html );

return (
<div>
{ isFocused && <BlockArrangement block={ block } /> }
<div className="embed-block__form">
<div className="embed-block__content" dangerouslySetInnerHTML={ { __html: html } } />
<EnhancedInputComponent
ref={ this.bindInput }
moveUp={ moveUp }
removePrevious={ removePrevious }
moveDown={ moveDown }
value={ block.url }
onChange={ ( value ) => change( { url: value } ) }
focusConfig={ focusConfig }
onFocusChange={ ( config ) => focus( config ) }
placeholder="Enter an embed URL"
/>
</div>
</div>
);
}
}
31 changes: 31 additions & 0 deletions tinymce-per-block/src/blocks/embed-block/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* External dependencies
*/
import { registerBlock } from 'wp-blocks';
import { VideoAlt3Icon } from 'dashicons';

/**
* Internal dependencies
*/
import form from './form';
import { getEmbedHtmlFromUrl } from '../../utils/embed';

registerBlock( 'embed', {
title: 'Embed',
form: form,
icon: VideoAlt3Icon,
parse: ( rawBlock ) => {
console.log( rawBlock );
return {
blockType: 'embed',
url: rawBlock.attrs.url,
};
},
serialize: ( block ) => {
return {
blockType: 'embed',
attrs: { url: block.url },
rawContent: getEmbedHtmlFromUrl( block.url )
};
}
} );
8 changes: 4 additions & 4 deletions tinymce-per-block/src/blocks/image-block/_style.scss
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.image-caption-block {
.image-block__form {
position: relative;

&.align-full-width {
Expand Down Expand Up @@ -26,13 +26,13 @@
}


.image-caption-block__display {
.image-block__display {
display: block;
max-width: 100%;
}

.image-caption-block__caption textarea,
.image-caption-block__caption .textarea-mirror {
.image-block__caption textarea,
.image-block__caption .textarea-mirror {
margin-top: 10px;
width: 100%;
border: none;
Expand Down
6 changes: 3 additions & 3 deletions tinymce-per-block/src/blocks/image-block/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export default class ImageBlockForm extends Component {
const alignValue = block.align || 'no-align';

return (
<div className={ classNames( 'image-caption-block', alignValue ) }>
<div className={ classNames( 'image-block__form', alignValue ) }>
{ isFocused && <BlockArrangement block={ block } /> }
{ isFocused &&
<div className="block-list__block-controls">
Expand All @@ -71,12 +71,12 @@ export default class ImageBlockForm extends Component {
}
<img
src={ block.src }
className="image-caption-block__display"
className="image-block__display"
onClick={ () => {
! isFocused && focus();
} }
/>
<div className="image-caption-block__caption">
<div className="image-block__caption">
<EnhancedInputComponent
ref={ this.bindCaption }
moveUp={ moveUp }
Expand Down
13 changes: 13 additions & 0 deletions tinymce-per-block/src/external/dashicons/icons/video-alt3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* External dependencies
*/
import { createElement } from 'wp-elements';

/**
* Internal dependencies
*/
import createDashicon from '../create-dashicon';

export default createDashicon(
<path d="M19 15v-10c0-1.1-0.9-2-2-2h-13c-1.1 0-2 0.9-2 2v10c0 1.1 0.9 2 2 2h13c1.1 0 2-0.9 2-2zM8 14v-8l6 4z"></path>
);
1 change: 1 addition & 0 deletions tinymce-per-block/src/external/dashicons/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ export { default as ImageNoAlignIcon } from './icons/image-align-left';
export { default as ImageAlignLeftIcon } from './icons/image-align-left';
export { default as ImageAlignRightIcon } from './icons/image-align-right';
export { default as ImageFullWidthIcon } from './icons/image-full-width';
export { default as VideoAlt3Icon } from './icons/video-alt3';
2 changes: 2 additions & 0 deletions tinymce-per-block/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import 'blocks/image-block';
import 'blocks/heading-block';
import 'blocks/text-block';
import 'blocks/quote-block';
import 'blocks/embed-block';

class App extends Component {
state = {
Expand All @@ -43,6 +44,7 @@ class App extends Component {
} );
return;
case 'html':
console.log( parsers.block.parse( nextContent ) );
this.setState( {
content: {
block: parsers.block.parse( nextContent )
Expand Down
54 changes: 35 additions & 19 deletions tinymce-per-block/src/parsers/block/grammar.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions tinymce-per-block/src/utils/embed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export const getEmbedHtmlFromUrl = ( url ) => {
const getYoutubeId = ( youtubeUrl ) => {
const regExp = /^.*(youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=|\&v=)([^#\&\?]*).*/;
const match = youtubeUrl.match( regExp );

if ( match && match[ 2 ].length === 11 ) {
return match[ 2 ];
}

return false;
};

const youtubeId = getYoutubeId( url );
return youtubeId
? '<iframe width="560" height="315" src="//www.youtube.com/embed/' +
youtubeId + '" frameborder="0" allowfullscreen></iframe>'
: '';
};