Skip to content

Commit d039dcb

Browse files
authored
feat: 添加快捷键+更完整的辅助线提示 (#148) add shortcuts for editor and more guidelines
* fix scrollbar show when h-line&v-line visible * remove unused text code * add hotkey to menu tools * 除了left和top,其他四条线(midx,right,midy,bottom)也提示辅助线 * change width and height when point move situation * rename argument and optimize some experience * Fix typo thanks @shihongzhi
1 parent a9a7123 commit d039dcb

File tree

4 files changed

+96
-66
lines changed

4 files changed

+96
-66
lines changed

front-end/h5/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"core-js": "^2.6.5",
2424
"element-ui": "^2.13.0",
2525
"font-awesome": "4.7.0",
26+
"hotkeys-js": "^3.7.6",
2627
"html2canvas": "^1.0.0-rc.3",
2728
"proxy-agent": "^3.1.0",
2829
"qrcode": "^1.4.1",

front-end/h5/src/components/core/editor/canvas/edit.js

+69-43
Original file line numberDiff line numberDiff line change
@@ -23,73 +23,97 @@ export default {
2323
// TODO #!zh: 优化代码
2424
// generate vertical line
2525
drawVLine (newLeft) {
26-
// this.editingElement.commonStyle.left = newLeft
27-
this.setElementPosition({ left: newLeft })
2826
this.vLines = [{ left: newLeft }]
2927
},
28+
clearVLine () {
29+
this.vLines = []
30+
},
3031
// generate horizontal line
3132
drawHLine (newTop) {
32-
// this.editingElement.commonStyle.top = newTop
33-
this.setElementPosition({ top: newTop })
3433
this.hLines = [{ top: newTop }]
3534
},
36-
calcX (newLeft) {
35+
clearHLine () {
36+
this.hLines = []
37+
},
38+
calcVHLine (isPointMove) {
3739
const uuid = this.editingElement.uuid
38-
let xCoords = []
39-
this.elements.filter(e => e.uuid !== uuid).forEach(e => {
40+
const referElements = this.elements.filter(e => e.uuid !== uuid)
41+
let referElementsXCoords = []
42+
let referElementsYCoords = []
43+
referElements.forEach(e => {
4044
const width = e.commonStyle.width
4145
const left = e.commonStyle.left
42-
xCoords = [
43-
...xCoords,
46+
const height = e.commonStyle.height
47+
const top = e.commonStyle.top
48+
49+
referElementsXCoords = [
50+
...referElementsXCoords,
4451
left,
4552
left + (width / 2),
4653
left + width
4754
]
48-
})
49-
50-
xCoords.some(x => {
51-
if (Math.abs(newLeft - x) <= 5) {
52-
this.drawVLine(x)
53-
return true
54-
} else {
55-
this.vLines = []
56-
}
57-
})
58-
},
59-
calcY (newTop) {
60-
const uuid = this.editingElement.uuid
61-
let yCoords = []
62-
this.elements.filter(e => e.uuid !== uuid).forEach(e => {
63-
const height = e.commonStyle.height
64-
const top = e.commonStyle.top
65-
yCoords = [
66-
...yCoords,
55+
referElementsYCoords = [
56+
...referElementsYCoords,
6757
top,
6858
top + (height / 2),
6959
top + height
7060
]
7161
})
7262

73-
yCoords.some(y => {
74-
if (Math.abs(newTop - y) <= 5) {
75-
this.drawHLine(y)
76-
return true
77-
} else {
78-
this.hLines = []
79-
}
63+
// e代表 editingElement
64+
const eleft = this.editingElement.commonStyle.left
65+
const etop = this.editingElement.commonStyle.top
66+
const ewidth = this.editingElement.commonStyle.width
67+
const eheight = this.editingElement.commonStyle.height
68+
const exCoords = [eleft + ewidth, eleft + (ewidth / 2), eleft]
69+
const eyCoords = [etop + eheight, etop + (eheight / 2), etop]
70+
let hasVLine = false
71+
let hasHLine = false
72+
exCoords.forEach(eX => {
73+
referElementsXCoords.forEach(referX => {
74+
let offset = referX - eX
75+
if (Math.abs(offset) <= 5) {
76+
if (isPointMove) {
77+
this.setElementPosition({ width: ewidth + offset })
78+
} else {
79+
this.setElementPosition({ left: eleft + offset })
80+
}
81+
this.drawVLine(referX)
82+
hasVLine = true
83+
}
84+
})
85+
})
86+
eyCoords.forEach(eY => {
87+
referElementsYCoords.forEach(referY => {
88+
let offset = referY - eY
89+
if (Math.abs(offset) <= 5) {
90+
if (isPointMove) {
91+
this.setElementPosition({ height: eheight + offset })
92+
} else {
93+
this.setElementPosition({ top: etop + offset })
94+
}
95+
this.drawHLine(referY)
96+
hasHLine = true
97+
}
98+
})
8099
})
100+
if (!hasVLine) {
101+
this.clearVLine()
102+
}
103+
if (!hasHLine) {
104+
this.clearHLine()
105+
}
81106
},
82107
/**
83108
* #!zh: 在元素移动过程中,计算和生成辅助线
84109
*/
85110
handleElementMove (pos) {
86111
this.setElementPosition(pos)
87-
this.calcX(pos.left)
88-
this.calcY(pos.top)
112+
this.calcVHLine(false)
89113
},
90-
handlePointMove ({ top, left }) {
91-
this.calcX(left)
92-
this.calcY(top)
114+
handlePointMove (pos) {
115+
this.setElementPosition(pos)
116+
this.calcVHLine(true)
93117
},
94118
bindContextMenu (e) {
95119
// 优化右击菜单的显示,去除冗余的无效逻辑
@@ -185,14 +209,16 @@ export default {
185209
this.setEditingElement(element)
186210
}}
187211
// TODO 矩形四周的点叫什么?暂时叫 Point 吧
188-
handlePointMoveProp={pos => {
189-
this.setElementPosition(pos)
190-
}}
212+
handlePointMoveProp={this.handlePointMove}
191213
handleElementMoveProp={this.handleElementMove}
192214
handleElementMouseUpProp={() => {
215+
this.clearHLine()
216+
this.clearVLine()
193217
this.recordElementRect()
194218
}}
195219
handlePointMouseUpProp={() => {
220+
this.clearHLine()
221+
this.clearVLine()
196222
this.recordElementRect()
197223
}}
198224
nativeOnContextmenu={e => {

front-end/h5/src/components/core/editor/index.js

+21-23
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { mapState, mapActions } from 'vuex'
2+
import hotkeys from 'hotkeys-js'
23
import undoRedoHistory from '../../../store/plugins/undo-redo/History'
34

45
import '../styles/index.scss'
@@ -44,58 +45,47 @@ import Feedback from '@/components/common/feedback/index'
4445
const fixedTools = [
4546
{
4647
i18nTooltip: 'editor.fixedTool.undo',
47-
'tooltip': '撤消', // TODO 支持快捷键
48-
'text': '撤消',
4948
'icon': 'mail-reply',
50-
'action': () => undoRedoHistory.undo()
49+
'action': () => undoRedoHistory.undo(),
50+
'hotkey': 'ctrl&z,⌘&z'
5151
},
5252
{
5353
i18nTooltip: 'editor.fixedTool.redo',
54-
'tooltip': '恢复',
55-
'text': '恢复',
5654
'icon': 'mail-forward',
57-
'action': () => undoRedoHistory.redo()
55+
'action': () => undoRedoHistory.redo(),
56+
'hotkey': 'ctrl&y,⌘&u'
5857
},
5958
{
6059
i18nTooltip: 'editor.fixedTool.preview',
61-
'tooltip': '刷新预览',
62-
'text': '刷新预览',
6360
'icon': 'eye',
6461
'action': function () { this.previewVisible = true }
6562
},
6663
{
6764
i18nTooltip: 'editor.fixedTool.copyCurrentPage',
68-
'tooltip': '复制当前页',
69-
'text': '复制当前页',
7065
'icon': 'copy',
71-
'action': function () { this.pageManager({ type: 'copy' }) }
66+
'action': function () { this.pageManager({ type: 'copy' }) },
67+
'hotkey': 'ctrl&c,⌘&c'
7268
},
7369
{
7470
i18nTooltip: 'editor.fixedTool.importPSD',
75-
'tooltip': '导入PSD',
76-
'text': 'Ps',
7771
'icon': '',
7872
'action': '',
7973
'disabled': true
8074
},
8175
{
8276
i18nTooltip: 'editor.fixedTool.zoomOut',
83-
'tooltip': '放大画布',
84-
'text': '放大画布',
8577
'icon': 'plus',
86-
'action': function () { this.scaleRate += 0.25 }
78+
'action': function () { this.scaleRate += 0.25 },
79+
'hotkey': 'ctrl&=,⌘&='
8780
},
8881
{
8982
i18nTooltip: 'editor.fixedTool.zoomIn',
90-
'tooltip': '缩小画布',
91-
'text': '缩小画布',
9283
'icon': 'minus',
93-
'action': function () { this.scaleRate -= 0.25 }
84+
'action': function () { this.scaleRate -= 0.25 },
85+
'hotkey': 'ctrl&-,⌘&-'
9486
},
9587
{
9688
i18nTooltip: 'editor.fixedTool.issues',
97-
'tooltip': 'issues',
98-
'text': '常见问题',
9989
'icon': 'question',
10090
'action': function () { window.open('https://github.com/ly525/luban-h5/issues/110') }
10191
}
@@ -237,6 +227,15 @@ export default {
237227
// }
238228
}
239229
},
230+
mounted () {
231+
fixedTools.map(tool => {
232+
tool.hotkey && hotkeys(tool.hotkey, { splitKey: '&' }, (event, handler) => {
233+
event.preventDefault()
234+
event.stopPropagation()
235+
tool.action && tool.action.call(this)
236+
})
237+
})
238+
},
240239
render (h) {
241240
return (
242241
<a-layout id="luban-editor-layout" style={{ height: '100vh' }}>
@@ -340,10 +339,9 @@ export default {
340339
<a-button-group style={{ display: 'flex', flexDirection: 'column' }}>
341340
{
342341
fixedTools.map(tool => (
343-
// <a-tooltip effect="dark" placement="left" title={tool.tooltip}>
344342
<a-tooltip effect="dark" placement="left" title={this.$t(tool.i18nTooltip)}>
345343
<a-button block class="transparent-bg" type="link" size="small" style={{ height: '40px', color: '#000' }} onClick={() => tool.action && tool.action.call(this) } disabled={!!tool.disabled}>
346-
{ tool.icon ? <i class={['shortcut-icon', 'fa', `fa-${tool.icon}`]} aria-hidden='true'/> : tool.text }
344+
{ tool.icon ? <i class={['shortcut-icon', 'fa', `fa-${tool.icon}`]} aria-hidden='true'/> : this.$t(tool.i18nTooltip) }
347345
</a-button>
348346
{ tool.icon === 'minus' && <div style={{ fontSize: '12px', textAlign: 'center' }}>{this.scaleRate * 100}%</div> }
349347
</a-tooltip>

front-end/h5/yarn.lock

+5
Original file line numberDiff line numberDiff line change
@@ -5071,6 +5071,11 @@ hosted-git-info@^2.1.4:
50715071
resolved "https://registry.npm.taobao.org/hosted-git-info/download/hosted-git-info-2.8.5.tgz#759cfcf2c4d156ade59b0b2dfabddc42a6b9c70c"
50725072
integrity sha1-dZz88sTRVq3lmwst+r3cQqa5xww=
50735073

5074+
hotkeys-js@^3.7.6:
5075+
version "3.7.6"
5076+
resolved "https://registry.npm.taobao.org/hotkeys-js/download/hotkeys-js-3.7.6.tgz#b90ae3453a7be2f2b2bed6ee55cb96443944c77b"
5077+
integrity sha1-uQrjRTp74vKyvtbuVcuWRDlEx3s=
5078+
50745079
hpack.js@^2.1.6:
50755080
version "2.1.6"
50765081
resolved "https://registry.npm.taobao.org/hpack.js/download/hpack.js-2.1.6.tgz#87774c0949e513f42e84575b3c45681fade2a0b2"

0 commit comments

Comments
 (0)