@foxpage/foxpage-core
Version:
foxpage core
69 lines (68 loc) • 2.61 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MainParser = void 0;
const foxpage_shared_1 = require("@foxpage/foxpage-shared");
const block_1 = require("./block");
const page_1 = require("./page");
const template_1 = require("./template");
class MainParser {
constructor({ content }, ctx) {
/**
* messages
*/
this.messages = new foxpage_shared_1.Messages();
this.mainContent = content;
if (this.isPage()) {
this.templateParser = new template_1.TemplateParserImpl();
this.blockParser = new block_1.BlockParserImpl();
this.pageParser = new page_1.PageParserImpl(content);
this.prepare(ctx);
}
else if (this.isBlock()) {
this.blockParser = new block_1.BlockParserImpl();
}
}
prepare(ctx) {
var _a, _b, _c;
if (this.isPage()) {
(_a = this.pageParser) === null || _a === void 0 ? void 0 : _a.preParse(ctx);
(_b = this.templateParser) === null || _b === void 0 ? void 0 : _b.preParse(ctx, {
containerGetter: templateId => { var _a; return (_a = this.pageParser) === null || _a === void 0 ? void 0 : _a.getTemplateSchemas(templateId); },
});
(_c = this.blockParser) === null || _c === void 0 ? void 0 : _c.preParse(ctx, {
containerGetter: blockId => { var _a; return (_a = this.pageParser) === null || _a === void 0 ? void 0 : _a.getBlockSchemas(blockId); },
});
}
}
async parse(ctx) {
var _a, _b, _c, _d;
let result;
if (this.isPage()) {
(_a = this.templateParser) === null || _a === void 0 ? void 0 : _a.parse(ctx);
(_b = this.blockParser) === null || _b === void 0 ? void 0 : _b.parse(ctx);
result = (_c = this.pageParser) === null || _c === void 0 ? void 0 : _c.parse(ctx);
}
if (this.isBlock()) {
result = (_d = this.blockParser) === null || _d === void 0 ? void 0 : _d.parseOne(this.mainContent, ctx);
}
if (result) {
ctx.updatePage(result.parsed);
}
// clear memory
this.reset();
return result;
}
isPage() {
return this.mainContent.type === 'page';
}
isBlock() {
return this.mainContent.type === 'block';
}
reset() {
this.pageParser = undefined;
this.templateParser = undefined;
this.blockParser = undefined;
this.messages = undefined;
}
}
exports.MainParser = MainParser;