Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,24 @@ var editor = new EditorJS({

```

## Align InlineToolbar

If you want to align the inline toolbar, you can set the option `alignInlineToolbar` to `'left'`, `'center'`, or `'right'`.
The default behavior is `'left'`.

**Possible values:**
- `'left'`: aligns the toolbar to the left edge of the selection (default)
- `'center'`: centers the toolbar
- `'right'`: aligns the toolbar to the right edge of the selection

```js
var editor = new EditorJS({
//...
alignInlineToolbar: 'right',
//...
});
```

## Holder
The `holder` property supports an id or a reference to dom element.

Expand Down
5 changes: 5 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,11 @@
placeholder: 'Write something or press / to select a tool',
autofocus: true,

/**
* Inline toolbar alignment
*/
alignInlineToolbar:"right",

/**
* Initial Editor data
*/
Expand Down
37 changes: 34 additions & 3 deletions src/components/modules/toolbar/inline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ export default class InlineToolbar extends Module<InlineToolbarNodes> {
*/
private tools: Map<InlineToolAdapter, IInlineTool> = new Map();

/**
* Inline toolbar alignment
*/
private align: 'left' | 'center' | 'right' = 'left';

/**
* @param moduleConfiguration - Module Configuration
* @param moduleConfiguration.config - Editor's config
Expand All @@ -68,6 +73,9 @@ export default class InlineToolbar extends Module<InlineToolbarNodes> {
eventsDispatcher,
});

// Get the value from the config
this.align = config.alignInlineToolbar ?? 'left';

window.requestIdleCallback(() => {
this.make();
}, { timeout: 2000 });
Expand Down Expand Up @@ -218,11 +226,27 @@ export default class InlineToolbar extends Module<InlineToolbarNodes> {
private move(popoverWidth: number): void {
const selectionRect = SelectionUtils.rect as DOMRect;
const wrapperOffset = this.Editor.UI.nodes.wrapper.getBoundingClientRect();


let newX: number;

switch (this.align) {
default:
case 'left':
newX = selectionRect.x - wrapperOffset.x;
break;
case 'right':
newX = selectionRect.x + selectionRect.width - popoverWidth - wrapperOffset.x;
break;
case 'center':
newX = selectionRect.x + selectionRect.width / 2 - popoverWidth / 2 - wrapperOffset.x;
break;
}

const newCoords = {
x: selectionRect.x - wrapperOffset.x,
x: newX,
y: selectionRect.y +
selectionRect.height -
// + window.scrollY
wrapperOffset.top +
this.toolbarVerticalMargin,
};
Expand All @@ -233,7 +257,14 @@ export default class InlineToolbar extends Module<InlineToolbarNodes> {
* Prevent InlineToolbar from overflowing the content zone on the right side
*/
if (realRightCoord > this.Editor.UI.contentRect.right) {
newCoords.x = this.Editor.UI.contentRect.right -popoverWidth - wrapperOffset.x;
newCoords.x = this.Editor.UI.contentRect.right - popoverWidth - wrapperOffset.x;
}

/**
* Prevent InlineToolbar from overflowing the content zone on the left side
*/
if (newCoords.x < 0) {
newCoords.x = 0;
}

this.nodes.wrapper!.style.left = Math.floor(newCoords.x) + 'px';
Expand Down
86 changes: 86 additions & 0 deletions test/cypress/tests/ui/InlineToolbar.cy.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Header from '@editorjs/header';
import type { InlineTool, MenuConfig } from '../../../../types/tools';
import { createEditorWithTextBlocks } from '../../support/utils/createEditorWithTextBlocks';
import Paragraph from '@editorjs/paragraph';

describe('Inline Toolbar', () => {
describe('Separators', () => {
Expand Down Expand Up @@ -227,5 +228,90 @@ describe('Inline Toolbar', () => {
cy.get('@toolSurround').should('have.been.called');
});
});

describe('Default align to left', () => {
it('Should align the InlineToolbar to the left as default', () => {
cy.createEditor({
tools: {
block: Paragraph,
},
data: {
blocks: [
{
type: 'paragraph',
data: {
text: 'Test inline toolbar alignment',
},
},
],
},
});

cy.get('[data-cy=editorjs]')
.find('.ce-paragraph')
.first()
.selectText('inline toolbar');

cy.get('[data-cy="inline-toolbar"] .ce-popover__container').should('be.visible');

cy.window().then((win) => {
cy.get('[data-cy="inline-toolbar"] .ce-popover__container').then(($toolbar) => {
const toolbarRect = $toolbar[0].getBoundingClientRect();
const selection = win.getSelection();

if (!selection || selection.rangeCount === 0) {
throw new Error('No selection found');
}
const rangeRect = selection.getRangeAt(0).getBoundingClientRect();

// Assert toolbar left is approximately equal to selection left
expect(Math.abs(toolbarRect.left - rangeRect.left)).to.be.lessThan(5);
});
});
});
});

describe('Align to rigth', () => {
it('Should align the InlineToolbar to the right', () => {
cy.createEditor({
alignInlineToolbar: 'right',
tools: {
block: Paragraph,
},
data: {
blocks: [
{
type: 'paragraph',
data: {
text: 'Test inline toolbar alignment',
},
},
],
},
});

cy.get('[data-cy=editorjs]')
.find('.ce-paragraph')
.first()
.selectText('inline toolbar');

cy.get('[data-cy="inline-toolbar"] .ce-popover__container').should('be.visible');

cy.window().then((win) => {
cy.get('[data-cy="inline-toolbar"] .ce-popover__container').then(($toolbar) => {
const toolbarRect = $toolbar[0].getBoundingClientRect();
const selection = win.getSelection();

if (!selection || selection.rangeCount === 0) {
throw new Error('No selection found');
}
const rangeRect = selection.getRangeAt(0).getBoundingClientRect();

// Assert toolbar right is approximately equal to selection right
expect(Math.abs(toolbarRect.right - rangeRect.right)).to.be.lessThan(5);
});
});
});
});
});

6 changes: 6 additions & 0 deletions types/configs/editor-config.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@ export interface EditorConfig {
*/
inlineToolbar?: string[]|boolean;

/**
* Inline Toolbar alignment config
*/
alignInlineToolbar?: 'left' | 'center' | 'right';


/**
* Common Block Tunes list. Will be added to all the blocks which do not specify their own 'tunes' set
*/
Expand Down