-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3277 from serlo/feat/editor-plugin-copy-tool
feat(editor): mvp for copy&pasting plugins
- Loading branch information
Showing
5 changed files
with
95 additions
and
3 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
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
47 changes: 47 additions & 0 deletions
47
packages/editor/src/editor-ui/plugin-toolbar/plugin-tool-menu/plugin-copy-tool.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,47 @@ | ||
import { selectStaticDocumentWithoutIds, store } from '@editor/store' | ||
import { faCopy } from '@fortawesome/free-solid-svg-icons' | ||
import { shouldUseFeature } from '@serlo/frontend/src/components/user/profile-experimental' | ||
import { useInstanceData } from '@serlo/frontend/src/contexts/instance-context' | ||
import { useEditorStrings } from '@serlo/frontend/src/contexts/logged-in-data-context' | ||
import { showToastNotice } from '@serlo/frontend/src/helper/show-toast-notice' | ||
import { useCallback } from 'react' | ||
|
||
import { DropdownButton } from './dropdown-button' | ||
|
||
interface PluginCopyToolProps { | ||
pluginId: string | ||
} | ||
/** | ||
* experimental plugin to copy plugin's editor state to the clipboard | ||
*/ | ||
export function PluginCopyTool({ pluginId }: PluginCopyToolProps) { | ||
const editorStrings = useEditorStrings() | ||
const { strings } = useInstanceData() | ||
|
||
const handleOnClick = useCallback(() => { | ||
const document = selectStaticDocumentWithoutIds(store.getState(), pluginId) | ||
const rowsDocument = { plugin: 'rows', state: [document] } | ||
|
||
void navigator.clipboard.writeText(JSON.stringify(rowsDocument)) | ||
showToastNotice(strings.share.copySuccess, 'success', 2000) | ||
showToastNotice( | ||
'👉 ' + editorStrings.edtrIo.pluginCopyInfo, | ||
undefined, | ||
4000 | ||
) | ||
}, [pluginId, strings, editorStrings]) | ||
|
||
if (!navigator.clipboard || !shouldUseFeature('editorPluginCopyTool')) { | ||
return null | ||
} | ||
|
||
return ( | ||
<DropdownButton | ||
onClick={handleOnClick} | ||
label={editorStrings.edtrIo.pluginCopyButtonLabel} | ||
icon={faCopy} | ||
className="mt-2.5 border-t pt-2.5" | ||
dataQa="plugin-copy-tool-button" | ||
/> | ||
) | ||
} |
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
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