Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow holderId work with ref on dom element #710

Merged
merged 17 commits into from
Apr 15, 2019
6 changes: 4 additions & 2 deletions src/components/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,9 @@ export default class Core {
/**
* If holderId is empty then set a default value
*/
if (!this.config.holderId || typeof this.config.holderId !== 'string') {
if (!this.config.holderId ||
!$.isElement(this.config.holderId) ||
typeof this.config.holderId !== 'string') {
this.config.holderId = 'editorjs';
}

Expand Down Expand Up @@ -197,7 +199,7 @@ export default class Core {
/**
* Check for a holder element's existence
*/
if (!$.get(this.config.holderId)) {
if (typeof this.config.holderId === 'string' && !$.get(this.config.holderId)) {
throw Error(`element with ID «${this.config.holderId}» is missing. Pass correct holder's ID.`);
}
}
Expand Down
11 changes: 10 additions & 1 deletion src/components/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ export default class Dom {
* @param {Object} node
* @returns {boolean}
*/
public static isElement(node: any): boolean {
public static isElement(node: any): node is Element {
return node && typeof node === 'object' && node.nodeType && node.nodeType === Node.ELEMENT_NODE;
}

Expand Down Expand Up @@ -518,4 +518,13 @@ export default class Dom {
return [...result, ...Dom.getDeepestBlockElements(element as HTMLElement)];
}, []);
}

/**
* Helper for get holder from {string} or return HTMLElement
* @param element
*/
public static getHolder(element: string | HTMLElement): HTMLElement {
if (typeof element === 'string') { return document.getElementById(element); }
return element;
}
}
2 changes: 1 addition & 1 deletion src/components/modules/blockManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ export default class BlockManager extends Module {
* @returns {Block}
*/
public getBlock(element: HTMLElement): Block {
if (!$.isElement(element)) {
Copy link
Author

@dimensi dimensi Apr 9, 2019

Choose a reason for hiding this comment

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

Not understand why you do this. Than i change return type isElement method, i start get errors
[tsl] ERROR in /home/dimensi/projects/@editorjs/editor.js/src/components/modules/blockManager.ts(459,25) TS2339: Property 'parentNode' does not exist on type 'never'.

if (!$.isElement(element) as boolean) {
element = element.parentNode as HTMLElement;
}

Expand Down
9 changes: 3 additions & 6 deletions src/components/modules/ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,7 @@ export default class UI extends Module {
* Element where we need to append Editor.js
* @type {Element}
*/
this.nodes.holder = document.getElementById(this.config.holderId);

if (!this.nodes.holder) {
Copy link
Author

Choose a reason for hiding this comment

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

I remove it, because we check on exists in validation method

throw Error('Holder wasn\'t found by ID: #' + this.config.holderId);
}
this.nodes.holder = $.getHolder(this.config.holderId);

/**
* Create and save main UI elements
Expand Down Expand Up @@ -329,7 +325,8 @@ export default class UI extends Module {
*/
const target = event.target as HTMLElement;
const clickedOnInlineToolbarButton = target.closest(`.${this.Editor.InlineToolbar.CSS.inlineToolbar}`);
const clickedInsideOfEditor = !!target.closest(`#${this.config.holderId}`) || Selection.isAtEditor;

const clickedInsideOfEditor = this.nodes.holder.contains(target) || Selection.isAtEditor;

if (!clickedInsideOfEditor) {
/**
Expand Down
3 changes: 2 additions & 1 deletion types/configs/editor-config.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import {SanitizerConfig} from './sanitizer-config';
export interface EditorConfig {
/**
* Element where Editor will be append
* TODO: rename holderId on holder, element or el in next major for better consistent
*/
holderId?: string;
holderId?: string | HTMLElement;

/**
* If true, set caret at the first Block after Editor is ready
Expand Down