Skip to content

Commit 8a53ba5

Browse files
authored
Selection of the element only in contenteditable (codex-team#542)
1 parent 185eed8 commit 8a53ba5

1 file changed

Lines changed: 23 additions & 16 deletions

File tree

‎src/components/modules/toolbar/inline.ts‎

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export default class InlineToolbar extends Module {
2121
* Returns internal inline tools
2222
* Includes Bold, Italic, Link
2323
*/
24-
private get internalTools(): {[name: string]: InlineTool} {
24+
private get internalTools(): { [name: string]: InlineTool } {
2525
return {
2626
bold: this.Editor.Tools.constructInline(BoldInlineTool),
2727
italic: this.Editor.Tools.constructInline(ItalicInlineTool),
@@ -33,7 +33,7 @@ export default class InlineToolbar extends Module {
3333
* Get external tools
3434
* Tools that has isInline is true
3535
*/
36-
private get externalTools(): {[name: string]: InlineTool} {
36+
private get externalTools(): { [name: string]: InlineTool } {
3737
const result = {};
3838

3939
for (const tool in this.Editor.Tools.inline) {
@@ -55,12 +55,13 @@ export default class InlineToolbar extends Module {
5555
actionsWrapper: 'ce-inline-toolbar__actions',
5656
inlineToolButton: 'ce-inline-tool',
5757
inlineToolButtonLast: 'ce-inline-tool--last',
58+
inputField: 'cdx-input',
5859
};
5960

6061
/**
6162
* Inline Toolbar elements
6263
*/
63-
private nodes: {wrapper: HTMLElement, buttons: HTMLElement, actions: HTMLElement} = {
64+
private nodes: { wrapper: HTMLElement, buttons: HTMLElement, actions: HTMLElement } = {
6465
wrapper: null,
6566
buttons: null,
6667
/**
@@ -155,10 +156,10 @@ export default class InlineToolbar extends Module {
155156
const newCoords = {
156157
x: selectionRect.x - wrapperOffset.left,
157158
y: selectionRect.y
158-
+ selectionRect.height
159-
// + window.scrollY
160-
- wrapperOffset.top
161-
+ this.toolbarVerticalMargin,
159+
+ selectionRect.height
160+
// + window.scrollY
161+
- wrapperOffset.top
162+
+ this.toolbarVerticalMargin,
162163
};
163164

164165
/**
@@ -177,7 +178,7 @@ export default class InlineToolbar extends Module {
177178
*/
178179
public close(): void {
179180
this.nodes.wrapper.classList.remove(this.CSS.inlineToolbarShowed);
180-
this.tools.forEach( (toolInstance, toolName) => {
181+
this.tools.forEach((toolInstance, toolName) => {
181182
if (typeof toolInstance.clear === 'function') {
182183
toolInstance.clear();
183184
}
@@ -201,7 +202,7 @@ export default class InlineToolbar extends Module {
201202
/**
202203
* Call 'clear' method for Inline Tools (for example, 'link' want to clear input)
203204
*/
204-
this.tools.forEach( (toolInstance: InlineTool) => {
205+
this.tools.forEach((toolInstance: InlineTool) => {
205206
if (typeof toolInstance.clear === 'function') {
206207
toolInstance.clear();
207208
}
@@ -223,6 +224,12 @@ export default class InlineToolbar extends Module {
223224
return false;
224225
}
225226

227+
// The selection of the element only in contenteditable
228+
const contenteditable = event.target.closest('[contenteditable="true"]');
229+
if (contenteditable === null) {
230+
return false;
231+
}
232+
226233
const currentSelection = SelectionUtils.get(),
227234
selectedText = SelectionUtils.text;
228235

@@ -301,7 +308,7 @@ export default class InlineToolbar extends Module {
301308
* Fill Inline Toolbar with Tools
302309
*/
303310
private addTools(): void {
304-
this.tools.forEach( (toolInstance, toolName) => {
311+
this.tools.forEach((toolInstance, toolName) => {
305312
this.addTool(toolName, toolInstance);
306313
});
307314
}
@@ -348,14 +355,14 @@ export default class InlineToolbar extends Module {
348355
*/
349356
const internalTools: string[] = Object
350357
.entries(Tools.internalTools)
351-
.filter(([name, toolClass]: [string, ToolConstructable|ToolSettings]) => {
358+
.filter(([name, toolClass]: [string, ToolConstructable | ToolSettings]) => {
352359
if (_.isFunction(toolClass)) {
353360
return toolClass[Tools.apiSettings.IS_INLINE];
354361
}
355362

356363
return (toolClass as ToolSettings).class[Tools.apiSettings.IS_INLINE];
357364
})
358-
.map(([name, toolClass]: [string, InlineToolConstructable|ToolSettings]) => name);
365+
.map(([name, toolClass]: [string, InlineToolConstructable | ToolSettings]) => name);
359366

360367
/**
361368
* 1) For internal tools, check public getter 'shortcut'
@@ -395,9 +402,9 @@ export default class InlineToolbar extends Module {
395402
* it can be used by tools like «Mention» that works without selection:
396403
* Example: by SHIFT+@ show dropdown and insert selected username
397404
*/
398-
// if (SelectionUtils.isCollapsed) return;
405+
// if (SelectionUtils.isCollapsed) return;
399406

400-
const toolSettings = this.Editor.Tools.getToolSettings(currentBlock.name);
407+
const toolSettings = this.Editor.Tools.getToolSettings(currentBlock.name);
401408

402409
if (!toolSettings || !toolSettings[this.Editor.Tools.apiSettings.IS_ENABLED_INLINE_TOOLBAR]) {
403410
return;
@@ -424,7 +431,7 @@ export default class InlineToolbar extends Module {
424431
* Check Tools` state by selection
425432
*/
426433
private checkToolsState(): void {
427-
this.tools.forEach( (toolInstance) => {
434+
this.tools.forEach((toolInstance) => {
428435
toolInstance.checkState(SelectionUtils.get());
429436
});
430437
}
@@ -433,7 +440,7 @@ export default class InlineToolbar extends Module {
433440
* Get inline tools tools
434441
* Tools that has isInline is true
435442
*/
436-
private get inlineTools(): {[name: string]: InlineTool} {
443+
private get inlineTools(): { [name: string]: InlineTool } {
437444
const result = {};
438445

439446
for (const tool in this.Editor.Tools.inline) {

0 commit comments

Comments
 (0)