Skip to content
This repository was archived by the owner on Nov 8, 2022. It is now read-only.

Commit c6ac24c

Browse files
committed
refactor(editor): table header & strip export done
1 parent d2d1d0d commit c6ac24c

File tree

8 files changed

+72
-17
lines changed

8 files changed

+72
-17
lines changed

lib/helper/converter/editor_to_html/class.ex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ defmodule Helper.Converter.EditorToHTML.Class do
4747
"table" => %{
4848
"wrapper" => "table-wrapper",
4949
"cell" => "table-cell",
50+
"th_header" => "th_header",
51+
"td_stripe" => "td_stripe",
5052
"align_center" => "align-center",
5153
"align_left" => "align-left",
5254
"align_right" => "align-right"

lib/helper/converter/editor_to_html/frags/table.ex

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ defmodule Helper.Converter.EditorToHTML.Frags.Table do
1212
def get_row(group_items) do
1313
tr_content =
1414
Enum.reduce(group_items, "", fn item, acc ->
15-
acc <> frag(:td, item)
15+
cell_type = if Map.has_key?(item, "isHeader"), do: :th, else: :td
16+
acc <> frag(cell_type, item)
1617
end)
1718

1819
~s(<tr>#{tr_content}</tr>)
@@ -21,25 +22,37 @@ defmodule Helper.Converter.EditorToHTML.Frags.Table do
2122
def frag(:td, item) do
2223
%{
2324
"align" => align,
24-
# "isZebraStripe" => isZebraStripe,
25+
"isStripe" => is_stripe,
2526
"text" => text
2627
} = item
2728

28-
IO.inspect(Map.has_key?(item, "width"), label: "the width")
29-
3029
cell_class = @class["cell"]
3130
align_class = get_align_class(align)
31+
scripe_class = if is_stripe, do: @class["td_stripe"], else: ""
3232

3333
case Map.has_key?(item, "width") do
3434
true ->
3535
style = ~s(width: #{Map.get(item, "width")})
36-
~s(<td style="#{style}"><div class="#{cell_class} #{align_class}">#{text}</div></td>)
36+
37+
~s(<td class="#{scripe_class}" style="#{style}"><div class="#{cell_class} #{align_class}">#{
38+
text
39+
}</div></td>)
3740

3841
false ->
39-
~s(<td><div class="#{cell_class} #{align_class}">#{text}</div></td>)
42+
~s(<td class="#{scripe_class}"><div class="#{cell_class} #{align_class}">#{text}</div></td>)
4043
end
4144
end
4245

46+
def frag(:th, item) do
47+
%{"align" => align, "text" => text} = item
48+
49+
cell_class = @class["cell"]
50+
align_class = get_align_class(align)
51+
header_class = @class["th_header"]
52+
53+
~s(<th class="#{header_class}"><div class="#{cell_class} #{align_class}">#{text}</div></th>)
54+
end
55+
4356
defp get_align_class("center"), do: @class["align_center"]
4457
defp get_align_class("right"), do: @class["align_right"]
4558
defp get_align_class(_), do: @class["align_left"]

lib/helper/converter/editor_to_html/frontend_test/script.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/helper/converter/editor_to_html/frontend_test/styles.css

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,17 @@ td {
250250
vertical-align: inherit;
251251
}
252252

253+
.th_header {
254+
padding-top: 18px;
255+
font-weight: bold;
256+
border-bottom: 2px solid #dbdbe2 !important;
257+
display: table-cell;
258+
vertical-align: inherit;
259+
}
260+
.td_stripe {
261+
background: #f7f7f7;
262+
}
263+
253264
.article-viewer-wrapper table .table-cell {
254265
padding: 12px 10px;
255266
vertical-align: top;

lib/helper/converter/editor_to_html/index.ex

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,9 @@ defmodule Helper.Converter.EditorToHTML do
6868
defp parse_block(%{"type" => "table", "data" => data}) do
6969
%{"items" => items, "columnCount" => column_count} = data
7070

71-
groupped_items = Enum.split(items, column_count) |> Tuple.to_list()
71+
# IO.inspect(column_count, label: "the fuck column_count")
72+
73+
groupped_items = Enum.chunk_every(items, column_count)
7274

7375
rows_content =
7476
Enum.reduce(groupped_items, "", fn group, acc ->

lib/helper/converter/editor_to_html/validator/editor_schema.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ defmodule Helper.Converter.EditorToHTML.Validator.EditorSchema do
5252
item: %{
5353
"text" => [:string],
5454
"align" => [enum: @valid_table_align],
55-
"isZebraStripe" => [:boolean],
55+
"isStripe" => [:boolean],
56+
"isHeader" => [:boolean, required: false],
5657
"width" => [:string, required: false]
5758
}
5859
]

lib/helper/converter/html_sanitizer.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ defmodule Helper.Converter.HtmlSanitizer do
4242
Meta.allow_tag_with_these_attributes("table", [])
4343
Meta.allow_tag_with_these_attributes("tbody", [])
4444
Meta.allow_tag_with_these_attributes("tr", [])
45-
Meta.allow_tag_with_these_attributes("td", ["style"])
45+
Meta.allow_tag_with_these_attributes("th", ["class"])
46+
Meta.allow_tag_with_these_attributes("td", ["class", "style"])
4647

4748
Meta.allow_tag_with_these_attributes("svg", [
4849
"t",

test/helper/converter/editor_to_html_test/table_test.exs

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,38 +33,63 @@ defmodule GroupherServer.Test.Helper.Converter.EditorToHTML.Table do
3333
set_items(4, [
3434
%{
3535
"align" => "left",
36-
"isZebraStripe" => false,
36+
"isHeader" => true,
37+
"isStripe" => false,
38+
"text" => "title 0"
39+
},
40+
%{
41+
"align" => "center",
42+
"isHeader" => true,
43+
"isStripe" => false,
44+
"text" => "title 1",
45+
"width" => "180px"
46+
},
47+
%{
48+
"align" => "right",
49+
"isHeader" => true,
50+
"isStripe" => false,
51+
"text" => "title 2"
52+
},
53+
%{
54+
"align" => "left",
55+
"isHeader" => true,
56+
"isStripe" => false,
57+
"text" => "title 3"
58+
},
59+
%{
60+
"align" => "left",
61+
"isStripe" => false,
3762
"text" => "cell 0"
3863
},
3964
%{
4065
"align" => "center",
41-
"isZebraStripe" => false,
66+
"isStripe" => false,
4267
"text" => "cell 1",
4368
"width" => "180px"
4469
},
4570
%{
4671
"align" => "right",
47-
"isZebraStripe" => false,
72+
"isStripe" => false,
4873
"text" => "cell 2"
4974
},
5075
%{
5176
"align" => "left",
52-
"isZebraStripe" => false,
77+
"isStripe" => false,
5378
"text" => "cell 3"
5479
},
5580
%{
5681
"align" => "left",
57-
"isZebraStripe" => false,
82+
"isStripe" => true,
5883
"text" => "cell 4"
5984
},
6085
%{
6186
"align" => "left",
62-
"isZebraStripe" => false,
87+
"isStripe" => true,
6388
"text" => "cell 5"
6489
},
6590
%{
6691
"align" => "left",
67-
"isZebraStripe" => false,
92+
"isStripe" => true,
6893
"text" => ""
6994
}
7095
])

0 commit comments

Comments
 (0)