-
Notifications
You must be signed in to change notification settings - Fork 2
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
feat: Add option for line-wrapping to prevent horizontal scrolling #20
base: main
Are you sure you want to change the base?
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
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 |
---|---|---|
|
@@ -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> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changing the markup here breaks selection feature. Line numbers get added into selection. For the reference you can check how it works on Github Screen.Recording.2024-02-25.at.16.11.35.movThere was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ah, yeah that's a good catch, thanks. Added some CSS that should fix this. |
||
</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() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. changing the signature of this is not a backwards compatible change There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated this to be backwards compatible. |
||
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.
Why is this needed?
The original idea of this component to remove ace dependency from the bundle when syntax highlight is not used.
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.
I needed something to go from
string
toReactElement
for non-highlighted code, for this draft/POC I used the texthighlight rules from Ace for that because it's quick to grab. I can write a simple function within this repo to do the same thing before merging if we decide this approach for line-wrapping looks good.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.
Thanks for the info. Let's ensure it gets removed at the final stage
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.
Removed it and replaced it with a function defined in the PR.