Skip to content

Commit

Permalink
feat: pass entry meta to templates via the snd
Browse files Browse the repository at this point in the history
  • Loading branch information
BigJk committed Feb 17, 2024
1 parent 9666e91 commit 158b91e
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 17 deletions.
20 changes: 20 additions & 0 deletions frontend/src/js/core/templating.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { cloneDeep } from 'lodash-es';

import hash from 'object-hash';

import entry from 'js/types/entry';
import Settings from 'js/types/settings';
// @ts-ignore
import TemplatingWorker from 'js/workers/templating-worker?worker';
Expand Down Expand Up @@ -226,3 +227,22 @@ export const render = (
*/
export const containsAi = (template: string) =>
(template.includes('ai') && template.includes('endai') && template.includes('user')) || template.includes('aiPrompt(');

/**
* Add entry meta to the state it object.
* @param entry Entry object.
* @param it State it object.
* @returns State it object with entry meta.
*/
export const addEntryMeta = (entry: entry | null, it: any) => {
if (!it) {
return it;
}
return {
...it,
snd: {
id: entry?.id ?? 'skeleton-entry-id',
name: entry?.name ?? 'Skeleton Entry Name',
},
};
};
16 changes: 11 additions & 5 deletions frontend/src/js/ui/components/editor/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { fillConfigValues } from 'src/js/types/config';
import * as API from 'js/core/api';
import { createNunjucksCompletionProvider } from 'js/core/monaco/completion-nunjucks';
import { settings } from 'js/core/store';
import { render } from 'js/core/templating';
import { addEntryMeta, render } from 'js/core/templating';

import Button from 'js/ui/spectre/button';
import Label from 'js/ui/spectre/label';
Expand All @@ -31,6 +31,7 @@ import { dialogWarning, error } from 'js/ui/toast';
type TemplateEditorProps = {
template: Template;
onChange: (updated: Template) => void;
onJSONError?: (error: any | null) => void;
editMode?: boolean;
};

Expand All @@ -41,6 +42,7 @@ type TemplateEditorState = {
entriesKey: string;
entries: Entry[];
listPreview: string;
jsonSkeleton: string;
errorsPrint: PrintPreviewError[];
errorsList: PrintPreviewError[];
};
Expand All @@ -53,6 +55,7 @@ export default (): m.Component<TemplateEditorProps> => {
entriesKey: '',
entries: [],
listPreview: '',
jsonSkeleton: '{}',
errorsPrint: [],
errorsList: [],
};
Expand Down Expand Up @@ -88,7 +91,7 @@ export default (): m.Component<TemplateEditorProps> => {
render(
val,
{
it: attrs.template.skeletonData,
it: addEntryMeta(null, attrs.template.skeletonData),
sources: attrs.template.dataSources,
config: state.config,
settings: settings.value,
Expand Down Expand Up @@ -171,6 +174,7 @@ export default (): m.Component<TemplateEditorProps> => {
return {
oninit({ attrs }) {
renderListPreview(attrs.template.listTemplate, attrs);
state.jsonSkeleton = JSON.stringify(attrs.template.skeletonData, null, 2);
fetchEntries(attrs);
},
onupdate({ attrs }) {
Expand Down Expand Up @@ -318,14 +322,16 @@ export default (): m.Component<TemplateEditorProps> => {
m(Monaco, {
key: 'data-skeleton-monaco',
language: 'json',
value: JSON.stringify(attrs.template.skeletonData, null, 2),
value: state.jsonSkeleton,
className: '.flex-grow-1',
wordWrap: 'on',
onChange: (value) => {
state.jsonSkeleton = value;
try {
attrs.onChange({ ...attrs.template, skeletonData: JSON.parse(value) });
if (attrs.onJSONError) attrs.onJSONError(null);
} catch (e) {
// Monaco will show the error
if (attrs.onJSONError) attrs.onJSONError(e);
}
},
}),
Expand All @@ -346,7 +352,7 @@ export default (): m.Component<TemplateEditorProps> => {
className: '.flex-grow-1',
errors: state.errorsPrint,
completion: createNunjucksCompletionProvider({
it: attrs.template.skeletonData,
it: addEntryMeta(null, attrs.template.skeletonData),
images: attrs.template.images,
settings: settings.value,
sources: attrs.template.dataSources,
Expand Down
9 changes: 6 additions & 3 deletions frontend/src/js/ui/components/print-preview-template.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import m from 'mithril';
import { cloneDeep, debounce, isEqual } from 'lodash-es';

import Entry from 'js/types/entry';
import Generator from 'js/types/generator';
import Template from 'js/types/template';
import store, { settings } from 'js/core/store';
import { containsAi, render } from 'js/core/templating';
import { addEntryMeta, containsAi, render } from 'js/core/templating';

import Button from 'js/ui/spectre/button';

Expand All @@ -24,6 +25,7 @@ export type PrintPreviewTemplateProps = {
generator?: Generator;
useListTemplate?: boolean;
it?: any;
entry?: Entry;
config?: any;
hideAiNotice?: boolean;
width?: number;
Expand Down Expand Up @@ -54,7 +56,8 @@ export default (): m.Component<PrintPreviewTemplateProps> => {
isEqual(lastProps.template, attrs.template) &&
isEqual(lastProps.it, attrs.it) &&
isEqual(lastProps.generator, attrs.generator) &&
isEqual(lastProps.config, attrs.config)
isEqual(lastProps.config, attrs.config) &&
isEqual(lastProps.entry, attrs.entry)
) {
return;
}
Expand All @@ -65,7 +68,7 @@ export default (): m.Component<PrintPreviewTemplateProps> => {

if (attrs.template === null && attrs.generator === null) return;
const printTemplate = getTemplate(attrs);
const it = attrs.it ?? attrs.template?.skeletonData;
const it = addEntryMeta(attrs.entry ?? null, attrs.it) ?? addEntryMeta(null, attrs.template?.skeletonData);

render(printTemplate ?? '', {
it: it ?? {},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import m from 'mithril';

import Entry from 'js/types/entry';
import Generator from 'js/types/generator';
import Template from 'js/types/template';

Expand All @@ -17,6 +18,7 @@ type SidebarPrintProps = {
template?: Template;
generator?: Generator;
it?: any;
entry?: Entry;
config?: any;
tabs: TabDefinition[];
content: Record<string, () => m.Component>;
Expand Down Expand Up @@ -55,6 +57,7 @@ export default (): m.Component<SidebarPrintProps> => {
template: vnode.attrs.template,
generator: vnode.attrs.generator,
it: vnode.attrs.it,
entry: vnode.attrs.entry,
config: vnode.attrs.config,
width: 380,
className: '.bg-black-05.ph1.ba.b--black-10',
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/js/ui/views/extern-print/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import m from 'mithril';
import Template from 'js/types/template';
import * as API from 'js/core/api';
import { settings } from 'js/core/store';
import { render } from 'js/core/templating';
import { addEntryMeta, render } from 'js/core/templating';

type ExternPrintProps = {
id: string;
Expand Down Expand Up @@ -34,7 +34,7 @@ export default (): m.Component<ExternPrintProps> => {
API.exec<Template>(API.GET_TEMPLATE, state.id)
.then((tmpl) => {
render(tmpl.printTemplate, {
it: state.json,
it: addEntryMeta({ name: '', id: '', data: {} }, state.json), // TODO: rework this
images: tmpl.images,
sources: tmpl.dataSources,
config: JSON.parse(atob(attrs.config)),
Expand Down
6 changes: 1 addition & 5 deletions frontend/src/js/ui/views/template/create-entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,6 @@ export default (): m.Component<CreateTemplateEntityProps> => {
})
.catch(error);
}

// Testing stuff
// console.log(state.schema);
// console.log(getCategories());
// console.log(initialData(state.schema));
});
},
view({ attrs }) {
Expand Down Expand Up @@ -206,6 +201,7 @@ export default (): m.Component<CreateTemplateEntityProps> => {
m(SidebarPrintPage, {
template: state.template,
it: state.data,
entry: state,
tabs: [{ icon: 'filing', label: 'Entry' }],
content: {
Entry: () =>
Expand Down
11 changes: 10 additions & 1 deletion frontend/src/js/ui/views/template/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type TemplateCreateProps = {

export default (): m.Component<TemplateCreateProps> => {
let state: Template = createEmptyTemplate();
let hadJSONError = false;

return {
oninit({ attrs }) {
Expand Down Expand Up @@ -47,7 +48,12 @@ export default (): m.Component<TemplateCreateProps> => {
size: 'sm',
intend: 'success',
onClick: () => {
if (!state) return;
if (hadJSONError) {
error('Errors in the "Data Skeleton", please fix them before saving.');
return;
}

if (!state || hadJSONError) return;
if (attrs.id && buildId('template', state) === attrs.id) {
error('You cannot duplicate a template with the same slug as the original.');
return;
Expand Down Expand Up @@ -86,6 +92,9 @@ export default (): m.Component<TemplateCreateProps> => {
state = template;
m.redraw();
},
onJSONError: (error) => {
hadJSONError = !!error;
},
}),
);
},
Expand Down
11 changes: 10 additions & 1 deletion frontend/src/js/ui/views/template/edit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type EditTemplateProps = {

export default (): m.Component<EditTemplateProps> => {
let state: Template | null = null;
let hadJSONError = false;

return {
oninit({ attrs }) {
Expand Down Expand Up @@ -52,7 +53,12 @@ export default (): m.Component<EditTemplateProps> => {
size: 'sm',
intend: 'success',
onClick: () => {
if (!state) return;
if (hadJSONError) {
error('Errors in the "Data Skeleton", please fix them before saving.');
return;
}

if (!state || hadJSONError) return;
API.exec<void>(API.SAVE_TEMPLATE, state)
.then(() => {
if (!state) return;
Expand Down Expand Up @@ -90,6 +96,9 @@ export default (): m.Component<EditTemplateProps> => {
state = template;
m.redraw();
},
onJSONError: (error) => {
hadJSONError = !!error;
},
editMode: true,
})
: m(Loader),
Expand Down
1 change: 1 addition & 0 deletions frontend/src/js/ui/views/template/single.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,7 @@ export default (): m.Component<SingleTemplateProps> => {
m(SidebarPrintPage, {
template: state.template,
it: state.selectedEntry?.data,
entry: state.selectedEntry,
onRendered: (html) => (state.lastRendered = html),
tabs: [
{ icon: 'filing', label: 'Entries' },
Expand Down

0 comments on commit 158b91e

Please sign in to comment.