Add svelte as a view engine to swaf #33
@ -1,8 +1,18 @@
|
||||
import {Router} from "express";
|
||||
|
||||
import ApplicationComponent from "../ApplicationComponent.js";
|
||||
import FrontendToolsComponent from "./FrontendToolsComponent.js";
|
||||
|
||||
export default class FormHelperComponent extends ApplicationComponent {
|
||||
|
||||
public async init(): Promise<void> {
|
||||
const globals = this.getApp().asOptional(FrontendToolsComponent)?.getGlobals();
|
||||
if (globals) {
|
||||
globals.set('validation', () => ({}));
|
||||
globals.set('previousFormData', () => ({}));
|
||||
}
|
||||
}
|
||||
|
||||
public async initRoutes(router: Router): Promise<void> {
|
||||
router.use((req, res, next) => {
|
||||
let _validation: unknown | null;
|
||||
@ -24,15 +34,6 @@ export default class FormHelperComponent extends ApplicationComponent {
|
||||
|
||||
return _previousFormData;
|
||||
};
|
||||
|
||||
let _formPrefix: string | null;
|
||||
res.locals.getFormPrefix = () => {
|
||||
return _formPrefix;
|
||||
};
|
||||
res.locals.setFormPrefix = (formPrefix: string) => {
|
||||
_formPrefix = formPrefix;
|
||||
return '';
|
||||
};
|
||||
next();
|
||||
});
|
||||
|
||||
|
@ -242,8 +242,8 @@ export default class SvelteViewEngine extends ViewEngine {
|
||||
}
|
||||
|
||||
let backendCall = output.substring(startIndex, endIndex);
|
||||
if (backendCall.match(/([^()]+)\((.+?)\)/)) {
|
||||
backendCall = backendCall.replace(/([^()]+)\((.+?)\)/, "'$1', `[$2]`");
|
||||
if (backendCall.match(/([^()]+)\((.*?)\)/)) {
|
||||
backendCall = backendCall.replace(/([^()]+)\((.*?)\)/, "'$1', `[$2]`");
|
||||
} else {
|
||||
backendCall = backendCall.replace(/([^()]+)(\(\))?/, "'$1'");
|
||||
}
|
||||
@ -309,7 +309,10 @@ export default class SvelteViewEngine extends ViewEngine {
|
||||
if (arg.startsWith("'")) return '"' + arg.substring(1, arg.length - 1) + '"';
|
||||
return arg;
|
||||
})
|
||||
.map(arg => Function(`"use strict";const $locals = arguments[0];return (${arg});`)(locals)); // Uses named parameter locals
|
||||
.filter(arg => arg.length > 0)
|
||||
.map(arg => {
|
||||
return Function(`"use strict";const $locals = arguments[0];return (${arg});`)(locals);
|
||||
}); // Uses named parameter locals
|
||||
|
||||
const f = locals[key];
|
||||
if (typeof f !== 'function') throw new Error(key + ' is not a function.');
|
||||
|
Loading…
Reference in New Issue
Block a user