Skip to content
This repository has been archived by the owner on Dec 16, 2024. It is now read-only.

Commit

Permalink
dotCMS/core#22029 Doing a selection moving up is broken
Browse files Browse the repository at this point in the history
  • Loading branch information
fmontes committed Apr 14, 2022
1 parent d102e95 commit d04013c
Show file tree
Hide file tree
Showing 4 changed files with 1,245 additions and 48,446 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
display: block;
min-height: 100%;
outline: none;
padding: $spacing-8;
padding: $spacing-3 $spacing-8;
font: 14px/1.3;

ul,
Expand Down
24 changes: 16 additions & 8 deletions libs/block-editor/src/lib/plugins/dot-bubble-menu.plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const DotBubbleMenuPlugin = (options: DotBubbleMenuPluginProps) => {
const component = options.component.instance;
const changeTo = options.changeToComponent.instance;

return new Plugin({
return new Plugin<DotBubbleMenuPluginProps>({
key:
typeof options.pluginKey === 'string'
? new PluginKey(options.pluginKey)
Expand Down Expand Up @@ -70,6 +70,8 @@ export class DotBubbleMenuPluginView extends BubbleMenuView {

public tippyChangeTo: Instance | undefined;

private shouldShowProp = false;

/* @Overrrider */
constructor(props: DotBubbleMenuViewProps) {
// Inherit the parent class
Expand All @@ -94,6 +96,16 @@ export class DotBubbleMenuPluginView extends BubbleMenuView {

// We need to also react to page scrolling.
document.body.addEventListener('scroll', this.hanlderScroll.bind(this), true);
document.body.addEventListener('mouseup', this.handleMouseUp.bind(this), true);
}

handleMouseUp() {
if (this.shouldShowProp) {
this.tippyChangeTo?.setProps({
getReferenceClientRect: () => this.tippy?.popper.getBoundingClientRect()
});
this.show();
}
}

/* @Overrrider */
Expand All @@ -114,7 +126,7 @@ export class DotBubbleMenuPluginView extends BubbleMenuView {
const from = Math.min(...ranges.map((range) => range.$from.pos));
const to = Math.max(...ranges.map((range) => range.$to.pos));

const shouldShow = this.shouldShow?.({
this.shouldShowProp = this.shouldShow?.({
editor: this.editor,
view,
state,
Expand All @@ -123,7 +135,7 @@ export class DotBubbleMenuPluginView extends BubbleMenuView {
to
});

if (!shouldShow) {
if (!this.shouldShowProp) {
this.hide();
this.tippyChangeTo?.hide();
return;
Expand All @@ -144,13 +156,8 @@ export class DotBubbleMenuPluginView extends BubbleMenuView {
}
});

this.tippyChangeTo?.setProps({
getReferenceClientRect: () => this.tippy?.popper.getBoundingClientRect()
});

this.updateComponent();
this.setMenuItems(doc, from);
this.show();
}

/* @Overrrider */
Expand All @@ -170,6 +177,7 @@ export class DotBubbleMenuPluginView extends BubbleMenuView {
this.changeTo.destroy();

document.body.removeEventListener('scroll', this.hanlderScroll.bind(this), true);
document.body.removeEventListener('mouseup', this.handleMouseUp.bind(this), true);
}

/* Update Component */
Expand Down
5 changes: 5 additions & 0 deletions libs/block-editor/src/lib/utils/bubble-menu.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,17 @@ export const shouldShowBubbleMenu = ({ editor, state, from, to }: ShouldShowProp
// Current selected node
const node = editor.state.doc.nodeAt(editor.state.selection.from);

if (!node) {
return false;
}

// Sometime check for `empty` is not enough.
// Doubleclick an empty paragraph returns a node size of 2.
// So we check also for an empty text size.
const isEmptyTextBlock = !doc.textBetween(from, to).length && isTextSelection(state.selection);

// If it's empty or the current node is type dotContent, it will not open.
// if (!editor.isActive('dotContent')) { // this might be the same as the condition below
if (!editor.isFocused || empty || isEmptyTextBlock || node.type.name == 'dotContent') {
return false;
}
Expand Down
Loading

0 comments on commit d04013c

Please sign in to comment.