Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

42 строки
1.8 KiB

  1. /**
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. *
  7. * @format
  8. * @flow strict-local
  9. * @emails oncall+draft_js
  10. */
  11. 'use strict';
  12. const TokenizeUtil = require("fbjs/lib/TokenizeUtil");
  13. const punctuation = TokenizeUtil.getPunctuation(); // The apostrophe and curly single quotes behave in a curious way: when
  14. // surrounded on both sides by word characters, they behave as word chars; when
  15. // either neighbor is punctuation or an end of the string, they behave as
  16. // punctuation.
  17. const CHAMELEON_CHARS = "['\u2018\u2019]"; // Remove the underscore, which should count as part of the removable word. The
  18. // "chameleon chars" also count as punctuation in this regex.
  19. const WHITESPACE_AND_PUNCTUATION = '\\s|(?![_])' + punctuation;
  20. const DELETE_STRING = '^' + '(?:' + WHITESPACE_AND_PUNCTUATION + ')*' + '(?:' + CHAMELEON_CHARS + '|(?!' + WHITESPACE_AND_PUNCTUATION + ').)*' + '(?:(?!' + WHITESPACE_AND_PUNCTUATION + ').)';
  21. const DELETE_REGEX = new RegExp(DELETE_STRING);
  22. const BACKSPACE_STRING = '(?:(?!' + WHITESPACE_AND_PUNCTUATION + ').)' + '(?:' + CHAMELEON_CHARS + '|(?!' + WHITESPACE_AND_PUNCTUATION + ').)*' + '(?:' + WHITESPACE_AND_PUNCTUATION + ')*' + '$';
  23. const BACKSPACE_REGEX = new RegExp(BACKSPACE_STRING);
  24. function getRemovableWord(text: string, isBackward: boolean): string {
  25. const matches = isBackward ? BACKSPACE_REGEX.exec(text) : DELETE_REGEX.exec(text);
  26. return matches ? matches[0] : text;
  27. }
  28. const DraftRemovableWord = {
  29. getBackward: function (text: string): string {
  30. return getRemovableWord(text, true);
  31. },
  32. getForward: function (text: string): string {
  33. return getRemovableWord(text, false);
  34. }
  35. };
  36. module.exports = DraftRemovableWord;