-
Notifications
You must be signed in to change notification settings - Fork 5
feat: Add option for line-wrapping to prevent horizontal scrolling #20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
59ecbce
9097cfc
6cd99c4
b40b2b0
6b9b6e7
c9dd827
81bc51e
4cd5711
1cdc45f
078fd46
d44f75b
fb13d49
6da84dc
0658716
73a2b33
e1982b3
a6277a0
d0550ce
529aeb4
6741164
4dc0603
d746e42
d3f469e
b015fba
b10273d
ceecca6
f543ac5
323c76f
7929a08
ed856c0
18ce8bb
6e86b8d
e4c46c7
f300bd1
5b7e6e6
713e49a
33411cc
b0659b6
4fc29e2
0dc2c7c
e4b2dae
4d69e81
3b8ad86
3de5396
df9f1b7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| import { SpaceBetween } from "@cloudscape-design/components"; | ||
| import { CodeView } from "../../lib/components"; | ||
| import { ScreenshotArea } from "../screenshot-area"; | ||
| export default function CodeViewPage() { | ||
| return ( | ||
| <ScreenshotArea> | ||
| <h1>Code View</h1> | ||
| <SpaceBetween direction="vertical" size="l"> | ||
| No wrapping, no line numbers | ||
| <CodeView | ||
| content={`Hello this is a short line.\nHello this is a long line. Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.\nHello this is another short line.`} | ||
| /> | ||
| No wrapping, line numbers | ||
| <CodeView | ||
| lineNumbers={true} | ||
| content={`Hello this is a short line.\nHello this is a long line. Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.\nHello this is another short line.`} | ||
| /> | ||
| Wrapping, no line numbers | ||
| <CodeView | ||
| lineWrapping={true} | ||
| content={`Hello this is a short line.\nHello this is a long line. Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.\nHello this is another short line.`} | ||
| /> | ||
| Wrapping, line numbers | ||
| <CodeView | ||
| lineWrapping={true} | ||
| lineNumbers={true} | ||
| content={`Hello this is a short line.\nHello this is a long line. Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.\nHello this is another short line.`} | ||
| /> | ||
| </SpaceBetween> | ||
| </ScreenshotArea> | ||
| ); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,6 +24,13 @@ export interface CodeViewProps { | |
| */ | ||
| lineNumbers?: boolean; | ||
|
|
||
| /** | ||
| * Controls lines wrap when overflowing on the right side. | ||
| * | ||
| * Defaults to `false`. | ||
| */ | ||
| lineWrapping?: boolean; | ||
|
|
||
| /** | ||
| * An optional slot to display a button to enable users to perform actions, such as copy or download the code snippet. | ||
| * | ||
|
|
@@ -34,5 +41,5 @@ export interface CodeViewProps { | |
| * A function to perform custom syntax highlighting. | ||
| * | ||
| */ | ||
| highlight?: (code: string) => React.ReactNode; | ||
| highlight?: (code: string) => React.ReactElement; | ||
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,71 +2,84 @@ | |
| // SPDX-License-Identifier: Apache-2.0 | ||
| import { useCurrentMode } from "@cloudscape-design/component-toolkit/internal"; | ||
| import Box from "@cloudscape-design/components/box"; | ||
| import { TextHighlightRules } from "ace-code/src/mode/text_highlight_rules"; | ||
|
||
| import clsx from "clsx"; | ||
| import { useRef } from "react"; | ||
| import { Children } from "react"; | ||
| import { InternalBaseComponentProps, getBaseProps } from "../internal/base-component/use-base-component"; | ||
| import { createHighlight } from "./highlight"; | ||
| import { CodeViewProps } from "./interfaces"; | ||
| import styles from "./styles.css.js"; | ||
|
|
||
| const ACE_CLASSES = { light: "ace-cloud_editor", dark: "ace-cloud_editor_dark" }; | ||
|
|
||
| function getLineNumbers(content: string) { | ||
| return content.split("\n").map((_, n) => n + 1); | ||
| } | ||
|
|
||
| type InternalCodeViewProps = CodeViewProps & InternalBaseComponentProps; | ||
|
|
||
| const textHighlight = createHighlight(new TextHighlightRules()); | ||
|
|
||
| export function InternalCodeView({ | ||
| content, | ||
| actions, | ||
| lineNumbers, | ||
| lineWrapping, | ||
| highlight, | ||
| ariaLabel, | ||
| ariaLabelledby, | ||
| __internalRootRef = null, | ||
| ...props | ||
| }: InternalCodeViewProps) { | ||
| const code = highlight ? highlight(content) : <span>{content}</span>; | ||
| const baseProps = getBaseProps(props); | ||
| const preRef = useRef<HTMLPreElement>(null); | ||
| const darkMode = useCurrentMode(preRef) === "dark"; | ||
|
|
||
| const regionProps = ariaLabel || ariaLabelledby ? { role: "region" } : {}; | ||
|
|
||
| // Create tokenized elements of the content. | ||
| const code = highlight ? highlight(content) : textHighlight(content); | ||
|
|
||
| return ( | ||
| <div | ||
| className={styles.root} | ||
| className={clsx(darkMode ? ACE_CLASSES.dark : ACE_CLASSES.light, styles.root)} | ||
| {...regionProps} | ||
| {...baseProps} | ||
| aria-label={ariaLabel} | ||
| aria-labelledby={ariaLabelledby} | ||
| ref={__internalRootRef} | ||
| > | ||
| <div className={clsx(lineNumbers && styles["root-with-numbers"], actions && styles["root-with-actions"])}> | ||
| <Box color="text-status-inactive" fontSize="body-m"> | ||
| {lineNumbers && ( | ||
| <div className={styles["line-numbers"]} aria-hidden={true}> | ||
| {getLineNumbers(content).map((number) => ( | ||
| <span key={number}>{number}</span> | ||
| ))} | ||
| </div> | ||
| )} | ||
| </Box> | ||
| <pre | ||
| ref={preRef} | ||
| className={clsx( | ||
| darkMode ? ACE_CLASSES.dark : ACE_CLASSES.light, | ||
| styles.code, | ||
| lineNumbers && styles["code-with-line-numbers"], | ||
| actions && styles["code-with-actions"] | ||
| )} | ||
| > | ||
| <Box color="inherit" variant="code" fontSize="body-m"> | ||
| {code} | ||
| </Box> | ||
| </pre> | ||
| {actions && <div className={styles.actions}>{actions}</div>} | ||
| </div> | ||
| <table className={clsx(styles["code-table"])}> | ||
| <colgroup> | ||
| <col style={{ width: 1 } /* shrink to fit content */} /> | ||
| <col style={{ width: "auto" }} /> | ||
| </colgroup> | ||
| <tbody> | ||
| {Children.map(code.props.children, (child, index) => { | ||
| return ( | ||
| <tr key={index}> | ||
| {lineNumbers && ( | ||
| <td className={styles["line-number"]} aria-hidden={true}> | ||
| <Box color="text-status-inactive" fontSize="body-m"> | ||
| {index + 1} | ||
| </Box> | ||
|
||
| </td> | ||
| )} | ||
| <td className={styles["code-line"]}> | ||
| <Box color="text-status-inactive" variant="code" fontSize="body-m"> | ||
| <span | ||
| className={clsx( | ||
| code.props.className, | ||
| lineWrapping ? styles["code-line-wrap"] : styles["code-line-nowrap"] | ||
| )} | ||
| > | ||
| {child} | ||
| </span> | ||
| </Box> | ||
| </td> | ||
| </tr> | ||
| ); | ||
| })} | ||
| </tbody> | ||
| </table> | ||
| {actions && <div className={styles.actions}>{actions}</div>} | ||
| </div> | ||
| ); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,8 +6,8 @@ import styles from "../../../code-view/styles.selectors.js"; | |
| export default class CodeViewWrapper extends ComponentWrapper { | ||
| static rootSelector: string = styles.root; | ||
|
|
||
| findContent(): ElementWrapper { | ||
| return this.findByClassName(styles.code)!; | ||
| findContent() { | ||
|
||
| return this.findAllByClassName(styles["code-line"])!; | ||
| } | ||
|
|
||
| findActions(): ElementWrapper | null { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please update this page and also
with-syntax-highlighting.page.tsxwith wrapping text content inside SpaceBetween in some container, e.g. a Box? That is to satisfy the guidelines: https://cloudscape.aws.dev/components/space-between/?tabId=apiCurrently, there are React warnings in these pages.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah yeah good point, fixed in new commit