Skip to content

Commit

Permalink
fix: do not lose focus and fire a focus/blur event when opening a mod…
Browse files Browse the repository at this point in the history
…al inside the editor
  • Loading branch information
josdejong committed Sep 22, 2021
1 parent 7ffd586 commit cbb0c79
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
16 changes: 14 additions & 2 deletions src/lib/components/modes/codemode/CodeMode.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,15 @@
}
}
// modalOpen is true when one of the modals is open.
// This is used to track whether the editor still has focus
let modalOpen = false
createFocusTracker({
onMount,
onDestroy,
getWindow: () => getWindow(domCodeMode),
hasFocus: () => activeElementIsChildOf(domCodeMode),
hasFocus: () => (modalOpen && document.hasFocus()) || activeElementIsChildOf(domCodeMode),
onFocus,
onBlur
})
Expand Down Expand Up @@ -226,6 +230,8 @@
try {
const json = JSON.parse(text)
modalOpen = true
open(
SortModal,
{
Expand All @@ -239,7 +245,10 @@
},
SORT_MODAL_OPTIONS,
{
onClose: focus
onClose: () => {
modalOpen = false
focus()
}
}
)
} catch (err) {
Expand All @@ -251,6 +260,8 @@
try {
const json = JSON.parse(text)
modalOpen = true
open(
TransformModal,
{
Expand All @@ -275,6 +286,7 @@
TRANSFORM_MODAL_OPTIONS,
{
onClose: () => {
modalOpen = false
focus()
if (onClose) {
onClose()
Expand Down
23 changes: 20 additions & 3 deletions src/lib/components/modes/treemode/TreeMode.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,15 @@
export let onFocus
export let onBlur
// modalOpen is true when one of the modals is open.
// This is used to track whether the editor still has focus
let modalOpen = false
createFocusTracker({
onMount,
onDestroy,
getWindow: () => getWindow(refJsonEditor),
hasFocus: () => activeElementIsChildOf(refJsonEditor),
hasFocus: () => (modalOpen && document.hasFocus()) || activeElementIsChildOf(refJsonEditor),
onFocus: () => {
hasFocus = true
if (onFocus) {
Expand Down Expand Up @@ -973,6 +977,8 @@
return
}
modalOpen = true
open(
SortModal,
{
Expand All @@ -995,7 +1001,10 @@
},
SORT_MODAL_OPTIONS,
{
onClose: () => focus()
onClose: () => {
modalOpen = false
focus()
}
}
)
}
Expand All @@ -1019,6 +1028,8 @@
return
}
modalOpen = true
open(
TransformModal,
{
Expand Down Expand Up @@ -1051,6 +1062,7 @@
TRANSFORM_MODAL_OPTIONS,
{
onClose: () => {
modalOpen = false
focus()
if (onClose) {
onClose()
Expand Down Expand Up @@ -1540,6 +1552,8 @@
}
}
modalOpen = true
openAbsolutePopup(ContextMenu, props, {
left,
top,
Expand All @@ -1549,7 +1563,10 @@
height,
anchor,
closeOnOuterClick: true,
onClose: focus
onClose: () => {
modalOpen = false
focus()
}
})
}
Expand Down

0 comments on commit cbb0c79

Please sign in to comment.