Skip to content
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 code view component #5

Merged
merged 48 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
03a7ba0
feat: Add Code View component
Dec 13, 2023
dcbd4f2
chore: Update Contributing
Dec 14, 2023
37bd5c4
chore: Update README
Dec 14, 2023
6d0a925
chore: Update bug report template
Dec 14, 2023
a1b10aa
chore: Hash ace classes
Dec 14, 2023
789887c
chore: Remove visual regression workflow
Dec 14, 2023
e5cc70f
chore: Update test to use toHaveTextContent
Dec 14, 2023
db847bb
chore: Fix docs script and add tsconfig to ignore highlight paths
Dec 14, 2023
7bc07d7
chore: Update styles and pages
Dec 14, 2023
3641c20
chore: Update exports
Dec 14, 2023
34f5367
feat: Add dev pages
Dec 15, 2023
e6f6672
chore: Remove Box and fix styling and highlighting imports
Dec 18, 2023
2f78dcd
chore: Remove Box and fix styling and highlighting imports
Dec 18, 2023
5bef138
chore: Use node module resolution for dev pages
Dec 18, 2023
e950007
chore: Use node module resolution for dev pages
Dec 18, 2023
e0c2b91
chore: Create variables for background and add dark mode class
Dec 19, 2023
4152192
Merge pull request #7 from cloudscape-design/new-demos
Dec 19, 2023
3c59653
chore: Add dark mode selector
Dec 19, 2023
4014695
chore: Fix selector
Dec 19, 2023
1a2d171
chore: Simplify test-util
Dec 19, 2023
661eae3
chore: Merge branch 'main' into new-code-view
Dec 19, 2023
b051aaf
chore: Center line numbers
Dec 19, 2023
46831df
chore: Vend tokenize function
Dec 20, 2023
698adf3
chore: Address PR feedback
Dec 20, 2023
a9ea9e2
chore: Use spacing design tokens
Dec 20, 2023
e2499d8
chore: Add stylelint to project and use logical props
Dec 20, 2023
3a44328
Merge branch 'main' into new-code-view
Dec 20, 2023
011f133
chore: Fix package-lock
Dec 20, 2023
9cc9ee3
feat: Change copyButton to actions
Dec 20, 2023
39a366e
chore: Improve demo pages
Dec 20, 2023
b52d3c1
chore: Fix border-radius styling
Dec 20, 2023
3e48ab8
chore: Fix styling and update demos
Dec 20, 2023
763944d
chore: No bundling of ace themes
Jan 3, 2024
3a70032
chore: Add SDK languages
Jan 3, 2024
2d75bce
chore: Add exports for highligthing languages
Jan 3, 2024
5bee5b0
chore: Fix test-utils and colors
Jan 3, 2024
84001e4
chore: Update test util
Jan 4, 2024
fec82c7
chore: Add JSX highlighting
Jan 4, 2024
bcaa28b
chore: Add aria-label interfaces
Jan 10, 2024
73d9717
chore: Update line numbers color
Jan 10, 2024
fd34b7e
chore: Update Box position
Jan 11, 2024
2548d5a
chore: Update scripts
Jan 11, 2024
8e03503
chore: Update vitest dependencies
Jan 11, 2024
5640d22
chore: Update package lock
Jan 11, 2024
faa966d
chore: Add aria labels and fix type name
Jan 15, 2024
ea3f443
chore: Remove ABAP language
Jan 16, 2024
23247e9
Merge branch 'main' into new-code-view
Jan 16, 2024
4fead42
chore: Remove unecessary main
Jan 17, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .stylelintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"extends": ["stylelint-config-recommended-scss"],
"rules": {
"property-disallowed-list": ["border", "border-radius", "border-style", "margin", "padding"],
"scss/comment-no-empty": null,
"selector-pseudo-class-no-unknown": [true, { "ignorePseudoClasses": ["global"] }],
"selector-max-type": [0, { "ignore": ["compounded"] }],
"selector-max-universal": [0],
"order/order": ["rules"]
},
"plugins": ["stylelint-use-logical", "stylelint-order"]
}
24,926 changes: 7,609 additions & 17,317 deletions package-lock.json
rubencarvalho marked this conversation as resolved.
Show resolved Hide resolved

Large diffs are not rendered by default.

43 changes: 30 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
"scripts": {
"prebuild": "rm -rf lib dist .cache",
"build": "npm-run-all build:pkg --parallel build:src:* --parallel build:pages:*",
"lint": "eslint --ignore-path .gitignore --ext ts,tsx,js .",
"lint": "eslint --ignore-path .gitignore --ext ts,tsx,js . && stylelint --ignore-path .gitignore '{src,pages}/**/*.{css,scss}'",
"prepare": "husky install",
"test:unit": "vitest run --config vite.unit.config.js",
"test:unit": "vitest run --config vite.unit.config.mjs",
"test:visual": "run-p -r preview test:visual:vitest",
"test:visual:vitest": "vitest run --config vite.e2e-visual.config.js",
"test:visual:vitest": "vitest run --config vite.e2e-visual.config.mjs",
"test:visual:update": "UPDATE_SCREENSHOTS=true npm run test:visual",
"pretest": "tsc -p tsconfig.unit.json && tsc -p tsconfig.e2e.json",
"test": "run-s lint test:unit",
Expand All @@ -23,7 +23,6 @@
"start:watch:ts": "tsc --watch",
"start:watch:css": "chokidar \"./src/**/*.scss\" -c \"npm run build:src:css\"",
"build:pkg": "node scripts/package-json.js",
"build:src:ace": "vite build --config vite.ace.config.js",
"build:src:js": "tsc -p tsconfig.json",
"build:src:css": "node scripts/compile-styles.js",
"build:src:test-utils": "node scripts/test-utils.js",
Expand All @@ -37,15 +36,24 @@
".": "./index.js",
"./code-view": "./code-view/index.js",
"./highlight": "./code-view/highlight/index.js",
"./highlight/abap": "./code-view/highlight/abap.js",
rubencarvalho marked this conversation as resolved.
Show resolved Hide resolved
"./highlight/cpp": "./code-view/highlight/cpp.js",
"./highlight/csharp": "./code-view/highlight/csharp.js",
"./highlight/css": "./code-view/highlight/css.js",
"./highlight/go": "./code-view/highlight/go.js",
"./highlight/html": "./code-view/highlight/html.js",
"./highlight/java": "./code-view/highlight/java.js",
"./highlight/javascript": "./code-view/highlight/javascript.js",
"./highlight/json": "./code-view/highlight/json.js",
"./highlight/jsx": "./code-view/highlight/jsx.js",
"./highlight/kotlin": "./code-view/highlight/kotlin.js",
"./highlight/markdown": "./code-view/highlight/markdown.js",
"./highlight/php": "./code-view/highlight/php.js",
"./highlight/python": "./code-view/highlight/python.js",
"./highlight/ruby": "./code-view/highlight/ruby.js",
"./highlight/rust": "./code-view/highlight/rust.js",
"./highlight/sh": "./code-view/highlight/sh.js",
"./highlight/swift": "./code-view/highlight/swift.js",
"./highlight/typescript": "./code-view/highlight/typescript.js",
"./highlight/xml": "./code-view/highlight/xml.js",
"./highlight/yaml": "./code-view/highlight/yaml.js",
Expand All @@ -54,7 +62,7 @@
"./test-utils/selectors": "./test-utils/selectors/index.js"
},
"dependencies": {
"ace-code": "^1.32.1",
"ace-code": "^1.32.3",
"clsx": "^1.2.1"
},
"peerDependencies": {
Expand All @@ -74,13 +82,13 @@
"@testing-library/react": "^13.4.0",
"@types/jest-image-snapshot": "^6.1.0",
"@types/lodash": "^4.14.191",
"@types/node": "^16.0.0",
"@types/node": "^18.19.4",
"@types/react": "^18.0.26",
"@types/react-dom": "^18.0.10",
"@typescript-eslint/eslint-plugin": "^5.48.0",
"@typescript-eslint/parser": "^5.48.0",
"@vitejs/plugin-react": "^3.0.0",
"@vitest/coverage-istanbul": "^0.28.3",
"@vitejs/plugin-react": "^4.2.1",
"@vitest/coverage-istanbul": "^1.1.1",
"chokidar-cli": "^3.0.0",
"eslint": "^8.31.0",
"eslint-config-prettier": "^8.6.0",
Expand All @@ -104,18 +112,27 @@
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-router-dom": "^6.6.1",
"typescript": "^4.9.4",
"vite": "^4.0.5",
"vitest": "^0.34.6"
"stylelint": "^15.11.0",
"stylelint-config-recommended-scss": "^12.0.0",
"stylelint-order": "^6.0.3",
"stylelint-prettier": "^2.0.0",
"stylelint-scss": "^5.0.1",
"stylelint-use-logical": "^2.1.0",
"typescript": "4.9.4",
"vite": "^5.0.10",
"vitest": "^1.1.1"
},
"//": "ensure that typedoc uses latest typescript. It prints a warning, but works",
"//": "ensure that typedoc uses typescript 4.9.4. It prints a warning, but works",
"overrides": {
"typescript": "^4.9.4"
"typescript": "4.9.4"
},
"lint-staged": {
"*.{js,jsx,ts,tsx}": [
"eslint --fix"
],
"*.{scss,css}": [
"stylelint --fix"
],
"package-lock.json": [
"./scripts/prepare-package-lock.js"
]
Expand Down
3 changes: 2 additions & 1 deletion pages/app/page-layout.module.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.content {
padding: 20px;
padding-block: 20px;
padding-inline: 20px;
}
18 changes: 14 additions & 4 deletions pages/app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import { SpaceBetween, Toggle } from "@cloudscape-design/components";
import { Mode, applyMode } from "@cloudscape-design/global-styles";
import { Density, Mode, applyDensity, applyMode } from "@cloudscape-design/global-styles";
import { Suspense } from "react";
import { useEffect, useState } from "react";
import { pagesMap } from "../pages";
Expand All @@ -14,19 +14,29 @@ export interface PageProps {
export default function Page({ pageId }: PageProps) {
const Component = pagesMap[pageId];
const [dark, setDark] = useState(false);
const [compact, setCompact] = useState(false);

useEffect(() => {
applyMode(dark ? Mode.Dark : Mode.Light, document.documentElement);
}, [dark]);

useEffect(() => {
applyDensity(compact ? Density.Compact : Density.Comfortable, document.documentElement);
}, [compact]);

return (
<PageLayout>
<Suspense fallback="Loading">
<SpaceBetween direction="vertical" size="m">
<a href="/index.html#">Back to all pages</a>
<Toggle checked={dark} onChange={(event) => setDark(event.detail.checked)}>
Dark mode
</Toggle>
<SpaceBetween direction="horizontal" size="m">
<Toggle checked={dark} onChange={(event) => setDark(event.detail.checked)}>
Dark mode
</Toggle>
<Toggle checked={compact} onChange={(event) => setCompact(event.detail.checked)}>
Compact mode
</Toggle>
</SpaceBetween>
</SpaceBetween>
<Component />
</Suspense>
Expand Down
22 changes: 22 additions & 0 deletions pages/code-view/with-actions-button.page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import { Button, 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>
<main>
<SpaceBetween direction="vertical" size="l">
<CodeView actions={<Button iconName="copy"></Button>} content={`Lorem Ipsum `} />
rubencarvalho marked this conversation as resolved.
Show resolved Hide resolved
<CodeView
actions={<Button iconName="copy"></Button>}
content={`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.`}
/>
</SpaceBetween>
</main>
</ScreenshotArea>
);
}
3 changes: 2 additions & 1 deletion pages/code-view/with-all-features.page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ export default function CodeViewPage() {
<h1>Code View</h1>
<main>
<CodeView
copyButton={<Button>Copy</Button>}
ariaLabel="Code snippet with all features enabled"
actions={<Button>Copy</Button>}
content={`const hello = "np";\nconsole.log('Hello')\nfunction hello() {}\nthrow new Error()`}
lineNumbers={true}
highlight={javascriptHighlight}
Expand Down
19 changes: 0 additions & 19 deletions pages/code-view/with-copy-button.page.tsx

This file was deleted.

19 changes: 0 additions & 19 deletions pages/code-view/with-highlight.page.tsx

This file was deleted.

11 changes: 5 additions & 6 deletions pages/code-view/with-line-numbers.page.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// 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";
Expand All @@ -9,12 +10,10 @@ export default function CodeViewPage() {
<ScreenshotArea>
<h1>Code View</h1>
<main>
<CodeView
content={`Hello
World
!`}
lineNumbers={true}
/>
<SpaceBetween direction="vertical" size="l">
<CodeView lineNumbers={true} content={`# Hello World`} />
<CodeView lineNumbers={true} content={`# Hello World\n\nThis is Cloudscape.`} />
</SpaceBetween>
</main>
</ScreenshotArea>
);
Expand Down
57 changes: 57 additions & 0 deletions pages/code-view/with-syntax-highlighting.page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// 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 cssHighlight from "../../lib/components/code-view/highlight/css";
import htmlHighlight from "../../lib/components/code-view/highlight/html";
import javaHighlight from "../../lib/components/code-view/highlight/java";
import javascriptHighlight from "../../lib/components/code-view/highlight/javascript";
import jsonHighlight from "../../lib/components/code-view/highlight/json";
import markdownHighlight from "../../lib/components/code-view/highlight/markdown";
import phpHighlight from "../../lib/components/code-view/highlight/php";
import pythonHighlight from "../../lib/components/code-view/highlight/python";
import shHighlight from "../../lib/components/code-view/highlight/sh";
import typescriptHighlight from "../../lib/components/code-view/highlight/typescript";
import xmlHighlight from "../../lib/components/code-view/highlight/xml";
import yamlHighlight from "../../lib/components/code-view/highlight/yaml";

import { ScreenshotArea } from "../screenshot-area";
export default function CodeViewPage() {
return (
<ScreenshotArea>
<h1>Code View</h1>
<main>
<SpaceBetween direction="vertical" size="l">
JavaScript
<CodeView content={`const hello = "world";\nconsole.log(hello);`} highlight={javascriptHighlight} />
TypeScript
<CodeView content={`let hello: string = "world";\nconsole.log(hello);`} highlight={typescriptHighlight} />
XML
<CodeView content={`<greeting>Hello, world!</greeting>`} highlight={xmlHighlight} />
Markdown (MDX)
<CodeView content={`# Hello World\n\nThis is a markdown example.`} highlight={markdownHighlight} />
Bash (Shell Script)
<CodeView content={`echo "Hello, world!"`} highlight={shHighlight} />
CSS
<CodeView content={`body { background-color: lightblue; }`} highlight={cssHighlight} />
HTML
<CodeView content={`<h1>Hello, World!</h1>`} highlight={htmlHighlight} />
Java
<CodeView
content={`public class HelloWorld {\n public static void main(String[] args) {\n System.out.println("Hello, World!");\n }\n}`}
highlight={javaHighlight}
/>
JSON
<CodeView content={`{"greeting": "Hello, world!"}`} highlight={jsonHighlight} />
PHP
<CodeView content={`<?php\necho 'Hello, world!';\n?>`} highlight={phpHighlight} />
Python
<CodeView content={`print("Hello, World!")`} highlight={pythonHighlight} />
YAML
<CodeView content={`greeting: Hello, world!`} highlight={yamlHighlight} />
</SpaceBetween>
</main>
</ScreenshotArea>
);
}
14 changes: 8 additions & 6 deletions scripts/compile-styles.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
// SPDX-License-Identifier: Apache-2.0
import { ThemeBuilder, buildThemedComponentsInternal } from "@cloudscape-design/theming-build";

// Generates scoped styles for the component
// The output includes files like 'styles.css.js', 'styles.scoped.css', and 'styles.selectors.js'
await buildThemedComponentsInternal({
primary: new ThemeBuilder("unused", ":root", []).build(),
componentsOutputDir: "lib/components",
skip: ["design-tokens", "preset"],
scssDir: "src",
variablesMap: {},
exposed: [],
primary: new ThemeBuilder("unused", ":root", []).build(), // "unused" as placeholder and ":root" for global style
componentsOutputDir: "lib/components", // sets the output directory for the processed styles
skip: ["design-tokens", "preset"], // skips generation of design tokens and presets
scssDir: "src", // sets the source directory for SCSS files
variablesMap: {}, // not needed - can be used for custom token-variable mappings
exposed: [], // not needed - can be used for exposing specific design tokens
});
2 changes: 1 addition & 1 deletion scripts/environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ writeFile(`${basePath}.json`, JSON.stringify(values, null, 2));
writeFile(
`${basePath}.js`,
Object.entries(values)
.map(([key, value]) => `export var ${key} = ${JSON.stringify(value)};`)
.map(([key, value]) => `export const ${key} = ${JSON.stringify(value)};`)
.join("\n")
);
writeFile(
Expand Down
32 changes: 12 additions & 20 deletions scripts/prepare-package-lock.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,20 @@ const require = createRequire(import.meta.url);
const filename = require.resolve("../package-lock.json");
const packageLock = require(filename);

if (packageLock.lockfileVersion !== 2) {
throw new Error('package-lock.json must have "lockfileVersion": 2');
if (packageLock.lockfileVersion !== 3) {
throw Error("package-lock.json file is not version 3. Use regular npm to update the packages.");
}

function unlock(packages) {
Object.keys(packages).forEach((dependencyName) => {
const dependency = packages[dependencyName];

if (dependencyName.includes("@cloudscape-design/")) {
delete packages[dependencyName];
} else if (dependency.resolved && dependency.resolved.includes("codeartifact.us-west-2.amazonaws.com")) {
throw Error(
"package-lock.json file contains a reference to CodeArtifact. Use regular npm to update the packages."
);
}
});

return packages;
}

packageLock.packages = unlock(packageLock.packages);
packageLock.dependencies = unlock(packageLock.dependencies);
Object.keys(packageLock.packages).forEach((dependencyName) => {
const dependency = packageLock.packages[dependencyName];
if (dependencyName.includes("@cloudscape-design/")) {
delete packageLock.packages[dependencyName];
} else if (dependency.resolved && dependency.resolved.indexOf("codeartifact.us-west-2.amazonaws.com") !== -1) {
throw Error(
`package-lock.json file contains a reference to CodeArtifact at ${dependencyName}. Use regular npm to update the packages.`
);
}
});

fs.writeFileSync(filename, JSON.stringify(packageLock, null, 2) + "\n");
console.log("Removed @cloudscape-design/ dependencies from package-lock file");
Loading