WriteUpperCase now uses patching instead of observing
This commit is contained in:
parent
31718b9171
commit
a99cda8a48
|
@ -1,11 +1,17 @@
|
||||||
//META{"name":"WriteUpperCase"}*//
|
//META{"name":"WriteUpperCase"}*//
|
||||||
|
|
||||||
class WriteUpperCase {
|
class WriteUpperCase {
|
||||||
|
initConstructor () {
|
||||||
|
this.moduleTypes = {
|
||||||
|
"ChannelTextArea":"componentDidMount"
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
getName () {return "WriteUpperCase";}
|
getName () {return "WriteUpperCase";}
|
||||||
|
|
||||||
getDescription () {return "Change input to uppercase.";}
|
getDescription () {return "Change input to uppercase.";}
|
||||||
|
|
||||||
getVersion () {return "1.1.7";}
|
getVersion () {return "1.1.8";}
|
||||||
|
|
||||||
getAuthor () {return "DevilBro";}
|
getAuthor () {return "DevilBro";}
|
||||||
|
|
||||||
|
@ -30,24 +36,12 @@ class WriteUpperCase {
|
||||||
if (typeof BDFDB === "object") {
|
if (typeof BDFDB === "object") {
|
||||||
BDFDB.loadMessage(this);
|
BDFDB.loadMessage(this);
|
||||||
|
|
||||||
var observer = null;
|
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);}});
|
||||||
|
}
|
||||||
|
|
||||||
observer = new MutationObserver((changes, _) => {
|
this.forceAllUpdates();
|
||||||
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});
|
|
||||||
|
|
||||||
document.querySelectorAll("textarea" + BDFDB.dotCN.textarea).forEach(textarea => {this.bindEventToTextArea(textarea);});
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
console.error(this.getName() + ": Fatal Error: Could not load BD functions!");
|
console.error(this.getName() + ": Fatal Error: Could not load BD functions!");
|
||||||
|
@ -68,23 +62,15 @@ class WriteUpperCase {
|
||||||
$(textarea)
|
$(textarea)
|
||||||
.off("keyup." + this.getName())
|
.off("keyup." + this.getName())
|
||||||
.on("keyup." + this.getName(), () => {
|
.on("keyup." + this.getName(), () => {
|
||||||
clearTimeout(textarea.writeuppercasetimeout);
|
clearTimeout(textarea.WriteUpperCaseTimeout);
|
||||||
textarea.writeuppercasetimeout = setTimeout(() => {this.formatText(textarea);},1);
|
textarea.WriteUpperCaseTimeout = setTimeout(() => {
|
||||||
});
|
let string = textarea.value;
|
||||||
}
|
|
||||||
|
|
||||||
formatText (textarea) {
|
|
||||||
var string = textarea.value;
|
|
||||||
if (string.length > 0) {
|
if (string.length > 0) {
|
||||||
var newstring = string;
|
let newstring = string;
|
||||||
var first = string.charAt(0);
|
let first = string.charAt(0);
|
||||||
var position = textarea.selectionStart;
|
let position = textarea.selectionStart;
|
||||||
if (first === first.toUpperCase() && (string.toLowerCase().indexOf("http") == 0 || string.toLowerCase().indexOf("s/") == 0)) {
|
if (first === first.toUpperCase() && (string.toLowerCase().indexOf("http") == 0 || string.toLowerCase().indexOf("s/") == 0)) newstring = string.charAt(0).toLowerCase() + string.slice(1);
|
||||||
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);
|
||||||
}
|
|
||||||
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) {
|
if (string != newstring) {
|
||||||
textarea.focus();
|
textarea.focus();
|
||||||
textarea.selectionStart = 0;
|
textarea.selectionStart = 0;
|
||||||
|
@ -94,5 +80,32 @@ class WriteUpperCase {
|
||||||
textarea.selectionEnd = position;
|
textarea.selectionEnd = position;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
},1);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue