mesdan
Version:
Latinî mesdan ile Osmanî metin yazmak için hazırlanmış bir alettir.
101 lines (100 loc) • 4.88 kB
JavaScript
import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext';
import { $getSelection, $isRangeSelection, $isTextNode, COMMAND_PRIORITY_HIGH, KEY_DOWN_COMMAND } from 'lexical';
import { useEffect } from 'react';
import { harfLatinidenOsmaniye } from '../nuve/lexical/harf-latiniden-osmaniye';
import { silOsmaniHarfiBaşra, silOsmaniHarfiSonra } from '../nuve/lexical/sil-harf-metingerde';
export const OsmaniMesdanEki = () => {
const [metinger] = useLexicalComposerContext();
useEffect(() => {
const removeKeyDownListener = metinger.registerCommand(KEY_DOWN_COMMAND, vaqa => {
const { key: ca, shiftKey, altKey, metaKey, ctrlKey } = vaqa;
if (ctrlKey || metaKey)
return false; // ker ve fer çalışsın diye
// ArrowDown, ArrowUp, ArrowRight, ArrowLeft
// Enter, Delete, Home, PageUp, PageDown, End, Pause, Escape, AltGraph
// F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12
if (ca === 'Enter') {
return false;
}
if (ca === 'Backspace') {
silOsmaniHarfiBaşra(metinger, vaqa);
return true;
}
if (ca === 'Delete') {
silOsmaniHarfiSonra(metinger, vaqa);
return true;
}
if (ca === 'ArrowLeft' || ca === 'ArrowRight') {
vaqa.preventDefault();
vaqa.stopPropagation();
metinger.update(() => {
const selection = $getSelection();
if (!$isRangeSelection(selection) || !selection.isCollapsed())
return;
const backward = ca === 'ArrowRight';
const granularity = vaqa.ctrlKey ? 'word' : 'character';
selection.modify('move', backward, granularity);
});
return true;
}
if (ca === 'Home' || ca === 'End') {
vaqa.preventDefault();
vaqa.stopPropagation();
metinger.update(() => {
const selection = $getSelection();
if (!$isRangeSelection(selection) || !selection.isCollapsed())
return;
const backward = ca === 'Home'; // Home → move to start of line
selection.modify('move', backward, 'lineboundary');
});
return true;
}
// Up and Down: let browser handle them
if (ca === 'ArrowUp' || ca === 'ArrowDown') {
return false;
}
// Bütün basılabilir karakterleri (harfler, rakamlar, noktalama işaretleri) yakala
if (ca.length === 1) {
vaqa.preventDefault();
vaqa.stopPropagation();
metinger.update(() => {
const selection = $getSelection();
if (!$isRangeSelection(selection))
return;
const anchor = selection.anchor;
const offset = anchor.offset;
const node = anchor.getNode();
const marMetin = node.getTextContent().slice(0, offset);
const marHarf = marMetin.slice(-1);
if (marHarf === 'ا' && ca.toLowerCase() === 'a') {
selection.deleteCharacter(true);
selection.insertText('آ'); // Medli Elif
}
else {
const osmanliHarf = harfLatinidenOsmaniye(ca, marHarf, shiftKey, altKey);
// Eğer yazdırılacak harf sadece harekelerden/işaretlerden ibaretse
const isaretRegex = /^[\u064B-\u0656\u0670]+$/;
if ($isTextNode(node) && isaretRegex.test(osmanliHarf)) {
// Öncesinde de hareke/işaret var mı baq
const match = marMetin.match(/[\u064B-\u0656\u0670]+$/);
if (match) {
const matchLength = match[0].length;
// Önceki harekeleri seç ve sil
selection.anchor.set(node.getKey(), offset - matchLength, 'text');
selection.focus.set(node.getKey(), offset, 'text');
selection.removeText();
}
}
selection.insertText(osmanliHarf);
}
});
return true;
}
return false;
}, COMMAND_PRIORITY_HIGH);
return () => {
removeKeyDownListener();
};
}, [metinger]);
return null;
};