Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Extension } from "@tiptap/core";
import { Plugin, PluginKey } from "@tiptap/pm/state";
import { Extension, combineTransactionSteps, getChangedRanges, findChildrenInRange } from "@tiptap/core";
import { Plugin, PluginKey, Transaction } from "@tiptap/pm/state";

const RTL = "\u0591-\u07FF\uFB1D-\uFDFD\uFE70-\uFEFC";
const LTR =
Expand Down Expand Up @@ -40,11 +40,16 @@ function TextDirectionPlugin({ types }: { types: string[] }) {
}

let modified = false;
const tr = newState.tr;
const { tr } = newState;
const transform = combineTransactionSteps(oldState.doc, transactions as Transaction[]);
const changes = getChangedRanges(transform);

tr.setMeta("addToHistory", false);

newState.doc.descendants((node, pos) => {
if (types.includes(node.type.name)) {
changes.forEach(({ newRange }) => {
const nodes = findChildrenInRange(newState.doc, newRange, node => types.includes(node.type.name))

nodes.forEach(({ node, pos }) => {
if (node.attrs.dir !== null && node.textContent.length > 0) {
return;
}
Expand All @@ -55,7 +60,7 @@ function TextDirectionPlugin({ types }: { types: string[] }) {
tr.addStoredMark(mark);
}
modified = true;
}
});
});

return modified ? tr : null;
Expand Down