Skip to content

Commit be5fa8e

Browse files
committed
MC-14994: Heading (& Text, Banner, Slide) toolbar menu is positioned incorrectly on first drop on stage
- fix toolbar race condition
1 parent e8ed034 commit be5fa8e

File tree

5 files changed

+51
-36
lines changed

5 files changed

+51
-36
lines changed

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

Lines changed: 13 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/heading/preview.js

Lines changed: 11 additions & 18 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/template/content-type-toolbar.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66
-->
77

8-
<div class="pagebuilder-toolbar-options">
8+
<div class="pagebuilder-toolbar-options" afterRender="function (element) { afterRender.call($data, element); }">
99
<div class="pagebuilder-toolbar-options-wrapper" each="options">
1010
<ul class="pagebuilder-toolbar-options-links" css="key" each="values">
1111
<li class="pagebuilder-toolbar-options-link">

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,17 @@ import Preview from "./content-type/preview";
1111
import {PreviewCollectionInterface} from "./content-type/preview-collection.types";
1212
import {PreviewInterface} from "./content-type/preview.types";
1313
import checkStageFullScreen from "./utils/check-stage-full-screen";
14+
import deferred, {DeferredInterface} from "./utils/promise-deferred";
1415

1516
/**
1617
* @api
1718
*/
1819
export default class Toolbar {
1920
public options: KnockoutObservableArray<OptionInterface> = ko.observableArray([]);
2021
public observer: MutationObserver;
22+
public afterRenderDeferred: DeferredInterface = deferred();
2123
private preview: Preview;
24+
private element: Element;
2225

2326
/**
2427
* Toolbar Options constructor
@@ -40,6 +43,16 @@ export default class Toolbar {
4043
return "Magento_PageBuilder/content-type-toolbar";
4144
}
4245

46+
/**
47+
* On render init the toolbar
48+
*
49+
* @param {Element} element
50+
*/
51+
public afterRender(element: Element): void {
52+
this.element = element;
53+
this.afterRenderDeferred.resolve(element);
54+
}
55+
4356
/**
4457
* Upon clicking the option update the value as directed
4558
* When user toggles the option off, set the value back to default

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

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@
44
*/
55

66
import $ from "jquery";
7-
import events from "Magento_PageBuilder/js/events";
87
import _ from "underscore";
98
import ContentTypeConfigInterface from "../../content-type-config.types";
109
import HideShowOption from "../../content-type-menu/hide-show-option";
1110
import {OptionsInterface} from "../../content-type-menu/option.types";
1211
import Toolbar, {ContentTypeToolbarPreviewInterface} from "../../content-type-toolbar";
1312
import {OptionInterface} from "../../content-type-toolbar.types";
1413
import ContentTypeInterface from "../../content-type.types";
15-
import {ContentTypeDroppedCreateEventParamsInterface} from "../content-type-events.types";
14+
import deferred, {DeferredInterface} from "../../utils/promise-deferred";
1615
import ObservableUpdater from "../observable-updater";
1716
import BasePreview from "../preview";
1817

@@ -22,6 +21,7 @@ import BasePreview from "../preview";
2221
export default class Preview extends BasePreview implements ContentTypeToolbarPreviewInterface {
2322
public toolbar: Toolbar;
2423
private element: Element;
24+
private afterRenderDeferred: DeferredInterface = deferred();
2525

2626
/**
2727
* @param {ContentTypeInterface} contentType
@@ -38,6 +38,15 @@ export default class Preview extends BasePreview implements ContentTypeToolbarPr
3838
this,
3939
this.getToolbarOptions(),
4040
);
41+
42+
Promise.all([
43+
this.afterRenderDeferred.promise,
44+
this.toolbar.afterRenderDeferred.promise,
45+
]).then(([element]) => {
46+
_.defer(() => {
47+
$(element).focus();
48+
});
49+
});
4150
}
4251

4352
/**
@@ -61,25 +70,13 @@ export default class Preview extends BasePreview implements ContentTypeToolbarPr
6170
}
6271

6372
/**
64-
* On render init the tabs widget
73+
* On render init the heading
6574
*
6675
* @param {Element} element
6776
*/
6877
public afterRender(element: Element): void {
6978
this.element = element;
70-
}
71-
72-
public bindEvents() {
73-
super.bindEvents();
74-
75-
// When a heading is dropped for the first time show heading toolbar
76-
events.on("heading:dropAfter", (args: ContentTypeDroppedCreateEventParamsInterface) => {
77-
if (args.id === this.contentType.id) {
78-
_.delay(() => {
79-
$(this.element).focus();
80-
}, 100); // 100 ms delay to allow for heading to render
81-
}
82-
});
79+
this.afterRenderDeferred.resolve(element);
8380
}
8481

8582
/**

0 commit comments

Comments
 (0)