-
-
Notifications
You must be signed in to change notification settings - Fork 6
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
6394ee2
commit ce906b5
Showing
10 changed files
with
217 additions
and
16 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,2 @@ | ||
export {default as XJsx} from './x-jsx' | ||
export {default as XTpl} from './x-tpl' |
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,50 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
import {FunctionComponent, JsxFn, JsxNode} from '@/utils/types-helper' | ||
import {defineComponent, VNode, h, PropType} from 'vue-demi' | ||
|
||
const valueIsFunctionComponent = (value: any): value is FunctionComponent => { | ||
return typeof value === 'object' && value.functional && typeof value.render === 'function' | ||
} | ||
|
||
/** | ||
* jsx render component | ||
* | ||
* @example | ||
* ```jsx | ||
* <x-jsx :jsx="yourJsx"> | ||
* i am child text | ||
* </x-jsx> | ||
* | ||
* const yourJsx = <div>hello</div> | ||
* const yourJsx = (props) => <div>hello, {props.children}</div> | ||
* ``` | ||
*/ | ||
const vm = defineComponent({ | ||
name: 'XJsx', | ||
props: { | ||
jsx: { | ||
type: [Function, Object, Array, String, Number, Boolean] as PropType<JsxNode | JsxFn | FunctionComponent> | ||
} | ||
}, | ||
render(): VNode { | ||
const {$slots, $attrs, $props} = this as InstanceType<typeof vm> | ||
const {jsx} = $props | ||
|
||
const children = typeof $slots.default === 'function' ? $slots.default() : $slots.default | ||
const props = {...$attrs, children} | ||
|
||
let result: VNode | ||
|
||
if (valueIsFunctionComponent(jsx)) { | ||
result = jsx.render(h, props) | ||
} else if (typeof jsx === 'function') { | ||
result = jsx(props, h) as VNode | ||
} else { | ||
result = jsx as VNode | ||
} | ||
|
||
return result | ||
} | ||
}) | ||
|
||
export default vm |
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,77 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
import {Vue2CompileResult} from '@/utils/types-helper' | ||
import {Vue, defineComponent, VNode, h, PropType, isVue2} from 'vue-demi' | ||
|
||
// Use weak map to store the mapping relationship between ctx and {[template]: renderFunction} | ||
// This memory will be automatically reclaimed when other references to ctx are destroyed (related to the browser memory reclamation mechanism) | ||
const compileMap = new WeakMap<Object, Record<string, Function>>() | ||
|
||
/** | ||
* compile a template to render function | ||
* @param template template string | ||
* @param ctx context | ||
* @returns render function | ||
*/ | ||
const compile = (template: string, ctx: Object): Function => { | ||
const historyResults = compileMap.get(ctx) | ||
if (historyResults && historyResults[template]) return historyResults[template].bind(ctx) | ||
|
||
let renderFunc: Function | ||
if (isVue2) { | ||
const compileResult = Vue.compile(template) as unknown as Vue2CompileResult | ||
renderFunc = compileResult.staticRenderFns?.[0] ?? compileResult.render | ||
} else { | ||
renderFunc = Vue.compile(template) | ||
} | ||
|
||
const results = { | ||
...historyResults, | ||
[template]: renderFunc | ||
} | ||
compileMap.set(ctx, results) | ||
return renderFunc.bind(ctx) | ||
} | ||
|
||
/** | ||
* template render component | ||
* | ||
* @example | ||
* ```jsx | ||
* <x-tpl :tpl="yourTemplate" :ctx="this" /> | ||
* | ||
* export default { | ||
* data() { | ||
* return { | ||
* name: 'xiao ming', | ||
* yourTemplate: '<div>hello, {{name}}</div>' | ||
* } | ||
* } | ||
* ``` | ||
*/ | ||
const vm = defineComponent({ | ||
name: 'XTpl', | ||
props: { | ||
tpl: { | ||
type: String | ||
}, | ||
ctx: { | ||
type: Object as PropType<Record<string, any>> | ||
} | ||
}, | ||
render() { | ||
const {$props, $parent} = this as InstanceType<typeof vm> | ||
const {tpl, ctx} = $props | ||
const _ctx = ctx || $parent || {} | ||
|
||
// console.log('render', isVue2, tpl, _ctx) | ||
|
||
if (!tpl) return null | ||
|
||
const renderFunc = compile(tpl, _ctx) | ||
const result = isVue2 ? renderFunc(h) : renderFunc(ctx) | ||
|
||
return result as VNode | ||
} | ||
}) | ||
|
||
export default vm |
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 +1,3 @@ | ||
export * from './hooks' | ||
export * from './components' | ||
export * from './utils/types-helper' |
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