@tldraw/tlschema
Version:
tldraw infinite canvas SDK (schema).
125 lines (124 loc) • 3.42 kB
JavaScript
import { T } from "@tldraw/validate";
class StyleProp {
/** @internal */
constructor(id, defaultValue, type) {
this.id = id;
this.defaultValue = defaultValue;
this.type = type;
}
id;
defaultValue;
type;
/**
* Define a new {@link StyleProp}.
*
* @param uniqueId - Each StyleProp must have a unique ID. We recommend you prefix this with
* your app/library name.
* @param options -
* - `defaultValue`: The default value for this style prop.
*
* - `type`: Optionally, describe what type of data you expect for this style prop.
*
* @example
* ```ts
* import {T} from '@tldraw/validate'
* import {StyleProp} from '@tldraw/tlschema'
*
* const MyLineWidthProp = StyleProp.define('myApp:lineWidth', {
* defaultValue: 1,
* type: T.number,
* })
* ```
* @public
*/
static define(uniqueId, options) {
const { defaultValue, type = T.any } = options;
return new StyleProp(uniqueId, defaultValue, type);
}
/**
* Define a new {@link StyleProp} as a list of possible values.
*
* @param uniqueId - Each StyleProp must have a unique ID. We recommend you prefix this with
* your app/library name.
* @param options -
* - `defaultValue`: The default value for this style prop.
*
* - `values`: An array of possible values of this style prop.
*
* @example
* ```ts
* import {StyleProp} from '@tldraw/tlschema'
*
* const MySizeProp = StyleProp.defineEnum('myApp:size', {
* defaultValue: 'medium',
* values: ['small', 'medium', 'large'],
* })
* ```
*/
static defineEnum(uniqueId, options) {
const { defaultValue, values } = options;
return new EnumStyleProp(uniqueId, defaultValue, values);
}
setDefaultValue(value) {
this.defaultValue = value;
}
validate(value) {
return this.type.validate(value);
}
validateUsingKnownGoodVersion(prevValue, newValue) {
if (this.type.validateUsingKnownGoodVersion) {
return this.type.validateUsingKnownGoodVersion(prevValue, newValue);
} else {
return this.validate(newValue);
}
}
}
class EnumStyleProp extends StyleProp {
/** @internal */
constructor(id, defaultValue, values) {
super(id, defaultValue, T.literalEnum(...values));
this.values = [...values];
}
values;
/**
* Add new values to this enum style prop at runtime. This is useful for extending
* the built-in styles with custom values (e.g. adding custom colors). Be sure to
* also modify the associated types.
*
* @param newValues - The new values to add.
*
* @public
*/
addValues(...newValues) {
for (const v of newValues) {
if (!this.values.includes(v)) {
this.values.push(v);
}
}
;
this.type = T.literalEnum(...this.values);
}
/**
* Remove values from this enum style prop at runtime. This is useful for narrowing
* the built-in styles with custom values (e.g. adding custom colors). Be sure to
* also modify the associated types.
*
* @param valuesToRemove - The values to remove.
*
* @public
*/
removeValues(...valuesToRemove) {
for (const v of valuesToRemove) {
if (this.values.includes(v)) {
this.values.splice(this.values.indexOf(v), 1);
}
}
;
this.type = T.literalEnum(...this.values);
}
}
export {
EnumStyleProp,
StyleProp
};
//# sourceMappingURL=StyleProp.mjs.map