<commit_before>"use strict";

/* eslint no-unused-vars: 0 */


// Init This Window with jQuery ready Callback
$(document).ready(init);




// unlaodWindow Handler, to generate messages that form is left unsaved
function unloadWindowWarning(event) {
  let dialogText = "Oops, it looks like you have forgotten to save your work.";
  event.returnValue = dialogText;
  return dialogText;
}

var mdRender;
// initialise all callbacks with jQuery
function init() {
  mdRender = window.markdownit();
  mdRender.use(window.markdownitSup);
  mdRender.use(window["markdown-it-imsize.js"]);

  $("#collection")
    .change(onchangeCollection)
    .change(FitToContent)
    .keyup(onchangeCollection)
    .keyup(FitToContent)
    .keyup(showModified)
    .trigger("change");
  $(".markdownEdit")
    .change(convertMinusNoTranslation)
    .change(previewMarkdown)
    .change(showModified)
    .change(FitToContent)
    .change(checkMarkdownError)
    .focusout(convertMinusNoTranslation)
    .keyup(previewMarkdown)
    .keyup(checkForTable)
    .keyup(FitToContent)
    .keyup(checkMarkdownError)
    .keyup(showModified)
    .on("paste", pasteEvent)
    .on("drop", pasteEvent)
    .trigger("change");
  $("#title")
    .keyup(showModified);
  $("#predecessorId")
    .change(showModified);
  $("#blog")
    .change(showHideUnpublishReason)
    .change(showModified)
    .trigger("change");
  $("#unpublishReason")
    .change(showModified)
    .trigger("change");
  $("#unpublishReference")
    .keyup(showModified)
    .trigger("change");
  $("#categoryEN")
    .change(showModified)
    .change(syncPlaceholder)
    .change(showHideUnpublishReason)

    .trigger("change");
  $("#AddComment")
    .change(FitToContent)
    .keyup(FitToContent)
    .trigger("change");
  $("#editComment")
    .change(FitToContent)
    .keyup(FitToContent)
    .trigger("change");
  $('[data-toggle="tooltip"]').tooltip();
  window.submitAndRedraw = submitAndRedraw;
  window.saveButton = saveButton;
}

function saveButton() {
  function save() {
    disableUnchanged(); document.getElementById("input").submit();
  }
  if (document.getElementById("comment").value.trim() === "") return save();

  var jqForm = $("form#AddComment");
  var url = jqForm.attr("action");
  var toPost = jqForm.serialize();

  $.post(url, toPost, function (data) {
    if (data === "OK") {
      save();
    }
  },
  "text" // I expect a JSON response
  ).fail(function (err) {
    console.error(err);
  });
}




// Check, wether value of field is "-" and set it to "no translation"
// Install as onleave event handler
function convertMinusNoTranslation() {
  /* jshint validthis: true */
  var object = $(this);
  var md = object.val();
  if (md && md.trim() === "-") {
    object.val("no translation");
    object.trigger("change");
  }
}




// Function to convert markdown into html
// Add the Blog typic list element to preview, but only
// for specific types, (Missing: configuring this by user.)
function convert(text) {
  // The cut at the beginning is a little bit "historic" from the
  // OSMBC startup, so only used for emotional motivation of the developer :-)
  if (text.substring(0, 2) === "* ") text = text.substring(2, 999999);


  // convert md to html
  text = mdRender.render(text);


  // Display as list, this should depend on type

  var cat = $("#categoryEN").val();
  if (cat !== "Picture" && cat !== "Events" && cat !== "Releases") {
    text = "<ul><li>" + text + "</li></ul>";
  }
  return text;
}

// Test, wether a text is an url or not
// based on regex.
function isURL(t) {
  var isUrlRegex = /^(http|ftp|https):\/\/[\w-]+(\.[\w-]+)+([\w.,@?^=%&amp;:\/~+#-]*[\w@?^=%&amp;\/~+#-])?/;
  if (t) return isUrlRegex.test(t);
  return isUrlRegex;
}

// Try to copy text to the clipboard
// if it fails, alert an error and offer the possibility to copy manual
function clip(text) {
  var copyElement = document.createElement("input");
  copyElement.setAttribute("type", "text");
  copyElement.setAttribute("value", text);
  copyElement = document.body.appendChild(copyElement);
  copyElement.select();
  try {
    if (!document.execCommand("copy")) throw new Error("Not allowed.");
  } catch (e) {
    copyElement.remove();
    prompt("Copy not supported, use your mobile to copy and press enter", text);
  } finally {
    if (typeof e === "undefined") {
      copyElement.remove();
    }
  }
}

// Function that generates a markdown link in the case a
// url is pasted or drag'n dropped to a markdown textfield
//
function generateMarkdownLink2(par1, par2) {
  // tb == Text Before Selection
  // ta == Text Afer Selection
  var tb = par1.text.substring(0, par1.startselection);
  var ta = par1.text.substring(par1.endselection, 99999);

  // beforeSnip == selected Text before paste
  // afterSnip == selected Text after Insert
  var beforeSnip = par1.text.substring(par1.startselection, par1.endselection);
  var afterSnip = par2.text.substring(par1.startselection, par2.endselection);

  // only in the case, the "afterSnip" variable contains an URL, something
  // has to be done
  if (isURL(afterSnip.trim())) {
    afterSnip = afterSnip.trim();

    // Check, wether, the url is pasted into brackets ()
    // then nothing has to be done
    var alreadyMarkdowned = false;
    if (tb !== "" && tb.charAt(tb.length - 1) === "(") alreadyMarkdowned = true;
    if (ta !== "" && tb.charAt(0) === ")") alreadyMarkdowned = true;
    if (!alreadyMarkdowned) {
      // combine the new text
      var r = tb + "[" + beforeSnip + "](" + afterSnip + ")" + ta;

      // calculate the position to point to
      // for the new text
      // Between [] in the case, that only a link was added
      // after the [link](http://somewhere) in case the link has already a "display" word
      var c;
      if (beforeSnip === "") {
        c = (tb + "[").length;
      } else {
        c = (tb + "[" + beforeSnip + "](" + afterSnip + ")").length;
      }
      return { text: r, pos: c };
    }
  }
  return null;
}<commit_msg>Close #803. Better Markdown link generation while pasting.<commit_after>"use strict";

/* eslint no-unused-vars: 0 */


// Init This Window with jQuery ready Callback
$(document).ready(init);




// unlaodWindow Handler, to generate messages that form is left unsaved
function unloadWindowWarning(event) {
  let dialogText = "Oops, it looks like you have forgotten to save your work.";
  event.returnValue = dialogText;
  return dialogText;
}

var mdRender;
// initialise all callbacks with jQuery
function init() {
  mdRender = window.markdownit();
  mdRender.use(window.markdownitSup);
  mdRender.use(window["markdown-it-imsize.js"]);

  $("#collection")
    .change(onchangeCollection)
    .change(FitToContent)
    .keyup(onchangeCollection)
    .keyup(FitToContent)
    .keyup(showModified)
    .trigger("change");
  $(".markdownEdit")
    .change(convertMinusNoTranslation)
    .change(previewMarkdown)
    .change(showModified)
    .change(FitToContent)
    .change(checkMarkdownError)
    .focusout(convertMinusNoTranslation)
    .keyup(previewMarkdown)
    .keyup(checkForTable)
    .keyup(FitToContent)
    .keyup(checkMarkdownError)
    .keyup(showModified)
    .on("paste", pasteEvent)
    .on("drop", pasteEvent)
    .trigger("change");
  $("#title")
    .keyup(showModified);
  $("#predecessorId")
    .change(showModified);
  $("#blog")
    .change(showHideUnpublishReason)
    .change(showModified)
    .trigger("change");
  $("#unpublishReason")
    .change(showModified)
    .trigger("change");
  $("#unpublishReference")
    .keyup(showModified)
    .trigger("change");
  $("#categoryEN")
    .change(showModified)
    .change(syncPlaceholder)
    .change(showHideUnpublishReason)

    .trigger("change");
  $("#AddComment")
    .change(FitToContent)
    .keyup(FitToContent)
    .trigger("change");
  $("#editComment")
    .change(FitToContent)
    .keyup(FitToContent)
    .trigger("change");
  $('[data-toggle="tooltip"]').tooltip();
  window.submitAndRedraw = submitAndRedraw;
  window.saveButton = saveButton;
}

function saveButton() {
  function save() {
    disableUnchanged(); document.getElementById("input").submit();
  }
  if (document.getElementById("comment").value.trim() === "") return save();

  var jqForm = $("form#AddComment");
  var url = jqForm.attr("action");
  var toPost = jqForm.serialize();

  $.post(url, toPost, function (data) {
    if (data === "OK") {
      save();
    }
  },
  "text" // I expect a JSON response
  ).fail(function (err) {
    console.error(err);
  });
}




// Check, wether value of field is "-" and set it to "no translation"
// Install as onleave event handler
function convertMinusNoTranslation() {
  /* jshint validthis: true */
  var object = $(this);
  var md = object.val();
  if (md && md.trim() === "-") {
    object.val("no translation");
    object.trigger("change");
  }
}




// Function to convert markdown into html
// Add the Blog typic list element to preview, but only
// for specific types, (Missing: configuring this by user.)
function convert(text) {
  // The cut at the beginning is a little bit "historic" from the
  // OSMBC startup, so only used for emotional motivation of the developer :-)
  if (text.substring(0, 2) === "* ") text = text.substring(2, 999999);


  // convert md to html
  text = mdRender.render(text);


  // Display as list, this should depend on type

  var cat = $("#categoryEN").val();
  if (cat !== "Picture" && cat !== "Events" && cat !== "Releases") {
    text = "<ul><li>" + text + "</li></ul>";
  }
  return text;
}

// Test, wether a text is an url or not
// based on regex.
function isURL(t) {
  var isUrlRegex = /^(http|ftp|https):\/\/[\w-]+(\.[\w-]+)+([\w.,@?^=%&amp;:\/~+#-]*[\w@?^=%&amp;\/~+#-])?/;
  if (t) return isUrlRegex.test(t);
  return isUrlRegex;
}

// Try to copy text to the clipboard
// if it fails, alert an error and offer the possibility to copy manual
function clip(text) {
  var copyElement = document.createElement("input");
  copyElement.setAttribute("type", "text");
  copyElement.setAttribute("value", text);
  copyElement = document.body.appendChild(copyElement);
  copyElement.select();
  try {
    if (!document.execCommand("copy")) throw new Error("Not allowed.");
  } catch (e) {
    copyElement.remove();
    prompt("Copy not supported, use your mobile to copy and press enter", text);
  } finally {
    if (typeof e === "undefined") {
      copyElement.remove();
    }
  }
}

// Function that generates a markdown link in the case a
// url is pasted or drag'n dropped to a markdown textfield
//
function generateMarkdownLink2(par1, par2) {
  // tb == Text Before Selection
  // ta == Text Afer Selection
  var tb = par1.text.substring(0, par1.startselection);
  var ta = par1.text.substring(par1.endselection, 99999);

  // beforeSnip == selected Text before paste
  // afterSnip == selected Text after Insert
  var beforeSnip = par1.text.substring(par1.startselection, par1.endselection);
  var afterSnip = par2.text.substring(par1.startselection, par2.endselection);

  // only in the case, the "afterSnip" variable contains an URL, something
  // has to be done
  if (isURL(afterSnip.trim())) {
    afterSnip = afterSnip.trim();

    // Check, wether, the url is pasted into brackets ()
    // then nothing has to be done
    var alreadyMarkdowned = false;
    if (tb.length > 2 && tb.charAt(tb.length - 1) === "(" && tb.charAt(tb.length - 2) === "]") alreadyMarkdowned = true;
    if (ta !== "" && tb.charAt(0) === ")") alreadyMarkdowned = true;
    if (!alreadyMarkdowned) {
      // combine the new text
      var r = tb + "[" + beforeSnip + "](" + afterSnip + ")" + ta;

      // calculate the position to point to
      // for the new text
      // Between [] in the case, that only a link was added
      // after the [link](http://somewhere) in case the link has already a "display" word
      var c;
      if (beforeSnip === "") {
        c = (tb + "[").length;
      } else {
        c = (tb + "[" + beforeSnip + "](" + afterSnip + ")").length;
      }
      return { text: r, pos: c };
    }
  }
  return null;
}
{'fraction_non_alphanumeric': 0.16002980379170462, 'fraction_numerical': 0.005546816789469327, 'mean_word_length': 3.3081312410841655, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5506302', 'n_tokens_mistral': 3915, 'n_tokens_neox': 3815, 'n_words': 1044}