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

fix: richtext support globalLineHeight support #1721

Merged
merged 2 commits into from
Feb 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/vrender-core/src/graphic/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,7 @@ export const DefaultTextAttribute: Required<ITextGraphicAttribute> = {
export const DefaultRichTextAttribute: Required<IRichTextGraphicAttribute> = {
...DefaultAttribute,
...DefaultTextStyle,
upgradeAttrs: null,
editable: false,
editOptions: null,
ascentDescentMode: 'actual',
Expand Down
19 changes: 14 additions & 5 deletions packages/vrender-core/src/graphic/richtext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -370,9 +370,11 @@ export class RichText extends Graphic<IRichTextGraphicAttribute> implements IRic
lineWidth,
opacity,
fillOpacity,
strokeOpacity
lineHeight,
strokeOpacity,
upgradeAttrs
} = this.attribute;
return {
const out = {
fill,
stroke,
fontSize,
Expand All @@ -385,6 +387,10 @@ export class RichText extends Graphic<IRichTextGraphicAttribute> implements IRic
strokeOpacity,
...config
};
if (upgradeAttrs?.lineHeight) {
out.lineHeight = lineHeight;
}
return out;
}
doUpdateFrameCache(tc?: IRichTextCharacter[]) {
// 1. 测量,生成paragraph
Expand All @@ -402,9 +408,12 @@ export class RichText extends Graphic<IRichTextGraphicAttribute> implements IRic
singleLine,
disableAutoWrapLine,
editable,
ascentDescentMode
ascentDescentMode,
upgradeAttrs
} = this.attribute;

const enableMultiBreakLine = upgradeAttrs && upgradeAttrs.multiBreakLine;

let { textConfig: _tc = [] } = this.attribute;

// 预处理editable,将textConfig中的text转换为单个字符
Expand Down Expand Up @@ -519,7 +528,7 @@ export class RichText extends Graphic<IRichTextGraphicAttribute> implements IRic
const wrapper = new Wrapper(frame);
// @since 0.22.0
// 如果可编辑的话,则支持多换行符
wrapper.newLine = editable;
wrapper.newLine = enableMultiBreakLine;
if (disableAutoWrapLine) {
let lineCount = 0;
let skip = false;
Expand Down Expand Up @@ -579,7 +588,7 @@ export class RichText extends Graphic<IRichTextGraphicAttribute> implements IRic
}

// 处理空行
if (editable) {
if (enableMultiBreakLine) {
frame.lines.forEach(item => {
const lastParagraphs = item.paragraphs;
item.paragraphs = item.paragraphs.filter(p => (p as any).text !== '');
Expand Down
9 changes: 8 additions & 1 deletion packages/vrender-core/src/graphic/richtext/paragraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,14 @@ export default class Paragraph {
textAlign: string,
lineHeight: number
) {
if (!(this.character.background && (!this.character.backgroundOpacity || this.character.backgroundOpacity > 0))) {
if (
!(
this.text !== '' &&
this.text !== '\n' &&
this.character.background &&
(!this.character.backgroundOpacity || this.character.backgroundOpacity > 0)
)
) {
return;
}
let baseline = top + ascent;
Expand Down
8 changes: 8 additions & 0 deletions packages/vrender-core/src/interface/graphic/richText.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ export type IRichTextEditOptionsType = {
};

export type IRichTextAttribute = {
/**
* @since 0.22.1
* 升级后避免break change,通过开关来控制
*/
upgradeAttrs: {
lineHeight: true;
multiBreakLine: true;
} | null;
/**
* 富文本的总宽度
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import type {
// }

export function getDefaultCharacterConfig(attribute: IRichTextGraphicAttribute) {
const { fill = 'black', stroke = false, fontWeight = 'normal', fontFamily = 'Arial' } = attribute;
const { fill = 'black', stroke = false, fontWeight = 'normal', lineHeight, fontFamily = 'Arial' } = attribute;
let { fontSize = 12 } = attribute;
if (!isFinite(fontSize)) {
fontSize = 12;
Expand All @@ -28,7 +28,8 @@ export function getDefaultCharacterConfig(attribute: IRichTextGraphicAttribute)
stroke,
fontSize,
fontWeight,
fontFamily
fontFamily,
lineHeight
} as any;
}

Expand Down
Loading
Loading