Skip to content

Commit 4874c9b

Browse files
author
chunqiuyiyu
committed
Add virtualkey for iPad support
1 parent f0043db commit 4874c9b

File tree

10 files changed

+109
-26
lines changed

10 files changed

+109
-26
lines changed

app/components/MonacoEditor/state.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { findLanguageByextensions, findModeByName } from './utils/findLanguage'
1818
import ConditionWidget from './ConditionWidget'
1919
import initialOptions from './monacoDefaultOptions'
2020
import config from 'config'
21+
import { state as virtualKeyState } from '../VirtualKey'
2122

2223
reaction(() => initialOptions.theme, (theme) => {
2324
monaco.editor.setTheme(theme)
@@ -95,6 +96,7 @@ class EditorInfo {
9596
state.editors.set(this.uri, monacoEditor)
9697

9798
monacoEditor.onDidFocusEditorText(() => {
99+
virtualKeyState.show = true
98100
state.activeMonacoEditor = monacoEditor
99101
if (state.activeEditorListeners && state.activeEditorListeners.length > 0) {
100102
for (const activeEditorListener of state.activeEditorListeners) {
Lines changed: 58 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import React, { Component } from 'react'
2-
import dispatchCommand from 'commands/dispatchCommand'
2+
import editorState from '../MonacoEditor/state'
3+
import { observable } from 'mobx'
4+
import { observer } from 'mobx-react'
35

46
const keys = `{}()<>[]"';\/=+-:,.?&|!_\`$%*#@^~`
7+
const state = observable({
8+
show: false
9+
})
510

611
class VirtualKey extends Component {
712
constructor(props) {
@@ -10,28 +15,67 @@ class VirtualKey extends Component {
1015
}
1116

1217
emitterEvent(item) {
13-
dispatchCommand('highlight_line')
14-
const keyboardEvent = new KeyboardEvent('keypress', {bubbles:true})
15-
Object.defineProperty(keyboardEvent, 'charCode',
16-
{ get:() => this.charCodeVal }
17-
)
18-
19-
keyboardEvent.charCodeVal = item.charCodeAt()
20-
// console.log(ke)
21-
document.querySelector('.monaco-editor').dispatchEvent(keyboardEvent)
18+
const monacoEditor = editorState.activeMonacoEditor
19+
if (!monacoEditor) return
20+
21+
if (item === 'Tab') {
22+
item = '\t'
23+
}
24+
25+
if (item === 'undo') {
26+
monacoEditor.trigger('', 'undo')
27+
return
28+
}
29+
30+
if (item === 'redo') {
31+
monacoEditor.trigger('', 'redo')
32+
return
33+
}
34+
35+
monacoEditor.trigger('', 'type', { text: item })
36+
// Insert pair symbols
37+
let nextSymbol = ''
38+
switch (item) {
39+
case '{': nextSymbol = '}'; break
40+
case '(': nextSymbol = ')'; break
41+
case '[': nextSymbol = ']'; break
42+
case '"': nextSymbol = '"'; break
43+
case `'`: nextSymbol = `'`; break
44+
case '`': nextSymbol = '`'; break
45+
default: break
46+
}
47+
48+
monacoEditor.trigger('', 'type', { text: nextSymbol })
49+
if (nextSymbol) {
50+
const position = monacoEditor.cursor.getPosition()
51+
monacoEditor.setPosition({
52+
lineNumber : position.lineNumber, column: position.column - 1
53+
})
54+
}
2255
}
2356

2457
render() {
25-
return <div style={{ zIndex: '100' }}>
58+
return <div className={'virtual-container'}
59+
style={{ display: state.show ? 'flex' : 'none' }}>
2660
<ul className="virtual-keymap">
2761
{this.keymap.map(
2862
item => <li onClick={() => this.emitterEvent(item)}>{item}</li>
2963
)}
30-
<li><i className="fa fa-undo" /></li>
31-
<li><i className="fa fa-repeat" /></li>
3264
</ul>
65+
<div className="icons">
66+
<div onClick={() => this.emitterEvent('undo')}>
67+
<i className="fa fa-undo" />
68+
</div>
69+
<div onClick={() => this.emitterEvent('redo')}>
70+
<i className="fa fa-repeat" />
71+
</div>
72+
<div onClick={() => state.show = false}>
73+
<i className="fa fa-times" />
74+
</div>
75+
</div>
3376
</div>
3477
}
3578
}
3679

37-
export default VirtualKey
80+
export default observer(VirtualKey)
81+
export { state }

app/containers/IDE.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class IDE extends Component {
3939
<GlobalPrompt />
4040
<PanelsContainer />
4141
<Utilities />
42-
<VirtualKey />
42+
{config.isPad && <VirtualKey />}
4343
</div>
4444
)
4545
}

app/styles/core-ui/PanelAndPane.styl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ safariHeightFix() {
2929

3030
.panel-container,
3131
.pane-container {
32-
z-index: 4
3332
// not every .pane(l) need safariHeightFix, only children of .resizable do
3433
&.resizable {
3534
min-width: 30px;

app/styles/core-ui/VirtualKey.styl

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,35 @@
1-
.virtual-keymap {
1+
.virtual-container {
22
z-index: 100;
33
display: flex;
4-
height: 40px;
5-
6-
li {
7-
width: 40px;
4+
position: sticky;
5+
top: 0;
6+
width: 100%;
7+
8+
.virtual-keymap {
9+
overflow-x: scroll;
10+
width: calc(100% - 80px);
811
display: flex;
9-
align-items: center
12+
height: 40px;
13+
14+
li {
15+
width: 40px;
16+
display: flex;
17+
align-items: center;
18+
flex-shrink: 0;
19+
align-items: center;
20+
justify-content: center;
21+
}
22+
}
23+
24+
.icons {
25+
display: flex;
26+
27+
i {
28+
width: 40px;
29+
height: 40px;
30+
display: flex;
31+
align-items: center;
32+
justify-content: center;
33+
}
1034
}
1135
}

app/styles/dark/index.styl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
@import './styles/settings';
2121
@import './styles/plugin';
2222
@import './styles/search';
23+
@import './styles/virtualkey';
2324

2425
// 插件用样式
2526
@import './styles/weapp';
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.virtual-container {
2+
background: $tab-background-color;
3+
color: $text-color;
4+
5+
.icons {
6+
background: $$tab-background-color-active;
7+
}
8+
}

app/styles/light/index.styl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@
1919
@import './styles/search';
2020
@import './styles/plugin';
2121
@import './styles/menubar';
22+
@import './styles/virtualkey';
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.virtual-container {
2+
background: $tab-background-color;
3+
color: $text-color;
4+
5+
.icons {
6+
background: $$tab-background-color-active;
7+
}
8+
}

app/styles/main.styl

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,6 @@
4141
z-index: z(utilities-container);
4242
}
4343

44-
.iPadShowKeyboard {
45-
display: none;
46-
}
47-
4844
html,body {
4945
height: 100%
5046
}

0 commit comments

Comments
 (0)