diff --git a/setup.cfg b/setup.cfg index d4cb537..bc3ac20 100644 --- a/setup.cfg +++ b/setup.cfg @@ -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 diff --git a/wagtail_editorjs/__init__.py b/wagtail_editorjs/__init__.py index e57944a..8189314 100644 --- a/wagtail_editorjs/__init__.py +++ b/wagtail_editorjs/__init__.py @@ -11,5 +11,5 @@ -__version__ = '1.6.3' +__version__ = '1.6.4' VERSION = pv.LooseVersion(__version__) diff --git a/wagtail_editorjs/features/blocks.py b/wagtail_editorjs/features/blocks.py index 46e4eec..0d1480b 100644 --- a/wagtail_editorjs/features/blocks.py +++ b/wagtail_editorjs/features/blocks.py @@ -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"): @@ -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"
{ self.block.render(value) }
") + @classmethod + def get_test_data(cls): + return [] + @property def css(self): return self.widget.media._css diff --git a/wagtail_editorjs/static/wagtail_editorjs/js/tools/wagtail-block.js b/wagtail_editorjs/static/wagtail_editorjs/js/tools/wagtail-block.js index 47b5281..6997fb9 100644 --- a/wagtail_editorjs/static/wagtail_editorjs/js/tools/wagtail-block.js +++ b/wagtail_editorjs/static/wagtail_editorjs/js/tools/wagtail-block.js @@ -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(); } @@ -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 || {}; } } diff --git a/wagtail_editorjs/test/core/tests/test_blocks.py b/wagtail_editorjs/test/core/tests/test_blocks.py index b76d193..ce9fe6d 100644 --- a/wagtail_editorjs/test/core/tests/test_blocks.py +++ b/wagtail_editorjs/test/core/tests/test_blocks.py @@ -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 = {