Skip to content

Commit

Permalink
Fixes to EditorJSSoupElement attribute addition with class (styles NY…
Browse files Browse the repository at this point in the history
…I yet), add tool_name as class on wagtail blocks.Block 'div' wrapper
  • Loading branch information
Nigel2392 committed May 13, 2024
1 parent 5e3f902 commit 3efe7f8
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 9 deletions.
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ class TextBlock(EditorJSFeatureStructBlock):

class Meta:
template = "myapp/text_block.html"
allowed_tags = ["div", "h1", "h2", "p"]
allowed_tags = ["h1", "h2", "p"]
# Html looks like:
# <h1>{{ self.heading.title }}</h1>
# <h2>{{ self.heading.subtitle }}</h2>
Expand All @@ -166,6 +166,18 @@ def register_editor_js_features(registry: EditorJSFeatures):
)
```

The block will then be rendered as any structblock, but it will be wrapped in a div with the class `wagtail-text-block` (the feature name).

Example:

```html
<div class="wagtail-text-block">
<h1>My title</h1>
<h2>My subtitle</h2>
<p>My body</p>
</div>
```

## Settings

### `EDITORJS_CLEAN_HTML`
Expand Down
21 changes: 16 additions & 5 deletions wagtail_editorjs/features/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,10 +356,21 @@ def allowed_tags(self):
@property
def allowed_attributes(self):
if hasattr(self.block, "allowed_attributes"):
return self.block.allowed_attributes or {}
if hasattr(self.block.meta, "allowed_attributes"):
return self.block.meta.allowed_attributes or {}
return super().allowed_attributes or {}
d = self.block.allowed_attributes
elif hasattr(self.block.meta, "allowed_attributes"):
d = self.block.meta.allowed_attributes
else:
d = super().allowed_attributes

d = d or {}

class_d = d.setdefault("class", [])
if isinstance(class_d, str):
class_d = [class_d]

class_d.append(self.tool_name)

return d

@property
def js(self):
Expand All @@ -371,7 +382,7 @@ def js(self):
def render_block_data(self, block: EditorJSBlock, context=None) -> EditorJSElement:
prefix = block["data"].get("__prefix__") or ""
value: blocks.StructValue = self.block.value_from_datadict(block["data"].get("block", {}), {}, prefix)
return EditorJSSoupElement(f"<div>{ self.block.render(value) }</div>")
return EditorJSSoupElement(f"<div class=\"{self.tool_name}\">{ self.block.render(value) }</div>")

@property
def css(self):
Expand Down
12 changes: 9 additions & 3 deletions wagtail_editorjs/registry/element/element.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,15 @@ def append(self, element: "EditorJSElement"):

def add_attributes(self, **attrs: Union[str, list[str], dict[str, Any]]):
for key, value in attrs.items():
if key == "class_":
key = "class"
self.soupContent[key] = str(value)
if key == "class_" or key == "class":
classList = self.soupContent.get("class", [])
if isinstance(value, str):
classList.append(value)
elif isinstance(value, list):
classList.extend(value)
self.soupContent["class"] = classList
else:
self.soupContent[key] = value


def wrapper(element: EditorJSElement, attrs: dict[str, EditorJSElementAttribute] = None, tag: str = "div"):
Expand Down

0 comments on commit 3efe7f8

Please sign in to comment.