Skip to content

Commit

Permalink
Merge pull request #222 from eecs485staff/fix-copy-btn
Browse files Browse the repository at this point in the history
[Enhanced code blocks] Copy button should not omit lines with no text by default
  • Loading branch information
seshrs authored Nov 9, 2022
2 parents 6c6a378 + fe71f26 commit ffd1019
Showing 1 changed file with 6 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ const CONSOLE_COPY_LINES_MAP_FN = (line: HTMLElement) => {
* map/filter method to extract text from each line.
*
* @param codeblock The codeblock whose lines need to be copied
* @param mapFn (OPTIONAL) A method that extracts text from a given line HTMLElement
* @param mapFn (OPTIONAL) A method that extracts text from a given line
* HTMLElement. If the method returns `null`, the line is
* *omitted* (and a newline will not be copied).
*/
async function copyLines(
codeblock: HTMLElement,
Expand All @@ -97,7 +99,9 @@ async function copyLines(
const lines = codeblock.querySelectorAll(
`.${CODEBLOCK_LINE_CLASS}`,
) as NodeListOf<HTMLElement>;
const linesOfText = [...lines].map((line) => mapFn(line)).filter(Boolean);
const linesOfText = [...lines]
.map((line) => mapFn(line))
.filter((lineText) => lineText != null);
const text = linesOfText.join('\n');
await navigator.clipboard.writeText(text);
}

0 comments on commit ffd1019

Please sign in to comment.