Skip to content

Commit

Permalink
support for pasting markup
Browse files Browse the repository at this point in the history
  • Loading branch information
gpoitch committed Feb 8, 2015
1 parent 0a21ae7 commit bf5b57f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 55 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
41 changes: 22 additions & 19 deletions src/js/editor/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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;
});
}

Expand Down Expand Up @@ -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);
Expand Down
34 changes: 0 additions & 34 deletions src/js/utils/paste-utils.js

This file was deleted.

0 comments on commit bf5b57f

Please sign in to comment.