Last active
August 19, 2024 18:07
-
-
Save daliborgogic/61c9c435244828563bd02f9dcad40dae to your computer and use it in GitHub Desktop.
Gutenberg Block built with Vue.js
This file contains hidden or 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
// https://wordpress.org/gutenberg/ | |
(wp => { | |
const el = wp.element.createElement | |
const __ = wp.i18n.__ | |
wp.blocks.registerBlockType( 'plugins/gutenvue', { | |
title: __('Gutenberg: VueJS', 'gutenvue'), | |
category: 'widgets', | |
supportHTML: false, | |
attributes: { | |
who: { | |
selector: 'p', | |
attribute: 'who' | |
} | |
}, | |
edit: props => { | |
const attributes = props.attributes | |
const setAttributes = props.setAttributes | |
const className = props.className | |
const focus = props.focus | |
const id = props.id | |
// Define id to mount VueJs app into | |
const vueAppIdAttr = 'vuetest-' + id | |
// Set default for who we're saying who to. | |
if (!attributes.hasOwnProperty('who')) { | |
attributes.who = 'Dlbr' | |
} | |
// Create copy of attributes to create intial state of Vue app with | |
let vueInitialState = {} | |
Object.keys(attributes).forEach(key => | |
vueInitialState[key] = attributes[key] | |
) | |
// Create Vue app | |
const APP = new Vue({ | |
// Mount on element we're about to create with el() | |
el: '#' + vueAppIdAttr, | |
data () { | |
return vueInitialState | |
}, | |
// Use watchers to update | |
watch: { | |
who (newValue) { | |
setAttributes({who:newValue}) | |
} | |
}, | |
// Template for Vue app | |
template: '<div><p>who: {{who}}</p><input v-model="who"/></div>' | |
}) | |
// Return using WordPress createElement | |
return el ( | |
'div', | |
{className}, | |
[ | |
el( | |
'p', | |
{ | |
className, | |
who: attributes.who | |
}, | |
el( | |
'div', | |
{id: vueAppIdAttr} | |
) | |
) | |
] | |
) | |
}, | |
save (attributes,className) { | |
el('p', { | |
className, | |
who: attributes.who | |
}) | |
} | |
} | |
) | |
})(window.wp) |
Author
daliborgogic
commented
Jul 31, 2018
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment