-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
148 lines (124 loc) · 4.35 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
function onMoveCursorWithMouse (delta) {
return (dispatch, getState) => {
dispatch({
type: 'MOUSE_MOVE_CURSOR',
delta: delta,
effect() {
// dispatch({type: "SESSION_USER_DATA", data: "[D"})
const { sessions } = getState()
const uid = sessions.activeUid
const keyCode = delta < 0 ? '\u001bOD' : '\u001bOC'
console.log(keyCode)
window.rpc.emit('data', {
uid: uid,
data: keyCode.repeat(Math.abs(delta))
})
}
})
}
}
exports.mapTermsDispatch = (dispatch, map) => {
return Object.assign(map, {
onMoveCursorWithMouse: (delta) => {
dispatch(onMoveCursorWithMouse(delta))
}
})
}
exports.getTermGroupProps = (uid, parentProps, props) => {
return Object.assign(props, {
onMoveCursorWithMouse: parentProps.onMoveCursorWithMouse
})
}
exports.getTermProps = (uid, parentProps, props) => {
return Object.assign(props, {
onMoveCursorWithMouse: parentProps.onMoveCursorWithMouse
})
}
function findRow (el) {
if (el.tagName === 'X-ROW') {
return el
}
while ((el = el.parentElement) && el.tagName !== 'X-ROW') {}
return el
}
function textContentOnly (node) {
return Array.from(node.childNodes).filter((node) => {
return node.nodeType === 3
}).map((node) => {
return node.nodeValue
}).join('')
}
exports.decorateTerm = function (Term, { React }) {
return class extends React.Component {
constructor (props, context) {
super(props, context)
this.onCursorClick = this.onCursorClick.bind(this)
this.onTerminal = this.onTerminal.bind(this)
}
onTerminal (term) {
if (this.props.onTerminal) {
this.props.onTerminal(term)
}
this.term = term
const { screen_, onTerminalReady } = term
let self = this
term.onTerminalReady = function () {
onTerminalReady.apply(this, arguments)
self.screenNode = term.scrollPort_.getScreenNode()
self.iframe = term.scrollPort_.iframe_
self.screenNode.addEventListener('click', self.onCursorClick)
}
}
onCursorClick (event) {
if (!event.altKey) {
return
}
let cursorRow = this.term.screen_.cursorRowNode_
let rowClicked = findRow(event.target)
if (rowClicked != null) {
let wholeTextContent = rowClicked.textContent
let textContent = textContentOnly(rowClicked)
let dummyCopy = document.createElement('span')
dummyCopy.style.position = 'absolute'
dummyCopy.style.left = 0
for (let i = 0; i < wholeTextContent.length; i++) {
let charSpan = document.createElement('span')
charSpan.setAttribute('data-index', i)
charSpan.innerHTML = wholeTextContent[i]
dummyCopy.appendChild(charSpan)
}
rowClicked.appendChild(dummyCopy)
let el = this.iframe.contentWindow.document.elementFromPoint(event.pageX, event.pageY)
let delta = 0
if (cursorRow == rowClicked) {
delta = parseInt(el.getAttribute('data-index')) - this.term.screen_.cursorOffset_ - (wholeTextContent.length - textContent.length)
} else {
let rowNodes = Array.from(this.term.scrollPort_.rowNodes_.childNodes)
let rowClickedIndex = rowNodes.indexOf(rowClicked)
let cursorRowIndex = rowNodes.indexOf(cursorRow)
if (rowClickedIndex < cursorRowIndex) {
for (let i = rowClickedIndex + 1; i < cursorRowIndex; i++) {
delta -= rowNodes[i].textContent.length
}
delta -= this.term.screen_.cursorOffset_
delta -= (wholeTextContent.length - parseInt(el.getAttribute('data-index')))
} else {
for (let i = cursorRowIndex + 1; i <= rowClickedIndex -1; i++) {
delta += rowNodes[i].textContent.length
}
delta += textContentOnly(cursorRow).length - this.term.screen_.cursorOffset_
delta += parseInt(el.getAttribute('data-index'))
}
}
rowClicked.removeChild(dummyCopy)
this.props.onMoveCursorWithMouse(delta)
}
}
render () {
const props = Object.assign({}, this.props, {
onTerminal: this.onTerminal
})
return React.createElement(Term, props)
}
}
}