Skip to content

Commit

Permalink
fix: using space bar to move the cursor not working on Android (AppFl…
Browse files Browse the repository at this point in the history
  • Loading branch information
asjqkkkk authored and LucasXu0 committed Jan 7, 2025
1 parent ea1eb16 commit 1832ba0
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ Future<void> onNonTextUpdate(
// for the another keyboards (e.g. system keyboard), they will trigger the
// `onFloatingCursor` event instead.
AppFlowyEditorLog.input.debug('[Android] onNonTextUpdate: $nonTextUpdate');
if (selection != null && selection != editorState.selection) {
if (selection != null) {
editorState.updateSelectionWithReason(
Selection.collapsed(
Position(
Expand Down
20 changes: 19 additions & 1 deletion lib/src/plugins/markdown/decoder/document_markdown_decoder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ class DocumentMarkdownDecoder extends Converter<String, Document> {

@override
Document convert(String input) {
final formattedMarkdown = _formatMarkdown(input);
final List<md.Node> mdNodes = md.Document(
extensionSet: md.ExtensionSet.gitHubFlavored,
inlineSyntaxes: [
...inlineSyntaxes,
UnderlineInlineSyntax(),
],
encodeHtml: false,
).parse(input);
).parse(formattedMarkdown);

final document = Document.blank();
final nodes = mdNodes
Expand Down Expand Up @@ -61,4 +62,21 @@ class DocumentMarkdownDecoder extends Converter<String, Document> {

return nodes;
}

String _formatMarkdown(String markdown) {
// Rule 1: single '\n' between text and image, add double '\n'
String result = markdown.replaceAllMapped(
RegExp(r'([^\n])\n!\[([^\]]*)\]\(([^)]+)\)', multiLine: true),
(match) {
final text = match[1] ?? '';
final altText = match[2] ?? '';
final url = match[3] ?? '';
return '$text\n\n![$altText]($url)';
},
);

// Add another rules here.

return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,25 +39,13 @@ class MarkdownParagraphParserV2 extends CustomMarkdownParser {
final splitContent = _splitByBrTag(ec);

// Transform each split content into a paragraph node
final result = <Node>[];
for (final content in splitContent) {
for (final node in content) {
if (node is md.Element && node.tag == 'img') {
final image = const MarkdownImageParserV2()
.transform(node, parsers)
.firstOrNull;
if (image != null) {
result.add(image);
continue;
}
}
final deltaDecoder = DeltaMarkdownDecoder();
final delta = deltaDecoder.convertNodes([node]);
result.add(paragraphNode(delta: delta));
}
}
return splitContent.map((content) {
final deltaDecoder = DeltaMarkdownDecoder();
final delta = deltaDecoder.convertNodes(content);
return paragraphNode(delta: delta);
}).toList();

return result;
// return result;
}
}

Expand Down
15 changes: 13 additions & 2 deletions test/plugins/markdown/document_markdown_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,24 @@ void main() {
expect(markdown, markdownDocumentEncoded);
});

test('paragraph + image', () {
test('paragraph + image with single \n', () {
const markdown = '''This is the first line
![image](https://example.com/image.png)''';
final document = markdownToDocument(markdown);
final nodes = document.root.children;
expect(nodes.length, 2);
expect(nodes[0].delta?.toPlainText(), 'This is the first line\n');
expect(nodes[0].delta?.toPlainText(), 'This is the first line');
expect(nodes[1].attributes['url'], 'https://example.com/image.png');
});

test('paragraph + image with double \n', () {
const markdown = '''This is the first line
![image](https://example.com/image.png)''';
final document = markdownToDocument(markdown);
final nodes = document.root.children;
expect(nodes.length, 2);
expect(nodes[0].delta?.toPlainText(), 'This is the first line');
expect(nodes[1].attributes['url'], 'https://example.com/image.png');
});
});
Expand Down

0 comments on commit 1832ba0

Please sign in to comment.