@tldraw/tlschema
Version:
tldraw infinite canvas SDK (schema).
145 lines (144 loc) • 4.43 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var StyleProp_exports = {};
__export(StyleProp_exports, {
EnumStyleProp: () => EnumStyleProp,
StyleProp: () => StyleProp
});
module.exports = __toCommonJS(StyleProp_exports);
var import_validate = require("@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 = import_validate.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, import_validate.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 = import_validate.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 = import_validate.T.literalEnum(...this.values);
}
}
//# sourceMappingURL=StyleProp.js.map