diff --git a/app/lib/common/widgets/edit_html_description_sheet.dart b/app/lib/common/widgets/edit_html_description_sheet.dart index 7dac686240e2..af62b2c2415e 100644 --- a/app/lib/common/widgets/edit_html_description_sheet.dart +++ b/app/lib/common/widgets/edit_html_description_sheet.dart @@ -56,12 +56,12 @@ class _EditHtmlDescriptionSheetState @override void initState() { super.initState(); - final html = widget.descriptionHtmlValue; - final markdown = widget.descriptionMarkdownValue ?? ''; - final document = html != null - ? ActerDocumentHelpers.fromHtml(html) - : ActerDocumentHelpers.fromMarkdown(markdown); - textEditorState = EditorState(document: document); + textEditorState = EditorState( + document: ActerDocumentHelpers.parse( + widget.descriptionMarkdownValue ?? '', + htmlContent: widget.descriptionHtmlValue, + ), + ); } @override @@ -97,13 +97,14 @@ class _EditHtmlDescriptionSheetState ActerPrimaryActionButton( onPressed: () { // No need to change - final htmlBodyDescription = textEditorState.intoHtml(); + String htmlBodyDescription = textEditorState.intoHtml(); final plainDescription = textEditorState.intoMarkdown(); if (htmlBodyDescription == widget.descriptionHtmlValue || plainDescription == widget.descriptionMarkdownValue) { Navigator.pop(context); return; } + widget.onSave(htmlBodyDescription, plainDescription); }, child: Text(L10n.of(context).save), diff --git a/app/lib/common/widgets/html_editor.dart b/app/lib/common/widgets/html_editor.dart index bdf050df13f2..6b2ccd3783d4 100644 --- a/app/lib/common/widgets/html_editor.dart +++ b/app/lib/common/widgets/html_editor.dart @@ -50,27 +50,48 @@ extension ActerEditorStateHelpers on EditorState { } extension ActerDocumentHelpers on Document { - static Document fromHtml( + static Document? _fromHtml( String content, { AppFlowyEditorHTMLCodec? codec, }) { - return (codec ?? defaultHtmlCodec).decode(content); + if (content.isEmpty) { + return null; + } + + Document document = (codec ?? defaultHtmlCodec).decode(content); + if (document.isEmpty) { + return null; + } + return document; } - static Document fromMarkdown( + static Document _fromMarkdown( String content, { AppFlowyEditorMarkdownCodec? codec, }) { return (codec ?? defaultMarkdownCodec).decode(content); } - static Document fromMsgContent(MsgContent msgContent) { - final formattedBody = msgContent.formattedBody(); - if (formattedBody != null) { - return ActerDocumentHelpers.fromHtml(formattedBody); - } else { - return ActerDocumentHelpers.fromMarkdown(msgContent.body()); + static Document parse( + String content, { + String? htmlContent, + AppFlowyEditorMarkdownCodec? codec, + }) { + if (htmlContent != null) { + final document = ActerDocumentHelpers._fromHtml(htmlContent); + if (document != null) { + return document; + } } + // fallback: parse from markdown + return ActerDocumentHelpers._fromMarkdown(content); + } + + static Document fromMsgContent(MsgContent msgContent) { + return ActerDocumentHelpers.parse( + msgContent.body(), + htmlContent: msgContent.formattedBody(), + ); } } diff --git a/app/lib/features/comments/widgets/create_comment.dart b/app/lib/features/comments/widgets/create_comment.dart index e7396eeb1a9e..b7b505e5678e 100644 --- a/app/lib/features/comments/widgets/create_comment.dart +++ b/app/lib/features/comments/widgets/create_comment.dart @@ -59,10 +59,12 @@ class _CreateCommentWidgetState extends ConsumerState { editorState: textEditorState, footer: const SizedBox(), onChanged: (body, html) { - final document = html != null - ? ActerDocumentHelpers.fromHtml(html) - : ActerDocumentHelpers.fromMarkdown(body); - textEditorState = EditorState(document: document); + textEditorState = EditorState( + document: ActerDocumentHelpers.parse( + body, + htmlContent: html, + ), + ); }, ), ), diff --git a/app/lib/features/events/pages/create_event_page.dart b/app/lib/features/events/pages/create_event_page.dart index eb199d88369e..3e4d529d1881 100644 --- a/app/lib/features/events/pages/create_event_page.dart +++ b/app/lib/features/events/pages/create_event_page.dart @@ -56,17 +56,14 @@ class CreateEventPageConsumerState extends ConsumerState { // description final desc = event.description(); if (desc != null) { - final formatted = desc.formatted(); - if (formatted != null) { - textEditorState = - EditorState(document: ActerDocumentHelpers.fromHtml(formatted)); - } else { - textEditorState = EditorState( - document: ActerDocumentHelpers.fromMarkdown( - desc.body(), - ), - ); - } + textEditorState = EditorState( + document: ActerDocumentHelpers.parse( + desc.body(), + htmlContent: desc.formatted(), + ), + ); + } else { + textEditorState = EditorState.blank(); } // Getting start and end date time @@ -377,10 +374,12 @@ class CreateEventPageConsumerState extends ConsumerState { editable: true, autoFocus: false, onChanged: (body, html) { - final document = html != null - ? ActerDocumentHelpers.fromHtml(html) - : ActerDocumentHelpers.fromMarkdown(body); - textEditorState = EditorState(document: document); + textEditorState = EditorState( + document: ActerDocumentHelpers.parse( + body, + htmlContent: html, + ), + ); }, ), ), diff --git a/app/lib/features/news/pages/add_news_page.dart b/app/lib/features/news/pages/add_news_page.dart index dcc354ee2700..732ca421f0ea 100644 --- a/app/lib/features/news/pages/add_news_page.dart +++ b/app/lib/features/news/pages/add_news_page.dart @@ -54,9 +54,9 @@ class AddNewsState extends ConsumerState { final changed = prevState?.currentNewsSlide != nextState.currentNewsSlide; if (isText && changed) { final next = nextState.currentNewsSlide!; - final document = next.html != null - ? ActerDocumentHelpers.fromHtml(next.html!) - : ActerDocumentHelpers.fromMarkdown(next.text ?? ''); + final document = + ActerDocumentHelpers.parse(next.text ?? '', htmlContent: next.html); + final autoFocus = (next.html?.isEmpty ?? true) && (next.text?.isEmpty ?? true); diff --git a/app/lib/features/tasks/sheets/create_update_task_list.dart b/app/lib/features/tasks/sheets/create_update_task_list.dart index 57d54f3424a1..b076e59d6122 100644 --- a/app/lib/features/tasks/sheets/create_update_task_list.dart +++ b/app/lib/features/tasks/sheets/create_update_task_list.dart @@ -170,10 +170,12 @@ class _CreateUpdateTaskListConsumerState editable: true, autoFocus: false, onChanged: (body, html) { - final document = html != null - ? ActerDocumentHelpers.fromHtml(html) - : ActerDocumentHelpers.fromMarkdown(body); - textEditorState = EditorState(document: document); + textEditorState = EditorState( + document: ActerDocumentHelpers.parse( + body, + htmlContent: html, + ), + ); }, ), ),