Skip to content

Commit bccc2ef

Browse files
authored
fix(dia.attributes): fix to take the inline font attributes into acco… (#2679)
1 parent a919a42 commit bccc2ef

File tree

2 files changed

+50
-14
lines changed

2 files changed

+50
-14
lines changed

packages/joint-core/src/dia/attributes/text.mjs

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ function isTextInUse(_value, _node, attrs) {
77
return (attrs.text !== undefined);
88
}
99

10+
const FONT_ATTRIBUTES = ['font-weight', 'font-family', 'font-size', 'letter-spacing', 'text-transform'];
11+
1012
const textAttributesNS = {
1113

1214
'line-height': {
@@ -122,18 +124,28 @@ const textAttributesNS = {
122124
var text = value.text;
123125
if (text === undefined) text = attrs.text;
124126
if (text !== undefined) {
127+
125128
const breakTextFn = value.breakText || breakText;
126129
const computedStyles = getComputedStyle(node);
130+
const wrapFontAttributes = {};
131+
// The font size attributes must be set on the node
132+
// to get the correct text wrapping.
133+
// TODO: set the native SVG attributes before special attributes
134+
for (let i = 0; i < FONT_ATTRIBUTES.length; i++) {
135+
const name = FONT_ATTRIBUTES[i];
136+
if (name in attrs) {
137+
node.setAttribute(name, attrs[name]);
138+
}
139+
// Note: computedStyles is a live object
140+
// i.e. the properties are evaluated when accessed.
141+
wrapFontAttributes[name] = computedStyles[name];
142+
}
127143

128-
wrappedText = breakTextFn('' + text, size, {
129-
'font-weight': computedStyles.fontWeight,
130-
'font-family': computedStyles.fontFamily,
131-
'text-transform': computedStyles.textTransform,
132-
'font-size': computedStyles.fontSize,
133-
'letter-spacing': computedStyles.letterSpacing,
134-
// The `line-height` attribute in SVG is JoinJS specific.
135-
'lineHeight': attrs['line-height'],
136-
}, {
144+
// The `line-height` attribute in SVG is JoinJS specific.
145+
// TODO: change the `lineHeight` to breakText option.
146+
wrapFontAttributes.lineHeight = attrs['line-height'];
147+
148+
wrappedText = breakTextFn('' + text, size, wrapFontAttributes, {
137149
// Provide an existing SVG Document here
138150
// instead of creating a temporary one over again.
139151
svgDocument: this.paper.svg,
@@ -147,7 +159,11 @@ const textAttributesNS = {
147159
wrappedText = '';
148160
}
149161
textAttributesNS.text.set.call(this, wrappedText, refBBox, node, attrs);
150-
}
162+
},
163+
// We expose the font attributes list to allow
164+
// the user to take other custom font attributes into account
165+
// when wrapping the text.
166+
FONT_ATTRIBUTES
151167
},
152168

153169
'title': {

packages/joint-core/test/jointjs/dia/attributes.js

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,14 @@ QUnit.module('Attributes', function() {
104104
return obj.width === refBBox.width - 11 && obj.height === refBBox.height - 13;
105105
})));
106106

107-
// external css styles taken into account
108-
spy.resetHistory();
107+
spy.restore();
108+
});
109+
110+
QUnit.test('takes external CSS into account', function(assert) {
111+
112+
const spy = sinon.spy(joint.util, 'breakText');
113+
114+
// no text
109115
const fontSize = '23px';
110116
const fontFamily = 'Arial';
111117
const fontWeight = '800';
@@ -121,7 +127,21 @@ QUnit.module('Attributes', function() {
121127
}
122128
`);
123129
paper.svg.prepend(stylesheet);
124-
textWrap.set.call(cellView, { text: 'text', breakText: spy }, bbox, node, {});
130+
131+
const el = new joint.shapes.standard.Rectangle({
132+
attrs: {
133+
label: {
134+
text: 'text',
135+
textWrap: {
136+
breakText: spy
137+
}
138+
}
139+
}
140+
141+
});
142+
el.addTo(graph);
143+
paper.requireView(el);
144+
125145
assert.ok(spy.calledWith(
126146
sinon.match.string,
127147
sinon.match.object,
@@ -134,8 +154,8 @@ QUnit.module('Attributes', function() {
134154
obj['font-weight'] === fontWeight
135155
);
136156
})));
137-
stylesheet.remove();
138157

158+
stylesheet.remove();
139159
spy.restore();
140160
});
141161

0 commit comments

Comments
 (0)