Skip to content

Commit d232f0b

Browse files
authored
Fix line breaks were stripped out (#11)
2 parents fc10a4c + 0b1ddee commit d232f0b

5 files changed

Lines changed: 31 additions & 7 deletions

File tree

CHANGELOG.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,20 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [unreleased]
99

10-
## [0.3.1] - 2025-04-01
10+
## [0.3.2] - 2025-04-02
11+
12+
## Fixed
13+
14+
- Line breaks were stripped out ([#11](https://github.com/torchbox/wagtail-tinytableblock/pull/11))
15+
16+
17+
## [0.3.1] - 2025-04-02
1118

1219
## Added
1320

1421
- The missing unlink toolbar icon, when links are enabled
1522

16-
## [0.3] - 2025-04-01
23+
## [0.3] - 2025-04-02
1724

1825
## Added
1926

@@ -63,6 +70,7 @@ Initial release
6370

6471

6572
[unreleased]: https://github.com/torchbox/wagtail-tinytableblock/compare/v0.2.3...HEAD
73+
[0.3.2]: https://github.com/torchbox/wagtail-tinytableblock/compare/v0.3.1...v0.3.2
6674
[0.3.1]: https://github.com/torchbox/wagtail-tinytableblock/compare/v0.3...v0.3.1
6775
[0.3]: https://github.com/torchbox/wagtail-tinytableblock/compare/v0.2.4...v0.3
6876
[0.2.4]: https://github.com/torchbox/wagtail-tinytableblock/compare/v0.2.3...v0.2.4
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "0.3.1"
1+
__version__ = "0.3.2"

src/wagtail_tinytableblock/static/wagtail_tinytableblock/js/tiny-table-block.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class TinyTableBlockDefinition extends window.wagtailStreamField.blocks.FieldBlo
4242
table_sizing_mode: 'responsive',
4343
table_resize_bars: false,
4444
object_resizing: false,
45+
newline_behavior: "linebreak",
4546
contextmenu_never_use_native: contextmenu_never_use_native,
4647
skin: (window.matchMedia("(prefers-color-scheme: dark)").matches ? "oxide-dark" : "oxide"),
4748
content_css: (window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "default"),
@@ -90,7 +91,22 @@ class TinyTableBlockDefinition extends window.wagtailStreamField.blocks.FieldBlo
9091
e.preventDefault();
9192
}
9293
});
93-
}
94+
},
95+
paste_preprocess: function(editor, args) {
96+
// Check if the current selection is inside a table cell
97+
const isTableCell = editor.dom.getParent(editor.selection.getNode(), 'td,th');
98+
99+
if (isTableCell) {
100+
// Replace newlines with <br> tags
101+
args.content = args.content.replace(/\n/g, '<br>');
102+
103+
// Also unwrap any paragraphs that might come with the paste
104+
args.content = args.content.replace(/<p>(.*?)<\/p>/g, '$1<br>');
105+
106+
// Remove trailing <br> if present
107+
args.content = args.content.replace(/<br>$/, '');
108+
}
109+
}
94110
});
95111

96112
return block;

src/wagtail_tinytableblock/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616

1717
def sanitise_html(content: str, *, allow_links: bool = False) -> str:
18-
tags: set[str] = {"table", "tr", "th", "td", "thead", "tbody", "caption"}
18+
tags: set[str] = {"table", "tr", "th", "td", "thead", "tbody", "caption", "br"}
1919
attributes: dict[str, set[str]] = {
2020
"*": {"class"},
2121
"th": {"colspan", "rowspan", "align", "scope", "style"},

tests/test_utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def test_simple_table_with_thead(self):
3636
</thead>
3737
<tbody>
3838
<tr>
39-
<td>Cell 1</td>
39+
<td>Cell on<br> on two lines</td>
4040
<td>Cell 2</td>
4141
</tr>
4242
</tbody>
@@ -64,7 +64,7 @@ def test_simple_table_with_thead(self):
6464
result["rows"][0],
6565
[
6666
{
67-
"value": "Cell 1",
67+
"value": "Cell on<br/> on two lines",
6868
"type": "td",
6969
},
7070
{

0 commit comments

Comments
 (0)