-
-
Notifications
You must be signed in to change notification settings - Fork 749
feat: reusable React API for source blocks with previews #2883
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
YousefED
wants to merge
1
commit into
code-block-previews
Choose a base branch
from
source-preview-react-api
base: code-block-previews
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
98 changes: 10 additions & 88 deletions
98
packages/math-block/src/block/react/render/MathBlockPreviewWithPopup.tsx
This file contains hidden or 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 |
|---|---|---|
| @@ -1,104 +1,26 @@ | ||
| import { SourceBlockWithPreviewExtension } from "@blocknote/core"; | ||
| import { | ||
| ReactCustomBlockRenderProps, | ||
| useExtension, | ||
| useExtensionState, | ||
| SourceBlockWithPreview, | ||
| } from "@blocknote/react"; | ||
| import { MouseEvent } from "react"; | ||
|
|
||
| import { MathBlockConfig } from "../../createMathBlockConfig.js"; | ||
| import { getMathPlainTextContent } from "../../../shared/getMathPlainTextContent.js"; | ||
| import { AddSourceButton } from "../../../shared/react/render/AddSourceButton.js"; | ||
| import { useLatexToMathMLString } from "../../../shared/react/render/useLatexToMathML.js"; | ||
|
|
||
| export const MathBlockPreviewWithPopup = ( | ||
| props: ReactCustomBlockRenderProps<MathBlockConfig>, | ||
| ) => { | ||
| const { block, editor, contentRef } = props; | ||
|
|
||
| const source = getMathPlainTextContent(block.content); | ||
|
|
||
| const { store } = useExtension(SourceBlockWithPreviewExtension, { editor }); | ||
| const popupOpen = useExtensionState(SourceBlockWithPreviewExtension, { | ||
| editor, | ||
| selector: (state) => state.popupOpen === block.id, | ||
| }); | ||
| const selected = useExtensionState(SourceBlockWithPreviewExtension, { | ||
| editor, | ||
| selector: (state) => state.selected === block.id, | ||
| }); | ||
|
|
||
| const source = getMathPlainTextContent(props.block.content); | ||
| const { mathMLString, error } = useLatexToMathMLString(source); | ||
|
|
||
| // Opens the popup when clicking the preview. | ||
| const handlePreviewMouseDown = (event: MouseEvent) => { | ||
| if (!editor.isEditable) { | ||
| return; | ||
| } | ||
|
|
||
| store.setState((state) => ({ ...state, popupOpen: block.id })); | ||
|
|
||
| event.preventDefault(); | ||
| event.stopPropagation(); | ||
|
|
||
| editor.setTextCursorPosition(block.id, "end"); | ||
| editor.focus(); | ||
| }; | ||
|
|
||
| // Closes the popup when clicking the "OK" button. | ||
| const handleOkButtonMouseDown = (event: MouseEvent) => { | ||
| event.preventDefault(); | ||
| event.stopPropagation(); | ||
|
|
||
| store.setState((state) => ({ ...state, popupOpen: undefined })); | ||
| }; | ||
|
|
||
| return ( | ||
| <div | ||
| className={ | ||
| "bn-preview-with-source-popup" + | ||
| (selected ? " ProseMirror-selectednode" : "") | ||
| } | ||
| data-open={popupOpen ? "true" : "false"} | ||
| > | ||
| <div | ||
| className="bn-preview-container" | ||
| contentEditable={false} | ||
| onMouseDown={handlePreviewMouseDown} | ||
| > | ||
| {source.length > 0 ? ( | ||
| <span dangerouslySetInnerHTML={{ __html: mathMLString }} /> | ||
| ) : ( | ||
| <AddSourceButton | ||
| text={editor.dictionary.code_block.add_source_button_text} | ||
| /> | ||
| )} | ||
| </div> | ||
| <div className="bn-source-block-popup"> | ||
| <div className="bn-code-block-source-popup-body"> | ||
| <pre> | ||
| <code ref={contentRef} /> | ||
| </pre> | ||
| <div | ||
| className="bn-code-block-source-popup-ok-button-wrapper" | ||
| contentEditable={false} | ||
| > | ||
| <button | ||
| className="bn-code-block-source-popup-ok-button" | ||
| onMouseDown={handleOkButtonMouseDown} | ||
| > | ||
| OK | ||
| </button> | ||
| </div> | ||
| </div> | ||
| <div | ||
| className="bn-code-block-source-error" | ||
| contentEditable={false} | ||
| style={{ display: error ? "block" : "none" }} | ||
| > | ||
| {error} | ||
| </div> | ||
| </div> | ||
| </div> | ||
| <SourceBlockWithPreview | ||
| block={props.block} | ||
| editor={props.editor} | ||
| contentRef={props.contentRef} | ||
| source={source} | ||
| preview={<span dangerouslySetInnerHTML={{ __html: mathMLString }} />} | ||
| error={error} | ||
| /> | ||
| ); | ||
| }; |
This file contains hidden or 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
42 changes: 42 additions & 0 deletions
42
packages/math-block/src/inlineContent/MathInlineInputRulesExtension.ts
This file contains hidden or 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,42 @@ | ||
| import { createExtension } from "@blocknote/core"; | ||
| import { | ||
| InputRule, | ||
| inputRules as inputRulesPlugin, | ||
| } from "@handlewithcare/prosemirror-inputrules"; | ||
|
|
||
| import { mathInlineContentConfig } from "./mathInlineContentConfig.js"; | ||
|
|
||
| /** | ||
| * Converts text wrapped in LaTeX inline-math delimiters into inline math | ||
| * content as it's typed: | ||
| * - `$...$` (TeX inline math) | ||
| * - `\(...\)` (LaTeX inline math) | ||
| * | ||
| * The delimiters are removed and the enclosed source becomes the inline math's | ||
| * content. | ||
| */ | ||
| export const MathInlineInputRulesExtension = createExtension({ | ||
| key: "math-inline-input-rules", | ||
| // Cannot use the `inputRules` field as it only allows for converting matched | ||
| // content to blocks. | ||
| prosemirrorPlugins: [ | ||
| inputRulesPlugin({ | ||
| rules: [/\$([^$]+)\$$/, /\\\((.+?)\\\)$/].map( | ||
| (find) => | ||
| new InputRule(find, (state, match, start, end) => { | ||
| const source = match[1]?.trim(); | ||
| const nodeType = state.schema.nodes[mathInlineContentConfig.type]; | ||
| if (!source || !nodeType) { | ||
| return null; | ||
| } | ||
|
|
||
| return state.tr.replaceRangeWith( | ||
| start, | ||
| end, | ||
| nodeType.create(null, state.schema.text(source)), | ||
| ); | ||
| }), | ||
| ), | ||
| }), | ||
| ], | ||
| }); |
This file contains hidden or 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 hidden or 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
2 changes: 1 addition & 1 deletion
2
...c/shared/react/render/AddSourceButton.tsx → ...cks/SourceWithPreview/AddSourceButton.tsx
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we're changing this here we should probably do the same for the math block