devexpress-richedit
Version:
DevExpress Rich Text Editor is an advanced word-processing tool designed for working with rich text documents.
114 lines (113 loc) • 3.32 kB
JavaScript
import { isDefined } from "@devexpress/utils/lib/utils/common";
export class MixedSize {
constructor() {
this._UISizePart = 0;
this._layoutSizePart = 0;
this._scale = 1;
this._scaleIsSpecified = false;
}
static fromLayout(value) {
return new MixedSize().addLayoutSize(value);
}
static fromUI(value) {
return new MixedSize().addUISize(value);
}
static min(a, b) {
a.checkSizeHasSameScale(b);
return a.LayoutSize < b.LayoutSize ? a : b;
}
static max(a, b) {
a.checkSizeHasSameScale(b);
return a.LayoutSize > b.LayoutSize ? a : b;
}
static substract(a, b) {
a.checkSizeHasSameScale(b);
return MixedSize.from(a).subtractSize(b);
}
static add(a, b) {
a.checkSizeHasSameScale(b);
return MixedSize.from(a).addSize(b);
}
static from(size) {
return new MixedSize().useScale(size.scale).addSize(size);
}
get scale() {
return this._scale;
}
get UISize() {
this.checkScaleIsSpecified();
if (!isDefined(this._UISize))
this._UISize = this._layoutSizePart * this._scale + this._UISizePart;
return this._UISize;
}
get LayoutSize() {
this.checkScaleIsSpecified();
if (!isDefined(this._layoutSize))
this._layoutSize = this._layoutSizePart + this._UISizePart / this._scale;
return this._layoutSize;
}
get UISizePart() {
return this._UISizePart;
}
set UISizePart(value) {
this._UISizePart = value;
this.onSizeChanged();
}
get LayoutSizePart() {
return this._layoutSizePart;
}
set LayoutSizePart(value) {
this._layoutSizePart = value;
this.onSizeChanged();
}
onSizeChanged() {
this._UISize = null;
this._layoutSize = null;
}
addUISize(value) {
this.UISizePart += value;
return this;
}
addLayoutSize(value) {
this.LayoutSizePart += value;
return this;
}
addSize(value) {
this.checkSizeHasSameScale(value);
this.addLayoutSize(value._layoutSizePart);
this.addUISize(value._UISizePart);
return this;
}
subtractUISize(value) {
this.UISizePart -= value;
return this;
}
subtractLayoutSize(value) {
this.LayoutSizePart -= value;
return this;
}
subtractSize(value) {
this.checkSizeHasSameScale(value);
this.subtractLayoutSize(value._layoutSizePart);
this.subtractUISize(value._UISizePart);
return this;
}
checkSizeHasSameScale(size) {
if (size._scaleIsSpecified && size._scale !== this._scale)
throw new Error("The size has a different scale and cannot be added.");
}
checkScaleIsSpecified() {
if (!this._scaleIsSpecified)
console.warn("MixedSize is used without specifying the scale.");
}
useScale(value) {
this._scale = value;
this._scaleIsSpecified = true;
return this;
}
clear() {
this.UISizePart = 0;
this.LayoutSizePart = 0;
return this;
}
}