feat(svgcanvas): Trigger 'changed' event on text attribute modifications (#1085)
* feat(svgcanvas): Trigger 'changed' event on text attribute modifications 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. * refactor(svgcanvas): Simplify text attribute methods and optimize 'changed' 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.
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { beforeEach, afterEach, describe, expect, it } from 'vitest'
|
||||
import { beforeEach, afterEach, describe, expect, it, vi } from 'vitest'
|
||||
import { NS } from '../../packages/svgcanvas/core/namespaces.js'
|
||||
import * as history from '../../packages/svgcanvas/core/history.js'
|
||||
import dataStorage from '../../packages/svgcanvas/core/dataStorage.js'
|
||||
@@ -35,7 +35,8 @@ describe('elem-get-set', () => {
|
||||
clear () {}
|
||||
},
|
||||
runExtensions () {},
|
||||
call () {},
|
||||
call: vi.fn(),
|
||||
changeSelectedAttribute: vi.fn(),
|
||||
getDOMDocument () { return document },
|
||||
getSvgContent () { return svgContent },
|
||||
getSelectedElements () { return this.selectedElements || [] },
|
||||
@@ -51,6 +52,10 @@ describe('elem-get-set', () => {
|
||||
},
|
||||
addCommandToHistory (cmd) {
|
||||
historyStack.push(cmd)
|
||||
},
|
||||
setCurText () {},
|
||||
textActions: {
|
||||
setCursor () {}
|
||||
}
|
||||
}
|
||||
svgContent.setAttribute('width', '100')
|
||||
@@ -232,4 +237,43 @@ describe('elem-get-set', () => {
|
||||
expect(localCanvas.contentW).toBe(200)
|
||||
expect(localCanvas.contentH).toBe(150)
|
||||
})
|
||||
|
||||
it('setBold() emits changed only for modified text elements', () => {
|
||||
const text = createSvgElement('text')
|
||||
text.textContent = 'Hello'
|
||||
const rect = createSvgElement('rect')
|
||||
svgContent.append(text, rect)
|
||||
canvas.selectedElements = [text, rect]
|
||||
|
||||
canvas.setBold(true)
|
||||
|
||||
expect(canvas.changeSelectedAttribute).toHaveBeenCalledWith('font-weight', 'bold', [text])
|
||||
expect(canvas.call).toHaveBeenCalledWith('changed', [text])
|
||||
})
|
||||
|
||||
it('setBold() skips changed event for no-op text updates', () => {
|
||||
const text = createSvgElement('text')
|
||||
text.textContent = 'Hello'
|
||||
text.setAttribute('font-weight', 'bold')
|
||||
svgContent.append(text)
|
||||
canvas.selectedElements = [text]
|
||||
|
||||
canvas.setBold(true)
|
||||
|
||||
expect(canvas.changeSelectedAttribute).not.toHaveBeenCalled()
|
||||
expect(canvas.call).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('setFontColor() ignores non-text selections when emitting changed', () => {
|
||||
const text = createSvgElement('text')
|
||||
text.textContent = 'Hello'
|
||||
const rect = createSvgElement('rect')
|
||||
svgContent.append(text, rect)
|
||||
canvas.selectedElements = [text, rect]
|
||||
|
||||
canvas.setFontColor('#f00')
|
||||
|
||||
expect(canvas.changeSelectedAttribute).toHaveBeenCalledWith('fill', '#f00', [text])
|
||||
expect(canvas.call).toHaveBeenCalledWith('changed', [text])
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user