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
78386e60
Commit
78386e60
authored
Feb 12, 2024
by
Alejandro Celaya
Committed by
Alejandro Celaya
Feb 12, 2024
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Extract logic to dynamically render a native dialog
parent
2a7a3607
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
162 additions
and
148 deletions
+162
-148
ModalDialog.tsx
src/annotator/components/ModalDialog.tsx
+83
-0
NotebookModal.tsx
src/annotator/components/NotebookModal.tsx
+4
-83
ModalDialog-test.js
src/annotator/components/test/ModalDialog-test.js
+60
-0
NotebookModal-test.js
src/annotator/components/test/NotebookModal-test.js
+15
-65
No files found.
src/annotator/components/ModalDialog.tsx
0 → 100644
View file @
78386e60
import
classnames
from
'classnames'
;
import
type
{
ComponentChildren
}
from
'preact'
;
import
{
useEffect
,
useMemo
,
useRef
}
from
'preact/hooks'
;
type
DialogProps
=
{
closed
:
boolean
;
children
:
ComponentChildren
;
onClose
:
()
=>
void
;
};
const
NativeDialog
=
({
closed
,
children
,
onClose
}:
DialogProps
)
=>
{
const
dialogRef
=
useRef
<
HTMLDialogElement
|
null
>
(
null
);
useEffect
(()
=>
{
if
(
closed
)
{
dialogRef
.
current
?.
close
();
}
else
{
dialogRef
.
current
?.
showModal
();
}
},
[
closed
]);
useEffect
(()
=>
{
const
dialogElement
=
dialogRef
.
current
;
dialogElement
?.
addEventListener
(
'cancel'
,
onClose
);
return
()
=>
{
dialogElement
?.
removeEventListener
(
'cancel'
,
onClose
);
};
},
[
onClose
]);
return
(
<
dialog
ref=
{
dialogRef
}
className=
"relative w-full h-full backdrop:bg-black/50"
data
-
testid=
"notebook-outer"
>
{
children
}
</
dialog
>
);
};
/**
* Temporary fallback used in browsers not supporting `dialog` element.
* It can be removed once all browsers we support can use it.
*/
const
FallbackDialog
=
({
closed
,
children
}:
DialogProps
)
=>
{
return
(
<
div
className=
{
classnames
(
'fixed z-max top-0 left-0 right-0 bottom-0 p-3 bg-black/50'
,
{
hidden
:
closed
},
)
}
data
-
testid=
"notebook-outer"
>
<
div
className=
"relative w-full h-full"
data
-
testid=
"notebook-inner"
>
{
children
}
</
div
>
</
div
>
);
};
/** Checks if the browser supports native modal dialogs */
function
isModalDialogSupported
(
document
:
Document
)
{
const
dialog
=
document
.
createElement
(
'dialog'
);
return
typeof
dialog
.
showModal
===
'function'
;
}
export
type
ModalDialogProps
=
DialogProps
&
{
document_
?:
Document
;
};
export
default
function
ModalDialog
({
/* istanbul ignore next - test seam */
document_
=
document
,
...
rest
}:
ModalDialogProps
)
{
const
Dialog
=
useMemo
(
()
=>
(
isModalDialogSupported
(
document_
)
?
NativeDialog
:
FallbackDialog
),
[
document_
],
);
return
<
Dialog
{
...
rest
}
/>;
}
src/annotator/components/NotebookModal.tsx
View file @
78386e60
import
{
IconButton
,
CancelIcon
}
from
'@hypothesis/frontend-shared'
;
import
{
IconButton
,
CancelIcon
}
from
'@hypothesis/frontend-shared'
;
import
classnames
from
'classnames'
;
import
classnames
from
'classnames'
;
import
type
{
ComponentChildren
}
from
'preact'
;
import
{
useCallback
,
useEffect
,
useRef
,
useState
}
from
'preact/hooks'
;
import
{
useCallback
,
useEffect
,
useMemo
,
useRef
,
useState
,
}
from
'preact/hooks'
;
import
{
addConfigFragment
}
from
'../../shared/config-fragment'
;
import
{
addConfigFragment
}
from
'../../shared/config-fragment'
;
import
{
createAppConfig
}
from
'../config/app'
;
import
{
createAppConfig
}
from
'../config/app'
;
import
type
{
EventBus
,
Emitter
}
from
'../util/emitter'
;
import
type
{
EventBus
,
Emitter
}
from
'../util/emitter'
;
import
ModalDialog
from
'./ModalDialog'
;
/**
/**
* Configuration used to launch the notebook application.
* Configuration used to launch the notebook application.
...
@@ -48,75 +42,9 @@ function NotebookIframe({ config, groupId }: NotebookIframeProps) {
...
@@ -48,75 +42,9 @@ function NotebookIframe({ config, groupId }: NotebookIframeProps) {
);
);
}
}
/** Checks if the browser supports native modal dialogs */
function
isModalDialogSupported
(
document
:
Document
)
{
const
dialog
=
document
.
createElement
(
'dialog'
);
return
typeof
dialog
.
showModal
===
'function'
;
}
export
type
NotebookModalProps
=
{
export
type
NotebookModalProps
=
{
eventBus
:
EventBus
;
eventBus
:
EventBus
;
config
:
NotebookConfig
;
config
:
NotebookConfig
;
/** Test seam */
document_
?:
Document
;
};
type
DialogProps
=
{
isHidden
:
boolean
;
children
:
ComponentChildren
;
onClose
:
()
=>
void
;
};
const
NativeDialog
=
({
isHidden
,
children
,
onClose
}:
DialogProps
)
=>
{
const
dialogRef
=
useRef
<
HTMLDialogElement
|
null
>
(
null
);
useEffect
(()
=>
{
if
(
isHidden
)
{
dialogRef
.
current
?.
close
();
}
else
{
dialogRef
.
current
?.
showModal
();
}
},
[
isHidden
]);
useEffect
(()
=>
{
const
dialogElement
=
dialogRef
.
current
;
dialogElement
?.
addEventListener
(
'cancel'
,
onClose
);
return
()
=>
{
dialogElement
?.
removeEventListener
(
'cancel'
,
onClose
);
};
},
[
onClose
]);
return
(
<
dialog
ref=
{
dialogRef
}
className=
"relative w-full h-full backdrop:bg-black/50"
data
-
testid=
"notebook-outer"
>
{
children
}
</
dialog
>
);
};
/**
* Temporary fallback used in browsers not supporting `dialog` element.
* It can be removed once all browsers we support can use it.
*/
const
FallbackDialog
=
({
isHidden
,
children
}:
DialogProps
)
=>
{
return
(
<
div
className=
{
classnames
(
'fixed z-max top-0 left-0 right-0 bottom-0 p-3 bg-black/50'
,
{
hidden
:
isHidden
},
)
}
data
-
testid=
"notebook-outer"
>
<
div
className=
"relative w-full h-full"
data
-
testid=
"notebook-inner"
>
{
children
}
</
div
>
</
div
>
);
};
};
/**
/**
...
@@ -125,8 +53,6 @@ const FallbackDialog = ({ isHidden, children }: DialogProps) => {
...
@@ -125,8 +53,6 @@ const FallbackDialog = ({ isHidden, children }: DialogProps) => {
export
default
function
NotebookModal
({
export
default
function
NotebookModal
({
eventBus
,
eventBus
,
config
,
config
,
/* istanbul ignore next - test seam */
document_
=
document
,
}:
NotebookModalProps
)
{
}:
NotebookModalProps
)
{
// Temporary solution: while there is no mechanism to sync new annotations in
// Temporary solution: while there is no mechanism to sync new annotations in
// the notebook, we force re-rendering of the iframe on every 'openNotebook'
// the notebook, we force re-rendering of the iframe on every 'openNotebook'
...
@@ -138,11 +64,6 @@ export default function NotebookModal({
...
@@ -138,11 +64,6 @@ export default function NotebookModal({
const
originalDocumentOverflowStyle
=
useRef
(
''
);
const
originalDocumentOverflowStyle
=
useRef
(
''
);
const
emitterRef
=
useRef
<
Emitter
|
null
>
(
null
);
const
emitterRef
=
useRef
<
Emitter
|
null
>
(
null
);
const
Dialog
=
useMemo
(
()
=>
(
isModalDialogSupported
(
document_
)
?
NativeDialog
:
FallbackDialog
),
[
document_
],
);
// Stores the original overflow CSS property of document.body and reset it
// Stores the original overflow CSS property of document.body and reset it
// when the component is destroyed
// when the component is destroyed
useEffect
(()
=>
{
useEffect
(()
=>
{
...
@@ -187,7 +108,7 @@ export default function NotebookModal({
...
@@ -187,7 +108,7 @@ export default function NotebookModal({
}
}
return
(
return
(
<
Dialog
isHidden
=
{
isHidden
}
onClose=
{
onClose
}
>
<
ModalDialog
closed
=
{
isHidden
}
onClose=
{
onClose
}
>
<
div
className=
"absolute right-0 m-3"
>
<
div
className=
"absolute right-0 m-3"
>
<
IconButton
<
IconButton
title=
"Close the Notebook"
title=
"Close the Notebook"
...
@@ -206,6 +127,6 @@ export default function NotebookModal({
...
@@ -206,6 +127,6 @@ export default function NotebookModal({
</
IconButton
>
</
IconButton
>
</
div
>
</
div
>
<
NotebookIframe
key=
{
iframeKey
}
config=
{
config
}
groupId=
{
groupId
}
/>
<
NotebookIframe
key=
{
iframeKey
}
config=
{
config
}
groupId=
{
groupId
}
/>
</
Dialog
>
</
Modal
Dialog
>
);
);
}
}
src/annotator/components/test/ModalDialog-test.js
0 → 100644
View file @
78386e60
import
{
mount
}
from
'enzyme'
;
import
ModalDialog
from
'../ModalDialog'
;
describe
(
'ModalDialog'
,
()
=>
{
let
components
;
const
createComponent
=
props
=>
{
const
attachTo
=
document
.
createElement
(
'div'
);
document
.
body
.
appendChild
(
attachTo
);
const
component
=
mount
(
<
ModalDialog
open
{...
props
}
/>,
{
attachTo
,
});
components
.
push
([
component
,
attachTo
]);
return
component
;
};
beforeEach
(()
=>
{
components
=
[];
});
afterEach
(()
=>
{
components
.
forEach
(([
component
,
container
])
=>
{
component
.
unmount
();
container
.
remove
();
});
});
context
(
'when native modal dialog is not supported'
,
()
=>
{
let
fakeDocument
;
beforeEach
(()
=>
{
fakeDocument
=
{
createElement
:
sinon
.
stub
().
returns
({}),
};
});
it
(
'does not render a dialog element'
,
()
=>
{
const
wrapper
=
createComponent
({
document_
:
fakeDocument
});
assert
.
isFalse
(
wrapper
.
exists
(
'dialog'
));
});
});
context
(
'when native modal dialog is supported'
,
()
=>
{
it
(
'renders a dialog element'
,
()
=>
{
const
wrapper
=
createComponent
();
assert
.
isTrue
(
wrapper
.
exists
(
'dialog'
));
});
it
(
'closes native dialog on cancel'
,
()
=>
{
const
onClose
=
sinon
.
stub
();
const
wrapper
=
createComponent
({
onClose
});
wrapper
.
find
(
'dialog'
).
getDOMNode
().
dispatchEvent
(
new
Event
(
'cancel'
));
wrapper
.
update
();
assert
.
called
(
onClose
);
});
});
});
src/annotator/components/test/NotebookModal-test.js
View file @
78386e60
...
@@ -14,7 +14,7 @@ describe('NotebookModal', () => {
...
@@ -14,7 +14,7 @@ describe('NotebookModal', () => {
const
outerSelector
=
'[data-testid="notebook-outer"]'
;
const
outerSelector
=
'[data-testid="notebook-outer"]'
;
const
createComponent
=
(
config
,
fakeDocument
)
=>
{
const
createComponent
=
config
=>
{
const
attachTo
=
document
.
createElement
(
'div'
);
const
attachTo
=
document
.
createElement
(
'div'
);
document
.
body
.
appendChild
(
attachTo
);
document
.
body
.
appendChild
(
attachTo
);
...
@@ -22,7 +22,6 @@ describe('NotebookModal', () => {
...
@@ -22,7 +22,6 @@ describe('NotebookModal', () => {
<
NotebookModal
<
NotebookModal
eventBus
=
{
eventBus
}
eventBus
=
{
eventBus
}
config
=
{{
notebookAppUrl
:
notebookURL
,
...
config
}}
config
=
{{
notebookAppUrl
:
notebookURL
,
...
config
}}
document_
=
{
fakeDocument
}
/>
,
/>
,
{
attachTo
},
{
attachTo
},
);
);
...
@@ -125,74 +124,25 @@ describe('NotebookModal', () => {
...
@@ -125,74 +124,25 @@ describe('NotebookModal', () => {
assert
.
equal
(
document
.
body
.
style
.
overflow
,
'hidden'
);
assert
.
equal
(
document
.
body
.
style
.
overflow
,
'hidden'
);
});
});
context
(
'when native modal dialog is not supported'
,
()
=>
{
[
let
fakeDocument
;
// Close via clicking close button
wrapper
=>
getCloseButton
(
wrapper
).
props
().
onClick
(),
beforeEach
(()
=>
{
// Close via "cancel" event, like pressing `Esc` key
fakeDocument
=
{
wrapper
=>
createElement
:
sinon
.
stub
().
returns
({}),
wrapper
.
find
(
'dialog'
).
getDOMNode
().
dispatchEvent
(
new
Event
(
'cancel'
)),
};
].
forEach
(
closeDialog
=>
{
});
it
(
'opens and closes native dialog'
,
()
=>
{
it
(
'does not render a dialog element'
,
()
=>
{
const
wrapper
=
createComponent
({},
fakeDocument
);
emitter
.
publish
(
'openNotebook'
,
'myGroup'
);
wrapper
.
update
();
assert
.
isFalse
(
wrapper
.
exists
(
'dialog'
));
});
it
(
'hides modal on closing'
,
()
=>
{
const
wrapper
=
createComponent
({},
fakeDocument
);
emitter
.
publish
(
'openNotebook'
,
'myGroup'
);
wrapper
.
update
();
let
outer
=
wrapper
.
find
(
outerSelector
);
assert
.
isFalse
(
outer
.
hasClass
(
'hidden'
));
act
(()
=>
{
getCloseButton
(
wrapper
).
prop
(
'onClick'
)();
});
wrapper
.
update
();
outer
=
wrapper
.
find
(
outerSelector
);
assert
.
isTrue
(
outer
.
hasClass
(
'hidden'
));
});
});
context
(
'when native modal dialog is supported'
,
()
=>
{
it
(
'renders a dialog element'
,
()
=>
{
const
wrapper
=
createComponent
({});
const
wrapper
=
createComponent
({});
const
isDialogOpen
=
()
=>
wrapper
.
find
(
'dialog'
).
getDOMNode
().
open
;
emitter
.
publish
(
'openNotebook'
,
'myGroup'
);
act
(()
=>
emitter
.
publish
(
'openNotebook'
,
'myGroup'
)
);
wrapper
.
update
();
wrapper
.
update
();
assert
.
isTrue
(
isDialogOpen
());
assert
.
isTrue
(
wrapper
.
exists
(
'dialog'
));
act
(()
=>
closeDialog
(
wrapper
));
});
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