vue-jsx-vapor
Version:
Convert Vue JSX to Vapor
123 lines (121 loc) • 3.72 kB
JavaScript
const require_chunk = require('./chunk-CUT6urMc.cjs');
const vue = require_chunk.__toESM(require("vue"));
//#region src/core/runtime.ts
function getCurrentInstance() {
return vue.currentInstance || vue.getCurrentInstance();
}
/**
* Returns the props of the current component instance.
*
* @example
* ```tsx
* import { useProps } from 'vue-jsx-vapor'
*
* defineComponent(({ foo = '' })=>{
* const props = useProps() // { foo: '' }
* })
* ```
*/
function useProps() {
const i = getCurrentInstance();
return i.props;
}
/**
* Returns the merged props and attrs of the current component.\
* Equivalent to `useProps()` + `useAttrs()`.
*
* @example
* ```tsx
* import { useFullProps } from 'vue-jsx-vapor'
*
* defineComponent((props) => {
* const fullProps = useFullProps() // = useAttrs() + useProps()
* })
*/
function useFullProps() {
return (0, vue.proxyRefs)({
...(0, vue.toRefs)(useProps()),
...(0, vue.toRefs)((0, vue.useAttrs)())
});
}
function createFragment(nodes, anchor = document.createTextNode("")) {
const frag = new vue.VaporFragment(nodes);
frag.anchor = anchor;
return frag;
}
function normalizeNode(node, anchor) {
if (node instanceof Node || (0, vue.isFragment)(node)) {
anchor && (anchor.textContent = "");
return node;
} else if ((0, vue.isVaporComponent)(node)) {
anchor && (anchor.textContent = "");
return createFragment(node, anchor);
} else if (Array.isArray(node)) {
anchor && (anchor.textContent = "");
return createFragment(node.map((i) => normalizeNode(i)), anchor);
} else {
const result = node == null || typeof node === "boolean" ? "" : String(node);
if (anchor) {
anchor.textContent = result;
return anchor;
} else return document.createTextNode(result);
}
}
function resolveValue(current, value, _anchor) {
const node = normalizeNode(value, _anchor);
if (current) {
if ((0, vue.isFragment)(current)) {
const { anchor } = current;
if (anchor && anchor.parentNode) {
(0, vue.remove)(current.nodes, anchor.parentNode);
(0, vue.insert)(node, anchor.parentNode, anchor);
!_anchor && anchor.parentNode.removeChild(anchor);
}
} else if (current instanceof Node) {
if ((0, vue.isFragment)(node) && current.parentNode) {
(0, vue.insert)(node, current.parentNode, current);
current.parentNode.removeChild(current);
} else if (node instanceof Node) {
if (current.nodeType === 3 && node.nodeType === 3) {
current.textContent = node.textContent;
return current;
} else if (current.parentNode) current.parentNode.replaceChild(node, current);
}
}
}
return node;
}
function resolveValues(values = [], _anchor) {
const nodes = [];
const frag = createFragment(nodes, _anchor);
const scopes = [];
for (const [index, value] of values.entries()) {
const anchor = index === values.length - 1 ? _anchor : void 0;
if (typeof value === "function") (0, vue.renderEffect)(() => {
if (scopes[index]) scopes[index].stop();
scopes[index] = new vue.EffectScope();
nodes[index] = scopes[index].run(() => resolveValue(nodes[index], value(), anchor));
});
else nodes[index] = resolveValue(nodes[index], value, anchor);
}
return frag;
}
function setNodes(anchor, ...values) {
const resolvedValues = resolveValues(values, anchor);
anchor.parentNode && (0, vue.insert)(resolvedValues, anchor.parentNode, anchor);
}
function createNodes(...values) {
return resolveValues(values);
}
//#endregion
exports.createNodes = createNodes;
exports.getCurrentInstance = getCurrentInstance;
exports.setNodes = setNodes;
exports.useFullProps = useFullProps;
exports.useProps = useProps;
Object.defineProperty(exports, 'useRef', {
enumerable: true,
get: function () {
return vue.shallowRef;
}
});