Skip to content

Instantly share code, notes, and snippets.

@mgambill
Created April 13, 2021 00:20
Show Gist options
  • Save mgambill/856f7b12e37addc7a392fc324ccca852 to your computer and use it in GitHub Desktop.
Save mgambill/856f7b12e37addc7a392fc324ccca852 to your computer and use it in GitHub Desktop.
Vue.js v3 - Directive Plugin
// 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)
}
}
@renestalder
Copy link

What's the correct way to include that in a Vue 3 project?

@mgambill
Copy link
Author

mgambill commented Aug 1, 2021

assuming the file is in ./src/components/Storyblok.js

in the main.js

import { createApp } from 'vue'
import StoryblokVue from './components/Storyblok'
import App from './App.vue'
import * as bloks from "./components/bloks"
import './assets/tailwind.css'

const app = createApp(App)
  .use(StoryblokVue);

Array.from(bloks).forEach( b => app.component(b))

app.mount('#app')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment