Skip to content

Commit

Permalink
Merge pull request #8 from vadimgierko/enable-copying-markdown-and-ht…
Browse files Browse the repository at this point in the history
…ml-to-clipboard

enable copying markdown and html to clipboard
  • Loading branch information
vadimgierko authored Sep 27, 2024
2 parents 9a41e10 + 043a988 commit 20f1919
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 6 deletions.
41 changes: 36 additions & 5 deletions src/components/MarkdownEditor/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useEffect, useState } from "react";
import {
getMarkdownFromLocalStorage,
prerenderSanitizeHtml,
saveMarkdownInStorage,
} from "@/utils/index";
import MarkdownRenderer from "../MarkdownRenderer";
Expand Down Expand Up @@ -28,6 +29,17 @@ export default function MarkdownEditor({

const [localMarkdown, setLocalMarkdown] = useState();

async function copyToClipBoard(string) {
try {
await navigator.clipboard.writeText(string);
alert("Copied to clipboard");
console.log("Text copied to clipboard:");
console.log(string);
} catch (error) {
console.error("Failed to copy text: ", error);
}
}

// NEED TO FETCH FROM LOCAL STORAGE IN USE EFFECT
// BECAUSE IT RUNS ON CLIENT
useEffect(() => {
Expand All @@ -42,7 +54,7 @@ export default function MarkdownEditor({
return (
<div className="markdown-editor-component">
<div className="markdown-editor-navbar">
{/* SHOW EDITOR TOGGLE */}
{/* SHOW MARKDOWN EDITOR TOGGLE */}
<label className="me-1">
<input
type="checkbox"
Expand All @@ -55,9 +67,10 @@ export default function MarkdownEditor({
: showEditor
}
/>{" "}
editor
md editor
{/* editor */}
</label>
{/* SHOW RENDERER TOGGLE */}
{/* SHOW RENDERED HTML TOGGLE */}
<label className="me-1">
<input
type="checkbox"
Expand All @@ -72,15 +85,33 @@ export default function MarkdownEditor({
/>{" "}
renderer
</label>
<span
onClick={async () => {
await copyToClipBoard(localMarkdown);
}}
className="me-1"
style={{ cursor: "pointer" }}
>
<i className="bi bi-clipboard"></i> copy md
</span>
<span
style={{ cursor: "pointer" }}
onClick={async () => {
const htmlString = await prerenderSanitizeHtml(localMarkdown);
await copyToClipBoard(htmlString);
}}
>
<i className="bi bi-clipboard"></i> copy html
</span>
{/* CUSTOM RENDERER TOGGLE */}
<label className="">
{/* <label className="">
<input
type="checkbox"
checked={!isCustomRenderer}
onChange={toggleCustomRenderer}
/>{" "}
use react-markdown
</label>
</label> */}
</div>
<div
className={`markdown-editor-container ${
Expand Down
1 change: 1 addition & 0 deletions src/content/about.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ I've added new editor/renderer features, upgraded tech stack (upgraded React and
| inline CSS styles | ❌ | ✅ |
| embed elements | ❌ | ✅ |
| react-markdown/ custom renderer switching | ❌ | ✅ |
| copy markdown or HTML to clipboard | ❌ | ✅ |
---
Expand Down
1 change: 0 additions & 1 deletion src/pages/editor/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ export default function Editor({ pageData }: { pageData: PageData }) {
return <Page pageData={pageData} />;
}

// ❗✅ TODO: add getStaticProps here, even for editor
export async function getStaticProps() {
const pageData: PageData = {
head: {
Expand Down
10 changes: 10 additions & 0 deletions src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ export async function prerenderSanitizeHtml(markdown = "") {
return prerendered_sanitized_html;
}

export async function copyToClipBoard(string) {
try {
await navigator.clipboard.writeText(string);
console.log("Text copied to clipboard:");
console.log(string);
} catch (error) {
console.error("Failed to copy text: ", error);
}
}

//==================== LOCAL STORAGE =====================//

export function getMarkdownFromLocalStorage() {
Expand Down

0 comments on commit 20f1919

Please sign in to comment.