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) => {
|
||||
|
||||
let tableName = /\sFROM\s(\w+)/.exec($1)[0];
|
||||
|
||||
return `sys().executeSQL(${$2}, ${$1}, ${tableName})\n`;
|
||||
let tableName = /\sFROM\s(\w+)/.exec($2)[1];
|
||||
let sql = `SELECT ${$2}`.replace(tableName, '?');
|
||||
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) => {
|
||||
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) => {
|
||||
|
|
|
@ -188,7 +188,9 @@ export class SystemKeywords {
|
|||
* @see puppeteer.
|
||||
*/
|
||||
private async renderTable(data, renderPDF, renderImage) {
|
||||
|
||||
if (!data[1]){
|
||||
return null;
|
||||
}
|
||||
const gbaiName = `${this.min.botId}.gbai`;
|
||||
const browser = await puppeteer.launch({ headless: false });
|
||||
const page = await browser.newPage();
|
||||
|
@ -210,37 +212,46 @@ export class SystemKeywords {
|
|||
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 = [];
|
||||
for (let i = 0; i < data.length; i++) {
|
||||
fields.push({field:data[i]});
|
||||
let keys = Object.keys(data[1]);
|
||||
for (let i = 0; i < keys.length; i++) {
|
||||
fields.push({field: keys[i], title: keys[i]});
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Adds DIV for Tabulator.
|
||||
|
||||
await page.evaluate(() => {
|
||||
const el = document.createElement("div");
|
||||
el.id = "table";
|
||||
document.body.prepend(el);
|
||||
el.setAttribute("id", "table");
|
||||
document.body.appendChild(el);
|
||||
});
|
||||
|
||||
await page.evaluate(`
|
||||
new Tabulator("#example-table", {
|
||||
const code = `
|
||||
var table = new Tabulator("#table", {
|
||||
height:"311px",
|
||||
layout:"fitColumns",
|
||||
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 localName;
|
||||
|
||||
if (renderImage) {
|
||||
|
||||
localName = Path.join('work', gbaiName, 'cache', `img${GBAdminService.getRndReadableIdentifier()}.png`);
|
||||
|
||||
await page.screenshot({ path: localName });
|
||||
|
||||
url = urlJoin(
|
||||
GBServer.globals.publicAddress,
|
||||
this.min.botId,
|
||||
|
@ -250,16 +261,16 @@ export class SystemKeywords {
|
|||
GBLog.info(`BASIC: Table image generated at ${url} .`);
|
||||
}
|
||||
|
||||
// Handles PDF generation.
|
||||
|
||||
if (renderPDF) {
|
||||
localName = Path.join('work', gbaiName, 'cache', `img${GBAdminService.getRndReadableIdentifier()}.pdf`);
|
||||
|
||||
url = urlJoin(
|
||||
GBServer.globals.publicAddress,
|
||||
this.min.botId,
|
||||
'cache',
|
||||
Path.basename(localName)
|
||||
);
|
||||
|
||||
let pdf = await page.pdf({ format: 'A4' });
|
||||
GBLog.info(`BASIC: Table PDF generated at ${url} .`);
|
||||
}
|
||||
|
@ -280,10 +291,10 @@ export class SystemKeywords {
|
|||
}
|
||||
|
||||
public async executeSQL(data, sql, tableName) {
|
||||
|
||||
sql = `SELECT ${sql}`.replaceAll(tableName, '?');
|
||||
|
||||
return alasql(sql, [data]);
|
||||
const first = data.shift();
|
||||
data = alasql(sql, [data]);
|
||||
data.unshift(first);
|
||||
return data;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Add table
Reference in a new issue