From a9a688832a9615f934d65cfb39cec6a8fad6051d Mon Sep 17 00:00:00 2001 From: chris <kokosias@yahoo.gr> Date: Thu, 22 Oct 2020 23:03:59 +0300 Subject: [PATCH] replace all --- .../ExandedFindAndReplaceComponent.js | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/wax-prosemirror-components/src/components/findAndReplace/ExandedFindAndReplaceComponent.js b/wax-prosemirror-components/src/components/findAndReplace/ExandedFindAndReplaceComponent.js index 2d8f86d46..c42edf520 100644 --- a/wax-prosemirror-components/src/components/findAndReplace/ExandedFindAndReplaceComponent.js +++ b/wax-prosemirror-components/src/components/findAndReplace/ExandedFindAndReplaceComponent.js @@ -6,7 +6,7 @@ import React, { useCallback, useEffect, } from 'react'; -import { each, debounce } from 'lodash'; +import { each, eachRight, debounce } from 'lodash'; import { WaxContext } from 'wax-prosemirror-core'; import styled from 'styled-components'; import { grid, th } from '@pubsweet/ui-toolkit'; @@ -138,6 +138,7 @@ const ExandedFindAndReplaceComponent = ({ close, nonExpandedText }) => { const searchRef = useRef(null); const replaceRef = useRef(null); const [searchValue, setSearchValue] = useState(nonExpandedText); + const [replaceValue, setReplaceValue] = useState(''); const [counterText, setCounterText] = useState('0 of 0'); const findAndReplacePlugin = app.PmPlugins.get('findAndReplacePlugin'); @@ -175,13 +176,27 @@ const ExandedFindAndReplaceComponent = ({ close, nonExpandedText }) => { } }; - const onChangeReplaceInput = () => {}; + const onChangeReplaceInput = () => { + setReplaceValue(replaceRef.current.value); + }; const replace = () => { // const { from, to } = results[0]; // dispatch(state.tr.insertText(replace, from, to)); }; - const replaceAll = () => {}; + const replaceAll = () => { + each(view, (singleView, viewId) => { + const results = helpers.findMatches(singleView.state.doc, searchValue); + const { + state: { tr }, + } = singleView; + + eachRight(results, result => { + tr.insertText(replaceValue, result.from, result.to); + }); + singleView.dispatch(tr); + }); + }; const closeFind = () => { findAndReplacePlugin.props.setSearchText(''); -- GitLab