Skip to content

Commit f45baa6

Browse files
committed
fix: data sources tab of generator
1 parent d767fb8 commit f45baa6

File tree

10 files changed

+40
-9
lines changed

10 files changed

+40
-9
lines changed

frontend/src/js/core/monaco/completion-nunjucks.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ const buildLabel = (context: any, path: string) => {
2525
return label;
2626
};
2727

28+
const buildType = (value: any) => {
29+
const type = typeof value;
30+
if (type === 'object' && Array.isArray(value)) {
31+
return 'array';
32+
}
33+
return type;
34+
};
35+
2836
/**
2937
* Create a completion provider for Nunjucks templates.
3038
* @param context The context object containing the variables that are available in the template.
@@ -33,7 +41,7 @@ export const createNunjucksCompletionProvider = (context: any): CompletionFuncti
3341
// Context is an object. We want to recursively create a string array with the paths to all the fields in the context object.
3442
// We will use this to easily check if the word at the cursor matches any of the fields.
3543
// Example: [a.b.c, a.b.d, a.e, f]
36-
let contextFields: string[] = [];
44+
let contextFields: string[] = [...Object.keys(context)];
3745
let contextFieldPaths = (context: any, path: string) => {
3846
if (typeof context === 'object') {
3947
for (let key in context) {
@@ -49,7 +57,9 @@ export const createNunjucksCompletionProvider = (context: any): CompletionFuncti
4957
}
5058
}
5159
} else {
52-
contextFields.push(path);
60+
if (!contextFields.includes(path)) {
61+
contextFields.push(path);
62+
}
5363
}
5464
};
5565
contextFieldPaths(context, '');
@@ -110,7 +120,7 @@ export const createNunjucksCompletionProvider = (context: any): CompletionFuncti
110120
kind: monaco.languages.CompletionItemKind.Field,
111121
insertText: field,
112122
range: range,
113-
detail: typeof get(context, field),
123+
detail: buildType(get(context, field)),
114124
}));
115125
}),
116126
);

frontend/src/js/core/templating.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ const aiScript = `
144144
export type TemplateState = {
145145
it: any;
146146
config: any;
147+
sources: string[];
147148
settings: Settings;
148149
images: Record<string, string>;
149150
};
@@ -152,8 +153,9 @@ export type TemplateState = {
152153
* State object for generator rendering.
153154
*/
154155
export type GeneratorState = {
155-
settings: Settings;
156156
config: any;
157+
sources: string[];
158+
settings: Settings;
157159
images: Record<string, string>;
158160
};
159161

frontend/src/js/ui/components/editor/generator.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ export default (): m.Component<GeneratorEditorProps> => {
9292
m('div.ph3', [
9393
m(EditorHeader, {
9494
title: 'Data Sources',
95-
description: 'Add and remove data sources. Entries of these data sources will be linked to this template.',
95+
description:
96+
'Add and remove static data sources that should not be configurable by the user. Available under the "sources" variable in the template.',
9697
}), //
9798
m(
9899
'div.mb3',
@@ -181,6 +182,7 @@ export default (): m.Component<GeneratorEditorProps> => {
181182
config: state.config,
182183
images: attrs.generator.images,
183184
settings: settings.value,
185+
sources: attrs.generator.dataSources,
184186
}),
185187
errors: state.errors,
186188
onChange: (value) => {

frontend/src/js/ui/components/editor/template.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ export default (): m.Component<TemplateEditorProps> => {
8989
val,
9090
{
9191
it: attrs.template.skeletonData,
92+
sources: attrs.template.dataSources,
9293
config: state.config,
9394
settings: settings.value,
9495
images: {},
@@ -348,6 +349,7 @@ export default (): m.Component<TemplateEditorProps> => {
348349
it: attrs.template.skeletonData,
349350
images: attrs.template.images,
350351
settings: settings.value,
352+
sources: attrs.template.dataSources,
351353
}),
352354
onChange: (value) => attrs.onChange({ ...attrs.template, printTemplate: value }),
353355
}),
@@ -376,6 +378,7 @@ export default (): m.Component<TemplateEditorProps> => {
376378
it: attrs.template.skeletonData,
377379
images: attrs.template.images,
378380
settings: settings.value,
381+
sources: attrs.template.dataSources,
379382
}),
380383
onChange: (value) => {
381384
attrs.onChange({ ...attrs.template, listTemplate: value });

frontend/src/js/ui/components/print-preview-template.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ export default (): m.Component<PrintPreviewTemplateProps> => {
7070
render(printTemplate ?? '', {
7171
it: it ?? {},
7272
config: attrs.config ?? {},
73+
sources: attrs.generator?.dataSources ?? attrs.template?.dataSources ?? [],
7374
settings: settings.value,
7475
images: attrs.template?.images ?? attrs.generator?.images ?? {},
7576
aiEnabled: aiEnabled,

frontend/src/js/ui/components/view-layout/property-header.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ type HeaderProps = {
1212

1313
export default (): m.Component<HeaderProps> => ({
1414
view({ attrs }) {
15-
let element = [m(`div.f4.pt3.lh-copy`, attrs.title), attrs.description ? m('div.f7.text-muted.mb3', attrs.description) : null];
15+
let element = [m(`div.f4.pt3.lh-copy`, attrs.title), attrs.description ? m('div.f7.text-muted.mb3.lh-copy', attrs.description) : null];
1616
if (attrs.icon) {
1717
return m(Flex, { items: 'center', className: `${attrs.className ?? ''}` }, [
1818
m(Icon, { icon: attrs.icon, size: 3, className: '.mr3' }),

frontend/src/js/ui/views/extern-print/generator.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,13 @@ export default (): m.Component<ExternPrintProps> => {
3535

3636
API.exec<Generator>(API.GET_GENERATOR, state.id)
3737
.then((gen) => {
38-
render(gen.printTemplate, { it: state.json, images: gen.images, config: JSON.parse(atob(attrs.config)), settings: settings.value })
38+
render(gen.printTemplate, {
39+
it: state.json,
40+
sources: gen.dataSources,
41+
images: gen.images,
42+
config: JSON.parse(atob(attrs.config)),
43+
settings: settings.value,
44+
})
3945
.then((res) => {
4046
state.gen = res;
4147
})

frontend/src/js/ui/views/extern-print/template.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,13 @@ export default (): m.Component<ExternPrintProps> => {
3333

3434
API.exec<Template>(API.GET_TEMPLATE, state.id)
3535
.then((tmpl) => {
36-
render(tmpl.printTemplate, { it: state.json, images: tmpl.images, config: JSON.parse(atob(attrs.config)), settings: settings.value })
36+
render(tmpl.printTemplate, {
37+
it: state.json,
38+
images: tmpl.images,
39+
sources: tmpl.dataSources,
40+
config: JSON.parse(atob(attrs.config)),
41+
settings: settings.value,
42+
})
3743
.then((res) => {
3844
state.tmpl = res;
3945
})

frontend/src/js/ui/views/generator/create.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export default (): m.Component<GeneratorCreateProps> => {
3939
confirm: true,
4040
confirmText: 'Are you sure you want to leave this page? Changes are not saved.',
4141
items: [
42-
{ link: '/generator', label: 'Templates' },
42+
{ link: '/generator', label: 'Generators' },
4343
{ link: `/generator/${state ? buildId('generator', state) : ''}`, label: state ? state.name : m(Loader, { className: '.mh2' }) },
4444
{ label: 'Edit' },
4545
],

frontend/src/js/ui/views/template/single.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ export default (): m.Component<SingleTemplateProps> => {
8383
state.template?.listTemplate!,
8484
{
8585
it: e.data,
86+
sources: state.template?.dataSources!,
8687
config: {},
8788
settings: settings.value,
8889
images: {}, // Don't need images for list template

0 commit comments

Comments
 (0)