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
26 changes: 13 additions & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 24 additions & 8 deletions src/lib/code-editor/CodeEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export class CodeEditor {
private _editableCompartment: Compartment | null = null
private _readOnlyCompartment: Compartment | null = null
private _accessibilityCompartment: Compartment | null = null
private _accessibility: { label?: string, description?: string } = {}
private _onDirtyChange?: (dirty: boolean) => void
private _isDirty = false

Expand All @@ -39,6 +40,7 @@ export class CodeEditor {
this._editableCompartment = new Compartment()
this._readOnlyCompartment = new Compartment()
this._accessibilityCompartment = new Compartment()
this._accessibility = accessibility ?? {}
this._onDirtyChange = onDirtyChange
this._isDirty = false
const languageExtension = await this._getLanguageExtension(contentType)
Expand All @@ -55,10 +57,9 @@ export class CodeEditor {
this._languageCompartment.of(languageExtension),
this._editableCompartment.of(EditorView.editable.of(true)),
this._readOnlyCompartment.of(EditorState.readOnly.of(false)),
this._accessibilityCompartment.of(EditorView.contentAttributes.of({
...(accessibility?.label ? { 'aria-label': accessibility.label } : {}),
...(accessibility?.description ? { 'aria-description': accessibility.description } : {})
})),
this._accessibilityCompartment.of(EditorView.contentAttributes.of(
this._contentAttributes(this._accessibility, false)
)),
syntaxHighlighting(defaultHighlightStyle, { fallback: true }),
lineNumbers(),
history(),
Expand Down Expand Up @@ -111,21 +112,25 @@ export class CodeEditor {
if (!this._view) return
const editableCompartment = this._editableCompartment
const readOnlyCompartment = this._readOnlyCompartment
if (!editableCompartment || !readOnlyCompartment) return
const accessibilityCompartment = this._accessibilityCompartment
if (!editableCompartment || !readOnlyCompartment || !accessibilityCompartment) return
this._view.dispatch({
effects: [
editableCompartment.reconfigure(EditorView.editable.of(!readOnly)),
readOnlyCompartment.reconfigure(EditorState.readOnly.of(readOnly))
readOnlyCompartment.reconfigure(EditorState.readOnly.of(readOnly)),
accessibilityCompartment.reconfigure(EditorView.contentAttributes.of(
this._contentAttributes(this._accessibility, readOnly)
))
]
})
}

setAccessibility (accessibility: { label?: string, description?: string }) {
if (!this._view || !this._accessibilityCompartment) return
this._accessibility = accessibility
this._view.dispatch({
effects: this._accessibilityCompartment.reconfigure(EditorView.contentAttributes.of({
...(accessibility.label ? { 'aria-label': accessibility.label } : {}),
...(accessibility.description ? { 'aria-description': accessibility.description } : {})
...this._contentAttributes(accessibility, this._view.state.readOnly)
}))
})
}
Expand All @@ -134,6 +139,17 @@ export class CodeEditor {
this._view?.focus()
}

private _contentAttributes (
accessibility: { label?: string, description?: string },
readOnly: boolean
): Record<string, string> {
return {
...(accessibility.label ? { 'aria-label': accessibility.label } : {}),
...(accessibility.description ? { 'aria-description': accessibility.description } : {}),
...(readOnly ? { tabindex: '0' } : {})
}
}

async setLanguage (contentType: string) {
if (!this._view) return
const languageCompartment = this._languageCompartment
Expand Down
60 changes: 59 additions & 1 deletion test/unit/codeEditor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,14 @@ describe('CodeEditor', () => {
expect(view.dispatch).toHaveBeenLastCalledWith({
effects: [
{ type: 'reconfigure', value: { type: 'editable', value: false } },
{ type: 'reconfigure', value: { type: 'readOnly', value: true } }
{ type: 'reconfigure', value: { type: 'readOnly', value: true } },
{
type: 'reconfigure',
value: {
type: 'contentAttributes',
value: { tabindex: '0' }
}
}
]
})

Expand Down Expand Up @@ -265,4 +272,55 @@ describe('CodeEditor', () => {
}
})
})

it('keeps the read-only editing surface keyboard focusable without making it editable', async () => {
const editor = new CodeEditor()
const container = document.createElement('div')
document.body.appendChild(container)

await editor.initialize(container, 'content', 'text/plain', 'dark', undefined, {
label: 'Source editor for https://pod.example/file.txt',
description: 'Read-only source content.'
})
editor.setReadOnly(true)

expect(createdViews[0].dispatch).toHaveBeenLastCalledWith({
effects: [
{ type: 'reconfigure', value: { type: 'editable', value: false } },
{ type: 'reconfigure', value: { type: 'readOnly', value: true } },
{
type: 'reconfigure',
value: {
type: 'contentAttributes',
value: {
'aria-label': 'Source editor for https://pod.example/file.txt',
'aria-description': 'Read-only source content.',
tabindex: '0'
}
}
}
]
})

// The mock does not apply read-only reconfiguration to its state.
createdViews[0].state.readOnly = true
editor.setAccessibility({
label: 'Updated source editor',
description: 'Updated read-only source content.'
})

expect(createdViews[0].dispatch).toHaveBeenLastCalledWith({
effects: {
type: 'reconfigure',
value: {
type: 'contentAttributes',
value: {
'aria-label': 'Updated source editor',
'aria-description': 'Updated read-only source content.',
tabindex: '0'
}
}
}
})
})
Comment thread
Copilot marked this conversation as resolved.
Resolved
})
Loading