Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use @chordbook/editor #682

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
6 changes: 3 additions & 3 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ <h1 class="Header__site-name">
</li>
</ul>
<div class="EditorContainer">
<div class="ChordSheetEditor LineNumbers EditorContainer__lineNumbers" id="chordSheetEditor__lineNumbers" data-mode="text"></div>
<textarea class="ChordSheetEditor EditorContainer__editor" id="chordSheetEditor" data-mode="text">
<div class="ChordSheetEditor EditorContainer__editor" id="chordSheetEditor" data-mode="text"><script type="text/template">
{title: Let it be}
{subtitle: ChordSheetJS example version}
{key: C}
Expand All @@ -88,9 +87,10 @@ <h1 class="Header__site-name">
Let it [Am]be, let it [C/G]be, let it [F]be, let it [C]be
[C]Whisper words of [G]wisdom, let it [F]be [C/E] [Dm] [C]
{end_of_chorus}
</textarea>
</script></textarea>
</div>
<div class="EditorContainer__errorMessage" id="chordSheetEditor__errorMessage"></div>
<small>Powered by <a href="https://github.com/chordbook/editor">@chordbook/editor</a></small>
</section>
<section class="App__column" id="chordSheetViewer">
<ul class="RadioGroup">
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"githubHome": "https://github.com/martijnversluis/ChordFiddle",
"about": "https://github.com/martijnversluis/ChordFiddle/blob/master/README.md#chordfiddle",
"dependencies": {
"@chordbook/editor": "^0.0.7",
"chordjs": "^2.0.1",
"chordsheetjs": "^7.17.0",
"lz-string": "^1.5.0",
Expand Down
4 changes: 2 additions & 2 deletions src/index.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@
{{/each}}
</ul>
<div class="EditorContainer">
<div class="ChordSheetEditor LineNumbers EditorContainer__lineNumbers" id="chordSheetEditor__lineNumbers" data-mode="text"></div>
<textarea class="ChordSheetEditor EditorContainer__editor" id="chordSheetEditor" data-mode="text">{{example_chord_pro_sheet}}</textarea>
<div class="ChordSheetEditor EditorContainer__editor" id="chordSheetEditor" data-mode="text"><script type="text/template">{{example_chord_pro_sheet}}</script></textarea>
</div>
<div class="EditorContainer__errorMessage" id="chordSheetEditor__errorMessage"></div>
<small>Powered by <a href="https://github.com/chordbook/editor">@chordbook/editor</a></small>
</section>
<section class="App__column" id="chordSheetViewer">
<ul class="RadioGroup">
Expand Down
4 changes: 1 addition & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function onChordSheetChange(chordSheet) {
chordSheetViewer.render(song);
chordSheetEditor.resetError();
} catch ({ message, location }) {
chordSheetEditor.showError(message, location.start.line);
chordSheetEditor.showError(message, location);
}
}

Expand Down Expand Up @@ -58,5 +58,3 @@ if (chordSheet) {
} else {
onChordSheetChange(chordSheetEditor.getValue());
}

chordSheetEditor.updateLineNumbers();
82 changes: 31 additions & 51 deletions src/js/chord_sheet_editor.js
Original file line number Diff line number Diff line change
@@ -1,86 +1,66 @@
import { createEditor } from '@chordbook/editor';
import { EditorSelection } from '@codemirror/state';
import { linter, setDiagnostics } from '@codemirror/lint';
import Component from './component';
import valueWithHistory from './value_with_history';
import debounce from './debounce';

class ChordSheetEditor extends Component {
onChordSheetChange = () => {};

errorLineNumber = valueWithHistory(null);

setup() {
this.container.addEventListener('input', debounce(() => {
this.onChordSheetChange(this.getValue());
this.updateLineNumbers();
}));

this.container.addEventListener('scroll', () => {
this.lineNumbersContainer.scrollTop = this.container.scrollTop;
this.editor = createEditor({
doc: this.container.querySelector('*').innerText,
parent: this.container,
extensions: [
linter(),
],
});
}

get lineNumbersContainer() {
return this.element('lineNumbers');
}

updateLineNumbers() {
const lineNumbers = this.lineNumbersContainer.innerText.split('\n').filter((t) => t);
const editorLineCount = this.getValue().split('\n').length;
const previousErrorLine = this.errorLineNumber.valueWas;
const newErrorLine = this.errorLineNumber.value;

if (previousErrorLine) {
lineNumbers[previousErrorLine - 1] = `${previousErrorLine}`;
}

if (newErrorLine) {
lineNumbers[newErrorLine - 1] = `<div class="LineNumbers__error">${newErrorLine}</div>`;
}

if (lineNumbers.length > editorLineCount) {
lineNumbers.splice(editorLineCount);
}

while (lineNumbers.length < editorLineCount) {
lineNumbers.push(`${lineNumbers.length + 1}`);
}

this.lineNumbersContainer.innerHTML = lineNumbers.join('<br>');
this.container.addEventListener('change', (e) => {
this.onChordSheetChange(e.detail.doc);
});
}

getSelectionRange() {
const { selectionStart, selectionEnd } = this.container;
return [selectionStart, selectionEnd];
const { from, to } = this.editor.state.selection.main;
return [from, to];
}

setSelectionRange(selectionStart, selectionEnd) {
this.container.setSelectionRange(selectionStart, selectionEnd);
this.editor.dispatch({
selection: EditorSelection.create([
EditorSelection.range(selectionStart, selectionEnd),
EditorSelection.cursor(selectionEnd),
]),
});
}

focus() {
this.container.focus();
this.editor.focus();
}

getValue() {
return this.container.value;
return this.editor.state.doc.toString();
}

setValue(value) {
this.container.value = value;
this.updateLineNumbers();
this.editor.dispatch({ changes: { from: 0, to: this.editor.state.doc.length, insert: value } });
}

setError(error) {
this.element('errorMessage').innerText = error;
}

showError(message, line) {
this.setError(`Line ${line}: ${message}`);
this.errorLineNumber.set(line);
showError(message, location) {
this.editor.dispatch(setDiagnostics(this.editor.state, [{
from: location.start.offset,
to: location.end.offset,
severity: 'error',
message,
}]));
}

resetError() {
this.setError('');
this.errorLineNumber.set(null);
this.editor.dispatch(setDiagnostics(this.editor.state, []));
}

transformChordSheet(transformationFunc) {
Expand Down
23 changes: 0 additions & 23 deletions src/sass/ChordSheetEditor.sass

This file was deleted.

21 changes: 1 addition & 20 deletions src/sass/EditorContainer.sass
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,7 @@
$editor-padding: $spacing-m
$editor-space-between: $spacing-m

display: flex
flex-direction: row
flex: 1 1 auto
max-height: 100%
overflow: hidden

&__editor
$border-color: $theme-background

border-left: 1px $border-color solid
flex: 1 0 auto

&__lineNumbers
$right-padding: 28px
$scrollbar-correction: 20px
$width: 65px

flex: 0 0 ($width + $scrollbar-correction)
margin-right: -1 * $scrollbar-correction
padding-right: $right-padding + $scrollbar-correction
overflow: auto

&__errorMessage
background: red
Expand Down
1 change: 0 additions & 1 deletion src/sass/main.sass
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
@import 'button'

@import 'App'
@import 'ChordSheetEditor'
@import 'ChordSheetViewer'
@import 'EditorContainer'
@import 'Header'
Expand Down
Loading