Skip to content

Commit 564011b

Browse files
committed
Add stimulus controller for syntax highlight preview
Controller depends on name value of syntax highlight and for link tags to have a data attribute specifying its name. On connect, controller will look for all link tags with a syntax highlight data attribute and will disabled non-current links which will ensure the browser disables the corresponding CSS.
1 parent 84c7150 commit 564011b

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

app/javascript/controllers/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import AnalyticsCustomEvent from './analytics/custom-event';
1616
import TableOfContents from './table-of-contents';
1717

1818
import FrameForm from './forms/frame';
19+
import SyntaxHighlightPreview from './syntax-highlight/preview';
1920

2021
application.register('analytics', AnalyticsCustomEvent);
2122
application.register('code-example', CodeExample);
@@ -28,3 +29,4 @@ application.register('pwa-web-push-demo', PwaWebPushDemo);
2829
application.register('table-of-contents', TableOfContents);
2930

3031
application.register('frame-form', FrameForm);
32+
application.register('syntax-highlight-preview', SyntaxHighlightPreview);
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { Controller } from '@hotwired/stimulus';
2+
import debug from '../../utils/debug';
3+
4+
const console = debug('app:javascript:controllers:syntax-highlight:preview');
5+
6+
export default class extends Controller {
7+
static values = {
8+
name: String,
9+
};
10+
11+
connect() {
12+
console.log('connect', this.nameValue);
13+
const name = this.nameValue;
14+
document
15+
.querySelectorAll('link[rel="stylesheet"][data-syntax-highlight]')
16+
.forEach((link) => {
17+
console.log('link', link, {
18+
disabling: name !== link.dataset.syntaxHighlight,
19+
});
20+
link.disabled = name !== link.dataset.syntaxHighlight;
21+
});
22+
}
23+
}

0 commit comments

Comments
 (0)