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

feat: support customizing text attribute key and rendering #244

Merged
merged 1 commit into from
Jun 23, 2023

Conversation

LucasXu0
Copy link
Collaborator

@LucasXu0 LucasXu0 commented Jun 23, 2023

  • support customizing the text attributes key

For example, we want to embed a page icon and page link into the paragraph.

  1. customize a new attributes for delta, here's {'mention': {'id': 'page_id'}}
    final document = Document.blank()
      ..addParagraph(
        builder: (index) {
          return Delta()
            ..insert(
              '\$',
              attributes: {
                'mention': {'id': 'PAGE_ID'},
              },
            );
        },
      );
  1. customize the textSpanDecorator function inside editor style, VERY IMPORTANT: The length of the text must be 1 if you provide the WidgetSpan; otherwise, the selection will be inaccurate.
final editorStyle = EditorStyle.desktop(
      // Example for customizing a new attribute key.
      textSpanDecorator: (textInsert, textSpan) {
        final attributes = textInsert.attributes;
        if (attributes == null) {
          return textSpan;
        }
        final mention = attributes['mention'] as Map?;
        if (mention != null) {
          return WidgetSpan(
            child: MouseRegion(
              cursor: SystemMouseCursors.click,
              child: GestureDetector(
                behavior: HitTestBehavior.translucent,
                child: Row(
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    const Icon(Icons.edit_document),
                    Text(mention['id']),
                  ],
                ),
              ),
            ),
          );
        }
        return textSpan;
      },
    );
  1. bingo.
Screenshot 2023-06-23 at 17 19 23

@codecov
Copy link

codecov bot commented Jun 23, 2023

Codecov Report

Merging #244 (d664873) into main (cd0f67a) will increase coverage by 0.02%.
The diff coverage is 72.72%.

@@            Coverage Diff             @@
##             main     #244      +/-   ##
==========================================
+ Coverage   62.70%   62.73%   +0.02%     
==========================================
  Files         230      230              
  Lines       10332    10337       +5     
==========================================
+ Hits         6479     6485       +6     
+ Misses       3853     3852       -1     
Impacted Files Coverage Δ
lib/src/render/style/editor_style.dart 6.81% <0.00%> (+0.93%) ⬆️
lib/src/render/rich_text/flowy_rich_text.dart 90.81% <100.00%> (+1.73%) ⬆️

@LucasXu0 LucasXu0 merged commit d359da8 into AppFlowy-IO:main Jun 23, 2023
zoli added a commit to zoli/appflowy-editor that referenced this pull request Jun 25, 2023
* main:
  fix: error occurs when applying divider on Mobile (AppFlowy-IO#252)
  feat: text and background color in mobile toolbar (AppFlowy-IO#233)
  fix: extend attribute keys shouldn't be sliced (AppFlowy-IO#248)
  feat: support customizing text attribute key and rendering (AppFlowy-IO#244)
@MichalNemec
Copy link

amazing! will try it out and give feedback!

@MichalNemec
Copy link

works flawlessly! When can we expect version with this included on pub.dev?

@MichalNemec
Copy link

MichalNemec commented Jul 2, 2023

Coming back to it. It seems like whenever you do this and continue to type afterwards, its bugging out the text.

Test it out: type in whatever, select it, do the action and then type afterwards, it takes only the first letter (w) and then it starts to bug out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants