Sha256: f393f2ab58b9c55ae0f141f3adc6f31fdffc20fbf8af497aa36a8918e9ae5eb2

Contents?: true

Size: 1.36 KB

Versions: 4

Compression:

Stored size: 1.36 KB

Contents

Trix.extend
  normalizeSpaces: (string) ->
    string
      .replace(///#{Trix.ZERO_WIDTH_SPACE}///g, "")
      .replace(///#{Trix.NON_BREAKING_SPACE}///g, " ")

  summarizeStringChange: (oldString, newString) ->
    oldString = Trix.UTF16String.box(oldString)
    newString = Trix.UTF16String.box(newString)

    if newString.length < oldString.length
      [removed, added] = utf16StringDifferences(oldString, newString)
    else
      [added, removed] = utf16StringDifferences(newString, oldString)

    {added, removed}

utf16StringDifferences = (a, b) ->
  return ["", ""] if a.isEqualTo(b)

  diffA = utf16StringDifference(a, b)
  {length} = diffA.utf16String

  diffB = if length
    {offset} = diffA
    codepoints = a.codepoints.slice(0, offset).concat(a.codepoints.slice(offset + length))
    utf16StringDifference(b, Trix.UTF16String.fromCodepoints(codepoints))
  else
    utf16StringDifference(b, a)

  [diffA.utf16String.toString(), diffB.utf16String.toString()]

utf16StringDifference = (a, b) ->
  leftIndex = 0
  rightIndexA = a.length
  rightIndexB = b.length

  while leftIndex < rightIndexA and a.charAt(leftIndex).isEqualTo(b.charAt(leftIndex))
    leftIndex++

  while rightIndexA > leftIndex + 1 and a.charAt(rightIndexA - 1).isEqualTo(b.charAt(rightIndexB - 1))
    rightIndexA--
    rightIndexB--

  utf16String: a.slice(leftIndex, rightIndexA)
  offset: leftIndex

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
vapid-0.1.3 lib/vapid/vendor/trix/src/trix/core/helpers/strings.coffee
vapid-0.1.2 lib/vapid/vendor/trix/src/trix/core/helpers/strings.coffee
vapid-0.1.1 lib/vapid/vendor/trix/src/trix/core/helpers/strings.coffee
vapid-0.1.0 lib/vapid/vendor/trix/src/trix/core/helpers/strings.coffee