Skip to content

Commit 83d93bd

Browse files
committed
#419: Nested Links If User Adds Link Attribute & Link Widget to Banner/Slide - Banner/Slide dialog for content Widget validation.
1 parent eea19fd commit 83d93bd

File tree

8 files changed

+114
-4
lines changed

8 files changed

+114
-4
lines changed

app/code/Magento/PageBuilder/view/adminhtml/web/js/content-type/banner/preview.js

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/code/Magento/PageBuilder/view/adminhtml/web/js/content-type/slide/preview.js

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/code/Magento/PageBuilder/view/adminhtml/web/js/form/element/validator-rules-mixin.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ define([
5858
* @return {Boolean}
5959
*/
6060
function validateWysiwygHasWidget(str) {
61-
return (/({{widget)+([^}}]+)}}/igm).test(str);
61+
return (/\{\{widget(.*?)\}\}/ig).test(str);
6262
}
6363

6464
/**

app/code/Magento/PageBuilder/view/adminhtml/web/js/utils/editor.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/code/Magento/PageBuilder/view/adminhtml/web/js/utils/nesting-widget-dialog.js

Lines changed: 47 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/code/Magento/PageBuilder/view/adminhtml/web/ts/js/content-type/banner/preview.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
unlockImageSize,
2727
} from "../../utils/editor";
2828
import nestingLinkDialog from "../../utils/nesting-link-dialog";
29+
import nestingWidgetDialog from "../../utils/nesting-widget-dialog";
2930
import WysiwygFactory from "../../wysiwyg/factory";
3031
import WysiwygInterface from "../../wysiwyg/wysiwyg-interface";
3132
import {ContentTypeReadyEventParamsInterface} from "../content-type-events.types";
@@ -497,6 +498,7 @@ export default class Preview extends BasePreview {
497498
}
498499
events.trigger(`image:${this.contentType.id}:assignAfter`, imageObject);
499500
nestingLinkDialog(this.contentType.dataStore, this.wysiwyg, "message", "link_url");
501+
nestingWidgetDialog(this.contentType.dataStore, this.wysiwyg, "message", "link_url");
500502
});
501503
this.contentType.dataStore.subscribe(function(data: DataObject) {
502504
if (this.shouldUpdateVideo(data)) {

app/code/Magento/PageBuilder/view/adminhtml/web/ts/js/content-type/slide/preview.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import {
2727
unlockImageSize,
2828
} from "../../utils/editor";
2929
import nestingLinkDialog from "../../utils/nesting-link-dialog";
30+
import nestingWidgetDialog from "../../utils/nesting-widget-dialog";
3031
import WysiwygFactory from "../../wysiwyg/factory";
3132
import WysiwygInterface from "../../wysiwyg/wysiwyg-interface";
3233
import {ContentTypeMountEventParamsInterface, ContentTypeReadyEventParamsInterface} from "../content-type-events.types";
@@ -488,6 +489,7 @@ export default class Preview extends BasePreview {
488489
const imageObject = (dataStore[this.config.additional_data.uploaderConfig.dataScope] as object[])[0] || {};
489490
events.trigger(`image:${this.contentType.id}:assignAfter`, imageObject);
490491
nestingLinkDialog(this.contentType.dataStore, this.wysiwyg, "content", "link_url");
492+
nestingWidgetDialog(this.contentType.dataStore, this.wysiwyg, "content", "link_url");
491493
});
492494

493495
// Remove wysiwyg before assign new instance.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
import $ from "jquery";
7+
import $t from "mage/translate";
8+
import confirmationDialog from "Magento_PageBuilder/js/modal/dismissible-confirm";
9+
import {FieldDefaultsInterface} from "../content-type-factory";
10+
import DataStore, {DataObject} from "../data-store";
11+
import WysiwygInterface from "../wysiwyg/wysiwyg-interface";
12+
13+
/**
14+
* Validate inline editor for having nested widget
15+
* Creates a dialog and removes inline editor widget if present
16+
*
17+
* @param {DataStore} dataStore
18+
* @param {WysiwygInterface} wysiwyg
19+
* @param {string} inlineMessageField
20+
* @param {string} linkUrlField
21+
*/
22+
export default function nestingLinkDialog(
23+
dataStore: DataStore,
24+
wysiwyg: WysiwygInterface,
25+
inlineMessageField: string,
26+
linkUrlField: string,
27+
) {
28+
const dataStoreContent = dataStore.getState() as DataObject;
29+
const inlineMessage = dataStoreContent[inlineMessageField] as string;
30+
const linkUrl = dataStoreContent[linkUrlField] as FieldDefaultsInterface;
31+
const widgetRegex = /\{\{widget(.*?)\}\}/ig;
32+
const widgetPlaceholderRegex = /<span.*(class=)(\"|\').*((magento-placeholder).*(magento-widget)|(magento-widget).*(magento-placeholder)).*<\/span>/igm;
33+
34+
if (wysiwyg &&
35+
inlineMessage.match(widgetRegex) &&
36+
linkUrl &&
37+
["page", "product", "category", "default"].indexOf(linkUrl.type) !== -1 &&
38+
linkUrl[linkUrl.type] &&
39+
linkUrl[linkUrl.type].length !== 0
40+
) {
41+
const inlineEditor = $("#" + wysiwyg.elementId);
42+
inlineEditor.blur();
43+
confirmationDialog({
44+
actions: {
45+
always: () => {
46+
const widgetLessDataStoreMessage = inlineMessage.replace(widgetRegex, "");
47+
const widgetLessInlineMessage = inlineEditor.html().replace(widgetPlaceholderRegex, "");
48+
dataStore.set(inlineMessageField, widgetLessDataStoreMessage);
49+
inlineEditor.html(widgetLessInlineMessage);
50+
},
51+
},
52+
content: $t("We are unable to support widget within the content field whilst having a link set on the content type. Please remove the content type link if you'd like to set a widget within the content. We will automatically remove the widget within the content field."), // tslint:disable-line:max-line-length
53+
title: $t("Nested widgets are not allowed"),
54+
haveCancelButton: false,
55+
});
56+
}
57+
}

0 commit comments

Comments
 (0)