Skip to content

Commit

Permalink
Disable contenteditable in links if ctrl pressed
Browse files Browse the repository at this point in the history
  • Loading branch information
BlueGreenMagick committed Apr 28, 2020
1 parent 23db921 commit a95c306
Showing 1 changed file with 96 additions and 75 deletions.
171 changes: 96 additions & 75 deletions edit_field_during_review_cloze/web/global_card.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ EFDRC.specials_ctrl = {
};

//wrappedExceptForWhitespace, wrapInternal from /anki/editor.ts
EFDRC.wrappedExceptForWhitespace = function(text, front, back) {
EFDRC.wrappedExceptForWhitespace = function (text, front, back) {
var match = text.match(/^(\s*)([^]*?)(\s*)$/);
return match[1] + front + match[2] + back + match[3];
}

EFDRC.wrapInternal = function(front, back) {
EFDRC.wrapInternal = function (front, back) {
if (document.activeElement.dir === "rtl") {
front = "‫" + front + "‬";
back = "‫" + back + "‬";
Expand All @@ -59,211 +59,211 @@ EFDRC.wrapInternal = function(front, back) {
}
}

EFDRC.b64DecodeUnicode = function(str) {
return decodeURIComponent(atob(str).split('').map(function(c) {
EFDRC.b64DecodeUnicode = function (str) {
return decodeURIComponent(atob(str).split('').map(function (c) {
return '%%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
}).join(''));
}

EFDRC.removeSpan = function(el){
EFDRC.removeSpan = function (el) {
//removes all span code because depending on the note type
//pressing backspace can wrap the text in span and apply different styling.
var elems = el.getElementsByTagName("span");
for(var x = 0; x < elems.length; x++){
for (var x = 0; x < elems.length; x++) {
var span = elems[x];
var children = span.childNodes;
for(var y = 0; y < children.length; y++){
for (var y = 0; y < children.length; y++) {
//insert after node so caret position is maintained. If last sibling, inserted at end.
span.parentNode.insertBefore(children[y], span.nextSibling);
span.parentNode.insertBefore(children[y], span.nextSibling);
}
span.parentNode.removeChild(span);
}
}

EFDRC.handlePaste = function(e){
EFDRC.handlePaste = function (e) {
var mimetype = ["text/html", "image/", "video/", "audio/", "application/"];
var paste = (e.clipboardData || window.clipboardData);
for(var x = 0; x < paste.types.length; x++){
for (var x = 0; x < paste.types.length; x++) {
var mtype = paste.types[x];
var to_send = false;
for(var y = 0; y < mimetype.length; y++){
if(mtype.indexOf(mimetype[y]) != -1){
for (var y = 0; y < mimetype.length; y++) {
if (mtype.indexOf(mimetype[y]) != -1) {
to_send = true;
break;
}
}
if(to_send){
if (to_send) {
e.preventDefault();
pycmd("EFDRC!paste"); //python code accesses clipboard
break;
}
}
}
}

EFDRC.ctrldown = function(){
els = document.querySelectorAll("[data-EFDRC='true']");
for(var e = 0; e < els.length; e++){
EFDRC.ctrldown = function () {
els = document.querySelectorAll("[data-EFDRC='true']");
for (var e = 0; e < els.length; e++) {
var el = els[e];
el.setAttribute("contenteditable", "true");
if(el.hasAttribute("data-EFDRCnotctrl")){
if (el.hasAttribute("data-EFDRCnotctrl")) {
el.removeAttribute("data-EFDRCnotctrl");
}
}
}

EFDRC.ctrlup = function(){
EFDRC.ctrlup = function () {
var els = document.querySelectorAll("[data-EFDRC='true']");
for(var e = 0; e < els.length; e++){
for (var e = 0; e < els.length; e++) {
var el = els[e];
if(el == document.activeElement){
if (el == document.activeElement) {
el.setAttribute("data-EFDRCnotctrl", "true");
}else{
} else {
el.setAttribute("contenteditable", "false");
}
}
}

EFDRC.addListeners = function(e, fld){
if(EFDRC.PASTE){
EFDRC.addListeners = function (e, fld) {
if (EFDRC.PASTE) {
e.addEventListener('paste', EFDRC.handlePaste);
}

e.addEventListener('focus', function(event){
if(typeof showTooltip == "function" && typeof showTooltip2 == "undefined"){
e.addEventListener('focus', function (event) {
if (typeof showTooltip == "function" && typeof showTooltip2 == "undefined") {
// Disable Popup Dictionary addon tooltip on double mouse click.
// Using hotkey should still work however.
showTooltip2 = showTooltip;
showTooltip = function(event, tooltip, element){
showTooltip = function (event, tooltip, element) {
EFDRC.tooltip = {
ev: event,
tt: tooltip,
el: element
};
};
showTooltip.hide = function(){};
};
showTooltip.hide = function () { };
invokeTooltipAtSelectedElm2 = invokeTooltipAtSelectedElm;
invokeTooltipAtSelectedElm = function(){
invokeTooltipAtSelectedElm = function () {
invokeTooltipAtSelectedElm2();
showTooltip2(EFDRC.tooltip.ev, EFDRC.tooltip.tt, EFDRC.tooltip.el);
}
}

pycmd("EFDRC!focuson#" + fld);
})
e.addEventListener('blur',function(event){
if(typeof showTooltip2 == "function"){
e.addEventListener('blur', function (event) {
if (typeof showTooltip2 == "function") {
// Restore Popup Dictionary
showTooltip = showTooltip2;
delete showTooltip2;
invokeTooltipAtSelectedElm = invokeTooltipAtSelectedElm2;
delete invokeTooltipAtSelectedElm2;
}

var el = event.currentTarget;
if(EFDRC.REMSPAN){
if (EFDRC.REMSPAN) {
EFDRC.removeSpan(el);
}
if(el.hasAttribute("data-EFDRCnotctrl")){
if (el.hasAttribute("data-EFDRCnotctrl")) {
el.removeAttribute("data-EFDRCnotctrl");
el.setAttribute("contenteditable", "false");
}
if(el.hasAttribute("data-EFDRCnid")){
if (el.hasAttribute("data-EFDRCnid")) {
EFDRC.cleanResize(el)
pycmd("EFDRC#" + el.getAttribute("data-EFDRCnid") + "#" + el.getAttribute("data-EFDRCfield") + "#" + el.innerHTML);
pycmd("EFDRC!reload");
}else{
} else {
pycmd("EFDRC!reload");
}
})


e.addEventListener('keydown',function(event){
e.addEventListener('keydown', function (event) {
//Slightly faster.
var ctrlKey = event.ctrlKey||event.metaKey
var ctrlKey = event.ctrlKey || event.metaKey
var shiftKey = event.shiftKey;
var altKey = event.altKey;
var codeKey = event.code;
var el = event.currentTarget;
if(EFDRC.SPAN){
if (EFDRC.SPAN) {
if (codeKey == "Backspace") {
event.stopPropagation();
}
}
if(EFDRC.REMSPAN){
if(codeKey == "Backspace"||codeKey == "Delete"){
setTimeout(function(){
if (EFDRC.REMSPAN) {
if (codeKey == "Backspace" || codeKey == "Delete") {
setTimeout(function () {
EFDRC.removeSpan(el);
}, 0)
}
}

if(event.code == "KeyS" && event.altKey
&& !event.shiftKey && !event.ctrlKey && !event.metaKey){
if (event.code == "KeyS" && event.altKey
&& !event.shiftKey && !event.ctrlKey && !event.metaKey) {
//image resizer
EFDRC.resizeImageMode = !EFDRC.resizeImageMode;
EFDRC.maybeResizeOrClean()
event.preventDefault();
event.stopPropagation();
}

if(ctrlKey){
if (ctrlKey) {
//cloze deletion, onCloze from aqt.editor
if(event.code == "KeyC" && shiftKey){
if (event.code == "KeyC" && shiftKey) {
var highest = 0;
var val = el.innerHTML;
var m;
var myRe = /\{\{c(\d+)::/g;
while ((m = myRe.exec(val)) !== null) {
highest = Math.max(highest, m[1]);
}
if(!altKey){
if (!altKey) {
highest += 1;
}
}
var highest = Math.max(1, highest);
EFDRC.wrapInternal("{\{c" + highest + "::", "}\}");
}

//Special formatting that requires ctrl key.
for(var special in EFDRC.specials_ctrl){
for (var special in EFDRC.specials_ctrl) {
specialVal = EFDRC.specials_ctrl[special]
if(specialVal[4]){
if (specialVal[4]) {
var enabled = EFDRC.SPECIAL[special][0]
var parmVal = EFDRC.SPECIAL[special][1]
}else{
} else {
var enabled = EFDRC.SPECIAL[special]
}
if(enabled){
if (enabled) {
var s = specialVal[0];
var a = specialVal[1];
var c = specialVal[2];
if(shiftKey == s && altKey == a && codeKey == c){
if(specialVal[4]){
if (shiftKey == s && altKey == a && codeKey == c) {
if (specialVal[4]) {
document.execCommand(specialVal[3], false, parmVal);
}else{
} else {
document.execCommand(specialVal[3], false);
}
event.preventDefault();
}
}
}
}else{
} else {
//Special formatting that doesn't require ctrl key
for(var special in EFDRC.specials_noctrl){
for (var special in EFDRC.specials_noctrl) {
specialVal = EFDRC.specials_noctrl[special]
if(specialVal[4]){
if (specialVal[4]) {
var enabled = EFDRC.SPECIAL[special][0]
var parmVal = EFDRC.SPECIAL[special][1]
}else{
} else {
var enabled = EFDRC.SPECIAL[special]
}
if(enabled){
if (enabled) {
var s = specialVal[0];
var a = specialVal[1];
var c = specialVal[2];
if(shiftKey == s && altKey == a && codeKey == c){
if(specialVal[4]){
if (shiftKey == s && altKey == a && codeKey == c) {
if (specialVal[4]) {
document.execCommand(specialVal[3], false, parmVal);
}else{
} else {
document.execCommand(specialVal[3], false);
}
event.preventDefault();
Expand All @@ -275,16 +275,37 @@ EFDRC.addListeners = function(e, fld){
})
}

if(EFDRC.CTRL){
window.addEventListener('keydown',function(event){
if(["ControlLeft", "MetaLeft"].includes(event.code)){

EFDRC.ctrlLinkEnable = function () {
links = document.querySelectorAll("[data-EFDRC='true'] a")
for (var x = 0; x < links.length; x++) {
el = links[x];
el.setAttribute("contenteditable", "false");
}
}

EFDRC.ctrlLinkDisable = function () {
links = document.querySelectorAll("[data-EFDRC='true'] a[contenteditable='false']")
for (var x = 0; x < links.length; x++) {
el = links[x];
el.removeAttribute("contenteditable");
}
}

window.addEventListener('keydown', function (event) {
if (["ControlLeft", "MetaLeft"].includes(event.code)) {
EFDRC.ctrlLinkEnable();
if (EFDRC.CTRL) {
EFDRC.ctrldown();
}
})
}
}
})

window.addEventListener('keyup',function(event){
if(["ControlLeft", "MetaLeft"].includes(event.code)){
window.addEventListener('keyup', function (event) {
if (["ControlLeft", "MetaLeft"].includes(event.code)) {
EFDRC.ctrlLinkDisable();
if (EFDRC.CTRL) {
EFDRC.ctrlup();
}
})
}
}
}
})

0 comments on commit a95c306

Please sign in to comment.