Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as QuerySelector from './QuerySelector.ts'

export const toHaveText = (locator) => {
const getElementText = (locator) => {
const element = QuerySelector.querySelectorOne(locator._parsed)
if (!element) {
return {
Expand All @@ -14,6 +14,14 @@ export const toHaveText = (locator) => {
}
}

export const toHaveText = (locator) => {
return getElementText(locator)
}

export const toContainText = (locator) => {
return getElementText(locator)
}

export const toHaveAttribute = (locator, { key, value }) => {
const element = QuerySelector.querySelectorOne(locator._parsed)
if (!element) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,76 @@
import * as ElementActions from './ElementActions.ts'

const keyCodeMap = {
' ': { code: 'Space', keyCode: 32 },
'.': { code: 'Period', keyCode: 190 },
'>': { code: 'Period', keyCode: 190, shiftKey: true },
Enter: { code: 'Enter', keyCode: 13 },
Space: { code: 'Space', key: ' ', keyCode: 32 },
}

const getKeyboardOptions = (options) => {
const key = options.key || ''
const normalizedKey = key === 'Space' ? ' ' : key
if (keyCodeMap[key]) {
const mapped = keyCodeMap[key]
const keyCode = mapped.keyCode
return {
...options,
...mapped,
key: mapped.key || normalizedKey,
which: keyCode,
}
}
if (normalizedKey.length === 1) {
const upper = normalizedKey.toUpperCase()
const keyCode = upper.codePointAt(0)
const code = /[A-Z]/.test(upper) ? `Key${upper}` : `Digit${upper}`
return {
...options,
code,
key: normalizedKey,
keyCode,
which: keyCode,
}
}
return options
}

const isXtermTextArea = (element: Element | null): element is HTMLTextAreaElement => {
return element instanceof HTMLTextAreaElement && element.classList.contains('xterm-helper-textarea')
}

const getPrintableKey = (options) => {
if (options.key === 'Space') {
return ' '
}
if (options.key?.length === 1) {
return options.key
}
return ''
}

export const press = (options) => {
const element = document.activeElement
if (!element) {
return
}
const text = getPrintableKey(options)
if (isXtermTextArea(element) && text) {
element.value = text
element.dispatchEvent(
new InputEvent('input', {
bubbles: true,
cancelable: true,
data: text,
inputType: 'insertText',
}),
)
element.value = ''
return
}
const keyboardOptions = getKeyboardOptions(options)

ElementActions.keyDown(element, options)
ElementActions.keyUp(element, options)
ElementActions.keyDown(element, keyboardOptions)
ElementActions.keyUp(element, keyboardOptions)
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ export const handleMouseDown = (state) => {
focus(state)
}

export const attachEvents = (state) => {
state.$Viewlet.addEventListener('mousedown', () => {
handleMouseDown(state)
})
}

export const dispose = (state) => {
for (const disposable of state.disposables) {
disposable.dispose()
Expand Down
13 changes: 12 additions & 1 deletion packages/renderer-process/test/ViewletTerminal2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,20 @@ test('write', () => {
expect(terminal.written).toEqual([data])
})

test('dispose', () => {
test('attachEvents focuses terminal on mousedown', () => {
const state = ViewletTerminal2.create()
ViewletTerminal2.setTerminal(state, 3)
ViewletTerminal2.attachEvents(state)
const terminal = terminalInstances.at(-1)!

state.$Viewlet.dispatchEvent(new MouseEvent('mousedown'))

expect(terminal.focused).toBe(true)
})

test('dispose', () => {
const state = ViewletTerminal2.create()
ViewletTerminal2.setTerminal(state, 4)
const terminal = terminalInstances.at(-1)!
ViewletTerminal2.dispose(state)
expect(terminal.disposed).toBe(true)
Expand Down
Loading