Skip to content
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

[lexical-markdown] Bug Fix: standardise selection to move to start after markdown toggle #6220

Merged
merged 7 commits into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/lexical-markdown/src/MarkdownExport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ import {

import {isEmptyParagraph, transformersByType} from './utils';

/**
* Renders string from markdown. The selection is moved to the start after the operation.
*/
export function createMarkdownExport(
transformers: Array<Transformer>,
shouldPreserveNewLines: boolean = false,
Expand Down
5 changes: 4 additions & 1 deletion packages/lexical-markdown/src/MarkdownImport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ type TextFormatTransformersIndex = Readonly<{
transformersByTag: Readonly<Record<string, TextFormatTransformer>>;
}>;

/**
* Renders markdown from a string. The selection is moved to the start after the operation.
*/
export function createMarkdownImport(
transformers: Array<Transformer>,
shouldPreserveNewLines = false,
Expand Down Expand Up @@ -95,7 +98,7 @@ export function createMarkdownImport(
}

if ($getSelection() !== null) {
root.selectEnd();
root.selectStart();
}
};
}
Expand Down
6 changes: 6 additions & 0 deletions packages/lexical-markdown/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ const TRANSFORMERS: Array<Transformer> = [
...TEXT_MATCH_TRANSFORMERS,
];

/**
* Renders markdown from a string. The selection is moved to the start after the operation.
*/
function $convertFromMarkdownString(
markdown: string,
transformers: Array<Transformer> = TRANSFORMERS,
Expand All @@ -81,6 +84,9 @@ function $convertFromMarkdownString(
return importMarkdown(markdown, node);
}

/**
* Renders string from markdown. The selection is moved to the start after the operation.
*/
function $convertToMarkdownString(
transformers: Array<Transformer> = TRANSFORMERS,
node?: ElementNode,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,6 @@ export default function ActionsPlugin({
$createCodeNode('markdown').append($createTextNode(markdown)),
);
}
root.selectEnd();
Copy link
Contributor Author

@potatowagon potatowagon Jun 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

after removing this, we expose the selection behavior of $convertFromMarkdownString and $convertToMarkdownString.

$convertToMarkdownString moves the selector (aka cursor) to the start after the markdown -> string conversion.

demo:

Screen.Recording.2024-06-05.at.3.34.06.PM.mov

in this PR adjust $convertFromMarkdownString to do the same. Also the attached issue has raised a preference for having the cursor at the start instead of the end

});
}, [editor, shouldPreserveNewLinesInMarkdown]);

Expand Down
Loading