|
| 1 | +/** |
| 2 | + * SPDX-FileCopyrightText: 2023 Amir Hossein Hashemi |
| 3 | + * SPDX-License-Identifier: MIT |
| 4 | + */ |
| 5 | + |
| 6 | +import { Extension } from '@tiptap/core' |
| 7 | +import { Plugin, PluginKey } from '@tiptap/pm/state' |
| 8 | + |
| 9 | +const RTL = '\u0591-\u07FF\uFB1D-\uFDFD\uFE70-\uFEFC' |
| 10 | +const LTR = 'A-Za-z\u00C0-\u00D6\u00D8-\u00F6' |
| 11 | + + '\u00F8-\u02B8\u0300-\u0590\u0800-\u1FFF\u200E\u2C00-\uFB1C' |
| 12 | + + '\uFE00-\uFE6F\uFEFD-\uFFFF' |
| 13 | + |
| 14 | +/* eslint-disable no-misleading-character-class */ |
| 15 | +const RTL_REGEX = new RegExp('^[^' + LTR + ']*[' + RTL + ']') |
| 16 | +const LTR_REGEX = new RegExp('^[^' + RTL + ']*[' + LTR + ']') |
| 17 | + |
| 18 | +/** |
| 19 | + * @param text Text string |
| 20 | + * |
| 21 | + *Source: https://github.com/facebook/lexical/blob/429e3eb5b5a244026fa4776650aabe3c8e17536b/packages/lexical/src/LexicalUtils.ts#L163 |
| 22 | + */ |
| 23 | +export function getTextDirection(text: string): 'ltr' | 'rtl' | null { |
| 24 | + if (text.length === 0) { |
| 25 | + return null |
| 26 | + } |
| 27 | + if (RTL_REGEX.test(text)) { |
| 28 | + return 'rtl' |
| 29 | + } |
| 30 | + if (LTR_REGEX.test(text)) { |
| 31 | + return 'ltr' |
| 32 | + } |
| 33 | + return null |
| 34 | +} |
| 35 | + |
| 36 | +const validDirections = ['ltr', 'rtl', 'auto'] as const |
| 37 | + |
| 38 | +type Direction = (typeof validDirections)[number] |
| 39 | + |
| 40 | +/** |
| 41 | + * @param object Property object |
| 42 | + * @param object.types List of node types to consider |
| 43 | + */ |
| 44 | +function TextDirectionPlugin({ types }: { types: string[] }) { |
| 45 | + return new Plugin({ |
| 46 | + key: new PluginKey('textDirection'), |
| 47 | + appendTransaction: (transactions, oldState, newState) => { |
| 48 | + const docChanges = transactions.some( |
| 49 | + (transaction) => transaction.docChanged, |
| 50 | + ) |
| 51 | + if (!docChanges) { |
| 52 | + return |
| 53 | + } |
| 54 | + |
| 55 | + let modified = false |
| 56 | + const tr = newState.tr |
| 57 | + tr.setMeta('addToHistory', false) |
| 58 | + |
| 59 | + newState.doc.descendants((node, pos) => { |
| 60 | + if (types.includes(node.type.name)) { |
| 61 | + if (node.attrs.dir !== null && node.textContent.length > 0) { |
| 62 | + return |
| 63 | + } |
| 64 | + const newTextDirection = getTextDirection(node.textContent) |
| 65 | + if (node.attrs.dir === newTextDirection) { |
| 66 | + return |
| 67 | + } |
| 68 | + |
| 69 | + const marks = tr.storedMarks || [] |
| 70 | + tr.setNodeAttribute(pos, 'dir', newTextDirection) |
| 71 | + // `tr.setNodeAttribute` resets the stored marks so we'll restore them |
| 72 | + for (const mark of marks) { |
| 73 | + tr.addStoredMark(mark) |
| 74 | + } |
| 75 | + modified = true |
| 76 | + } |
| 77 | + }) |
| 78 | + |
| 79 | + return modified ? tr : null |
| 80 | + }, |
| 81 | + }) |
| 82 | +} |
| 83 | + |
| 84 | +declare module '@tiptap/core' { |
| 85 | + interface Commands<ReturnType> { |
| 86 | + textDirection: { |
| 87 | + /** |
| 88 | + * Set the text direction attribute |
| 89 | + */ |
| 90 | + setTextDirection: (direction: Direction) => ReturnType |
| 91 | + /** |
| 92 | + * Unset the text direction attribute |
| 93 | + */ |
| 94 | + unsetTextDirection: () => ReturnType |
| 95 | + } |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +export interface TextDirectionOptions { |
| 100 | + types: string[] |
| 101 | + defaultDirection: Direction | null |
| 102 | +} |
| 103 | + |
| 104 | +export const TextDirection = Extension.create<TextDirectionOptions>({ |
| 105 | + name: 'textDirection', |
| 106 | + |
| 107 | + addOptions() { |
| 108 | + return { |
| 109 | + types: [], |
| 110 | + defaultDirection: null, |
| 111 | + } |
| 112 | + }, |
| 113 | + |
| 114 | + addGlobalAttributes() { |
| 115 | + return [ |
| 116 | + { |
| 117 | + types: this.options.types, |
| 118 | + attributes: { |
| 119 | + dir: { |
| 120 | + default: null, |
| 121 | + parseHTML: (element) => |
| 122 | + element.dir || this.options.defaultDirection, |
| 123 | + renderHTML: (attributes) => { |
| 124 | + if (attributes.dir === this.options.defaultDirection) { |
| 125 | + return {} |
| 126 | + } |
| 127 | + return { dir: attributes.dir } |
| 128 | + }, |
| 129 | + }, |
| 130 | + }, |
| 131 | + }, |
| 132 | + ] |
| 133 | + }, |
| 134 | + |
| 135 | + addCommands() { |
| 136 | + return { |
| 137 | + setTextDirection: |
| 138 | + (direction: Direction) => |
| 139 | + ({ commands }) => { |
| 140 | + if (!validDirections.includes(direction)) { |
| 141 | + return false |
| 142 | + } |
| 143 | + |
| 144 | + return this.options.types.every((type) => |
| 145 | + commands.updateAttributes(type, { dir: direction }), |
| 146 | + ) |
| 147 | + }, |
| 148 | + |
| 149 | + unsetTextDirection: |
| 150 | + () => |
| 151 | + ({ commands }) => { |
| 152 | + return this.options.types.every((type) => |
| 153 | + commands.resetAttributes(type, 'dir'), |
| 154 | + ) |
| 155 | + }, |
| 156 | + } |
| 157 | + }, |
| 158 | + |
| 159 | + addKeyboardShortcuts() { |
| 160 | + return { |
| 161 | + 'Mod-Alt-l': () => this.editor.commands.setTextDirection('ltr'), |
| 162 | + 'Mod-Alt-r': () => this.editor.commands.setTextDirection('rtl'), |
| 163 | + } |
| 164 | + }, |
| 165 | + |
| 166 | + addProseMirrorPlugins() { |
| 167 | + return [ |
| 168 | + TextDirectionPlugin({ |
| 169 | + types: this.options.types, |
| 170 | + }), |
| 171 | + ] |
| 172 | + }, |
| 173 | +}) |
| 174 | + |
| 175 | +export default TextDirection |
0 commit comments