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

custom onClick handler #14

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ var editor = EditorJS({
class: LinkTool,
config: {
endpoint: 'http://localhost:8008/fetchUrl', // Your backend endpoint for url data fetching
onClick: (data) => console.log(data) //handle what happens on click
}
}
}
Expand All @@ -67,9 +68,10 @@ var editor = EditorJS({

Link Tool supports these configuration parameters:

| Field | Type | Description |
| ---------|-------------|------------------------------------------------|
| endpoint | `string` | **Required:** endpoint for link data fetching. |
| Field | Type | Description |
| ---------|----------------------------|------------------------------------------------|
| endpoint | `string` | **Required:** endpoint for link data fetching. |
| onClick | (data_from_bellow): void | Custom click handle. |

## Output data

Expand Down
14 changes: 11 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ export default class LinkTool {
* Tool's initial config
*/
this.config = {
endpoint: config.endpoint || ''
endpoint: config.endpoint || '',
onClick: config.onClick || null
};

this.nodes = {
Expand Down Expand Up @@ -270,6 +271,13 @@ export default class LinkTool {
rel: 'nofollow noindex noreferrer'
});

if (this.config.onClick) {
holder.addEventListener('click', (e) => {
e.preventDefault();
this.config.onClick(this._data);
});
}

this.nodes.linkImage = this.make('div', this.CSS.linkImage);
this.nodes.linkTitle = this.make('div', this.CSS.linkTitle);
this.nodes.linkDescription = this.make('p', this.CSS.linkDescription);
Expand Down Expand Up @@ -408,15 +416,15 @@ export default class LinkTool {
* @return {HTMLElement}
*/
make(tagName, classNames = null, attributes = {}) {
let el = document.createElement(tagName);
const el = document.createElement(tagName);

if (Array.isArray(classNames)) {
el.classList.add(...classNames);
} else if (classNames) {
el.classList.add(classNames);
}

for (let attrName in attributes) {
for (const attrName in attributes) {
el[attrName] = attributes[attrName];
}

Expand Down