From e59eaf7f9cf5fb4b33ea2f58daf30d18c2a3a416 Mon Sep 17 00:00:00 2001 From: Matthew Beale Date: Mon, 6 Jul 2015 15:52:27 -0400 Subject: [PATCH] Update editor to parse the DOM to the post AT --- src/js/editor/editor.js | 65 ++++++++++++++++++++++++---- src/js/renderers/new-dom-renderer.js | 29 +++++-------- src/js/utils/element-map.js | 30 +++++++++++++ 3 files changed, 98 insertions(+), 26 deletions(-) create mode 100644 src/js/utils/element-map.js diff --git a/src/js/editor/editor.js b/src/js/editor/editor.js index ab7e6a329..76f881456 100644 --- a/src/js/editor/editor.js +++ b/src/js/editor/editor.js @@ -22,6 +22,7 @@ import { } from 'content-kit-compiler'; import { toArray, merge, mergeWithOptions } from 'content-kit-utils'; import { win, doc } from 'content-kit-editor/utils/compat'; +import ElementMap from "../utils/element-map"; var defaults = { placeholder: 'Write here...', @@ -51,6 +52,17 @@ var defaults = { cards: {} }; +function replaceInArray(array, original, replacement) { + var i, l, possibleOriginal; + for (i=0,l=array.length;i { + var selection = document.getSelection(); + if (selection.rangeCount) { + var range = selection.getRangeAt(0); + if (range.collapsed) { + var element = range.startContainer; + var sectionElement, section; + while (element) { + section = editor.sectionElementMap.get(element); + if (section) { + sectionElement = element; + break; + } + element = element.parentNode; + } + + if (!sectionElement) { + throw new Error('There is not section element for the previous edit'); + } + + var previousSectionElement; + if (sectionElement && sectionElement.previousSibling) { + previousSectionElement = sectionElement.previousSibling; + } + + var newSection = editor.compiler.parseSection( + previousSectionElement.dataset.section, + sectionElement.firstChild + ); + + // FIXME: This would benefit from post being a linked-list of sections + replaceInArray(editor.model.sections, section, newSection); + editor.sectionElementMap.set(sectionElement, newSection); + editor.trigger('update'); + return; + } + } editor.syncContentEditableBlocks(); }); } @@ -178,6 +226,8 @@ function Editor(element, options) { }); } + this.sectionElementMap = new ElementMap(); + if (element) { applyClassName(element); applyPlaceholder(element, editor.placeholder); @@ -188,7 +238,11 @@ function Editor(element, options) { if (editor.model) { editor.loadModel(editor.model); } else { - editor.sync(); + this.syncModel(); + while (element.childNodes.length) { + element.childNodes[0].remove(); + } + this.syncVisual(); } bindContentEditableTypingListeners(editor); @@ -219,12 +273,7 @@ Editor.prototype.syncModel = function() { }; Editor.prototype.syncVisual = function() { - this.compiler.render(this.model, this.element); -}; - -Editor.prototype.sync = function() { - this.syncModel(); - this.syncVisual(); + this.compiler.render(this.model, this.sectionElementMap, this.element); }; Editor.prototype.getCurrentBlockIndex = function(element) { diff --git a/src/js/renderers/new-dom-renderer.js b/src/js/renderers/new-dom-renderer.js index 72658696c..a1fb30ccd 100644 --- a/src/js/renderers/new-dom-renderer.js +++ b/src/js/renderers/new-dom-renderer.js @@ -1,11 +1,5 @@ -function renderHeadline(doc, text) { - var element = doc.createElement('h2'); - element.textContent = text; - return element; -} - -function renderParagraph(doc, types, markers) { - var element = doc.createElement('p'); +function renderMarkupSection(doc, section, markers) { + var element = doc.createElement(section.tagName); var elements = [element]; var currentElement = element; var i, l, j, m, marker, openTypes, closeTypes, text; @@ -13,9 +7,9 @@ function renderParagraph(doc, types, markers) { var openedElement, openedTagName; for (i=0, l=markers.length;i