-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Try: Framework-agnostic block interoperability ("Vanilla", Vue)
- Loading branch information
Showing
8 changed files
with
312 additions
and
12 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
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,47 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { registerBlockType, source } from '../../api'; | ||
|
||
const { text } = source; | ||
|
||
registerBlockType( 'core/vanilla-banner', { | ||
title: __( 'Vanilla Banner' ), | ||
|
||
icon: 'marker', | ||
|
||
category: 'widgets', | ||
|
||
attributes: { | ||
text: { | ||
type: 'string', | ||
source: text( 'h1' ), | ||
default: 'Hello World', | ||
}, | ||
}, | ||
|
||
className: false, | ||
|
||
edit( { attributes, setAttributes } ) { | ||
return [ 'div', | ||
[ 'input', { | ||
value: attributes.text, | ||
onChange( event ) { | ||
setAttributes( { | ||
text: event.target.value, | ||
} ); | ||
}, | ||
} ], | ||
[ 'h1', attributes.text ], | ||
]; | ||
}, | ||
|
||
save( { attributes } ) { | ||
return [ 'h1', attributes.text ]; | ||
}, | ||
} ); |
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,78 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import Vue from 'vue/dist/vue'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { registerBlockType, source } from '../../api'; | ||
|
||
const { text } = source; | ||
|
||
registerBlockType( 'core/vue-banner', { | ||
title: __( 'Vue Banner' ), | ||
|
||
icon: 'marker', | ||
|
||
category: 'widgets', | ||
|
||
attributes: { | ||
text: { | ||
type: 'string', | ||
source: text( 'h1' ), | ||
default: 'Hello World', | ||
}, | ||
}, | ||
|
||
className: false, | ||
|
||
edit( { attributes, setAttributes, target } ) { | ||
if ( target.firstChild ) { | ||
Object.assign( target.firstChild.__vue__, attributes ); | ||
return; | ||
} | ||
|
||
const child = document.createElement( 'div' ); | ||
target.appendChild( child ); | ||
|
||
new Vue( { | ||
el: target.firstChild, | ||
|
||
data: () => ( { ...attributes } ), | ||
|
||
template: ` | ||
<div> | ||
<input :value="text" @input="setText( $event.target.value )"> | ||
<h1>{{ text }}</h1> | ||
</div> | ||
`, | ||
|
||
methods: { | ||
setText( nextText ) { | ||
setAttributes( { text: nextText } ); | ||
}, | ||
}, | ||
} ); | ||
}, | ||
|
||
save( { attributes, target } ) { | ||
const child = document.createElement( 'div' ); | ||
target.appendChild( child ); | ||
|
||
new Vue( { | ||
el: target.firstChild, | ||
|
||
data: () => ( { ...attributes } ), | ||
|
||
template: ` | ||
<h1>{{ text }}</h1> | ||
`, | ||
} ); | ||
}, | ||
} ); |
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,33 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { buildVTree, renderSubtreeIntoContainer, Component } from '@wordpress/element'; | ||
|
||
class BlockRenderContext extends Component { | ||
componentDidMount() { | ||
this.delegatedRender(); | ||
} | ||
|
||
componentDidUpdate() { | ||
this.delegatedRender(); | ||
} | ||
|
||
delegatedRender() { | ||
const { render, ...props } = this.props; | ||
const result = render( { ...props, target: this.node } ); | ||
|
||
if ( result ) { | ||
renderSubtreeIntoContainer( | ||
this, | ||
buildVTree( result ), | ||
this.node | ||
); | ||
} | ||
} | ||
|
||
render() { | ||
return <div ref={ ( node ) => this.node = node } />; | ||
} | ||
} | ||
|
||
export default BlockRenderContext; |
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
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