Skip to content
This repository was archived by the owner on Jan 9, 2022. It is now read-only.

Commit ff01e25

Browse files
committed
feat: change plugin api
- combine the properties of the plugin into one $dialog - make $dialog accessible from option api
1 parent 46f35c4 commit ff01e25

File tree

4 files changed

+52
-43
lines changed

4 files changed

+52
-43
lines changed

Diff for: src/components/GDialogRoot.vue

+8-10
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,23 @@
1111
<script lang="ts">
1212
import { inject } from 'vue'
1313
14-
import { DialogMethodsKey, DialogDialogListKey } from '../index'
14+
import { dialogInjectionKey } from '../index'
1515
1616
export default {
1717
name: 'GDialogRoot',
1818
1919
setup() {
20-
const dialogs = inject(DialogDialogListKey, [])
21-
const $dialog = inject(DialogMethodsKey, null)
20+
const {
21+
dialogs,
22+
removeDialog,
23+
} = inject(dialogInjectionKey)!
2224
23-
if(!$dialog) {
24-
console.error('The plugin is not initialized')
25+
if(!dialogs) {
26+
console.error('The giart-vue-dialog plugin is not initialized')
2527
}
2628
2729
function onClose(index: number) {
28-
if(!$dialog) {
29-
throw new Error('good')
30-
}
31-
32-
$dialog.remove(index)
30+
removeDialog(index)
3331
}
3432
3533
return {

Diff for: src/index.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ import './scss/main.scss'
44
export { default as GDialog } from './components/GDialog.vue'
55

66
// using plugin
7+
export * from './types/Plugin'
8+
79
export { default as GDialogRoot } from './components/GDialogRoot.vue'
810

9-
export { DialogDialogListKey as DialogDialogListKey } from './plugin'
10-
export { DialogMethodsKey as DialogMethodsKey } from './plugin'
11-
export { plugin as plugin } from './plugin'
11+
export {
12+
plugin, dialogInjectionKey,
13+
} from './plugin'

Diff for: src/plugin.ts

+29-26
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,41 @@
11
import {
2-
reactive, InjectionKey, shallowRef, Plugin,
2+
reactive, shallowRef, Plugin,
33
} from 'vue'
44

5-
import { DialogMethods, IDialogItem } from './types/Plugin'
6-
7-
export const DialogMethodsKey: InjectionKey<DialogMethods> = Symbol('DialogMethod')
8-
export const DialogDialogListKey: InjectionKey<IDialogItem[]> = Symbol('DialogList')
5+
import {
6+
IDialog, IDialogItem, DialogInjectionKey,
7+
} from './types/Plugin'
98

109
const dialogs = reactive<IDialogItem[]>([])
10+
const $dialog: IDialog = {
11+
dialogs,
1112

12-
export const plugin: Plugin = {
13-
install: (app) => {
14-
app.provide(DialogMethodsKey, {
15-
add: ({ component, props }) => {
16-
dialogs.push({
17-
component: shallowRef(component),
18-
id: Date.now() + Math.random(),
13+
addDialog: ({ component, props }) => {
14+
dialogs.push({
15+
component: shallowRef(component),
16+
id: Date.now() + Math.random(),
1917

20-
props: {
21-
modelValue: true,
22-
...props,
23-
},
24-
})
25-
},
26-
27-
remove: (index) => {
28-
const dialog = dialogs[index]
29-
dialog.props.modelValue = false
30-
setTimeout(() => {
31-
dialogs.splice(index, 1)
32-
}, 150)
18+
props: {
19+
modelValue: true,
20+
...props,
3321
},
3422
})
23+
},
3524

36-
app.provide(DialogDialogListKey, dialogs)
25+
removeDialog: (index) => {
26+
const dialog = dialogs[index]
27+
dialog.props.modelValue = false
28+
setTimeout(() => {
29+
dialogs.splice(index, 1)
30+
}, 150)
31+
},
32+
}
33+
34+
export const dialogInjectionKey: DialogInjectionKey = Symbol('GDialog')
35+
36+
export const plugin: Plugin = {
37+
install: (app) => {
38+
app.provide(dialogInjectionKey, $dialog)
39+
app.config.globalProperties.$dialog = $dialog
3740
},
3841
}

Diff for: src/types/Plugin.ts

+10-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {
2-
Component, ShallowUnwrapRef,
2+
Component, ShallowUnwrapRef, InjectionKey,
33
} from 'vue'
44

55
export interface IDialogItem {
@@ -19,7 +19,13 @@ type DialogRemoveMethod = (
1919
index: number
2020
) => void
2121

22-
export interface DialogMethods {
23-
add: DialogAddMethod
24-
remove: DialogRemoveMethod
22+
interface IDialogMethods {
23+
addDialog: DialogAddMethod
24+
removeDialog: DialogRemoveMethod
2525
}
26+
27+
export interface IDialog extends IDialogMethods {
28+
dialogs: IDialogItem[]
29+
}
30+
31+
export type DialogInjectionKey = InjectionKey<IDialog>

0 commit comments

Comments
 (0)