diff --git a/package.json b/package.json index fbfac6983..32555ddbe 100644 --- a/package.json +++ b/package.json @@ -27,8 +27,8 @@ "express": "^4.11.1" }, "devDependencies": { - "content-kit-utils": "0.1.0", - "content-kit-compiler": "0.1.1", + "content-kit-utils": "0.1.1", + "content-kit-compiler": "0.1.2", "del": "^1.1.1", "gulp": "^3.8.10", "gulp-concat": "~2.1.7", diff --git a/src/js/editor/editor.js b/src/js/editor/editor.js index 07ba63751..a1c26d5eb 100644 --- a/src/js/editor/editor.js +++ b/src/js/editor/editor.js @@ -13,9 +13,8 @@ import OrderedListCommand from '../commands/ordered-list'; import ImageCommand from '../commands/image'; import OEmbedCommand from '../commands/oembed'; import Keycodes from '../utils/keycodes'; -import { getSelectionBlockElement, getSelectionBlockTagName, getCursorOffsetInElement } from '../utils/selection-utils'; +import { getSelectionBlockElement, getCursorOffsetInElement } from '../utils/selection-utils'; import EventEmitter from '../utils/event-emitter'; -import { cleanPastedContent } from '../utils/paste-utils'; import Compiler from 'node_modules/content-kit-compiler/src/compiler'; import Type from 'node_modules/content-kit-compiler/src/types/type'; import { toArray } from 'node_modules/content-kit-utils/src/array-utils'; @@ -53,28 +52,32 @@ var defaults = { function bindContentEditableTypingListeners(editor) { // Correct some contentEditable woes before reparsing editor.element.addEventListener('keyup', function(e) { - if((!e.shiftKey && e.which === Keycodes.ENTER) || (e.ctrlKey && e.which === Keycodes.M)) { - // On a carrage return, make sure it always generates a 'p' tag - var selectionTag = getSelectionBlockTagName(); - if (!selectionTag || selectionTag === Type.QUOTE.tag) { - document.execCommand('formatBlock', false, Type.PARAGRAPH.tag); - } - }// else if (e.which === Keycodes.BKSP) { - // TODO: need to rerender when backspacing 2 blocks together - //} - // Assure there is always a supported block tag, and not empty text nodes or divs. - if (!getSelectionBlockElement()) { + // On a carrage return, make sure to always generate a 'p' tag + if (!getSelectionBlockElement() || + !editor.element.textContent || + (!e.shiftKey && e.which === Keycodes.ENTER) || (e.ctrlKey && e.which === Keycodes.M)) { document.execCommand('formatBlock', false, Type.PARAGRAPH.tag); - } + } //else if (e.which === Keycodes.BKSP) { + // TODO: Need to rerender when backspacing 2 blocks together + //var cursorIndex = editor.getCursorIndexInCurrentBlock(); + //var currentBlockElement = getSelectionBlockElement(); + //editor.renderBlockAt(editor.getCurrentBlockIndex(), true); + //setCursorIndexInElement(currentBlockElement, cursorIndex); + //} }); - // On 'PASTE' convert content to blocks and insert + // On 'PASTE' sanitize and insert editor.element.addEventListener('paste', function(e) { - var cleanedContent = cleanPastedContent(e, Type.PARAGRAPH.tag); - if (cleanedContent) { - document.execCommand('insertHTML', false, cleanedContent); + var data = e.clipboardData; + var pastedHTML = data && data.getData && data.getData('text/html'); + var sanitizedHTML = pastedHTML && editor.compiler.rerender(pastedHTML); + if (sanitizedHTML) { + document.execCommand('insertHTML', false, sanitizedHTML); + editor.syncVisual(); } + e.preventDefault(); + return false; }); } @@ -227,7 +230,7 @@ Editor.prototype.getCurrentBlockIndex = function(element) { return blockElements.indexOf(selectionEl); }; -Editor.prototype.getCurrentCursorIndex = function() { +Editor.prototype.getCursorIndexInCurrentBlock = function() { var currentBlock = getSelectionBlockElement(); if (currentBlock) { return getCursorOffsetInElement(currentBlock); diff --git a/src/js/utils/paste-utils.js b/src/js/utils/paste-utils.js deleted file mode 100644 index b66e8daed..000000000 --- a/src/js/utils/paste-utils.js +++ /dev/null @@ -1,34 +0,0 @@ - -var RegExpNewLine = /[\r\n]/g; - -function plainTextToBlocks(plainText, tag) { - var blocks = plainText.split(RegExpNewLine); - var len = blocks.length; - var block, openTag, closeTag, content, i; - - if(len < 2) { - return plainText; - } else { - content = ''; - openTag = '<' + tag + '>'; - closeTag = ''; - for(i = 0; i < len; ++i) { - block = blocks[i]; - if(block !== '') { - content += openTag + block + closeTag; - } - } - return content; - } -} - -function cleanPastedContent(event, defaultBlockTag) { - event.preventDefault(); - var data = event.clipboardData, plainText; - if(data && data.getData) { - plainText = data.getData('text/plain'); - return plainTextToBlocks(plainText, defaultBlockTag); - } -} - -export { cleanPastedContent };