Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save r7kamura/17250b9713b393b0e1be to your computer and use it in GitHub Desktop.
Save r7kamura/17250b9713b393b0e1be to your computer and use it in GitHub Desktop.
import { Rx } from '@cycle/core'
import { h } from '@cycle/dom'
import CodeMirror from 'codemirror'
class CodeMirrorRenderer {
constructor(events) {
this.events = events;
}
hook(node, propertyName, previousValue) {
if (!previousValue) {
this.onRendered(node);
}
}
insertCodeMirrorInto(node) {
const codeMirror = new CodeMirror(node);
Object.keys(this.events).forEach((eventName) => {
codeMirror.on(eventName, (...event) => {
this.events[eventName].onNext(event);
});
});
}
onRendered(node) {
this.insertCodeMirrorInto(node);
}
}
function template({ events }) {
return h('div.editor', { hook: new CodeMirrorRenderer(events) });
}
export default function (responses) {
const events = {
blur: new Rx.Subject(),
changes: new Rx.Subject(),
focus: new Rx.Subject()
};
return {
DOM: Rx.Observable.just(template({ events })),
events
};
}
import { makeDOMDriver } from '@cycle/dom';
import Cycle, { Rx } from '@cycle/core'
import editor from './widgets/editor'
import intent from './intent'
import model from './model'
import view from './view'
Cycle.run(
(responses) => view(model(intent(responses))),
{
DOM: makeDOMDriver('body', { editor })
}
);
@r7kamura
Copy link
Author

r7kamura commented Oct 7, 2015

Cycle.jsのcutsom elementsvirtual-domのhooks の仕組みを使って、DOMを描画したときにフックしてCodeMirrorを挿入する。custom elementsを使うことで外部にCustomEventを発行できるので、CodeMirror起因のイベントを配信することで、アプリケーション側でCodeMirrorのイベントを利用して任意の処理を実行できるようになる。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment