Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
de5f5aa0
Commit
de5f5aa0
authored
Feb 23, 2022
by
Simon Knox
Committed by
Paul Slaughter
Feb 23, 2022
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Limit scrollToTargetOnResize to note hashes
-
https://gitlab.com/gitlab-org/gitlab/-/merge_requests/81192
parent
92914b5a
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
43 additions
and
20 deletions
+43
-20
app/assets/javascripts/lib/utils/resize_observer.js
app/assets/javascripts/lib/utils/resize_observer.js
+15
-7
spec/frontend/lib/utils/resize_observer_spec.js
spec/frontend/lib/utils/resize_observer_spec.js
+28
-13
No files found.
app/assets/javascripts/lib/utils/resize_observer.js
View file @
de5f5aa0
...
...
@@ -10,22 +10,30 @@ export function createResizeObserver() {
});
}
// watches for change in size of a container element (e.g. for lazy-loaded images)
// and scroll the target element to the top of the content area
// stop watching after any user input. So if user opens sidebar or manually
// scrolls the page we don't hijack their scroll position
/**
* Watches for change in size of a container element (e.g. for lazy-loaded images)
* and scrolls the target note to the top of the content area.
* Stops watching after any user input. So if user opens sidebar or manually
* scrolls the page we don't hijack their scroll position
*
* @param {Object} options
* @param {string} options.targetId - id of element to scroll to
* @param {string} options.container - Selector of element containing target
*
* @return {ResizeObserver|null} - ResizeObserver instance if target looks like a note DOM ID
*/
export
function
scrollToTargetOnResize
({
target
=
window
.
location
.
hash
,
target
Id
=
window
.
location
.
hash
.
slice
(
1
)
,
container
=
'
#content-body
'
,
}
=
{})
{
if
(
!
target
)
return
null
;
if
(
!
target
Id
)
return
null
;
const
ro
=
createResizeObserver
();
const
containerEl
=
document
.
querySelector
(
container
);
let
interactionListenersAdded
=
false
;
function
keepTargetAtTop
()
{
const
anchorEl
=
document
.
querySelector
(
target
);
const
anchorEl
=
document
.
getElementById
(
targetId
);
if
(
!
anchorEl
)
return
;
...
...
spec/frontend/lib/utils/resize_observer_spec.js
View file @
de5f5aa0
...
...
@@ -19,16 +19,11 @@ describe('ResizeObserver Utility', () => {
jest
.
spyOn
(
document
.
documentElement
,
'
scrollTo
'
);
setFixtures
(
`<div id="content-body"><div
class="target">element
to scroll to</div></div>`
);
setFixtures
(
`<div id="content-body"><div
id="note_1234">note
to scroll to</div></div>`
);
const
target
=
document
.
querySelector
(
'
.target
'
);
const
target
=
document
.
querySelector
(
'
#note_1234
'
);
jest
.
spyOn
(
target
,
'
getBoundingClientRect
'
).
mockReturnValue
({
top
:
200
});
observer
=
scrollToTargetOnResize
({
target
:
'
.target
'
,
container
:
'
#content-body
'
,
});
});
afterEach
(()
=>
{
...
...
@@ -38,21 +33,22 @@ describe('ResizeObserver Utility', () => {
describe
(
'
Observer behavior
'
,
()
=>
{
it
(
'
returns null for empty target
'
,
()
=>
{
observer
=
scrollToTargetOnResize
({
target
:
''
,
target
Id
:
''
,
container
:
'
#content-body
'
,
});
expect
(
observer
).
toBe
(
null
);
});
it
(
'
returns ResizeObserver instance
'
,
()
=>
{
expect
(
observer
).
toBeInstanceOf
(
ResizeObserver
);
});
it
(
'
does not scroll if target does not exist
'
,
()
=>
{
observer
=
scrollToTargetOnResize
({
targetId
:
'
some_imaginary_id
'
,
container
:
'
#content-body
'
,
});
it
(
'
scrolls body so anchor is just below sticky header (contentTop)
'
,
()
=>
{
triggerResize
();
expect
(
document
.
documentElement
.
scrollTo
).
toHaveBeenCalledWith
({
top
:
110
}
);
expect
(
document
.
documentElement
.
scrollTo
).
not
.
toHaveBeenCalled
(
);
});
const
interactionEvents
=
[
'
mousedown
'
,
'
touchstart
'
,
'
keydown
'
,
'
wheel
'
];
...
...
@@ -64,5 +60,24 @@ describe('ResizeObserver Utility', () => {
expect
(
document
.
documentElement
.
scrollTo
).
not
.
toHaveBeenCalledWith
();
});
describe
(
'
with existing target
'
,
()
=>
{
beforeEach
(()
=>
{
observer
=
scrollToTargetOnResize
({
targetId
:
'
note_1234
'
,
container
:
'
#content-body
'
,
});
});
it
(
'
returns ResizeObserver instance
'
,
()
=>
{
expect
(
observer
).
toBeInstanceOf
(
ResizeObserver
);
});
it
(
'
scrolls body so anchor is just below sticky header (contentTop)
'
,
()
=>
{
triggerResize
();
expect
(
document
.
documentElement
.
scrollTo
).
toHaveBeenCalledWith
({
top
:
110
});
});
});
});
});
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