-
-
Notifications
You must be signed in to change notification settings - Fork 547
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cedd866
commit 4b77c4a
Showing
3 changed files
with
76 additions
and
50 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import Alpine from 'alpinejs' | ||
|
||
const nearestVm = (el, callback = vm => true) => { | ||
let node = el; | ||
while (node) { | ||
if (node.__vue__) break; | ||
node = node.parentNode; | ||
} | ||
let vm = node.__vue__; | ||
if (!vm) { | ||
return; | ||
} | ||
const root = vm.$root; | ||
while (vm !== root) { | ||
if (callback(vm)) return vm; | ||
vm = vm.$parent; | ||
} | ||
}; | ||
|
||
Alpine.magic('useField', (el) => { | ||
return () => { | ||
const vm = nearestVm(el, vm => vm.$options.name.match(/-fieldtype$/)); | ||
if (!vm) { | ||
return; | ||
} | ||
const data = Alpine.reactive({ | ||
value: vm.value, | ||
fieldPathPrefix: vm.fieldPathPrefix, | ||
update: vm.update, | ||
updateDebounced: vm.updateDebounced, | ||
updateMeta: vm.updateMeta, | ||
}); | ||
vm.$watch('value', () => { | ||
data.value = vm.value; | ||
}); | ||
vm.$watch('fieldPathPrefix', () => { | ||
data.fieldPathPrefix = vm.fieldPathPrefix; | ||
}); | ||
return data; | ||
} | ||
}); | ||
|
||
Alpine.magic('useStore', (el, { Alpine }) => { | ||
return () => { | ||
const vm = nearestVm(el, vm => vm.$options.name === 'publish-container'); | ||
if (!vm) { | ||
return; | ||
} | ||
const state = vm.$store.state.publish[vm.name] || []; | ||
const data = Alpine.reactive({ | ||
values: state.values, | ||
meta: state.meta, | ||
errors: state.errors, | ||
setFieldValue: vm.setFieldValue, | ||
setFieldMeta: vm.setFieldMeta, | ||
}); | ||
vm.$store.subscribe((mutation) => { | ||
if (mutation.type === `publish/${vm.name}/setFieldValue`) { | ||
data.values = { ...state.values }; | ||
} | ||
if (mutation.type === `publish/${vm.name}/setFieldMeta`) { | ||
data.meta = { ...state.meta }; | ||
} | ||
if (mutation.type === `publish/${vm.name}/setErrors`) { | ||
data.errors = { ...state.errors }; | ||
} | ||
}); | ||
return data; | ||
}; | ||
}); | ||
|
||
Alpine.start(); |
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 |
---|---|---|
@@ -1,13 +1,9 @@ | ||
<template> | ||
<div class="html-fieldtype-wrapper" v-html="config.html" /> | ||
<div v-html="config.html" /> | ||
</template> | ||
|
||
<script> | ||
export default { | ||
mixins: [Fieldtype], | ||
inject: ['storeName'], | ||
mixins: [Fieldtype] | ||
}; | ||
</script> | ||
</script> |