Skip to content

Commit 2f1f862

Browse files
committed
a
1 parent c675714 commit 2f1f862

32 files changed

+293
-69
lines changed

docs/en/_sidebar.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
- [Method](/en/class-component/method/method.md)
77
- [Lifecycle hooks](/en/class-component/lifecycle-hook/lifecycle-hook.md)
88
- [Component property](/en/class-component/component-property/component-property.md)
9-
- [Getter](/en/class-component/getter/getter.md)
9+
- [Accessor](/en/class-component/accessor/accessor.md)
1010
- [Event](/en/class-component/event/event.md)
1111
- [Reference](/en/class-component/reference/reference.md)
1212
- [Watcher](/en/class-component/watcher/watcher.md)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
## Usage
2+
3+
Property getters will be tranformed into `{computed:{get:Foo}}`.
4+
5+
[](./code-usage.ts ':include :type=code typescript')
6+
7+
## Writable
8+
9+
Property setters will be tranformed into `{computed:{set:Foo}}`.
10+
11+
[](./code-writable.ts ':include :type=code typescript')
12+
13+
## Vanilla getter
14+
15+
We can define a ES vanilla getter by `@Vanilla`.
16+
17+
[](./code-vanilla-getter.ts ':include :type=code typescript')
18+
19+
## Vanilla setter
20+
21+
We can define a ES vanilla setter by `@Vanilla`.
22+
23+
[](./code-vanilla-setter.ts ':include :type=code typescript')

docs/en/class-component/getter/code-usage.ts renamed to docs/en/class-component/accessor/code-usage.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { Component, Vue } from 'vue-facing-decorator'
55
Vue options API
66
{
77
computed:{
8-
getter(){
8+
get(){
99
return 'value'
1010
}
1111
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
import { Component, Vue, Vanilla } from 'vue-facing-decorator'
3+
4+
@Component
5+
export default class MyComponent extends Vue {
6+
@Vanilla
7+
get getter() {
8+
return 'value'
9+
}
10+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
import { Component, Vue, Vanilla } from 'vue-facing-decorator'
3+
4+
@Component
5+
export default class MyComponent extends Vue {
6+
foo = ''
7+
@Vanilla
8+
set setter(bar: string) {
9+
this.foo = bar
10+
}
11+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
import { Component, Vue } from 'vue-facing-decorator'
3+
4+
/*
5+
Vue options API
6+
{
7+
computed:{
8+
set(){
9+
return 'value'
10+
}
11+
}
12+
}
13+
*/
14+
15+
@Component
16+
export default class MyComponent extends Vue {
17+
foo = ''
18+
set setter(bar: string) {
19+
this.foo = bar
20+
}
21+
}

docs/en/class-component/getter/getter.md

Lines changed: 0 additions & 5 deletions
This file was deleted.

docs/zh-cn/_sidebar.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
- [方法](/zh-cn/class-component/method/method.md)
77
- [生命周期钩子](/zh-cn/class-component/lifecycle-hook/lifecycle-hook.md)
88
- [组件属性](/zh-cn/class-component/component-property/component-property.md)
9-
- [访问器](/zh-cn/class-component/getter/getter.md)
9+
- [存取器](/zh-cn/class-component/accessor/accessor.md)
1010
- [事件](/zh-cn/class-component/event/event.md)
1111
- [引用](/zh-cn/class-component/reference/reference.md)
1212
- [监视器](/zh-cn/class-component/watcher/watcher.md)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
## 用法
2+
3+
类中的访问器会被转换到vue `{computed:{get:Foo}}`中。
4+
5+
[](./code-usage.ts ':include :type=code typescript')
6+
7+
## 可写的
8+
9+
类中的设置器会被转换到vue `{computed:{set:Foo}}`中。
10+
11+
[](./code-writable.ts ':include :type=code typescript')
12+
13+
## 原生访问器
14+
15+
使用`@Vanilla`定义ES原生访问器。
16+
17+
[](./code-vanilla-getter.ts ':include :type=code typescript')
18+
19+
## 原生设置器
20+
21+
使用`@Vanilla`定义原生设置器。
22+
23+
[](./code-vanilla-setter.ts ':include :type=code typescript')

docs/zh-cn/class-component/getter/code-usage.ts renamed to docs/zh-cn/class-component/accessor/code-usage.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { Component, Vue } from 'vue-facing-decorator'
55
Vue options API
66
{
77
computed:{
8-
getter(){
8+
get(){
99
return 'value'
1010
}
1111
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
import { Component, Vue, Vanilla } from 'vue-facing-decorator'
3+
4+
@Component
5+
export default class MyComponent extends Vue {
6+
@Vanilla
7+
get getter() {
8+
return 'value'
9+
}
10+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
import { Component, Vue, Vanilla } from 'vue-facing-decorator'
3+
4+
@Component
5+
export default class MyComponent extends Vue {
6+
foo = ''
7+
@Vanilla
8+
set setter(bar: string) {
9+
this.foo = bar
10+
}
11+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
import { Component, Vue } from 'vue-facing-decorator'
3+
4+
/*
5+
Vue options API
6+
{
7+
computed:{
8+
set(){
9+
return 'value'
10+
}
11+
}
12+
}
13+
*/
14+
15+
@Component
16+
export default class MyComponent extends Vue {
17+
foo = ''
18+
set setter(bar: string) {
19+
this.foo = bar
20+
}
21+
}

docs/zh-cn/class-component/getter/getter.md

Lines changed: 0 additions & 5 deletions
This file was deleted.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vue-facing-decorator",
3-
"version": "2.1.7",
3+
"version": "2.1.8",
44
"description": "Vue typescript class and decorator based component.",
55
"main": "dist/index.js",
66
"keywords": [

src/component.ts

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,21 @@ import { build as optionProps, PropsConfig } from './option/props'
99
import { build as optionInject, InjectConfig } from './option/inject'
1010
import { build as optionEmit } from './option/emit'
1111
import { build as optionVModel, VModelConfig } from './option/vmodel'
12-
export interface OptionBuilder {
13-
name?: string
14-
data?: Record<string, any>
15-
methods?: Record<string, Function>
16-
lifecycle?: Record<string, Function>
17-
computed?: Record<string, any>
18-
watch?: Record<string, WatchConfig>
19-
props?: Record<string, PropsConfig>
20-
inject?: Record<string, InjectConfig>
21-
beforeCreateCallbacks?:Function[]
12+
import { build as optionAccessor } from './option/accessor'
13+
import { OptionBuilder } from './optionBuilder'
2214

23-
}
2415
export interface Cons { new(): any, prototype: any }
2516
function ComponentOption(cons: Cons, extend?: any) {
2617
const optionBuilder: OptionBuilder = {}
27-
optionVModel(cons,optionBuilder)
18+
optionVModel(cons, optionBuilder)
2819
optionComputed(cons, optionBuilder)//after VModel
2920
optionWatch(cons, optionBuilder)
3021
optionProps(cons, optionBuilder)
3122
optionInject(cons, optionBuilder)
3223
optionEmit(cons, optionBuilder)
3324
optionRef(cons, optionBuilder)//after Computed
34-
optionMethodsAndLifecycle(cons, optionBuilder)//after Ref
35-
36-
25+
optionMethodsAndLifecycle(cons, optionBuilder)//after Ref Computed
26+
optionAccessor(cons, optionBuilder)
3727
const raw = {
3828
data() {
3929
const optionBuilder: OptionBuilder = {}

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export { decorator as Prop } from './option/props'
66
export { decorator as Inject } from './option/inject'
77
export { decorator as Emit } from './option/emit'
88
export { decorator as VModel, decorator as Model } from './option/vmodel'
9+
export { decorator as Vanilla } from './option/vanilla'
910
import type {
1011
ComponentPublicInstance
1112
} from 'vue'

src/option/accessor.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { Cons } from '../component'
2+
import { OptionBuilder,applyAccessors} from '../optionBuilder'
3+
import { toComponentReverse, obtainSlot } from '../utils'
4+
5+
export function build(cons: Cons, optionBuilder: OptionBuilder) {
6+
const slot = obtainSlot(cons.prototype)
7+
let vanillaMap = slot.obtainMap('vanilla')
8+
const protoArr = toComponentReverse(cons.prototype)
9+
const map: Map<string, { get: (() => any) | undefined, set: ((v: any) => any) | undefined }> = new Map
10+
11+
applyAccessors(optionBuilder,(ctx:any)=>{
12+
protoArr.forEach(proto => {
13+
const deses = Object.getOwnPropertyDescriptors(proto)
14+
for (const name in deses) {
15+
const des = deses[name]
16+
if (des && vanillaMap.has(name)) {
17+
if (typeof des.get === 'function' || typeof des.set === 'function') {
18+
map.set(name, {
19+
set: typeof des.set === 'function' ? des.set.bind(ctx) : undefined,
20+
get: typeof des.get === 'function' ? des.get.bind(ctx) : undefined,
21+
})
22+
}
23+
}
24+
}
25+
})
26+
return map
27+
})
28+
}

src/option/computed.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,25 @@
11
import { makeObject } from '../utils'
2-
import { Cons, OptionBuilder } from '../component'
2+
3+
import { Cons } from '../component'
34
import { obtainSlot, toComponentReverse, getValidNames } from '../utils'
5+
import { OptionBuilder } from '../optionBuilder'
46
export function build(cons: Cons, optionBuilder: OptionBuilder) {
57
optionBuilder.computed ??= {}
68
const slot = obtainSlot(cons.prototype)
79
let map = slot.obtainMap<Map<string, any>>('computed')
10+
let vanillaMap = slot.obtainMap<Map<string, any>>('vanilla')
811
const protoArr = toComponentReverse(cons.prototype)
912
protoArr.forEach(proto => {
10-
getValidNames(proto, (des) => {
11-
return typeof des.get === 'function'
13+
getValidNames(proto, (des, name) => {
14+
return (typeof des.get === 'function' || typeof des.set === 'function') && !vanillaMap.has(name)
1215
}).forEach(name => {
16+
1317
map.set(name, true)
14-
optionBuilder.computed![name] = Object.getOwnPropertyDescriptor(proto, name)!.get!
18+
const des = Object.getOwnPropertyDescriptor(proto, name)!
19+
optionBuilder.computed![name] = {
20+
get: typeof des.get === 'function' ? des.get : undefined,
21+
set: typeof des.set === 'function' ? des.set : undefined
22+
}
1523
})
1624
})
1725
}

src/option/data.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { makeObject } from '../utils'
2-
import { Cons, OptionBuilder } from '../component'
2+
import { Cons } from '../component'
3+
import { OptionBuilder } from '../optionBuilder'
34
import { obtainSlot, excludeNames, getValidNames } from '../utils'
45
export function build(cons: Cons, optionBuilder: OptionBuilder) {
56
optionBuilder.data ??= {}

src/option/emit.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { Cons, OptionBuilder } from '../component'
1+
import { Cons } from '../component'
2+
import { OptionBuilder } from '../optionBuilder'
23
import { obtainSlot, optoinNullableMemberDecorator } from '../utils'
34
export type EmitConfig = null | string
45
export const decorator = optoinNullableMemberDecorator(function (proto: any, name: string, key?: string) {

src/option/inject.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11

2-
import { Cons, OptionBuilder } from '../component'
2+
import { Cons } from '../component'
3+
import { OptionBuilder } from '../optionBuilder'
34
import { obtainSlot, optoinNullableMemberDecorator } from '../utils'
45
export interface InjectConfig {
56
from?: string | Symbol

src/option/methodsAndLifecycle.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { makeObject, obtainSlot } from '../utils'
2-
import { Cons, OptionBuilder } from '../component'
2+
import { Cons } from '../component'
3+
import { OptionBuilder } from '../optionBuilder'
34
import { toComponentReverse, excludeNames, getValidNames } from '../utils'
45
export const LifecycleNames = [
56
"beforeCreate",

src/option/props.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { Cons, OptionBuilder } from '../component'
1+
import { Cons } from '../component'
2+
import { OptionBuilder } from '../optionBuilder'
23
import { obtainSlot, optoinNullableMemberDecorator } from '../utils'
34
export interface PropsConfig {
45
type?: any

src/option/ref.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { Cons, OptionBuilder } from '../component'
1+
import { Cons } from '../component'
2+
import { OptionBuilder, applyAccessors } from '../optionBuilder'
23
import { obtainSlot, optoinNullableMemberDecorator } from '../utils'
34

45
export const decorator = optoinNullableMemberDecorator(function (proto: any, name: string, option?: {}) {
@@ -12,19 +13,17 @@ export function build(cons: Cons, optionBuilder: OptionBuilder) {
1213
const slot = obtainSlot(cons.prototype)
1314
const names = slot.obtainMap<Map<string, any>>('ref')!
1415
if (names) {
15-
optionBuilder.beforeCreateCallbacks??=[]
16-
optionBuilder.beforeCreateCallbacks.push(function(this:any){
17-
const ctx = this
16+
applyAccessors(optionBuilder, (ctx: any) => {
17+
const data: Map<string, { get: () => any, set: undefined }> = new Map
1818
names.forEach((value, name) => {
19-
20-
Object.defineProperty(this,name,{
21-
get(this:any){
19+
data.set(name, {
20+
get: function (this: any) {
2221
return ctx.$refs[name]
23-
}
22+
},
23+
set: undefined
2424
})
25-
2625
})
26+
return data
2727
})
28-
2928
}
3029
}

src/option/vanilla.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { obtainSlot, optoinNullableMemberDecorator } from '../utils'
2+
3+
export const decorator = optoinNullableMemberDecorator(function (proto: any, name: string, option?: {}) {
4+
const slot = obtainSlot(proto)
5+
let map = slot.obtainMap<Map<string, any>>('vanilla')
6+
map.set(name, true)
7+
})

src/option/vmodel.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { Cons, OptionBuilder } from '../component'
1+
import { Cons } from '../component'
2+
import { OptionBuilder } from '../optionBuilder'
23
import { obtainSlot, optoinNullableMemberDecorator } from '../utils'
34
import { decorator as PropsDecorator, PropsConfig } from './props'
45
export type VModelConfig = PropsConfig & {

0 commit comments

Comments
 (0)