Skip to content

Commit

Permalink
Update CodeMirrorModifier
Browse files Browse the repository at this point in the history
  • Loading branch information
darashi committed Mar 9, 2024
1 parent 74b0075 commit 0888860
Showing 1 changed file with 20 additions and 37 deletions.
57 changes: 20 additions & 37 deletions frontend/app/modifiers/code-mirror.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { action } from '@ember/object';
import { bind } from '@ember/runloop';
import codemirror from 'codemirror';
import Modifier from 'ember-modifier';

Expand All @@ -8,41 +6,26 @@ import 'codemirror/mode/markdown/markdown';
import 'codemirror/mode/sparql/sparql';

export default class CodeMirrorModifier extends Modifier {
didInstall() {
this._setup();
}

didUpdateArguments() {
if (this._editor.getValue() !== this.args.named.content) {
this._editor.setValue(this.args.named.content);
didSetup = false;

modify(element, _, { content, onUpdate }) {
if (!this.didSetup) {
const editor = codemirror(element, {
lineNumbers: true,
matchBrackets: true,
mode: 'markdown',
readOnly: false,
styleActiveLine: true,
theme: 'base16-light',
value: content || '',
viewportMargin: Infinity,
});

editor.on('change', () => {
onUpdate(editor.getValue());
});

this.didSetup = true;
}

this._editor.setOption('readOnly', this.args.named.readOnly);
}

@action
_onChange(editor) {
this.args.named.onUpdate(editor.getValue());
}

_setup() {
if (!this.element) {
throw new Error('CodeMirror modifier has no element');
}

const editor = codemirror(this.element, {
lineNumbers: true,
matchBrackets: true,
mode: 'markdown',
readOnly: this.args.named.readOnly,
styleActiveLine: true,
theme: 'base16-light',
value: this.args.named.content || '',
viewportMargin: Infinity,
});

editor.on('change', bind(this, this._onChange));

this._editor = editor;
}
}

0 comments on commit 0888860

Please sign in to comment.