Class: Crm::ArticleRevisionDiffRowComponent::SegmentCursor

Inherits:
Object
  • Object
show all
Defined in:
app/components/crm/article_revision_diff_row_component/segment_cursor.rb

Overview

Consumes diff segments across multiple DOM text nodes without flattening
the surrounding headings, lists, tables, and links.

Instance Method Summary collapse

Constructor Details

#initialize(segments) ⇒ SegmentCursor

Returns a new instance of SegmentCursor.

Parameters:

  • segments (Array<Array(String, Boolean)>)

    ordered (text, changed?) pairs to consume



7
8
9
10
11
# File 'app/components/crm/article_revision_diff_row_component/segment_cursor.rb', line 7

def initialize(segments)
  @segments = segments
  @segment_index = 0
  @segment_offset = 0
end

Instance Method Details

#take(text) ⇒ Array<Array(String, Boolean)>

Splits the given text into pieces aligned with the remaining segments.

Parameters:

  • text (String)

    text of the current DOM node

Returns:

  • (Array<Array(String, Boolean)>)

    (piece, changed?) pairs



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/components/crm/article_revision_diff_row_component/segment_cursor.rb', line 17

def take(text)
  remaining = text.dup
  pieces = []
  while remaining.length.positive?
    segment = current_segment
    unless segment
      pieces << [remaining, false]
      break
    end

    length = [remaining.length, available_length].min
    pieces << [remaining.slice!(0, length), segment.last]
    consume(length)
  end
  pieces
end