How to quickly replace same nodes throughout the doc #5149
-
Hi everyone. The nature of my project is the transcription files editor (where there are utterances, each of them contain a single speaker label and many paragraphs). I need to create a functionality to change a speaker name, and it should be reflected throughout the doc. Here is how I approached this so far: const changeAllSpeakerNames = (oldName, newName) => {
const { editor } = props
let nodesToChange = []
const $utterances = editor.$nodes('utterance')
const speakerLabels = $utterances.map((utterance) => {
return { node: utterance.node.content.content[0], pos: utterance.pos + 1 }
})
speakerLabels.forEach(({ node, pos }) => {
if (node.attrs.speaker === oldName) {
nodesToChange.push({ pos, node })
}
})
const transactionsChain = nodesToChange.reduce((chain, { pos, node }) => {
return chain.insertContentAt(
{ from: pos, to: pos + node.nodeSize },
{
type: 'speaker-label',
attrs: {
speaker: newName,
speakerId: node.attrs.speakerId,
start: node.attrs.start,
end: node.attrs.end,
},
content: [{ type: 'text', text: '1' }], // 1 is inserted here as I'm using custom React component renderer and display the name via node attribute
}
)
}, editor.chain())
transactionsChain
.setMeta('ignoreTransaction', true)
.setMeta('renameAllSpeakers', true)
.run()
} The reason I'm doing this weird thing with transaction chain is because I want to update them in one batch (single transaction), so user can revert it with The problem is, as my documents sometimes can be large (transcription of 1.5 hour interview) - it takes around 5 seconds to complete this transaction. And it's not finding the nodes to change itself (even if I set this as hardcoded variable it still takes long), it's applying the transaction. Had anyone worked on a similar case as me? Do you have any ideas on how I can improve this? What comes to my mind, is don't do it as a batch transaction but instead as a series of transactions, but don't add those transactions to the history (and manually spawn some custom transaction to indicate speakers rename to let user revert it). This way it'll be fine if it takes 5 seconds to change all doc, at least the user won't be blocked and will see the progress instantly. 🙏 Thanks, any help would be appreciated |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
If anyone comes across: the problem was with the heavy usage of React Node Views. After migrating it to Javascript Node Views - it had drastically improved the performance and solved the problem |
Beta Was this translation helpful? Give feedback.
If anyone comes across: the problem was with the heavy usage of React Node Views. After migrating it to Javascript Node Views - it had drastically improved the performance and solved the problem