Skip to content
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
2 changes: 1 addition & 1 deletion src/display/components/Text.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { Tintable } from '../mixins/Tintable';
import { mixins } from '../mixins/utils';

const EXTRA_KEYS = {
PLACEMENT: ['text', 'split'],
PLACEMENT: ['text', 'style', 'split'],
};

const ComposedText = mixins(
Expand Down
9 changes: 9 additions & 0 deletions src/display/data-schema/data.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,15 @@ export interface TextStyle {
*/
wordWrapWidth?: number | 'auto';

/**
* Determines how to handle text that overflows its content area.
* - 'visible': Text flows outside the bounds (default).
* - 'hidden': Text exceeding the bounds is clipped.
* - 'ellipsis': Text exceeding the bounds is replaced with '...'.
* @default 'visible'
*/
overflow?: 'visible' | 'hidden' | 'ellipsis';

/**
* Allows any other properties, similar to PIXI.TextStyleOptions.
* This provides flexibility for standard text styling.
Expand Down
7 changes: 4 additions & 3 deletions src/display/data-schema/primitive-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import {
DEFAULT_TEXTSTYLE,
} from '../mixins/constants';
import {
HslColor,
HslaColor,
HsvColor,
HslColor,
HsvaColor,
HsvColor,
Color as PixiColor,
RgbColor,
RgbaColor,
RgbColor,
} from './color-schema';

/**
Expand Down Expand Up @@ -244,6 +244,7 @@ export const TextStyle = z
fontWeight: z.any().default(DEFAULT_TEXTSTYLE.fontWeight),
fill: z.any().default(DEFAULT_TEXTSTYLE.fill),
wordWrapWidth: z.union([z.number(), z.literal('auto')]).optional(),
overflow: z.enum(['visible', 'hidden', 'ellipsis']).default('visible'),
})
.passthrough()
.default({});
4 changes: 3 additions & 1 deletion src/display/data-schema/primitive-schema.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import {
Margin,
Placement,
PxOrPercentSize,
pxOrPercentSchema,
RelationsStyle,
Size,
TextStyle,
TextureStyle,
pxOrPercentSchema,
} from './primitive-schema';

vi.mock('../../utils/uuid');
Expand Down Expand Up @@ -359,6 +359,7 @@ describe('Primitive Schema Tests', () => {
fontFamily: 'FiraCode',
fontSize: 16,
fontWeight: 400,
overflow: 'visible',
});
});

Expand All @@ -369,6 +370,7 @@ describe('Primitive Schema Tests', () => {
fontFamily: 'Arial',
fontWeight: 'bold',
fill: 'red',
overflow: 'visible',
});
});
});
Expand Down
8 changes: 5 additions & 3 deletions src/display/mixins/Base.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export const Base = (superClass) => {
}

static registerHandler(keys, handler, stage) {
if (!Object.prototype.hasOwnProperty.call(this, '_handlerRegistry')) {
if (!Object.hasOwn(this, '_handlerRegistry')) {
this._handlerRegistry = new Map(this._handlerRegistry);
this._handlerMap = new Map(this._handlerMap);
}
Expand All @@ -58,7 +58,9 @@ export const Base = (superClass) => {
keys: new Set(),
stage: stage ?? 99,
};
keys.forEach((key) => registration.keys.add(key));
keys.forEach((key) => {
registration.keys.add(key);
});
this._handlerRegistry.set(handler, registration);
registration.keys.forEach((key) => {
if (!this._handlerMap.has(key)) this._handlerMap.set(key, new Set());
Expand Down Expand Up @@ -116,7 +118,7 @@ export const Base = (superClass) => {
this.constructor._handlerRegistry.get(handler).keys;
const fullPayload = {};
keysForHandler.forEach((key) => {
if (Object.prototype.hasOwnProperty.call(this.props, key)) {
if (Object.hasOwn(this.props, key)) {
fullPayload[key] = this.props[key];
}
});
Expand Down
16 changes: 4 additions & 12 deletions src/display/mixins/Textable.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { UPDATE_STAGES } from './constants';
import { splitText } from './utils';

const KEYS = ['text', 'split'];

export const Textable = (superClass) => {
const MixedClass = class extends superClass {
_applyText(relevantChanges) {
const { text, split } = relevantChanges;
this.text = splitText(text, split);
this._fullText = splitText(text, split);
this.text = this._fullText;
this._isTruncated = false;
}
};
MixedClass.registerHandler(
Expand All @@ -16,14 +19,3 @@ export const Textable = (superClass) => {
);
return MixedClass;
};

const splitText = (text, split) => {
if (split === 0) {
return text;
}
let result = '';
for (let i = 0; i < text.length; i += split) {
result += `${text.slice(i, i + split)}\n`;
}
return result.trim();
};
48 changes: 47 additions & 1 deletion src/display/mixins/Textstyleable.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
FONT_WEIGHT,
UPDATE_STAGES,
} from './constants';
import { getLayoutContext } from './utils';
import { getLayoutContext, splitText } from './utils';

const KEYS = ['text', 'split', 'style', 'margin'];

Expand All @@ -19,6 +19,13 @@ export const Textstyleable = (superClass) => {
this.style = new TextStyle();
}

if (this._isTruncated) {
this.text =
this._fullText ??
splitText(this.props.text || '', this.props.split || 0);
this._isTruncated = false;
}

for (const key in style) {
if (
(key === 'fontSize' || key === 'wordWrapWidth') &&
Expand All @@ -45,6 +52,10 @@ export const Textstyleable = (superClass) => {
const range = style.autoFont ?? DEFAULT_AUTO_FONT_RANGE;
setAutoFontSize(this, margin, range);
}

if (style.overflow && style.overflow !== 'visible') {
applyOverflow(this, margin, style.overflow, this.text);
}
}

_getFontFamily(value) {
Expand Down Expand Up @@ -88,6 +99,41 @@ const setAutoWordWrapWidth = (object, margin) => {
object.style.wordWrapWidth = contentWidth;
};

const applyOverflow = (object, margin, overflowType, fullText) => {
const { contentWidth, contentHeight } = getContentSize(object, margin);
const bounds = object.getLocalBounds();

if (bounds.width <= contentWidth && bounds.height <= contentHeight) {
return;
}

const suffix = overflowType === 'ellipsis' ? '…' : '';
let low = 0;
let high = fullText.length;
let bestFitText = '';

while (low <= high) {
const mid = Math.floor((low + high) / 2);
const sliced = fullText.slice(0, mid);
const candidate = mid === fullText.length ? sliced : sliced + suffix;

if (doesFit(candidate)) {
bestFitText = candidate;
low = mid + 1;
} else {
high = mid - 1;
}
}
object.text = bestFitText;
object._isTruncated = bestFitText !== fullText;

function doesFit(text) {
object.text = text;
const b = object.getLocalBounds();
return b.width <= contentWidth && b.height <= contentHeight;
}
};

const getContentSize = (object, margin) => {
const { contentWidth, contentHeight } = getLayoutContext(object);
return {
Expand Down
11 changes: 11 additions & 0 deletions src/display/mixins/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,14 @@ export const getLayoutContext = (component) => {
parentPadding,
};
};

export const splitText = (text, split) => {
if (!split || split === 0) {
return text;
}
let result = '';
for (let i = 0; i < text.length; i += split) {
result += `${text.slice(i, i + split)}\n`;
}
return result.trim();
};
Loading