I’m trying to replace combinations of letters with different letters (a+b → letter from other language).
Rule to translate on every transaction.
new InputRule(/(.*)/i, (state, match, start, end) => {
if (!match[0]) {
return null;
}
let tr = state.tr.setMeta('NewTranslateWord', {word: match[0], start, end})
return tr;
})
Plugin that replaces text.
import { Plugin, PluginKey } from 'prosemirror-state';
// replaces combinations of letters with letters in other language.
import { Translate } from './Utils';
const Languages = () => {
return new Plugin({
key: new PluginKey( 'Translate'),
state: {
init() {
return { newWord : {} };
},
apply(tr, state) {
let newWordObject = tr.getMeta('NewTranslateWord')
console.log(newWordObject)
if(!!newWordObject){
let newState = newWordObject;
return newState
}
return {}
},
},
view: (view) => {
return {
update: (view, prevState) => {
let newWordObject = view.state.Translate$;
console.log(view,newWordObject);
if(!!newWordObject.word){
const {start, end} = newWordObject;
let translatedString = Translate(newWordObject.word);
view.dispatch(view.state.tr.insertText(translatedString, start, end));
view.state.tr.setMeta('NewTranslateWord', {})
return true;
}
}
}
}
});
};
The problem with this plugin is, we are loosing marks (like bold, strike etc…) after every transaction. How do we keep the marks but just keep replacing the letters.