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,8 +12,13 @@ | ||
| 12 | * | 12 | * |
| 13 | */ | 13 | */ |
| 14 | 14 | ||
| 15 | +import { mapState } from 'vuex' | ||
| 15 | import './contexmenu.scss' | 16 | import './contexmenu.scss' |
| 16 | 17 | ||
| 18 | +function isRegExp (value) { | ||
| 19 | + return value instanceof RegExp | ||
| 20 | +} | ||
| 21 | + | ||
| 17 | // 垂直菜单 | 22 | // 垂直菜单 |
| 18 | const contextmenuOptions = [ | 23 | const contextmenuOptions = [ |
| 19 | { | 24 | { |
| @@ -25,6 +30,30 @@ const contextmenuOptions = [ | @@ -25,6 +30,30 @@ const contextmenuOptions = [ | ||
| 25 | i18nLabel: 'editor.centerPanel.contextMenu.delete', | 30 | i18nLabel: 'editor.centerPanel.contextMenu.delete', |
| 26 | label: '删除', | 31 | label: '删除', |
| 27 | value: 'delete' | 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,6 +82,37 @@ const zindexContextMenu = [ | ||
| 53 | ] | 82 | ] |
| 54 | 83 | ||
| 55 | export default { | 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 | props: { | 116 | props: { |
| 57 | position: { | 117 | position: { |
| 58 | type: Array, | 118 | type: Array, |
| @@ -76,13 +136,14 @@ export default { | @@ -76,13 +136,14 @@ export default { | ||
| 76 | onSelect={this.handleSelectMenu} | 136 | onSelect={this.handleSelectMenu} |
| 77 | class="contextmenu__vertical-menus" | 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 | </a-menu> | 148 | </a-menu> |
| 88 | <a-menu | 149 | <a-menu |
front-end/h5/src/locales/lang/en-US.js
| @@ -60,7 +60,9 @@ export default { | @@ -60,7 +60,9 @@ export default { | ||
| 60 | moveToTop: 'Move To Top', | 60 | moveToTop: 'Move To Top', |
| 61 | moveToBottom: 'Move To Bottom', | 61 | moveToBottom: 'Move To Bottom', |
| 62 | moveUp: 'Move Up', | 62 | moveUp: 'Move Up', |
| 63 | - moveDown: 'Move Down' | 63 | + moveDown: 'Move Down', |
| 64 | + showOnlyButton: 'showOnlyButton', | ||
| 65 | + showExcludePicture: 'showExcludePicture' | ||
| 64 | } | 66 | } |
| 65 | }, | 67 | }, |
| 66 | fixedTool: { | 68 | fixedTool: { |
front-end/h5/src/locales/lang/zh-CN.js
| @@ -69,7 +69,9 @@ export default { | @@ -69,7 +69,9 @@ export default { | ||
| 69 | moveToTop: '置顶', | 69 | moveToTop: '置顶', |
| 70 | moveToBottom: '置底', | 70 | moveToBottom: '置底', |
| 71 | moveUp: '上移', | 71 | moveUp: '上移', |
| 72 | - moveDown: '下移' | 72 | + moveDown: '下移', |
| 73 | + showOnlyButton: '只有按钮才显示该选项', | ||
| 74 | + showExcludePicture: '除了图片都显示该选项' | ||
| 73 | } | 75 | } |
| 74 | }, | 76 | }, |
| 75 | fixedTool: { | 77 | fixedTool: { |