Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
coopwire-hypothesis
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
孙灵跃 Leon Sun
coopwire-hypothesis
Commits
1afc7062
Commit
1afc7062
authored
Feb 09, 2024
by
Alejandro Celaya
Committed by
Alejandro Celaya
Feb 09, 2024
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Allow notebook to be closed by pressing Esc
parent
1d898d40
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
39 additions
and
24 deletions
+39
-24
NotebookModal.tsx
src/annotator/components/NotebookModal.tsx
+19
-13
NotebookModal-test.js
src/annotator/components/test/NotebookModal-test.js
+20
-11
No files found.
src/annotator/components/NotebookModal.tsx
View file @
1afc7062
import
{
IconButton
,
CancelIcon
}
from
'@hypothesis/frontend-shared'
;
import
classnames
from
'classnames'
;
import
type
{
ComponentChildren
}
from
'preact'
;
import
{
useEffect
,
useMemo
,
useRef
,
useState
}
from
'preact/hooks'
;
import
{
useCallback
,
useEffect
,
useMemo
,
useRef
,
useState
,
}
from
'preact/hooks'
;
import
{
addConfigFragment
}
from
'../../shared/config-fragment'
;
import
{
createAppConfig
}
from
'../config/app'
;
...
...
@@ -56,9 +62,13 @@ export type NotebookModalProps = {
document_
?:
Document
;
};
type
DialogProps
=
{
isHidden
:
boolean
;
children
:
ComponentChildren
};
type
DialogProps
=
{
isHidden
:
boolean
;
children
:
ComponentChildren
;
onClose
:
()
=>
void
;
};
const
NativeDialog
=
({
isHidden
,
children
}:
DialogProps
)
=>
{
const
NativeDialog
=
({
isHidden
,
children
,
onClose
}:
DialogProps
)
=>
{
const
dialogRef
=
useRef
<
HTMLDialogElement
|
null
>
(
null
);
useEffect
(()
=>
{
...
...
@@ -69,18 +79,14 @@ const NativeDialog = ({ isHidden, children }: DialogProps) => {
}
},
[
isHidden
]);
// Prevent the dialog from closing when `Esc` is pressed, to keep previous
// behavior
useEffect
(()
=>
{
const
dialogElement
=
dialogRef
.
current
;
const
listener
=
(
event
:
Event
)
=>
event
.
preventDefault
();
dialogElement
?.
addEventListener
(
'cancel'
,
listener
);
dialogElement
?.
addEventListener
(
'cancel'
,
onClose
);
return
()
=>
{
dialogElement
?.
removeEventListener
(
'cancel'
,
listener
);
dialogElement
?.
removeEventListener
(
'cancel'
,
onClose
);
};
},
[]);
},
[
onClose
]);
return
(
<
dialog
...
...
@@ -171,17 +177,17 @@ export default function NotebookModal({
};
},
[
eventBus
]);
const
onClose
=
()
=>
{
const
onClose
=
useCallback
(
()
=>
{
setIsHidden
(
true
);
emitterRef
.
current
?.
publish
(
'closeNotebook'
);
};
}
,
[])
;
if
(
groupId
===
null
)
{
return
null
;
}
return
(
<
Dialog
isHidden=
{
isHidden
}
>
<
Dialog
isHidden=
{
isHidden
}
onClose=
{
onClose
}
>
<
div
className=
"absolute right-0 m-3"
>
<
IconButton
title=
"Close the Notebook"
...
...
src/annotator/components/test/NotebookModal-test.js
View file @
1afc7062
...
...
@@ -173,17 +173,26 @@ describe('NotebookModal', () => {
assert
.
isTrue
(
wrapper
.
exists
(
'dialog'
));
});
it
(
'opens and closes native dialog'
,
()
=>
{
const
wrapper
=
createComponent
({});
const
isDialogOpen
=
()
=>
wrapper
.
find
(
'dialog'
).
getDOMNode
().
open
;
act
(()
=>
emitter
.
publish
(
'openNotebook'
,
'myGroup'
));
wrapper
.
update
();
assert
.
isTrue
(
isDialogOpen
());
act
(()
=>
getCloseButton
(
wrapper
).
prop
(
'onClick'
)());
wrapper
.
update
();
assert
.
isFalse
(
isDialogOpen
());
[
// Close via clicking close button
wrapper
=>
getCloseButton
(
wrapper
).
props
().
onClick
(),
// Close via "cancel" event, like pressing `Esc` key
wrapper
=>
wrapper
.
find
(
'dialog'
).
getDOMNode
().
dispatchEvent
(
new
Event
(
'cancel'
)),
].
forEach
(
closeDialog
=>
{
it
(
'opens and closes native dialog'
,
()
=>
{
const
wrapper
=
createComponent
({});
const
isDialogOpen
=
()
=>
wrapper
.
find
(
'dialog'
).
getDOMNode
().
open
;
act
(()
=>
emitter
.
publish
(
'openNotebook'
,
'myGroup'
));
wrapper
.
update
();
assert
.
isTrue
(
isDialogOpen
());
act
(()
=>
closeDialog
(
wrapper
));
wrapper
.
update
();
assert
.
isFalse
(
isDialogOpen
());
});
});
});
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment