-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TinyMCE per block: Adding a simple embed block
- Loading branch information
1 parent
f88ad8a
commit 639b034
Showing
13 changed files
with
193 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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%; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ) | ||
}; | ||
} | ||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
tinymce-per-block/src/external/dashicons/icons/video-alt3.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>' | ||
: ''; | ||
}; |