Extension自定义属性
1.自定义属性
Extension 中自定义的公开属性会自动生成对应的设置项,私有属性则不会。
私有属性指以#开头的属性,公开属性包括直接定义的公开属性和通过 get 方法定义的属性。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| class ExtensionSample { #a = 1; abc = "123"; get a() { return this.#a; } set a(a) { this.#a = a; } } export default ExtensionSample;
|
上面的代码会生成如下设置项:
2.自定义属性类型
上面的示例中生成的设置项,abc 类型为字符串,a 类型为数字。下面介绍所有的类型。
2.1 数字
1 2 3 4 5
| class PropertySample { setting = 123; } export default PropertySample;
|
数字属性生成样式如下:
2.2 数字拖动条
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| class PropertySample { setting = Decimal.default(10, { min: -180, max: 180, type: "int", step: 1, unit: "度", showInput: true, inputWidth: 50, }); onPropertyChanged(property) { if (property == "setting") { console.log(this.setting.value); } } } export default PropertySample;
|
数字拖动条属性生成效果如下:
2.3 字符串
1 2 3 4 5
| class PropertySample { setting = "Hello World"; } export default PropertySample;
|
字符串属性生成样式如下:
2.4 组件
1 2 3 4 5 6 7 8 9 10 11
| class PropertySample { setting = Element.empty(); onPropertyChanged(property) { if (property == "setting") { const element = this.setting; console.log("element name is " + element.name); } } } export default PropertySample;
|
组件属性生成样式如下:
2.5 组件数组
1 2 3 4 5 6 7 8 9 10 11 12 13
| class PropertySample { setting = ElementArray.empty(); onPropertyChanged(property) { if (property == "setting") { for (let i = 0; i < this.setting.length; i++) { const element = this.setting[i]; console.log("element " + i + " name is " + element.name); } } } } export default PropertySample;
|
组件数组属性生成效果如下:
2.6 向量
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| class PropertySample { setting = Vector.default([0, 0], { generics: [ { key: "x", type: "int" }, { key: "y", type: "int" }, ], unit: "米", hideGeneric: false, }); onPropertyChanged(property) { if (property == "setting") { console.log(this.setting.value); } } } export default PropertySample;
|
向量属性生成效果如下:
2.7 数据字段
1 2 3 4 5 6 7 8 9 10 11 12 13
| class PropertySample { setting = Field.default({ minFields: 1, maxFields: 3, }); async init() { const data = await this.setting.readDataAsync(); console.log("data", data); } } export default PropertySample;
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
| class Field { static default(options?: FieldOption);
readData(callback: ReadDataCallback, options?: ReadDataOptions): void;
async readDataAsync(options?: ReadDataAsyncOptions): Promise<any[]>;
static readData(fields: Field[], callback: ReadDataCallback, options?: ReadDataOptions): void;
static async readDataAsync(fields: Field[], options?: ReadDataAsyncOptions): Promise<any[]>; } type FieldOption = { minFields?: number, maxFields?: number, };
|
数据字段属性生成的效果如下:
2.8 文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| class PropertySample { setting = File.default( "https://www.shanhaibi.com/wp-content/themes/twentyseventeen/assets/images/header/home_icon_black.png", { format: "image|video|.pdf|model|.mp3", isMultiple: false, } ); onPropertyChanged(property) { if (property == "setting") { console.log(this.setting.value); } } } export default PropertySample;
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| class File {
static default(value?: string | string[], option?: FileOption); get value(): string | string[]; async readAsText(): Promise<string>; async readAsJson(): Promise<any>; async readAsBlob(): Promise<Blob>; async readAsArrayBuffer(): Promise<ArrayBuffer>; } type FileOption = {
format?: string; isMultiple?: boolean; };
|
文件属性生成的效果如下:
2.9 下拉选项
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| class PropertySample { setting = Select.default(2, { selectChoices: [ { value: 2, label: "大" }, { value: 1, label: "小" }, ], isMultiple: false, }); onPropertyChanged(property) { if (property == "setting") { console.log(this.setting.value); } } } export default PropertySample;
|
下拉选项属性生成的效果如下:
2.10 字体
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| class PropertySample { setting = Font.default( { family: "", size: 12, color: "#fff", bold: false, italic: false, underline: false, "line-through": false, }, { bold: true, italic: true, underline: false, "line-through": false, } ); onPropertyChanged(property) { if (property == "setting") { console.log(this.setting.value); } } } export default PropertySample;
|
字体属性生成结果如下:
2.11 段落
1 2 3 4 5 6 7 8 9 10 11 12 13
| class PropertySample { setting = Paragraph.default("山海鲸是一站式可视化平台", { rows: 2, }); onPropertyChanged(property) { if (property == "setting") { console.log(this.setting.value); } } } export default PropertySample;
|
段落属性生成样式如下:
2.12 颜色
1 2 3 4 5 6 7 8 9 10 11
| class PropertySample { setting = Color.default("#00FF00"); onPropertyChanged(property) { if (property == "setting") { console.log(this.setting.value); } } } export default PropertySample;
|
颜色属性生成的效果如下: