-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Update TextEditor.tsx * Update TextEditor.tsx * Create CodeBlockWithCopy.tsx * Update useTextEditor.ts * Create Check.tsx * Create Copy.tsx * Update index.css * Update types.d.ts * Update README.md * Update README.md
- Loading branch information
1 parent
b127201
commit f255f66
Showing
7 changed files
with
162 additions
and
31 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/** | ||
* This file defines a custom CodeBlockWithCopy component for use with the TipTap editor. | ||
* It includes a button to copy the code block content to the clipboard. | ||
* The component uses lowlight for syntax highlighting and integrates with TipTap's NodeViewRenderer. | ||
*/ | ||
|
||
import { NodeViewWrapper, NodeViewContent, ReactNodeViewRenderer } from '@tiptap/react'; | ||
import { useState } from 'react'; | ||
import { createLowlight, common } from "lowlight"; | ||
import CodeBlockLowlight from '@tiptap/extension-code-block-lowlight'; | ||
import { CodeBlockWithCopyProps } from '../types'; | ||
import Copy from '../icons/Copy'; | ||
import Check from '../icons/Check'; | ||
|
||
const CodeBlockWithCopy = ({ node }: any) => { | ||
const [copied, setCopied] = useState(false); | ||
|
||
|
||
const copyToClipboard = () => { | ||
navigator.clipboard.writeText(node.textContent).then(() => { | ||
setCopied(true); | ||
setTimeout(() => setCopied(false), 2000); // "Copied!" message for 2 seconds | ||
}); | ||
}; | ||
|
||
return ( | ||
<NodeViewWrapper className="code-block-root"> | ||
<button onClick={copyToClipboard}> | ||
{copied ? <Check /> : <Copy />} | ||
</button> | ||
<pre> | ||
<NodeViewContent as="code" /> | ||
</pre> | ||
</NodeViewWrapper> | ||
); | ||
}; | ||
|
||
export const getCodeBlockWithCopy = (props?: CodeBlockWithCopyProps) => { | ||
const { language = 'javascript', className } = props || {}; | ||
|
||
return CodeBlockLowlight | ||
.extend({ | ||
addNodeView() { | ||
// Use ReactNodeViewRenderer to render the CodeBlockWithCopy component | ||
return ReactNodeViewRenderer( | ||
(props: any) => <CodeBlockWithCopy {...props} />, | ||
{ className } | ||
); | ||
}, | ||
}) | ||
.configure({ | ||
// Configure lowlight with common languages and set default language | ||
lowlight: createLowlight(common), | ||
defaultLanguage: language, | ||
}) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
|
||
import { SvgIcon } from "@mui/material"; | ||
|
||
const Check = () => { | ||
return ( | ||
<SvgIcon> | ||
<svg stroke="currentColor" fill="none" stroke-width="2" viewBox="0 0 24 24" strokeLinecap="round" strokeLinejoin="round" height="1em" width="1em" xmlns="http://www.w3.org/2000/svg"> | ||
<polyline points="20 6 9 17 4 12" /> | ||
</svg> | ||
</SvgIcon> | ||
); | ||
} | ||
|
||
export default Check; |
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,15 @@ | ||
|
||
import { SvgIcon } from "@mui/material"; | ||
|
||
const Copy = () => { | ||
return ( | ||
<SvgIcon> | ||
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" stroke-linejoin="round"> | ||
<rect width="14" height="14" x="8" y="8" rx="2" ry="2" /> | ||
<path d="M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2" /> | ||
</svg> | ||
</SvgIcon> | ||
); | ||
} | ||
|
||
export default Copy; |
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