Skip to content

Commit

Permalink
🚚(frontend) move Markdown button to is own file
Browse files Browse the repository at this point in the history
To keep the codebase clean and organized,
we moved the Markdown button to its own file.
  • Loading branch information
AntoLC committed Oct 2, 2024
1 parent 3a4e9ec commit a894269
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@ import {
NestBlockButton,
TextAlignButton,
UnnestBlockButton,
useBlockNoteEditor,
useComponentsContext,
useSelectedBlocks,
} from '@blocknote/react';
import { forEach, isArray } from 'lodash';
import React, { useMemo } from 'react';
import React from 'react';

import { MarkdownButton } from './MarkdownButton';

export const BlockNoteToolbar = () => {
return (
Expand Down Expand Up @@ -57,79 +55,3 @@ export const BlockNoteToolbar = () => {
/>
);
};

type Block = {
type: string;
text: string;
content: Block[];
};

function isBlock(block: Block): block is Block {
return (
block.content &&
isArray(block.content) &&
block.content.length > 0 &&
typeof block.type !== 'undefined'
);
}

const recursiveContent = (content: Block[], base: string = '') => {
let fullContent = base;
for (const innerContent of content) {
if (innerContent.type === 'text') {
fullContent += innerContent.text;
} else if (isBlock(innerContent)) {
fullContent = recursiveContent(innerContent.content, fullContent);
}
}

return fullContent;
};

/**
* Custom Formatting Toolbar Button to convert markdown to json.
*/
export function MarkdownButton() {
const editor = useBlockNoteEditor();
const Components = useComponentsContext();
const selectedBlocks = useSelectedBlocks(editor);

const handleConvertMarkdown = () => {
const blocks = editor.getSelection()?.blocks;

forEach(blocks, async (block) => {
if (!isBlock(block as unknown as Block)) {
return;
}

try {
const fullContent = recursiveContent(
block.content as unknown as Block[],
);

const blockMarkdown =
await editor.tryParseMarkdownToBlocks(fullContent);
editor.replaceBlocks([block.id], blockMarkdown);
} catch (error) {
console.error('Error parsing Markdown:', error);
}
});
};

const show = useMemo(() => {
return !!selectedBlocks.find((block) => block.content !== undefined);
}, [selectedBlocks]);

if (!show || !editor.isEditable || !Components) {
return null;
}

return (
<Components.FormattingToolbar.Button
mainTooltip="Convert Markdown"
onClick={handleConvertMarkdown}
>
M
</Components.FormattingToolbar.Button>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import '@blocknote/mantine/style.css';
import {
useBlockNoteEditor,
useComponentsContext,
useSelectedBlocks,
} from '@blocknote/react';
import { forEach, isArray } from 'lodash';
import React, { useMemo } from 'react';

type Block = {
type: string;
text: string;
content: Block[];
};

function isBlock(block: Block): block is Block {
return (
block.content &&
isArray(block.content) &&
block.content.length > 0 &&
typeof block.type !== 'undefined'
);
}

const recursiveContent = (content: Block[], base: string = '') => {
let fullContent = base;
for (const innerContent of content) {
if (innerContent.type === 'text') {
fullContent += innerContent.text;
} else if (isBlock(innerContent)) {
fullContent = recursiveContent(innerContent.content, fullContent);
}
}

return fullContent;
};

/**
* Custom Formatting Toolbar Button to convert markdown to json.
*/
export function MarkdownButton() {
const editor = useBlockNoteEditor();
const Components = useComponentsContext();
const selectedBlocks = useSelectedBlocks(editor);

const handleConvertMarkdown = () => {
const blocks = editor.getSelection()?.blocks;

forEach(blocks, async (block) => {
if (!isBlock(block as unknown as Block)) {
return;
}

try {
const fullContent = recursiveContent(
block.content as unknown as Block[],
);

const blockMarkdown =
await editor.tryParseMarkdownToBlocks(fullContent);
editor.replaceBlocks([block.id], blockMarkdown);
} catch (error) {
console.error('Error parsing Markdown:', error);
}
});
};

const show = useMemo(() => {
return !!selectedBlocks.find((block) => block.content !== undefined);
}, [selectedBlocks]);

if (!show || !editor.isEditable || !Components) {
return null;
}

return (
<Components.FormattingToolbar.Button
mainTooltip="Convert Markdown"
onClick={handleConvertMarkdown}
>
M
</Components.FormattingToolbar.Button>
);
}

0 comments on commit a894269

Please sign in to comment.