@bookbox/generator-js
Version:
Bookbox generator for js code
112 lines (111 loc) • 3.78 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.defaultBookApi = exports.bookCreator = exports.bookEnd = exports.bookStart = void 0;
const core_1 = require("@bookbox/core");
const getElement = name => props => (...children) => {
const list = (0, core_1.isTemplateParams)(children) ? (0, core_1.templateToList)(...children) : children;
return {
name,
props,
children: list,
};
};
const proxyBuilder = (getBuild, prepare) => {
const getProxy = (params) => new Proxy(getBuild(params), {
get(_, name) {
return (...children) => {
var _a;
const value = (0, core_1.isTemplateParams)(children) ? prepare(...children) : (_a = children[0]) !== null && _a !== void 0 ? _a : true;
return getProxy(Object.assign(Object.assign({}, params), { [name]: value }));
};
},
});
return getProxy({});
};
function getElementProxy() {
return function g(name, prepare = String.raw) {
const getElementWithProps = getElement(name);
const builder = proxyBuilder(params => getElementWithProps(params), prepare);
return builder;
};
}
const bookStart = elem => {
const { name, props } = elem();
return {
__start: name,
props,
};
};
exports.bookStart = bookStart;
const bookEnd = elem => {
const { name, props } = elem();
return {
__end: name,
props,
};
};
exports.bookEnd = bookEnd;
const bookCreator = (text, ...elements) => {
return { schema: (0, core_1.templateToList)(text, ...elements) };
};
exports.bookCreator = bookCreator;
exports.bookCreator.root = exports.bookCreator;
const getRootProxy = getElementProxy();
const getFormatProxy = getElementProxy();
const getWebProxy = getElementProxy();
exports.defaultBookApi = {
title: getRootProxy('title'),
authors: getRootProxy('authors'),
draft: getRootProxy('draft'),
em: getRootProxy('em'),
strong: getRootProxy('strong'),
format: {
sup: getFormatProxy('format.sup'),
sub: getFormatProxy('format.sub'),
pre: getFormatProxy('format.pre'),
i: getFormatProxy('format.i'),
b: getFormatProxy('format.b'),
u: getFormatProxy('format.u'),
s: getFormatProxy('format.s'),
small: getFormatProxy('format.small'),
},
format_sup: getRootProxy('format_sup'),
format_sub: getRootProxy('format_sub'),
format_pre: getRootProxy('format_pre'),
format_i: getRootProxy('format_i'),
format_b: getRootProxy('format_b'),
format_u: getRootProxy('format_u'),
format_s: getRootProxy('format_s'),
format_small: getRootProxy('format_small'),
web: {
video: getWebProxy('web.video'),
audio: getWebProxy('web.audio'),
message: getWebProxy('web.message'),
},
web_video: getRootProxy('web_video'),
web_audio: getRootProxy('web_audio'),
web_message: getRootProxy('web_message'),
header: getRootProxy('header'),
code: getRootProxy('code'),
label: getRootProxy('label'),
link: getRootProxy('link'),
tooltip: getRootProxy('tooltip'),
image: getRootProxy('image'),
external: getRootProxy('external'),
area: getRootProxy('area'),
list: getRootProxy('list'),
separator: getRootProxy('separator'),
item: getRootProxy('item'),
math: getRootProxy('math'),
video: getRootProxy('video'),
audio: getRootProxy('audio'),
use: getRootProxy('use'),
counter: getRootProxy('counter'),
resource: getRootProxy('resource'),
table: getRootProxy('table'),
row: getRootProxy('row'),
cell: getRootProxy('cell'),
start: exports.bookStart,
end: exports.bookEnd,
book: exports.bookCreator,
};