-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(helper): update vue3 helper logic
- Loading branch information
1 parent
b602eac
commit dc1cb16
Showing
5 changed files
with
55 additions
and
34 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,8 @@ | ||
<script setup lang="ts"> | ||
import FieldPlugin from './components/FieldPlugin.vue' | ||
import FieldPluginExample from './components/FieldPluginExample/index.vue' | ||
import { FieldPluginProvider } from '@storyblok/field-plugin/vue3' | ||
</script> | ||
|
||
<template> | ||
<FieldPluginProvider> | ||
<template v-slot:loading><span>Loading...</span></template> | ||
<template v-slot:error><span>Error</span></template> | ||
|
||
<FieldPluginExample /> | ||
</FieldPluginProvider> | ||
<FieldPluginExample /> | ||
</template> |
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 |
---|---|---|
@@ -1,2 +1 @@ | ||
export * from './useFieldPlugin.ts' | ||
export { default as FieldPluginProvider } from './FieldPluginProvider.vue' |
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,23 +1,43 @@ | ||
import { inject } from 'vue' | ||
import { type FieldPluginResponse } from '@storyblok/field-plugin' | ||
import { | ||
createFieldPlugin, | ||
CreateFieldPluginOptions, | ||
FieldPluginResponse, | ||
} from '@storyblok/field-plugin' | ||
import { onMounted, reactive, UnwrapRef } from 'vue' | ||
|
||
export const useFieldPlugin = () => { | ||
const plugin = inject<FieldPluginResponse<unknown>>( | ||
'field-plugin', | ||
() => { | ||
throw new Error( | ||
`You need to wrap your app with \`<FieldPluginProvider>\` component.`, | ||
) | ||
}, | ||
true, | ||
) | ||
export const useFieldPlugin = <Content>({ | ||
parseContent, | ||
}: Omit<CreateFieldPluginOptions<Content>, 'onUpdateState'>): UnwrapRef< | ||
FieldPluginResponse<Content> | ||
> => { | ||
const plugin = reactive<FieldPluginResponse<Content>>({ | ||
type: 'loading', | ||
}) | ||
|
||
console.log(plugin) | ||
if (plugin.type !== 'loaded') { | ||
throw new Error( | ||
'The plugin is not loaded, yet `useFieldPlugin()` was invoked. Ensure that the component that invoked `useFieldPlugin()` is wrapped within `<FieldPluginProvider>`, and that it is placed within the default slot.', | ||
) | ||
} | ||
onMounted(() => { | ||
createFieldPlugin<Content>({ | ||
onUpdateState: (state) => { | ||
if (state.type === 'error') { | ||
// plugin.type = 'error' | ||
// plugin.error = state.error | ||
Object.assign(plugin, { | ||
type: 'error', | ||
error: state.error, | ||
}) | ||
} else if (state.type === 'loaded') { | ||
Object.assign(plugin, { | ||
type: 'loaded', | ||
data: state.data, | ||
actions: state.actions, | ||
}) | ||
// plugin.type = 'loaded' | ||
// plugin.data = state.data | ||
// plugin.actions = state.actions | ||
} | ||
}, | ||
parseContent, | ||
}) | ||
}) | ||
|
||
return plugin | ||
} |