Created
April 13, 2021 00:20
-
-
Save mgambill/856f7b12e37addc7a392fc324ccca852 to your computer and use it in GitHub Desktop.
Vue.js v3 - Directive Plugin
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
// Vue 3 | |
const addClass = (el, className) => { | |
if (el.classList) { | |
el.classList.add(className) | |
} else if (!new RegExp('\\b' + className + '\\b').test(el.className)) { | |
el.className += ' ' + className | |
} | |
} | |
export default { | |
install: (app, options) => { | |
app.directive('editable', { | |
beforeMount(el, binding, value) { | |
console.log( binding.value) | |
if (typeof binding.value._editable === 'undefined' || binding.value._editable === null) { | |
return | |
} | |
var options = JSON.parse(binding.value._editable.replace('<!--#storyblok#', '').replace('-->', '')) | |
el.setAttribute('data-blok-c', JSON.stringify(options)) | |
el.setAttribute('data-blok-uid', options.id + '-' + options.uid) | |
addClass(el, 'storyblok__outline') | |
} | |
}) | |
if (typeof window !== 'undefined' && typeof window.storyblok !== 'undefined') | |
app.config.globalProperties.$storyBlok = window.storyblok | |
app.provide('storyblok', options) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
assuming the file is in ./src/components/Storyblok.js
in the main.js