Skip to content

Commit

Permalink
Merge pull request #2200 from acterglobal/ben-html-editor-bug-fix
Browse files Browse the repository at this point in the history
refactor of html editor bug fix
  • Loading branch information
gnunicorn authored Sep 25, 2024
2 parents d13e87f + a88c552 commit 3d1ac86
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 42 deletions.
15 changes: 8 additions & 7 deletions app/lib/common/widgets/edit_html_description_sheet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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),
Expand Down
39 changes: 30 additions & 9 deletions app/lib/common/widgets/html_editor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
);
}
}

Expand Down
10 changes: 6 additions & 4 deletions app/lib/features/comments/widgets/create_comment.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,12 @@ class _CreateCommentWidgetState extends ConsumerState<CreateCommentWidget> {
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,
),
);
},
),
),
Expand Down
29 changes: 14 additions & 15 deletions app/lib/features/events/pages/create_event_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,14 @@ class CreateEventPageConsumerState extends ConsumerState<CreateEventPage> {
// 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
Expand Down Expand Up @@ -377,10 +374,12 @@ class CreateEventPageConsumerState extends ConsumerState<CreateEventPage> {
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,
),
);
},
),
),
Expand Down
6 changes: 3 additions & 3 deletions app/lib/features/news/pages/add_news_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ class AddNewsState extends ConsumerState<AddNewsPage> {
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);

Expand Down
10 changes: 6 additions & 4 deletions app/lib/features/tasks/sheets/create_update_task_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
),
);
},
),
),
Expand Down

0 comments on commit 3d1ac86

Please sign in to comment.