Skip to content

Commit

Permalink
Include text-based inputs inside X-data when translating #8009
Browse files Browse the repository at this point in the history
  • Loading branch information
ashklianko committed Oct 30, 2024
1 parent 0354567 commit 99f0e26
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 4 deletions.
44 changes: 41 additions & 3 deletions modules/lib/src/main/resources/assets/js/app/ai/AI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {StringHelper} from '@enonic/lib-admin-ui/util/StringHelper';
import {Content} from '../content/Content';
import {ContentType} from '../inputtype/schema/ContentType';
import {GetLocalesRequest} from '../resource/GetLocalesRequest';
import {ContentData, ContentLanguage, ContentSchema} from './event/data/AiData';
import {ContentData, ContentLanguage, ContentSchema, ContentXData} from './event/data/AiData';
import {EnonicAiAppliedData} from './event/data/EnonicAiAppliedData';
import {EnonicAiContentOperatorSetupData} from './event/data/EnonicAiContentOperatorSetupData';
import {EnonicAiTranslatorSetupData} from './event/data/EnonicAiTranslatorSetupData';
Expand All @@ -21,6 +21,9 @@ import {AiTranslatorStartedEvent} from './event/incoming/AiTranslatorStartedEven
import {AiContentOperatorConfigureEvent} from './event/outgoing/AiContentOperatorConfigureEvent';
import {AiTranslatorConfigureEvent} from './event/outgoing/AiTranslatorConfigureEvent';
import {AiUpdateDataEvent} from './event/outgoing/AiUpdateDataEvent';
import {ExtraData} from '../content/ExtraData';
import {XData} from '../content/XData';
import {XDataWizardStepForm} from '../wizard/XDataWizardStepForm';

declare global {
interface Window {
Expand Down Expand Up @@ -61,6 +64,8 @@ export class AI {

private locales: Locale[];

private xDataSchemas: XData[];

private instructions: Record<EnonicAiPlugin, string | undefined>;

private resultReceivedListeners: ((data: EnonicAiAppliedData) => void)[] = [];
Expand Down Expand Up @@ -103,6 +108,7 @@ export class AI {
new AiUpdateDataEvent({
data: this.createContentData(),
language: this.createContentLanguage(),
xData: this.createXData(),
}).fire();
this.checkAndNotifyReady();
}
Expand All @@ -123,6 +129,11 @@ export class AI {
new AiUpdateDataEvent({language: this.createContentLanguage()}).fire();
}

setXDataSchemas(xDataSchemas: XData[]): void {
this.xDataSchemas = xDataSchemas;
new AiUpdateDataEvent({xDataSchemas: this.createXDataSchemas()}).fire();
}

updateInstructions(configs: ApplicationConfig[]): void {
this.instructions = {
contentOperator: undefined,
Expand Down Expand Up @@ -181,11 +192,12 @@ export class AI {
}

private translatorStartedEventListener = (event: AiTranslatorStartedEvent) => {
AiHelper.getAiHelperByPath(event.path)?.setState(AiHelperState.PROCESSING);
const helper = event.prefix ? this.getAiHelperByXData(event.prefix, event.path) : AiHelper.getAiHelperByPath(event.path);
helper?.setState(AiHelperState.PROCESSING);
};

private translatorCompletedEventListener = (event: AiTranslatorCompletedEvent) => {
const helper = AiHelper.getAiHelperByPath(event.path);
const helper = event.prefix ? this.getAiHelperByXData(event.prefix, event.path) : AiHelper.getAiHelperByPath(event.path);
helper?.setValue(event.value);
helper?.setState(AiHelperState.COMPLETED);
};
Expand All @@ -198,6 +210,13 @@ export class AI {
});
}

private createXData(): ContentXData[] {
return this.content ? this.content.getAllExtraData().map((extraData: ExtraData) => ({
name: extraData.getName().toString(),
fields: extraData.getData().toJson(),
})) : [];
}

private createContentLanguage(): ContentLanguage | undefined {
if (!this.content) {
return;
Expand All @@ -217,6 +236,15 @@ export class AI {
};
}

private createXDataSchemas(): ContentSchema[] {
return this.xDataSchemas ? this.xDataSchemas.map(xData => {
return {
form: xData.toForm().toJson(),
name: xData.getName(),
};
}): [];
}

private applyContentOperatorEventListener = (event: AiContentOperatorResultAppliedEvent) => {
const {topic} = event.result;
const hasDisplayNameChanged = !StringHelper.isEmpty(topic) && topic !== this.content.getDisplayName();
Expand Down Expand Up @@ -253,4 +281,14 @@ export class AI {
this.readyListeners = [];
}
}

private getAiHelperByXData(xDataName: string, path: string): AiHelper | undefined {
const xDataStepForm = XDataWizardStepForm.getXDataWizardStepForm(xDataName);

if (xDataStepForm) {
return AiHelper.getAiHelpersByParent(xDataStepForm).find(aiHelper => aiHelper.getDataPath() === path);
}

return undefined;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ export interface AiData {
data?: ContentData;
schema?: ContentSchema;
language?: ContentLanguage;
xData?: ContentXData[];
xDataSchemas?: ContentSchema[];
}

export interface ContentData {
Expand All @@ -21,3 +23,8 @@ export interface ContentLanguage {
tag: string;
name: string;
}

export interface ContentXData {
name: string;
fields: PropertyArrayJson[];
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ export class AiTranslatorCompletedEvent

readonly value: string;

readonly prefix?: string;

private constructor() {
super();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ export class AiTranslatorStartedEvent

readonly path: string;

readonly prefix?: string;

private constructor() {
super();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1976,6 +1976,8 @@ export class ContentWizardPanel
}

private createXDataWizardStepForms(xDatas: XData[]): XDataWizardStepForm[] {
AI.get().setXDataSchemas(xDatas);

const added: XDataWizardStepForm[] = [];

xDatas.forEach((xData: XData) => {
Expand Down Expand Up @@ -2832,7 +2834,7 @@ export class ContentWizardPanel

const xDataFormContext = ContentFormContext.create()
.setContentTypeName(type)
.setAiEditable(false)
.setAiEditable(true)
.setValidationErrors(content.getValidationErrors().filter(ValidationErrorHelper.isCustomError))
.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,15 @@ export class XDataWizardStepForm

private enableChangedListeners: ((value: boolean) => void)[] = [];

private static REGISTRY = new Map<string, XDataWizardStepForm>();

constructor(xData: XData) {
super();
this.addClass('x-data-wizard-step-form');

this.xData = xData;

XDataWizardStepForm.REGISTRY.set(this.xData.getName(), this);
}

getXData(): XData {
Expand Down Expand Up @@ -187,4 +191,8 @@ export class XDataWizardStepForm

return persistedXData?.getData()?.getRoot()?.getPropertyArrays().length > 0;
}

static getXDataWizardStepForm(name: string): XDataWizardStepForm | undefined {
return XDataWizardStepForm.REGISTRY.get(name);
}
}

0 comments on commit 99f0e26

Please sign in to comment.