Skip to content
This repository has been archived by the owner on Nov 17, 2021. It is now read-only.

Commit

Permalink
fix(JS): Initalize JS even if script is loaded after DOMContentLoaded
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-ketch committed Sep 12, 2019
1 parent 8e050b9 commit 7639b4b
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 13 deletions.
20 changes: 15 additions & 5 deletions src/common/js/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@
* This file is imported by all themes, allowing for shared configurations among themes.
*/

import './syntaxHighlight'
import { codeHighlight } from './syntaxHighlight'

export const init = (): void => {
export const formatReferences = (): void => {
const referenceListItemSel = '.references > li'
const titleSel = 'span[itemprop="headline"]'
const datePublishedSel = '[itemprop="datePublished"]'
const publicationIssueSel =
'[itemtype="https://schema.org/PublicationIssue"]'
const publicationIssueSel = '[itemtype="https://schema.org/PublicationIssue"]'

document.querySelectorAll(referenceListItemSel).forEach(node => {
const datePublished = node.querySelector(datePublishedSel) as HTMLElement
Expand Down Expand Up @@ -50,4 +49,15 @@ export const init = (): void => {
})
}

document.addEventListener('DOMContentLoaded', init)
const onReadyHandler = (): void => {
codeHighlight()
formatReferences()
}

export const load = (): void => {
if (document.readyState !== 'loading') {
onReadyHandler()
}

document.addEventListener('DOMContentLoaded', onReadyHandler)
}
8 changes: 5 additions & 3 deletions src/common/js/syntaxHighlight.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import Prism from 'prismjs'
import 'prismjs/components/prism-javascript'
import 'prismjs/components/prism-json'
import 'prismjs/components/prism-markdown'
import 'prismjs/components/prism-markup'
import 'prismjs/components/prism-python'
import 'prismjs/components/prism-r'
import 'prismjs/plugins/line-highlight/prism-line-highlight'
import 'prismjs/plugins/line-numbers/prism-line-numbers'

const ready = (): void => {
export const codeHighlight = (): void => {
document
.querySelectorAll('pre[class*="language-"]')
.forEach(node => node.classList.add('line-numbers'))

Prism.highlightAll()
}

document.addEventListener('DOMContentLoaded', ready)
10 changes: 9 additions & 1 deletion src/examples/simple.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ <h5>Title 5</h5>
<h6>Title 6</h6>

<stencila-codechunk data-collapsed="false" data-execution_count="4">
<pre slot="code"><code>import time
<pre slot="code"><code class="lang-python">import time
time.sleep(10)</code></pre>
<figure slot="outputs">
<img src="https://via.placeholder.com/350x500" />
Expand All @@ -56,6 +56,14 @@ <h6>Title 6</h6>
<li>Item number 8</li>
</ul>

<stencila-codechunk data-collapsed="false" data-execution_count="4">
<pre slot="code"><code class="lang-python">import time
time.sleep(10)</code></pre>
<figure slot="outputs">
<img src="https://via.placeholder.com/350x500" />
</figure>
</stencila-codechunk>

<pre slot="code"><code>import time
time.sleep(10)</code></pre>
</body>
Expand Down
2 changes: 1 addition & 1 deletion src/themes/eLife/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { init } from '../../common/js/index'
export { load } from '../../common/js/index'
2 changes: 1 addition & 1 deletion src/themes/nature/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { init } from '../../common/js/index'
export { load } from '../../common/js/index'
5 changes: 3 additions & 2 deletions src/themes/stencila/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export const init = (): void => {
}
import { load } from '../../common/js/index'

load()

0 comments on commit 7639b4b

Please sign in to comment.