-
Notifications
You must be signed in to change notification settings - Fork 247
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add plugin for options api support
- Loading branch information
Showing
14 changed files
with
213 additions
and
21 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,6 @@ | ||
export default { | ||
metaInfo: { | ||
title: 'Title from Options API' | ||
}, | ||
template: '<div>This component uses the Options API</div>' | ||
} |
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
declare module '*.vue' { | ||
import { ComponentOptions } from 'vue' | ||
const component: ComponentOptions | ||
export default component | ||
} |
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
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,35 @@ | ||
import { isFunction } from '@vue/shared' | ||
import { App, ComponentOptionsMixin, computed, getCurrentInstance } from 'vue' | ||
import { ComponentOptionsMetaInfo } from './types/options' | ||
import { useMeta } from './useApi' | ||
|
||
export type PluginOptions = { | ||
keyName: string | ||
} | ||
|
||
export const defaultOptions: PluginOptions = { | ||
keyName: 'metaInfo' | ||
} | ||
|
||
type CreateMixin = (options: PluginOptions) => ComponentOptionsMixin | ||
export const createMixin: CreateMixin = options => ({ | ||
created () { | ||
const instance = getCurrentInstance() | ||
if (!instance?.type || !(options.keyName in instance.type)) { | ||
return | ||
} | ||
|
||
const metaInfo = (instance.type as any)[options.keyName] as ComponentOptionsMetaInfo | ||
if (isFunction(metaInfo)) { | ||
const computedMetaInfo = computed(metaInfo) | ||
useMeta(computedMetaInfo) | ||
} else { | ||
useMeta(metaInfo) | ||
} | ||
} | ||
}) | ||
|
||
export const install = (app: App, _options: Partial<PluginOptions> = {}) => { | ||
const options = Object.assign({}, defaultOptions, _options) | ||
app.mixin(createMixin(options)) | ||
} |
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,137 @@ | ||
export interface AttributeProperty { | ||
[key: string]: string | string[] | ||
} | ||
|
||
export interface MetaDataProperty { | ||
vmid?: string, | ||
once?: boolean, | ||
skip?: boolean, | ||
body?: boolean, | ||
pbody?: boolean, | ||
[key: string]: any | ||
} | ||
|
||
export interface MetaPropertyCharset extends MetaDataProperty { | ||
charset: string, | ||
} | ||
|
||
export interface MetaPropertyEquiv extends MetaDataProperty { | ||
httpEquiv: string, | ||
content: string, | ||
} | ||
|
||
export interface MetaPropertyTrueEquiv extends MetaDataProperty { | ||
'http-equiv': string, | ||
content: string, | ||
} | ||
|
||
export interface MetaPropertyName extends MetaDataProperty { | ||
name: string, | ||
content: string, | ||
} | ||
|
||
export interface MetaPropertyMicrodata extends MetaDataProperty { | ||
itemprop: string, | ||
content: string, | ||
} | ||
|
||
// non-w3c interface | ||
export interface MetaPropertyProperty extends MetaDataProperty { | ||
property: string, | ||
content: string, | ||
} | ||
|
||
export interface LinkPropertyBase extends MetaDataProperty { | ||
rel: string, | ||
crossOrigin?: string | null, | ||
media?: string, | ||
nonce?: string, | ||
referrerPolicy?: string, | ||
rev?: string, | ||
type?: string | ||
} | ||
|
||
export interface LinkPropertyHref extends LinkPropertyBase { | ||
href?: string, | ||
hreflang?: string, | ||
callback?: void | ||
} | ||
|
||
export interface LinkPropertyHrefCallback extends LinkPropertyBase { | ||
vmid: string, | ||
// callback: CallbackFn, | ||
href?: string, | ||
hreflang?: string | ||
} | ||
|
||
export interface StyleProperty extends MetaDataProperty { | ||
cssText: string, | ||
// callback?: CallbackFn | ||
media?: string, | ||
nonce?: string, | ||
type?: string, | ||
} | ||
|
||
export interface ScriptPropertyBase extends MetaDataProperty { | ||
type?: string, | ||
charset?: string, | ||
async?: boolean, | ||
defer?: boolean, | ||
crossOrigin?: string, | ||
nonce?: string | ||
} | ||
|
||
export interface ScriptPropertyText extends ScriptPropertyBase { | ||
innerHTML: string | ||
} | ||
|
||
export interface ScriptPropertySrc extends ScriptPropertyBase { | ||
src: string, | ||
callback?: void | ||
} | ||
|
||
export interface ScriptPropertySrcCallback extends ScriptPropertyBase { | ||
vmid: string, | ||
// callback: CallbackFn | ||
} | ||
|
||
// eslint-disable-next-line no-use-before-define | ||
type JsonVal = string | number | boolean | JsonObj | JsonObj[] | null | ||
|
||
interface JsonObj { | ||
[key: string]: JsonVal | JsonVal[] | ||
} | ||
|
||
export interface ScriptPropertyJson extends ScriptPropertyBase { | ||
json: JsonObj | ||
} | ||
|
||
export interface NoScriptProperty extends MetaDataProperty { | ||
innerHTML: string, | ||
} | ||
|
||
export interface ComponentMetaInfo { | ||
title?: string | ||
|
||
htmlAttrs?: AttributeProperty | ||
headAttrs?: AttributeProperty | ||
bodyAttrs?: AttributeProperty | ||
|
||
base?: { | ||
target: string, | ||
href: string | ||
} | ||
|
||
meta?: (MetaPropertyCharset | MetaPropertyEquiv | MetaPropertyTrueEquiv | MetaPropertyName | MetaPropertyMicrodata | MetaPropertyProperty)[] | ||
link?: (LinkPropertyBase | LinkPropertyHref | LinkPropertyHrefCallback)[] | ||
style?: StyleProperty[] | ||
script?: (ScriptPropertyText | ScriptPropertySrc | ScriptPropertySrcCallback | ScriptPropertyJson)[] | ||
noscript?: NoScriptProperty[] | ||
|
||
__dangerouslyDisableSanitizers?: string[] | ||
__dangerouslyDisableSanitizersByTagID?: { | ||
[key: string]: string[] | ||
} | ||
} | ||
|
||
export type ComponentOptionsMetaInfo = ComponentMetaInfo | (() => ComponentMetaInfo) |
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