-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add buttons to download and copy to clipboard content to
TextEditor
(…
…#37333) * Add editor buttons * Set icon size * Move downloadObject to OSS * remove unused test-id * Use values from theme * Fix buttons positioning * Convert jsx files to tsx * Use const instead of var * Add z-index to buttons * Add license
- Loading branch information
1 parent
ef1463e
commit 23ea85f
Showing
4 changed files
with
247 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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 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 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 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); | ||
}; |