Skip to content

Commit

Permalink
feat: adds editor functionality to CodeDisplay (#615)
Browse files Browse the repository at this point in the history
* adds basic editor

* adds editor styling progress

* removes type change

* updates components

* adds revisions

* removes unused prop

* removes Span for span

* remove unneeded reference to older version of @looker/components-test-utils

* cleans up @looker/code-editor

* flips transparent condition

* replaces old name

* fixes package.json

Co-authored-by: Bryn Ryans <bryn.ryans@looker.com>
  • Loading branch information
noahamac and bryans99 authored Apr 21, 2021
1 parent 3674b2a commit b256d14
Show file tree
Hide file tree
Showing 26 changed files with 230 additions and 718 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"register": "SUPPRESS_NO_CONFIG_WARNING=1 ts-node -O '{ \"module\": \"commonjs\", \"target\": \"es2019\" }' packages/sdk-codegen-scripts/scripts/register.ts",
"test": "yarn jest",
"test:ts": "yarn jest --reporters=default --reporters=jest-junit",
"test:apix": "yarn jest packages/api-explorer packages/run-it",
"test:apix": "yarn jest packages/api-explorer packages/run-it packages/code-editor",
"test:iphone": "xcodebuild test -project swift/looker/looker.xcodeproj -scheme looker-Package -sdk iphonesimulator -destination 'platform=iOS Simulator,name=iPhone 11,OS=13.4.1' | xcpretty --test --color",
"test:gen": "yarn jest packages/sdk-codegen",
"test:sdk": "yarn jest packages/sdk",
Expand Down
5 changes: 1 addition & 4 deletions packages/api-explorer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,10 @@
"@looker/sdk": "^21.4.1",
"@looker/sdk-codegen": "^21.0.13",
"@looker/sdk-rtl": "^21.0.12",
"ace": "^1.3.0",
"@looker/code-display": "^0.0.1",
"ace-builds": "^1.4.11",
"@looker/code-editor": "^0.0.1",
"history": "^4.10.1",
"lodash": "^4.17.19",
"react": "^16.13.1",
"react-ace": "^9.2.0",
"react-diff-viewer": "^3.1.1",
"react-dom": "^16.13.1",
"react-is": "^16.13.1",
Expand Down
58 changes: 3 additions & 55 deletions packages/api-explorer/src/components/DocCode/DocCode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,73 +25,21 @@
*/

import React, { FC, useContext } from 'react'
import AceEditor from 'react-ace'
import { findGenerator } from '@looker/sdk-codegen'
import { CodeDisplay } from '@looker/code-display'
import { CodeDisplay } from '@looker/code-editor'
import { SearchContext } from '../../context/search'
import { highlightSourceCode } from './utils'

// TODO Use webpack resolver instead.
/* eslint-disable @typescript-eslint/ban-ts-comment,@typescript-eslint/no-unused-vars,@typescript-eslint/no-var-requires,import/no-extraneous-dependencies */
// @ts-ignore
const python = require('ace-builds/src-noconflict/mode-python')
// @ts-ignore
const typescript = require('ace-builds/src-noconflict/mode-typescript')
// @ts-ignore
const kotlin = require('ace-builds/src-noconflict/mode-kotlin')
// @ts-ignore
const swift = require('ace-builds/src-noconflict/mode-swift')
// @ts-ignore
const csharp = require('ace-builds/src-noconflict/mode-csharp')
// @ts-ignore
const json = require('ace-builds/src-noconflict/mode-json')
// @ts-ignore
const theme = require('ace-builds/src-noconflict/theme-dracula')
// @ts-ignore
const golang = require('ace-builds/src-noconflict/mode-golang')
/* eslint-enable @typescript-eslint/ban-ts-comment,@typescript-eslint/no-unused-vars,@typescript-eslint/no-var-requires,import/no-extraneous-dependencies */

interface DocCodeProps {
code: string
language?: string
fontSize?: number
width?: string
editor?: string
}

export const DocCode: FC<DocCodeProps> = ({
code,
language = 'json',
fontSize = 16,
width = 'auto',
editor = 'prism',
}) => {
export const DocCode: FC<DocCodeProps> = ({ code, language = 'json' }) => {
const gen = findGenerator(language)
if (gen) language = gen.language.toLocaleLowerCase()
const {
searchSettings: { pattern },
} = useContext(SearchContext)
const markers = highlightSourceCode(pattern, code)

return editor === 'ace' ? (
<AceEditor
mode={language}
name={language}
fontSize={fontSize}
readOnly={true}
showPrintMargin={false}
showGutter={false}
tabSize={2}
width={width}
theme={'dracula'}
value={code}
wrapEnabled={true}
markers={markers}
setOptions={{
useWorker: false,
}}
/>
) : (
<CodeDisplay language={language} code={code} pattern={pattern} />
)
return <CodeDisplay language={language} code={code} pattern={pattern} />
}
61 changes: 0 additions & 61 deletions packages/api-explorer/src/components/DocCode/utils.spec.ts

This file was deleted.

File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@looker/code-display",
"name": "@looker/code-editor",
"version": "0.0.1",
"description": "A syntax highlighter for Looker apps.",
"description": "A syntax highlighter Viewer and Editor for Looker SDK supported languages.",
"main": "lib/esm/index.js",
"typings": "lib/index.d.ts",
"files": [
Expand All @@ -13,17 +13,18 @@
"repository": {
"type": "git",
"url": "git+https://github.com/looker-open-source/sdk-codegen.git",
"directory": "packages/code-display"
"directory": "packages/code-editor"
},
"homepage": "https://github.com/looker-open-source/sdk-codegen/tree/main/packages/code-display",
"homepage": "https://github.com/looker-open-source/sdk-codegen/tree/main/packages/code-editor",
"bugs": {
"url": "https://github.com/looker-open-source/sdk-codegen/issues"
},
"scripts": {
"test": "jest",
"watch": "yarn lerna exec --scope @looker/code-display --stream 'BABEL_ENV=build babel src --root-mode upward --out-dir lib/esm --source-maps --extensions .ts,.tsx --no-comments --watch'"
"watch": "yarn lerna exec --scope @looker/code-editor --stream 'BABEL_ENV=build babel src --root-mode upward --out-dir lib/esm --source-maps --extensions .ts,.tsx --no-comments --watch'"
},
"devDependencies": {
"@looker/components-test-utils": "^1.1.3",
"@testing-library/jest-dom": "^5.11.6",
"@testing-library/react": "^11.2.2",
"@testing-library/user-event": "^12.6.0",
Expand All @@ -45,6 +46,8 @@
"prism-react-renderer": "^1.2.0",
"react": "^16.13.1",
"react-dom": "^16.13.1",
"styled-components": "^5.2.1"
"react-is": "^16.13.1",
"styled-components": "^5.2.1",
"react-simple-code-editor": "^0.11.0"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,25 @@ import { getPrismLanguage, getOverridenTheme } from './utils'
// require("prismjs/components/prism-csharp")
// require("prismjs/components/prism-swift")

interface PrismEditorProps {
/** SDK programming language */
language?: string
export interface CodeDisplayProps {
/** Code blob to be highlighted */
code: string
/** Pattern to be search (if applicable) */
/** SDK programming language to syntax highlight */
language?: string
/** Search pattern to be marked */
pattern?: string
/** Flag to provide background or not */
transparent?: boolean
}

const Pre = styled.pre`
padding: 1rem;
overflow: auto;
white-space: pre-wrap;
overflow: auto;
// override default margin for Pre
// so we can set from parent
margin: 0px;
// selector for search matches
.match {
border: 1px yellow solid;
border-radius: 4px;
Expand All @@ -74,22 +79,21 @@ const LineContent = styled(Span)`
`

/**
* The PrismEditor provides a useful implementation of prism-react-renderer
* for Looker apps. Syntax highlighting is available for all supported SDK languages.
* Provides a view-only syntax highlighter for all supported SDK languages.
* TODO: LookML syntax highlighting
*/
export const CodeDisplay: FC<PrismEditorProps> = ({
export const CodeDisplay: FC<CodeDisplayProps> = ({
language = 'json',
code,
pattern = '',
transparent = false,
}) => {
const highlighterLang = getPrismLanguage(language)
return (
<Highlight
{...defaultProps}
code={code}
language={highlighterLang}
theme={getOverridenTheme()}
language={getPrismLanguage(language)}
theme={getOverridenTheme(transparent)}
>
{({ className, style, tokens, getLineProps, getTokenProps }) => (
<Pre className={className} style={style}>
Expand Down
64 changes: 64 additions & 0 deletions packages/code-editor/src/CodeEditor.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
MIT License
Copyright (c) 2021 Looker Data Sciences, Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
import React from 'react'
import { renderWithTheme } from '@looker/components-test-utils'
import userEvent from '@testing-library/user-event'
import { screen } from '@testing-library/react'

import { CodeEditor } from './CodeEditor'
import { pythonTestCode } from './test-data'

describe('CodeEditor', () => {
const setState = jest.fn()
const useStateSpy = jest.spyOn(React, 'useState')
useStateSpy.mockImplementation(() => [pythonTestCode, setState])

test('it syntax highlights', () => {
renderWithTheme(
<CodeEditor onChange={setState} code={pythonTestCode} language="python" />
)
expect(screen.getByText('all_lookml_models').closest('span')).toHaveClass(
'function'
)
expect(screen.getByText('def').closest('span')).toHaveClass('keyword')
expect(
screen
.getByText('# GET /lookml_models -> Sequence[models.LookmlModel]')
.closest('span')
).toHaveClass('comment')
})
test('it is edittable', () => {
renderWithTheme(
<CodeEditor onChange={setState} code={pythonTestCode} language="python" />
)
const editPattern = '# This is the new code'
const input = screen
.getByRole('code-editor')
.getElementsByClassName('npm__react-simple-code-editor__textarea')[0]
userEvent.paste(input, editPattern)
expect(setState).toHaveBeenCalledWith(pythonTestCode + editPattern)
})
})
Loading

0 comments on commit b256d14

Please sign in to comment.