diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 96791b9f4..7be0da766 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -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 diff --git a/src/components/block/index.ts b/src/components/block/index.ts index 01883731c..9938d8b53 100644 --- a/src/components/block/index.ts +++ b/src/components/block/index.ts @@ -24,7 +24,7 @@ import EventsDispatcher from '../utils/events'; 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'; @@ -473,7 +473,13 @@ export default class Block extends EventsDispatcher { * @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 }); + } } /** diff --git a/src/components/events/BlockStretchToggled.ts b/src/components/events/BlockStretchToggled.ts new file mode 100644 index 000000000..1374f821c --- /dev/null +++ b/src/components/events/BlockStretchToggled.ts @@ -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; +} diff --git a/src/components/events/index.ts b/src/components/events/index.ts index 10f82cdc1..b5281ec3d 100644 --- a/src/components/events/index.ts +++ b/src/components/events/index.ts @@ -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'; @@ -16,6 +18,7 @@ import { EditorMobileLayoutToggled } from './EditorMobileLayoutToggled'; export { RedactorDomChanged, BlockChanged, + BlockStretchToggled, FakeCursorAboutToBeToggled, FakeCursorHaveBeenSet, EditorMobileLayoutToggled @@ -26,6 +29,7 @@ export { */ export interface EditorEventMap { [BlockHovered]: BlockHoveredPayload; + [BlockStretchToggled]: BlockStretchToggledPayload; [RedactorDomChanged]: RedactorDomChangedPayload; [BlockChanged]: BlockChangedPayload; [FakeCursorAboutToBeToggled]: FakeCursorAboutToBeToggledPayload; diff --git a/src/components/modules/toolbar/index.ts b/src/components/modules/toolbar/index.ts index 3098c6aba..d187c31f0 100644 --- a/src/components/modules/toolbar/index.ts +++ b/src/components/modules/toolbar/index.ts @@ -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'; @@ -130,6 +131,7 @@ export default class Toolbar extends Module { actionsOpened: 'ce-toolbar__actions--opened', toolbarOpened: 'ce-toolbar--opened', + toolbarStretched: 'ce-toolbar--stretched', openedToolboxHolderModifier: 'codex-editor--toolbox-opened', plusButton: 'ce-toolbar__plus', @@ -354,6 +356,11 @@ export default class Toolbar extends Module { // 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); + /** * Do not show Block Tunes Toggler near single and empty block */ @@ -388,6 +395,7 @@ export default class Toolbar extends Module { */ private reset(): void { this.nodes.wrapper.style.top = 'unset'; + this.nodes.wrapper.classList.remove(this.CSS.toolbarStretched); } /** @@ -597,6 +605,26 @@ export default class Toolbar extends Module { 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) { + return; + } + + this.moveAndOpen(block); + }); } } diff --git a/src/styles/block.css b/src/styles/block.css index 6921cb52c..6a595ae96 100644 --- a/src/styles/block.css +++ b/src/styles/block.css @@ -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); } } @@ -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( diff --git a/src/styles/rtl.css b/src/styles/rtl.css index 060693313..c74c9bb14 100644 --- a/src/styles/rtl.css +++ b/src/styles/rtl.css @@ -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%; diff --git a/src/styles/toolbar.css b/src/styles/toolbar.css index 1d337893d..404663afc 100644 --- a/src/styles/toolbar.css +++ b/src/styles/toolbar.css @@ -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( @@ -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; diff --git a/src/styles/variables.css b/src/styles/variables.css index ff2bdc378..833b2e868 100644 --- a/src/styles/variables.css +++ b/src/styles/variables.css @@ -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 */ diff --git a/test/cypress/tests/api/toolbar.cy.ts b/test/cypress/tests/api/toolbar.cy.ts index 41da27b14..a9ba4d73d 100644 --- a/test/cypress/tests/api/toolbar.cy.ts +++ b/test/cypress/tests/api/toolbar.cy.ts @@ -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('@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('@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('@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('@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('@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(); + }); + }); });