WriteUpperCase now uses patching instead of observing

This commit is contained in:
Mirco Wittrien 2018-12-26 22:35:33 +01:00
parent 31718b9171
commit a99cda8a48
1 changed files with 53 additions and 40 deletions

View File

@ -1,11 +1,17 @@
//META{"name":"WriteUpperCase"}*//
class WriteUpperCase {
initConstructor () {
this.moduleTypes = {
"ChannelTextArea":"componentDidMount"
};
}
getName () {return "WriteUpperCase";}
getDescription () {return "Change input to uppercase.";}
getVersion () {return "1.1.7";}
getVersion () {return "1.1.8";}
getAuthor () {return "DevilBro";}
@ -30,24 +36,12 @@ class WriteUpperCase {
if (typeof BDFDB === "object") {
BDFDB.loadMessage(this);
var observer = null;
observer = new MutationObserver((changes, _) => {
changes.forEach(
(change, i) => {
if (change.addedNodes) {
change.addedNodes.forEach((node) => {
if (node && node.tagName && node.querySelector(BDFDB.dotCN.textareainner + ":not(" + BDFDB.dotCN.textareainnerdisabled + ")")) {
this.bindEventToTextArea(node.querySelector(BDFDB.dotCN.textarea));
}
});
}
}
);
});
BDFDB.addObserver(this, BDFDB.dotCN.appmount, {name:"textareaObserver",instance:observer}, {childList: true, subtree:true});
for (let type in this.moduleTypes) {
let module = BDFDB.WebModules.findByName(type);
if (module && module.prototype) BDFDB.WebModules.patch(module.prototype, this.moduleTypes[type], this, {after: (e) => {this.initiateProcess(e.thisObject, type);}});
}
document.querySelectorAll("textarea" + BDFDB.dotCN.textarea).forEach(textarea => {this.bindEventToTextArea(textarea);});
this.forceAllUpdates();
}
else {
console.error(this.getName() + ": Fatal Error: Could not load BD functions!");
@ -68,31 +62,50 @@ class WriteUpperCase {
$(textarea)
.off("keyup." + this.getName())
.on("keyup." + this.getName(), () => {
clearTimeout(textarea.writeuppercasetimeout);
textarea.writeuppercasetimeout = setTimeout(() => {this.formatText(textarea);},1);
clearTimeout(textarea.WriteUpperCaseTimeout);
textarea.WriteUpperCaseTimeout = setTimeout(() => {
let string = textarea.value;
if (string.length > 0) {
let newstring = string;
let first = string.charAt(0);
let position = textarea.selectionStart;
if (first === first.toUpperCase() && (string.toLowerCase().indexOf("http") == 0 || string.toLowerCase().indexOf("s/") == 0)) newstring = string.charAt(0).toLowerCase() + string.slice(1);
else if (first === first.toLowerCase() && first !== first.toUpperCase() && string.toLowerCase().indexOf("http") != 0 && string.toLowerCase().indexOf("s/") != 0) newstring = string.charAt(0).toUpperCase() + string.slice(1);
if (string != newstring) {
textarea.focus();
textarea.selectionStart = 0;
textarea.selectionEnd = textarea.value.length;
document.execCommand("insertText", false, newstring);
textarea.selectionStart = position;
textarea.selectionEnd = position;
}
}
},1);
});
}
formatText (textarea) {
var string = textarea.value;
if (string.length > 0) {
var newstring = string;
var first = string.charAt(0);
var position = textarea.selectionStart;
if (first === first.toUpperCase() && (string.toLowerCase().indexOf("http") == 0 || string.toLowerCase().indexOf("s/") == 0)) {
newstring = string.charAt(0).toLowerCase() + string.slice(1);
}
else if (first === first.toLowerCase() && first !== first.toUpperCase() && string.toLowerCase().indexOf("http") != 0 && string.toLowerCase().indexOf("s/") != 0) {
newstring = string.charAt(0).toUpperCase() + string.slice(1);
}
if (string != newstring) {
textarea.focus();
textarea.selectionStart = 0;
textarea.selectionEnd = textarea.value.length;
document.execCommand("insertText", false, newstring);
textarea.selectionStart = position;
textarea.selectionEnd = position;
}
initiateProcess (instance, type) {
type = type.replace(/[^A-z]/g,"");
type = type[0].toUpperCase() + type.slice(1);
if (typeof this["process" + type] == "function") {
let wrapper = BDFDB.React.findDOMNodeSafe(instance);
if (wrapper) this["process" + type](instance, wrapper);
else setImmediate(() => {
this["process" + type](instance, BDFDB.React.findDOMNodeSafe(instance));
});
}
}
processChannelTextArea (instance, wrapper) {
if (!wrapper) return;
if (instance.props && instance.props.type) this.bindEventToTextArea(wrapper.querySelector("textarea"));
}
forceAllUpdates () {
let app = document.querySelector(BDFDB.dotCN.app);
if (app) {
let ins = BDFDB.getOwnerInstance({node:app, name:Object.keys(this.moduleTypes), all:true, noCopies:true, group:true, depth:99999999, time:99999999});
for (let type in ins) for (let i in ins[type]) this.initiateProcess(ins[type][i], type);
}
}
}