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

Commit

Permalink
Use new scribe-common helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
robinedman committed Jun 17, 2014
1 parent dc86bf5 commit 32dcb69
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 21 deletions.
3 changes: 2 additions & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"name": "scribe",
"dependencies": {
"lodash-amd": "2.4.1"
"lodash-amd": "2.4.1",
"scribe-common": "0.0.4"
},
"devDependencies": {
"requirejs": "~2.1.9",
Expand Down
21 changes: 8 additions & 13 deletions src/dom-observer.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
define([
'lodash-amd/modern/arrays/flatten',
'lodash-amd/modern/collections/toArray'
'lodash-amd/modern/collections/toArray',
'scribe-common/element',
'scribe-common/node'
], function (
flatten,
toArray
toArray,
elementHelpers,
nodeHelpers
) {

function observeDomChanges(el, callback) {
function notEmptyTextNode(node) {
return ! (node.nodeType === Node.TEXT_NODE && node.textContent === '');
}

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

function includeRealMutations(mutations) {
var allChangedNodes = flatten(mutations.map(function(mutation) {
var added = toArray(mutation.addedNodes);
Expand All @@ -23,13 +19,12 @@ define([
}));

var realChangedNodes = allChangedNodes.
filter(notEmptyTextNode).
filter(notSelectionMarkerNode);
filter(function(n) { return ! nodeHelpers.isEmptyTextNode(n); }).
filter(function(n) { return ! elementHelpers.isSelectionMarkerNode(n); });

return realChangedNodes.length > 0;
}


// Flag to avoid running recursively
var runningPostMutation = false;
var observer = new MutationObserver(function(mutations) {
Expand Down
22 changes: 15 additions & 7 deletions src/plugins/core/patches/commands/insert-list.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
define(['../../../../api/element'], function (element) {
define(['../../../../api/element',
'scribe-common/node'], function (element, nodeHelpers) {

'use strict';

Expand Down Expand Up @@ -31,7 +32,7 @@ define(['../../../../api/element'], function (element) {

if (listElement.nextElementSibling &&
listElement.nextElementSibling.childNodes.length === 0) {
listElement.parentNode.removeChild(listElement.nextElementSibling);
nodeHelpers.removeNode(listElement.nextElementSibling);
}

/**
Expand All @@ -46,13 +47,20 @@ define(['../../../../api/element'], function (element) {
if (listParentNode && /^(H[1-6]|P)$/.test(listParentNode.nodeName)) {
selection.placeMarkers();
// Move listElement out of the block
listParentNode.parentNode.insertBefore(listElement, listParentNode.nextElementSibling);
nodeHelpers.insertAfter(listElement, listParentNode);
selection.selectMarkers();

/**
* Chrome 27-34: An empty text node is inserted.
*/
if (listParentNode.childNodes.length === 2 &&
nodeHelpers.isEmptyTextNode(listParentNode.firstChild)) {
nodeHelpers.removeNode(listParentNode);
}

// Remove the block if it's empty
if (listParentNode.childNodes.length === 0
|| (listParentNode.childNodes.length === 1 && listParentNode.firstChild.nodeName === 'BR')
|| (listParentNode.firstChild.nodeType === Node.TEXT_NODE && listParentNode.firstChild.textContent === '')) {
listParentNode.parentNode.removeChild(listParentNode);
if (listParentNode.childNodes.length === 0) {
nodeHelpers.removeNode(listParentNode);
}
}
}
Expand Down

0 comments on commit 32dcb69

Please sign in to comment.