Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 | 1x 1x 10x 44x 44x 8x 20x 20x 20x 13x 2x 11x 4x 7x 24x 24x 18x 10x 10x 7x 7x 7x 2x 2x 8x 8x 5x 1x 2x 2x 1x 1x 1x 1x 1x 1x 1x 6x 6x 2x 2x 2x 4x 4x 4x 4x 4x 4x 4x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 8x 8x 8x 8x 20x 8x 10x 10x 10x 10x 2x 2x 2x 8x 8x 8x 8x 10x 10x 10x 10x 10x 8x 8x 2x 2x 2x 2x 2x 2x 6x 6x 6x 6x 6x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x | import { useEffect, useRef, type RefObject } from 'react';
const EDITABLE_SELECTOR = 'input, textarea, select, [contenteditable]';
const NON_TEXT_INPUT_TYPES = new Set([
'button',
'checkbox',
'color',
'file',
'hidden',
'image',
'radio',
'range',
'reset',
'submit',
]);
function isSelectAllShortcut(event: KeyboardEvent) {
return (
event.key.toLowerCase() === 'a' &&
(event.metaKey || event.ctrlKey) &&
!event.altKey &&
!event.shiftKey
);
}
function getElementFromTarget(target: EventTarget | Node | null): Element | null {
Iif (!target) return null;
if (target instanceof Element) return target;
Eif (target instanceof Node) return target.parentElement;
return null;
}
function isEditableTarget(target: EventTarget | null) {
const element = getElementFromTarget(target);
const editable = element?.closest(EDITABLE_SELECTOR);
if (!editable) return false;
if (editable instanceof HTMLInputElement) {
return !NON_TEXT_INPUT_TYPES.has(editable.type);
}
if (editable instanceof HTMLElement && editable.hasAttribute('contenteditable')) {
return editable.getAttribute('contenteditable') !== 'false';
}
return true;
}
function findCodeBlock(element: Element | null, container: HTMLElement) {
const block = element?.closest('pre');
if (!(block instanceof HTMLElement)) return null;
return container.contains(block) ? block : null;
}
function findCodeBlockFromSelection(container: HTMLElement) {
const selection = window.getSelection();
if (!selection || selection.rangeCount === 0) return null;
const anchorBlock = findCodeBlock(getElementFromTarget(selection.anchorNode), container);
const focusBlock = findCodeBlock(getElementFromTarget(selection.focusNode), container);
if (anchorBlock && (!focusBlock || focusBlock === anchorBlock)) return anchorBlock;
Iif (focusBlock && selection.isCollapsed) return focusBlock;
return null;
}
function getSelectableCodeElement(block: HTMLElement) {
return block.querySelector('[data-markdown-code-content="true"]') ?? block.querySelector('code') ?? block;
}
function isTextControl(element: Element): element is HTMLInputElement | HTMLTextAreaElement {
if (element instanceof HTMLTextAreaElement) return true;
return element instanceof HTMLInputElement && !NON_TEXT_INPUT_TYPES.has(element.type);
}
function isFullTextControlSelection(element: HTMLInputElement | HTMLTextAreaElement) {
return element.selectionStart === 0 && element.selectionEnd === element.value.length;
}
function isFullCodeBlockSelection(block: HTMLElement) {
const selectable = getSelectableCodeElement(block);
if (isTextControl(selectable)) {
return isFullTextControlSelection(selectable);
}
const selection = window.getSelection();
Iif (!selection || selection.rangeCount !== 1) return false;
const selectedRange = selection.getRangeAt(0);
const targetRange = document.createRange();
targetRange.selectNodeContents(selectable);
return (
selectedRange.compareBoundaryPoints(Range.START_TO_START, targetRange) === 0 &&
selectedRange.compareBoundaryPoints(Range.END_TO_END, targetRange) === 0
);
}
function selectCodeBlockContents(block: HTMLElement) {
const selectable = getSelectableCodeElement(block);
if (isTextControl(selectable)) {
selectable.focus();
selectable.select();
return true;
}
const selection = window.getSelection();
Iif (!selection) return false;
const range = document.createRange();
range.selectNodeContents(selectable);
selection.removeAllRanges();
selection.addRange(range);
return true;
}
function selectContainerContents(container: HTMLElement) {
const activeElement = document.activeElement;
Eif (activeElement instanceof HTMLElement) {
activeElement.blur();
}
const selection = window.getSelection();
Iif (!selection) return false;
const range = document.createRange();
range.selectNodeContents(container);
selection.removeAllRanges();
selection.addRange(range);
return true;
}
export function useCodeBlockSelectAll(containerRef: RefObject<HTMLElement | null>, enabled: boolean) {
const focusedCodeBlockRef = useRef<HTMLElement | null>(null);
const selectedByShortcutRef = useRef<HTMLElement | null>(null);
useEffect(() => {
Iif (!enabled) return undefined;
const getContainer = () => containerRef.current;
const updateFocusedCodeBlock = (target: EventTarget | null) => {
const container = getContainer();
Iif (!container) {
focusedCodeBlockRef.current = null;
selectedByShortcutRef.current = null;
return;
}
const targetBlock = findCodeBlock(getElementFromTarget(target), container);
if (isEditableTarget(target) && !targetBlock) {
focusedCodeBlockRef.current = null;
selectedByShortcutRef.current = null;
return;
}
focusedCodeBlockRef.current = targetBlock;
selectedByShortcutRef.current = null;
};
const handleSelectionChange = () => {
const selectedBlock = selectedByShortcutRef.current;
if (selectedBlock && !isFullCodeBlockSelection(selectedBlock)) {
selectedByShortcutRef.current = null;
}
};
const handleKeyDown = (event: KeyboardEvent) => {
Iif (event.defaultPrevented || !isSelectAllShortcut(event)) return;
const container = getContainer();
Iif (!container) return;
const codeBlock = findCodeBlockFromSelection(container) ?? focusedCodeBlockRef.current;
if (isEditableTarget(event.target) && !codeBlock) return;
Iif (!codeBlock || !container.contains(codeBlock)) return;
if (selectedByShortcutRef.current === codeBlock && isFullCodeBlockSelection(codeBlock)) {
event.preventDefault();
event.stopPropagation();
focusedCodeBlockRef.current = null;
selectedByShortcutRef.current = null;
selectContainerContents(container);
return;
}
event.preventDefault();
event.stopPropagation();
Eif (selectCodeBlockContents(codeBlock)) {
focusedCodeBlockRef.current = codeBlock;
selectedByShortcutRef.current = codeBlock;
}
};
const handlePointerDown = (event: Event) => updateFocusedCodeBlock(event.target);
const handleFocusIn = (event: Event) => updateFocusedCodeBlock(event.target);
document.addEventListener('pointerdown', handlePointerDown, true);
document.addEventListener('focusin', handleFocusIn, true);
document.addEventListener('selectionchange', handleSelectionChange);
document.addEventListener('keydown', handleKeyDown, true);
return () => {
document.removeEventListener('pointerdown', handlePointerDown, true);
document.removeEventListener('focusin', handleFocusIn, true);
document.removeEventListener('selectionchange', handleSelectionChange);
document.removeEventListener('keydown', handleKeyDown, true);
};
}, [containerRef, enabled]);
}
|