33 * SPDX-License-Identifier: MIT
44 */
55
6- import { Extension } from '@tiptap/core'
7- import { Plugin , PluginKey } from '@tiptap/pm/state'
6+ import {
7+ Extension ,
8+ combineTransactionSteps ,
9+ findChildrenInRange ,
10+ getChangedRanges ,
11+ } from '@tiptap/core'
12+ import { Plugin , PluginKey , Transaction } from '@tiptap/pm/state'
813
914const RTL = '\u0591-\u07FF\uFB1D-\uFDFD\uFE70-\uFEFC'
1015const LTR = 'A-Za-z\u00C0-\u00D6\u00D8-\u00F6'
@@ -53,11 +58,21 @@ function TextDirectionPlugin({ types }: { types: string[] }) {
5358 }
5459
5560 let modified = false
56- const tr = newState . tr
61+ const { tr } = newState
62+ const transform = combineTransactionSteps (
63+ oldState . doc ,
64+ transactions as Transaction [ ] ,
65+ )
66+ const changes = getChangedRanges ( transform )
67+
5768 tr . setMeta ( 'addToHistory' , false )
5869
59- newState . doc . descendants ( ( node , pos ) => {
60- if ( types . includes ( node . type . name ) ) {
70+ changes . forEach ( ( { newRange } ) => {
71+ const nodes = findChildrenInRange ( newState . doc , newRange , ( node ) =>
72+ types . includes ( node . type . name ) ,
73+ )
74+
75+ nodes . forEach ( ( { node, pos } ) => {
6176 if ( node . attrs . dir !== null && node . textContent . length > 0 ) {
6277 return
6378 }
@@ -73,7 +88,7 @@ function TextDirectionPlugin({ types }: { types: string[] }) {
7388 tr . addStoredMark ( mark )
7489 }
7590 modified = true
76- }
91+ } )
7792 } )
7893
7994 return modified ? tr : null
0 commit comments