Skip to content

Commit

Permalink
Shift+PageUp/Down to select NB output contents (#207962)
Browse files Browse the repository at this point in the history
  • Loading branch information
DonJayamanne authored Mar 17, 2024
1 parent 8c7cf6a commit b216b43
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,7 @@ registerAction2(class extends NotebookCellAction {
NOTEBOOK_EDITOR_FOCUSED,
ContextKeyExpr.has(InputFocusedContextKey),
EditorContextKeys.editorTextFocus,
NOTEBOOK_OUTPUT_FOCUSED.negate(), // Webview handles Shift+PageUp for selection of output contents
),
primary: KeyMod.Shift | KeyCode.PageUp,
weight: NOTEBOOK_EDITOR_WIDGET_ACTION_WEIGHT
Expand Down Expand Up @@ -406,6 +407,7 @@ registerAction2(class extends NotebookCellAction {
NOTEBOOK_EDITOR_FOCUSED,
ContextKeyExpr.has(InputFocusedContextKey),
EditorContextKeys.editorTextFocus,
NOTEBOOK_OUTPUT_FOCUSED.negate(), // Webview handles Shift+PageDown for selection of output contents
),
primary: KeyMod.Shift | KeyCode.PageDown,
weight: NOTEBOOK_EDITOR_WIDGET_ACTION_WEIGHT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,38 @@ async function webviewPreloads(ctx: PreloadContext) {

};

const onPageUpDownSelectionHandler = (e: KeyboardEvent) => {
if (!lastFocusedOutput?.id || !e.shiftKey) {
return;
}
// We want to handle just `Shift + PageUp/PageDown` & `Shift + Cmd + ArrowUp/ArrowDown` (for mac)
if (!(e.code === 'PageUp' || e.code === 'PageDown') && !(e.metaKey && (e.code === 'ArrowDown' || e.code === 'ArrowUp'))) {
return;
}
const outputContainer = window.document.getElementById(lastFocusedOutput.id);
const selection = window.getSelection();
if (!outputContainer || !selection?.anchorNode) {
return;
}

// These should change the scroll position, not adjust the selected cell in the notebook
e.stopPropagation(); // We don't want the notebook to handle this.
e.preventDefault(); // We will handle selection.

const { anchorNode, anchorOffset } = selection;
const range = document.createRange();
if (e.code === 'PageDown' || e.code === 'ArrowDown') {
range.setStart(anchorNode, anchorOffset);
range.setEnd(outputContainer, 1);
}
else {
range.setStart(outputContainer, 0);
range.setEnd(anchorNode, anchorOffset);
}
selection.removeAllRanges();
selection.addRange(range);
};

const handleDataUrl = async (data: string | ArrayBuffer | null, downloadName: string) => {
postNotebookMessage<webviewMessages.IClickedDataUrlMessage>('clicked-data-url', {
data,
Expand All @@ -310,6 +342,7 @@ async function webviewPreloads(ctx: PreloadContext) {
window.document.body.addEventListener('click', handleInnerClick);
window.document.body.addEventListener('focusin', checkOutputInputFocus);
window.document.body.addEventListener('focusout', handleOutputFocusOut);
window.document.body.addEventListener('keydown', onPageUpDownSelectionHandler);

interface RendererContext extends rendererApi.RendererContext<unknown> {
readonly onDidChangeSettings: Event<RenderOptions>;
Expand Down

0 comments on commit b216b43

Please sign in to comment.