-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Add buttons to download and copy to clipboard content to TextEditor
#37333
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
Merged
mcbattirola
merged 17 commits into
master
from
mcbattirola/download-and-copy-buttons-code-editor
Feb 1, 2024
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
e92ca9a
Add editor buttons
mcbattirola 375309f
Set icon size
mcbattirola 9cb1faa
Merge branch 'master' into mcbattirola/download-and-copy-buttons-code…
mcbattirola 39d2de8
Move downloadObject to OSS
mcbattirola e051084
remove unused test-id
mcbattirola 3f36f27
Use values from theme
mcbattirola c68d992
Fix buttons positioning
mcbattirola a1b6deb
Merge branch 'master' of github.com:gravitational/teleport into mcbat…
mcbattirola d4d99f1
Convert jsx files to tsx
mcbattirola 92a9ccd
Use const instead of var
mcbattirola 9f95636
Add z-index to buttons
mcbattirola 1bad6dd
Merge branch 'master' into mcbattirola/download-and-copy-buttons-code…
mcbattirola 7ed9175
Merge branch 'master' into mcbattirola/download-and-copy-buttons-code…
mcbattirola 659a743
Add license
mcbattirola b00245d
Merge branch 'master' into mcbattirola/download-and-copy-buttons-code…
mcbattirola 8d2a26d
Merge branch 'master' into mcbattirola/download-and-copy-buttons-code…
mcbattirola afe2a63
Merge branch 'master' into mcbattirola/download-and-copy-buttons-code…
mcbattirola File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
web/packages/shared/components/TextEditor/TextEditor.story.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| /* | ||
| * Teleport | ||
| * Copyright (C) 2024 Gravitational, Inc. | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU Affero General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU Affero General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU Affero General Public License | ||
| * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
|
|
||
| import React from 'react'; | ||
|
|
||
| import Flex from 'design/Flex'; | ||
|
|
||
| import TextEditor from './TextEditor'; | ||
|
|
||
| export default { | ||
| title: 'Shared/TextEditor', | ||
| }; | ||
|
|
||
| export const Editor = () => { | ||
| return ( | ||
| <Flex height="600px" width="600px" py={3} pr={3} bg="levels.deep"> | ||
| <TextEditor | ||
| bg="levels.deep" | ||
| data={[ | ||
| { | ||
| content, | ||
| type: 'yaml', | ||
| }, | ||
| ]} | ||
| /> | ||
| </Flex> | ||
| ); | ||
| }; | ||
|
|
||
| export const ReadOnly = () => { | ||
| return ( | ||
| <Flex height="600px" width="600px" py={3} pr={3} bg="levels.deep"> | ||
| <TextEditor | ||
| bg="levels.deep" | ||
| readOnly | ||
| data={[ | ||
| { | ||
| content, | ||
| type: 'yaml', | ||
| }, | ||
| ]} | ||
| /> | ||
| </Flex> | ||
| ); | ||
| }; | ||
|
|
||
| export const WithButtons = () => { | ||
| return ( | ||
| <Flex height="600px" width="600px" py={3} pr={3} bg="levels.deep"> | ||
| <TextEditor | ||
| bg="levels.deep" | ||
| data={[ | ||
| { | ||
| content, | ||
| type: 'yaml', | ||
| }, | ||
| ]} | ||
| copyButton | ||
| downloadButton | ||
| downloadFileName="content.yaml" | ||
| /> | ||
| </Flex> | ||
| ); | ||
| }; | ||
|
|
||
| const content = `# example | ||
| kind: github | ||
| version: v3 | ||
| metadata: | ||
| name: github | ||
| spec: | ||
| client_id: client-id | ||
| client_secret: client-secret | ||
| display: GitHub | ||
| redirect_url: https://tele.example.com:443/v1/webapi/github/callback | ||
| teams_to_roles: | ||
| - organization: octocats | ||
| team: admin | ||
| roles: ["access"] | ||
| `; |
68 changes: 68 additions & 0 deletions
68
web/packages/shared/components/TextEditor/TextEditor.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| /** | ||
| * Teleport | ||
| * Copyright (C) 2024 Gravitational, Inc. | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU Affero General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU Affero General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU Affero General Public License | ||
| * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
|
|
||
| import React from 'react'; | ||
| import { render, userEvent, screen } from 'design/utils/testing'; | ||
| import * as copyModule from 'design/utils/copyToClipboard'; | ||
|
|
||
| import * as downloadsModule from 'shared/utils/download'; | ||
|
|
||
| import TextEditor from '.'; | ||
|
|
||
| describe('textEditor tests', () => { | ||
| afterEach(() => { | ||
| jest.clearAllMocks(); | ||
| }); | ||
|
|
||
| test('copy content button', async () => { | ||
| render( | ||
| <TextEditor | ||
| copyButton | ||
| data={[ | ||
| { | ||
| content: 'my-content', | ||
| type: 'yaml', | ||
| }, | ||
| ]} | ||
| /> | ||
| ); | ||
| const mockedCopyToClipboard = jest.spyOn(copyModule, 'copyToClipboard'); | ||
|
|
||
| await userEvent.click(screen.getByTitle('Copy to clipboard')); | ||
| expect(mockedCopyToClipboard).toHaveBeenCalledWith('my-content'); | ||
| }); | ||
|
|
||
| test('download content button', async () => { | ||
| render( | ||
| <TextEditor | ||
| downloadButton | ||
| downloadFileName="test.yaml" | ||
| data={[ | ||
| { | ||
| content: 'my-content', | ||
| type: 'yaml', | ||
| }, | ||
| ]} | ||
| /> | ||
| ); | ||
| const mockedDownload = jest.spyOn(downloadsModule, 'downloadObject'); | ||
|
|
||
| await userEvent.click(screen.getByTitle('Download')); | ||
| expect(mockedDownload).toHaveBeenCalledWith('test.yaml', 'my-content'); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| /** | ||
| * Teleport | ||
| * Copyright (C) 2024 Gravitational, Inc. | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU Affero General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU Affero General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU Affero General Public License | ||
| * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
|
|
||
| export const downloadObject = (filename: string, text: string) => { | ||
| /* | ||
| * http://stackoverflow.com/questions/3665115/create-a-file-in-memory-for-user-to-download-not-through-server | ||
| */ | ||
| const element = document.createElement('a'); | ||
| element.setAttribute( | ||
| 'href', | ||
| 'data:text/plain;charset=utf-8,' + encodeURIComponent(text) | ||
| ); | ||
| element.setAttribute('download', filename); | ||
| element.style.display = 'none'; | ||
|
|
||
| document.body.appendChild(element); | ||
|
|
||
| element.click(); | ||
| document.body.removeChild(element); | ||
| }; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.