Skip to content

Commit

Permalink
Wagtail Chooser Support
Browse files Browse the repository at this point in the history
  • Loading branch information
Nigel2392 committed May 13, 2024
1 parent 3dfa61d commit 27e31ed
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 21 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ It is **not** allowed to be or include:

* A `StreamBlock` (mainly due to styling issues)
* A `ListBlock` (mainly due to styling issues)
* Any type of `ChooserBlock` (cannot initialize)
* A `RichTextBlock` (cannot initialize)

*Help with these issues is highly appreciated!*
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = wagtail_editorjs
version = 1.6.4
version = 1.6.5rc1
description = EditorJS as a widget for Wagtail, with Page- and Image chooser support
long_description = file: README.md
long_description_content_type = text/markdown
Expand Down
2 changes: 1 addition & 1 deletion wagtail_editorjs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@



__version__ = '1.6.4'
__version__ = '1.6.5rc1'
VERSION = pv.LooseVersion(__version__)
23 changes: 18 additions & 5 deletions wagtail_editorjs/features/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,9 +314,6 @@ def __init__(self,
if not isinstance(block, blocks.Block) and issubclass(block, blocks.Block):
block = block()

if not isinstance(block, (blocks.StructBlock, blocks.FieldBlock)):
raise TypeError("block must be an instance of EditorJSFeatureStructBlock or FieldBlock")

self.block = block

self.block.set_name(
Expand Down Expand Up @@ -397,9 +394,25 @@ def validate(self, data: Any):
if "block" not in data["data"]:
raise forms.ValidationError("Invalid block value")

self.block.clean(
data["data"]["block"],
try:
data["data"]["block"] = self.block.get_prep_value(
self.block.clean(self.block.to_python(data["data"]["block"]))
)
except blocks.StreamBlockValidationError as e:
raise e
except blocks.list_block.ListBlockValidationError as e:
raise e
except blocks.struct_block.StructBlockValidationError as e:
raise e
except Exception as e:
raise e

def value_for_form(self, value: dict) -> dict:
value = super().value_for_form(value)
value["data"]["block"] = self.block.get_form_state(
self.block.to_python(value["data"]["block"])
)
return value

def render_block_data(self, block: EditorJSBlock, context=None) -> EditorJSElement:
value: blocks.StructValue = self.block.to_python(block["data"]["block"])
Expand Down
4 changes: 3 additions & 1 deletion wagtail_editorjs/registry/feature_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ def prepare_value(self, tools: list[str], data: dict):
if key not in tools:
del tunes[key]

blocks[i] = item
blocks[i] = self[block_type].value_for_form(item)

data["blocks"] = list(filter(None, blocks))
return data
Expand Down Expand Up @@ -232,6 +232,8 @@ def validate_for_tools(self, tools: list[str], data: dict):
if item["type"] == tool:
tool_mapping.validate(item)

data["blocks"] = block_list

return data


Expand Down
8 changes: 8 additions & 0 deletions wagtail_editorjs/registry/features/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,14 @@ def __init__(self,
self.allowed_tags = set(allowed_tags)
self.allowed_attributes = allowed_attributes

def value_for_form(self, value: dict) -> dict:
"""
Prepare the value for the feature.
This is useful for when you need to modify the data
before it is passed to the frontend.
"""
return value

def init_static(self, css, js):
css = css or []
js = js or []
Expand Down
35 changes: 23 additions & 12 deletions wagtail_editorjs/static/wagtail_editorjs/js/tools/wagtail-block.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,25 +49,36 @@

const element = this.wrapperElement.querySelector(`#${this.blockPrefix}`);
const id = element.id;

//
if (!window.telepath) {
console.error('Telepath is not defined');
return;
console.error('Telepath is not defined');
return;
}

//
// const dataValue = JSON.parse(element.getAttribute('data-w-block-data-value'));
// const argumentsValue = JSON.parse(element.getAttribute('data-w-block-arguments-value'));
if (element.dataset.controller) {
delete element.dataset.controller;
}
const dataValue = JSON.parse(element.dataset.wBlockDataValue);
const argumentsValue = JSON.parse(element.dataset.wBlockArgumentsValue);
this.blockDef = telepath.unpack(dataValue);

this.block = this.blockDef.render(
element, id, ...argumentsValue,
)

if (this.data) {
this.block.setState(this.data["block"]);
}
this.wrapperElement.addEventListener('DOMNodeInserted', (e) => {
if (!(e.relatedNode.firstElementChild == this.wrapperElement)) {
return;
}

// Wait for the element block to be rendered by the browser
setTimeout(() => {
this.block = this.blockDef.render(
element, id, ...argumentsValue,
)
if (this.data) {
this.block.setState(this.data["block"]);
}
}, 0);
});

return super.render();
}
Expand All @@ -81,7 +92,7 @@
if (!this.block.getState) {
console.error('Block does not have a getState method', this.block)
} else {
this.data["block"] = this.block.getState();
this.data["block"] = this.block.getValue();
}
return this.data || {};
}
Expand Down

0 comments on commit 27e31ed

Please sign in to comment.