Commit b6c26b028825d092f32ca50346062e4e7cce6465
Committed by
小小鲁班
1 parent
7bd33cb7
feat: contextmenu support whitelist !#zh: 右键菜单支持元素黑白名单
Showing
3 changed files
with
74 additions
and
9 deletions
front-end/h5/src/components/core/support/contexmenu.js
| ... | ... | @@ -12,8 +12,13 @@ |
| 12 | 12 | * |
| 13 | 13 | */ |
| 14 | 14 | |
| 15 | +import { mapState } from 'vuex' | |
| 15 | 16 | import './contexmenu.scss' |
| 16 | 17 | |
| 18 | +function isRegExp (value) { | |
| 19 | + return value instanceof RegExp | |
| 20 | +} | |
| 21 | + | |
| 17 | 22 | // 垂直菜单 |
| 18 | 23 | const contextmenuOptions = [ |
| 19 | 24 | { |
| ... | ... | @@ -25,6 +30,30 @@ const contextmenuOptions = [ |
| 25 | 30 | i18nLabel: 'editor.centerPanel.contextMenu.delete', |
| 26 | 31 | label: '删除', |
| 27 | 32 | value: 'delete' |
| 33 | + }, | |
| 34 | + /** | |
| 35 | + * contextMenu 白名单,只有匹配白名单列表里的元素,才会显示该选项 | |
| 36 | + * 支持正则、数组 | |
| 37 | + * 数组:[ElementName] | |
| 38 | + * 正则:RegExp | |
| 39 | + */ | |
| 40 | + { | |
| 41 | + i18nLabel: 'editor.centerPanel.contextMenu.showOnlyButton', | |
| 42 | + label: 'showOnlyButton', | |
| 43 | + value: 'showOnlyButton', | |
| 44 | + elementWhiteList: ['lbp-button'] | |
| 45 | + }, | |
| 46 | + /** | |
| 47 | + * contextMenu 黑名单,在黑名单列表里的元素,不会显示该选项 | |
| 48 | + * 支持正则、数组 | |
| 49 | + * 数组:[ElementName] | |
| 50 | + * 正则:RegExp | |
| 51 | + */ | |
| 52 | + { | |
| 53 | + i18nLabel: 'editor.centerPanel.contextMenu.showExcludePicture', | |
| 54 | + label: 'showExcludePicture', | |
| 55 | + value: 'showExcludePicture', | |
| 56 | + elementBlackList: /^lbp-picture/ | |
| 28 | 57 | } |
| 29 | 58 | ] |
| 30 | 59 | |
| ... | ... | @@ -53,6 +82,37 @@ const zindexContextMenu = [ |
| 53 | 82 | ] |
| 54 | 83 | |
| 55 | 84 | export default { |
| 85 | + computed: { | |
| 86 | + ...mapState('editor', ['editingElement', 'work']), | |
| 87 | + /** | |
| 88 | + * 做一下扩展,提供:黑白名单,来针对某些特定组件,展示特定右键菜单 | |
| 89 | + * TODO:后期提供如下方式,来扩展右键菜单 | |
| 90 | + window.GlobalLuban.contextmenu.registerMenu({ | |
| 91 | + label: '复制', | |
| 92 | + value: 'copy', | |
| 93 | + elementWhiteList: Array || RegExp | |
| 94 | + elementBlackList: Array || RegExp | |
| 95 | + }) | |
| 96 | + */ | |
| 97 | + filteredOptions () { | |
| 98 | + const elementName = this.editingElement.name | |
| 99 | + const filteredOptions = contextmenuOptions.filter(option => { | |
| 100 | + const wl = option.elementWhiteList | |
| 101 | + const bl = option.elementBlackList | |
| 102 | + if (wl) { | |
| 103 | + if (Array.isArray(wl)) return wl.includes(elementName) | |
| 104 | + if (isRegExp(wl)) return wl.test(elementName) | |
| 105 | + } | |
| 106 | + if (bl) { | |
| 107 | + if (Array.isArray(bl)) return !bl.includes(elementName) | |
| 108 | + debugger | |
| 109 | + if (isRegExp(bl)) return !bl.test(elementName) | |
| 110 | + } | |
| 111 | + return true | |
| 112 | + }) | |
| 113 | + return filteredOptions | |
| 114 | + } | |
| 115 | + }, | |
| 56 | 116 | props: { |
| 57 | 117 | position: { |
| 58 | 118 | type: Array, |
| ... | ... | @@ -76,13 +136,14 @@ export default { |
| 76 | 136 | onSelect={this.handleSelectMenu} |
| 77 | 137 | class="contextmenu__vertical-menus" |
| 78 | 138 | > |
| 79 | - { contextmenuOptions.map(option => ( | |
| 80 | - <a-menu-item | |
| 81 | - key={option.value} | |
| 82 | - data-command={option.value} | |
| 83 | - class="contextmenu__vertical-menus__item" | |
| 84 | - >{this.$t(option.i18nLabel)}</a-menu-item> | |
| 85 | - )) | |
| 139 | + { | |
| 140 | + this.filteredOptions.map(option => ( | |
| 141 | + <a-menu-item | |
| 142 | + key={option.value} | |
| 143 | + data-command={option.value} | |
| 144 | + class="contextmenu__vertical-menus__item" | |
| 145 | + >{this.$t(option.i18nLabel)}</a-menu-item> | |
| 146 | + )) | |
| 86 | 147 | } |
| 87 | 148 | </a-menu> |
| 88 | 149 | <a-menu | ... | ... |
front-end/h5/src/locales/lang/en-US.js
| ... | ... | @@ -60,7 +60,9 @@ export default { |
| 60 | 60 | moveToTop: 'Move To Top', |
| 61 | 61 | moveToBottom: 'Move To Bottom', |
| 62 | 62 | moveUp: 'Move Up', |
| 63 | - moveDown: 'Move Down' | |
| 63 | + moveDown: 'Move Down', | |
| 64 | + showOnlyButton: 'showOnlyButton', | |
| 65 | + showExcludePicture: 'showExcludePicture' | |
| 64 | 66 | } |
| 65 | 67 | }, |
| 66 | 68 | fixedTool: { | ... | ... |
front-end/h5/src/locales/lang/zh-CN.js