From 1b5d54ae43336867bb1abf89766e3b5e1c567958 Mon Sep 17 00:00:00 2001 From: Patrick Dubroy Date: Wed, 17 Sep 2014 15:57:53 +0200 Subject: [PATCH] Re-organize the editor into a more self-contained object. --- editor/editor.js | 106 +++++++++++++++++++---------------- editor/plugins/metadata.html | 2 +- lib/moonchild-bundle.js | 37 ++++++------ moonchild.coffee | 27 +++++---- 4 files changed, 97 insertions(+), 75 deletions(-) diff --git a/editor/editor.js b/editor/editor.js index 9593c3e..3cb5bdd 100644 --- a/editor/editor.js +++ b/editor/editor.js @@ -1,48 +1,20 @@ -function $(sel) { return document.querySelector(sel); } -function $$(sel) { return document.querySelectorAll(sel); } - -var nodes = {}; -var codeMirror = CodeMirror.fromTextArea($('textarea')); -codeMirror.on('change', _.debounce(editorOnChange, 250)); - var moonchild = Moonchild.registerExtension(); +Moonchild.setEditor(new Editor()); + var options = {}; -function editorOnChange(cm, changeObj) { - Moonchild.onChange(cm.getValue()); -} +var codeMirror; // TODO: Get rid of this global. -function render(node) { - var widgetInfo = moonchild.getWidget(node); - if (widgetInfo) - renderWidget(codeMirror, node, widgetInfo); - else - clearWidgets(codeMirror, node); -} +// Private helpers +// --------------- -moonchild.on('render', function(ast, comments) { - ast.each(render); - comments.each(render); -}); +function $(sel) { return document.querySelector(sel); } +function $$(sel) { return document.querySelectorAll(sel); } -// Highlights the text of the given AST node in the editor. -function highlightNode(node) { - var marker = markNodeText(codeMirror, node, { className: 'highlight' }); - setTimeout(function() { - marker.clear(); - }, 1000); +function editorOnChange(cm, changeObj) { + Moonchild.onChange(cm.getValue()); } -codeMirror.on('mousedown', function(cm, e) { - var classList = e.target.classList; - if (classList.contains('mc-metadata')) { - var id = _.find(classList, function(className) { - return className.indexOf('mc-node-') == 0; - }); - highlightNode(nodes[id]); - } -}); - function toggle(el, value) { if (value !== undefined) el.classList.toggle('on', value); @@ -51,15 +23,55 @@ function toggle(el, value) { options[el.id] = el.classList.contains('on'); } -var controls = $$('#controls > div'); -for (var i = 0; i < controls.length; i++) { - controls[i].addEventListener('click', function(e) { - toggle(this); - e.preventDefault(); - if (this.id == 'all') { - for (var j = 0; j < controls.length; j++) - toggle(controls[j], this.classList.contains('on')); - } - Moonchild.onChange(codeMirror.getValue()); +function initializeExtensionToggles(cm) { + var controls = $$('#controls > div'); + for (var i = 0; i < controls.length; i++) { + controls[i].addEventListener('click', function(e) { + toggle(this); + e.preventDefault(); + if (this.id == 'all') { + for (var j = 0; j < controls.length; j++) + toggle(controls[j], this.classList.contains('on')); + } + Moonchild.onChange(cm.getValue()); + }); + } +} + +function renderNode(cm, node) { + var widgetInfo = moonchild.getWidget(node); + if (widgetInfo) + renderWidget(cm, node, widgetInfo); + else + clearWidgets(cm, node); +} + +// Editor +// ------ + +function Editor() { + codeMirror = CodeMirror.fromTextArea($('textarea')); + codeMirror.on('change', _.debounce(editorOnChange, 250)); + + var render = _.partial(renderNode, codeMirror); + moonchild.on('render', function(ast, comments) { + ast.each(render); + comments.each(render); }); + + codeMirror.on('cursorActivity', function(cm, e) { + var adjacentMarks = cm.findMarksAt(cm.getCursor()); + if (adjacentMarks.length == 0 || !adjacentMarks[0].replacedWith) + return; + + var markEl = widgetForMark(adjacentMarks[0]); + var widgetType = markEl._moonchildWidgetType; + if (widgetType && widgetType.editable) + console.log(markEl); + }); + + initializeExtensionToggles(codeMirror); +} + +Editor.prototype.replaceRange = function(text, fromOffset, toOffset) { } diff --git a/editor/plugins/metadata.html b/editor/plugins/metadata.html index 70b791b..2d80181 100644 --- a/editor/plugins/metadata.html +++ b/editor/plugins/metadata.html @@ -7,6 +7,7 @@