Skip to content

Handled Custom Block Tools #2

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

Open
wants to merge 3 commits 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
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,27 @@ class Post(models.Model):
- `Attaches` - (`dict`) configuration for tool `AttachesTool`. (_For more info see official documentation for tool_).
- `Table` - (`dict`) configuration for tool `Table`. (_For more info see official documentation for tool_).

## Using Custom Block Tools

You can now add custom block tools by serving a js file named "custom-editorjs-tools.js" in the static folder, this can contain any custom block tool, let's imagine it contains a *SimpleImage* block tool. You can now use this block by setting something like this in the configuration. **Warning: The class content should not ever depend on user input or any nontrusted input, as it could lead to xss**

```python
class Post(models.Model):
title = models.TextField()
body = EditorJsField(
editorjs_config={
"tools": {
"Custom": {
"Customimage": {
"class": "SimpleImage"
}
}
}
}
)

```

# API

- `EditorJsField`
Expand Down
18 changes: 18 additions & 0 deletions django_editorjs/static/django-editorjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,20 @@
);
}

/**
* @param {Object} config
*/
function getCustomTools(config){
if (config && config.tools && config.tools["Custom"]){
var tools = {};
for (const [key, value] of Object.entries(config.tools["Custom"])) {
tools[key] = value;
tools[key].class = eval(tools[key].class);
}
return tools;
}
}

/**
* @param {HTMLDivElement} field_wrapper
*/
Expand Down Expand Up @@ -109,6 +123,10 @@
inlineToolbar: true,
});
}
tools = {
...tools,
...getCustomTools(config)
};

const editor = new EditorJS({
holder: holder_el,
Expand Down
1 change: 1 addition & 0 deletions django_editorjs/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def media(self):
css={"all": ["django-editorjs.css"]},
js=(
"https://cdn.jsdelivr.net/combine/npm/@editorjs/[email protected],npm/@editorjs/[email protected],npm/@editorjs/[email protected],npm/@editorjs/[email protected],npm/@editorjs/[email protected],npm/@editorjs/[email protected],npm/@editorjs/[email protected],npm/@editorjs/[email protected],npm/@editorjs/[email protected],npm/@editorjs/[email protected],npm/@editorjs/[email protected],npm/@editorjs/[email protected],npm/@editorjs/[email protected],npm/@editorjs/[email protected],npm/@editorjs/[email protected]",
"custom-editorjs-tools.js",
"django-editorjs.js",
),
)
Expand Down