Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if(typeof String.prototype.trimLeft !== 'function') {
- String.prototype.trimLeft = function() {
- return this.replace(/^s+/,"");
- }
- }
- if(typeof String.prototype.trimRight !== 'function') {
- String.prototype.trimRight = function() {
- return this.replace(/s+$/,"");
- }
- }
- trimContent = () => {
- const editorState = this.state.editorState;
- let currentContent = this.state.editorState.getCurrentContent();
- const firstBlock = currentContent.getBlockMap().first();
- const lastBlock = currentContent.getBlockMap().last();
- const firstBlockKey = firstBlock.getKey();
- const lastBlockKey = lastBlock.getKey();
- const textStart = firstBlock.getText()
- const trimmedTextStart = textStart.trimLeft();
- const lengthOfTrimmedCharsStart = textStart.length - trimmedTextStart.length;
- let newSelection = new SelectionState({
- anchorKey: firstBlockKey,
- anchorOffset: 0,
- focusKey: firstBlockKey,
- focusOffset: lengthOfTrimmedCharsStart
- });
- currentContent = Modifier.replaceText(
- currentContent,
- newSelection,
- '',
- )
- let newEditorState = EditorState.push(
- editorState,
- currentContent,
- )
- const textEnd = lastBlock.getText()
- const trimmedTextEnd = textEnd.trimRight();
- const lengthOfTrimmedCharsEnd = textEnd.length - trimmedTextEnd.length
- newSelection = new SelectionState({
- anchorKey: lastBlockKey,
- anchorOffset: trimmedTextEnd.length,
- focusKey: lastBlockKey,
- focusOffset: textEnd.length
- });
- currentContent = Modifier.replaceText(
- currentContent,
- newSelection,
- '',
- )
- newEditorState = EditorState.push(
- editorState,
- currentContent,
- )
- this._handleChange(newEditorState);
- }
Add Comment
Please, Sign In to add comment