664 lines
16 KiB
JavaScript
664 lines
16 KiB
JavaScript
var alaem = ' .،؛,:';
|
||
var letterYeh = "یي";
|
||
var pishVand = ' نمی '
|
||
var letterNoChasban = "ءاإأآؤدذرزو";
|
||
var pasvands = " ها هایی های هایش هایم هایشان هایتان هایمان ای اید ایم اند ام";
|
||
|
||
var pairedAlaemEnd = '}>])﴾»\"';
|
||
var pairedAlaemBeg = '{<[(﴿«\"';
|
||
var sanadVaseth = ' عن عنه ';
|
||
var sanadEnd = ' سالت سألت قلت قال یقول قالوا قالا قلنا ';
|
||
var wlast = '';
|
||
var indexlast = -1;
|
||
|
||
|
||
var onCharacterKeyPress = function isCharacterKeyPress(evt) {
|
||
if (typeof evt.which == "undefined") {
|
||
// This is IE, which only fires keypress events for printable keys
|
||
return true;
|
||
} else if (typeof evt.which == "number" && evt.which > 0) {
|
||
// In other browsers except old versions of WebKit, evt.which is
|
||
// only greater than zero if the keypress is a printable key.
|
||
// We need to filter out backspace and ctrl/alt/meta key combinations
|
||
return !evt.ctrlKey && !evt.metaKey && !evt.altKey && evt.which != 8;
|
||
}
|
||
return false;
|
||
}
|
||
|
||
function isErab(c){
|
||
return c.match(/[\u064B-\u0652]/g);
|
||
}
|
||
function removeErab(text){
|
||
text = text.replace(/[\u064B-\u0652]/g, '');
|
||
return text;
|
||
}
|
||
|
||
function isPairedChar(c){
|
||
var index = pairedAlaemBeg.indexOf(c);
|
||
if( index === -1 )
|
||
return '';
|
||
return pairedAlaemEnd[index];
|
||
}
|
||
|
||
function isAlaem(c){
|
||
//var c = String.fromCharCode(keyCode);
|
||
return alaem.indexOf(c) !== -1;
|
||
}
|
||
|
||
function getNextWord(text, index){
|
||
var w = '';
|
||
if(index < 0 )
|
||
index = 0;
|
||
for(var i=index; i < text.length; i++ )
|
||
{
|
||
var c = text[i];
|
||
if(alaem.indexOf(text[i]) !== -1)
|
||
return w;
|
||
w += text[i];
|
||
}
|
||
return w;
|
||
}
|
||
|
||
function getLastWord(text, index){
|
||
var w = '';
|
||
if(index>=text.length )
|
||
index = text.length-1;
|
||
for(var i=index; i >= 0; i-- )
|
||
{
|
||
var c = text[i];
|
||
if(isAlaem(text[i]))
|
||
return w;
|
||
w = text[i] + w;
|
||
}
|
||
return w;
|
||
}
|
||
|
||
function isHalfSpaceNeed(range){
|
||
|
||
var index = -1;
|
||
var w1 = '', w2 = '';
|
||
var ret = [index, w1, w2 ];
|
||
var text = range.endContainer.data;
|
||
var i1 = range.endOffset - ' '.length;
|
||
//var i1 = text.length-1 - ' '.length;
|
||
var w1 = getLastWord(text, i1);
|
||
var w2 = '';
|
||
if(w1 != '')
|
||
{
|
||
wlast = w1;
|
||
indexlast = i1 + 1 + ' '.length;
|
||
|
||
w2 = getLastWord(text, i1-w1.length-1);
|
||
}
|
||
if(w2 === '')
|
||
return ret;
|
||
var correcting = false;
|
||
var c2Last = w2[w2.length-1];
|
||
if(letterYeh.indexOf(c2Last) !== -1){
|
||
if( w2.length < 3 || pishVand.indexOf(' ' + w2) !== -1)
|
||
correcting = true;
|
||
}
|
||
if( !correcting && letterNoChasban.indexOf(c2Last) === -1)
|
||
{
|
||
if(pasvands.indexOf(' ' + w1) !== -1)
|
||
{
|
||
correcting = true;
|
||
}
|
||
}
|
||
if(correcting){ // می شود ...
|
||
ret[0] = range.endOffset-w1.length-1; // space position
|
||
ret[1] = w1; // شود
|
||
ret[2] = w2; //می
|
||
|
||
return ret;
|
||
}
|
||
else
|
||
return ret;
|
||
}
|
||
|
||
function isTextElement(range)
|
||
{
|
||
return range.endContainer != null && range.endContainer.nodeType === 3;
|
||
}
|
||
|
||
function unSelect(editor){
|
||
if(!editor.selection.isCollapsed()){
|
||
editor.selection.collapse(false);
|
||
editor.focus();
|
||
}
|
||
}
|
||
|
||
function selectParag(editor){
|
||
var range = editor.selection.getRng().cloneRange();
|
||
var node = range.endContainer;
|
||
while( !isValidLineTag(node) )
|
||
{
|
||
if( node.parentNode == null )
|
||
break;
|
||
node = node.parentNode;
|
||
}
|
||
if(node === null)
|
||
return false;
|
||
|
||
range.selectNodeContents(node);
|
||
editor.selection.setRng(range);
|
||
editor.focus();
|
||
return true;
|
||
}
|
||
|
||
function selectWord(editor){
|
||
//var b = tinyMCE.Env.browser.isIE();
|
||
if (editor.selection.isCollapsed()) {
|
||
if (tinyMCE.Env.browser.isIE()) { // Internet Explorer
|
||
var range = editor.selection.getRng().cloneRange();
|
||
var text = range.endContainer.data;
|
||
if(text.length == 0)
|
||
return false;
|
||
|
||
var i1 = range.endOffset-1;
|
||
if( i1<0 ) i1 = 0;
|
||
if( i1 >= text.length ) i1 = text.length-1;
|
||
var c = text[i1];
|
||
var w = '';
|
||
while( i1 >= 0 && !isAlaem(text[i1])){
|
||
i1--;
|
||
}
|
||
if (i1 < 0) i1 = 0;
|
||
if(isAlaem(text[i1]))
|
||
i1++;
|
||
w = getNextWord(text, i1);
|
||
range.setStart(range.endContainer, i1);
|
||
range.setEnd(range.endContainer, i1+w.length);
|
||
|
||
editor.selection.setRng(range);
|
||
editor.focus();
|
||
return true;
|
||
}
|
||
}
|
||
return false;
|
||
}
|
||
|
||
|
||
|
||
function isLetter(keyCode){
|
||
if ( (keyCode < 65 ) ||
|
||
(keyCode > 90 && keyCode < 97 ) ||
|
||
(keyCode > 122 && keyCode < 161 ) ||
|
||
(keyCode > 1631 && keyCode < 1646 )
|
||
){
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
function getRange(editor){
|
||
var range = editor.selection.getRng().cloneRange();
|
||
if(editor.selection.isCollapsed()){
|
||
|
||
var node = range.endContainer;
|
||
while( !isValidLineTag(node) )
|
||
{
|
||
if( node.parentNode == null )
|
||
break;
|
||
node = node.parentNode;
|
||
}
|
||
if(node === null)
|
||
return;
|
||
|
||
range.selectNodeContents(node);
|
||
}
|
||
|
||
editor.selection.setRng(range);
|
||
return range;
|
||
}
|
||
|
||
function removeContentErab(editor){
|
||
|
||
var range = getRange(editor);
|
||
|
||
var text = editor.selection.getContent();
|
||
text = removeErab(text);
|
||
|
||
editor.undoManager.add();
|
||
//editor.undoManager.transact(function () {
|
||
editor.selection.setContent(text);
|
||
//});
|
||
range.collapse(false);
|
||
editor.focus();
|
||
return true;
|
||
}
|
||
|
||
|
||
function parseHadith(editor){
|
||
var range = getRange(editor);
|
||
var text = editor.selection.getContent();
|
||
if(text.indexOf('class="tsanad"') != -1 || text.indexOf('class="thadith"') != -1){
|
||
range.collapse(false);
|
||
editor.focus();
|
||
return false;
|
||
}
|
||
|
||
var text2 = '';
|
||
var sanadvalid = false;
|
||
var w = '', w2 = '';
|
||
for(var i=0; i < text.length; i++ )
|
||
{
|
||
var c = text[i];
|
||
if(isAlaem(c)){
|
||
w2 += c;
|
||
if(!sanadvalid && w != ''){
|
||
if(sanadEnd.indexOf(' ' + w) != -1){
|
||
text2 = '<span class=\"tsanad\">' + text2 + w2 + '</span>';
|
||
text2 += '<span class=\"thadith\">'
|
||
sanadvalid = true;
|
||
w2 = '';
|
||
w = '';
|
||
continue;
|
||
}
|
||
}
|
||
text2 += w2;
|
||
w2 = '';
|
||
w = '';
|
||
}
|
||
else {
|
||
w2 += c;
|
||
if( !isErab(c) )
|
||
w += c;
|
||
}
|
||
}
|
||
text2 += w2;
|
||
if(sanadvalid)
|
||
text2 += '</span>';
|
||
|
||
editor.undoManager.add();
|
||
//editor.undoManager.transact(function () {
|
||
editor.selection.setContent(text2 + ' ');
|
||
//});
|
||
range.collapse(false);
|
||
editor.focus();
|
||
return true;
|
||
}
|
||
|
||
|
||
function checkPairedChar(editor, e){
|
||
var range = editor.selection.getRng().cloneRange();
|
||
if( isTextElement(range)){
|
||
var c = isPairedChar(e.key);
|
||
if( c!== '' ){
|
||
if(!editor.selection.isCollapsed()){
|
||
editor.selection.setContent( e.key + editor.selection.getContent() + c);
|
||
editor.focus();
|
||
return true;
|
||
}
|
||
else{
|
||
editor.execCommand('mceInsertContent', false, e.key + c);
|
||
//editor.selection.setContent( e.key + c);
|
||
var range2 = editor.selection.getRng().cloneRange();
|
||
if(range2.endContainer.nodeType == 3)
|
||
range2.setEnd(range2.endContainer, range2.endOffset-1);
|
||
else if(range2.endContainer.nodeType==1 && range2.endContainer.lastChild && range2.endContainer.lastChild.nodeType == 3)
|
||
range2.setEnd(range2.endContainer.lastChild, range2.endContainer.lastChild.textContent.length-1);
|
||
else
|
||
return true;
|
||
|
||
editor.selection.setRng(range2);
|
||
editor.focus();
|
||
return true;
|
||
}
|
||
}
|
||
}
|
||
return false;
|
||
}
|
||
function setingTag2(editor, e){
|
||
if(e.keyCode === 35 || e.key === '#'){ //'#'
|
||
var range = editor.selection.getRng().cloneRange();
|
||
var text = range.endContainer.data;
|
||
var i1 = range.endOffset;
|
||
if(i1< text.length){
|
||
var w1 = getNextWord(text,i1)
|
||
if(w1.length > 0){
|
||
range.setStart(range.endContainer,i1-1);
|
||
range.setEnd(range.endContainer, i1+w1.length+1);
|
||
editor.selection.setRng(range);
|
||
|
||
var t = editor.selection.getContent();
|
||
if(t[0] === '#')
|
||
t = t.substring(1);
|
||
editor.selection.setContent('<span class=\"ttag\">'+t+'</span>');
|
||
editor.focus();
|
||
return true;
|
||
}
|
||
}
|
||
return false;
|
||
}
|
||
return false;
|
||
}
|
||
|
||
|
||
function setingTag(editor, e, range){
|
||
if(isLetter(e.keyCode) ){
|
||
var text = range.endContainer.data;
|
||
var i1 = range.endOffset-1;
|
||
if( i1 > 0 ){
|
||
if( text[i1-1] === '#' && text[i1-2] === ' ') {
|
||
if( !isTagging(range.endContainer.parentNode) ){
|
||
range.setStart(range.endContainer,i1-1);
|
||
range.setEnd(range.endContainer, i1+1);
|
||
editor.selection.setRng(range);
|
||
|
||
editor.selection.setContent('<span class=\"ttag\">'+editor.selection.getContent()+'</span>');
|
||
range.selectNodeContents(range.endContainer.nextSibling);
|
||
range.collapse(false);
|
||
|
||
editor.selection.setRng(range);
|
||
editor.focus();
|
||
return true;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
return false;
|
||
}
|
||
|
||
|
||
function endigTag(editor, e, range){
|
||
if(isTagging(range.endContainer.parentNode)){
|
||
var tag = range.endContainer.parentNode;
|
||
var t = tag.innerText;
|
||
if(t.length < 2)
|
||
return false;
|
||
if(t[0] === '#')
|
||
t = t.substring(1);
|
||
|
||
range.selectNodeContents(tag);
|
||
editor.selection.setRng(range);
|
||
|
||
editor.selection.setContent(t);
|
||
|
||
if(e.keyCode == 13)
|
||
tinyMCE.execCommand('removeformat');
|
||
else if(tag.nextSibling)
|
||
{
|
||
range.setStart(tag.nextSibling,0);
|
||
range.setEnd(tag.nextSibling,0);
|
||
editor.selection.setRng(range);
|
||
editor.selection.setContent( ' ' + editor.selection.getContent());
|
||
}
|
||
|
||
editor.focus();
|
||
return true;
|
||
}
|
||
return false;
|
||
}
|
||
|
||
function endingMarkDowns(editor, e){
|
||
var range = editor.selection.getRng().cloneRange();
|
||
if(e.keyCode === 13){
|
||
if(endigTag(editor, e, range))
|
||
return true;
|
||
}
|
||
return false;
|
||
}
|
||
|
||
|
||
function checkMarkDown(editor, e){
|
||
var range = editor.selection.getRng().cloneRange();
|
||
if( isTextElement(range)){
|
||
if( editor.selection.isCollapsed() ){
|
||
if(e.keyCode == 32 || e.keyCode == 160 ){
|
||
var text = range.endContainer.data;
|
||
var i1 = range.endOffset-1 - ' '.length;
|
||
//var i1 = text.length-1 - ' '.length;
|
||
var w1 = '';
|
||
var numbering = false;
|
||
const regex = /\d/g;
|
||
if( i1 > 0 ){
|
||
if( text[i1] === '.') {
|
||
if(regex.exec(text[i1-1]) !== null ){
|
||
w1 = text[i1-1] +'.';
|
||
numbering = true;
|
||
}
|
||
}
|
||
}
|
||
if(w1 === '')
|
||
w1 = getLastWord(text,i1);
|
||
if(w1 === '')
|
||
return false;
|
||
var index = i1-w1.length;
|
||
if(index < 0)
|
||
index = 0;
|
||
|
||
// if( w1 === 'آیه' || w1 === 'آيه'){
|
||
// editor.undoManager.transact(function () {
|
||
// tinyMCE.execCommand('mceInsertContent', false, '-');
|
||
// });
|
||
// //tinyMCE.execCommand('ayahautocomplate', true, 'ناس');
|
||
// return true;
|
||
// }
|
||
|
||
|
||
if(w1 === '=' || w1 === '==' || w1 === '===' || w1 ==='*' || numbering )
|
||
{
|
||
if(index != 0)
|
||
return false;
|
||
var node = range.endContainer;
|
||
while( !isValidLineTag(node) )
|
||
{
|
||
if( node.parentNode == null )
|
||
break;
|
||
node = node.parentNode;
|
||
}
|
||
var id = "";
|
||
if(node == null)
|
||
{
|
||
range.setStart(range.endContainer,index);
|
||
range.setEnd(range.endContainer, index + w1.length);
|
||
}
|
||
else
|
||
{
|
||
range.selectNodeContents(node);
|
||
id = node.getAttribute('id');
|
||
}
|
||
editor.selection.setRng(range);
|
||
|
||
if(id != null && id != '')
|
||
id = ' id="'+id+'"';
|
||
else
|
||
id = '';
|
||
var nodefocus = range.endContainer;
|
||
var t = editor.selection.getContent();
|
||
if(t.length >= w1.length && t.substring(0,w1.length) === w1)
|
||
t = t.substring(w1.length);
|
||
if(numbering) {
|
||
var m = regex.exec(w1[0]);
|
||
editor.selection.setContent('<ol start="'+w1[0]+'"><li'+id+'>'+t+'</li></ol>');
|
||
nodefocus = range.endContainer.firstChild.firstChild;
|
||
}
|
||
else if(w1 === '*') {
|
||
editor.selection.setContent('<ul><li'+id+'>'+t+'</li></ul>');
|
||
nodefocus = range.endContainer.firstChild.firstChild;
|
||
}
|
||
else
|
||
{
|
||
if(w1 === '=')
|
||
editor.selection.setContent('<h1'+id+'>'+t+'</h1>');
|
||
if(w1 === '==')
|
||
editor.selection.setContent('<h2'+id+'>'+t+'</h2>');
|
||
if(w1 === '===')
|
||
editor.selection.setContent('<h3'+id+'>'+t+'</h3>');
|
||
|
||
nodefocus = range.endContainer.firstChild;
|
||
}
|
||
range.selectNodeContents(nodefocus);
|
||
range.collapse(false);
|
||
|
||
editor.selection.setRng(range);
|
||
editor.focus();
|
||
return true;
|
||
}
|
||
else if(endigTag( editor, e, range))
|
||
return true;
|
||
}
|
||
else if(setingTag(editor, e, range) )
|
||
return true;
|
||
}
|
||
}
|
||
return false;
|
||
}
|
||
|
||
var isTagging = function (elm) {
|
||
return elm && elm.nodeName.toLowerCase() === 'span' && elm.className ==='ttag';
|
||
};
|
||
|
||
var isValidLineTag = function (elm) {
|
||
if(elm == null)
|
||
return false;
|
||
var name = elm.nodeName.toLowerCase();
|
||
return elm && (name === 'p' || name.indexOf('h')===0 || name === 'li');
|
||
};
|
||
|
||
function checkHalfSpace(editor, e){
|
||
var range = editor.selection.getRng().cloneRange();
|
||
if( isTextElement(range)){
|
||
if( editor.selection.isCollapsed() ){
|
||
if (isAlaem(e.key) || e.keyCode == 32 || e.keyCode == 160 ){
|
||
var ret = isHalfSpaceNeed(range);
|
||
if(ret[0] != -1){
|
||
var index = ret[0];
|
||
var c = String.fromCharCode(e.keyCode);
|
||
|
||
editor.undoManager.add();
|
||
|
||
//var node1 = range.endContainer;
|
||
range.setStart(range.endContainer,index+ ret[1].length + 1);
|
||
range.setEnd(range.endContainer,index+ ret[1].length + 1);
|
||
editor.selection.setRng(range);
|
||
var bm = editor.selection.getBookmark(2, true);
|
||
editor.selection.setContent(c); // درج فاصله بعد از کلمه برای حالت عادی و ctrl+z
|
||
|
||
|
||
var isTexttype = false;
|
||
var n1 = range.endContainer;
|
||
while(n1 != null)
|
||
{
|
||
if(n1.nodeType==3)
|
||
{
|
||
isTexttype = true;
|
||
break;
|
||
}
|
||
else
|
||
n1 = n1.firstChild;
|
||
}
|
||
if(isTexttype)
|
||
{
|
||
var len = index+ ret[1].length+2;
|
||
if(len <= n1.data.length)
|
||
{
|
||
range.setStart(n1, len);
|
||
range.setEnd(n1, len);
|
||
}
|
||
else
|
||
range.selectNodeContents(n1);
|
||
}
|
||
else
|
||
range.selectNodeContents(range.endContainer);
|
||
|
||
//range.selectNodeContents(range.endContainer);
|
||
//range.collapse(false);
|
||
editor.selection.setRng(range);
|
||
editor.selection.collapse(false);
|
||
editor.focus();
|
||
editor.undoManager.add();
|
||
var bm2 = editor.selection.getBookmark(2, true);
|
||
|
||
editor.selection.moveToBookmark(bm);
|
||
range = editor.selection.getRng().cloneRange();
|
||
var node = range.endContainer;
|
||
//if(node.nodeType === 1 && node.lastChild.nodeType === 3)
|
||
// node = node.lastChild;
|
||
|
||
var i2 = index-ret[2].length;
|
||
if(i2 >= 0 && i2 < node.data.length ){
|
||
range.setStart(node, index-ret[2].length);
|
||
range.setEnd(node, index+ ret[1].length+1);
|
||
editor.selection.setRng(range);
|
||
|
||
editor.selection.setContent(ret[2] +'\u200C' + ret[1]);
|
||
}
|
||
|
||
editor.selection.moveToBookmark(bm2);
|
||
editor.selection.collapse(false);
|
||
editor.focus();
|
||
|
||
//editor.undoManager.add();
|
||
|
||
e.preventDefault();
|
||
return true;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
wlast = '';
|
||
indexlast = -1;
|
||
}
|
||
}
|
||
}
|
||
return false;
|
||
}
|
||
|
||
function checkNemayah(editor, e){
|
||
var range = editor.selection.getRng().cloneRange();
|
||
if( isTextElement(range)){
|
||
if( editor.selection.isCollapsed() ){
|
||
if( e.key === ':' ){
|
||
|
||
var text = range.endContainer.data;
|
||
var i1 = range.endOffset-1;
|
||
var i2 = range.endOffset-2;
|
||
if(i2 < 0)
|
||
return false;
|
||
var c1 = text.charCodeAt(i1);
|
||
var c2 = text[i2];
|
||
if( c1 != 32 && c1 != 160 )
|
||
return false;
|
||
if( alaem.indexOf(c2) !== -1)
|
||
return false;
|
||
|
||
var parent = range.endContainer.parentNode;
|
||
if(parent != null && parent.firstChild === range.endContainer )
|
||
{
|
||
editor.undoManager.add();
|
||
range.setStart(range.endContainer,0);
|
||
range.setEnd(range.endContainer, range.endOffset-1);
|
||
editor.selection.setRng(range);
|
||
editor.selection.setContent('<span class=\"tnemayah\">'+ editor.selection.getContent() +'</span>' + ' :');
|
||
|
||
//range.selectNodeContents(range.endContainer);
|
||
range.collapse(false);
|
||
//editor.selection.setRng(range);
|
||
editor.focus();
|
||
e.preventDefault();
|
||
return true;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
return false;
|
||
}
|
||
|
||
function safePaste(content){
|
||
|
||
content = content.replace(/\<br[^<>]*?\>/g, '\n');
|
||
content = content.replace(/\<div[^<>]*?\>/g, '');
|
||
content = content.replace(/\<\/div\>/g, '');
|
||
|
||
content = content.replace(/\<BR[^<>]*?\>/g, '\n');
|
||
content = content.replace(/\<DIV[^<>]*?\>/g, '');
|
||
content = content.replace(/\<\/DIV\>/g, '');
|
||
|
||
var pattern1 = /(\<[a-zA-Z0-9]+[^<>]*?)(id="[^<>]*")([^<>]*?\>)/g
|
||
content = content.replace(pattern1, '$1$3');
|
||
|
||
|
||
return content;
|
||
} |