-
Couldn't load subscription status.
- Fork 2.2k
feat(layers): support per-object text max width #9747
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -143,6 +143,11 @@ type _TextLayerProps<DataT> = { | |||
| * @default -1 | ||||
| */ | ||||
| maxWidth?: number; | ||||
| /** | ||||
| * Per-object width limit accessor. Returns the same unit as `maxWidth`. | ||||
| * @default -1 | ||||
| */ | ||||
| getMaxWidth?: Accessor<DataT, number>; | ||||
| /** | ||||
| * Label text accessor | ||||
| */ | ||||
|
|
@@ -214,6 +219,7 @@ const defaultProps: DefaultProps<TextLayerProps> = { | |||
| // auto wrapping options | ||||
| wordBreak: 'break-word', | ||||
| maxWidth: {type: 'number', value: -1}, | ||||
| getMaxWidth: {type: 'accessor', value: -1}, | ||||
|
|
||||
| getText: {type: 'accessor', value: (x: any) => x.text}, | ||||
| getPosition: {type: 'accessor', value: (x: any) => x.position}, | ||||
|
|
@@ -274,7 +280,10 @@ export default class TextLayer<DataT = any, ExtraPropsT extends {} = {}> extends | |||
| fontChanged || | ||||
| props.lineHeight !== oldProps.lineHeight || | ||||
| props.wordBreak !== oldProps.wordBreak || | ||||
| props.maxWidth !== oldProps.maxWidth; | ||||
| props.maxWidth !== oldProps.maxWidth || | ||||
| props.getMaxWidth !== oldProps.getMaxWidth || | ||||
| (changeFlags.updateTriggersChanged && | ||||
| (changeFlags.updateTriggersChanged.all || changeFlags.updateTriggersChanged.getMaxWidth)); | ||||
|
|
||||
| if (styleChanged) { | ||||
| this.setState({ | ||||
|
|
@@ -387,14 +396,25 @@ export default class TextLayer<DataT = any, ExtraPropsT extends {} = {}> extends | |||
| const {fontAtlasManager} = this.state; | ||||
| const iconMapping = fontAtlasManager.mapping!; | ||||
| const getText = this.state.getText!; | ||||
| const {wordBreak, lineHeight, maxWidth} = this.props; | ||||
| const {wordBreak, lineHeight, maxWidth, getMaxWidth} = this.props; | ||||
|
|
||||
| let objectMaxWidth = maxWidth; | ||||
| const maxWidthAccessor = getMaxWidth; | ||||
| if (typeof maxWidthAccessor === 'function') { | ||||
| const value = maxWidthAccessor(object, objectInfo); | ||||
| if (Number.isFinite(value) && value >= 0) { | ||||
| objectMaxWidth = value; | ||||
| } | ||||
| } else if (Number.isFinite(maxWidthAccessor) && maxWidthAccessor >= 0) { | ||||
| objectMaxWidth = maxWidthAccessor; | ||||
|
||||
| objectMaxWidth = maxWidthAccessor; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] The variable
maxWidthAccessoris unnecessary sincegetMaxWidthis already descriptive. Consider usinggetMaxWidthdirectly in the conditional checks below to reduce code complexity.