Class: DraftjsHtml::Draftjs::Block
- Inherits:
-
Struct
- Object
- Struct
- DraftjsHtml::Draftjs::Block
- Defined in:
- lib/draftjs_html/draftjs/block.rb
Defined Under Namespace
Classes: CharRange
Instance Attribute Summary collapse
-
#depth ⇒ Object
Returns the value of attribute depth.
-
#inline_style_ranges ⇒ Object
Returns the value of attribute inline_style_ranges.
-
#key ⇒ Object
Returns the value of attribute key.
-
#raw_entity_ranges ⇒ Object
Returns the value of attribute raw_entity_ranges.
-
#text ⇒ Object
(also: #plaintext)
Returns the value of attribute text.
-
#type ⇒ Object
Returns the value of attribute type.
Class Method Summary collapse
Instance Method Summary collapse
- #add_entity(key, range) ⇒ Object
- #add_style(name, range) ⇒ Object
- #blank? ⇒ Boolean
- #each_char ⇒ Object
- #each_range {|ranges.last| ... } ⇒ Object
- #entity_ranges ⇒ Object
- #inline_styles ⇒ Object
- #length ⇒ Object
Instance Attribute Details
#depth ⇒ Object
Returns the value of attribute depth
5 6 7 |
# File 'lib/draftjs_html/draftjs/block.rb', line 5 def depth @depth end |
#inline_style_ranges ⇒ Object
Returns the value of attribute inline_style_ranges
5 6 7 |
# File 'lib/draftjs_html/draftjs/block.rb', line 5 def inline_style_ranges @inline_style_ranges end |
#key ⇒ Object
Returns the value of attribute key
5 6 7 |
# File 'lib/draftjs_html/draftjs/block.rb', line 5 def key @key end |
#raw_entity_ranges ⇒ Object
Returns the value of attribute raw_entity_ranges
5 6 7 |
# File 'lib/draftjs_html/draftjs/block.rb', line 5 def raw_entity_ranges @raw_entity_ranges end |
#text ⇒ Object Also known as: plaintext
Returns the value of attribute text
5 6 7 |
# File 'lib/draftjs_html/draftjs/block.rb', line 5 def text @text end |
#type ⇒ Object
Returns the value of attribute type
5 6 7 |
# File 'lib/draftjs_html/draftjs/block.rb', line 5 def type @type end |
Class Method Details
.parse(raw) ⇒ Object
6 7 8 9 10 11 12 13 14 15 |
# File 'lib/draftjs_html/draftjs/block.rb', line 6 def self.parse(raw) new( key: raw['key'], text: raw['text'], type: raw['type'], depth: raw.fetch('depth', 0).to_i, inline_style_ranges: Array(raw['inlineStyleRanges']), raw_entity_ranges: Array(raw['entityRanges']), ) end |
Instance Method Details
#add_entity(key, range) ⇒ Object
77 78 79 |
# File 'lib/draftjs_html/draftjs/block.rb', line 77 def add_entity(key, range) entity_ranges << ApplicableRange.new(name: key, range: range) end |
#add_style(name, range) ⇒ Object
73 74 75 |
# File 'lib/draftjs_html/draftjs/block.rb', line 73 def add_style(name, range) inline_styles << ApplicableRange.new(name: name, range: range) end |
#blank? ⇒ Boolean
21 22 23 |
# File 'lib/draftjs_html/draftjs/block.rb', line 21 def blank? text.empty? && entity_ranges.empty? end |
#each_char ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/draftjs_html/draftjs/block.rb', line 25 def each_char return to_enum(:each_char) unless block_given? text.chars.map.with_index do |char, index| yield CharacterMeta.new( char: char, style_names: inline_styles.select { _1.range.cover?(index) }.map(&:name), entity_key: entity_ranges.select { _1.range.cover?(index) }.map(&:name).first, ) end end |
#each_range {|ranges.last| ... } ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/draftjs_html/draftjs/block.rb', line 38 def each_range return to_enum(:each_range) unless block_given? current_styles = [] current_entity = nil ranges = [CharRange.new(text: '', style_names: current_styles, entity_key: current_entity)] each_char.with_index do |char, index| if char.style_names != current_styles || char.entity_key != current_entity current_styles = char.style_names current_entity = char.entity_key yield(ranges.last) unless index == 0 ranges << CharRange.new(text: '', style_names: current_styles, entity_key: current_entity) end ranges.last.text += char.char end yield ranges.last end |
#entity_ranges ⇒ Object
67 68 69 70 71 |
# File 'lib/draftjs_html/draftjs/block.rb', line 67 def entity_ranges @entity_ranges ||= raw_entity_ranges.map do |raw| ApplicableRange.parse(raw['key'].to_s, raw) end end |
#inline_styles ⇒ Object
61 62 63 64 65 |
# File 'lib/draftjs_html/draftjs/block.rb', line 61 def inline_styles @inline_styles ||= inline_style_ranges.map do |raw| ApplicableRange.parse(raw['style'], raw) end end |
#length ⇒ Object
17 18 19 |
# File 'lib/draftjs_html/draftjs/block.rb', line 17 def length text.length end |