Skip to content

Feat cover image reposition(#2462) #8102

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

Open
wants to merge 11 commits into
base: main
Choose a base branch
from

Conversation

PirateBrook
Copy link

@PirateBrook PirateBrook commented Jul 3, 2025

Feature Preview

related feat #2462

Kapture.2025-07-02.at.18.35.13.webm

PR Checklist

  • My code adheres to AppFlowy's Conventions
  • I've listed at least one issue that this PR fixes in the description above.
  • I've added a test(s) to validate changes in this PR, or this PR only contains semantic changes.
  • All existing tests are passing.

Summary by Sourcery

Enable repositioning of document cover images on desktop and web by introducing an alignment mode, draggable reposition controls, and persistent alignment offsets.

New Features:

  • Add cover image repositioning mode with drag gestures on desktop/web.
  • Introduce overlay align, save, and cancel buttons for cover alignment editing.
  • Persist cover alignment offsets as a new coverOffset attribute in document metadata.
  • Add DesktopCoverAlign widget and DesktopCoverAlignController to render and manage image alignment.

Enhancements:

  • Extend FlowyNetworkImage with an optional imageBuilder to wrap network images in the alignment widget.
  • Update onChangeCover callbacks and toolbar logic to pass and handle alignment parameters.
  • Add isAlignEnable flags to PageStyleCover and CoverType for conditional alignment support.

Copy link
Contributor

sourcery-ai bot commented Jul 3, 2025

Reviewer's Guide

This PR adds a desktop/web cover image repositioning feature by introducing a draggable alignment component, integrating new alignment UI into existing cover widgets, extending the data model to store cover offset, and plumbing through the image rendering pipeline to support custom builders.

Sequence diagram for saving cover image alignment

sequenceDiagram
    actor User
    participant DocumentCoverState
    participant DesktopCoverAlignController
    participant DocumentHeaderBlockKeys
    participant App (Data Store)
    User->>DocumentCoverState: Drag and reposition cover image
    DocumentCoverState->>DesktopCoverAlignController: changeAlign(x, y)
    User->>DocumentCoverState: Click 'Save'
    DocumentCoverState->>DesktopCoverAlignController: getAlignAttribute()
    DocumentCoverState->>App (Data Store): Save cover_offset attribute
    App (Data Store)-->>DocumentCoverState: Confirm save
    DocumentCoverState-->>User: Update cover image position
Loading

Class diagram for cover image alignment feature

classDiagram
    class DocumentHeaderBlockKeys {
      <<static>>
      +coverType : String
      +coverDetails : String
      +icon : String
      +coverOffset : String
    }
    class DesktopCoverAlignController {
      -_initialAlignment : Alignment
      -_adjustedAlign : Alignment
      +alignment : Alignment
      +reset()
      +cancel()
      +changeAlign(x: double, y: double)
      +isModified : bool
      +getAlignAttribute() : String
    }
    class DesktopCoverAlign {
      +controller : DesktopCoverAlignController
      +imageProvider : ImageProvider
      +fit : BoxFit
      +alignEnable : bool
    }
    class DocumentCoverState {
      +isAlignOpen : bool
      +coverAlignController : DesktopCoverAlignController?
      +switchAlignMode()
      +cancelCoverAlign()
      +saveCoverAlign()
    }
    DocumentCoverState --> DesktopCoverAlignController
    DesktopCoverAlign --> DesktopCoverAlignController
Loading

File-Level Changes

Change Details Files
Implemented DesktopCoverAlign component for interactive cover repositioning
  • Created DesktopCoverAlignController to parse, track, and compute alignment offsets
  • Built DesktopCoverAlign StatefulWidget wrapping ImageProvider for drag gestures
  • Added AlignCoverButton, AlignCoverSaveButton, and AlignCoverCancelButton UI widgets
frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/header/desktop_cover_align.dart
frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/header/document_cover_widget.dart
Integrated alignment UI and state into DocumentCoverWidget
  • Added isAlignOpen flag and switchAlignMode/cancelCoverAlign/saveCoverAlign methods
  • Inserted alignment overlay buttons via _buildConverAlignOverlayButtons
  • Wired onAlignControllerCreated to supply controller to DesktopCover
frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/header/document_cover_widget.dart
Extended DesktopCover to render alignable images
  • Wrapped all Image.asset, Image.file, and FlowyNetworkImage in DesktopCoverAlign
  • Passed enableAlign and onAlignControllerCreated props down to align component
  • Reset controller on coverDetails change in didUpdateWidget
frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/header/desktop_cover.dart
Persisted cover alignment offset and updated APIs
  • Added new DocumentHeaderBlockKeys.coverOffset key
  • Extended onChangeCover callbacks and _saveIconOrCover to accept and store offset
  • Updated DocumentHeaderToolbar signature and default cover calls to include null offset
  • Added isAlignEnable getter in PageStyleCover to flag alignment support
frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/header/document_cover_widget.dart
frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/header/desktop_cover.dart
frontend/appflowy_flutter/lib/mobile/application/page_style/document_page_style_bloc.dart
Enabled custom image rendering via imageBuilder in FlowyNetworkImage
  • Added imageBuilder parameter to FlowyNetworkImage API
  • Passed imageBuilder through to underlying CachedNetworkImage
frontend/appflowy_flutter/lib/shared/appflowy_network_image.dart

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@PirateBrook PirateBrook changed the title Feat cover image reposition Feat cover image reposition(#2462) Jul 3, 2025
Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @PirateBrook - I've reviewed your changes - here's some feedback:

  • The new onChangeCover signature with a triple tuple is hard to follow—consider introducing a small Cover value class or data object to encapsulate type, details, and offset instead of passing three separate values.
  • There’s a lot of repeated logic for wrapping images (Image.asset, Image.file, network) with DesktopCoverAlign; extracting a single helper that accepts an ImageProvider and returns the aligned widget would reduce duplication.
  • The DesktopCoverAlign widget manually manages ImageStream and listeners, which is verbose and error-prone; consider leveraging Flutter’s built-in Image widget callbacks or a simpler approach to avoid potential memory leaks.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The new onChangeCover signature with a triple tuple is hard to follow—consider introducing a small Cover value class or data object to encapsulate type, details, and offset instead of passing three separate values.
- There’s a lot of repeated logic for wrapping images (`Image.asset`, `Image.file`, network) with DesktopCoverAlign; extracting a single helper that accepts an ImageProvider and returns the aligned widget would reduce duplication.
- The DesktopCoverAlign widget manually manages ImageStream and listeners, which is verbose and error-prone; consider leveraging Flutter’s built-in Image widget callbacks or a simpler approach to avoid potential memory leaks.

## Individual Comments

### Comment 1
<location> `frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/header/desktop_cover_align.dart:32` </location>
<code_context>
+
+  Alignment get alignment => _adjustedAlign;
+
+  void reset() {
+    _adjustedAlign = Alignment.center;
+    notifyListeners();
+  }
</code_context>

<issue_to_address>
reset() always sets alignment to center, not to initial value.

reset() should set _adjustedAlign to _initialAlignment to properly restore the original alignment.
</issue_to_address>

### Comment 2
<location> `frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/header/desktop_cover_align.dart:42` </location>
<code_context>
+    notifyListeners();
+  }
+
+  void changeAlign(double x, double y) {
+    _adjustedAlign = Alignment(x, y);
+  }
+
</code_context>

<issue_to_address>
changeAlign does not notify listeners after updating alignment.

Please add notifyListeners() at the end of changeAlign to ensure UI updates when alignment changes.
</issue_to_address>

### Comment 3
<location> `frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/header/desktop_cover_align.dart:172` </location>
<code_context>
+      final frameRatio = _frameSize!.aspectRatio;
+      final isVertical = imageRatio < frameRatio;
+
+      final imageFrameHeight =
+          _frameSize!.width / _imageSize!.width * _imageSize!.height;
+      final imageFrameWidth =
+          _frameSize!.height / _imageSize!.height * _imageSize!.width;
</code_context>

<issue_to_address>
Potential division by zero in imageFrameHeight and imageFrameWidth calculations.

Add checks to ensure _imageSize!.width and _imageSize!.height are not zero before performing these calculations.
</issue_to_address>

### Comment 4
<location> `frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/header/desktop_cover_align.dart:181` </location>
<code_context>
+
+      if (isVertical) {
+        final targetY = y + offset.dy / exceedHeight * 2;
+        if (targetY >= -1 && targetY <= 1) {
+          y = targetY;
+        }
+      } else {
</code_context>

<issue_to_address>
No clamping for x/y values outside [-1, 1] range.

Clamping targetX and targetY to [-1, 1] would ensure the UI remains responsive even with large or fast drags.
</issue_to_address>

<suggested_fix>
<<<<<<< SEARCH
      if (isVertical) {
        final targetY = y + offset.dy / exceedHeight * 2;
        if (targetY >= -1 && targetY <= 1) {
          y = targetY;
        }
      } else {
        final targetX = x + offset.dx / exceedWidth * 2;
        if (targetX >= -1 && targetX <= 1) {
          x = targetX;
        }
      }
=======
      if (isVertical) {
        final targetY = y + offset.dy / exceedHeight * 2;
        y = targetY.clamp(-1.0, 1.0);
      } else {
        final targetX = x + offset.dx / exceedWidth * 2;
        x = targetX.clamp(-1.0, 1.0);
      }
>>>>>>> REPLACE

</suggested_fix>

### Comment 5
<location> `frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/header/document_cover_widget.dart:330` </location>
<code_context>
     if (cover != null) {
       attributes[DocumentHeaderBlockKeys.coverType] = cover.$1.toString();
       attributes[DocumentHeaderBlockKeys.coverDetails] = cover.$2;
+      attributes[DocumentHeaderBlockKeys.coverOffset] = cover.$3;
     }
     if (icon != null) {
</code_context>

<issue_to_address>
coverOffset is always set, even if null.

Only assign attributes[DocumentHeaderBlockKeys.coverOffset] if cover.$3 is not null to avoid introducing null values into the attribute map.
</issue_to_address>

<suggested_fix>
<<<<<<< SEARCH
    if (cover != null) {
      attributes[DocumentHeaderBlockKeys.coverType] = cover.$1.toString();
      attributes[DocumentHeaderBlockKeys.coverDetails] = cover.$2;
      attributes[DocumentHeaderBlockKeys.coverOffset] = cover.$3;
    }
=======
    if (cover != null) {
      attributes[DocumentHeaderBlockKeys.coverType] = cover.$1.toString();
      attributes[DocumentHeaderBlockKeys.coverDetails] = cover.$2;
      if (cover.$3 != null) {
        attributes[DocumentHeaderBlockKeys.coverOffset] = cover.$3;
      }
    }
>>>>>>> REPLACE

</suggested_fix>

### Comment 6
<location> `frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/header/desktop_cover.dart:66` </location>
<code_context>
+
+  @override
+  void didUpdateWidget(covariant DesktopCover oldWidget) {
+    if (widget.coverDetails != oldWidget.coverDetails) {
+      coverAlignController.reset();
+    }
+    super.didUpdateWidget(oldWidget);
</code_context>

<issue_to_address>
reset() on coverAlignController may not restore previous alignment.

Currently, reset() always sets alignment to center, which may not match the new coverDetails. Please update the controller to initialize alignment based on the new coverDetails.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment on lines +32 to +33
void reset() {
_adjustedAlign = Alignment.center;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (bug_risk): reset() always sets alignment to center, not to initial value.

reset() should set _adjustedAlign to _initialAlignment to properly restore the original alignment.

Comment on lines +42 to +43
void changeAlign(double x, double y) {
_adjustedAlign = Alignment(x, y);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (bug_risk): changeAlign does not notify listeners after updating alignment.

Please add notifyListeners() at the end of changeAlign to ensure UI updates when alignment changes.

Comment on lines +172 to +173
final imageFrameHeight =
_frameSize!.width / _imageSize!.width * _imageSize!.height;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (bug_risk): Potential division by zero in imageFrameHeight and imageFrameWidth calculations.

Add checks to ensure _imageSize!.width and _imageSize!.height are not zero before performing these calculations.

Comment on lines +179 to +189
if (isVertical) {
final targetY = y + offset.dy / exceedHeight * 2;
if (targetY >= -1 && targetY <= 1) {
y = targetY;
}
} else {
final targetX = x + offset.dx / exceedWidth * 2;
if (targetX >= -1 && targetX <= 1) {
x = targetX;
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (bug_risk): No clamping for x/y values outside [-1, 1] range.

Clamping targetX and targetY to [-1, 1] would ensure the UI remains responsive even with large or fast drags.

Suggested change
if (isVertical) {
final targetY = y + offset.dy / exceedHeight * 2;
if (targetY >= -1 && targetY <= 1) {
y = targetY;
}
} else {
final targetX = x + offset.dx / exceedWidth * 2;
if (targetX >= -1 && targetX <= 1) {
x = targetX;
}
}
if (isVertical) {
final targetY = y + offset.dy / exceedHeight * 2;
y = targetY.clamp(-1.0, 1.0);
} else {
final targetX = x + offset.dx / exceedWidth * 2;
x = targetX.clamp(-1.0, 1.0);
}

Comment on lines 330 to 334
if (cover != null) {
attributes[DocumentHeaderBlockKeys.coverType] = cover.$1.toString();
attributes[DocumentHeaderBlockKeys.coverDetails] = cover.$2;
attributes[DocumentHeaderBlockKeys.coverOffset] = cover.$3;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (bug_risk): coverOffset is always set, even if null.

Only assign attributes[DocumentHeaderBlockKeys.coverOffset] if cover.$3 is not null to avoid introducing null values into the attribute map.

Suggested change
if (cover != null) {
attributes[DocumentHeaderBlockKeys.coverType] = cover.$1.toString();
attributes[DocumentHeaderBlockKeys.coverDetails] = cover.$2;
attributes[DocumentHeaderBlockKeys.coverOffset] = cover.$3;
}
if (cover != null) {
attributes[DocumentHeaderBlockKeys.coverType] = cover.$1.toString();
attributes[DocumentHeaderBlockKeys.coverDetails] = cover.$2;
if (cover.$3 != null) {
attributes[DocumentHeaderBlockKeys.coverOffset] = cover.$3;
}
}

Comment on lines +66 to +67
if (widget.coverDetails != oldWidget.coverDetails) {
coverAlignController.reset();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (bug_risk): reset() on coverAlignController may not restore previous alignment.

Currently, reset() always sets alignment to center, which may not match the new coverDetails. Please update the controller to initialize alignment based on the new coverDetails.

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.

1 participant