1# Widget Editing Development 2<!--Kit: Form Kit--> 3<!--Subsystem: Ability--> 4<!--Owner: @cx983299475--> 5<!--Designer: @xueyulong--> 6<!--Tester: @chenmingze--> 7<!--Adviser: @Brilliantry_Rui--> 8The home screen provides a unified widget editing page. The widget provider uses [FormEditExtensionAbility](../reference/apis-form-kit/js-apis-app-form-formEditExtensionAbility.md) provided by the widget framework to develop the widget editing function. 9 10## How to Develop 111. In the entry module of the project, create a code file named EntryFormEditAbility. In the **EntryFormEditAbility** file, implement the [startSecondPage](../reference/apis-form-kit/js-apis-inner-application-formEditExtensionContext.md#startsecondpage) method. In the [onSessionCreate](../reference/apis-ability-kit/js-apis-app-ability-uiExtensionAbility.md#onsessioncreate) callback method, load the level-1 widget editing page and transfer the implementation of **startSecondPage** to the level-1 widget editing page. 12 ```ts 13 // src/main/ets/entryformeditability/EntryFormEditAbility.ets 14 15 import { FormEditExtensionAbility } from '@kit.FormKit'; 16 import { Want,UIExtensionContentSession } from '@kit.AbilityKit'; 17 import { ExtensionEvent } from '../pages/model/ExtensionEvent'; 18 19 const TAG: string = 'FormEditDemo[EntryFormEditAbility] -->'; 20 export default class EntryFormEditAbility extends FormEditExtensionAbility { 21 onCreate() { 22 console.info(`${TAG} onCreate`); 23 } 24 onForeground(): void { 25 console.info(`${TAG} EntryFormEditAbility onForeground.....`); 26 } 27 onBackground(): void { 28 console.info(`${TAG} EntryFormEditAbility onBackground......`); 29 } 30 onDestroy(): void { 31 console.info(`${TAG} EntryFormEditAbility onDestroy......`); 32 } 33 onSessionCreate(want: Want, session: UIExtensionContentSession) { 34 console.info(`${TAG} onSessionCreate start..... want: ${JSON.stringify(want)}`); 35 let storage: LocalStorage = new LocalStorage(); 36 let extensionEvent: ExtensionEvent = new ExtensionEvent(); 37 extensionEvent.setStartSecondPage(() => this.startSecondPage()); 38 storage.setOrCreate('extensionEvent', extensionEvent); 39 try { 40 session.loadContent('pages/Extension', storage); 41 } catch (e) { 42 console.error(`${TAG} EntryFormEditAbility loadContent err, want: ${JSON.stringify(e)}`); 43 } 44 } 45 onSessionDestroy(session: UIExtensionContentSession) { 46 console.info(`${TAG} onSessionDestroy`); 47 } 48 private startSecondPage(): void { 49 const bundleName: string = this.context.extensionAbilityInfo.bundleName; 50 const secPageAbilityName: string = 'FormEditSecPageAbility'; 51 console.info(`${TAG} startSecondPage. bundleName: ${bundleName}, secPageAbilityName: ${secPageAbilityName}.`); 52 try { 53 this.context.startSecondPage({ 54 bundleName: bundleName, 55 parameters: { 56 "secPageAbilityName": secPageAbilityName 57 } 58 }); 59 } catch (err) { 60 console.error(`${TAG} startSecondPage failed: ${err}`); 61 } 62 } 63 }; 64 ``` 65 662. Add an extension file to display the level-1 editing page of the widget. 67 ```ts 68 // src/main/ets/pages/Extension.ets 69 70 import { UIExtensionContentSession } from '@kit.AbilityKit'; 71 import { ExtensionEvent } from './model/ExtensionEvent'; 72 73 let storage = new LocalStorage(); 74 const TAG: string = 'FormEditDemo[Extension] -->'; 75 @Entry(storage) 76 @Component 77 struct Extension { 78 @State message: string = 'UIExtension Provider'; 79 private session: UIExtensionContentSession | undefined = storage.get<UIExtensionContentSession>('session'); 80 private extensionEvent: ExtensionEvent | undefined = storage.get<ExtensionEvent>('extensionEvent'); 81 onPageShow() { 82 console.info(`${TAG} onPageShow. extensionEvent: ${JSON.stringify(this.extensionEvent)}, session: ${JSON.stringify(this.session)}.`); 83 } 84 build() { 85 Row() { 86 Column() { 87 Text(this.message) 88 .fontSize(20) 89 .fontWeight(FontWeight.Bold) 90 .textAlign(TextAlign.Center) 91 Button("Add") 92 .width('80%') 93 .type(ButtonType.Capsule) 94 .margin({ 95 top: 20 96 }) 97 .onClick(() => { 98 console.info(`${TAG} Button onClick`); 99 this.extensionEvent?.startFormEditSecondPage(); 100 }) 101 } 102 } 103 .justifyContent(FlexAlign.Center) 104 .width('100%') 105 } 106 } 107 ``` 108 1093. Add the **ExtensionEvent** file and use the **startFormEditSecondPage** method to invoke the [startSecondPage](../reference/apis-form-kit/js-apis-inner-application-formEditExtensionContext.md#startsecondpage) method. 110 ```ts 111 // src/main/ets/widget/pages/model/ExtensionEvent.ets 112 113 const TAG: string = 'FormEditDemo[ExtensionEvent] -->'; 114 export class ExtensionEvent { 115 private startSecondPage: () => void = () => { 116 console.info(`${TAG} startSecondPage is empty!`); 117 }; 118 public setStartSecondPage(startSecondPage: () => void) { 119 console.info(`${TAG} setStartSecondPage`); 120 this.startSecondPage = startSecondPage; 121 } 122 public startFormEditSecondPage(): void { 123 console.info(`${TAG} startFormEditSecondPage`); 124 this.startSecondPage(); 125 } 126 } 127 128 ``` 129 1304. Add the widget editing configuration to the [module.json5](../quick-start/module-configuration-file.md) configuration file of the application. 131 ```json 132 "extensionAbilities": [ 133 { 134 "name": "EntryFormEditAbility", 135 "srcEntry": "./ets/entryformeditability/EntryFormEditAbility.ets", 136 "type": "formEdit" 137 } 138 ] 139 ``` 140 1415. Add the formConfigAbility configuration to the [form_config.json](./arkts-ui-widget-configuration.md#fields-in-configuration-file) configuration file of the widget. 142 ```json 143 { 144 "forms": [ 145 { 146 "name": "widget", 147 "displayName": "$string:widget_display_name", 148 "description": "$string:widget_desc", 149 "src": "./ets/widget/pages/WidgetCard.ets", 150 "uiSyntax": "arkts", 151 "formConfigAbility": "ability://EntryFormEditAbility", 152 "window": { 153 "designWidth": 720, 154 "autoDesignWidth": true 155 }, 156 "colorMode": "auto", 157 "isDynamic": true, 158 "isDefault": true, 159 "updateEnabled": false, 160 "scheduledUpdateTime": "10:30", 161 "updateDuration": 1, 162 "defaultDimension": "1*2", 163 "supportDimensions": [ 164 "1*2", 165 "2*2", 166 "2*4", 167 "4*4" 168 ] 169 } 170 ] 171 } 172 ``` 1736. Register the Extension page file in the **resource/base/profile/main_pages.json** file in the development view. 174 ```json 175 { 176 "src": [ 177 "pages/Index", 178 "pages/Extension" 179 ] 180 } 181 ``` 182