Skip to content

Commit bae24d3

Browse files
committed
Spike snippet editor stimulus
1 parent 900be21 commit bae24d3

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

app/javascript/controllers/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import TableOfContents from './table-of-contents';
1818
import FrameForm from './forms/frame';
1919
import SyntaxHighlightPreview from './syntax-highlight/preview';
2020
import SnippetPreview from './snippets/preview';
21+
import SnippetEditor from './snippets/editor';
2122

2223
application.register('analytics', AnalyticsCustomEvent);
2324
application.register('code-example', CodeExample);
@@ -31,4 +32,5 @@ application.register('table-of-contents', TableOfContents);
3132

3233
application.register('frame-form', FrameForm);
3334
application.register('snippet-preview', SnippetPreview);
35+
application.register('snippet-editor', SnippetEditor);
3436
application.register('syntax-highlight-preview', SyntaxHighlightPreview);
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { Controller } from '@hotwired/stimulus';
2+
3+
import debug from '../../utils/debug';
4+
5+
const console = debug('app:javascript:controllers:snippets:editor');
6+
7+
export default class extends Controller {
8+
static targets = ['previewButton', 'source', 'textarea'];
9+
10+
declare previewButtonTarget: HTMLButtonElement;
11+
declare sourceTarget: HTMLElement;
12+
declare textareaTarget: HTMLTextAreaElement;
13+
14+
connect() {
15+
console.log('Stimulus controller connected');
16+
this.showCode();
17+
this.syncTextarea();
18+
// this.textareaTarget.addEventListener('input', this.autoGrow.bind(this));
19+
// this.textareaTarget.addEventListener(
20+
// 'blur',
21+
// this.handleTextareaBlur.bind(this),
22+
// );
23+
}
24+
25+
disconnect() {
26+
// this.textareaTarget.removeEventListener('input', this.autoGrow.bind(this));
27+
// this.textareaTarget.removeEventListener(
28+
// 'blur',
29+
// this.handleTextareaBlur.bind(this),
30+
// );
31+
}
32+
33+
preview() {
34+
this.previewButtonTarget.click();
35+
}
36+
37+
showTextarea() {
38+
this.sourceTarget.style.display = 'none';
39+
this.textareaTarget.style.display = 'block';
40+
this.textareaTarget.focus();
41+
this.autoGrow();
42+
}
43+
44+
syncTextarea() {
45+
this.textareaTarget.value = this.sourceTarget.textContent || '';
46+
this.autoGrow();
47+
}
48+
49+
showCode() {
50+
this.sourceTarget.style.display = 'block';
51+
this.textareaTarget.style.display = 'none';
52+
}
53+
54+
autoGrow() {
55+
this.textareaTarget.style.height = 'auto';
56+
this.textareaTarget.style.height = this.textareaTarget.scrollHeight + 'px';
57+
}
58+
}

0 commit comments

Comments
 (0)