<commit_before># utility functions to escape a query
# @param str the string to escape
escapeRegexp = (str) -> str.replace /([.?*+^$[\]\\(){}|-])/g, "\\$1"

# removes all img tags
# @param str a string possibly containing img tags
noImg = (str) -> str.replace /<img[^>]*>/g, ''

angular.module('radians.contenteditable', [])
.directive('contenteditable', ->
  require: 'ngModel',
  link: (scope, elmt, attrs, ctrl) ->
    old_render = ctrl.$render # save for later
    view_to_model = ->
      scope.$apply ->
        ctrl.$setViewValue elmt.html()
        null
      
    # view -> model
    elmt.bind 'blur', view_to_model
    elmt.bind 'input', view_to_model
    elmt.bind 'change', view_to_model

    # model -> view
    ctrl.$render = ->
      old_render() if old_render != null # old_render? leads to linted js
      elmt.html ctrl.$viewValue
      null

    null
)
.filter('typeaheadHighlight', ->
  # don't highlight anything!
  (matchItem, query) -> matchItem
)
.filter('ignoreImg', ->
  # when matching query against the items, ignore all img tags
  (items, query) ->
    item for item in items \
    when noImg(item).match(new RegExp(escapeRegexp(noImg(query)), 'gi'))
)
<commit_msg>move cursor position to the end of contenteditable div after model is changed<commit_after># utility functions to escape a query
# @param str the string to escape
escapeRegexp = (str) -> str.replace /([.?*+^$[\]\\(){}|-])/g, "\\$1"

# removes all img tags
# @param str a string possibly containing img tags
noImg = (str) -> str.replace /<img[^>]*>/g, ''

angular.module('radians.contenteditable', [])
.directive('contenteditable', ->
  require: 'ngModel',
  link: (scope, elmt, attrs, ctrl) ->
    old_render = ctrl.$render # save for later
    view_to_model = ->
      scope.$apply ->
        ctrl.$setViewValue elmt.html()
        null
      
    # view -> model
    elmt.bind 'blur', view_to_model
    elmt.bind 'input', view_to_model
    elmt.bind 'change', view_to_model

    # model -> view
    ctrl.$render = ->
      old_render() if old_render != null # old_render? leads to linted js
      elmt.html ctrl.$viewValue
      # move cursor to the end
      el = elmt.get(0)
      range = document.createRange()
      sel = window.getSelection()
      if el.childNodes.length > 0
        el2 = el.childNodes[el.childNodes.length - 1]
        range.setStartAfter(el2)
      else
        range.setStartAfter(el)
      range.collapse(true)
      sel.removeAllRanges()
      sel.addRange(range)
      null

    null
)
.filter('typeaheadHighlight', ->
  # don't highlight anything!
  (matchItem, query) -> matchItem
)
.filter('ignoreImg', ->
  # when matching query against the items, ignore all img tags
  (items, query) ->
    item for item in items \
    when noImg(item).match(new RegExp(escapeRegexp(noImg(query)), 'gi'))
)

{'fraction_non_alphanumeric': 0.14457831325301204, 'fraction_numerical': 0.0024805102763997165, 'mean_word_length': 2.915395284327323, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 34, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5697012', 'n_tokens_mistral': 930, 'n_tokens_neox': 905, 'n_words': 253}