Skip to content

Commit

Permalink
[lexical-markdown] Bug Fix: standardise selection to move to start af…
Browse files Browse the repository at this point in the history
…ter markdown toggle (#6220)
  • Loading branch information
potatowagon authored Jun 7, 2024
1 parent b4281f7 commit 0e168d1
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 2 deletions.
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();
});
}, [editor, shouldPreserveNewLinesInMarkdown]);

Expand Down

0 comments on commit 0e168d1

Please sign in to comment.