1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-27 12:07:19 +00:00

Cancel open popups when inputs get focus

This commit is contained in:
Jeremy Ruston 2019-07-14 16:07:36 +01:00 committed by GitHub
commit ac49b4d20c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 50 additions and 7 deletions

View File

@ -79,6 +79,7 @@ function FramedEngine(options) {
// Add event listeners
$tw.utils.addEventListeners(this.domNode,[
{name: "click",handlerObject: this,handlerMethod: "handleClickEvent"},
{name: "focus",handlerObject: this.widget,handlerMethod: "handleFocusEvent"},
{name: "input",handlerObject: this,handlerMethod: "handleInputEvent"},
{name: "keydown",handlerObject: this.widget,handlerMethod: "handleKeydownEvent"}
]);
@ -153,6 +154,13 @@ FramedEngine.prototype.focus = function() {
}
};
/*
Handle the focus event
*/
FramedEngine.prototype.handleFocusEvent = function() {
this.widget.cancelPopups();
};
/*
Handle a click
*/

View File

@ -122,6 +122,7 @@ SimpleEngine.prototype.handleInputEvent = function(event) {
Handle a dom "focus" event
*/
SimpleEngine.prototype.handleFocusEvent = function(event) {
this.widget.cancelPopups();
if(this.widget.editFocusPopup) {
$tw.popup.triggerPopup({
domNode: this.domNode,

View File

@ -248,6 +248,13 @@ function editTextWidgetFactory(toolbarEngine,nonToolbarEngine) {
}
};
/*
Cancel Popups
*/
EditTextWidget.prototype.cancelPopups = function() {
$tw.popup.cancel(0,this.engine.domNode);
};
/*
Handle a dom "keydown" event, which we'll bubble up to our container for the keyboard widgets benefit
*/

View File

@ -148,14 +148,37 @@ Popup.prototype.show = function(options) {
}
};
/*
Detect if a Popup contains an input field that has focus
Returns true or false
*/
Popup.prototype.detectInputWithinPopup = function(node) {
var withinPopup = false;
for(var i=0; i<this.popups.length; i++) {
var popup = (this.popups[i] && this.popups[i].domNode) ? this.popups[i].domNode : null;
while(node && popup) {
if(node === popup || (node.classList && node.classList.contains("tc-popup-keep"))) {
withinPopup = true;
}
node = node.parentNode;
}
}
return withinPopup;
};
/*
Cancel all popups at or above a specified level or DOM node
level: popup level to cancel (0 cancels all popups)
*/
Popup.prototype.cancel = function(level) {
Popup.prototype.cancel = function(level,focusedInputNode) {
var numPopups = this.popups.length;
level = Math.max(0,Math.min(level,numPopups));
for(var t=level; t<numPopups; t++) {
var inputWithinPopup;
if(focusedInputNode) {
inputWithinPopup = this.detectInputWithinPopup(focusedInputNode);
}
if(!inputWithinPopup) {
var popup = this.popups.pop();
if(popup.title) {
if(popup.noStateReference) {
@ -165,6 +188,7 @@ Popup.prototype.cancel = function(level) {
}
}
}
}
if(this.popups.length === 0) {
this.rootElement.removeEventListener("click",this,false);
}

View File

@ -125,6 +125,9 @@ function CodeMirrorEngine(options) {
event.stopPropagation(); // Otherwise TW's dropzone widget sees the drop event
return false;
});
this.cm.on("focus",function() {
self.widget.cancelPopups();
});
this.cm.on("keydown",function(cm,event) {
return self.widget.handleKeydownEvent.call(self.widget,event);
});