Skip to content

Commit 8b1235b

Browse files
committed
add:inline-toolbar-alignment-option
1 parent 7da61e9 commit 8b1235b

File tree

4 files changed

+63
-3
lines changed

4 files changed

+63
-3
lines changed

docs/usage.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,24 @@ var editor = new EditorJS({
7171

7272
```
7373

74+
## Align InlineToolbar
75+
76+
If you want to align the inline toolbar, you can set the option `alignInlineToolbar` to `'left'`, `'center'`, or `'right'`.
77+
The default behavior is `'left'`.
78+
79+
**Possible values:**
80+
- `'left'`: aligns the toolbar to the left edge of the selection
81+
- `'center'`: centers the toolbar (default)
82+
- `'right'`: aligns the toolbar to the right edge of the selection
83+
84+
```js
85+
var editor = new EditorJS({
86+
//...
87+
alignInlineToolbar: 'left',
88+
//...
89+
});
90+
```
91+
7492
## Holder
7593
The `holder` property supports an id or a reference to dom element.
7694

index.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,11 @@
206206
placeholder: 'Write something or press / to select a tool',
207207
autofocus: true,
208208

209+
/**
210+
* Inline toolbar alignment
211+
*/
212+
alignInlineToolbar:"right",
213+
209214
/**
210215
* Initial Editor data
211216
*/

src/components/modules/toolbar/inline.ts

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ export default class InlineToolbar extends Module<InlineToolbarNodes> {
5757
*/
5858
private tools: Map<InlineToolAdapter, IInlineTool> = new Map();
5959

60+
/**
61+
* Inline toolbar alignment
62+
*/
63+
private align: 'left' | 'center' | 'right' = 'left';
64+
6065
/**
6166
* @param moduleConfiguration - Module Configuration
6267
* @param moduleConfiguration.config - Editor's config
@@ -68,6 +73,9 @@ export default class InlineToolbar extends Module<InlineToolbarNodes> {
6873
eventsDispatcher,
6974
});
7075

76+
// Get the value from the config
77+
this.align = config.alignInlineToolbar ?? 'left';
78+
7179
window.requestIdleCallback(() => {
7280
this.make();
7381
}, { timeout: 2000 });
@@ -218,11 +226,27 @@ export default class InlineToolbar extends Module<InlineToolbarNodes> {
218226
private move(popoverWidth: number): void {
219227
const selectionRect = SelectionUtils.rect as DOMRect;
220228
const wrapperOffset = this.Editor.UI.nodes.wrapper.getBoundingClientRect();
229+
230+
231+
let newX: number;
232+
233+
switch (this.align) {
234+
default:
235+
case 'left':
236+
newX = selectionRect.x - wrapperOffset.x;
237+
break;
238+
case 'right':
239+
newX = selectionRect.x + selectionRect.width - popoverWidth - wrapperOffset.x;
240+
break;
241+
case 'center':
242+
newX = selectionRect.x + selectionRect.width / 2 - popoverWidth / 2 - wrapperOffset.x;
243+
break;
244+
}
245+
221246
const newCoords = {
222-
x: selectionRect.x - wrapperOffset.x,
247+
x: newX,
223248
y: selectionRect.y +
224249
selectionRect.height -
225-
// + window.scrollY
226250
wrapperOffset.top +
227251
this.toolbarVerticalMargin,
228252
};
@@ -233,7 +257,14 @@ export default class InlineToolbar extends Module<InlineToolbarNodes> {
233257
* Prevent InlineToolbar from overflowing the content zone on the right side
234258
*/
235259
if (realRightCoord > this.Editor.UI.contentRect.right) {
236-
newCoords.x = this.Editor.UI.contentRect.right -popoverWidth - wrapperOffset.x;
260+
newCoords.x = this.Editor.UI.contentRect.right - popoverWidth - wrapperOffset.x;
261+
}
262+
263+
/**
264+
* Prevent InlineToolbar from overflowing the content zone on the left side
265+
*/
266+
if (newCoords.x < 0) {
267+
newCoords.x = 0;
237268
}
238269

239270
this.nodes.wrapper!.style.left = Math.floor(newCoords.x) + 'px';

types/configs/editor-config.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,12 @@ export interface EditorConfig {
100100
*/
101101
inlineToolbar?: string[]|boolean;
102102

103+
/**
104+
* Inline Toolbar alignment config
105+
*/
106+
alignInlineToolbar?: 'left' | 'center' | 'right';
107+
108+
103109
/**
104110
* Common Block Tunes list. Will be added to all the blocks which do not specify their own 'tunes' set
105111
*/

0 commit comments

Comments
 (0)