diff --git a/examples/TextInputComponentTest/entry/src/main/ets/pages/14-editMenuOptions/01-textinput.ets b/examples/TextInputComponentTest/entry/src/main/ets/pages/14-editMenuOptions/01-textinput.ets new file mode 100644 index 0000000000000000000000000000000000000000..473748fc19579886ea9fbefa9dd0769d6bd79101 --- /dev/null +++ b/examples/TextInputComponentTest/entry/src/main/ets/pages/14-editMenuOptions/01-textinput.ets @@ -0,0 +1,70 @@ +// xxx.ets +@Entry +@Component +struct EditMenuOptionsTextInputExample { + @State text: string = 'TextInput editMenuOptions'; + @State endIndex: number = 0; + onCreateMenu = (menuItems: Array) => { + let item1: TextMenuItem = { + content: 'create1', + icon: $r('app.media.startIcon'), + id: TextMenuItemId.of('create1'), + }; + let item2: TextMenuItem = { + content: 'create2', + id: TextMenuItemId.of('create2'), + icon: $r('app.media.startIcon'), + }; + menuItems.push(item1); + menuItems.unshift(item2); + return menuItems; + } + onMenuItemClick = (menuItem: TextMenuItem, textRange: TextRange) => { + if (menuItem.id.equals(TextMenuItemId.of("create2"))) { + console.log("拦截 id: create2 start:" + textRange.start + "; end:" + textRange.end); + return true; + } + if (menuItem.id.equals(TextMenuItemId.of("prepare1"))) { + console.log("拦截 id: prepare1 start:" + textRange.start + "; end:" + textRange.end); + return true; + } + if (menuItem.id.equals(TextMenuItemId.COPY)) { + console.log("拦截 COPY start:" + textRange.start + "; end:" + textRange.end); + return true; + } + if (menuItem.id.equals(TextMenuItemId.SELECT_ALL)) { + console.log("不拦截 SELECT_ALL start:" + textRange.start + "; end:" + textRange.end); + return false; + } + return false; + } + onPrepareMenu = (menuItems: Array) => { + let item1: TextMenuItem = { + content: 'prepare1_' + this.endIndex, + icon: $r('app.media.startIcon'), + id: TextMenuItemId.of('prepare1'), + }; + menuItems.unshift(item1); + return menuItems; + } + @State editMenuOptions: EditMenuOptions = { + onCreateMenu: this.onCreateMenu, + onMenuItemClick: this.onMenuItemClick, + onPrepareMenu: this.onPrepareMenu + }; + + build() { + Column() { + TextInput({ text: this.text }) + .width('95%') + .height(50) + .editMenuOptions(this.editMenuOptions) + .margin({ top: 100 }) + .onTextSelectionChange((selectionStart: number, selectionEnd: number) => { + this.endIndex = selectionEnd; + }) + } + .width("90%") + .margin("5%") + } +} \ No newline at end of file diff --git a/examples/TextInputComponentTest/entry/src/main/ets/pages/14-editMenuOptions/02-textarea.ets b/examples/TextInputComponentTest/entry/src/main/ets/pages/14-editMenuOptions/02-textarea.ets new file mode 100644 index 0000000000000000000000000000000000000000..ccf62a7f2f10d4a981df2aa610636a1a07a7cc53 --- /dev/null +++ b/examples/TextInputComponentTest/entry/src/main/ets/pages/14-editMenuOptions/02-textarea.ets @@ -0,0 +1,70 @@ +// xxx.ets +@Entry +@Component +struct EditMenuOptionsTextAreaExample { + @State text: string = 'TextArea editMenuOptions'; + @State endIndex: number = 0; + onCreateMenu = (menuItems: Array) => { + let item1: TextMenuItem = { + content: 'create1', + icon: $r('app.media.startIcon'), + id: TextMenuItemId.of('create1'), + }; + let item2: TextMenuItem = { + content: 'create2', + id: TextMenuItemId.of('create2'), + icon: $r('app.media.startIcon'), + }; + menuItems.push(item1); + menuItems.unshift(item2); + return menuItems; + } + onMenuItemClick = (menuItem: TextMenuItem, textRange: TextRange) => { + if (menuItem.id.equals(TextMenuItemId.of("create2"))) { + console.log("拦截 id: create2 start:" + textRange.start + "; end:" + textRange.end); + return true; + } + if (menuItem.id.equals(TextMenuItemId.of("prepare1"))) { + console.log("拦截 id: prepare1 start:" + textRange.start + "; end:" + textRange.end); + return true; + } + if (menuItem.id.equals(TextMenuItemId.COPY)) { + console.log("拦截 COPY start:" + textRange.start + "; end:" + textRange.end); + return true; + } + if (menuItem.id.equals(TextMenuItemId.SELECT_ALL)) { + console.log("不拦截 SELECT_ALL start:" + textRange.start + "; end:" + textRange.end); + return false; + } + return false; + } + onPrepareMenu = (menuItems: Array) => { + let item1: TextMenuItem = { + content: 'prepare1_' + this.endIndex, + icon: $r('app.media.startIcon'), + id: TextMenuItemId.of('prepare1'), + }; + menuItems.unshift(item1); + return menuItems; + } + @State editMenuOptions: EditMenuOptions = { + onCreateMenu: this.onCreateMenu, + onMenuItemClick: this.onMenuItemClick, + onPrepareMenu: this.onPrepareMenu + }; + + build() { + Column() { + TextArea({ text: this.text }) + .width('95%') + .height(56) + .editMenuOptions(this.editMenuOptions) + .margin({ top: 100 }) + .onTextSelectionChange((selectionStart: number, selectionEnd: number) => { + this.endIndex = selectionEnd; + }) + } + .width("90%") + .margin("5%") + } +} \ No newline at end of file diff --git a/examples/TextInputComponentTest/entry/src/main/ets/pages/14-editMenuOptions/03-text.ets b/examples/TextInputComponentTest/entry/src/main/ets/pages/14-editMenuOptions/03-text.ets new file mode 100644 index 0000000000000000000000000000000000000000..fdc5e9675e1d9b254135e51789d6fcaf2535e2b0 --- /dev/null +++ b/examples/TextInputComponentTest/entry/src/main/ets/pages/14-editMenuOptions/03-text.ets @@ -0,0 +1,70 @@ +// xxx.ets +@Entry +@Component +struct EditMenuOptionsTextExample { + @State text: string = 'Text editMenuOptions' + @State endIndex: number = 0; + onCreateMenu = (menuItems: Array) => { + let item1: TextMenuItem = { + content: 'create1', + icon: $r('app.media.startIcon'), + id: TextMenuItemId.of('create1'), + }; + let item2: TextMenuItem = { + content: 'create2', + id: TextMenuItemId.of('create2'), + icon: $r('app.media.startIcon'), + }; + menuItems.push(item1); + menuItems.unshift(item2); + return menuItems; + } + onMenuItemClick = (menuItem: TextMenuItem, textRange: TextRange) => { + if (menuItem.id.equals(TextMenuItemId.of("create2"))) { + console.log("拦截 id: create2 start:" + textRange.start + "; end:" + textRange.end); + return true; + } + if (menuItem.id.equals(TextMenuItemId.of("prepare1"))) { + console.log("拦截 id: prepare1 start:" + textRange.start + "; end:" + textRange.end); + return true; + } + if (menuItem.id.equals(TextMenuItemId.COPY)) { + console.log("拦截 COPY start:" + textRange.start + "; end:" + textRange.end); + return true; + } + if (menuItem.id.equals(TextMenuItemId.SELECT_ALL)) { + console.log("不拦截 SELECT_ALL start:" + textRange.start + "; end:" + textRange.end); + return false; + } + return false; + } + onPrepareMenu = (menuItems: Array) => { + let item1: TextMenuItem = { + content: 'prepare1_' + this.endIndex, + icon: $r('app.media.startIcon'), + id: TextMenuItemId.of('prepare1'), + }; + menuItems.unshift(item1); + return menuItems; + } + @State editMenuOptions: EditMenuOptions = { + onCreateMenu: this.onCreateMenu, + onMenuItemClick: this.onMenuItemClick, + onPrepareMenu: this.onPrepareMenu + }; + + build() { + Column() { + Text(this.text) + .fontSize(20) + .copyOption(CopyOptions.LocalDevice) + .editMenuOptions(this.editMenuOptions) + .margin({ top: 100 }) + .onTextSelectionChange((selectionStart: number, selectionEnd: number) => { + this.endIndex = selectionEnd; + }) + } + .width("90%") + .margin("5%") + } +} \ No newline at end of file