Guest User

Untitled

a guest
Oct 18th, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. if(typeof String.prototype.trimLeft !== 'function') {
  2. String.prototype.trimLeft = function() {
  3. return this.replace(/^s+/,"");
  4. }
  5. }
  6.  
  7. if(typeof String.prototype.trimRight !== 'function') {
  8. String.prototype.trimRight = function() {
  9. return this.replace(/s+$/,"");
  10. }
  11. }
  12.  
  13. trimContent = () => {
  14. const editorState = this.state.editorState;
  15. let currentContent = this.state.editorState.getCurrentContent();
  16. const firstBlock = currentContent.getBlockMap().first();
  17. const lastBlock = currentContent.getBlockMap().last();
  18. const firstBlockKey = firstBlock.getKey();
  19. const lastBlockKey = lastBlock.getKey();
  20.  
  21. const textStart = firstBlock.getText()
  22. const trimmedTextStart = textStart.trimLeft();
  23. const lengthOfTrimmedCharsStart = textStart.length - trimmedTextStart.length;
  24.  
  25. let newSelection = new SelectionState({
  26. anchorKey: firstBlockKey,
  27. anchorOffset: 0,
  28. focusKey: firstBlockKey,
  29. focusOffset: lengthOfTrimmedCharsStart
  30. });
  31.  
  32. currentContent = Modifier.replaceText(
  33. currentContent,
  34. newSelection,
  35. '',
  36. )
  37.  
  38. let newEditorState = EditorState.push(
  39. editorState,
  40. currentContent,
  41. )
  42.  
  43. const textEnd = lastBlock.getText()
  44. const trimmedTextEnd = textEnd.trimRight();
  45. const lengthOfTrimmedCharsEnd = textEnd.length - trimmedTextEnd.length
  46.  
  47. newSelection = new SelectionState({
  48. anchorKey: lastBlockKey,
  49. anchorOffset: trimmedTextEnd.length,
  50. focusKey: lastBlockKey,
  51. focusOffset: textEnd.length
  52. });
  53.  
  54. currentContent = Modifier.replaceText(
  55. currentContent,
  56. newSelection,
  57. '',
  58. )
  59.  
  60. newEditorState = EditorState.push(
  61. editorState,
  62. currentContent,
  63. )
  64.  
  65. this._handleChange(newEditorState);
  66. }
Add Comment
Please, Sign In to add comment