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: adds prism editor and implements for sdkdocs #581

Merged
merged 5 commits into from
Apr 13, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
1 change: 1 addition & 0 deletions packages/api-explorer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"@looker/sdk-codegen": "^21.0.11",
"@looker/sdk-rtl": "^21.0.11",
"ace": "^1.3.0",
"prism-react-renderer": "^1.2.0",
"ace-builds": "^1.4.11",
"history": "^4.10.1",
"lodash": "^4.17.19",
Expand Down
8 changes: 6 additions & 2 deletions packages/api-explorer/src/components/DocCode/DocCode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@

import React, { FC, useContext } from 'react'
import AceEditor from 'react-ace'

import { findGenerator } from '@looker/sdk-codegen'
import { SearchContext } from '../../context/search'
import { highlightSourceCode } from './utils'
import { PrismEditor } from './PrismEditor'

// 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 */
Expand All @@ -56,13 +56,15 @@ interface DocCodeProps {
language?: string
fontSize?: number
width?: string
editor?: string
}

export const DocCode: FC<DocCodeProps> = ({
code,
language = 'json',
fontSize = 16,
width = 'auto',
editor = 'ace',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Default to prism?

}) => {
const gen = findGenerator(language)
if (gen) language = gen.language.toLocaleLowerCase()
Expand All @@ -71,7 +73,7 @@ export const DocCode: FC<DocCodeProps> = ({
} = useContext(SearchContext)
const markers = highlightSourceCode(pattern, code)

return (
return editor === 'ace' ? (
<AceEditor
mode={language}
name={language}
Expand All @@ -89,5 +91,7 @@ export const DocCode: FC<DocCodeProps> = ({
useWorker: false,
}}
/>
) : (
<PrismEditor language={language} code={code} pattern={pattern} />
)
}
109 changes: 109 additions & 0 deletions packages/api-explorer/src/components/DocCode/PrismEditor.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*

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, { FC } from 'react'
import styled from 'styled-components'
import Highlight, { defaultProps, Language } from 'prism-react-renderer'
// import { Prism } from "prism-react-renderer"
import dracula from 'prism-react-renderer/themes/dracula'

// (typeof global !== "undefined" ? global : window).Prism = Prism
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. I'm having a hard time finding ApiExplorer's node_modules for where to import this. And TS doesn't seem to like the global addition... Any thoughts?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For some reason we need to do this for kotlin, c#, swift etc support https://www.npmjs.com/package/prism-react-renderer#faq

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not very familiar with how all things works under the hood. Maybe @bryans99 could chime in on this. Could we somehow lazy load the languages based on the user's sdk language preference?

// require("prismjs/components/prism-kotlin")
// require("prismjs/components/prism-csharp")
// require("prismjs/components/prism-swift")

interface PrismEditorProps {
language: string
code: string
pattern: string
}

const Pre = styled.pre`
padding: 1rem;
overflow: auto;
`

const Line = styled.div`
display: table-row;
`

const LineNo = styled.span`
display: table-cell;
text-align: right;
padding-right: 1em;
user-select: none;
opacity: 0.5;
`

const LineContent = styled.span`
display: table-cell;
`

export const PrismEditor: FC<PrismEditorProps> = ({
Copy link
Contributor Author

@noahamac noahamac Apr 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Where to move this? Looking to get those components out and function to a util file. Should be accessible for RunIt as well. Would like to be able to bring into Data Dict and Lookml Diagram at some point too. What's the overhead on making a new /packages/ ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remember that RunIt can be used on its own so this should live in RunIt if it's needed there as we cannot have any @looker/api-explorer dependencies in the @looker/run-it package.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And what if it needs to be used by both ApiExplorer and RunIt? (And DD and Lookml Diagram)? Are there are docs on creating a new packages item? Would this be a good candidate for that?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it needs to be used by both ApiExplorer and RunIt, like in this case, then it should live in RunIt. I don't believe we have any documentation on how to create a new package but I'd be happy to work on that with you

language,
code,
pattern,
}) => {
const getPrismLanguage = (language: string) => {
if (language === 'golang') {
return 'go'
noahamac marked this conversation as resolved.
Show resolved Hide resolved
}
return language as Language
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is ok for now, but we can change the generator language/label to Go in @looker/sdk-codegen. It was changed to Golang to avoid exactly this for Ace :)


return (
<Highlight
{...defaultProps}
code={code}
language={getPrismLanguage(language)}
theme={dracula}
>
{({ className, style, tokens, getLineProps, getTokenProps }) => (
<Pre className={className} style={style}>
{tokens.map((line, i) => (
<Line key={i} {...getLineProps({ line, key: i })}>
<LineNo>{i + 1}</LineNo>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Do we like the line number? Can ditch this

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have a preference

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line number can be useful for people discussing the code. Let's leave it in until we have people asking to remove it. Or we add a toggle/config setting when we start supporting APIX config settings more fully.

<LineContent>
{line.map((token, key) => {
const tokenProps = getTokenProps({ token, key })
const text = tokenProps.children
if (pattern !== '' && text.includes(pattern)) {
Copy link
Contributor Author

@noahamac noahamac Apr 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Search integration feedback. This is obviously unsophisticated, should we fuzzy match? Make case insensitive? Style a smaller token? Is it annoying that the main page zoomstotop? Should I add a prop to disable when searching?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's bad as is. There are times where it highlights the whole line and is a bit confusing but it's not a big deal. I do think we should open an issue to revisit highlighting across the whole app though because some of it got broken in the latest styling round

tokenProps.style = {
...tokenProps.style,
border: '1px yellow solid',
borderRadius: '4px',
}
}
return <span key={key} {...tokenProps} />
})}
</LineContent>
</Line>
))}
</Pre>
)}
</Highlight>
)
}
2 changes: 1 addition & 1 deletion packages/api-explorer/src/components/DocSDKs/DocSDKs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export const DocSDKs: FC<LanguageSDKProps> = ({ api, method, type }) => {
: gen.declareType('', item as IType)
return (
<TabPanel key={language}>
<DocCode language={language} code={code} />
<DocCode language={language} code={code} editor="prism" />
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps the prop should say editor="ace" for places where we are still using ace so that it's easier to find and replace later on. This is completely fine as is though, just throwing a suggestion out there.

</TabPanel>
)
})}
Expand Down