-
Notifications
You must be signed in to change notification settings - Fork 2.1k
[lexical-markdown][lexical-playground] Bug Fix: Keep indent when switching with markdown #7845
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
lytion
wants to merge
16
commits into
facebook:main
Choose a base branch
from
lytion:lytion/indet-lost-when-switching-markdown
base: main
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.
+60
−0
Open
Changes from 13 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
10027d5
Add Indent Transformer
lytion e9aacd5
Merge branch 'main' into lytion/indet-lost-when-switching-markdown
lytion c02ef17
Remove unused import
lytion e2a6712
Fix prettier error
lytion fe76540
Merge branch 'main' into lytion/indet-lost-when-switching-markdown
lytion 4c2a215
Merge branch 'main' into lytion/indet-lost-when-switching-markdown
SimonBct f93caa9
Merge branch 'main' into lytion/indet-lost-when-switching-markdown
SimonBct c93f030
Merge branch 'main' into lytion/indet-lost-when-switching-markdown
lytion 8cec7f1
Separate INDENT transformer from default one
SimonBct 4eca17d
Merge branch 'main' into lytion/indet-lost-when-switching-markdown
SimonBct 81deb22
Merge branch 'main' into lytion/indet-lost-when-switching-markdown
lytion 6c6b36d
Merge branch 'main' into lytion/indet-lost-when-switching-markdown
lytion b8a5dcd
Merge branch 'main' into lytion/indet-lost-when-switching-markdown
lytion 6bec51f
Refactor key comparison
lytion 74fec50
Merge branch 'main' into lytion/indet-lost-when-switching-markdown
lytion 18faefc
Merge branch 'main' into lytion/indet-lost-when-switching-markdown
lytion 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -36,6 +36,8 @@ import { | |
| $createLineBreakNode, | ||
| $createTextNode, | ||
| $getState, | ||
| $isParagraphNode, | ||
| $isTextNode, | ||
| $setState, | ||
| createState, | ||
| ElementNode, | ||
|
|
@@ -216,6 +218,7 @@ const TAG_START_REGEX = /^<[a-z_][\w-]*(?:\s[^<>]*)?\/?>/i; | |
| const TAG_END_REGEX = /^<\/[a-z_][\w-]*\s*>/i; | ||
| const ENDS_WITH = (regex: RegExp) => | ||
| new RegExp(`(?:${regex.source})$`, regex.flags); | ||
| const INDENT_REGEX = /^\t+/; | ||
|
|
||
| export const listMarkerState = createState('mdListMarker', { | ||
| parse: (v) => (typeof v === 'string' && /^[-*+]$/.test(v) ? v : '-'), | ||
|
|
@@ -622,6 +625,37 @@ export const LINK: TextMatchTransformer = { | |
| type: 'text-match', | ||
| }; | ||
|
|
||
| export const INDENT: TextMatchTransformer = { | ||
| dependencies: [], | ||
| export: (node, exportChildren, exportFormat) => { | ||
| const parentNode = node.getParent(); | ||
| const textContent = node.getTextContent(); | ||
| if ( | ||
| !$isParagraphNode(parentNode) || | ||
| !$isTextNode(node) || | ||
| parentNode.getFirstChild() !== node | ||
| ) { | ||
| return null; | ||
| } | ||
| const indent = parentNode.getIndent(); | ||
| const textWithFormat = exportFormat(node, textContent); | ||
| return textWithFormat ? '\t'.repeat(indent) + textWithFormat : null; | ||
| }, | ||
| importRegExp: INDENT_REGEX, | ||
| regExp: INDENT_REGEX, | ||
| replace: (textNode, match) => { | ||
| const [indents] = match; | ||
| const parentNode = textNode.getParent(); | ||
| if (!parentNode || !$isParagraphNode(parentNode)) { | ||
| return; | ||
| } | ||
| parentNode.setIndent(indents.length); | ||
| textNode.setTextContent(textNode.getTextContent().replace(/^\t+/, '')); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems like maybe there's a bug elsewhere because tabs are usually supposed to use TabNode |
||
| return textNode; | ||
| }, | ||
| type: 'text-match', | ||
| }; | ||
|
|
||
| export const ELEMENT_TRANSFORMERS: Array<ElementTransformer> = [ | ||
| HEADING, | ||
| QUOTE, | ||
|
|
||
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
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
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.
Uh oh!
There was an error while loading. Please reload this page.