Skip to content

feat(svgcanvas): Trigger 'changed' event on text attribute modifications#1085

Merged
jfhenon merged 2 commits into
SVG-Edit:masterfrom
shfshanyue:master
Mar 19, 2026
Merged

feat(svgcanvas): Trigger 'changed' event on text attribute modifications#1085
jfhenon merged 2 commits into
SVG-Edit:masterfrom
shfshanyue:master

Conversation

@shfshanyue
Copy link
Copy Markdown
Contributor

@shfshanyue shfshanyue commented Mar 19, 2026

This update adds a call to the 'changed' event for various text attribute methods, ensuring that changes to text elements are properly tracked and reflected in the SVG canvas. The methods updated include setBold, setItalic, setTextAnchor, setLetterSpacing, setWordSpacing, setTextLength, setLengthAdjust, setFontFamily, setFontColor, and setFontSize.

PR description

Checklist

Note that we require UI tests to ensure that the added feature will not be
nixed by some future fix and that there is at least some test-as-documentation
to indicate how the fix or enhancement is expected to behave.

  • - Added Cypress UI tests
  • - Ran npm test, ensuring linting passes and that Cypress UI tests keep
    coverage to at least the same percent (reflected in the coverage badge
    that should be updated after the tests run)
  • - Added any user documentation. Though not required, this can be a big
    help both for future users and for the PR reviewer.

Summary by Sourcery

Ensure text formatting updates on SVG text elements trigger canvas change notifications.

Bug Fixes:

  • Trigger the global 'changed' event after modifying text attributes so text edits are consistently tracked and reflected in the canvas state.

Enhancements:

  • Standardize text attribute setters (bold, italic, spacing, anchor, font properties) to notify listeners after applying changes.

This update adds a call to the 'changed' event for various text attribute methods, ensuring that changes to text elements are properly tracked and reflected in the SVG canvas. The methods updated include setBold, setItalic, setTextAnchor, setLetterSpacing, setWordSpacing, setTextLength, setLengthAdjust, setFontFamily, setFontColor, and setFontSize.
@sourcery-ai
Copy link
Copy Markdown

sourcery-ai Bot commented Mar 19, 2026

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

This PR updates SVG text attribute setters in svgcanvas so that any change to text styling or layout consistently fires the global 'changed' event, allowing downstream listeners (e.g., history/undo, UI sync) to track text modifications as they happen.

Sequence diagram for SVG text attribute update triggering changed event

sequenceDiagram
  actor User
  participant TextTool
  participant TextAttributeSetters
  participant SvgCanvas
  participant HistoryManager
  participant UiSync

  User->>TextTool: change font size
  TextTool->>TextAttributeSetters: setFontSizeMethod(val)
  TextAttributeSetters->>SvgCanvas: getSelectedElements()
  SvgCanvas-->>TextAttributeSetters: selectedElements
  TextAttributeSetters->>SvgCanvas: changeSelectedAttribute(font-size, val, selectedElements)
  alt firstCharacterBeingTyped
    TextAttributeSetters->>SvgCanvas: textActions.setCursor()
  end
  TextAttributeSetters->>SvgCanvas: call(changed, selectedElements)
  SvgCanvas->>HistoryManager: onChanged(selectedElements)
  SvgCanvas->>UiSync: onChanged(selectedElements)
Loading

Sequence diagram for SVG text color update triggering changed event

sequenceDiagram
  actor User
  participant ColorPicker
  participant TextAttributeSetters
  participant SvgCanvas
  participant HistoryManager
  participant UiSync

  User->>ColorPicker: pick text color
  ColorPicker->>TextAttributeSetters: setFontColorMethod(val)
  TextAttributeSetters->>SvgCanvas: getSelectedElements()
  SvgCanvas-->>TextAttributeSetters: selectedElements
  TextAttributeSetters->>SvgCanvas: setCurText(fill, val)
  TextAttributeSetters->>SvgCanvas: changeSelectedAttribute(fill, val)
  TextAttributeSetters->>SvgCanvas: call(changed, selectedElements)
  SvgCanvas->>HistoryManager: onChanged(selectedElements)
  SvgCanvas->>UiSync: onChanged(selectedElements)
Loading

Class diagram for updated svgcanvas text attribute setter methods

classDiagram
  class TextAttributeSetters {
    +setBoldMethod(b)
    +setItalicMethod(i)
    +setTextAnchorMethod(value)
    +setLetterSpacingMethod(value)
    +setWordSpacingMethod(value)
    +setTextLengthMethod(value)
    +setLengthAdjustMethod(value)
    +setFontFamilyMethod(val)
    +setFontColorMethod(val)
    +setFontSizeMethod(val)
  }

  class SvgCanvas {
    +getSelectedElements()
    +changeSelectedAttribute(name, value, elements)
    +setCurText(name, value)
    +call(eventName, elements)
    +textActions
  }

  class TextActions {
    +setCursor()
  }

  class HistoryManager {
    +onChanged(elements)
  }

  class UiSync {
    +onChanged(elements)
  }

  SvgCanvas *-- TextActions
  TextAttributeSetters ..> SvgCanvas : uses
  SvgCanvas ..> HistoryManager : emits_changed
  SvgCanvas ..> UiSync : emits_changed
Loading

File-Level Changes

Change Details Files
Ensure all text attribute setter methods trigger the svgCanvas 'changed' event after applying updates.
  • After setBold modifies the selection and potentially updates the text cursor, it now calls svgCanvas.call('changed', selectedElements).
  • After setItalic modifies the selection and potentially updates the text cursor, it now calls svgCanvas.call('changed', selectedElements).
  • After setTextAnchor updates the 'text-anchor' attribute on selected text elements, it now calls svgCanvas.call('changed', selectedElements).
  • After setLetterSpacing, setWordSpacing, setTextLength, setLengthAdjust, and setFontFamily update text elements and possibly reset the cursor, they now call svgCanvas.call('changed', selectedElements).
  • setFontColor now first retrieves selected elements, then updates text fill via setCurText and changeSelectedAttribute, and finally calls svgCanvas.call('changed', selectedElements).
  • After setFontSize updates the font size of selected text and possibly resets the cursor, it now calls svgCanvas.call('changed', selectedElements).
packages/svgcanvas/core/elem-get-set.js

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

Copy link
Copy Markdown

@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 - I've left some high level feedback:

  • The new svgCanvas.call('changed', selectedElements) calls are repeated with the same pattern across many setters; consider extracting a small helper (e.g., notifyTextChange(selectedElements)) to reduce duplication and keep future behavior changes centralized.
  • There is an inconsistency in what gets passed to changed: several methods operate on textElements but call changed with selectedElements (and setFontColorMethod now fetches selectedElements just for this); consider standardizing whether the event should receive all selected elements or only the text elements actually modified.
  • You might want to guard the svgCanvas.call('changed', ...) calls so they only fire when there is at least one relevant text element or when the attribute value actually changed, to avoid emitting redundant change events on no-op calls.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The new `svgCanvas.call('changed', selectedElements)` calls are repeated with the same pattern across many setters; consider extracting a small helper (e.g., `notifyTextChange(selectedElements)`) to reduce duplication and keep future behavior changes centralized.
- There is an inconsistency in what gets passed to `changed`: several methods operate on `textElements` but call `changed` with `selectedElements` (and `setFontColorMethod` now fetches `selectedElements` just for this); consider standardizing whether the event should receive all selected elements or only the text elements actually modified.
- You might want to guard the `svgCanvas.call('changed', ...)` calls so they only fire when there is at least one relevant text element or when the attribute value actually changed, to avoid emitting redundant change events on no-op calls.

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.

…anged' event emission

This update introduces helper functions to streamline the handling of selected text elements and their attribute changes. The methods setBold, setItalic, setTextAnchor, setLetterSpacing, setWordSpacing, and setTextLength now utilize these helpers to filter and notify only modified text elements, improving performance and clarity in event emissions.
@jfhenon jfhenon merged commit f6fbc8d into SVG-Edit:master Mar 19, 2026
8 checks passed
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