Skip to content
1 change: 1 addition & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- `Fix` - Invoke tool `reset()` methods on `Tools.destroy()`, catching and logging any errors as warnings
- `Fix` - `blocks.insertMany()` now triggers `onChange` with a `block-added` event for each inserted block
- `Fix` - Inline Tools shortcuts work in every Editor instance on a page and are released when an Editor is destroyed
- `Fix` - Toolbar is now aligned with the content of stretched Blocks
- `Fix` - Focus the contenteditable input when setting the caret, so autofocus works inside iframes

### 2.31.7
Expand Down
8 changes: 7 additions & 1 deletion src/components/block/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import type { TunesMenuConfigItem } from '../../../types/tools';
import { isMutationBelongsToElement } from '../utils/mutations';
import type { EditorEventMap } from '../events';
import { FakeCursorAboutToBeToggled, FakeCursorHaveBeenSet, RedactorDomChanged } from '../events';
import { BlockStretchToggled, FakeCursorAboutToBeToggled, FakeCursorHaveBeenSet, RedactorDomChanged } from '../events';
import type { RedactorDomChangedPayload } from '../events/RedactorDomChanged';
import { convertBlockDataToString, isSameBlockData } from '../utils/blocks';
import { PopoverItemType } from '@/types/utils/popover/popover-item-type';
Expand Down Expand Up @@ -67,7 +67,7 @@
/**
* @class Block
* @classdesc This class describes editor`s block, including block`s HTMLElement, data and tool
* @property {BlockTool} tool — current block tool (Paragraph, for example)

Check warning on line 70 in src/components/block/index.ts

View workflow job for this annotation

GitHub Actions / ESlint

The type 'BlockTool' is undefined
* @property {object} CSS — block`s css classes
*/

Expand Down Expand Up @@ -98,7 +98,7 @@

/**
* @classdesc Abstract Block class that contains Block information, Tool name and Tool class instance
* @property {BlockTool} tool - Tool instance

Check warning on line 101 in src/components/block/index.ts

View workflow job for this annotation

GitHub Actions / ESlint

The type 'BlockTool' is undefined
* @property {HTMLElement} holder - Div element that wraps block content with Tool's content. Has `ce-block` CSS class
* @property {HTMLElement} pluginsContent - HTML content that returns by Tool's render function
*/
Expand Down Expand Up @@ -226,7 +226,7 @@
this.name = tool.name;
this.id = id;
this.settings = tool.settings;
this.config = tool.settings.config || {};

Check warning on line 229 in src/components/block/index.ts

View workflow job for this annotation

GitHub Actions / ESlint

Unexpected any value in conditional. An explicit comparison or type cast is required
this.editorEventBus = eventBus || null;
this.blockAPI = new BlockAPI(this);

Expand Down Expand Up @@ -473,7 +473,13 @@
* @param {boolean} state - 'true' to enable, 'false' to disable stretched state
*/
public set stretched(state: boolean) {
const wasStretched = this.stretched;

this.holder.classList.toggle(Block.CSS.wrapperStretched, state);

if (wasStretched !== state) {
this.editorEventBus?.emit(BlockStretchToggled, { block: this });
}
}

/**
Expand Down Expand Up @@ -942,7 +948,7 @@
node = node.parentElement;
}

return node && (node as HTMLElement).closest('[data-mutation-free="true"]') !== null;

Check warning on line 951 in src/components/block/index.ts

View workflow job for this annotation

GitHub Actions / ESlint

Unexpected object value in conditional. The condition is always true
});
});

Expand Down
16 changes: 16 additions & 0 deletions src/components/events/BlockStretchToggled.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import type Block from '../block';

/**
* Fired when the stretched state of a Block is toggled
*/
export const BlockStretchToggled = 'block stretch toggled';

/**
* Payload that will be passed with the event
*/
export interface BlockStretchToggledPayload {
/**
* Block whose stretched state was toggled
*/
block: Block;
}
4 changes: 4 additions & 0 deletions src/components/events/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { RedactorDomChanged } from './RedactorDomChanged';
import type { BlockChangedPayload } from './BlockChanged';
import { BlockChanged } from './BlockChanged';
import type { BlockHovered, BlockHoveredPayload } from './BlockHovered';
import type { BlockStretchToggledPayload } from './BlockStretchToggled';
import { BlockStretchToggled } from './BlockStretchToggled';
import type { FakeCursorAboutToBeToggledPayload } from './FakeCursorAboutToBeToggled';
import { FakeCursorAboutToBeToggled } from './FakeCursorAboutToBeToggled';
import type { FakeCursorHaveBeenSetPayload } from './FakeCursorHaveBeenSet';
Expand All @@ -16,6 +18,7 @@ import { EditorMobileLayoutToggled } from './EditorMobileLayoutToggled';
export {
RedactorDomChanged,
BlockChanged,
BlockStretchToggled,
FakeCursorAboutToBeToggled,
FakeCursorHaveBeenSet,
EditorMobileLayoutToggled
Expand All @@ -26,6 +29,7 @@ export {
*/
export interface EditorEventMap {
[BlockHovered]: BlockHoveredPayload;
[BlockStretchToggled]: BlockStretchToggledPayload;
[RedactorDomChanged]: RedactorDomChangedPayload;
[BlockChanged]: BlockChangedPayload;
[FakeCursorAboutToBeToggled]: FakeCursorAboutToBeToggledPayload;
Expand Down
28 changes: 28 additions & 0 deletions src/components/modules/toolbar/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type Block from '../../block';
import Toolbox, { ToolboxEvent } from '../../ui/toolbox';
import { IconMenu, IconPlus } from '@codexteam/icons';
import { BlockHovered } from '../../events/BlockHovered';
import { BlockStretchToggled } from '../../events/BlockStretchToggled';
import { beautifyShortcut } from '../../utils';
import { getKeyboardKeyForCode } from '../../utils/keyboard';

Expand Down Expand Up @@ -130,6 +131,7 @@ export default class Toolbar extends Module<ToolbarNodes> {
actionsOpened: 'ce-toolbar__actions--opened',

toolbarOpened: 'ce-toolbar--opened',
toolbarStretched: 'ce-toolbar--stretched',
openedToolboxHolderModifier: 'codex-editor--toolbox-opened',

plusButton: 'ce-toolbar__plus',
Expand Down Expand Up @@ -354,6 +356,11 @@ export default class Toolbar extends Module<ToolbarNodes> {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
this.nodes.wrapper!.style.top = `${Math.floor(toolbarY)}px`;

/**
* Align Toolbar with the content width of the hovered Block
*/
this.nodes.wrapper.classList.toggle(this.CSS.toolbarStretched, block.stretched);
Comment thread
Anurag-S1ngh marked this conversation as resolved.
Resolved

/**
* Do not show Block Tunes Toggler near single and empty block
*/
Expand Down Expand Up @@ -388,6 +395,7 @@ export default class Toolbar extends Module<ToolbarNodes> {
*/
private reset(): void {
this.nodes.wrapper.style.top = 'unset';
this.nodes.wrapper.classList.remove(this.CSS.toolbarStretched);
}

/**
Expand Down Expand Up @@ -597,6 +605,26 @@ export default class Toolbar extends Module<ToolbarNodes> {

this.moveAndOpen(data.block);
});

/**
* Re-align Toolbar when the hovered Block changes its stretched state
*/
this.eventsDispatcher.on(BlockStretchToggled, ({ block }) => {
if (!this.opened || block !== this.hoveredBlock) {
return;
}

this.nodes.wrapper.classList.toggle(this.CSS.toolbarStretched, block.stretched);

/**
* Do not move toolbar if Block Settings or Toolbox opened
*/
if (this.Editor.BlockSettings.opened || this.toolboxInstance?.opened) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checked locally: stretching an image via the Image tool's Stretch tune still leaves the toolbar at the narrow column. The tune keeps Block Settings open after the click, so BlockStretchToggled arrives while settings are open and this early return skips it. Nothing re-applies the class after settings close either. Could you handle this case, e.g. just toggle ce-toolbar--stretched on the wrapper when settings are open (the popover is inside the toolbar and will move with it), or re-sync on Block Settings close? A test for it would be great too.

return;
}

this.moveAndOpen(block);
});
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/styles/block.css
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
max-width: none;

@media (--not-mobile) {
width: max(0px, calc(100% - var(--toolbar-actions-space)));
width: var(--stretched-content-width);
margin-left: var(--toolbar-actions-space);
}
}
Expand All @@ -51,7 +51,7 @@
@media (--not-mobile) {
width: min(
var(--content-width),
max(0px, calc(100% - var(--toolbar-actions-space)))
var(--stretched-content-width)
);
max-width: none;
margin-left: max(
Expand Down
5 changes: 5 additions & 0 deletions src/styles/rtl.css
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@
margin-right: var(--toolbar-actions-space);
}

.ce-toolbar--stretched .ce-toolbar__content {
margin-left: 0;
margin-right: var(--toolbar-actions-space);
}

.ce-toolbar__actions {
right: auto;
left: 100%;
Expand Down
11 changes: 10 additions & 1 deletion src/styles/toolbar.css
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
@media (--not-mobile) {
width: min(
var(--content-width),
max(0px, calc(100% - var(--toolbar-actions-space)))
var(--stretched-content-width)
);
max-width: none;
margin-left: max(
Expand All @@ -31,6 +31,15 @@
}
}

&--stretched {
.ce-toolbar__content {
@media (--not-mobile) {
width: var(--stretched-content-width);
margin-left: var(--toolbar-actions-space);
}
}
}

&__plus {
@apply --toolbox-button;
flex-shrink: 0;
Expand Down
5 changes: 5 additions & 0 deletions src/styles/variables.css
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@
*/
--toolbar-actions-space: 58px;

/**
* Width of Block and Toolbar content in the stretched mode
*/
--stretched-content-width: max(0px, calc(100% - var(--toolbar-actions-space)));

/**
* Toolbar Plus Button and Toolbox buttons height and width
*/
Expand Down
110 changes: 110 additions & 0 deletions test/cypress/tests/api/toolbar.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,114 @@ describe('api.toolbar', () => {
});
});
});

describe('stretched Blocks', () => {
/**
* Toolbar content should match the content width of the hovered Block
*/
const assertToolbarAlignedWithBlock = (): void => {
cy.get('[data-cy=editorjs]').then(($holder) => {
const blockContentRect = $holder.find('.ce-block__content')[0].getBoundingClientRect();
const toolbarContentRect = $holder.find('.ce-toolbar__content')[0].getBoundingClientRect();

expect(toolbarContentRect.left).to.be.closeTo(blockContentRect.left, 1);
expect(toolbarContentRect.width).to.be.closeTo(blockContentRect.width, 1);
});
};

beforeEach(function () {
cy.viewport(1000, 800);

/**
* Toolbox is created in requestIdleCallback, and Toolbar is not moved until it is ready
*/
cy.get('[data-cy=editorjs]')
.find('.ce-toolbox')
.should('exist');
});

it('should align the toolbar with the content of a stretched Block', function () {
cy.get<EditorJS>('@editorInstance').then((editor) => {
editor.blocks.getBlockByIndex(0).stretched = true;
});

cy.get('[data-cy=editorjs]')
.find('.ce-block')
.first()
.trigger('mousemove');

cy.get('[data-cy=editorjs]')
.find('.ce-toolbar')
.should('have.class', 'ce-toolbar--stretched');
assertToolbarAlignedWithBlock();
});

it('should re-align the toolbar when the stretched state of the hovered Block is toggled', function () {
cy.get('[data-cy=editorjs]')
.find('.ce-block')
.first()
.trigger('mousemove');

cy.get('[data-cy=editorjs]')
.find('.ce-toolbar')
.should('have.class', 'ce-toolbar--opened');
assertToolbarAlignedWithBlock();

cy.get<EditorJS>('@editorInstance').then((editor) => {
editor.blocks.getBlockByIndex(0).stretched = true;
});

cy.get('[data-cy=editorjs]')
.find('.ce-toolbar')
.should('have.class', 'ce-toolbar--stretched');
assertToolbarAlignedWithBlock();

cy.get<EditorJS>('@editorInstance').then((editor) => {
editor.blocks.getBlockByIndex(0).stretched = false;
});

cy.get('[data-cy=editorjs]')
.find('.ce-toolbar')
.should('not.have.class', 'ce-toolbar--stretched');
assertToolbarAlignedWithBlock();
});

it('should re-align the toolbar when the hovered Block is stretched while Block Settings are open', function () {
cy.get('[data-cy=editorjs]')
.find('.ce-block')
.first()
.trigger('mousemove');

cy.get('[data-cy=editorjs]')
.find('.ce-toolbar')
.should('have.class', 'ce-toolbar--opened');

cy.get('[data-cy=editorjs]')
.find('.ce-toolbar__settings-btn')
.click();

cy.get('[data-cy="block-tunes"] .ce-popover__container').should('be.visible');
assertToolbarAlignedWithBlock();

cy.get<EditorJS>('@editorInstance').then((editor) => {
editor.blocks.getBlockByIndex(0).stretched = true;
});

cy.get('[data-cy=editorjs]')
.find('.ce-toolbar')
.should('have.class', 'ce-toolbar--stretched');
cy.get('[data-cy="block-tunes"] .ce-popover__container').should('be.visible');
assertToolbarAlignedWithBlock();

cy.get<EditorJS>('@editorInstance').then((editor) => {
editor.blocks.getBlockByIndex(0).stretched = false;
});

cy.get('[data-cy=editorjs]')
.find('.ce-toolbar')
.should('not.have.class', 'ce-toolbar--stretched');
cy.get('[data-cy="block-tunes"] .ce-popover__container').should('be.visible');
assertToolbarAlignedWithBlock();
});
});
});
Loading