Sha256: eebda6e88ba8f2cfed9ddd8b53797b947933743bf6b5f5bb6fc420a352456df2

Contents?: true

Size: 1.14 KB

Versions: 3

Compression:

Stored size: 1.14 KB

Contents

# frozen_string_literal: true

class Scarpe
  class WebviewSpan < Scarpe::WebviewWidget
    SIZES = {
      inscription: 10,
      ins: 10,
      span: 12,
      caption: 14,
      tagline: 18,
      subtitle: 26,
      title: 34,
      banner: 48,
    }.freeze
    private_constant :SIZES

    def initialize(properties)
      super
    end

    def properties_changed(changes)
      text = changes.delete("text")
      if text
        html_element.inner_html = text
        return
      end

      # Not deleting, so this will re-render
      if changes["size"] && SIZES[@size.to_sym]
        @size = @size.to_sym
      end

      super
    end

    def element(&block)
      HTML.render do |h|
        h.span(**options, &block)
      end
    end

    def to_html
      element { @text }
    end

    private

    def options
      @html_attributes.merge(id: html_id, style: style)
    end

    def style
      {
        "color" => @stroke,
        "font-size" => font_size,
        "font-family" => @font,
      }.compact
    end

    def font_size
      font_size = @size.is_a?(Symbol) ? SIZES[@size] : @size

      Dimensions.length(font_size)
    end
  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
lacci-0.2.1 lib/scarpe/wv/span.rb
scarpe-0.2.1 lib/scarpe/wv/span.rb
scarpe-0.2.0 lib/scarpe/wv/span.rb