Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions packages/editor/ProseMirror/utils/jumpToHash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ const jumpToHash = (targetHash: string): Command => (state, dispatch, view) => {
}

let targetHeaderPos: number|null = null;
let targetHeadingNodePos: number|null = null;
forEachHeading(view.state.doc, (node, hash, pos) => {
if (hash === targetHash) {
// Subtract one to move the selection to the end of
// the node:
targetHeaderPos = pos + node.nodeSize - 1;
targetHeadingNodePos = pos;
}

return targetHeaderPos !== null;
Expand All @@ -26,6 +28,14 @@ const jumpToHash = (targetHash: string): Command => (state, dispatch, view) => {
.scrollIntoView(),
);
if (view) {
// Scroll the heading to the top of the viewport, consistent with view
// mode behavior. Uses window.scrollTo to avoid relying on scrollIntoView
// option support across WebView versions.
const headingDom = view.nodeDOM(targetHeadingNodePos);
if (headingDom instanceof Element) {
const rect = headingDom.getBoundingClientRect();
window.scrollTo(0, window.scrollY + rect.top);
}
focus('jumpToHash', view);
}
}
Expand Down
Loading