new(basic.gblib): AS IMAGE, AS PDF, SET THEME and SQL new keywords.
This commit is contained in:
parent
5052dbc1a4
commit
227e2bd6b3
2 changed files with 40 additions and 25 deletions
|
@ -195,9 +195,9 @@ export class GBVMService extends GBService {
|
||||||
|
|
||||||
code = code.replace(/(\w+)\s*\=\s*SELECT\s*(.*)/gi, ($0, $1, $2) => {
|
code = code.replace(/(\w+)\s*\=\s*SELECT\s*(.*)/gi, ($0, $1, $2) => {
|
||||||
|
|
||||||
let tableName = /\sFROM\s(\w+)/.exec($1)[0];
|
let tableName = /\sFROM\s(\w+)/.exec($2)[1];
|
||||||
|
let sql = `SELECT ${$2}`.replace(tableName, '?');
|
||||||
return `sys().executeSQL(${$2}, ${$1}, ${tableName})\n`;
|
return `${$1} = sys().executeSQL(${$1}, "${sql}", "${tableName}")\n`;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
@ -454,7 +454,11 @@ export class GBVMService extends GBService {
|
||||||
});
|
});
|
||||||
|
|
||||||
code = code.replace(/(\w+)\s*\=\s*(.*)\s*as image/gi, ($0, $1, $2) => {
|
code = code.replace(/(\w+)\s*\=\s*(.*)\s*as image/gi, ($0, $1, $2) => {
|
||||||
return `sys().asImage(${$2}, ${$1})\n`;
|
return `${$1} = sys().asImage(${$2})\n`;
|
||||||
|
});
|
||||||
|
|
||||||
|
code = code.replace(/(\w+)\s*\=\s*(.*)\s*as pdf/gi, ($0, $1, $2) => {
|
||||||
|
return `${$1} = sys().asPdf(${$2})\n`;
|
||||||
});
|
});
|
||||||
|
|
||||||
code = code.replace(/save\s(.*)\sas\s(.*)/gi, ($0, $1, $2, $3) => {
|
code = code.replace(/save\s(.*)\sas\s(.*)/gi, ($0, $1, $2, $3) => {
|
||||||
|
|
|
@ -188,7 +188,9 @@ export class SystemKeywords {
|
||||||
* @see puppeteer.
|
* @see puppeteer.
|
||||||
*/
|
*/
|
||||||
private async renderTable(data, renderPDF, renderImage) {
|
private async renderTable(data, renderPDF, renderImage) {
|
||||||
|
if (!data[1]){
|
||||||
|
return null;
|
||||||
|
}
|
||||||
const gbaiName = `${this.min.botId}.gbai`;
|
const gbaiName = `${this.min.botId}.gbai`;
|
||||||
const browser = await puppeteer.launch({ headless: false });
|
const browser = await puppeteer.launch({ headless: false });
|
||||||
const page = await browser.newPage();
|
const page = await browser.newPage();
|
||||||
|
@ -210,37 +212,46 @@ export class SystemKeywords {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
await page.addScriptTag({path: 'node_modules/tabulator-tables/dist/js/tabulator.min.js'});
|
||||||
|
|
||||||
|
// Removes internal hidden element used to hold one-based index arrays.
|
||||||
|
|
||||||
|
data.shift();
|
||||||
|
|
||||||
|
// Guess fields from data variable into Tabulator fields collection.
|
||||||
|
|
||||||
let fields = [];
|
let fields = [];
|
||||||
for (let i = 0; i < data.length; i++) {
|
let keys = Object.keys(data[1]);
|
||||||
fields.push({field:data[i]});
|
for (let i = 0; i < keys.length; i++) {
|
||||||
|
fields.push({field: keys[i], title: keys[i]});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Adds DIV for Tabulator.
|
||||||
|
|
||||||
await page.evaluate(() => {
|
await page.evaluate(() => {
|
||||||
const el = document.createElement("div");
|
const el = document.createElement("div");
|
||||||
el.id = "table";
|
el.setAttribute("id", "table");
|
||||||
document.body.prepend(el);
|
document.body.appendChild(el);
|
||||||
});
|
});
|
||||||
|
|
||||||
await page.evaluate(`
|
const code = `
|
||||||
new Tabulator("#example-table", {
|
var table = new Tabulator("#table", {
|
||||||
height:"311px",
|
height:"311px",
|
||||||
|
layout:"fitColumns",
|
||||||
data: ${JSON.stringify(data)},
|
data: ${JSON.stringify(data)},
|
||||||
columns:[ ${JSON.stringify(fields)}]
|
columns: ${JSON.stringify(fields)}
|
||||||
});
|
});
|
||||||
`);
|
`;
|
||||||
|
await page.evaluate(code);
|
||||||
|
await page.waitForSelector('#table');
|
||||||
|
|
||||||
|
// Handles image generation.
|
||||||
|
|
||||||
let url;
|
let url;
|
||||||
let localName;
|
let localName;
|
||||||
|
|
||||||
if (renderImage) {
|
if (renderImage) {
|
||||||
|
|
||||||
localName = Path.join('work', gbaiName, 'cache', `img${GBAdminService.getRndReadableIdentifier()}.png`);
|
localName = Path.join('work', gbaiName, 'cache', `img${GBAdminService.getRndReadableIdentifier()}.png`);
|
||||||
|
|
||||||
await page.screenshot({ path: localName });
|
await page.screenshot({ path: localName });
|
||||||
|
|
||||||
url = urlJoin(
|
url = urlJoin(
|
||||||
GBServer.globals.publicAddress,
|
GBServer.globals.publicAddress,
|
||||||
this.min.botId,
|
this.min.botId,
|
||||||
|
@ -250,16 +261,16 @@ export class SystemKeywords {
|
||||||
GBLog.info(`BASIC: Table image generated at ${url} .`);
|
GBLog.info(`BASIC: Table image generated at ${url} .`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Handles PDF generation.
|
||||||
|
|
||||||
if (renderPDF) {
|
if (renderPDF) {
|
||||||
localName = Path.join('work', gbaiName, 'cache', `img${GBAdminService.getRndReadableIdentifier()}.pdf`);
|
localName = Path.join('work', gbaiName, 'cache', `img${GBAdminService.getRndReadableIdentifier()}.pdf`);
|
||||||
|
|
||||||
url = urlJoin(
|
url = urlJoin(
|
||||||
GBServer.globals.publicAddress,
|
GBServer.globals.publicAddress,
|
||||||
this.min.botId,
|
this.min.botId,
|
||||||
'cache',
|
'cache',
|
||||||
Path.basename(localName)
|
Path.basename(localName)
|
||||||
);
|
);
|
||||||
|
|
||||||
let pdf = await page.pdf({ format: 'A4' });
|
let pdf = await page.pdf({ format: 'A4' });
|
||||||
GBLog.info(`BASIC: Table PDF generated at ${url} .`);
|
GBLog.info(`BASIC: Table PDF generated at ${url} .`);
|
||||||
}
|
}
|
||||||
|
@ -280,10 +291,10 @@ export class SystemKeywords {
|
||||||
}
|
}
|
||||||
|
|
||||||
public async executeSQL(data, sql, tableName) {
|
public async executeSQL(data, sql, tableName) {
|
||||||
|
const first = data.shift();
|
||||||
sql = `SELECT ${sql}`.replaceAll(tableName, '?');
|
data = alasql(sql, [data]);
|
||||||
|
data.unshift(first);
|
||||||
return alasql(sql, [data]);
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Add table
Reference in a new issue