משתמש:מהדורה קמא/בדיקת עוגנים.js: הבדלים בין גרסאות בדף

הצעה לתיקון
אין תקציר עריכה
(הצעה לתיקון)
שורה 28: שורה 28:
   });
   });


  function replaceDuplicateAnchors() {
function replaceDuplicateAnchors() {
     // קביעת משתנים והבניית הביטויים הרגולריים
     // קביעת משתנים והבניית הביטויים הרגולריים
     var textArea = $('#wpTextbox1');
     var textArea = $('#wpTextbox1');
שורה 38: שורה 38:
     // חיפוש והחלפת העוגנים הכפולים
     // חיפוש והחלפת העוגנים הכפולים
     while ((match = anchorRegex.exec(content)) !== null) {
     while ((match = anchorRegex.exec(content)) !== null) {
      var key = match[1].toLowerCase();
        var key = match[1].toLowerCase();
      var text = match[2] !== undefined ? match[2] : key; // אם אין טקסט חופשי, השתמש בפרמטר העוגן
        var text = match[2] !== undefined ? match[2] : key; // אם אין טקסט חופשי, השתמש בפרמטר העוגן
      if (!anchors[key]) {
        if (!anchors[key]) {
        anchors[key] = [];
            anchors[key] = [];
      }
        }
      anchors[key].push({ index: match.index, length: match[0].length, text: text });
        anchors[key].push({ index: match.index, length: match[0].length, text: text });
     }
     }


    // בדיקה שהתוכן של העוגנים בלבד השתנה
    var replacedContent = content;
     $.each(anchors, function (key, positions) {
     $.each(anchors, function (key, positions) {
      if (positions.length > 1) {
        if (positions.length > 1) {
        var offset = 0;
            var offset = 0;
        var counter = 1; // מספר התוקף הנוכחי
            var counter = 1; // מספר התוקף הנוכחי
        positions.forEach(function (pos, i) {
            positions.forEach(function (pos, i) {
          var replacement;
                var replacement;
          if (i > 0) { // מתחיל למספר מהעוגן השני
                if (i > 0) { // מתחיל למספר מהעוגן השני
            counter++;
                    counter++;
            replacement = `{{עוגן|${key}${counter}|${pos.text}}}`;
                    replacement = `{{עוגן|${key}${counter}|${pos.text}}}`;
          } else {
                } else {
            replacement = `{{עוגן|${key}|${pos.text}}}`; // העוגן הראשון ללא מספור
                    replacement = `{{עוגן|${key}|${pos.text}}}`; // העוגן הראשון ללא מספור
          }
                }
          var startIndex = pos.index + offset;
                var startIndex = pos.index + offset;
          content = content.slice(0, startIndex) + replacement + content.slice(startIndex + pos.length);
                replacedContent = replacedContent.slice(0, startIndex) + replacement + replacedContent.slice(startIndex + pos.length);
          offset += replacement.length - pos.length;
                offset += replacement.length - pos.length;
        });
            });
         textArea.val(content);
         }
      }
     });
     });


     // בדיקה שהתוכן של התיבה שונה והודעה למשתמש
     // בדיקה שהתוכן של התיבה שונה והודעה למשתמש
     if (textArea.val() !== content) {
     if (replacedContent !== content) {
      alert('הוחלפו עוגנים כפולים בהצלחה');
        var userContent = content.replace(/{{[^{}]+}}/g, ''); // הוצאת כל העוגנים מתוך התוכן המקורי
        var replacedUserContent = replacedContent.replace(/{{[^{}]+}}/g, ''); // הוצאת כל העוגנים מתוך התוכן שהוחלף
        if (userContent === replacedUserContent) {
            textArea.val(replacedContent);
            alert('הוחלפו עוגנים כפולים בהצלחה');
        } else {
            alert('יש שינויים בתוכן מעבר לעוגנים ולכן לא ניתן להחליף את העוגנים.');
        }
     } else {
     } else {
      alert('אין עוגנים כפולים');
        alert('אין עוגנים כפולים');
     }
     }
  }
}
});
});
       });
       });