Skip to content
This repository has been archived by the owner on Jun 15, 2021. It is now read-only.

Bring Scribe Common into Scribe #287

Merged
merged 6 commits into from
Nov 7, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 1 addition & 2 deletions Plumbing.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ module.exports = function (pipelines) {
// FIXME: auto?
preserveLicenseComments: false,
paths: {
'lodash-amd': '../bower_components/lodash-amd',
'scribe-common': '../bower_components/scribe-common'
'lodash-amd': '../bower_components/lodash-amd'
}
});

Expand Down
3 changes: 1 addition & 2 deletions bower.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
{
"name": "scribe",
"dependencies": {
"lodash-amd": "2.4.1",
"scribe-common": "0.0.11"
"lodash-amd": "2.4.1"
},
"devDependencies": {
"requirejs": "~2.1.9",
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
"version": "0.1.25",
"main": "src/scribe.js",
"dependencies": {
"lodash-amd": "~2.4.1",
"scribe-common": "~0.0.11"
"lodash-amd": "~2.4.1"
},
"devDependencies": {
"chai": "~1.9.1",
Expand Down
2 changes: 1 addition & 1 deletion src/api/selection.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
define([
'scribe-common/src/element'
'../element'
],
function (elementHelper) {

Expand Down
4 changes: 2 additions & 2 deletions src/dom-observer.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
define([
'lodash-amd/modern/arrays/flatten',
'lodash-amd/modern/collections/toArray',
'scribe-common/src/element',
'scribe-common/src/node'
'./element',
'./node'
], function (
flatten,
toArray,
Expand Down
34 changes: 34 additions & 0 deletions src/element.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
define(['lodash-amd/modern/collections/contains'], function (contains) {

'use strict';

// TODO: not exhaustive?
var blockElementNames = ['P', 'LI', 'DIV', 'BLOCKQUOTE', 'UL', 'OL', 'H1',
'H2', 'H3', 'H4', 'H5', 'H6', 'TABLE', 'TH', 'TD'];
function isBlockElement(node) {
return contains(blockElementNames, node.nodeName);
}

function isSelectionMarkerNode(node) {
return (node.nodeType === Node.ELEMENT_NODE && node.className === 'scribe-marker');
}

function isCaretPositionNode(node) {
return (node.nodeType === Node.ELEMENT_NODE && node.className === 'caret-position');
}

function unwrap(node, childNode) {
while (childNode.childNodes.length > 0) {
node.insertBefore(childNode.childNodes[0], childNode);
}
node.removeChild(childNode);
}

return {
isBlockElement: isBlockElement,
isSelectionMarkerNode: isSelectionMarkerNode,
isCaretPositionNode: isCaretPositionNode,
unwrap: unwrap
};

});
23 changes: 23 additions & 0 deletions src/node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
define([], function () {

'use strict';

function isEmptyTextNode(node) {
return (node.nodeType === Node.TEXT_NODE && node.textContent === '');
}

function insertAfter(newNode, referenceNode) {
return referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
}

function removeNode(node) {
return node.parentNode.removeChild(node);
}

return {
isEmptyTextNode: isEmptyTextNode,
insertAfter: insertAfter,
removeNode: removeNode
};

});
24 changes: 11 additions & 13 deletions src/plugins/core/formatters/html/enforce-p-elements.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
define([
'lodash-amd/modern/arrays/last',
'scribe-common/src/element'
'lodash-amd/modern/arrays/last'
], function (
last,
element
last
) {

/**
Expand All @@ -26,15 +24,15 @@ define([
/**
* Wrap consecutive inline elements and text nodes in a P element.
*/
function wrapChildNodes(parentNode) {
function wrapChildNodes(scribe, parentNode) {
var groups = Array.prototype.reduce.call(parentNode.childNodes,
function (accumulator, binChildNode) {
var group = last(accumulator);
if (! group) {
startNewGroup();
} else {
var isBlockGroup = element.isBlockElement(group[0]);
if (isBlockGroup === element.isBlockElement(binChildNode)) {
var isBlockGroup = scribe.element.isBlockElement(group[0]);
if (isBlockGroup === scribe.element.isBlockElement(binChildNode)) {
group.push(binChildNode);
} else {
startNewGroup();
Expand All @@ -50,7 +48,7 @@ define([
}, []);

var consecutiveInlineElementsAndTextNodes = groups.filter(function (group) {
var isBlockGroup = element.isBlockElement(group[0]);
var isBlockGroup = scribe.element.isBlockElement(group[0]);
return ! isBlockGroup;
});

Expand All @@ -66,7 +64,7 @@ define([
}

// Traverse the tree, wrapping child nodes as we go.
function traverse(parentNode) {
function traverse(scribe, parentNode) {
var treeWalker = document.createTreeWalker(parentNode, NodeFilter.SHOW_ELEMENT);
var node = treeWalker.firstChild();

Expand All @@ -76,8 +74,8 @@ define([
// TODO: At the moment we only support BLOCKQUOTEs. See failing
// tests.
if (node.nodeName === 'BLOCKQUOTE' && ! node._isWrapped) {
wrapChildNodes(node);
traverse(parentNode);
wrapChildNodes(scribe, node);
traverse(scribe, parentNode);
break;
}
node = treeWalker.nextSibling();
Expand All @@ -100,8 +98,8 @@ define([
var bin = document.createElement('div');
bin.innerHTML = html;

wrapChildNodes(bin);
traverse(bin);
wrapChildNodes(scribe, bin);
traverse(scribe, bin);

return bin.innerHTML;
});
Expand Down
12 changes: 6 additions & 6 deletions src/plugins/core/formatters/html/ensure-selectable-containers.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
define([
'scribe-common/src/element',
'../../../../element',
'lodash-amd/modern/collections/contains'
], function (
element,
Expand All @@ -17,7 +17,7 @@ define([
// http://www.w3.org/TR/html-markup/syntax.html#syntax-elements
var html5VoidElements = ['AREA', 'BASE', 'BR', 'COL', 'COMMAND', 'EMBED', 'HR', 'IMG', 'INPUT', 'KEYGEN', 'LINK', 'META', 'PARAM', 'SOURCE', 'TRACK', 'WBR'];

function parentHasNoTextContent(node) {
function parentHasNoTextContent(element, node) {
if (element.isCaretPositionNode(node)) {
return true;
} else {
Expand All @@ -26,7 +26,7 @@ define([
}


function traverse(parentNode) {
function traverse(element, parentNode) {
// Instead of TreeWalker, which gets confused when the BR is added to the dom,
// we recursively traverse the tree to look for an empty node that can have childNodes

Expand All @@ -41,7 +41,7 @@ define([

// Do not insert BR in empty non block elements with parent containing text
if (!element.isBlockElement(node) && node.children.length === 0) {
return parentHasNoTextContent(node);
return parentHasNoTextContent(element, node);
}

return false;
Expand All @@ -56,7 +56,7 @@ define([
!contains(html5VoidElements, node.nodeName)) {
node.appendChild(document.createElement('br'));
} else if (node.children.length > 0) {
traverse(node);
traverse(element, node);
}
}
node = node.nextElementSibling;
Expand All @@ -70,7 +70,7 @@ define([
var bin = document.createElement('div');
bin.innerHTML = html;

traverse(bin);
traverse(scribe.element, bin);

return bin.innerHTML;
});
Expand Down
3 changes: 2 additions & 1 deletion src/plugins/core/patches/commands/insert-html.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
define(['scribe-common/src/element'], function (element) {
define([], function () {

'use strict';

return function () {
return function (scribe) {
var insertHTMLCommandPatch = new scribe.api.CommandPatch('insertHTML');
var element = scribe.element;

insertHTMLCommandPatch.execute = function (value) {
scribe.transactionManager.run(function () {
Expand Down
6 changes: 4 additions & 2 deletions src/plugins/core/patches/commands/insert-list.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
define(['scribe-common/src/element',
'scribe-common/src/node'], function (element, nodeHelpers) {
define([], function () {

'use strict';

return function () {
return function (scribe) {
var element = scribe.element;
var nodeHelpers = scribe.node;

var InsertListCommandPatch = function (commandName) {
scribe.api.CommandPatch.call(this, commandName);
};
Expand Down
5 changes: 4 additions & 1 deletion src/plugins/core/patches/events.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
define(['scribe-common/src/element'], function (element) {
define([], function () {

'use strict';

Expand All @@ -21,6 +21,9 @@ define(['scribe-common/src/element'], function (element) {
// TODO: run in a transaction so as to record the change? how do
// we know in advance whether there will be a change though?
// TODO: share somehow with `InsertList` command

var element = scribe.element;

if (scribe.allowsBlockElements()) {
scribe.el.addEventListener('keyup', function (event) {
if (event.keyCode === 8 || event.keyCode === 46) { // backspace or delete
Expand Down
11 changes: 9 additions & 2 deletions src/scribe.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ define([
'./api',
'./transaction-manager',
'./undo-manager',
'./event-emitter'
'./event-emitter',
'./element',
'./node'
], function (
defaults,
flatten,
Expand All @@ -29,7 +31,9 @@ define([
Api,
buildTransactionManager,
buildUndoManager,
EventEmitter
EventEmitter,
elementHelpers,
nodeHelpers
) {

'use strict';
Expand All @@ -49,6 +53,9 @@ define([

this.api = new Api(this);

this.node = nodeHelpers;
this.element = elementHelpers;

var TransactionManager = buildTransactionManager(this);
this.transactionManager = new TransactionManager();

Expand Down