<commit_before>import { Composer } from "../views/composer";
import { Constants } from "../constants";

var autoList = function(editor, composer, range, textContent, e) {
  var selectedNode = range.startContainer;
  var blockElement = composer.parentElement(selectedNode, {
    nodeName: ["P", "DIV", "LI"]
  });
  if (blockElement && blockElement.nodeName != "LI") {
    if (composer.selection.caretIsAtStartOfNode(blockElement, range, selectedNode)) {
      var orderedList = hasPrefix(textContent, "1.");

      var deleteRange = document.createRange();
      deleteRange.setStart(range.startContainer, 0);
      deleteRange.setEnd(range.startContainer, orderedList ? 2 : 1);
      deleteRange.deleteContents();

      var selection = composer.selection.getSelection().nativeSelection;
      selection.removeAllRanges();
      selection.addRange(range);

      var command = orderedList ? "insertOrderedList" : "insertUnorderedList";
      composer.commands.exec(command);
      return true;
    }
  }
  return false;
};

var hasPrefix = function(string, prefix) {
  return string.indexOf(prefix) == 0
};

var isListPrefix = function(textContent, exact) {
  if (exact) {
    return (textContent == "1." || textContent == "•" || textContent == "*" || textContent == "-");
  } else {
    return (
      hasPrefix(textContent, "1.") ||
      hasPrefix(textContent, "•") ||
      hasPrefix(textContent, "*") ||
      hasPrefix(textContent, "-")
    );
  }
};

Composer.RegisterTextSubstitution(function(textContent) {
  return isListPrefix(textContent, true);
}, function(editor, composer, range, textContent, e) {
  if (autoList(editor, composer, range, textContent, e)) {
    if (e.type === "keydown" && e.keyCode == Constants.SPACE_KEY) {
      e.preventDefault();
    }
  }
}, {
  word: true,
  sentence: false
});

Composer.RegisterTextSubstitution(function(textContent) {
  return isListPrefix(textContent);
}, autoList, {
  word: false,
  sentence: true
});
<commit_msg>Revert "Merge pull request #61 from metalabdesign/remove_restore_autolist"<commit_after>import { Composer } from "../views/composer";
import { Constants } from "../constants";

var autoList = function(editor, composer, range, textContent, e) {
  var selectedNode = range.startContainer;
  var blockElement = composer.parentElement(selectedNode, {
    nodeName: ["P", "DIV", "LI"]
  });
  if (blockElement && blockElement.nodeName != "LI") {
    if (composer.selection.caretIsAtStartOfNode(blockElement, range, selectedNode)) {
      composer.selection.executeAndRestore(function() {
        var orderedList = hasPrefix(textContent, "1.");

        var deleteRange = document.createRange();
        deleteRange.setStart(range.startContainer, 0);
        deleteRange.setEnd(range.startContainer, orderedList ? 2 : 1);
        deleteRange.deleteContents();

        var selection = composer.selection.getSelection().nativeSelection;
        selection.removeAllRanges();
        selection.addRange(range);

        var command = orderedList ? "insertOrderedList" : "insertUnorderedList";
        composer.commands.exec(command);
      });
      return true;
    }
  }
  return false;
};

var hasPrefix = function(string, prefix) {
  return string.indexOf(prefix) == 0
};

var isListPrefix = function(textContent, exact) {
  if (exact) {
    return (textContent == "1." || textContent == "•" || textContent == "*" || textContent == "-");
  } else {
    return (
      hasPrefix(textContent, "1.") ||
      hasPrefix(textContent, "•") ||
      hasPrefix(textContent, "*") ||
      hasPrefix(textContent, "-")
    );
  }
};

Composer.RegisterTextSubstitution(function(textContent) {
  return isListPrefix(textContent, true);
}, function(editor, composer, range, textContent, e) {
  if (autoList(editor, composer, range, textContent, e)) {
    if (e.type === "keydown" && e.keyCode == Constants.SPACE_KEY) {
      e.preventDefault();
    }
  }
}, {
  word: true,
  sentence: false
});

Composer.RegisterTextSubstitution(function(textContent) {
  return isListPrefix(textContent);
}, autoList, {
  word: false,
  sentence: true
});

{'fraction_non_alphanumeric': 0.14759697487191997, 'fraction_numerical': 0.003903391070992925, 'mean_word_length': 3.8406139315230226, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '3861713', 'n_tokens_mistral': 1240, 'n_tokens_neox': 1225, 'n_words': 253}