aboutsummaryrefslogtreecommitdiff
path: root/src/client/views/nodes/DataVizBox/DocCreatorMenu/Backend
diff options
context:
space:
mode:
authorbobzel <zzzman@gmail.com>2025-06-01 16:07:30 -0400
committerbobzel <zzzman@gmail.com>2025-06-01 16:07:30 -0400
commit215f874c5f7f28964a3428909fcee0f667388bae (patch)
treee9881d33b845032725e75cdf880d4637507e9a32 /src/client/views/nodes/DataVizBox/DocCreatorMenu/Backend
parent451891b0e34c84873ccc89dde55442bb9bf23f3d (diff)
a number of fixes to document creator to fix preview of templates and templates with images.
Diffstat (limited to 'src/client/views/nodes/DataVizBox/DocCreatorMenu/Backend')
-rw-r--r--src/client/views/nodes/DataVizBox/DocCreatorMenu/Backend/TemplateManager.ts79
-rw-r--r--src/client/views/nodes/DataVizBox/DocCreatorMenu/Backend/TemplateMenuAIUtils.ts34
2 files changed, 41 insertions, 72 deletions
diff --git a/src/client/views/nodes/DataVizBox/DocCreatorMenu/Backend/TemplateManager.ts b/src/client/views/nodes/DataVizBox/DocCreatorMenu/Backend/TemplateManager.ts
index 78235d000..526fcf9c4 100644
--- a/src/client/views/nodes/DataVizBox/DocCreatorMenu/Backend/TemplateManager.ts
+++ b/src/client/views/nodes/DataVizBox/DocCreatorMenu/Backend/TemplateManager.ts
@@ -14,87 +14,64 @@ export type Conditional = {
target: string;
attribute: string;
value: string;
-}
+};
export class TemplateManager {
+ _templates: Template[] = [];
- templates: Template[] = [];
-
- conditionalFieldLogic: Record<string, Conditional[]> = {};
+ _conditionalFieldLogic: Record<string, Conditional[]> = {};
constructor(templateSettings: FieldSettings[]) {
makeAutoObservable(this);
- this.templates = this.initializeTemplates(templateSettings);
+ this._templates = templateSettings.map(settings => new Template(settings));
}
- initializeTemplates = (templateSettings: FieldSettings[]) => templateSettings.map(settings => {
- return new Template(settings)});
-
- getValidTemplates = (cols: Col[]) => this.templates.filter(template => template.isValidTemplate(cols));
+ getValidTemplates = (cols: Col[]) => this._templates.filter(template => template.isValidTemplate(cols));
- addTemplate = (newTemplate: Template) => this.templates.push(newTemplate);
+ addTemplate = (newTemplate: Template) => this._templates.push(newTemplate);
removeTemplate = (template: Template) => {
- this.templates.splice(this.templates.indexOf(template), 1);
+ if (this._templates.includes(template)) {
+ this._templates.splice(this._templates.indexOf(template), 1);
+ }
template.cleanup();
};
addFieldCondition = (fieldTitle: string, condition: Conditional) => {
- if (this.conditionalFieldLogic[fieldTitle] === undefined) {
- this.conditionalFieldLogic[fieldTitle] = [condition];
+ if (this._conditionalFieldLogic[fieldTitle] === undefined) {
+ this._conditionalFieldLogic[fieldTitle] = [condition];
} else {
- this.conditionalFieldLogic[fieldTitle].push(condition);
+ this._conditionalFieldLogic[fieldTitle].push(condition);
}
- }
+ };
- removeFieldCondition = (fieldTitle: string, condition: Conditional) => {
- if (this.conditionalFieldLogic[fieldTitle]) {
- this.conditionalFieldLogic[fieldTitle] = this.conditionalFieldLogic[fieldTitle].filter(cond => cond !== condition);
- }
- }
+ removeFieldCondition = (fieldTitle: string, condition: Conditional) => (this._conditionalFieldLogic[fieldTitle] = this._conditionalFieldLogic[fieldTitle]?.filter(cond => cond !== condition));
- addDataField = (title: string) => {
- this.templates.forEach(template => template.addDataField(title));
- }
+ addDataField = (title: string) => this._templates.forEach(template => template.addDataField(title));
- removeDataField = (title: string) => {
- this.templates.forEach(template => template.removeDataField(title));
- }
+ removeDataField = (title: string) => this._templates.forEach(template => template.removeDataField(title));
createDocsFromTemplate = action((dv: DataVizBox, template: Template, cols: Col[], debug: boolean = false) => {
const csvFields = Array.from(Object.keys(dv.records[0]));
- const processContent = async (content: { [title: string]: string }) => {
+ const processContent = (content: { [title: string]: string }) => {
const templateCopy = template.clone();
csvFields
.filter(title => title)
.forEach(title => {
const field = templateCopy.getFieldByTitle(title);
- field && field.setContent(content[title], field.viewType);
+ field?.setContent(content[title], field.viewType);
});
const gptFunc = (type: TemplateFieldType) => (type === TemplateFieldType.VISUAL ? TemplateMenuAIUtils.renderGPTImageCall : TemplateMenuAIUtils.renderGPTTextCall);
- const applyGPTContent = async () => {
- const promises = cols
- .filter(field => field.AIGenerated)
- .map(field => {
- const templateField: TemplateField = templateCopy.getFieldByTitle(field.title) as TemplateField;
- if (templateField !== undefined) {
- return gptFunc(field.type)(templateCopy, field, templateField.getID);
- }
- return null;
- })
- .filter(p => p !== null);
-
- await Promise.all(promises);
- };
-
- await applyGPTContent();
-
- templateCopy.applyConditionalLogic(this.conditionalFieldLogic);
-
- return templateCopy.getRenderedDoc();
+
+ const generateGptContent = cols
+ .map(field => ({ field, templateField: field?.AIGenerated && templateCopy.getFieldByTitle(field.title) }))
+ .filter(({ templateField }) => templateField instanceof TemplateField)
+ .map(({ field, templateField }) => gptFunc(field.type)(templateCopy, field, (templateField as TemplateField).getID));
+
+ return Promise.all(generateGptContent).then(() => templateCopy.applyConditionalLogic(this._conditionalFieldLogic));
};
const rowContents = debug
@@ -109,10 +86,6 @@ export class TemplateManager {
)
);
- return Promise.all(rowContents.map(processContent)).then(
- action(renderedDocs => {
- return renderedDocs;
- })
- );
+ return Promise.all(rowContents.map(processContent));
});
}
diff --git a/src/client/views/nodes/DataVizBox/DocCreatorMenu/Backend/TemplateMenuAIUtils.ts b/src/client/views/nodes/DataVizBox/DocCreatorMenu/Backend/TemplateMenuAIUtils.ts
index 162b7a1b1..08818dd6c 100644
--- a/src/client/views/nodes/DataVizBox/DocCreatorMenu/Backend/TemplateMenuAIUtils.ts
+++ b/src/client/views/nodes/DataVizBox/DocCreatorMenu/Backend/TemplateMenuAIUtils.ts
@@ -1,13 +1,13 @@
-import { ClientUtils } from "../../../../../../ClientUtils";
-import { Networking } from "../../../../../Network";
-import { gptImageCall, gptAPICall, GPTCallType } from "../../../../../apis/gpt/GPT";
-import { Col } from "../DocCreatorMenu";
-import { TemplateFieldSize, TemplateFieldType } from "../TemplateBackend";
-import { TemplateField, ViewType } from "../TemplateFieldTypes/TemplateField";
-import { Template } from "../Template";
+import { ClientUtils } from '../../../../../../ClientUtils';
+import { Networking } from '../../../../../Network';
+import { gptImageCall, gptAPICall, GPTCallType } from '../../../../../apis/gpt/GPT';
+import { Col } from '../DocCreatorMenu';
+import { TemplateFieldSize, TemplateFieldType } from '../TemplateBackend';
+import { ViewType } from '../TemplateFieldTypes/TemplateField';
+import { Template } from '../Template';
+import { Upload } from '../../../../../../server/SharedMediaTypes';
export class TemplateMenuAIUtils {
-
public static generateGPTImage = async (prompt: string): Promise<string | undefined> => {
try {
const res = await gptImageCall(prompt);
@@ -24,13 +24,10 @@ export class TemplateMenuAIUtils {
public static renderGPTImageCall = async (template: Template, col: Col, fieldNumber: number): Promise<boolean> => {
const generateAndLoadImage = async (id: number, prompt: string) => {
+ const field = template.getFieldByID(id);
const url = await this.generateGPTImage(prompt);
- // eslint-disable-next-line
- var field: TemplateField = template.getFieldByID(id);
-
- field.setContent(url ?? '', ViewType.IMG);
- field = template.getFieldByID(id);
- field.setTitle(col.title);
+ field?.setContent(url ?? '', ViewType.IMG);
+ field?.setTitle(col.title);
};
const fieldContent: string = template.compiledContent;
@@ -81,10 +78,10 @@ export class TemplateMenuAIUtils {
if (res) {
const assignments: { [title: string]: { number: string; content: string } } = JSON.parse(res);
Object.entries(assignments).forEach(([, /* title */ info]) => {
- const field: TemplateField = template.getFieldByID(Number(info.number));
+ const field = template.getFieldByID(Number(info.number));
- field.setContent(info.content ?? '', ViewType.TEXT);
- field.setTitle(col.title);
+ field?.setContent(info.content ?? '', ViewType.TEXT);
+ field?.setTitle(col.title);
});
}
} catch (err) {
@@ -122,5 +119,4 @@ export class TemplateMenuAIUtils {
return template;
};
-
-} \ No newline at end of file
+}