diff --git a/lib/rich-text.rb b/lib/rich-text.rb index f48c495..0a12579 100644 --- a/lib/rich-text.rb +++ b/lib/rich-text.rb @@ -15,15 +15,19 @@ def self.configure RichText.configure do |c| - c.html_inline_formats = { + c.html_inline_formats ||= { bold: { tag: 'strong' }, br: { tag: 'br' }, hr: { tag: 'hr', block_format: false }, italic: { tag: 'em' }, - link: { tag: 'a', apply: ->(el, op, ctx){ el[:href] = op.attributes[:link] } } + link: { tag: 'a', apply: ->(el, op, ctx){ el[:href] = op.attributes[:link] } }, + size: { tag: 'span', apply: ->(el, op, ctx) { el[:style] = el[:style].to_s + "font-size: #{op.attributes[:size]};" } }, + color: { tag: 'span', apply: ->(el, op, ctx) { el[:style] = el[:style].to_s + "color: #{op.attributes[:color]};" } }, + background: { tag: 'span', apply: ->(el, op, ctx) { el[:style] = el[:style].to_s + "background: #{op.attributes[:background]};" } }, } - c.html_block_formats = { + c.html_block_formats ||= { + align: { apply: ->(el, op, ctx) { el[:style] = "text-align: #{op.attributes[:align]}" } }, firstheader: { tag: 'h1' }, secondheader: { tag: 'h2' }, thirdheader: { tag: 'h3' }, diff --git a/test/unit/html_test.rb b/test/unit/html_test.rb index f4d9a7e..717bf71 100644 --- a/test/unit/html_test.rb +++ b/test/unit/html_test.rb @@ -7,10 +7,14 @@ bold: { tag: 'strong' }, br: { tag: 'br' }, italic: { tag: 'em' }, - link: { tag: 'a', apply: ->(el, op, ctx){ el[:href] = op.attributes[:link] } } + link: { tag: 'a', apply: ->(el, op, ctx){ el[:href] = op.attributes[:link] } }, + size: { tag: 'span', apply: ->(el, op, ctx) { el[:style] = el[:style].to_s + "font-size: #{op.attributes[:size]};" } }, + color: { tag: 'span', apply: ->(el, op, ctx) { el[:style] = el[:style].to_s + "color: #{op.attributes[:color]};" } }, + background: { tag: 'span', apply: ->(el, op, ctx) { el[:style] = el[:style].to_s + "background: #{op.attributes[:background]};" } }, }.freeze c.html_block_formats = { + align: { apply: ->(el, op, ctx) { el[:style] = el[:style].to_s + "text-align: #{op.attributes[:align]};" } }, firstheader: { tag: 'h1' }, secondheader: { tag: 'h2' }, thirdheader: { tag: 'h3' }, @@ -350,7 +354,33 @@ assert_equal '
mali principii
malus finis
', render_compact_html(d) end + it 'renders a paragraph with alignment' do + d = RichText::Delta.new([{ insert: "dextra", attributes: { align: "right"}}]) + assert_equal 'dextra
', render_compact_html(d) + end + + it 'renders a header with alignment' do + d = RichText::Delta.new([{ insert: "dextra", attributes: { align: "right", firstheader: true }}]) + assert_equal 'red balloon in blue sky
', render_compact_html(d) + end + + it 'renders a paragraph with many attributes' do + d = RichText::Delta.new([{ insert: "hello\n" }, { insert: "goodbye\n" }]) + d = RichText::Delta.new([{"attributes"=>{"color"=>"#e60000", "background"=>"#ffff00", "size"=>"21px"}, "insert"=>"hello"}, {"attributes"=>{"bold" => true, "align"=>"left", size: "10px"}, "insert"=>"world"}]) + assert_equal 'helloworld
', render_compact_html(d) + end + + it 'renders a paragraph with size' do + d = RichText::Delta.new([{ insert: "big text", attributes: { size: "50px" }}]) + assert_equal 'big text
', render_compact_html(d) + end + def render_compact_html(delta, options={}) RichText::HTML.new(options).render(delta).inner_html(save_with: 0) end -end \ No newline at end of file +end