Skip to content

Commit

Permalink
Mirroring
Browse files Browse the repository at this point in the history
  • Loading branch information
Nigel2392 committed May 13, 2024
1 parent aefc7a4 commit b4682bd
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 49 deletions.
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.3
version = 1.6.4
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.3'
__version__ = '1.6.4'
VERSION = pv.LooseVersion(__version__)
21 changes: 8 additions & 13 deletions wagtail_editorjs/features/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,11 +338,12 @@ def get_config(self, context: dict[str, Any] = None) -> dict:
data = super().get_config(context)
config = data.get("config", {})
config["rendered"] = self.widget.render_with_errors(
"__ID__", self.block.get_default(),
"__PREFIX__", self.block.get_default(),
)
data["config"] = config
return data


@property
def allowed_tags(self):
if hasattr(self.block, "allowed_tags"):
Expand Down Expand Up @@ -393,27 +394,21 @@ def js(self):
def validate(self, data: Any):
super().validate(data)

prefix = data["data"].get("__prefix__") or ""

if not prefix:
raise forms.ValidationError("Invalid prefix value")

if "block" not in data["data"]:
raise forms.ValidationError("Invalid block value")

value: blocks.StructValue = self.block.value_from_datadict(
data["data"].get("block", {}), {}, prefix,
)

self.block.clean(
value,
data["data"]["block"],
)

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)
value: blocks.StructValue = self.block.to_python(block["data"]["block"])
return EditorJSSoupElement(f"<div class=\"{self.tool_name}\">{ self.block.render(value) }</div>")

@classmethod
def get_test_data(cls):
return []

@property
def css(self):
return self.widget.media._css
Expand Down
53 changes: 23 additions & 30 deletions wagtail_editorjs/static/wagtail_editorjs/js/tools/wagtail-block.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,36 +38,36 @@
className: 'wagtail-block-feature-wrapper',
});

this.blockPrefix = this.data.__prefix__ || `${blockName}-${Math.random().toString(36).substring(7)}`;
this.blockPrefix = `${blockName}-${Math.random().toString(36).substring(7)}`;

const html = this.config.rendered.replace(
/__ID__/g,
/__PREFIX__/g,
this.blockPrefix,
);

this.wrapperElement.innerHTML = html;

this.block = this.wrapperElement.firstChild;
this.inputs = [];
const element = this.wrapperElement.querySelector(`#${this.blockPrefix}`);
const id = element.id;

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

const inputs = this.wrapperElement.querySelectorAll('input, textarea, select');
for (let i = 0; i < inputs.length; i++) {
const inp = inputs[i];
if (inp.name.startsWith(this.blockPrefix)) {
this.inputs.push(inp);
}
}
// const dataValue = JSON.parse(element.getAttribute('data-w-block-data-value'));
// const argumentsValue = JSON.parse(element.getAttribute('data-w-block-arguments-value'));
const dataValue = JSON.parse(element.dataset.wBlockDataValue);
const argumentsValue = JSON.parse(element.dataset.wBlockArgumentsValue);
this.blockDef = telepath.unpack(dataValue);

if (this.data) {
console.log(this.data);
for (let i = 0; i < this.inputs.length; i++) {
this.inputs[i].value = this.data["block"][this.inputs[i].name] || '';
}
}
this.block = this.blockDef.render(
element, id, ...argumentsValue,
)

}, 0);
if (this.data) {
this.block.setState(this.data["block"]);
}

return super.render();
}
Expand All @@ -78,18 +78,11 @@

save(blockContent) {
this.data = super.save(blockContent);

if (!this.data["block"]) {
this.data["block"] = {};
if (!this.block.getState) {
console.error('Block does not have a getState method', this.block)
} else {
this.data["block"] = this.block.getState();
}

for (let i = 0; i < this.inputs.length; i++) {
let inp = this.inputs[i];
this.data["block"][inp.name] = inp.value || '';
}

this.data["__prefix__"] = this.blockPrefix;

return this.data || {};
}
}
Expand Down
10 changes: 6 additions & 4 deletions wagtail_editorjs/test/core/tests/test_blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,12 @@ def setUp(self) -> None:

def test_wagtail_block_feature(self):
test_data = {
"test_feature-title": "Test Title",
"test_feature-text": "Test Text",
"test_feature-sub_block-sub_title": "Sub Title",
"test_feature-sub_block-sub_text": "Sub Text",
"title": "Test Title",
"text": "Test Text",
"sub_block": {
"sub_title": "Sub Title",
"sub_text": "Sub Text",
},
}

feature_value = {
Expand Down

0 comments on commit b4682bd

Please sign in to comment.