Skip to content

Commit ca36b58

Browse files
committed
Custom widgets - prefix name contract to model fix
1 parent b93f87f commit ca36b58

File tree

3 files changed

+11
-13
lines changed

3 files changed

+11
-13
lines changed

src/components/custom-widget/customWidgetModelBinder.publish.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,12 @@ import { Contract } from "@paperbits/common";
33
import { CustomWidgetModel } from "./customWidgetModel";
44
import { CustomWidgetContract } from "./customWidgetContract";
55
import { widgetName } from "./constants";
6+
import { customWidgetPrefixName, customWidgetRemovePrefixName } from "./ko/utils";
67

78
export class CustomWidgetModelBinder implements IModelBinder<CustomWidgetModel> {
8-
public canHandleModel(model: unknown): boolean {
9-
return model instanceof CustomWidgetModel;
10-
}
11-
129
public async contractToModel(contract: CustomWidgetContract): Promise<CustomWidgetModel> {
1310
const model = new CustomWidgetModel();
14-
model.name = contract.name ?? "";
11+
model.name = customWidgetPrefixName(contract.name) ?? "";
1512
model.displayName = contract.displayName || contract.widgetDisplayName;
1613
model.customInputValue = contract.customInputValue ?? "{}";
1714
model.instanceId = contract.instanceKey;
@@ -22,7 +19,7 @@ export class CustomWidgetModelBinder implements IModelBinder<CustomWidgetModel>
2219
public modelToContract(model: CustomWidgetModel): Contract {
2320
return {
2421
type: widgetName,
25-
name: model.name,
22+
name: customWidgetRemovePrefixName(model.name),
2623
displayName: model.displayName,
2724
customInputValue: model.customInputValue,
2825
instanceKey: model.instanceId,

src/components/custom-widget/customWidgetModelBinder.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,12 @@ import { Contract } from "@paperbits/common";
33
import { CustomWidgetModel } from "./customWidgetModel";
44
import { CustomWidgetContract } from "./customWidgetContract";
55
import { widgetName } from "./constants";
6+
import { customWidgetPrefixName, customWidgetRemovePrefixName } from "./ko/utils";
67

78
export class CustomWidgetModelBinder implements IModelBinder<CustomWidgetModel> {
8-
public canHandleModel(model: unknown, widgetName: string): boolean {
9-
return model instanceof CustomWidgetModel && model["name"] == widgetName;
10-
}
11-
129
public async contractToModel(contract: CustomWidgetContract): Promise<CustomWidgetModel> {
1310
const model = new CustomWidgetModel();
14-
model.name = contract.name ?? "";
11+
model.name = customWidgetPrefixName(contract.name) ?? "";
1512
model.displayName = contract.displayName || contract.widgetDisplayName;
1613
model.customInputValue = contract.customInputValue ?? "{}";
1714
model.instanceId = contract.instanceKey;
@@ -22,7 +19,7 @@ export class CustomWidgetModelBinder implements IModelBinder<CustomWidgetModel>
2219
public modelToContract(model: CustomWidgetModel): Contract {
2320
return {
2421
type: widgetName,
25-
name: model.name,
22+
name: customWidgetRemovePrefixName(model.name),
2623
displayName: model.displayName,
2724
customInputValue: model.customInputValue,
2825
instanceKey: model.instanceId,

src/components/custom-widget/ko/utils.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,17 @@ export const customWidgetPrefixName = (name: string): string => {
1515
return customWidgetNamePrefix + name;
1616
}
1717

18+
export const customWidgetRemovePrefixName = (name: string): string => {
19+
return name.startsWith(customWidgetNamePrefix) ? name.replace(customWidgetNamePrefix, "") : name; // replace first occurrence only
20+
}
21+
1822
export async function buildWidgetSource(
1923
blobStorage: MapiBlobStorage,
2024
model: CustomWidgetModel,
2125
environment: Environment,
2226
filePath: string,
2327
): Promise<{ override: string | null, src: string }> {
24-
const name = model.name.startsWith(customWidgetNamePrefix) ? model.name.replace(customWidgetNamePrefix, "") : model.name;
28+
const name = customWidgetRemovePrefixName(model.name);
2529

2630
// check is necessary during publishing as window.sessionStorage.getItem throws "DOMException {} node:internal/process/promises:279"
2731
const developmentSrc = environment !== "publishing"

0 commit comments

Comments
 (0)