-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add plot accessibility switcher (#150)
- Loading branch information
1 parent
16db768
commit bba9a32
Showing
7 changed files
with
216 additions
and
4 deletions.
There are no files selected for viewing
72 changes: 72 additions & 0 deletions
72
compiler/src/mdast/__test__/plot-accessibility-switcher.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { | ||
testProcessor, | ||
unindentString, | ||
} from '../../test-utils/test-processor'; | ||
|
||
describe('plot accessibility switcher', () => { | ||
it('should show the plot accessibility switcher correctly', async () => { | ||
const { html } = await testProcessor(` | ||
::::plot-accessibility-switcher | ||
:::visualisation | ||
The visualisation | ||
::: | ||
:::purpose | ||
Why does this plot exist? | ||
::: | ||
:::description | ||
What is on this plot? | ||
::: | ||
:::: | ||
`); | ||
|
||
const expected = unindentString(` | ||
<div class="plot-accessibility-switcher"> | ||
<ul> | ||
<li data-plot-accessibility="visualisation">Visualisation</li> | ||
<li data-plot-accessibility="purpose">Purpose</li> | ||
<li data-plot-accessibility="description">Description</li> | ||
</ul> | ||
<div data-plot-accessibility="visualisation" class="plot-accessibility"> | ||
<p>The visualisation</p> | ||
</div> | ||
<div data-plot-accessibility="purpose" class="plot-accessibility"> | ||
<p>Why does this plot exist?</p> | ||
</div> | ||
<div data-plot-accessibility="description" class="plot-accessibility"> | ||
<p>What is on this plot?</p> | ||
</div> | ||
</div> | ||
`); | ||
|
||
expect(html).toBe(expected); | ||
}); | ||
|
||
// it('should only show cli', async () => { | ||
// const md = ` | ||
// ::::program-switcher | ||
// :::command-line | ||
// I am cli | ||
// ::: | ||
|
||
// :::github-desktop | ||
// I am github desktop | ||
// ::: | ||
// :::: | ||
// `; | ||
// const { html } = await testProcessor(md, { | ||
// envProgram: 'command-line', | ||
// }); | ||
|
||
// const expected = unindentString(` | ||
// <div class="program-switcher"> | ||
// <div data-program="command-line" class="program show"> | ||
// <p>I am cli</p> | ||
// </div> | ||
// </div> | ||
// `); | ||
|
||
// expect(html).toBe(expected); | ||
// }); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { Element, ElementContent } from 'hast'; | ||
import { ContainerDirective } from 'mdast-util-directive'; | ||
import { toHast } from 'mdast-util-to-hast'; | ||
import { Node } from 'unist'; | ||
import { visit } from 'unist-util-visit'; | ||
|
||
import { Context } from '../context'; | ||
|
||
const tabs = ['visualisation', 'purpose', 'description']; | ||
const titles = ['Visualisation', 'Purpose', 'Description']; | ||
|
||
export function plotAccessibilitySwitcher(ctx: Context) { | ||
return (tree: Node) => { | ||
visit(tree, 'containerDirective', (node: ContainerDirective) => { | ||
if (node.name === 'plot-accessibility-switcher') { | ||
node.data = { | ||
hProperties: { | ||
className: 'plot-accessibility-switcher', | ||
}, | ||
hChildren: [processMenu(node), ...processChildren(node)], | ||
}; | ||
} | ||
}); | ||
}; | ||
} | ||
|
||
function processMenu(parent: ContainerDirective): ElementContent { | ||
const children = parent.children as ContainerDirective[]; | ||
return { | ||
type: 'element', | ||
tagName: 'ul', | ||
properties: {}, | ||
children: children.map((node) => { | ||
const element: ElementContent = { | ||
type: 'element', | ||
tagName: 'li', | ||
properties: { | ||
'data-plot-accessibility': node.name, | ||
}, | ||
children: [ | ||
{ | ||
type: 'text', | ||
value: titles[tabs.indexOf(node.name)], | ||
}, | ||
], | ||
}; | ||
return element; | ||
}), | ||
}; | ||
} | ||
|
||
function processChildren(parent: ContainerDirective): ElementContent[] { | ||
const children = parent.children.map((node) => { | ||
const parent = node as ContainerDirective; | ||
if (tabs.includes(parent.name)) { | ||
node.data = { | ||
hProperties: { | ||
'data-plot-accessibility': parent.name, | ||
className: ['plot-accessibility'], | ||
}, | ||
}; | ||
} | ||
return node; | ||
}); | ||
|
||
const parentHast = toHast({ | ||
type: 'root', | ||
children, | ||
}) as Element; | ||
|
||
return parentHast.children; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { saveState } from '../util'; | ||
import { state } from './state'; | ||
|
||
document.documentElement.classList.add( | ||
`plot-accessibility-${state.plotAccessibility}`, | ||
); | ||
|
||
document | ||
.querySelectorAll('.plot-accessibility-switcher > ul > li') | ||
.forEach((elem) => { | ||
const element = elem as HTMLElement; | ||
console.log( | ||
element.dataset.plotAccessibility, | ||
state.plotAccessibility, | ||
); | ||
if (element.dataset.plotAccessibility === state.plotAccessibility) { | ||
element.classList.add('active'); | ||
} | ||
element.addEventListener('click', setProgram); | ||
}); | ||
|
||
document | ||
.querySelectorAll('.plot-accessibility-switcher > .plot-accessibility') | ||
.forEach((elem) => { | ||
const element = elem as HTMLElement; | ||
if (element.dataset.plotAccessibility === state.plotAccessibility) { | ||
element.classList.add('show'); | ||
} | ||
}); | ||
|
||
function setProgram(e: Event) { | ||
const target = e.currentTarget as HTMLElement; | ||
const plotAccessibility = target.dataset.plotAccessibility as string; | ||
const newClass = `plot-accessibility-${plotAccessibility}`; | ||
|
||
if (!document.documentElement.classList.contains(newClass)) { | ||
const oldClass = `plot-accessibility-${state.plotAccessibility}`; | ||
document.documentElement.classList.replace(oldClass, newClass); | ||
state.plotAccessibility = plotAccessibility; | ||
saveState('environment', state); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { getSavedState } from '../util'; | ||
|
||
export type State = { | ||
plotAccessibility: string; | ||
}; | ||
|
||
export const defaultState: State = Object.freeze({ | ||
plotAccessibility: 'visualisation', | ||
}); | ||
|
||
export const state = getSavedState('environment', defaultState); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters